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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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-namedialog.hpp"
  23. #include "qt-wrappers.hpp"
  24. using namespace std;
  25. void EnumSceneCollections(std::function<bool (const char *, const char *)> &&cb)
  26. {
  27. char path[512];
  28. os_glob_t *glob;
  29. int ret = GetConfigPath(path, sizeof(path),
  30. "obs-studio/basic/scenes/*.json");
  31. if (ret <= 0) {
  32. blog(LOG_WARNING, "Failed to get config path for scene "
  33. "collections");
  34. return;
  35. }
  36. if (os_glob(path, 0, &glob) != 0) {
  37. blog(LOG_WARNING, "Failed to glob scene collections");
  38. return;
  39. }
  40. for (size_t i = 0; i < glob->gl_pathc; i++) {
  41. const char *filePath = glob->gl_pathv[i].path;
  42. if (glob->gl_pathv[i].directory)
  43. continue;
  44. obs_data_t *data = obs_data_create_from_json_file_safe(filePath,
  45. "bak");
  46. std::string name = obs_data_get_string(data, "name");
  47. /* if no name found, use the file name as the name
  48. * (this only happens when switching to the new version) */
  49. if (name.empty()) {
  50. name = strrchr(filePath, '/') + 1;
  51. name.resize(name.size() - 5);
  52. }
  53. obs_data_release(data);
  54. if (!cb(name.c_str(), filePath))
  55. break;
  56. }
  57. os_globfree(glob);
  58. }
  59. static bool SceneCollectionExists(const char *findName)
  60. {
  61. bool found = false;
  62. auto func = [&](const char *name, const char*)
  63. {
  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, const char *oldName = nullptr)
  75. {
  76. bool rename = oldName != nullptr;
  77. const char *title;
  78. const char *text;
  79. char path[512];
  80. size_t len;
  81. int ret;
  82. if (rename) {
  83. title = Str("Basic.Main.RenameSceneCollection.Title");
  84. text = Str("Basic.Main.AddSceneCollection.Text");
  85. } else {
  86. title = Str("Basic.Main.AddSceneCollection.Title");
  87. text = Str("Basic.Main.AddSceneCollection.Text");
  88. }
  89. for (;;) {
  90. bool success = NameDialog::AskForName(parent, title, text,
  91. name, QT_UTF8(oldName));
  92. if (!success) {
  93. return false;
  94. }
  95. if (name.empty()) {
  96. OBSMessageBox::information(parent,
  97. QTStr("NoNameEntered.Title"),
  98. QTStr("NoNameEntered.Text"));
  99. continue;
  100. }
  101. if (SceneCollectionExists(name.c_str())) {
  102. OBSMessageBox::information(parent,
  103. QTStr("NameExists.Title"),
  104. QTStr("NameExists.Text"));
  105. continue;
  106. }
  107. break;
  108. }
  109. if (!GetFileSafeName(name.c_str(), file)) {
  110. blog(LOG_WARNING, "Failed to create safe file name for '%s'",
  111. name.c_str());
  112. return false;
  113. }
  114. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes/");
  115. if (ret <= 0) {
  116. blog(LOG_WARNING, "Failed to get scene collection config path");
  117. return false;
  118. }
  119. len = file.size();
  120. file.insert(0, path);
  121. if (!GetClosestUnusedFileName(file, "json")) {
  122. blog(LOG_WARNING, "Failed to get closest file name for %s",
  123. file.c_str());
  124. return false;
  125. }
  126. file.erase(file.size() - 5, 5);
  127. file.erase(0, file.size() - len);
  128. return true;
  129. }
  130. void OBSBasic::AddSceneCollection(bool create_new)
  131. {
  132. std::string name;
  133. std::string file;
  134. if (!GetSceneCollectionName(this, name, file))
  135. return;
  136. SaveProjectNow();
  137. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  138. name.c_str());
  139. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  140. file.c_str());
  141. if (create_new) {
  142. CreateDefaultScene(false);
  143. }
  144. SaveProjectNow();
  145. RefreshSceneCollections();
  146. blog(LOG_INFO, "Added scene collection '%s' (%s, %s.json)",
  147. name.c_str(), create_new ? "clean" : "duplicate",
  148. file.c_str());
  149. blog(LOG_INFO, "------------------------------------------------");
  150. UpdateTitleBar();
  151. if (api) {
  152. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  153. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  154. }
  155. }
  156. void OBSBasic::RefreshSceneCollections()
  157. {
  158. QList<QAction*> menuActions = ui->sceneCollectionMenu->actions();
  159. int count = 0;
  160. for (int i = 0; i < menuActions.count(); i++) {
  161. QVariant v = menuActions[i]->property("file_name");
  162. if (v.typeName() != nullptr)
  163. delete menuActions[i];
  164. }
  165. const char *cur_name = config_get_string(App()->GlobalConfig(),
  166. "Basic", "SceneCollection");
  167. auto addCollection = [&](const char *name, const char *path)
  168. {
  169. std::string file = strrchr(path, '/') + 1;
  170. file.erase(file.size() - 5, 5);
  171. QAction *action = new QAction(QT_UTF8(name), this);
  172. action->setProperty("file_name", QT_UTF8(path));
  173. connect(action, &QAction::triggered,
  174. this, &OBSBasic::ChangeSceneCollection);
  175. action->setCheckable(true);
  176. action->setChecked(strcmp(name, cur_name) == 0);
  177. ui->sceneCollectionMenu->addAction(action);
  178. count++;
  179. return true;
  180. };
  181. EnumSceneCollections(addCollection);
  182. /* force saving of first scene collection on first run, otherwise
  183. * no scene collections will show up */
  184. if (!count) {
  185. long prevDisableVal = disableSaving;
  186. disableSaving = 0;
  187. SaveProjectNow();
  188. disableSaving = prevDisableVal;
  189. EnumSceneCollections(addCollection);
  190. }
  191. ui->actionRemoveSceneCollection->setEnabled(count > 1);
  192. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  193. main->ui->actionPasteFilters->setEnabled(false);
  194. main->ui->actionPasteRef->setEnabled(false);
  195. main->ui->actionPasteDup->setEnabled(false);
  196. }
  197. void OBSBasic::on_actionNewSceneCollection_triggered()
  198. {
  199. AddSceneCollection(true);
  200. }
  201. void OBSBasic::on_actionDupSceneCollection_triggered()
  202. {
  203. AddSceneCollection(false);
  204. }
  205. void OBSBasic::on_actionRenameSceneCollection_triggered()
  206. {
  207. std::string name;
  208. std::string file;
  209. std::string oldFile = config_get_string(App()->GlobalConfig(),
  210. "Basic", "SceneCollectionFile");
  211. const char *oldName = config_get_string(App()->GlobalConfig(),
  212. "Basic", "SceneCollection");
  213. bool success = GetSceneCollectionName(this, name, file, oldName);
  214. if (!success)
  215. return;
  216. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  217. name.c_str());
  218. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  219. file.c_str());
  220. SaveProjectNow();
  221. char path[512];
  222. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  223. if (ret <= 0) {
  224. blog(LOG_WARNING, "Failed to get scene collection config path");
  225. return;
  226. }
  227. oldFile.insert(0, path);
  228. oldFile += ".json";
  229. os_unlink(oldFile.c_str());
  230. oldFile += ".bak";
  231. os_unlink(oldFile.c_str());
  232. blog(LOG_INFO, "------------------------------------------------");
  233. blog(LOG_INFO, "Renamed scene collection to '%s' (%s.json)",
  234. name.c_str(), file.c_str());
  235. blog(LOG_INFO, "------------------------------------------------");
  236. UpdateTitleBar();
  237. RefreshSceneCollections();
  238. if (api) {
  239. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  240. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  241. }
  242. }
  243. void OBSBasic::on_actionRemoveSceneCollection_triggered()
  244. {
  245. std::string newName;
  246. std::string newPath;
  247. std::string oldFile = config_get_string(App()->GlobalConfig(),
  248. "Basic", "SceneCollectionFile");
  249. std::string oldName = config_get_string(App()->GlobalConfig(),
  250. "Basic", "SceneCollection");
  251. auto cb = [&](const char *name, const char *filePath)
  252. {
  253. if (strcmp(oldName.c_str(), name) != 0) {
  254. newName = name;
  255. newPath = filePath;
  256. return false;
  257. }
  258. return true;
  259. };
  260. EnumSceneCollections(cb);
  261. /* this should never be true due to menu item being grayed out */
  262. if (newPath.empty())
  263. return;
  264. QString text = QTStr("ConfirmRemove.Text");
  265. text.replace("$1", QT_UTF8(oldName.c_str()));
  266. QMessageBox::StandardButton button = OBSMessageBox::question(this,
  267. QTStr("ConfirmRemove.Title"), text);
  268. if (button == QMessageBox::No)
  269. return;
  270. char path[512];
  271. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  272. if (ret <= 0) {
  273. blog(LOG_WARNING, "Failed to get scene collection config path");
  274. return;
  275. }
  276. oldFile.insert(0, path);
  277. oldFile += ".json";
  278. os_unlink(oldFile.c_str());
  279. oldFile += ".bak";
  280. os_unlink(oldFile.c_str());
  281. Load(newPath.c_str());
  282. RefreshSceneCollections();
  283. const char *newFile = config_get_string(App()->GlobalConfig(),
  284. "Basic", "SceneCollectionFile");
  285. blog(LOG_INFO, "Removed scene collection '%s' (%s.json), "
  286. "switched to '%s' (%s.json)",
  287. oldName.c_str(), oldFile.c_str(),
  288. newName.c_str(), newFile);
  289. blog(LOG_INFO, "------------------------------------------------");
  290. UpdateTitleBar();
  291. if (api) {
  292. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  293. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  294. }
  295. }
  296. void OBSBasic::on_actionImportSceneCollection_triggered()
  297. {
  298. char path[512];
  299. QString qhome = QDir::homePath();
  300. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  301. if (ret <= 0) {
  302. blog(LOG_WARNING, "Failed to get scene collection config path");
  303. return;
  304. }
  305. QString qfilePath = QFileDialog::getOpenFileName(
  306. this,
  307. QTStr("Basic.MainMenu.SceneCollection.Import"),
  308. qhome,
  309. "JSON Files (*.json)");
  310. QFileInfo finfo(qfilePath);
  311. QString qfilename = finfo.fileName();
  312. QString qpath = QT_UTF8(path);
  313. QFileInfo destinfo(QT_UTF8(path) + qfilename);
  314. if (!qfilePath.isEmpty() && !qfilePath.isNull()) {
  315. string absPath = QT_TO_UTF8(finfo.absoluteFilePath());
  316. OBSData scenedata =
  317. obs_data_create_from_json_file(absPath.c_str());
  318. obs_data_release(scenedata);
  319. string origName = obs_data_get_string(scenedata, "name");
  320. string name = origName;
  321. string file;
  322. int inc = 1;
  323. while (SceneCollectionExists(name.c_str())) {
  324. name = origName + " (" + to_string(++inc) + ")";
  325. }
  326. obs_data_set_string(scenedata, "name", name.c_str());
  327. if (!GetFileSafeName(name.c_str(), file)) {
  328. blog(LOG_WARNING, "Failed to create "
  329. "safe file name for '%s'",
  330. name.c_str());
  331. return;
  332. }
  333. string filePath = path + file;
  334. if (!GetClosestUnusedFileName(filePath, "json")) {
  335. blog(LOG_WARNING, "Failed to get "
  336. "closest file name for %s",
  337. file.c_str());
  338. return;
  339. }
  340. obs_data_save_json_safe(scenedata, filePath.c_str(),
  341. "tmp", "bak");
  342. RefreshSceneCollections();
  343. }
  344. }
  345. void OBSBasic::on_actionExportSceneCollection_triggered()
  346. {
  347. char path[512];
  348. QString home = QDir::homePath();
  349. QString currentFile = QT_UTF8(config_get_string(App()->GlobalConfig(),
  350. "Basic", "SceneCollectionFile"));
  351. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  352. if (ret <= 0) {
  353. blog(LOG_WARNING, "Failed to get scene collection config path");
  354. return;
  355. }
  356. QString exportFile = QFileDialog::getSaveFileName(
  357. this,
  358. QTStr("Basic.MainMenu.SceneCollection.Export"),
  359. home + "/" + currentFile,
  360. "JSON Files (*.json)");
  361. string file = QT_TO_UTF8(exportFile);
  362. if (!exportFile.isEmpty() && !exportFile.isNull()) {
  363. if (QFile::exists(exportFile))
  364. QFile::remove(exportFile);
  365. QFile::copy(path + currentFile + ".json", exportFile);
  366. }
  367. }
  368. void OBSBasic::ChangeSceneCollection()
  369. {
  370. QAction *action = reinterpret_cast<QAction*>(sender());
  371. std::string fileName;
  372. if (!action)
  373. return;
  374. fileName = QT_TO_UTF8(action->property("file_name").value<QString>());
  375. if (fileName.empty())
  376. return;
  377. const char *oldName = config_get_string(App()->GlobalConfig(),
  378. "Basic", "SceneCollection");
  379. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  380. action->setChecked(true);
  381. return;
  382. }
  383. SaveProjectNow();
  384. Load(fileName.c_str());
  385. RefreshSceneCollections();
  386. const char *newName = config_get_string(App()->GlobalConfig(),
  387. "Basic", "SceneCollection");
  388. const char *newFile = config_get_string(App()->GlobalConfig(),
  389. "Basic", "SceneCollectionFile");
  390. blog(LOG_INFO, "Switched to scene collection '%s' (%s.json)",
  391. newName, newFile);
  392. blog(LOG_INFO, "------------------------------------------------");
  393. UpdateTitleBar();
  394. if (api)
  395. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  396. }