1
0

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

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