window-basic-main-profiles.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. int ret;
  123. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles/");
  124. if (ret <= 0) {
  125. blog(LOG_WARNING, "Failed to get profiles config path");
  126. return false;
  127. }
  128. strcat(path, fromPartial);
  129. strcat(path, "/*");
  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. ConfigFile config;
  157. if (!GetProfileName(this, newName, newDir, title, text, init_text))
  158. return false;
  159. std::string curDir = config_get_string(App()->GlobalConfig(),
  160. "Basic", "ProfileDir");
  161. char newPath[512];
  162. int ret = GetConfigPath(newPath, 512, "obs-studio/basic/profiles/");
  163. if (ret <= 0) {
  164. blog(LOG_WARNING, "Failed to get profiles config path");
  165. return false;
  166. }
  167. strcat(newPath, newDir.c_str());
  168. if (os_mkdir(newPath) < 0) {
  169. blog(LOG_WARNING, "Failed to create profile directory '%s'",
  170. newDir.c_str());
  171. return false;
  172. }
  173. if (!create_new)
  174. CopyProfile(curDir.c_str(), newPath);
  175. strcat(newPath, "/basic.ini");
  176. if (config.Open(newPath, CONFIG_OPEN_ALWAYS) != 0) {
  177. blog(LOG_ERROR, "Failed to open new config file '%s'",
  178. newDir.c_str());
  179. return false;
  180. }
  181. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  182. newName.c_str());
  183. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  184. newDir.c_str());
  185. config_set_string(config, "General", "Name", newName.c_str());
  186. config.SaveSafe("tmp");
  187. config.Swap(basicConfig);
  188. InitBasicConfigDefaults();
  189. RefreshProfiles();
  190. if (create_new)
  191. ResetProfileData();
  192. blog(LOG_INFO, "Created profile '%s' (%s, %s)", newName.c_str(),
  193. create_new ? "clean" : "duplicate", newDir.c_str());
  194. blog(LOG_INFO, "------------------------------------------------");
  195. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  196. UpdateTitleBar();
  197. if (api) {
  198. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  199. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  200. }
  201. return true;
  202. }
  203. void OBSBasic::DeleteProfile(const char *profileName, const char *profileDir)
  204. {
  205. char profilePath[512];
  206. char basePath[512];
  207. int ret = GetConfigPath(basePath, 512, "obs-studio/basic/profiles");
  208. if (ret <= 0) {
  209. blog(LOG_WARNING, "Failed to get profiles config path");
  210. return;
  211. }
  212. ret = snprintf(profilePath, 512, "%s/%s/*", basePath, profileDir);
  213. if (ret <= 0) {
  214. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  215. profileDir);
  216. return;
  217. }
  218. os_glob_t *glob;
  219. if (os_glob(profilePath, 0, &glob) != 0) {
  220. blog(LOG_WARNING, "Failed to glob profile dir '%s'",
  221. profileDir);
  222. return;
  223. }
  224. for (size_t i = 0; i < glob->gl_pathc; i++) {
  225. const char *filePath = glob->gl_pathv[i].path;
  226. if (glob->gl_pathv[i].directory)
  227. continue;
  228. os_unlink(filePath);
  229. }
  230. os_globfree(glob);
  231. ret = snprintf(profilePath, 512, "%s/%s", basePath, profileDir);
  232. if (ret <= 0) {
  233. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  234. profileDir);
  235. return;
  236. }
  237. os_rmdir(profilePath);
  238. blog(LOG_INFO, "------------------------------------------------");
  239. blog(LOG_INFO, "Removed profile '%s' (%s)",
  240. profileName, profileDir);
  241. blog(LOG_INFO, "------------------------------------------------");
  242. }
  243. void OBSBasic::RefreshProfiles()
  244. {
  245. QList<QAction*> menuActions = ui->profileMenu->actions();
  246. int count = 0;
  247. for (int i = 0; i < menuActions.count(); i++) {
  248. QVariant v = menuActions[i]->property("file_name");
  249. if (v.typeName() != nullptr)
  250. delete menuActions[i];
  251. }
  252. const char *curName = config_get_string(App()->GlobalConfig(),
  253. "Basic", "Profile");
  254. auto addProfile = [&](const char *name, const char *path)
  255. {
  256. std::string file = strrchr(path, '/') + 1;
  257. QAction *action = new QAction(QT_UTF8(name), this);
  258. action->setProperty("file_name", QT_UTF8(path));
  259. connect(action, &QAction::triggered,
  260. this, &OBSBasic::ChangeProfile);
  261. action->setCheckable(true);
  262. action->setChecked(strcmp(name, curName) == 0);
  263. ui->profileMenu->addAction(action);
  264. count++;
  265. return true;
  266. };
  267. EnumProfiles(addProfile);
  268. ui->actionRemoveProfile->setEnabled(count > 1);
  269. }
  270. void OBSBasic::ResetProfileData()
  271. {
  272. ResetVideo();
  273. service = nullptr;
  274. InitService();
  275. ResetOutputs();
  276. ClearHotkeys();
  277. CreateHotkeys();
  278. }
  279. void OBSBasic::on_actionNewProfile_triggered()
  280. {
  281. AddProfile(true, Str("AddProfile.Title"), Str("AddProfile.Text"));
  282. }
  283. void OBSBasic::on_actionDupProfile_triggered()
  284. {
  285. AddProfile(false, Str("AddProfile.Title"), Str("AddProfile.Text"));
  286. }
  287. void OBSBasic::on_actionRenameProfile_triggered()
  288. {
  289. std::string curDir = config_get_string(App()->GlobalConfig(),
  290. "Basic", "ProfileDir");
  291. std::string curName = config_get_string(App()->GlobalConfig(),
  292. "Basic", "Profile");
  293. /* Duplicate and delete in case there are any issues in the process */
  294. bool success = AddProfile(false, Str("RenameProfile.Title"),
  295. Str("AddProfile.Text"), curName.c_str());
  296. if (success) {
  297. DeleteProfile(curName.c_str(), curDir.c_str());
  298. RefreshProfiles();
  299. }
  300. if (api) {
  301. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  302. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  303. }
  304. }
  305. void OBSBasic::on_actionRemoveProfile_triggered()
  306. {
  307. std::string newName;
  308. std::string newPath;
  309. ConfigFile config;
  310. std::string oldDir = config_get_string(App()->GlobalConfig(),
  311. "Basic", "ProfileDir");
  312. std::string oldName = config_get_string(App()->GlobalConfig(),
  313. "Basic", "Profile");
  314. auto cb = [&](const char *name, const char *filePath)
  315. {
  316. if (strcmp(oldName.c_str(), name) != 0) {
  317. newName = name;
  318. newPath = filePath;
  319. return false;
  320. }
  321. return true;
  322. };
  323. EnumProfiles(cb);
  324. /* this should never be true due to menu item being grayed out */
  325. if (newPath.empty())
  326. return;
  327. QString text = QTStr("ConfirmRemove.Text");
  328. text.replace("$1", QT_UTF8(oldName.c_str()));
  329. QMessageBox::StandardButton button = OBSMessageBox::question(this,
  330. QTStr("ConfirmRemove.Title"), text);
  331. if (button == QMessageBox::No)
  332. return;
  333. size_t newPath_len = newPath.size();
  334. newPath += "/basic.ini";
  335. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  336. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  337. newPath.c_str());
  338. return;
  339. }
  340. newPath.resize(newPath_len);
  341. const char *newDir = strrchr(newPath.c_str(), '/') + 1;
  342. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  343. newName.c_str());
  344. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  345. newDir);
  346. config.Swap(basicConfig);
  347. InitBasicConfigDefaults();
  348. ResetProfileData();
  349. DeleteProfile(oldName.c_str(), oldDir.c_str());
  350. RefreshProfiles();
  351. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  352. blog(LOG_INFO, "Switched to profile '%s' (%s)",
  353. newName.c_str(), newDir);
  354. blog(LOG_INFO, "------------------------------------------------");
  355. UpdateTitleBar();
  356. if (api) {
  357. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  358. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  359. }
  360. }
  361. void OBSBasic::on_actionImportProfile_triggered()
  362. {
  363. char path[512];
  364. QString home = QDir::homePath();
  365. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  366. if (ret <= 0) {
  367. blog(LOG_WARNING, "Failed to get profile config path");
  368. return;
  369. }
  370. QString dir = QFileDialog::getExistingDirectory(
  371. this,
  372. QTStr("Basic.MainMenu.Profile.Import"),
  373. home,
  374. QFileDialog::ShowDirsOnly |
  375. QFileDialog::DontResolveSymlinks);
  376. if (!dir.isEmpty() && !dir.isNull()) {
  377. QString inputPath = QString::fromUtf8(path);
  378. QFileInfo finfo(dir);
  379. QString directory = finfo.fileName();
  380. QString profileDir = inputPath + directory;
  381. QDir folder(profileDir);
  382. if (!folder.exists()) {
  383. folder.mkpath(profileDir);
  384. QFile::copy(dir + "/basic.ini",
  385. profileDir + "/basic.ini");
  386. QFile::copy(dir + "/service.json",
  387. profileDir + "/service.json");
  388. QFile::copy(dir + "/streamEncoder.json",
  389. profileDir + "/streamEncoder.json");
  390. QFile::copy(dir + "/recordEncoder.json",
  391. profileDir + "/recordEncoder.json");
  392. RefreshProfiles();
  393. } else {
  394. OBSMessageBox::information(this,
  395. QTStr("Basic.MainMenu.Profile.Import"),
  396. QTStr("Basic.MainMenu.Profile.Exists"));
  397. }
  398. }
  399. }
  400. void OBSBasic::on_actionExportProfile_triggered()
  401. {
  402. char path[512];
  403. QString home = QDir::homePath();
  404. QString currentProfile =
  405. QString::fromUtf8(config_get_string(App()->GlobalConfig(),
  406. "Basic", "ProfileDir"));
  407. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  408. if (ret <= 0) {
  409. blog(LOG_WARNING, "Failed to get profile config path");
  410. return;
  411. }
  412. QString dir = QFileDialog::getExistingDirectory(
  413. this,
  414. QTStr("Basic.MainMenu.Profile.Export"),
  415. home,
  416. QFileDialog::ShowDirsOnly |
  417. QFileDialog::DontResolveSymlinks);
  418. if (!dir.isEmpty() && !dir.isNull()) {
  419. QString outputDir = dir + "/" + currentProfile;
  420. QString inputPath = QString::fromUtf8(path);
  421. QDir folder(outputDir);
  422. if (!folder.exists()) {
  423. folder.mkpath(outputDir);
  424. QFile::copy(inputPath + currentProfile + "/basic.ini",
  425. outputDir + "/basic.ini");
  426. QFile::copy(inputPath + currentProfile + "/service.json",
  427. outputDir + "/service.json");
  428. QFile::copy(inputPath + currentProfile + "/streamEncoder.json",
  429. outputDir + "/streamEncoder.json");
  430. QFile::copy(inputPath + currentProfile + "/recordEncoder.json",
  431. outputDir + "/recordEncoder.json");
  432. }
  433. }
  434. }
  435. void OBSBasic::ChangeProfile()
  436. {
  437. QAction *action = reinterpret_cast<QAction*>(sender());
  438. ConfigFile config;
  439. std::string path;
  440. if (!action)
  441. return;
  442. path = QT_TO_UTF8(action->property("file_name").value<QString>());
  443. if (path.empty())
  444. return;
  445. const char *oldName = config_get_string(App()->GlobalConfig(),
  446. "Basic", "Profile");
  447. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  448. action->setChecked(true);
  449. return;
  450. }
  451. size_t path_len = path.size();
  452. path += "/basic.ini";
  453. if (config.Open(path.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  454. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  455. path.c_str());
  456. return;
  457. }
  458. path.resize(path_len);
  459. const char *newName = config_get_string(config, "General", "Name");
  460. const char *newDir = strrchr(path.c_str(), '/') + 1;
  461. config_set_string(App()->GlobalConfig(), "Basic", "Profile", newName);
  462. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  463. newDir);
  464. config.Swap(basicConfig);
  465. InitBasicConfigDefaults();
  466. ResetProfileData();
  467. RefreshProfiles();
  468. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  469. UpdateTitleBar();
  470. blog(LOG_INFO, "Switched to profile '%s' (%s)",
  471. newName, newDir);
  472. blog(LOG_INFO, "------------------------------------------------");
  473. if (api)
  474. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  475. }