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

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