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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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::warning(parent,
  97. QTStr("NoNameEntered.Title"),
  98. QTStr("NoNameEntered.Text"));
  99. continue;
  100. }
  101. if (SceneCollectionExists(name.c_str())) {
  102. OBSMessageBox::warning(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. bool OBSBasic::AddSceneCollection(bool create_new, const QString &qname)
  131. {
  132. std::string name;
  133. std::string file;
  134. if (qname.isEmpty()) {
  135. if (!GetSceneCollectionName(this, name, file))
  136. return false;
  137. } else {
  138. name = QT_TO_UTF8(qname);
  139. if (SceneCollectionExists(name.c_str()))
  140. return false;
  141. }
  142. SaveProjectNow();
  143. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  144. name.c_str());
  145. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  146. file.c_str());
  147. if (create_new) {
  148. CreateDefaultScene(false);
  149. }
  150. SaveProjectNow();
  151. RefreshSceneCollections();
  152. blog(LOG_INFO, "Added scene collection '%s' (%s, %s.json)",
  153. name.c_str(), create_new ? "clean" : "duplicate",
  154. file.c_str());
  155. blog(LOG_INFO, "------------------------------------------------");
  156. UpdateTitleBar();
  157. if (api) {
  158. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  159. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  160. }
  161. return true;
  162. }
  163. void OBSBasic::RefreshSceneCollections()
  164. {
  165. QList<QAction*> menuActions = ui->sceneCollectionMenu->actions();
  166. int count = 0;
  167. for (int i = 0; i < menuActions.count(); i++) {
  168. QVariant v = menuActions[i]->property("file_name");
  169. if (v.typeName() != nullptr)
  170. delete menuActions[i];
  171. }
  172. const char *cur_name = config_get_string(App()->GlobalConfig(),
  173. "Basic", "SceneCollection");
  174. auto addCollection = [&](const char *name, const char *path)
  175. {
  176. std::string file = strrchr(path, '/') + 1;
  177. file.erase(file.size() - 5, 5);
  178. QAction *action = new QAction(QT_UTF8(name), this);
  179. action->setProperty("file_name", QT_UTF8(path));
  180. connect(action, &QAction::triggered,
  181. this, &OBSBasic::ChangeSceneCollection);
  182. action->setCheckable(true);
  183. action->setChecked(strcmp(name, cur_name) == 0);
  184. ui->sceneCollectionMenu->addAction(action);
  185. count++;
  186. return true;
  187. };
  188. EnumSceneCollections(addCollection);
  189. /* force saving of first scene collection on first run, otherwise
  190. * no scene collections will show up */
  191. if (!count) {
  192. long prevDisableVal = disableSaving;
  193. disableSaving = 0;
  194. SaveProjectNow();
  195. disableSaving = prevDisableVal;
  196. EnumSceneCollections(addCollection);
  197. }
  198. ui->actionRemoveSceneCollection->setEnabled(count > 1);
  199. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  200. main->ui->actionPasteFilters->setEnabled(false);
  201. main->ui->actionPasteRef->setEnabled(false);
  202. main->ui->actionPasteDup->setEnabled(false);
  203. }
  204. void OBSBasic::on_actionNewSceneCollection_triggered()
  205. {
  206. AddSceneCollection(true);
  207. }
  208. void OBSBasic::on_actionDupSceneCollection_triggered()
  209. {
  210. AddSceneCollection(false);
  211. }
  212. void OBSBasic::on_actionRenameSceneCollection_triggered()
  213. {
  214. std::string name;
  215. std::string file;
  216. std::string oldFile = config_get_string(App()->GlobalConfig(),
  217. "Basic", "SceneCollectionFile");
  218. const char *oldName = config_get_string(App()->GlobalConfig(),
  219. "Basic", "SceneCollection");
  220. bool success = GetSceneCollectionName(this, name, file, oldName);
  221. if (!success)
  222. return;
  223. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  224. name.c_str());
  225. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  226. file.c_str());
  227. SaveProjectNow();
  228. char path[512];
  229. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  230. if (ret <= 0) {
  231. blog(LOG_WARNING, "Failed to get scene collection config path");
  232. return;
  233. }
  234. oldFile.insert(0, path);
  235. oldFile += ".json";
  236. os_unlink(oldFile.c_str());
  237. oldFile += ".bak";
  238. os_unlink(oldFile.c_str());
  239. blog(LOG_INFO, "------------------------------------------------");
  240. blog(LOG_INFO, "Renamed scene collection to '%s' (%s.json)",
  241. name.c_str(), file.c_str());
  242. blog(LOG_INFO, "------------------------------------------------");
  243. UpdateTitleBar();
  244. RefreshSceneCollections();
  245. if (api) {
  246. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  247. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  248. }
  249. }
  250. void OBSBasic::on_actionRemoveSceneCollection_triggered()
  251. {
  252. std::string newName;
  253. std::string newPath;
  254. std::string oldFile = config_get_string(App()->GlobalConfig(),
  255. "Basic", "SceneCollectionFile");
  256. std::string oldName = config_get_string(App()->GlobalConfig(),
  257. "Basic", "SceneCollection");
  258. auto cb = [&](const char *name, const char *filePath)
  259. {
  260. if (strcmp(oldName.c_str(), name) != 0) {
  261. newName = name;
  262. newPath = filePath;
  263. return false;
  264. }
  265. return true;
  266. };
  267. EnumSceneCollections(cb);
  268. /* this should never be true due to menu item being grayed out */
  269. if (newPath.empty())
  270. return;
  271. QString text = QTStr("ConfirmRemove.Text");
  272. text.replace("$1", QT_UTF8(oldName.c_str()));
  273. QMessageBox::StandardButton button = OBSMessageBox::question(this,
  274. QTStr("ConfirmRemove.Title"), text);
  275. if (button == QMessageBox::No)
  276. return;
  277. char path[512];
  278. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  279. if (ret <= 0) {
  280. blog(LOG_WARNING, "Failed to get scene collection config path");
  281. return;
  282. }
  283. oldFile.insert(0, path);
  284. oldFile += ".json";
  285. os_unlink(oldFile.c_str());
  286. oldFile += ".bak";
  287. os_unlink(oldFile.c_str());
  288. Load(newPath.c_str());
  289. RefreshSceneCollections();
  290. const char *newFile = config_get_string(App()->GlobalConfig(),
  291. "Basic", "SceneCollectionFile");
  292. blog(LOG_INFO, "Removed scene collection '%s' (%s.json), "
  293. "switched to '%s' (%s.json)",
  294. oldName.c_str(), oldFile.c_str(),
  295. newName.c_str(), newFile);
  296. blog(LOG_INFO, "------------------------------------------------");
  297. UpdateTitleBar();
  298. if (api) {
  299. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  300. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  301. }
  302. }
  303. void OBSBasic::on_actionImportSceneCollection_triggered()
  304. {
  305. char path[512];
  306. QString qhome = QDir::homePath();
  307. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  308. if (ret <= 0) {
  309. blog(LOG_WARNING, "Failed to get scene collection config path");
  310. return;
  311. }
  312. QString qfilePath = QFileDialog::getOpenFileName(
  313. this,
  314. QTStr("Basic.MainMenu.SceneCollection.Import"),
  315. qhome,
  316. "JSON Files (*.json)");
  317. QFileInfo finfo(qfilePath);
  318. QString qfilename = finfo.fileName();
  319. QString qpath = QT_UTF8(path);
  320. QFileInfo destinfo(QT_UTF8(path) + qfilename);
  321. if (!qfilePath.isEmpty() && !qfilePath.isNull()) {
  322. string absPath = QT_TO_UTF8(finfo.absoluteFilePath());
  323. OBSData scenedata =
  324. obs_data_create_from_json_file(absPath.c_str());
  325. obs_data_release(scenedata);
  326. string origName = obs_data_get_string(scenedata, "name");
  327. string name = origName;
  328. string file;
  329. int inc = 1;
  330. while (SceneCollectionExists(name.c_str())) {
  331. name = origName + " (" + to_string(++inc) + ")";
  332. }
  333. obs_data_set_string(scenedata, "name", name.c_str());
  334. if (!GetFileSafeName(name.c_str(), file)) {
  335. blog(LOG_WARNING, "Failed to create "
  336. "safe file name for '%s'",
  337. name.c_str());
  338. return;
  339. }
  340. string filePath = path + file;
  341. if (!GetClosestUnusedFileName(filePath, "json")) {
  342. blog(LOG_WARNING, "Failed to get "
  343. "closest file name for %s",
  344. file.c_str());
  345. return;
  346. }
  347. obs_data_save_json_safe(scenedata, filePath.c_str(),
  348. "tmp", "bak");
  349. RefreshSceneCollections();
  350. }
  351. }
  352. void OBSBasic::on_actionExportSceneCollection_triggered()
  353. {
  354. SaveProjectNow();
  355. char path[512];
  356. QString home = QDir::homePath();
  357. QString currentFile = QT_UTF8(config_get_string(App()->GlobalConfig(),
  358. "Basic", "SceneCollectionFile"));
  359. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  360. if (ret <= 0) {
  361. blog(LOG_WARNING, "Failed to get scene collection config path");
  362. return;
  363. }
  364. QString exportFile = QFileDialog::getSaveFileName(
  365. this,
  366. QTStr("Basic.MainMenu.SceneCollection.Export"),
  367. home + "/" + currentFile,
  368. "JSON Files (*.json)");
  369. string file = QT_TO_UTF8(exportFile);
  370. if (!exportFile.isEmpty() && !exportFile.isNull()) {
  371. if (QFile::exists(exportFile))
  372. QFile::remove(exportFile);
  373. QFile::copy(path + currentFile + ".json", exportFile);
  374. }
  375. }
  376. void OBSBasic::ChangeSceneCollection()
  377. {
  378. QAction *action = reinterpret_cast<QAction*>(sender());
  379. std::string fileName;
  380. if (!action)
  381. return;
  382. fileName = QT_TO_UTF8(action->property("file_name").value<QString>());
  383. if (fileName.empty())
  384. return;
  385. const char *oldName = config_get_string(App()->GlobalConfig(),
  386. "Basic", "SceneCollection");
  387. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  388. action->setChecked(true);
  389. return;
  390. }
  391. SaveProjectNow();
  392. Load(fileName.c_str());
  393. RefreshSceneCollections();
  394. const char *newName = config_get_string(App()->GlobalConfig(),
  395. "Basic", "SceneCollection");
  396. const char *newFile = config_get_string(App()->GlobalConfig(),
  397. "Basic", "SceneCollectionFile");
  398. blog(LOG_INFO, "Switched to scene collection '%s' (%s.json)",
  399. newName, newFile);
  400. blog(LOG_INFO, "------------------------------------------------");
  401. UpdateTitleBar();
  402. if (api)
  403. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  404. }