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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 <qt-wrappers.hpp>
  21. #include "item-widget-helpers.hpp"
  22. #include "window-basic-main.hpp"
  23. #include "window-importer.hpp"
  24. #include "window-namedialog.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. OBSDataAutoRelease 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. if (!cb(name.c_str(), filePath))
  55. break;
  56. }
  57. os_globfree(glob);
  58. }
  59. 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 GetSceneCollectionName(QWidget *parent, std::string &name,
  73. std::string &file,
  74. const char *oldName = nullptr)
  75. {
  76. bool rename = oldName != nullptr;
  77. const char *title;
  78. const char *text;
  79. if (rename) {
  80. title = Str("Basic.Main.RenameSceneCollection.Title");
  81. text = Str("Basic.Main.AddSceneCollection.Text");
  82. } else {
  83. title = Str("Basic.Main.AddSceneCollection.Title");
  84. text = Str("Basic.Main.AddSceneCollection.Text");
  85. }
  86. for (;;) {
  87. bool success = NameDialog::AskForName(parent, title, text, name,
  88. QT_UTF8(oldName));
  89. if (!success) {
  90. return false;
  91. }
  92. if (name.empty()) {
  93. OBSMessageBox::warning(parent,
  94. QTStr("NoNameEntered.Title"),
  95. QTStr("NoNameEntered.Text"));
  96. continue;
  97. }
  98. if (SceneCollectionExists(name.c_str())) {
  99. OBSMessageBox::warning(parent,
  100. QTStr("NameExists.Title"),
  101. QTStr("NameExists.Text"));
  102. continue;
  103. }
  104. break;
  105. }
  106. if (!GetUnusedSceneCollectionFile(name, file)) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. bool OBSBasic::AddSceneCollection(bool create_new, const QString &qname)
  112. {
  113. std::string name;
  114. std::string file;
  115. if (qname.isEmpty()) {
  116. if (!GetSceneCollectionName(this, name, file))
  117. return false;
  118. } else {
  119. name = QT_TO_UTF8(qname);
  120. if (SceneCollectionExists(name.c_str()))
  121. return false;
  122. if (!GetUnusedSceneCollectionFile(name, file)) {
  123. return false;
  124. }
  125. }
  126. auto new_collection = [this, create_new](const std::string &file,
  127. const std::string &name) {
  128. SaveProjectNow();
  129. config_set_string(App()->GlobalConfig(), "Basic",
  130. "SceneCollection", name.c_str());
  131. config_set_string(App()->GlobalConfig(), "Basic",
  132. "SceneCollectionFile", file.c_str());
  133. if (create_new) {
  134. CreateDefaultScene(false);
  135. } else {
  136. obs_reset_source_uuids();
  137. }
  138. SaveProjectNow();
  139. RefreshSceneCollections();
  140. };
  141. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  142. new_collection(file, name);
  143. blog(LOG_INFO, "Added scene collection '%s' (%s, %s.json)",
  144. name.c_str(), create_new ? "clean" : "duplicate", file.c_str());
  145. blog(LOG_INFO, "------------------------------------------------");
  146. UpdateTitleBar();
  147. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  148. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  149. return true;
  150. }
  151. void OBSBasic::RefreshSceneCollections()
  152. {
  153. QList<QAction *> menuActions = ui->sceneCollectionMenu->actions();
  154. int count = 0;
  155. for (int i = 0; i < menuActions.count(); i++) {
  156. QVariant v = menuActions[i]->property("file_name");
  157. if (v.typeName() != nullptr)
  158. delete menuActions[i];
  159. }
  160. const char *cur_name = config_get_string(App()->GlobalConfig(), "Basic",
  161. "SceneCollection");
  162. auto addCollection = [&](const char *name, const char *path) {
  163. std::string file = strrchr(path, '/') + 1;
  164. file.erase(file.size() - 5, 5);
  165. QAction *action = new QAction(QT_UTF8(name), this);
  166. action->setProperty("file_name", QT_UTF8(path));
  167. connect(action, &QAction::triggered, this,
  168. &OBSBasic::ChangeSceneCollection);
  169. action->setCheckable(true);
  170. action->setChecked(strcmp(name, cur_name) == 0);
  171. ui->sceneCollectionMenu->addAction(action);
  172. count++;
  173. return true;
  174. };
  175. EnumSceneCollections(addCollection);
  176. /* force saving of first scene collection on first run, otherwise
  177. * no scene collections will show up */
  178. if (!count) {
  179. long prevDisableVal = disableSaving;
  180. disableSaving = 0;
  181. SaveProjectNow();
  182. disableSaving = prevDisableVal;
  183. EnumSceneCollections(addCollection);
  184. }
  185. ui->actionRemoveSceneCollection->setEnabled(count > 1);
  186. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  187. main->ui->actionPasteFilters->setEnabled(false);
  188. main->ui->actionPasteRef->setEnabled(false);
  189. main->ui->actionPasteDup->setEnabled(false);
  190. }
  191. void OBSBasic::on_actionNewSceneCollection_triggered()
  192. {
  193. AddSceneCollection(true);
  194. }
  195. void OBSBasic::on_actionDupSceneCollection_triggered()
  196. {
  197. AddSceneCollection(false);
  198. }
  199. void OBSBasic::on_actionRenameSceneCollection_triggered()
  200. {
  201. std::string name;
  202. std::string file;
  203. std::string oname;
  204. std::string oldFile = config_get_string(App()->GlobalConfig(), "Basic",
  205. "SceneCollectionFile");
  206. const char *oldName = config_get_string(App()->GlobalConfig(), "Basic",
  207. "SceneCollection");
  208. oname = std::string(oldName);
  209. bool success = GetSceneCollectionName(this, name, file, oldName);
  210. if (!success)
  211. return;
  212. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  213. name.c_str());
  214. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  215. file.c_str());
  216. SaveProjectNow();
  217. char path[512];
  218. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  219. if (ret <= 0) {
  220. blog(LOG_WARNING, "Failed to get scene collection config path");
  221. return;
  222. }
  223. oldFile.insert(0, path);
  224. oldFile += ".json";
  225. os_unlink(oldFile.c_str());
  226. oldFile += ".bak";
  227. os_unlink(oldFile.c_str());
  228. blog(LOG_INFO, "------------------------------------------------");
  229. blog(LOG_INFO, "Renamed scene collection to '%s' (%s.json)",
  230. name.c_str(), file.c_str());
  231. blog(LOG_INFO, "------------------------------------------------");
  232. UpdateTitleBar();
  233. RefreshSceneCollections();
  234. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_RENAMED);
  235. }
  236. void OBSBasic::on_actionRemoveSceneCollection_triggered()
  237. {
  238. std::string newName;
  239. std::string newPath;
  240. std::string oldFile = config_get_string(App()->GlobalConfig(), "Basic",
  241. "SceneCollectionFile");
  242. std::string oldName = config_get_string(App()->GlobalConfig(), "Basic",
  243. "SceneCollection");
  244. auto cb = [&](const char *name, const char *filePath) {
  245. if (strcmp(oldName.c_str(), name) != 0) {
  246. newName = name;
  247. newPath = filePath;
  248. return false;
  249. }
  250. return true;
  251. };
  252. EnumSceneCollections(cb);
  253. /* this should never be true due to menu item being grayed out */
  254. if (newPath.empty())
  255. return;
  256. QString text =
  257. QTStr("ConfirmRemove.Text").arg(QT_UTF8(oldName.c_str()));
  258. QMessageBox::StandardButton button = OBSMessageBox::question(
  259. this, QTStr("ConfirmRemove.Title"), text);
  260. if (button == QMessageBox::No)
  261. return;
  262. char path[512];
  263. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  264. if (ret <= 0) {
  265. blog(LOG_WARNING, "Failed to get scene collection config path");
  266. return;
  267. }
  268. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  269. oldFile.insert(0, path);
  270. /* os_rename() overwrites if necessary, only the .bak file will remain. */
  271. os_rename((oldFile + ".json").c_str(), (oldFile + ".json.bak").c_str());
  272. Load(newPath.c_str());
  273. RefreshSceneCollections();
  274. const char *newFile = config_get_string(App()->GlobalConfig(), "Basic",
  275. "SceneCollectionFile");
  276. blog(LOG_INFO,
  277. "Removed scene collection '%s' (%s.json), "
  278. "switched to '%s' (%s.json)",
  279. oldName.c_str(), oldFile.c_str(), newName.c_str(), newFile);
  280. blog(LOG_INFO, "------------------------------------------------");
  281. UpdateTitleBar();
  282. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  283. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  284. }
  285. void OBSBasic::on_actionImportSceneCollection_triggered()
  286. {
  287. OBSImporter imp(this);
  288. imp.exec();
  289. RefreshSceneCollections();
  290. }
  291. void OBSBasic::on_actionExportSceneCollection_triggered()
  292. {
  293. SaveProjectNow();
  294. char path[512];
  295. QString home = QDir::homePath();
  296. QString currentFile = QT_UTF8(config_get_string(
  297. App()->GlobalConfig(), "Basic", "SceneCollectionFile"));
  298. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  299. if (ret <= 0) {
  300. blog(LOG_WARNING, "Failed to get scene collection config path");
  301. return;
  302. }
  303. QString exportFile =
  304. SaveFile(this, QTStr("Basic.MainMenu.SceneCollection.Export"),
  305. home + "/" + currentFile, "JSON Files (*.json)");
  306. string file = QT_TO_UTF8(exportFile);
  307. if (!exportFile.isEmpty() && !exportFile.isNull()) {
  308. QString inputFile = path + currentFile + ".json";
  309. OBSDataAutoRelease collection =
  310. obs_data_create_from_json_file(QT_TO_UTF8(inputFile));
  311. OBSDataArrayAutoRelease sources =
  312. obs_data_get_array(collection, "sources");
  313. if (!sources) {
  314. blog(LOG_WARNING,
  315. "No sources in exported scene collection");
  316. return;
  317. }
  318. obs_data_erase(collection, "sources");
  319. // We're just using std::sort on a vector to make life easier.
  320. vector<OBSData> sourceItems;
  321. obs_data_array_enum(
  322. sources,
  323. [](obs_data_t *data, void *pVec) -> void {
  324. auto &sourceItems =
  325. *static_cast<vector<OBSData> *>(pVec);
  326. sourceItems.push_back(data);
  327. },
  328. &sourceItems);
  329. std::sort(sourceItems.begin(), sourceItems.end(),
  330. [](const OBSData &a, const OBSData &b) {
  331. return astrcmpi(obs_data_get_string(a,
  332. "name"),
  333. obs_data_get_string(
  334. b, "name")) < 0;
  335. });
  336. OBSDataArrayAutoRelease newSources = obs_data_array_create();
  337. for (auto &item : sourceItems)
  338. obs_data_array_push_back(newSources, item);
  339. obs_data_set_array(collection, "sources", newSources);
  340. obs_data_save_json_pretty_safe(
  341. collection, QT_TO_UTF8(exportFile), "tmp", "bak");
  342. }
  343. }
  344. void OBSBasic::ChangeSceneCollection()
  345. {
  346. QAction *action = reinterpret_cast<QAction *>(sender());
  347. std::string fileName;
  348. if (!action)
  349. return;
  350. fileName = QT_TO_UTF8(action->property("file_name").value<QString>());
  351. if (fileName.empty())
  352. return;
  353. const char *oldName = config_get_string(App()->GlobalConfig(), "Basic",
  354. "SceneCollection");
  355. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  356. action->setChecked(true);
  357. return;
  358. }
  359. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  360. SaveProjectNow();
  361. Load(fileName.c_str());
  362. RefreshSceneCollections();
  363. const char *newName = config_get_string(App()->GlobalConfig(), "Basic",
  364. "SceneCollection");
  365. const char *newFile = config_get_string(App()->GlobalConfig(), "Basic",
  366. "SceneCollectionFile");
  367. blog(LOG_INFO, "Switched to scene collection '%s' (%s.json)", newName,
  368. newFile);
  369. blog(LOG_INFO, "------------------------------------------------");
  370. UpdateTitleBar();
  371. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  372. }