window-basic-main-profiles.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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-basic-auto-config.hpp"
  22. #include "window-namedialog.hpp"
  23. #include "qt-wrappers.hpp"
  24. extern void DestroyPanelCookieManager();
  25. extern void DuplicateCurrentCookieProfile(ConfigFile &config);
  26. extern void CheckExistingCookieId();
  27. extern void DeleteCookies();
  28. void EnumProfiles(std::function<bool(const char *, const char *)> &&cb)
  29. {
  30. char path[512];
  31. os_glob_t *glob;
  32. int ret = GetConfigPath(path, sizeof(path),
  33. "obs-studio/basic/profiles/*");
  34. if (ret <= 0) {
  35. blog(LOG_WARNING, "Failed to get profiles config path");
  36. return;
  37. }
  38. if (os_glob(path, 0, &glob) != 0) {
  39. blog(LOG_WARNING, "Failed to glob profiles");
  40. return;
  41. }
  42. for (size_t i = 0; i < glob->gl_pathc; i++) {
  43. const char *filePath = glob->gl_pathv[i].path;
  44. const char *dirName = strrchr(filePath, '/') + 1;
  45. if (!glob->gl_pathv[i].directory)
  46. continue;
  47. if (strcmp(dirName, ".") == 0 || strcmp(dirName, "..") == 0)
  48. continue;
  49. std::string file = filePath;
  50. file += "/basic.ini";
  51. ConfigFile config;
  52. int ret = config.Open(file.c_str(), CONFIG_OPEN_EXISTING);
  53. if (ret != CONFIG_SUCCESS)
  54. continue;
  55. const char *name = config_get_string(config, "General", "Name");
  56. if (!name)
  57. name = strrchr(filePath, '/') + 1;
  58. if (!cb(name, filePath))
  59. break;
  60. }
  61. os_globfree(glob);
  62. }
  63. static bool GetProfileDir(const char *findName, const char *&profileDir)
  64. {
  65. bool found = false;
  66. auto func = [&](const char *name, const char *path) {
  67. if (strcmp(name, findName) == 0) {
  68. found = true;
  69. profileDir = strrchr(path, '/') + 1;
  70. return false;
  71. }
  72. return true;
  73. };
  74. EnumProfiles(func);
  75. return found;
  76. }
  77. static bool ProfileExists(const char *findName)
  78. {
  79. const char *profileDir = nullptr;
  80. return GetProfileDir(findName, profileDir);
  81. }
  82. static bool AskForProfileName(QWidget *parent, std::string &name,
  83. const char *title, const char *text,
  84. const bool showWizard, bool &wizardChecked,
  85. const char *oldName = nullptr)
  86. {
  87. for (;;) {
  88. bool success = false;
  89. if (showWizard) {
  90. success = NameDialog::AskForNameWithOption(
  91. parent, title, text, name,
  92. QTStr("AddProfile.WizardCheckbox"),
  93. wizardChecked, QT_UTF8(oldName));
  94. } else {
  95. success = NameDialog::AskForName(
  96. parent, title, text, name, QT_UTF8(oldName));
  97. }
  98. if (!success) {
  99. return false;
  100. }
  101. if (name.empty()) {
  102. OBSMessageBox::warning(parent,
  103. QTStr("NoNameEntered.Title"),
  104. QTStr("NoNameEntered.Text"));
  105. continue;
  106. }
  107. if (ProfileExists(name.c_str())) {
  108. OBSMessageBox::warning(parent,
  109. QTStr("NameExists.Title"),
  110. QTStr("NameExists.Text"));
  111. continue;
  112. }
  113. break;
  114. }
  115. return true;
  116. }
  117. static bool FindSafeProfileDirName(const std::string &profileName,
  118. std::string &dirName)
  119. {
  120. char path[512];
  121. int ret;
  122. if (ProfileExists(profileName.c_str())) {
  123. blog(LOG_WARNING, "Profile '%s' exists", profileName.c_str());
  124. return false;
  125. }
  126. if (!GetFileSafeName(profileName.c_str(), dirName)) {
  127. blog(LOG_WARNING, "Failed to create safe file name for '%s'",
  128. profileName.c_str());
  129. return false;
  130. }
  131. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles/");
  132. if (ret <= 0) {
  133. blog(LOG_WARNING, "Failed to get profiles config path");
  134. return false;
  135. }
  136. dirName.insert(0, path);
  137. if (!GetClosestUnusedFileName(dirName, nullptr)) {
  138. blog(LOG_WARNING, "Failed to get closest file name for %s",
  139. dirName.c_str());
  140. return false;
  141. }
  142. dirName.erase(0, ret);
  143. return true;
  144. }
  145. static bool CopyProfile(const char *fromPartial, const char *to)
  146. {
  147. os_glob_t *glob;
  148. char path[514];
  149. char dir[512];
  150. int ret;
  151. ret = GetConfigPath(dir, sizeof(dir), "obs-studio/basic/profiles/");
  152. if (ret <= 0) {
  153. blog(LOG_WARNING, "Failed to get profiles config path");
  154. return false;
  155. }
  156. snprintf(path, sizeof(path), "%s%s/*", dir, fromPartial);
  157. if (os_glob(path, 0, &glob) != 0) {
  158. blog(LOG_WARNING, "Failed to glob profile '%s'", fromPartial);
  159. return false;
  160. }
  161. for (size_t i = 0; i < glob->gl_pathc; i++) {
  162. const char *filePath = glob->gl_pathv[i].path;
  163. if (glob->gl_pathv[i].directory)
  164. continue;
  165. ret = snprintf(path, sizeof(path), "%s/%s", to,
  166. strrchr(filePath, '/') + 1);
  167. if (ret > 0) {
  168. if (os_copyfile(filePath, path) != 0) {
  169. blog(LOG_WARNING,
  170. "CopyProfile: Failed to "
  171. "copy file %s to %s",
  172. filePath, path);
  173. }
  174. }
  175. }
  176. os_globfree(glob);
  177. return true;
  178. }
  179. bool OBSBasic::AddProfile(bool create_new, const char *title, const char *text,
  180. const char *init_text, bool rename)
  181. {
  182. std::string name;
  183. bool showWizardChecked = config_get_bool(App()->GlobalConfig(), "Basic",
  184. "ConfigOnNewProfile");
  185. if (!AskForProfileName(this, name, title, text, create_new,
  186. showWizardChecked, init_text))
  187. return false;
  188. return CreateProfile(name, create_new, showWizardChecked, rename);
  189. }
  190. bool OBSBasic::CreateProfile(const std::string &newName, bool create_new,
  191. bool showWizardChecked, bool rename)
  192. {
  193. std::string newDir;
  194. std::string newPath;
  195. ConfigFile config;
  196. if (!FindSafeProfileDirName(newName, newDir))
  197. return false;
  198. if (create_new) {
  199. config_set_bool(App()->GlobalConfig(), "Basic",
  200. "ConfigOnNewProfile", showWizardChecked);
  201. }
  202. std::string curDir =
  203. config_get_string(App()->GlobalConfig(), "Basic", "ProfileDir");
  204. char baseDir[512];
  205. int ret = GetConfigPath(baseDir, sizeof(baseDir),
  206. "obs-studio/basic/profiles/");
  207. if (ret <= 0) {
  208. blog(LOG_WARNING, "Failed to get profiles config path");
  209. return false;
  210. }
  211. newPath = baseDir;
  212. newPath += newDir;
  213. if (os_mkdir(newPath.c_str()) < 0) {
  214. blog(LOG_WARNING, "Failed to create profile directory '%s'",
  215. newDir.c_str());
  216. return false;
  217. }
  218. if (!create_new)
  219. CopyProfile(curDir.c_str(), newPath.c_str());
  220. newPath += "/basic.ini";
  221. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  222. blog(LOG_ERROR, "Failed to open new config file '%s'",
  223. newDir.c_str());
  224. return false;
  225. }
  226. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  227. newName.c_str());
  228. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  229. newDir.c_str());
  230. Auth::Save();
  231. if (create_new) {
  232. auth.reset();
  233. DestroyPanelCookieManager();
  234. } else if (!rename) {
  235. DuplicateCurrentCookieProfile(config);
  236. }
  237. config_set_string(config, "General", "Name", newName.c_str());
  238. basicConfig.SaveSafe("tmp");
  239. config.SaveSafe("tmp");
  240. config.Swap(basicConfig);
  241. InitBasicConfigDefaults();
  242. InitBasicConfigDefaults2();
  243. RefreshProfiles();
  244. if (create_new)
  245. ResetProfileData();
  246. blog(LOG_INFO, "Created profile '%s' (%s, %s)", newName.c_str(),
  247. create_new ? "clean" : "duplicate", newDir.c_str());
  248. blog(LOG_INFO, "------------------------------------------------");
  249. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  250. UpdateTitleBar();
  251. // Run auto configuration setup wizard when a new profile is made to assist
  252. // setting up blank settings
  253. if (create_new && showWizardChecked) {
  254. AutoConfig wizard(this);
  255. wizard.setModal(true);
  256. wizard.show();
  257. wizard.exec();
  258. }
  259. if (api) {
  260. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  261. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  262. }
  263. return true;
  264. }
  265. bool OBSBasic::NewProfile(const QString &name)
  266. {
  267. return CreateProfile(name.toStdString(), true, false, false);
  268. }
  269. bool OBSBasic::DuplicateProfile(const QString &name)
  270. {
  271. return CreateProfile(name.toStdString(), false, false, false);
  272. }
  273. void OBSBasic::DeleteProfile(const char *profileName, const char *profileDir)
  274. {
  275. char profilePath[512];
  276. char basePath[512];
  277. int ret = GetConfigPath(basePath, 512, "obs-studio/basic/profiles");
  278. if (ret <= 0) {
  279. blog(LOG_WARNING, "Failed to get profiles config path");
  280. return;
  281. }
  282. ret = snprintf(profilePath, 512, "%s/%s/*", basePath, profileDir);
  283. if (ret <= 0) {
  284. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  285. profileDir);
  286. return;
  287. }
  288. os_glob_t *glob;
  289. if (os_glob(profilePath, 0, &glob) != 0) {
  290. blog(LOG_WARNING, "Failed to glob profile dir '%s'",
  291. profileDir);
  292. return;
  293. }
  294. for (size_t i = 0; i < glob->gl_pathc; i++) {
  295. const char *filePath = glob->gl_pathv[i].path;
  296. if (glob->gl_pathv[i].directory)
  297. continue;
  298. os_unlink(filePath);
  299. }
  300. os_globfree(glob);
  301. ret = snprintf(profilePath, 512, "%s/%s", basePath, profileDir);
  302. if (ret <= 0) {
  303. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  304. profileDir);
  305. return;
  306. }
  307. os_rmdir(profilePath);
  308. blog(LOG_INFO, "------------------------------------------------");
  309. blog(LOG_INFO, "Removed profile '%s' (%s)", profileName, profileDir);
  310. blog(LOG_INFO, "------------------------------------------------");
  311. }
  312. void OBSBasic::DeleteProfile(const QString &profileName)
  313. {
  314. std::string name = profileName.toStdString();
  315. const char *curName =
  316. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  317. if (strcmp(curName, name.c_str()) == 0) {
  318. on_actionRemoveProfile_triggered(true);
  319. return;
  320. }
  321. const char *profileDir = nullptr;
  322. if (!GetProfileDir(name.c_str(), profileDir)) {
  323. blog(LOG_WARNING, "Profile '%s' not found", name.c_str());
  324. return;
  325. }
  326. if (!profileDir) {
  327. blog(LOG_WARNING, "Failed to get profile dir for profile '%s'",
  328. name.c_str());
  329. return;
  330. }
  331. DeleteProfile(name.c_str(), profileDir);
  332. RefreshProfiles();
  333. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  334. if (api)
  335. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  336. }
  337. void OBSBasic::RefreshProfiles()
  338. {
  339. QList<QAction *> menuActions = ui->profileMenu->actions();
  340. int count = 0;
  341. for (int i = 0; i < menuActions.count(); i++) {
  342. QVariant v = menuActions[i]->property("file_name");
  343. if (v.typeName() != nullptr)
  344. delete menuActions[i];
  345. }
  346. const char *curName =
  347. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  348. auto addProfile = [&](const char *name, const char *path) {
  349. std::string file = strrchr(path, '/') + 1;
  350. QAction *action = new QAction(QT_UTF8(name), this);
  351. action->setProperty("file_name", QT_UTF8(path));
  352. connect(action, &QAction::triggered, this,
  353. &OBSBasic::ChangeProfile);
  354. action->setCheckable(true);
  355. action->setChecked(strcmp(name, curName) == 0);
  356. ui->profileMenu->addAction(action);
  357. count++;
  358. return true;
  359. };
  360. EnumProfiles(addProfile);
  361. ui->actionRemoveProfile->setEnabled(count > 1);
  362. }
  363. void OBSBasic::ResetProfileData()
  364. {
  365. ResetVideo();
  366. service = nullptr;
  367. InitService();
  368. ResetOutputs();
  369. ClearHotkeys();
  370. CreateHotkeys();
  371. /* load audio monitoring */
  372. #if defined(_WIN32) || defined(__APPLE__) || HAVE_PULSEAUDIO
  373. const char *device_name =
  374. config_get_string(basicConfig, "Audio", "MonitoringDeviceName");
  375. const char *device_id =
  376. config_get_string(basicConfig, "Audio", "MonitoringDeviceId");
  377. obs_set_audio_monitoring_device(device_name, device_id);
  378. blog(LOG_INFO, "Audio monitoring device:\n\tname: %s\n\tid: %s",
  379. device_name, device_id);
  380. #endif
  381. }
  382. void OBSBasic::on_actionNewProfile_triggered()
  383. {
  384. AddProfile(true, Str("AddProfile.Title"), Str("AddProfile.Text"));
  385. }
  386. void OBSBasic::on_actionDupProfile_triggered()
  387. {
  388. AddProfile(false, Str("AddProfile.Title"), Str("AddProfile.Text"));
  389. }
  390. void OBSBasic::on_actionRenameProfile_triggered()
  391. {
  392. std::string curDir =
  393. config_get_string(App()->GlobalConfig(), "Basic", "ProfileDir");
  394. std::string curName =
  395. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  396. /* Duplicate and delete in case there are any issues in the process */
  397. bool success = AddProfile(false, Str("RenameProfile.Title"),
  398. Str("AddProfile.Text"), curName.c_str(),
  399. true);
  400. if (success) {
  401. DeleteProfile(curName.c_str(), curDir.c_str());
  402. RefreshProfiles();
  403. }
  404. if (api) {
  405. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  406. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  407. }
  408. }
  409. void OBSBasic::on_actionRemoveProfile_triggered(bool skipConfirmation)
  410. {
  411. std::string newName;
  412. std::string newPath;
  413. ConfigFile config;
  414. std::string oldDir =
  415. config_get_string(App()->GlobalConfig(), "Basic", "ProfileDir");
  416. std::string oldName =
  417. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  418. auto cb = [&](const char *name, const char *filePath) {
  419. if (strcmp(oldName.c_str(), name) != 0) {
  420. newName = name;
  421. newPath = filePath;
  422. return false;
  423. }
  424. return true;
  425. };
  426. EnumProfiles(cb);
  427. /* this should never be true due to menu item being grayed out */
  428. if (newPath.empty())
  429. return;
  430. if (!skipConfirmation) {
  431. QString text = QTStr("ConfirmRemove.Text");
  432. text.replace("$1", QT_UTF8(oldName.c_str()));
  433. QMessageBox::StandardButton button = OBSMessageBox::question(
  434. this, QTStr("ConfirmRemove.Title"), text);
  435. if (button == QMessageBox::No)
  436. return;
  437. }
  438. size_t newPath_len = newPath.size();
  439. newPath += "/basic.ini";
  440. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  441. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  442. newPath.c_str());
  443. return;
  444. }
  445. newPath.resize(newPath_len);
  446. const char *newDir = strrchr(newPath.c_str(), '/') + 1;
  447. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  448. newName.c_str());
  449. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir", newDir);
  450. Auth::Save();
  451. auth.reset();
  452. DeleteCookies();
  453. DestroyPanelCookieManager();
  454. config.Swap(basicConfig);
  455. InitBasicConfigDefaults();
  456. InitBasicConfigDefaults2();
  457. ResetProfileData();
  458. DeleteProfile(oldName.c_str(), oldDir.c_str());
  459. RefreshProfiles();
  460. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  461. blog(LOG_INFO, "Switched to profile '%s' (%s)", newName.c_str(),
  462. newDir);
  463. blog(LOG_INFO, "------------------------------------------------");
  464. UpdateTitleBar();
  465. Auth::Load();
  466. if (api) {
  467. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  468. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  469. }
  470. }
  471. void OBSBasic::on_actionImportProfile_triggered()
  472. {
  473. char path[512];
  474. QString home = QDir::homePath();
  475. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  476. if (ret <= 0) {
  477. blog(LOG_WARNING, "Failed to get profile config path");
  478. return;
  479. }
  480. QString dir = SelectDirectory(
  481. this, QTStr("Basic.MainMenu.Profile.Import"), home);
  482. if (!dir.isEmpty() && !dir.isNull()) {
  483. QString inputPath = QString::fromUtf8(path);
  484. QFileInfo finfo(dir);
  485. QString directory = finfo.fileName();
  486. QString profileDir = inputPath + directory;
  487. if (ProfileExists(directory.toStdString().c_str())) {
  488. OBSMessageBox::warning(
  489. this, QTStr("Basic.MainMenu.Profile.Import"),
  490. QTStr("Basic.MainMenu.Profile.Exists"));
  491. } else if (os_mkdir(profileDir.toStdString().c_str()) < 0) {
  492. blog(LOG_WARNING,
  493. "Failed to create profile directory '%s'",
  494. directory.toStdString().c_str());
  495. } else {
  496. QFile::copy(dir + "/basic.ini",
  497. profileDir + "/basic.ini");
  498. QFile::copy(dir + "/service.json",
  499. profileDir + "/service.json");
  500. QFile::copy(dir + "/streamEncoder.json",
  501. profileDir + "/streamEncoder.json");
  502. QFile::copy(dir + "/recordEncoder.json",
  503. profileDir + "/recordEncoder.json");
  504. RefreshProfiles();
  505. }
  506. }
  507. }
  508. void OBSBasic::on_actionExportProfile_triggered()
  509. {
  510. char path[512];
  511. QString home = QDir::homePath();
  512. QString currentProfile = QString::fromUtf8(config_get_string(
  513. App()->GlobalConfig(), "Basic", "ProfileDir"));
  514. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  515. if (ret <= 0) {
  516. blog(LOG_WARNING, "Failed to get profile config path");
  517. return;
  518. }
  519. QString dir = SelectDirectory(
  520. this, QTStr("Basic.MainMenu.Profile.Export"), home);
  521. if (!dir.isEmpty() && !dir.isNull()) {
  522. QString outputDir = dir + "/" + currentProfile;
  523. QString inputPath = QString::fromUtf8(path);
  524. QDir folder(outputDir);
  525. if (!folder.exists()) {
  526. folder.mkpath(outputDir);
  527. } else {
  528. if (QFile::exists(outputDir + "/basic.ini"))
  529. QFile::remove(outputDir + "/basic.ini");
  530. if (QFile::exists(outputDir + "/service.json"))
  531. QFile::remove(outputDir + "/service.json");
  532. if (QFile::exists(outputDir + "/streamEncoder.json"))
  533. QFile::remove(outputDir +
  534. "/streamEncoder.json");
  535. if (QFile::exists(outputDir + "/recordEncoder.json"))
  536. QFile::remove(outputDir +
  537. "/recordEncoder.json");
  538. }
  539. QFile::copy(inputPath + currentProfile + "/basic.ini",
  540. outputDir + "/basic.ini");
  541. QFile::copy(inputPath + currentProfile + "/service.json",
  542. outputDir + "/service.json");
  543. QFile::copy(inputPath + currentProfile + "/streamEncoder.json",
  544. outputDir + "/streamEncoder.json");
  545. QFile::copy(inputPath + currentProfile + "/recordEncoder.json",
  546. outputDir + "/recordEncoder.json");
  547. }
  548. }
  549. void OBSBasic::ChangeProfile()
  550. {
  551. QAction *action = reinterpret_cast<QAction *>(sender());
  552. ConfigFile config;
  553. std::string path;
  554. if (!action)
  555. return;
  556. path = QT_TO_UTF8(action->property("file_name").value<QString>());
  557. if (path.empty())
  558. return;
  559. const char *oldName =
  560. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  561. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  562. action->setChecked(true);
  563. return;
  564. }
  565. size_t path_len = path.size();
  566. path += "/basic.ini";
  567. if (config.Open(path.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  568. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  569. path.c_str());
  570. return;
  571. }
  572. path.resize(path_len);
  573. const char *newName = config_get_string(config, "General", "Name");
  574. const char *newDir = strrchr(path.c_str(), '/') + 1;
  575. config_set_string(App()->GlobalConfig(), "Basic", "Profile", newName);
  576. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir", newDir);
  577. Auth::Save();
  578. auth.reset();
  579. DestroyPanelCookieManager();
  580. config.Swap(basicConfig);
  581. InitBasicConfigDefaults();
  582. InitBasicConfigDefaults2();
  583. ResetProfileData();
  584. RefreshProfiles();
  585. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  586. UpdateTitleBar();
  587. Auth::Load();
  588. CheckForSimpleModeX264Fallback();
  589. blog(LOG_INFO, "Switched to profile '%s' (%s)", newName, newDir);
  590. blog(LOG_INFO, "------------------------------------------------");
  591. if (api)
  592. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  593. }
  594. void OBSBasic::CheckForSimpleModeX264Fallback()
  595. {
  596. const char *curStreamEncoder =
  597. config_get_string(basicConfig, "SimpleOutput", "StreamEncoder");
  598. const char *curRecEncoder =
  599. config_get_string(basicConfig, "SimpleOutput", "RecEncoder");
  600. bool qsv_supported = false;
  601. bool amd_supported = false;
  602. bool nve_supported = false;
  603. bool changed = false;
  604. size_t idx = 0;
  605. const char *id;
  606. while (obs_enum_encoder_types(idx++, &id)) {
  607. if (strcmp(id, "amd_amf_h264") == 0)
  608. amd_supported = true;
  609. else if (strcmp(id, "obs_qsv11") == 0)
  610. qsv_supported = true;
  611. else if (strcmp(id, "ffmpeg_nvenc") == 0)
  612. nve_supported = true;
  613. }
  614. auto CheckEncoder = [&](const char *&name) {
  615. if (strcmp(name, SIMPLE_ENCODER_QSV) == 0) {
  616. if (!qsv_supported) {
  617. changed = true;
  618. name = SIMPLE_ENCODER_X264;
  619. return false;
  620. }
  621. } else if (strcmp(name, SIMPLE_ENCODER_NVENC) == 0) {
  622. if (!nve_supported) {
  623. changed = true;
  624. name = SIMPLE_ENCODER_X264;
  625. return false;
  626. }
  627. } else if (strcmp(name, SIMPLE_ENCODER_AMD) == 0) {
  628. if (!amd_supported) {
  629. changed = true;
  630. name = SIMPLE_ENCODER_X264;
  631. return false;
  632. }
  633. }
  634. return true;
  635. };
  636. if (!CheckEncoder(curStreamEncoder))
  637. config_set_string(basicConfig, "SimpleOutput", "StreamEncoder",
  638. curStreamEncoder);
  639. if (!CheckEncoder(curRecEncoder))
  640. config_set_string(basicConfig, "SimpleOutput", "RecEncoder",
  641. curRecEncoder);
  642. if (changed)
  643. config_save_safe(basicConfig, "tmp", nullptr);
  644. }