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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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->OpenSavedProjectors();
  194. main->ui->actionPasteFilters->setEnabled(false);
  195. main->ui->actionPasteRef->setEnabled(false);
  196. main->ui->actionPasteDup->setEnabled(false);
  197. }
  198. void OBSBasic::on_actionNewSceneCollection_triggered()
  199. {
  200. AddSceneCollection(true);
  201. }
  202. void OBSBasic::on_actionDupSceneCollection_triggered()
  203. {
  204. AddSceneCollection(false);
  205. }
  206. void OBSBasic::on_actionRenameSceneCollection_triggered()
  207. {
  208. std::string name;
  209. std::string file;
  210. std::string oldFile = config_get_string(App()->GlobalConfig(),
  211. "Basic", "SceneCollectionFile");
  212. const char *oldName = config_get_string(App()->GlobalConfig(),
  213. "Basic", "SceneCollection");
  214. bool success = GetSceneCollectionName(this, name, file, oldName);
  215. if (!success)
  216. return;
  217. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  218. name.c_str());
  219. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  220. file.c_str());
  221. SaveProjectNow();
  222. char path[512];
  223. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  224. if (ret <= 0) {
  225. blog(LOG_WARNING, "Failed to get scene collection config path");
  226. return;
  227. }
  228. oldFile.insert(0, path);
  229. oldFile += ".json";
  230. os_unlink(oldFile.c_str());
  231. oldFile += ".bak";
  232. os_unlink(oldFile.c_str());
  233. blog(LOG_INFO, "------------------------------------------------");
  234. blog(LOG_INFO, "Renamed scene collection to '%s' (%s.json)",
  235. name.c_str(), file.c_str());
  236. blog(LOG_INFO, "------------------------------------------------");
  237. UpdateTitleBar();
  238. RefreshSceneCollections();
  239. if (api) {
  240. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  241. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  242. }
  243. }
  244. void OBSBasic::on_actionRemoveSceneCollection_triggered()
  245. {
  246. std::string newName;
  247. std::string newPath;
  248. std::string oldFile = config_get_string(App()->GlobalConfig(),
  249. "Basic", "SceneCollectionFile");
  250. std::string oldName = config_get_string(App()->GlobalConfig(),
  251. "Basic", "SceneCollection");
  252. auto cb = [&](const char *name, const char *filePath)
  253. {
  254. if (strcmp(oldName.c_str(), name) != 0) {
  255. newName = name;
  256. newPath = filePath;
  257. return false;
  258. }
  259. return true;
  260. };
  261. EnumSceneCollections(cb);
  262. /* this should never be true due to menu item being grayed out */
  263. if (newPath.empty())
  264. return;
  265. QString text = QTStr("ConfirmRemove.Text");
  266. text.replace("$1", QT_UTF8(oldName.c_str()));
  267. QMessageBox::StandardButton button = OBSMessageBox::question(this,
  268. QTStr("ConfirmRemove.Title"), text);
  269. if (button == QMessageBox::No)
  270. return;
  271. char path[512];
  272. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  273. if (ret <= 0) {
  274. blog(LOG_WARNING, "Failed to get scene collection config path");
  275. return;
  276. }
  277. oldFile.insert(0, path);
  278. oldFile += ".json";
  279. os_unlink(oldFile.c_str());
  280. oldFile += ".bak";
  281. os_unlink(oldFile.c_str());
  282. Load(newPath.c_str());
  283. RefreshSceneCollections();
  284. const char *newFile = config_get_string(App()->GlobalConfig(),
  285. "Basic", "SceneCollectionFile");
  286. blog(LOG_INFO, "Removed scene collection '%s' (%s.json), "
  287. "switched to '%s' (%s.json)",
  288. oldName.c_str(), oldFile.c_str(),
  289. newName.c_str(), newFile);
  290. blog(LOG_INFO, "------------------------------------------------");
  291. UpdateTitleBar();
  292. if (api) {
  293. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  294. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  295. }
  296. }
  297. void OBSBasic::on_actionImportSceneCollection_triggered()
  298. {
  299. char path[512];
  300. QString home = QDir::homePath();
  301. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  302. if (ret <= 0) {
  303. blog(LOG_WARNING, "Failed to get scene collection config path");
  304. return;
  305. }
  306. QString file = QFileDialog::getOpenFileName(
  307. this,
  308. QTStr("Basic.MainMenu.SceneCollection.Import"),
  309. home,
  310. "JSON Files (*.json)");
  311. QFileInfo finfo(file);
  312. QString filename = finfo.fileName();
  313. QFileInfo destinfo(path + filename);
  314. if (!file.isEmpty() && !file.isNull()) {
  315. if (!destinfo.exists()) {
  316. QFile::copy(file, path + filename);
  317. RefreshSceneCollections();
  318. } else {
  319. OBSMessageBox::information(this,
  320. QTStr("Basic.MainMenu.SceneCollection.Import"),
  321. QTStr("Basic.MainMenu.SceneCollection.Exists"));
  322. }
  323. }
  324. }
  325. void OBSBasic::on_actionExportSceneCollection_triggered()
  326. {
  327. char path[512];
  328. QString home = QDir::homePath();
  329. QString currentFile = QT_UTF8(config_get_string(App()->GlobalConfig(),
  330. "Basic", "SceneCollectionFile"));
  331. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  332. if (ret <= 0) {
  333. blog(LOG_WARNING, "Failed to get scene collection config path");
  334. return;
  335. }
  336. QString exportFile = QFileDialog::getSaveFileName(
  337. this,
  338. QTStr("Basic.MainMenu.SceneCollection.Export"),
  339. home + "/" + currentFile,
  340. "JSON Files (*.json)");
  341. string file = QT_TO_UTF8(exportFile);
  342. if (!exportFile.isEmpty() && !exportFile.isNull()) {
  343. if (QFile::exists(exportFile))
  344. QFile::remove(exportFile);
  345. QFile::copy(path + currentFile + ".json", exportFile);
  346. }
  347. }
  348. void OBSBasic::ChangeSceneCollection()
  349. {
  350. QAction *action = reinterpret_cast<QAction*>(sender());
  351. std::string fileName;
  352. if (!action)
  353. return;
  354. fileName = QT_TO_UTF8(action->property("file_name").value<QString>());
  355. if (fileName.empty())
  356. return;
  357. const char *oldName = config_get_string(App()->GlobalConfig(),
  358. "Basic", "SceneCollection");
  359. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  360. action->setChecked(true);
  361. return;
  362. }
  363. SaveProjectNow();
  364. Load(fileName.c_str());
  365. RefreshSceneCollections();
  366. const char *newName = config_get_string(App()->GlobalConfig(),
  367. "Basic", "SceneCollection");
  368. const char *newFile = config_get_string(App()->GlobalConfig(),
  369. "Basic", "SceneCollectionFile");
  370. blog(LOG_INFO, "Switched to scene collection '%s' (%s.json)",
  371. newName, newFile);
  372. blog(LOG_INFO, "------------------------------------------------");
  373. UpdateTitleBar();
  374. if (api)
  375. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  376. }