window-basic-main-profiles.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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/platform.h>
  16. #include <util/util.hpp>
  17. #include <QMessageBox>
  18. #include <QVariant>
  19. #include <QFileDialog>
  20. #include "window-basic-main.hpp"
  21. #include "window-namedialog.hpp"
  22. #include "qt-wrappers.hpp"
  23. void EnumProfiles(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/profiles/*");
  29. if (ret <= 0) {
  30. blog(LOG_WARNING, "Failed to get profiles config path");
  31. return;
  32. }
  33. if (os_glob(path, 0, &glob) != 0) {
  34. blog(LOG_WARNING, "Failed to glob profiles");
  35. return;
  36. }
  37. for (size_t i = 0; i < glob->gl_pathc; i++) {
  38. const char *filePath = glob->gl_pathv[i].path;
  39. const char *dirName = strrchr(filePath, '/') + 1;
  40. if (!glob->gl_pathv[i].directory)
  41. continue;
  42. if (strcmp(dirName, ".") == 0 ||
  43. strcmp(dirName, "..") == 0)
  44. continue;
  45. std::string file = filePath;
  46. file += "/basic.ini";
  47. ConfigFile config;
  48. int ret = config.Open(file.c_str(), CONFIG_OPEN_EXISTING);
  49. if (ret != CONFIG_SUCCESS)
  50. continue;
  51. const char *name = config_get_string(config, "General", "Name");
  52. if (!name)
  53. name = strrchr(filePath, '/') + 1;
  54. if (!cb(name, filePath))
  55. break;
  56. }
  57. os_globfree(glob);
  58. }
  59. static bool ProfileExists(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. EnumProfiles(func);
  71. return found;
  72. }
  73. static bool GetProfileName(QWidget *parent, std::string &name,
  74. std::string &file, const char *title, const char *text,
  75. const char *oldName = nullptr)
  76. {
  77. char path[512];
  78. int ret;
  79. for (;;) {
  80. bool success = NameDialog::AskForName(parent, title, text,
  81. name, QT_UTF8(oldName));
  82. if (!success) {
  83. return false;
  84. }
  85. if (name.empty()) {
  86. OBSMessageBox::information(parent,
  87. QTStr("NoNameEntered.Title"),
  88. QTStr("NoNameEntered.Text"));
  89. continue;
  90. }
  91. if (ProfileExists(name.c_str())) {
  92. OBSMessageBox::information(parent,
  93. QTStr("NameExists.Title"),
  94. QTStr("NameExists.Text"));
  95. continue;
  96. }
  97. break;
  98. }
  99. if (!GetFileSafeName(name.c_str(), file)) {
  100. blog(LOG_WARNING, "Failed to create safe file name for '%s'",
  101. name.c_str());
  102. return false;
  103. }
  104. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles/");
  105. if (ret <= 0) {
  106. blog(LOG_WARNING, "Failed to get profiles config path");
  107. return false;
  108. }
  109. file.insert(0, path);
  110. if (!GetClosestUnusedFileName(file, nullptr)) {
  111. blog(LOG_WARNING, "Failed to get closest file name for %s",
  112. file.c_str());
  113. return false;
  114. }
  115. file.erase(0, ret);
  116. return true;
  117. }
  118. static bool CopyProfile(const char *fromPartial, const char *to)
  119. {
  120. os_glob_t *glob;
  121. char path[512];
  122. char dir[512];
  123. int ret;
  124. ret = GetConfigPath(dir, sizeof(dir), "obs-studio/basic/profiles/");
  125. if (ret <= 0) {
  126. blog(LOG_WARNING, "Failed to get profiles config path");
  127. return false;
  128. }
  129. snprintf(path, sizeof(path), "%s%s/*", dir, fromPartial);
  130. if (os_glob(path, 0, &glob) != 0) {
  131. blog(LOG_WARNING, "Failed to glob profile '%s'", fromPartial);
  132. return false;
  133. }
  134. for (size_t i = 0; i < glob->gl_pathc; i++) {
  135. const char *filePath = glob->gl_pathv[i].path;
  136. if (glob->gl_pathv[i].directory)
  137. continue;
  138. ret = snprintf(path, sizeof(path), "%s/%s",
  139. to, strrchr(filePath, '/') + 1);
  140. if (ret > 0) {
  141. if (os_copyfile(filePath, path) != 0) {
  142. blog(LOG_WARNING, "CopyProfile: Failed to "
  143. "copy file %s to %s",
  144. filePath, path);
  145. }
  146. }
  147. }
  148. os_globfree(glob);
  149. return true;
  150. }
  151. bool OBSBasic::AddProfile(bool create_new, const char *title, const char *text,
  152. const char *init_text)
  153. {
  154. std::string newName;
  155. std::string newDir;
  156. std::string newPath;
  157. ConfigFile config;
  158. if (!GetProfileName(this, newName, newDir, title, text, init_text))
  159. return false;
  160. std::string curDir = config_get_string(App()->GlobalConfig(),
  161. "Basic", "ProfileDir");
  162. char baseDir[512];
  163. int ret = GetConfigPath(baseDir, sizeof(baseDir),
  164. "obs-studio/basic/profiles/");
  165. if (ret <= 0) {
  166. blog(LOG_WARNING, "Failed to get profiles config path");
  167. return false;
  168. }
  169. newPath = baseDir;
  170. newPath += newDir;
  171. if (os_mkdir(newPath.c_str()) < 0) {
  172. blog(LOG_WARNING, "Failed to create profile directory '%s'",
  173. newDir.c_str());
  174. return false;
  175. }
  176. if (!create_new)
  177. CopyProfile(curDir.c_str(), newPath.c_str());
  178. newPath += "/basic.ini";
  179. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  180. blog(LOG_ERROR, "Failed to open new config file '%s'",
  181. newDir.c_str());
  182. return false;
  183. }
  184. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  185. newName.c_str());
  186. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  187. newDir.c_str());
  188. config_set_string(config, "General", "Name", newName.c_str());
  189. config.SaveSafe("tmp");
  190. config.Swap(basicConfig);
  191. InitBasicConfigDefaults();
  192. RefreshProfiles();
  193. if (create_new)
  194. ResetProfileData();
  195. blog(LOG_INFO, "Created profile '%s' (%s, %s)", newName.c_str(),
  196. create_new ? "clean" : "duplicate", newDir.c_str());
  197. blog(LOG_INFO, "------------------------------------------------");
  198. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  199. UpdateTitleBar();
  200. if (api) {
  201. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  202. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  203. }
  204. return true;
  205. }
  206. void OBSBasic::DeleteProfile(const char *profileName, const char *profileDir)
  207. {
  208. char profilePath[512];
  209. char basePath[512];
  210. int ret = GetConfigPath(basePath, 512, "obs-studio/basic/profiles");
  211. if (ret <= 0) {
  212. blog(LOG_WARNING, "Failed to get profiles config path");
  213. return;
  214. }
  215. ret = snprintf(profilePath, 512, "%s/%s/*", basePath, profileDir);
  216. if (ret <= 0) {
  217. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  218. profileDir);
  219. return;
  220. }
  221. os_glob_t *glob;
  222. if (os_glob(profilePath, 0, &glob) != 0) {
  223. blog(LOG_WARNING, "Failed to glob profile dir '%s'",
  224. profileDir);
  225. return;
  226. }
  227. for (size_t i = 0; i < glob->gl_pathc; i++) {
  228. const char *filePath = glob->gl_pathv[i].path;
  229. if (glob->gl_pathv[i].directory)
  230. continue;
  231. os_unlink(filePath);
  232. }
  233. os_globfree(glob);
  234. ret = snprintf(profilePath, 512, "%s/%s", basePath, profileDir);
  235. if (ret <= 0) {
  236. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  237. profileDir);
  238. return;
  239. }
  240. os_rmdir(profilePath);
  241. blog(LOG_INFO, "------------------------------------------------");
  242. blog(LOG_INFO, "Removed profile '%s' (%s)",
  243. profileName, profileDir);
  244. blog(LOG_INFO, "------------------------------------------------");
  245. }
  246. void OBSBasic::RefreshProfiles()
  247. {
  248. QList<QAction*> menuActions = ui->profileMenu->actions();
  249. int count = 0;
  250. for (int i = 0; i < menuActions.count(); i++) {
  251. QVariant v = menuActions[i]->property("file_name");
  252. if (v.typeName() != nullptr)
  253. delete menuActions[i];
  254. }
  255. const char *curName = config_get_string(App()->GlobalConfig(),
  256. "Basic", "Profile");
  257. auto addProfile = [&](const char *name, const char *path)
  258. {
  259. std::string file = strrchr(path, '/') + 1;
  260. QAction *action = new QAction(QT_UTF8(name), this);
  261. action->setProperty("file_name", QT_UTF8(path));
  262. connect(action, &QAction::triggered,
  263. this, &OBSBasic::ChangeProfile);
  264. action->setCheckable(true);
  265. action->setChecked(strcmp(name, curName) == 0);
  266. ui->profileMenu->addAction(action);
  267. count++;
  268. return true;
  269. };
  270. EnumProfiles(addProfile);
  271. ui->actionRemoveProfile->setEnabled(count > 1);
  272. }
  273. void OBSBasic::ResetProfileData()
  274. {
  275. ResetVideo();
  276. service = nullptr;
  277. InitService();
  278. ResetOutputs();
  279. ClearHotkeys();
  280. CreateHotkeys();
  281. }
  282. void OBSBasic::on_actionNewProfile_triggered()
  283. {
  284. AddProfile(true, Str("AddProfile.Title"), Str("AddProfile.Text"));
  285. }
  286. void OBSBasic::on_actionDupProfile_triggered()
  287. {
  288. AddProfile(false, Str("AddProfile.Title"), Str("AddProfile.Text"));
  289. }
  290. void OBSBasic::on_actionRenameProfile_triggered()
  291. {
  292. std::string curDir = config_get_string(App()->GlobalConfig(),
  293. "Basic", "ProfileDir");
  294. std::string curName = config_get_string(App()->GlobalConfig(),
  295. "Basic", "Profile");
  296. /* Duplicate and delete in case there are any issues in the process */
  297. bool success = AddProfile(false, Str("RenameProfile.Title"),
  298. Str("AddProfile.Text"), curName.c_str());
  299. if (success) {
  300. DeleteProfile(curName.c_str(), curDir.c_str());
  301. RefreshProfiles();
  302. }
  303. if (api) {
  304. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  305. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  306. }
  307. }
  308. void OBSBasic::on_actionRemoveProfile_triggered()
  309. {
  310. std::string newName;
  311. std::string newPath;
  312. ConfigFile config;
  313. std::string oldDir = config_get_string(App()->GlobalConfig(),
  314. "Basic", "ProfileDir");
  315. std::string oldName = config_get_string(App()->GlobalConfig(),
  316. "Basic", "Profile");
  317. auto cb = [&](const char *name, const char *filePath)
  318. {
  319. if (strcmp(oldName.c_str(), name) != 0) {
  320. newName = name;
  321. newPath = filePath;
  322. return false;
  323. }
  324. return true;
  325. };
  326. EnumProfiles(cb);
  327. /* this should never be true due to menu item being grayed out */
  328. if (newPath.empty())
  329. return;
  330. QString text = QTStr("ConfirmRemove.Text");
  331. text.replace("$1", QT_UTF8(oldName.c_str()));
  332. QMessageBox::StandardButton button = OBSMessageBox::question(this,
  333. QTStr("ConfirmRemove.Title"), text);
  334. if (button == QMessageBox::No)
  335. return;
  336. size_t newPath_len = newPath.size();
  337. newPath += "/basic.ini";
  338. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  339. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  340. newPath.c_str());
  341. return;
  342. }
  343. newPath.resize(newPath_len);
  344. const char *newDir = strrchr(newPath.c_str(), '/') + 1;
  345. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  346. newName.c_str());
  347. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  348. newDir);
  349. config.Swap(basicConfig);
  350. InitBasicConfigDefaults();
  351. ResetProfileData();
  352. DeleteProfile(oldName.c_str(), oldDir.c_str());
  353. RefreshProfiles();
  354. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  355. blog(LOG_INFO, "Switched to profile '%s' (%s)",
  356. newName.c_str(), newDir);
  357. blog(LOG_INFO, "------------------------------------------------");
  358. UpdateTitleBar();
  359. if (api) {
  360. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  361. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  362. }
  363. }
  364. void OBSBasic::on_actionImportProfile_triggered()
  365. {
  366. char path[512];
  367. QString home = QDir::homePath();
  368. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  369. if (ret <= 0) {
  370. blog(LOG_WARNING, "Failed to get profile config path");
  371. return;
  372. }
  373. QString dir = QFileDialog::getExistingDirectory(
  374. this,
  375. QTStr("Basic.MainMenu.Profile.Import"),
  376. home,
  377. QFileDialog::ShowDirsOnly |
  378. QFileDialog::DontResolveSymlinks);
  379. if (!dir.isEmpty() && !dir.isNull()) {
  380. QString inputPath = QString::fromUtf8(path);
  381. QFileInfo finfo(dir);
  382. QString directory = finfo.fileName();
  383. QString profileDir = inputPath + directory;
  384. QDir folder(profileDir);
  385. if (!folder.exists()) {
  386. folder.mkpath(profileDir);
  387. QFile::copy(dir + "/basic.ini",
  388. profileDir + "/basic.ini");
  389. QFile::copy(dir + "/service.json",
  390. profileDir + "/service.json");
  391. QFile::copy(dir + "/streamEncoder.json",
  392. profileDir + "/streamEncoder.json");
  393. QFile::copy(dir + "/recordEncoder.json",
  394. profileDir + "/recordEncoder.json");
  395. RefreshProfiles();
  396. } else {
  397. OBSMessageBox::information(this,
  398. QTStr("Basic.MainMenu.Profile.Import"),
  399. QTStr("Basic.MainMenu.Profile.Exists"));
  400. }
  401. }
  402. }
  403. void OBSBasic::on_actionExportProfile_triggered()
  404. {
  405. char path[512];
  406. QString home = QDir::homePath();
  407. QString currentProfile =
  408. QString::fromUtf8(config_get_string(App()->GlobalConfig(),
  409. "Basic", "ProfileDir"));
  410. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  411. if (ret <= 0) {
  412. blog(LOG_WARNING, "Failed to get profile config path");
  413. return;
  414. }
  415. QString dir = QFileDialog::getExistingDirectory(
  416. this,
  417. QTStr("Basic.MainMenu.Profile.Export"),
  418. home,
  419. QFileDialog::ShowDirsOnly |
  420. QFileDialog::DontResolveSymlinks);
  421. if (!dir.isEmpty() && !dir.isNull()) {
  422. QString outputDir = dir + "/" + currentProfile;
  423. QString inputPath = QString::fromUtf8(path);
  424. QDir folder(outputDir);
  425. if (!folder.exists()) {
  426. folder.mkpath(outputDir);
  427. } else {
  428. if (QFile::exists(outputDir + "/basic.ini"))
  429. QFile::remove(outputDir + "/basic.ini");
  430. if (QFile::exists(outputDir + "/service.json"))
  431. QFile::remove(outputDir + "/service.json");
  432. if (QFile::exists(outputDir + "/streamEncoder.json"))
  433. QFile::remove(outputDir + "/streamEncoder.json");
  434. if (QFile::exists(outputDir + "/recordEncoder.json"))
  435. QFile::remove(outputDir + "/recordEncoder.json");
  436. }
  437. QFile::copy(inputPath + currentProfile + "/basic.ini",
  438. outputDir + "/basic.ini");
  439. QFile::copy(inputPath + currentProfile + "/service.json",
  440. outputDir + "/service.json");
  441. QFile::copy(inputPath + currentProfile + "/streamEncoder.json",
  442. outputDir + "/streamEncoder.json");
  443. QFile::copy(inputPath + currentProfile + "/recordEncoder.json",
  444. outputDir + "/recordEncoder.json");
  445. }
  446. }
  447. void OBSBasic::ChangeProfile()
  448. {
  449. QAction *action = reinterpret_cast<QAction*>(sender());
  450. ConfigFile config;
  451. std::string path;
  452. if (!action)
  453. return;
  454. path = QT_TO_UTF8(action->property("file_name").value<QString>());
  455. if (path.empty())
  456. return;
  457. const char *oldName = config_get_string(App()->GlobalConfig(),
  458. "Basic", "Profile");
  459. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  460. action->setChecked(true);
  461. return;
  462. }
  463. size_t path_len = path.size();
  464. path += "/basic.ini";
  465. if (config.Open(path.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  466. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  467. path.c_str());
  468. return;
  469. }
  470. path.resize(path_len);
  471. const char *newName = config_get_string(config, "General", "Name");
  472. const char *newDir = strrchr(path.c_str(), '/') + 1;
  473. config_set_string(App()->GlobalConfig(), "Basic", "Profile", newName);
  474. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  475. newDir);
  476. config.Swap(basicConfig);
  477. InitBasicConfigDefaults();
  478. ResetProfileData();
  479. RefreshProfiles();
  480. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  481. UpdateTitleBar();
  482. blog(LOG_INFO, "Switched to profile '%s' (%s)",
  483. newName, newDir);
  484. blog(LOG_INFO, "------------------------------------------------");
  485. if (api)
  486. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  487. }