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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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-importer.hpp"
  23. #include "window-namedialog.hpp"
  24. #include "qt-wrappers.hpp"
  25. using namespace std;
  26. void EnumSceneCollections(std::function<bool(const char *, const char *)> &&cb)
  27. {
  28. char path[512];
  29. os_glob_t *glob;
  30. int ret = GetConfigPath(path, sizeof(path),
  31. "obs-studio/basic/scenes/*.json");
  32. if (ret <= 0) {
  33. blog(LOG_WARNING, "Failed to get config path for scene "
  34. "collections");
  35. return;
  36. }
  37. if (os_glob(path, 0, &glob) != 0) {
  38. blog(LOG_WARNING, "Failed to glob scene collections");
  39. return;
  40. }
  41. for (size_t i = 0; i < glob->gl_pathc; i++) {
  42. const char *filePath = glob->gl_pathv[i].path;
  43. if (glob->gl_pathv[i].directory)
  44. continue;
  45. obs_data_t *data =
  46. obs_data_create_from_json_file_safe(filePath, "bak");
  47. std::string name = obs_data_get_string(data, "name");
  48. /* if no name found, use the file name as the name
  49. * (this only happens when switching to the new version) */
  50. if (name.empty()) {
  51. name = strrchr(filePath, '/') + 1;
  52. name.resize(name.size() - 5);
  53. }
  54. obs_data_release(data);
  55. if (!cb(name.c_str(), filePath))
  56. break;
  57. }
  58. os_globfree(glob);
  59. }
  60. static bool SceneCollectionExists(const char *findName)
  61. {
  62. bool found = false;
  63. auto func = [&](const char *name, const char *) {
  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 GetUnusedSceneCollectionFile(std::string &name, std::string &file)
  74. {
  75. char path[512];
  76. size_t len;
  77. int ret;
  78. if (!GetFileSafeName(name.c_str(), file)) {
  79. blog(LOG_WARNING, "Failed to create safe file name for '%s'",
  80. name.c_str());
  81. return false;
  82. }
  83. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes/");
  84. if (ret <= 0) {
  85. blog(LOG_WARNING, "Failed to get scene collection config path");
  86. return false;
  87. }
  88. len = file.size();
  89. file.insert(0, path);
  90. if (!GetClosestUnusedFileName(file, "json")) {
  91. blog(LOG_WARNING, "Failed to get closest file name for %s",
  92. file.c_str());
  93. return false;
  94. }
  95. file.erase(file.size() - 5, 5);
  96. file.erase(0, file.size() - len);
  97. return true;
  98. }
  99. static bool GetSceneCollectionName(QWidget *parent, std::string &name,
  100. std::string &file,
  101. const char *oldName = nullptr)
  102. {
  103. bool rename = oldName != nullptr;
  104. const char *title;
  105. const char *text;
  106. if (rename) {
  107. title = Str("Basic.Main.RenameSceneCollection.Title");
  108. text = Str("Basic.Main.AddSceneCollection.Text");
  109. } else {
  110. title = Str("Basic.Main.AddSceneCollection.Title");
  111. text = Str("Basic.Main.AddSceneCollection.Text");
  112. }
  113. for (;;) {
  114. bool success = NameDialog::AskForName(parent, title, text, name,
  115. QT_UTF8(oldName));
  116. if (!success) {
  117. return false;
  118. }
  119. if (name.empty()) {
  120. OBSMessageBox::warning(parent,
  121. QTStr("NoNameEntered.Title"),
  122. QTStr("NoNameEntered.Text"));
  123. continue;
  124. }
  125. if (SceneCollectionExists(name.c_str())) {
  126. OBSMessageBox::warning(parent,
  127. QTStr("NameExists.Title"),
  128. QTStr("NameExists.Text"));
  129. continue;
  130. }
  131. break;
  132. }
  133. if (!GetUnusedSceneCollectionFile(name, file)) {
  134. return false;
  135. }
  136. return true;
  137. }
  138. bool OBSBasic::AddSceneCollection(bool create_new, const QString &qname)
  139. {
  140. std::string name;
  141. std::string file;
  142. if (qname.isEmpty()) {
  143. if (!GetSceneCollectionName(this, name, file))
  144. return false;
  145. } else {
  146. name = QT_TO_UTF8(qname);
  147. if (SceneCollectionExists(name.c_str()))
  148. return false;
  149. if (!GetUnusedSceneCollectionFile(name, file)) {
  150. return false;
  151. }
  152. }
  153. SaveProjectNow();
  154. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  155. name.c_str());
  156. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  157. file.c_str());
  158. if (create_new) {
  159. CreateDefaultScene(false);
  160. }
  161. SaveProjectNow();
  162. RefreshSceneCollections();
  163. blog(LOG_INFO, "Added scene collection '%s' (%s, %s.json)",
  164. name.c_str(), create_new ? "clean" : "duplicate", file.c_str());
  165. blog(LOG_INFO, "------------------------------------------------");
  166. UpdateTitleBar();
  167. if (api) {
  168. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  169. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  170. }
  171. return true;
  172. }
  173. void OBSBasic::RefreshSceneCollections()
  174. {
  175. QList<QAction *> menuActions = ui->sceneCollectionMenu->actions();
  176. int count = 0;
  177. for (int i = 0; i < menuActions.count(); i++) {
  178. QVariant v = menuActions[i]->property("file_name");
  179. if (v.typeName() != nullptr)
  180. delete menuActions[i];
  181. }
  182. const char *cur_name = config_get_string(App()->GlobalConfig(), "Basic",
  183. "SceneCollection");
  184. auto addCollection = [&](const char *name, const char *path) {
  185. std::string file = strrchr(path, '/') + 1;
  186. file.erase(file.size() - 5, 5);
  187. QAction *action = new QAction(QT_UTF8(name), this);
  188. action->setProperty("file_name", QT_UTF8(path));
  189. connect(action, &QAction::triggered, this,
  190. &OBSBasic::ChangeSceneCollection);
  191. action->setCheckable(true);
  192. action->setChecked(strcmp(name, cur_name) == 0);
  193. ui->sceneCollectionMenu->addAction(action);
  194. count++;
  195. return true;
  196. };
  197. EnumSceneCollections(addCollection);
  198. /* force saving of first scene collection on first run, otherwise
  199. * no scene collections will show up */
  200. if (!count) {
  201. long prevDisableVal = disableSaving;
  202. disableSaving = 0;
  203. SaveProjectNow();
  204. disableSaving = prevDisableVal;
  205. EnumSceneCollections(addCollection);
  206. }
  207. ui->actionRemoveSceneCollection->setEnabled(count > 1);
  208. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  209. main->ui->actionPasteFilters->setEnabled(false);
  210. main->ui->actionPasteRef->setEnabled(false);
  211. main->ui->actionPasteDup->setEnabled(false);
  212. }
  213. void OBSBasic::on_actionNewSceneCollection_triggered()
  214. {
  215. AddSceneCollection(true);
  216. }
  217. void OBSBasic::on_actionDupSceneCollection_triggered()
  218. {
  219. AddSceneCollection(false);
  220. }
  221. void OBSBasic::on_actionRenameSceneCollection_triggered()
  222. {
  223. std::string name;
  224. std::string file;
  225. std::string oldFile = config_get_string(App()->GlobalConfig(), "Basic",
  226. "SceneCollectionFile");
  227. const char *oldName = config_get_string(App()->GlobalConfig(), "Basic",
  228. "SceneCollection");
  229. bool success = GetSceneCollectionName(this, name, file, oldName);
  230. if (!success)
  231. return;
  232. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  233. name.c_str());
  234. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  235. file.c_str());
  236. SaveProjectNow();
  237. char path[512];
  238. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  239. if (ret <= 0) {
  240. blog(LOG_WARNING, "Failed to get scene collection config path");
  241. return;
  242. }
  243. oldFile.insert(0, path);
  244. oldFile += ".json";
  245. os_unlink(oldFile.c_str());
  246. oldFile += ".bak";
  247. os_unlink(oldFile.c_str());
  248. blog(LOG_INFO, "------------------------------------------------");
  249. blog(LOG_INFO, "Renamed scene collection to '%s' (%s.json)",
  250. name.c_str(), file.c_str());
  251. blog(LOG_INFO, "------------------------------------------------");
  252. UpdateTitleBar();
  253. RefreshSceneCollections();
  254. if (api) {
  255. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  256. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  257. }
  258. }
  259. void OBSBasic::on_actionRemoveSceneCollection_triggered()
  260. {
  261. std::string newName;
  262. std::string newPath;
  263. std::string oldFile = config_get_string(App()->GlobalConfig(), "Basic",
  264. "SceneCollectionFile");
  265. std::string oldName = config_get_string(App()->GlobalConfig(), "Basic",
  266. "SceneCollection");
  267. auto cb = [&](const char *name, const char *filePath) {
  268. if (strcmp(oldName.c_str(), name) != 0) {
  269. newName = name;
  270. newPath = filePath;
  271. return false;
  272. }
  273. return true;
  274. };
  275. EnumSceneCollections(cb);
  276. /* this should never be true due to menu item being grayed out */
  277. if (newPath.empty())
  278. return;
  279. QString text = QTStr("ConfirmRemove.Text");
  280. text.replace("$1", QT_UTF8(oldName.c_str()));
  281. QMessageBox::StandardButton button = OBSMessageBox::question(
  282. this, QTStr("ConfirmRemove.Title"), text);
  283. if (button == QMessageBox::No)
  284. return;
  285. char path[512];
  286. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  287. if (ret <= 0) {
  288. blog(LOG_WARNING, "Failed to get scene collection config path");
  289. return;
  290. }
  291. oldFile.insert(0, path);
  292. oldFile += ".json";
  293. os_unlink(oldFile.c_str());
  294. oldFile += ".bak";
  295. os_unlink(oldFile.c_str());
  296. Load(newPath.c_str());
  297. RefreshSceneCollections();
  298. const char *newFile = config_get_string(App()->GlobalConfig(), "Basic",
  299. "SceneCollectionFile");
  300. blog(LOG_INFO,
  301. "Removed scene collection '%s' (%s.json), "
  302. "switched to '%s' (%s.json)",
  303. oldName.c_str(), oldFile.c_str(), newName.c_str(), newFile);
  304. blog(LOG_INFO, "------------------------------------------------");
  305. UpdateTitleBar();
  306. if (api) {
  307. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  308. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  309. }
  310. }
  311. void OBSBasic::on_actionImportSceneCollection_triggered()
  312. {
  313. OBSImporter *imp;
  314. imp = new OBSImporter(this);
  315. imp->exec();
  316. delete imp;
  317. RefreshSceneCollections();
  318. }
  319. void OBSBasic::on_actionExportSceneCollection_triggered()
  320. {
  321. SaveProjectNow();
  322. char path[512];
  323. QString home = QDir::homePath();
  324. QString currentFile = QT_UTF8(config_get_string(
  325. App()->GlobalConfig(), "Basic", "SceneCollectionFile"));
  326. int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
  327. if (ret <= 0) {
  328. blog(LOG_WARNING, "Failed to get scene collection config path");
  329. return;
  330. }
  331. QString exportFile = QFileDialog::getSaveFileName(
  332. this, QTStr("Basic.MainMenu.SceneCollection.Export"),
  333. home + "/" + currentFile, "JSON Files (*.json)");
  334. string file = QT_TO_UTF8(exportFile);
  335. if (!exportFile.isEmpty() && !exportFile.isNull()) {
  336. if (QFile::exists(exportFile))
  337. QFile::remove(exportFile);
  338. QFile::copy(path + currentFile + ".json", exportFile);
  339. }
  340. }
  341. void OBSBasic::ChangeSceneCollection()
  342. {
  343. QAction *action = reinterpret_cast<QAction *>(sender());
  344. std::string fileName;
  345. if (!action)
  346. return;
  347. fileName = QT_TO_UTF8(action->property("file_name").value<QString>());
  348. if (fileName.empty())
  349. return;
  350. const char *oldName = config_get_string(App()->GlobalConfig(), "Basic",
  351. "SceneCollection");
  352. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  353. action->setChecked(true);
  354. return;
  355. }
  356. SaveProjectNow();
  357. Load(fileName.c_str());
  358. RefreshSceneCollections();
  359. const char *newName = config_get_string(App()->GlobalConfig(), "Basic",
  360. "SceneCollection");
  361. const char *newFile = config_get_string(App()->GlobalConfig(), "Basic",
  362. "SceneCollectionFile");
  363. blog(LOG_INFO, "Switched to scene collection '%s' (%s.json)", newName,
  364. newFile);
  365. blog(LOG_INFO, "------------------------------------------------");
  366. UpdateTitleBar();
  367. if (api)
  368. api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  369. }