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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "item-widget-helpers.hpp"
  19. #include "window-basic-main.hpp"
  20. #include "window-namedialog.hpp"
  21. #include "qt-wrappers.hpp"
  22. template <typename Func> static void EnumSceneCollections(Func &&cb)
  23. {
  24. char path[512];
  25. os_glob_t *glob;
  26. int ret = GetConfigPath(path, sizeof(path),
  27. "obs-studio/basic/scenes/*.json");
  28. if (ret <= 0) {
  29. blog(LOG_WARNING, "Failed to get config path for scene "
  30. "collections");
  31. return;
  32. }
  33. if (os_glob(path, 0, &glob) != 0) {
  34. blog(LOG_WARNING, "Failed to glob scene collections");
  35. return;
  36. }
  37. for (size_t i = 0; i < glob->gl_pathc; i++) {
  38. const char *filePath = glob->gl_pathv[i].path;
  39. if (glob->gl_pathv[i].directory)
  40. continue;
  41. obs_data_t *data = obs_data_create_from_json_file_safe(filePath,
  42. "bak");
  43. std::string name = obs_data_get_string(data, "name");
  44. /* if no name found, use the file name as the name
  45. * (this only happens when switching to the new version) */
  46. if (name.empty()) {
  47. name = strrchr(filePath, '/') + 1;
  48. name.resize(name.size() - 5);
  49. }
  50. obs_data_release(data);
  51. if (!cb(name.c_str(), filePath))
  52. break;
  53. }
  54. os_globfree(glob);
  55. }
  56. static bool SceneCollectionExists(const char *findName)
  57. {
  58. bool found = false;
  59. auto func = [&](const char *name, const char*)
  60. {
  61. if (strcmp(name, findName) == 0) {
  62. found = true;
  63. return false;
  64. }
  65. return true;
  66. };
  67. EnumSceneCollections(func);
  68. return found;
  69. }
  70. static bool GetSceneCollectionName(QWidget *parent, std::string &name,
  71. std::string &file, const char *oldName = nullptr)
  72. {
  73. bool rename = oldName != nullptr;
  74. const char *title;
  75. const char *text;
  76. char path[512];
  77. size_t len;
  78. int ret;
  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,
  88. name, QT_UTF8(oldName));
  89. if (!success) {
  90. return false;
  91. }
  92. if (name.empty()) {
  93. QMessageBox::information(parent,
  94. QTStr("NoNameEntered.Title"),
  95. QTStr("NoNameEntered.Text"));
  96. continue;
  97. }
  98. if (SceneCollectionExists(name.c_str())) {
  99. QMessageBox::information(parent,
  100. QTStr("NameExists.Title"),
  101. QTStr("NameExists.Text"));
  102. continue;
  103. }
  104. break;
  105. }
  106. if (!GetFileSafeName(name.c_str(), file)) {
  107. blog(LOG_WARNING, "Failed to create safe file name for '%s'",
  108. name.c_str());
  109. return false;
  110. }
  111. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes/");
  112. if (ret <= 0) {
  113. blog(LOG_WARNING, "Failed to get scene collection config path");
  114. return false;
  115. }
  116. len = file.size();
  117. file.insert(0, path);
  118. if (!GetClosestUnusedFileName(file, "json")) {
  119. blog(LOG_WARNING, "Failed to get closest file name for %s",
  120. file.c_str());
  121. return false;
  122. }
  123. file.erase(file.size() - 5, 5);
  124. file.erase(0, file.size() - len);
  125. return true;
  126. }
  127. void OBSBasic::AddSceneCollection(bool create_new)
  128. {
  129. std::string name;
  130. std::string file;
  131. if (!GetSceneCollectionName(this, name, file))
  132. return;
  133. SaveProjectNow();
  134. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  135. name.c_str());
  136. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  137. file.c_str());
  138. if (create_new) {
  139. CreateDefaultScene(false);
  140. }
  141. SaveProjectNow();
  142. RefreshSceneCollections();
  143. blog(LOG_INFO, "Added scene collection '%s' (%s, %s.json)",
  144. name.c_str(), create_new ? "clean" : "duplicate",
  145. file.c_str());
  146. blog(LOG_INFO, "------------------------------------------------");
  147. UpdateTitleBar();
  148. }
  149. void OBSBasic::RefreshSceneCollections()
  150. {
  151. QList<QAction*> menuActions = ui->sceneCollectionMenu->actions();
  152. int count = 0;
  153. for (int i = 0; i < menuActions.count(); i++) {
  154. QVariant v = menuActions[i]->property("fileName");
  155. if (v.typeName() != nullptr)
  156. delete menuActions[i];
  157. }
  158. const char *cur_name = config_get_string(App()->GlobalConfig(),
  159. "Basic", "SceneCollection");
  160. auto addCollection = [&](const char *name, const char *path)
  161. {
  162. std::string file = strrchr(path, '/') + 1;
  163. file.erase(file.size() - 5, 5);
  164. QAction *action = new QAction(QT_UTF8(name), this);
  165. action->setProperty("fileName", QT_UTF8(path));
  166. connect(action, &QAction::triggered,
  167. this, &OBSBasic::ChangeSceneCollection);
  168. action->setCheckable(true);
  169. action->setChecked(strcmp(name, cur_name) == 0);
  170. ui->sceneCollectionMenu->addAction(action);
  171. count++;
  172. return true;
  173. };
  174. EnumSceneCollections(addCollection);
  175. ui->actionRemoveSceneCollection->setEnabled(count > 1);
  176. }
  177. void OBSBasic::on_actionNewSceneCollection_triggered()
  178. {
  179. AddSceneCollection(true);
  180. }
  181. void OBSBasic::on_actionDupSceneCollection_triggered()
  182. {
  183. AddSceneCollection(false);
  184. }
  185. void OBSBasic::on_actionRenameSceneCollection_triggered()
  186. {
  187. std::string name;
  188. std::string file;
  189. std::string oldFile = config_get_string(App()->GlobalConfig(),
  190. "Basic", "SceneCollectionFile");
  191. const char *oldName = config_get_string(App()->GlobalConfig(),
  192. "Basic", "SceneCollection");
  193. bool success = GetSceneCollectionName(this, name, file, oldName);
  194. if (!success)
  195. return;
  196. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  197. name.c_str());
  198. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  199. file.c_str());
  200. SaveProjectNow();
  201. char path[512];
  202. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  203. if (ret <= 0) {
  204. blog(LOG_WARNING, "Failed to get scene collection config path");
  205. return;
  206. }
  207. oldFile.insert(0, path);
  208. oldFile += ".json";
  209. os_unlink(oldFile.c_str());
  210. oldFile += ".bak";
  211. os_unlink(oldFile.c_str());
  212. blog(LOG_INFO, "------------------------------------------------");
  213. blog(LOG_INFO, "Renamed scene collection to '%s' (%s.json)",
  214. name.c_str(), file.c_str());
  215. blog(LOG_INFO, "------------------------------------------------");
  216. UpdateTitleBar();
  217. RefreshSceneCollections();
  218. }
  219. void OBSBasic::on_actionRemoveSceneCollection_triggered()
  220. {
  221. std::string newName;
  222. std::string newPath;
  223. std::string oldFile = config_get_string(App()->GlobalConfig(),
  224. "Basic", "SceneCollectionFile");
  225. std::string oldName = config_get_string(App()->GlobalConfig(),
  226. "Basic", "SceneCollection");
  227. auto cb = [&](const char *name, const char *filePath)
  228. {
  229. if (strcmp(oldName.c_str(), name) != 0) {
  230. newName = name;
  231. newPath = filePath;
  232. return false;
  233. }
  234. return true;
  235. };
  236. EnumSceneCollections(cb);
  237. /* this should never be true due to menu item being grayed out */
  238. if (newPath.empty())
  239. return;
  240. QString text = QTStr("ConfirmRemove.Text");
  241. text.replace("$1", QT_UTF8(oldName.c_str()));
  242. QMessageBox::StandardButton button = QMessageBox::question(this,
  243. QTStr("ConfirmRemove.Title"), text);
  244. if (button == QMessageBox::No)
  245. return;
  246. char path[512];
  247. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  248. if (ret <= 0) {
  249. blog(LOG_WARNING, "Failed to get scene collection config path");
  250. return;
  251. }
  252. oldFile.insert(0, path);
  253. oldFile += ".json";
  254. os_unlink(oldFile.c_str());
  255. oldFile += ".bak";
  256. os_unlink(oldFile.c_str());
  257. Load(newPath.c_str());
  258. RefreshSceneCollections();
  259. const char *newFile = config_get_string(App()->GlobalConfig(),
  260. "Basic", "SceneCollectionFile");
  261. blog(LOG_INFO, "Removed scene collection '%s' (%s.json), "
  262. "switched to '%s' (%s.json)",
  263. oldName.c_str(), oldFile.c_str(),
  264. newName.c_str(), newFile);
  265. blog(LOG_INFO, "------------------------------------------------");
  266. UpdateTitleBar();
  267. }
  268. void OBSBasic::ChangeSceneCollection()
  269. {
  270. QAction *action = reinterpret_cast<QAction*>(sender());
  271. std::string fileName;
  272. if (!action)
  273. return;
  274. fileName = QT_TO_UTF8(action->property("fileName").value<QString>());
  275. if (fileName.empty())
  276. return;
  277. const char *oldName = config_get_string(App()->GlobalConfig(),
  278. "Basic", "SceneCollection");
  279. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  280. action->setChecked(true);
  281. return;
  282. }
  283. SaveProjectNow();
  284. Load(fileName.c_str());
  285. RefreshSceneCollections();
  286. const char *newName = config_get_string(App()->GlobalConfig(),
  287. "Basic", "SceneCollection");
  288. const char *newFile = config_get_string(App()->GlobalConfig(),
  289. "Basic", "SceneCollectionFile");
  290. blog(LOG_INFO, "Switched to scene collection '%s' (%s.json)",
  291. newName, newFile);
  292. blog(LOG_INFO, "------------------------------------------------");
  293. UpdateTitleBar();
  294. }