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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. BPtr<char> fileData = os_quick_read_utf8_file(filePath);
  42. if (!fileData)
  43. continue;
  44. obs_data_t *data = obs_data_create_from_json(fileData);
  45. std::string name = obs_data_get_string(data, "name");
  46. /* if no name found, use the file name as the name
  47. * (this only happens when switching to the new version) */
  48. if (name.empty()) {
  49. name = strrchr(filePath, '/') + 1;
  50. name.resize(name.size() - 5);
  51. }
  52. obs_data_release(data);
  53. if (!cb(name.c_str(), filePath))
  54. break;
  55. }
  56. os_globfree(glob);
  57. }
  58. static bool SceneCollectionExists(const char *findName)
  59. {
  60. bool found = false;
  61. auto func = [&](const char *name, const char*)
  62. {
  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, const char *oldName = nullptr)
  74. {
  75. bool rename = oldName != nullptr;
  76. const char *title;
  77. const char *text;
  78. char path[512];
  79. size_t len;
  80. int ret;
  81. if (rename) {
  82. title = Str("Basic.Main.RenameSceneCollection.Title");
  83. text = Str("Basic.Main.AddSceneCollection.Text");
  84. } else {
  85. title = Str("Basic.Main.AddSceneCollection.Title");
  86. text = Str("Basic.Main.AddSceneCollection.Text");
  87. }
  88. for (;;) {
  89. bool success = NameDialog::AskForName(parent, title, text,
  90. name, QT_UTF8(oldName));
  91. if (!success) {
  92. return false;
  93. }
  94. if (name.empty()) {
  95. QMessageBox::information(parent,
  96. QTStr("NoNameEntered.Title"),
  97. QTStr("NoNameEntered.Text"));
  98. continue;
  99. }
  100. if (SceneCollectionExists(name.c_str())) {
  101. QMessageBox::information(parent,
  102. QTStr("NameExists.Title"),
  103. QTStr("NameExists.Text"));
  104. continue;
  105. }
  106. break;
  107. }
  108. if (!GetFileSafeName(name.c_str(), file)) {
  109. blog(LOG_WARNING, "Failed to create safe file name for '%s'",
  110. name.c_str());
  111. return false;
  112. }
  113. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes/");
  114. if (ret <= 0) {
  115. blog(LOG_WARNING, "Failed to get scene collection config path");
  116. return false;
  117. }
  118. len = file.size();
  119. file.insert(0, path);
  120. if (!GetClosestUnusedFileName(file, "json")) {
  121. blog(LOG_WARNING, "Failed to get closest file name for %s",
  122. file.c_str());
  123. return false;
  124. }
  125. file.erase(file.size() - 5, 5);
  126. file.erase(0, file.size() - len);
  127. return true;
  128. }
  129. void OBSBasic::AddSceneCollection(bool create_new)
  130. {
  131. std::string name;
  132. std::string file;
  133. if (!GetSceneCollectionName(this, name, file))
  134. return;
  135. SaveProject();
  136. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  137. name.c_str());
  138. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  139. file.c_str());
  140. if (create_new) {
  141. CreateDefaultScene();
  142. }
  143. SaveProject();
  144. RefreshSceneCollections();
  145. blog(LOG_INFO, "Added scene collection '%s' (%s, %s.json)",
  146. name.c_str(), create_new ? "clean" : "duplicate",
  147. file.c_str());
  148. blog(LOG_INFO, "------------------------------------------------");
  149. UpdateTitleBar();
  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("fileName");
  157. if (v.typeName() != nullptr)
  158. delete menuActions[i];
  159. }
  160. const char *cur_name = config_get_string(App()->GlobalConfig(),
  161. "Basic", "SceneCollection");
  162. auto addCollection = [&](const char *name, const char *path)
  163. {
  164. std::string file = strrchr(path, '/') + 1;
  165. file.erase(file.size() - 5, 5);
  166. QAction *action = new QAction(QT_UTF8(name), this);
  167. action->setProperty("fileName", QT_UTF8(path));
  168. connect(action, &QAction::triggered,
  169. this, &OBSBasic::ChangeSceneCollection);
  170. action->setCheckable(true);
  171. action->setChecked(strcmp(name, cur_name) == 0);
  172. ui->sceneCollectionMenu->addAction(action);
  173. count++;
  174. return true;
  175. };
  176. EnumSceneCollections(addCollection);
  177. ui->actionRemoveSceneCollection->setEnabled(count > 1);
  178. }
  179. void OBSBasic::on_actionNewSceneCollection_triggered()
  180. {
  181. AddSceneCollection(true);
  182. }
  183. void OBSBasic::on_actionDupSceneCollection_triggered()
  184. {
  185. AddSceneCollection(false);
  186. }
  187. void OBSBasic::on_actionRenameSceneCollection_triggered()
  188. {
  189. std::string name;
  190. std::string file;
  191. std::string oldFile = config_get_string(App()->GlobalConfig(),
  192. "Basic", "SceneCollectionFile");
  193. const char *oldName = config_get_string(App()->GlobalConfig(),
  194. "Basic", "SceneCollection");
  195. bool success = GetSceneCollectionName(this, name, file, oldName);
  196. if (!success)
  197. return;
  198. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  199. name.c_str());
  200. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  201. file.c_str());
  202. SaveProject();
  203. char path[512];
  204. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  205. if (ret <= 0) {
  206. blog(LOG_WARNING, "Failed to get scene collection config path");
  207. return;
  208. }
  209. oldFile.insert(0, path);
  210. oldFile += ".json";
  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. Load(newPath.c_str());
  256. RefreshSceneCollections();
  257. const char *newFile = config_get_string(App()->GlobalConfig(),
  258. "Basic", "SceneCollectionFile");
  259. blog(LOG_INFO, "Removed scene collection '%s' (%s.json), "
  260. "switched to '%s' (%s.json)",
  261. oldName.c_str(), oldFile.c_str(),
  262. newName.c_str(), newFile);
  263. blog(LOG_INFO, "------------------------------------------------");
  264. UpdateTitleBar();
  265. }
  266. void OBSBasic::ChangeSceneCollection()
  267. {
  268. QAction *action = reinterpret_cast<QAction*>(sender());
  269. std::string fileName;
  270. if (!action)
  271. return;
  272. fileName = QT_TO_UTF8(action->property("fileName").value<QString>());
  273. if (fileName.empty())
  274. return;
  275. const char *oldName = config_get_string(App()->GlobalConfig(),
  276. "Basic", "SceneCollection");
  277. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  278. action->setChecked(true);
  279. return;
  280. }
  281. SaveProject();
  282. Load(fileName.c_str());
  283. RefreshSceneCollections();
  284. const char *newName = config_get_string(App()->GlobalConfig(),
  285. "Basic", "SceneCollection");
  286. const char *newFile = config_get_string(App()->GlobalConfig(),
  287. "Basic", "SceneCollectionFile");
  288. blog(LOG_INFO, "Switched to scene collection '%s' (%s.json)",
  289. newName, newFile);
  290. blog(LOG_INFO, "------------------------------------------------");
  291. UpdateTitleBar();
  292. }