1
0

window-basic-main-scene-collections.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /******************************************************************************
  2. Copyright (C) 2015 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <obs.hpp>
  15. #include <util/util.hpp>
  16. #include <QMessageBox>
  17. #include <QVariant>
  18. #include <QFileDialog>
  19. #include <QStandardPaths>
  20. #include "item-widget-helpers.hpp"
  21. #include "window-basic-main.hpp"
  22. #include "window-importer.hpp"
  23. #include "window-namedialog.hpp"
  24. #include "qt-wrappers.hpp"
  25. using namespace std;
  26. void EnumSceneCollections(std::function<bool(const char *, const char *)> &&cb)
  27. {
  28. char path[512];
  29. os_glob_t *glob;
  30. int ret = GetConfigPath(path, sizeof(path),
  31. "obs-studio/basic/scenes/*.json");
  32. if (ret <= 0) {
  33. blog(LOG_WARNING, "Failed to get config path for scene "
  34. "collections");
  35. return;
  36. }
  37. if (os_glob(path, 0, &glob) != 0) {
  38. blog(LOG_WARNING, "Failed to glob scene collections");
  39. return;
  40. }
  41. for (size_t i = 0; i < glob->gl_pathc; i++) {
  42. const char *filePath = glob->gl_pathv[i].path;
  43. if (glob->gl_pathv[i].directory)
  44. continue;
  45. obs_data_t *data =
  46. obs_data_create_from_json_file_safe(filePath, "bak");
  47. std::string name = obs_data_get_string(data, "name");
  48. /* if no name found, use the file name as the name
  49. * (this only happens when switching to the new version) */
  50. if (name.empty()) {
  51. name = strrchr(filePath, '/') + 1;
  52. name.resize(name.size() - 5);
  53. }
  54. obs_data_release(data);
  55. if (!cb(name.c_str(), filePath))
  56. break;
  57. }
  58. os_globfree(glob);
  59. }
  60. bool SceneCollectionExists(const char *findName)
  61. {
  62. bool found = false;
  63. auto func = [&](const char *name, const char *) {
  64. if (strcmp(name, findName) == 0) {
  65. found = true;
  66. return false;
  67. }
  68. return true;
  69. };
  70. EnumSceneCollections(func);
  71. return found;
  72. }
  73. static bool GetSceneCollectionName(QWidget *parent, std::string &name,
  74. std::string &file,
  75. const char *oldName = nullptr)
  76. {
  77. bool rename = oldName != nullptr;
  78. const char *title;
  79. const char *text;
  80. if (rename) {
  81. title = Str("Basic.Main.RenameSceneCollection.Title");
  82. text = Str("Basic.Main.AddSceneCollection.Text");
  83. } else {
  84. title = Str("Basic.Main.AddSceneCollection.Title");
  85. text = Str("Basic.Main.AddSceneCollection.Text");
  86. }
  87. for (;;) {
  88. bool success = NameDialog::AskForName(parent, title, text, name,
  89. QT_UTF8(oldName));
  90. if (!success) {
  91. return false;
  92. }
  93. if (name.empty()) {
  94. OBSMessageBox::warning(parent,
  95. QTStr("NoNameEntered.Title"),
  96. QTStr("NoNameEntered.Text"));
  97. continue;
  98. }
  99. if (SceneCollectionExists(name.c_str())) {
  100. OBSMessageBox::warning(parent,
  101. QTStr("NameExists.Title"),
  102. QTStr("NameExists.Text"));
  103. continue;
  104. }
  105. break;
  106. }
  107. if (!GetUnusedSceneCollectionFile(name, file)) {
  108. return false;
  109. }
  110. return true;
  111. }
  112. bool OBSBasic::AddSceneCollection(bool create_new, const QString &qname)
  113. {
  114. std::string name;
  115. std::string file;
  116. if (qname.isEmpty()) {
  117. if (!GetSceneCollectionName(this, name, file))
  118. return false;
  119. } else {
  120. name = QT_TO_UTF8(qname);
  121. if (SceneCollectionExists(name.c_str()))
  122. return false;
  123. if (!GetUnusedSceneCollectionFile(name, file)) {
  124. return false;
  125. }
  126. }
  127. auto new_collection = [this, create_new](const std::string &file,
  128. const std::string &name) {
  129. SaveProjectNow();
  130. config_set_string(App()->GlobalConfig(), "Basic",
  131. "SceneCollection", name.c_str());
  132. config_set_string(App()->GlobalConfig(), "Basic",
  133. "SceneCollectionFile", file.c_str());
  134. if (create_new) {
  135. CreateDefaultScene(false);
  136. }
  137. SaveProjectNow();
  138. RefreshSceneCollections();
  139. };
  140. new_collection(file, name);
  141. auto undo = [this, oldName = name](const std::string &data) {
  142. std::string newPath;
  143. auto cb = [&](const char *name, const char *filePath) {
  144. if (strcmp(oldName.c_str(), name) != 0) {
  145. newPath = filePath;
  146. return false;
  147. }
  148. return true;
  149. };
  150. EnumSceneCollections(cb);
  151. char path[512];
  152. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  153. if (ret <= 0) {
  154. blog(LOG_WARNING,
  155. "Failed to get scene collection config path");
  156. return;
  157. }
  158. std::string file = path + data + ".json";
  159. os_unlink(file.c_str());
  160. file += ".bak";
  161. os_unlink(file.c_str());
  162. Load(newPath.c_str());
  163. RefreshSceneCollections();
  164. };
  165. auto redo = [new_collection, file, name](const std::string &) {
  166. new_collection(file, name);
  167. };
  168. undo_s.add_action(QTStr("Undo.Add").arg(name.c_str()), undo, redo, file,
  169. "", NULL);
  170. blog(LOG_INFO, "Added scene collection '%s' (%s, %s.json)",
  171. name.c_str(), create_new ? "clean" : "duplicate", file.c_str());
  172. blog(LOG_INFO, "------------------------------------------------");
  173. UpdateTitleBar();
  174. if (api) {
  175. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  176. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  177. }
  178. return true;
  179. }
  180. void OBSBasic::RefreshSceneCollections()
  181. {
  182. QList<QAction *> menuActions = ui->sceneCollectionMenu->actions();
  183. int count = 0;
  184. for (int i = 0; i < menuActions.count(); i++) {
  185. QVariant v = menuActions[i]->property("file_name");
  186. if (v.typeName() != nullptr)
  187. delete menuActions[i];
  188. }
  189. const char *cur_name = config_get_string(App()->GlobalConfig(), "Basic",
  190. "SceneCollection");
  191. auto addCollection = [&](const char *name, const char *path) {
  192. std::string file = strrchr(path, '/') + 1;
  193. file.erase(file.size() - 5, 5);
  194. QAction *action = new QAction(QT_UTF8(name), this);
  195. action->setProperty("file_name", QT_UTF8(path));
  196. connect(action, &QAction::triggered, this,
  197. &OBSBasic::ChangeSceneCollection);
  198. action->setCheckable(true);
  199. action->setChecked(strcmp(name, cur_name) == 0);
  200. ui->sceneCollectionMenu->addAction(action);
  201. count++;
  202. return true;
  203. };
  204. EnumSceneCollections(addCollection);
  205. /* force saving of first scene collection on first run, otherwise
  206. * no scene collections will show up */
  207. if (!count) {
  208. long prevDisableVal = disableSaving;
  209. disableSaving = 0;
  210. SaveProjectNow();
  211. disableSaving = prevDisableVal;
  212. EnumSceneCollections(addCollection);
  213. }
  214. ui->actionRemoveSceneCollection->setEnabled(count > 1);
  215. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  216. main->ui->actionPasteFilters->setEnabled(false);
  217. main->ui->actionPasteRef->setEnabled(false);
  218. main->ui->actionPasteDup->setEnabled(false);
  219. }
  220. void OBSBasic::on_actionNewSceneCollection_triggered()
  221. {
  222. AddSceneCollection(true);
  223. }
  224. void OBSBasic::on_actionDupSceneCollection_triggered()
  225. {
  226. AddSceneCollection(false);
  227. }
  228. void OBSBasic::on_actionRenameSceneCollection_triggered()
  229. {
  230. std::string name;
  231. std::string file;
  232. std::string oname;
  233. std::string oldFile = config_get_string(App()->GlobalConfig(), "Basic",
  234. "SceneCollectionFile");
  235. const char *oldName = config_get_string(App()->GlobalConfig(), "Basic",
  236. "SceneCollection");
  237. oname = std::string(oldName);
  238. bool success = GetSceneCollectionName(this, name, file, oldName);
  239. if (!success)
  240. return;
  241. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  242. name.c_str());
  243. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  244. file.c_str());
  245. SaveProjectNow();
  246. char path[512];
  247. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  248. if (ret <= 0) {
  249. blog(LOG_WARNING, "Failed to get scene collection config path");
  250. return;
  251. }
  252. auto undo = [name = oname, file = oldFile, of = file,
  253. this](const std::string &) {
  254. config_set_string(App()->GlobalConfig(), "Basic",
  255. "SceneCollection", name.c_str());
  256. config_set_string(App()->GlobalConfig(), "Basic",
  257. "SceneCollectionFile", file.c_str());
  258. SaveProjectNow();
  259. char path[512];
  260. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  261. if (ret <= 0) {
  262. blog(LOG_WARNING,
  263. "Failed to get scene collection config path");
  264. return;
  265. }
  266. std::string oldFile = of;
  267. oldFile.insert(0, path);
  268. oldFile += ".json";
  269. os_unlink(oldFile.c_str());
  270. oldFile += ".bak";
  271. os_unlink(oldFile.c_str());
  272. UpdateTitleBar();
  273. RefreshSceneCollections();
  274. };
  275. auto redo = [of = oldFile, name, file, this](const std::string &) {
  276. config_set_string(App()->GlobalConfig(), "Basic",
  277. "SceneCollection", name.c_str());
  278. config_set_string(App()->GlobalConfig(), "Basic",
  279. "SceneCollectionFile", file.c_str());
  280. SaveProjectNow();
  281. char path[512];
  282. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  283. if (ret <= 0) {
  284. blog(LOG_WARNING,
  285. "Failed to get scene collection config path");
  286. return;
  287. }
  288. std::string oldFile = of;
  289. oldFile.insert(0, path);
  290. oldFile += ".json";
  291. os_unlink(oldFile.c_str());
  292. oldFile += ".bak";
  293. os_unlink(oldFile.c_str());
  294. UpdateTitleBar();
  295. RefreshSceneCollections();
  296. };
  297. oldFile.insert(0, path);
  298. oldFile += ".json";
  299. os_unlink(oldFile.c_str());
  300. oldFile += ".bak";
  301. os_unlink(oldFile.c_str());
  302. blog(LOG_INFO, "------------------------------------------------");
  303. blog(LOG_INFO, "Renamed scene collection to '%s' (%s.json)",
  304. name.c_str(), file.c_str());
  305. blog(LOG_INFO, "------------------------------------------------");
  306. UpdateTitleBar();
  307. RefreshSceneCollections();
  308. undo_s.add_action(QTStr("Undo.Rename").arg(name.c_str()), undo, redo,
  309. "", "", NULL);
  310. if (api) {
  311. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  312. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  313. }
  314. }
  315. void OBSBasic::on_actionRemoveSceneCollection_triggered()
  316. {
  317. std::string newName;
  318. std::string newPath;
  319. std::string oldFile = config_get_string(App()->GlobalConfig(), "Basic",
  320. "SceneCollectionFile");
  321. std::string oldName = config_get_string(App()->GlobalConfig(), "Basic",
  322. "SceneCollection");
  323. auto cb = [&](const char *name, const char *filePath) {
  324. if (strcmp(oldName.c_str(), name) != 0) {
  325. newName = name;
  326. newPath = filePath;
  327. return false;
  328. }
  329. return true;
  330. };
  331. EnumSceneCollections(cb);
  332. /* this should never be true due to menu item being grayed out */
  333. if (newPath.empty())
  334. return;
  335. QString text = QTStr("ConfirmRemove.Text");
  336. text.replace("$1", QT_UTF8(oldName.c_str()));
  337. QMessageBox::StandardButton button = OBSMessageBox::question(
  338. this, QTStr("ConfirmRemove.Title"), text);
  339. if (button == QMessageBox::No)
  340. return;
  341. char path[512];
  342. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  343. if (ret <= 0) {
  344. blog(LOG_WARNING, "Failed to get scene collection config path");
  345. return;
  346. }
  347. oldFile.insert(0, path);
  348. oldFile += ".json";
  349. obs_data_t *data =
  350. obs_data_create_from_json_file_safe(oldFile.c_str(), "bak");
  351. obs_data_set_string(data, "undo_filename", oldFile.c_str());
  352. auto undo = [this](const std::string &data) {
  353. obs_data_t *dat = obs_data_create_from_json(data.c_str());
  354. LoadData(dat, obs_data_get_string(dat, "undo_filename"));
  355. SaveProjectNow();
  356. RefreshSceneCollections();
  357. obs_data_release(dat);
  358. };
  359. auto redo = [this, of = oldFile, newPath](const std::string &) {
  360. std::string oldFile = of;
  361. os_unlink(oldFile.c_str());
  362. oldFile += ".bak";
  363. os_unlink(oldFile.c_str());
  364. Load(newPath.c_str());
  365. RefreshSceneCollections();
  366. };
  367. std::string undo_data = std::string(obs_data_get_json(data));
  368. undo_s.add_action(QTStr("Undo.Delete").arg(oldName.c_str()), undo, redo,
  369. undo_data, "", NULL);
  370. obs_data_release(data);
  371. os_unlink(oldFile.c_str());
  372. oldFile += ".bak";
  373. os_unlink(oldFile.c_str());
  374. Load(newPath.c_str());
  375. RefreshSceneCollections();
  376. const char *newFile = config_get_string(App()->GlobalConfig(), "Basic",
  377. "SceneCollectionFile");
  378. blog(LOG_INFO,
  379. "Removed scene collection '%s' (%s.json), "
  380. "switched to '%s' (%s.json)",
  381. oldName.c_str(), oldFile.c_str(), newName.c_str(), newFile);
  382. blog(LOG_INFO, "------------------------------------------------");
  383. UpdateTitleBar();
  384. if (api) {
  385. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  386. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  387. }
  388. }
  389. void OBSBasic::on_actionImportSceneCollection_triggered()
  390. {
  391. OBSImporter *imp;
  392. imp = new OBSImporter(this);
  393. imp->exec();
  394. delete imp;
  395. RefreshSceneCollections();
  396. }
  397. void OBSBasic::on_actionExportSceneCollection_triggered()
  398. {
  399. SaveProjectNow();
  400. char path[512];
  401. QString home = QDir::homePath();
  402. QString currentFile = QT_UTF8(config_get_string(
  403. App()->GlobalConfig(), "Basic", "SceneCollectionFile"));
  404. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  405. if (ret <= 0) {
  406. blog(LOG_WARNING, "Failed to get scene collection config path");
  407. return;
  408. }
  409. QString exportFile =
  410. SaveFile(this, QTStr("Basic.MainMenu.SceneCollection.Export"),
  411. home + "/" + currentFile, "JSON Files (*.json)");
  412. string file = QT_TO_UTF8(exportFile);
  413. if (!exportFile.isEmpty() && !exportFile.isNull()) {
  414. if (QFile::exists(exportFile))
  415. QFile::remove(exportFile);
  416. QFile::copy(path + currentFile + ".json", exportFile);
  417. }
  418. }
  419. void OBSBasic::ChangeSceneCollection()
  420. {
  421. QAction *action = reinterpret_cast<QAction *>(sender());
  422. std::string fileName;
  423. if (!action)
  424. return;
  425. fileName = QT_TO_UTF8(action->property("file_name").value<QString>());
  426. if (fileName.empty())
  427. return;
  428. const char *oldName = config_get_string(App()->GlobalConfig(), "Basic",
  429. "SceneCollection");
  430. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  431. action->setChecked(true);
  432. return;
  433. }
  434. SaveProjectNow();
  435. Load(fileName.c_str());
  436. RefreshSceneCollections();
  437. const char *newName = config_get_string(App()->GlobalConfig(), "Basic",
  438. "SceneCollection");
  439. const char *newFile = config_get_string(App()->GlobalConfig(), "Basic",
  440. "SceneCollectionFile");
  441. blog(LOG_INFO, "Switched to scene collection '%s' (%s.json)", newName,
  442. newFile);
  443. blog(LOG_INFO, "------------------------------------------------");
  444. UpdateTitleBar();
  445. if (api)
  446. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  447. }