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

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