window-basic-main-profiles.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 <filesystem>
  15. #include <functional>
  16. #include <string>
  17. #include <map>
  18. #include <tuple>
  19. #include <obs.hpp>
  20. #include <util/platform.h>
  21. #include <util/util.hpp>
  22. #include <QMessageBox>
  23. #include <QVariant>
  24. #include <QFileDialog>
  25. #include <qt-wrappers.hpp>
  26. #include "window-basic-main.hpp"
  27. #include "window-basic-auto-config.hpp"
  28. #include "window-namedialog.hpp"
  29. // MARK: Constant Expressions
  30. constexpr std::string_view OBSProfilePath = "/obs-studio/basic/profiles/";
  31. constexpr std::string_view OBSProfileSettingsFile = "basic.ini";
  32. // MARK: Forward Declarations
  33. extern void DestroyPanelCookieManager();
  34. extern void DuplicateCurrentCookieProfile(ConfigFile &config);
  35. extern void CheckExistingCookieId();
  36. extern void DeleteCookies();
  37. // MARK: - Main Profile Management Functions
  38. void OBSBasic::SetupNewProfile(const std::string &profileName, bool useWizard)
  39. {
  40. const OBSProfile &newProfile = CreateProfile(profileName);
  41. config_set_bool(App()->GetUserConfig(), "Basic", "ConfigOnNewProfile",
  42. useWizard);
  43. ActivateProfile(newProfile, true);
  44. blog(LOG_INFO, "Created profile '%s' (clean, %s)",
  45. newProfile.name.c_str(), newProfile.directoryName.c_str());
  46. blog(LOG_INFO, "------------------------------------------------");
  47. if (useWizard) {
  48. AutoConfig wizard(this);
  49. wizard.setModal(true);
  50. wizard.show();
  51. wizard.exec();
  52. }
  53. }
  54. void OBSBasic::SetupDuplicateProfile(const std::string &profileName)
  55. {
  56. const OBSProfile &newProfile = CreateProfile(profileName);
  57. const OBSProfile &currentProfile = GetCurrentProfile();
  58. const auto copyOptions =
  59. std::filesystem::copy_options::recursive |
  60. std::filesystem::copy_options::overwrite_existing;
  61. try {
  62. std::filesystem::copy(currentProfile.path, newProfile.path,
  63. copyOptions);
  64. } catch (const std::filesystem::filesystem_error &error) {
  65. blog(LOG_DEBUG, "%s", error.what());
  66. throw std::logic_error(
  67. "Failed to copy files for cloned profile: " +
  68. newProfile.name);
  69. }
  70. ActivateProfile(newProfile);
  71. blog(LOG_INFO, "Created profile '%s' (duplicate, %s)",
  72. newProfile.name.c_str(), newProfile.directoryName.c_str());
  73. blog(LOG_INFO, "------------------------------------------------");
  74. }
  75. void OBSBasic::SetupRenameProfile(const std::string &profileName)
  76. {
  77. const OBSProfile &newProfile = CreateProfile(profileName);
  78. const OBSProfile currentProfile = GetCurrentProfile();
  79. const auto copyOptions =
  80. std::filesystem::copy_options::recursive |
  81. std::filesystem::copy_options::overwrite_existing;
  82. try {
  83. std::filesystem::copy(currentProfile.path, newProfile.path,
  84. copyOptions);
  85. } catch (const std::filesystem::filesystem_error &error) {
  86. blog(LOG_DEBUG, "%s", error.what());
  87. throw std::logic_error("Failed to copy files for profile: " +
  88. currentProfile.name);
  89. }
  90. profiles.erase(currentProfile.name);
  91. ActivateProfile(newProfile);
  92. RemoveProfile(currentProfile);
  93. blog(LOG_INFO, "Renamed profile '%s' to '%s' (%s)",
  94. currentProfile.name.c_str(), newProfile.name.c_str(),
  95. newProfile.directoryName.c_str());
  96. blog(LOG_INFO, "------------------------------------------------");
  97. OnEvent(OBS_FRONTEND_EVENT_PROFILE_RENAMED);
  98. }
  99. // MARK: - Profile File Management Functions
  100. const OBSProfile &OBSBasic::CreateProfile(const std::string &profileName)
  101. {
  102. if (const auto &foundProfile = GetProfileByName(profileName)) {
  103. throw std::invalid_argument("Profile already exists: " +
  104. profileName);
  105. }
  106. std::string directoryName;
  107. if (!GetFileSafeName(profileName.c_str(), directoryName)) {
  108. throw std::invalid_argument(
  109. "Failed to create safe directory for new profile: " +
  110. profileName);
  111. }
  112. std::string profileDirectory;
  113. profileDirectory.reserve(App()->userProfilesLocation.u8string().size() +
  114. OBSProfilePath.size() + directoryName.size());
  115. profileDirectory.append(App()->userProfilesLocation.u8string())
  116. .append(OBSProfilePath)
  117. .append(directoryName);
  118. if (!GetClosestUnusedFileName(profileDirectory, nullptr)) {
  119. throw std::invalid_argument(
  120. "Failed to get closest directory name for new profile: " +
  121. profileName);
  122. }
  123. const std::filesystem::path profileDirectoryPath =
  124. std::filesystem::u8path(profileDirectory);
  125. try {
  126. std::filesystem::create_directory(profileDirectoryPath);
  127. } catch (const std::filesystem::filesystem_error error) {
  128. throw std::logic_error(
  129. "Failed to create directory for new profile: " +
  130. profileDirectory);
  131. }
  132. const std::filesystem::path profileFile =
  133. profileDirectoryPath /
  134. std::filesystem::u8path(OBSProfileSettingsFile);
  135. auto [iterator, success] = profiles.try_emplace(
  136. profileName,
  137. OBSProfile{profileName,
  138. profileDirectoryPath.filename().u8string(),
  139. profileDirectoryPath, profileFile});
  140. OnEvent(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  141. return iterator->second;
  142. }
  143. void OBSBasic::RemoveProfile(OBSProfile profile)
  144. {
  145. try {
  146. std::filesystem::remove_all(profile.path);
  147. } catch (const std::filesystem::filesystem_error &error) {
  148. blog(LOG_DEBUG, "%s", error.what());
  149. throw std::logic_error("Failed to remove profile directory: " +
  150. profile.directoryName);
  151. }
  152. blog(LOG_INFO, "------------------------------------------------");
  153. blog(LOG_INFO, "Removed profile '%s' (%s)", profile.name.c_str(),
  154. profile.directoryName.c_str());
  155. blog(LOG_INFO, "------------------------------------------------");
  156. }
  157. // MARK: - Profile UI Handling Functions
  158. bool OBSBasic::CreateNewProfile(const QString &name)
  159. {
  160. try {
  161. SetupNewProfile(name.toStdString());
  162. return true;
  163. } catch (const std::invalid_argument &error) {
  164. blog(LOG_ERROR, "%s", error.what());
  165. return false;
  166. } catch (const std::logic_error &error) {
  167. blog(LOG_ERROR, "%s", error.what());
  168. return false;
  169. }
  170. }
  171. bool OBSBasic::CreateDuplicateProfile(const QString &name)
  172. {
  173. try {
  174. SetupDuplicateProfile(name.toStdString());
  175. return true;
  176. } catch (const std::invalid_argument &error) {
  177. blog(LOG_ERROR, "%s", error.what());
  178. return false;
  179. } catch (const std::logic_error &error) {
  180. blog(LOG_ERROR, "%s", error.what());
  181. return false;
  182. }
  183. }
  184. void OBSBasic::DeleteProfile(const QString &name)
  185. {
  186. const std::string_view currentProfileName{
  187. config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  188. if (currentProfileName == name.toStdString()) {
  189. on_actionRemoveProfile_triggered();
  190. return;
  191. }
  192. auto foundProfile = GetProfileByName(name.toStdString());
  193. if (!foundProfile) {
  194. blog(LOG_ERROR, "Invalid profile name: %s", QT_TO_UTF8(name));
  195. return;
  196. }
  197. RemoveProfile(foundProfile.value());
  198. profiles.erase(name.toStdString());
  199. RefreshProfiles();
  200. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  201. OnEvent(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  202. }
  203. void OBSBasic::ChangeProfile()
  204. {
  205. QAction *action = reinterpret_cast<QAction *>(sender());
  206. if (!action) {
  207. return;
  208. }
  209. const std::string_view currentProfileName{
  210. config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  211. const std::string selectedProfileName{action->text().toStdString()};
  212. if (currentProfileName == selectedProfileName) {
  213. action->setChecked(true);
  214. return;
  215. }
  216. const std::optional<OBSProfile> foundProfile =
  217. GetProfileByName(selectedProfileName);
  218. if (!foundProfile) {
  219. const std::string errorMessage{"Selected profile not found: "};
  220. throw std::invalid_argument(errorMessage +
  221. currentProfileName.data());
  222. }
  223. const OBSProfile &selectedProfile = foundProfile.value();
  224. OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
  225. ActivateProfile(selectedProfile, true);
  226. blog(LOG_INFO, "Switched to profile '%s' (%s)",
  227. selectedProfile.name.c_str(),
  228. selectedProfile.directoryName.c_str());
  229. blog(LOG_INFO, "------------------------------------------------");
  230. }
  231. void OBSBasic::RefreshProfiles(bool refreshCache)
  232. {
  233. std::string_view currentProfileName{
  234. config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  235. QList<QAction *> menuActions = ui->profileMenu->actions();
  236. for (auto &action : menuActions) {
  237. QVariant variant = action->property("file_name");
  238. if (variant.typeName() != nullptr) {
  239. delete action;
  240. }
  241. }
  242. if (refreshCache) {
  243. RefreshProfileCache();
  244. }
  245. size_t numAddedProfiles = 0;
  246. for (auto &[profileName, profile] : profiles) {
  247. QAction *action =
  248. new QAction(QString().fromStdString(profileName), this);
  249. action->setProperty(
  250. "file_name",
  251. QString().fromStdString(profile.directoryName));
  252. connect(action, &QAction::triggered, this,
  253. &OBSBasic::ChangeProfile);
  254. action->setCheckable(true);
  255. action->setChecked(profileName == currentProfileName);
  256. ui->profileMenu->addAction(action);
  257. numAddedProfiles += 1;
  258. }
  259. ui->actionRemoveProfile->setEnabled(numAddedProfiles > 1);
  260. }
  261. // MARK: - Profile Cache Functions
  262. /// Refreshes profile cache data with profile state found on local file system.
  263. void OBSBasic::RefreshProfileCache()
  264. {
  265. std::map<std::string, OBSProfile> foundProfiles{};
  266. const std::filesystem::path profilesPath =
  267. App()->userProfilesLocation /
  268. std::filesystem::u8path(OBSProfilePath.substr(1));
  269. if (!std::filesystem::exists(profilesPath)) {
  270. blog(LOG_WARNING, "Failed to get profiles config path");
  271. return;
  272. }
  273. for (const auto &entry :
  274. std::filesystem::directory_iterator(profilesPath)) {
  275. if (!entry.is_directory()) {
  276. continue;
  277. }
  278. std::string profileCandidate;
  279. profileCandidate.reserve(entry.path().u8string().size() +
  280. OBSProfileSettingsFile.size() + 1);
  281. profileCandidate.append(entry.path().u8string())
  282. .append("/")
  283. .append(OBSProfileSettingsFile);
  284. ConfigFile config;
  285. if (config.Open(profileCandidate.c_str(),
  286. CONFIG_OPEN_EXISTING) != CONFIG_SUCCESS) {
  287. continue;
  288. }
  289. std::string candidateName;
  290. const char *configName =
  291. config_get_string(config, "General", "Name");
  292. if (configName) {
  293. candidateName = configName;
  294. } else {
  295. candidateName = entry.path().filename().u8string();
  296. }
  297. foundProfiles.try_emplace(
  298. candidateName,
  299. OBSProfile{candidateName,
  300. entry.path().filename().u8string(),
  301. entry.path(), profileCandidate});
  302. }
  303. profiles.swap(foundProfiles);
  304. }
  305. const OBSProfile &OBSBasic::GetCurrentProfile() const
  306. {
  307. std::string currentProfileName{
  308. config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  309. if (currentProfileName.empty()) {
  310. throw std::invalid_argument(
  311. "No valid profile name in configuration Basic->Profile");
  312. }
  313. const auto &foundProfile = profiles.find(currentProfileName);
  314. if (foundProfile != profiles.end()) {
  315. return foundProfile->second;
  316. } else {
  317. throw std::invalid_argument(
  318. "Profile not found in profile list: " +
  319. currentProfileName);
  320. }
  321. }
  322. std::optional<OBSProfile>
  323. OBSBasic::GetProfileByName(const std::string &profileName) const
  324. {
  325. auto foundProfile = profiles.find(profileName);
  326. if (foundProfile == profiles.end()) {
  327. return {};
  328. } else {
  329. return foundProfile->second;
  330. }
  331. }
  332. std::optional<OBSProfile>
  333. OBSBasic::GetProfileByDirectoryName(const std::string &directoryName) const
  334. {
  335. for (auto &[iterator, profile] : profiles) {
  336. if (profile.directoryName == directoryName) {
  337. return profile;
  338. }
  339. }
  340. return {};
  341. }
  342. // MARK: - Qt Slot Functions
  343. void OBSBasic::on_actionNewProfile_triggered()
  344. {
  345. bool useProfileWizard = config_get_bool(App()->GetUserConfig(), "Basic",
  346. "ConfigOnNewProfile");
  347. const OBSPromptCallback profilePromptCallback =
  348. [this](const OBSPromptResult &result) {
  349. if (GetProfileByName(result.promptValue)) {
  350. return false;
  351. }
  352. return true;
  353. };
  354. const OBSPromptRequest request{Str("AddProfile.Title"),
  355. Str("AddProfile.Text"),
  356. "",
  357. true,
  358. Str("AddProfile.WizardCheckbox"),
  359. useProfileWizard};
  360. OBSPromptResult result = PromptForName(request, profilePromptCallback);
  361. if (!result.success) {
  362. return;
  363. }
  364. try {
  365. SetupNewProfile(result.promptValue, result.optionValue);
  366. } catch (const std::invalid_argument &error) {
  367. blog(LOG_ERROR, "%s", error.what());
  368. } catch (const std::logic_error &error) {
  369. blog(LOG_ERROR, "%s", error.what());
  370. }
  371. }
  372. void OBSBasic::on_actionDupProfile_triggered()
  373. {
  374. const OBSPromptCallback profilePromptCallback =
  375. [this](const OBSPromptResult &result) {
  376. if (GetProfileByName(result.promptValue)) {
  377. return false;
  378. }
  379. return true;
  380. };
  381. const OBSPromptRequest request{Str("AddProfile.Title"),
  382. Str("AddProfile.Text")};
  383. OBSPromptResult result = PromptForName(request, profilePromptCallback);
  384. if (!result.success) {
  385. return;
  386. }
  387. try {
  388. SetupDuplicateProfile(result.promptValue);
  389. } catch (const std::invalid_argument &error) {
  390. blog(LOG_ERROR, "%s", error.what());
  391. } catch (const std::logic_error &error) {
  392. blog(LOG_ERROR, "%s", error.what());
  393. }
  394. }
  395. void OBSBasic::on_actionRenameProfile_triggered()
  396. {
  397. const std::string currentProfileName =
  398. config_get_string(App()->GetUserConfig(), "Basic", "Profile");
  399. const OBSPromptCallback profilePromptCallback =
  400. [this](const OBSPromptResult &result) {
  401. if (GetProfileByName(result.promptValue)) {
  402. return false;
  403. }
  404. return true;
  405. };
  406. const OBSPromptRequest request{Str("RenameProfile.Title"),
  407. Str("RenameProfile.Text"),
  408. currentProfileName};
  409. OBSPromptResult result = PromptForName(request, profilePromptCallback);
  410. if (!result.success) {
  411. return;
  412. }
  413. try {
  414. SetupRenameProfile(result.promptValue);
  415. } catch (const std::invalid_argument &error) {
  416. blog(LOG_ERROR, "%s", error.what());
  417. } catch (const std::logic_error &error) {
  418. blog(LOG_ERROR, "%s", error.what());
  419. }
  420. }
  421. void OBSBasic::on_actionRemoveProfile_triggered(bool skipConfirmation)
  422. {
  423. if (profiles.size() < 2) {
  424. return;
  425. }
  426. OBSProfile currentProfile;
  427. try {
  428. currentProfile = GetCurrentProfile();
  429. if (!skipConfirmation) {
  430. const QString confirmationText =
  431. QTStr("ConfirmRemove.Text")
  432. .arg(QString::fromStdString(
  433. currentProfile.name));
  434. const QMessageBox::StandardButton button =
  435. OBSMessageBox::question(
  436. this, QTStr("ConfirmRemove.Title"),
  437. confirmationText);
  438. if (button == QMessageBox::No) {
  439. return;
  440. }
  441. }
  442. OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
  443. profiles.erase(currentProfile.name);
  444. } catch (const std::invalid_argument &error) {
  445. blog(LOG_ERROR, "%s", error.what());
  446. } catch (const std::logic_error &error) {
  447. blog(LOG_ERROR, "%s", error.what());
  448. }
  449. const OBSProfile &newProfile = profiles.rbegin()->second;
  450. ActivateProfile(newProfile, true);
  451. RemoveProfile(currentProfile);
  452. #ifdef YOUTUBE_ENABLED
  453. if (YouTubeAppDock::IsYTServiceSelected() && !youtubeAppDock)
  454. NewYouTubeAppDock();
  455. #endif
  456. blog(LOG_INFO, "Switched to profile '%s' (%s)", newProfile.name.c_str(),
  457. newProfile.directoryName.c_str());
  458. blog(LOG_INFO, "------------------------------------------------");
  459. }
  460. void OBSBasic::on_actionImportProfile_triggered()
  461. {
  462. const QString home = QDir::homePath();
  463. const QString sourceDirectory = SelectDirectory(
  464. this, QTStr("Basic.MainMenu.Profile.Import"), home);
  465. if (!sourceDirectory.isEmpty() && !sourceDirectory.isNull()) {
  466. const std::filesystem::path sourcePath =
  467. std::filesystem::u8path(sourceDirectory.toStdString());
  468. const std::string directoryName =
  469. sourcePath.filename().string();
  470. if (auto profile = GetProfileByDirectoryName(directoryName)) {
  471. OBSMessageBox::warning(
  472. this, QTStr("Basic.MainMenu.Profile.Import"),
  473. QTStr("Basic.MainMenu.Profile.Exists"));
  474. return;
  475. }
  476. std::string destinationPathString;
  477. destinationPathString.reserve(
  478. App()->userProfilesLocation.u8string().size() +
  479. OBSProfilePath.size() + directoryName.size());
  480. destinationPathString
  481. .append(App()->userProfilesLocation.u8string())
  482. .append(OBSProfilePath)
  483. .append(directoryName);
  484. const std::filesystem::path destinationPath =
  485. std::filesystem::u8path(destinationPathString);
  486. try {
  487. std::filesystem::create_directory(destinationPath);
  488. } catch (const std::filesystem::filesystem_error &error) {
  489. blog(LOG_WARNING,
  490. "Failed to create profile directory '%s':\n%s",
  491. directoryName.c_str(), error.what());
  492. return;
  493. }
  494. const std::array<std::pair<std::string, bool>, 4> profileFiles{{
  495. {"basic.ini", true},
  496. {"service.json", false},
  497. {"streamEncoder.json", false},
  498. {"recordEncoder.json", false},
  499. }};
  500. for (auto &[file, isMandatory] : profileFiles) {
  501. const std::filesystem::path sourceFile =
  502. sourcePath / std::filesystem::u8path(file);
  503. if (!std::filesystem::exists(sourceFile)) {
  504. if (isMandatory) {
  505. blog(LOG_ERROR,
  506. "Failed to import profile from directory '%s' - necessary file '%s' not found",
  507. directoryName.c_str(),
  508. file.c_str());
  509. return;
  510. }
  511. continue;
  512. }
  513. const std::filesystem::path destinationFile =
  514. destinationPath / std::filesystem::u8path(file);
  515. try {
  516. std::filesystem::copy(sourceFile,
  517. destinationFile);
  518. } catch (
  519. const std::filesystem::filesystem_error &error) {
  520. blog(LOG_WARNING,
  521. "Failed to copy import file '%s' for profile '%s':\n%s",
  522. file.c_str(), directoryName.c_str(),
  523. error.what());
  524. return;
  525. }
  526. }
  527. RefreshProfiles(true);
  528. }
  529. }
  530. void OBSBasic::on_actionExportProfile_triggered()
  531. {
  532. const OBSProfile &currentProfile = GetCurrentProfile();
  533. const QString home = QDir::homePath();
  534. const QString destinationDirectory = SelectDirectory(
  535. this, QTStr("Basic.MainMenu.Profile.Export"), home);
  536. const std::array<std::string, 4> profileFiles{"basic.ini",
  537. "service.json",
  538. "streamEncoder.json",
  539. "recordEncoder.json"};
  540. if (!destinationDirectory.isEmpty() && !destinationDirectory.isNull()) {
  541. const std::filesystem::path sourcePath = currentProfile.path;
  542. const std::filesystem::path destinationPath =
  543. std::filesystem::u8path(
  544. destinationDirectory.toStdString()) /
  545. std::filesystem::u8path(currentProfile.directoryName);
  546. if (!std::filesystem::exists(destinationPath)) {
  547. std::filesystem::create_directory(destinationPath);
  548. }
  549. std::filesystem::copy_options copyOptions =
  550. std::filesystem::copy_options::overwrite_existing;
  551. for (auto &file : profileFiles) {
  552. const std::filesystem::path sourceFile =
  553. sourcePath / std::filesystem::u8path(file);
  554. if (!std::filesystem::exists(sourceFile)) {
  555. continue;
  556. }
  557. const std::filesystem::path destinationFile =
  558. destinationPath / std::filesystem::u8path(file);
  559. try {
  560. std::filesystem::copy(sourceFile,
  561. destinationFile,
  562. copyOptions);
  563. } catch (
  564. const std::filesystem::filesystem_error &error) {
  565. blog(LOG_WARNING,
  566. "Failed to copy export file '%s' for profile '%s'\n%s",
  567. file.c_str(), currentProfile.name.c_str(),
  568. error.what());
  569. return;
  570. }
  571. }
  572. }
  573. }
  574. // MARK: - Profile Management Helper Functions
  575. void OBSBasic::ActivateProfile(const OBSProfile &profile, bool reset)
  576. {
  577. ConfigFile config;
  578. if (config.Open(profile.profileFile.u8string().c_str(),
  579. CONFIG_OPEN_ALWAYS) != CONFIG_SUCCESS) {
  580. throw std::logic_error(
  581. "failed to open configuration file of new profile: " +
  582. profile.profileFile.string());
  583. }
  584. config_set_string(config, "General", "Name", profile.name.c_str());
  585. config.SaveSafe("tmp");
  586. std::vector<std::string> restartRequirements;
  587. if (activeConfiguration) {
  588. Auth::Save();
  589. if (reset) {
  590. auth.reset();
  591. DestroyPanelCookieManager();
  592. #ifdef YOUTUBE_ENABLED
  593. if (youtubeAppDock) {
  594. DeleteYouTubeAppDock();
  595. }
  596. #endif
  597. }
  598. restartRequirements = GetRestartRequirements(config);
  599. activeConfiguration.SaveSafe("tmp");
  600. }
  601. activeConfiguration.Swap(config);
  602. config_set_string(App()->GetUserConfig(), "Basic", "Profile",
  603. profile.name.c_str());
  604. config_set_string(App()->GetUserConfig(), "Basic", "ProfileDir",
  605. profile.directoryName.c_str());
  606. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  607. InitBasicConfigDefaults();
  608. InitBasicConfigDefaults2();
  609. if (reset) {
  610. ResetProfileData();
  611. }
  612. CheckForSimpleModeX264Fallback();
  613. RefreshProfiles();
  614. UpdateTitleBar();
  615. UpdateVolumeControlsDecayRate();
  616. Auth::Load();
  617. OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  618. if (!restartRequirements.empty()) {
  619. std::string requirements = std::accumulate(
  620. std::next(restartRequirements.begin()),
  621. restartRequirements.end(), restartRequirements[0],
  622. [](std::string a, std::string b) {
  623. return std::move(a) + "\n" + b;
  624. });
  625. QMessageBox::StandardButton button = OBSMessageBox::question(
  626. this, QTStr("Restart"),
  627. QTStr("LoadProfileNeedsRestart")
  628. .arg(requirements.c_str()));
  629. if (button == QMessageBox::Yes) {
  630. restart = true;
  631. close();
  632. }
  633. }
  634. }
  635. void OBSBasic::ResetProfileData()
  636. {
  637. ResetVideo();
  638. service = nullptr;
  639. InitService();
  640. ResetOutputs();
  641. ClearHotkeys();
  642. CreateHotkeys();
  643. /* load audio monitoring */
  644. if (obs_audio_monitoring_available()) {
  645. const char *device_name = config_get_string(
  646. activeConfiguration, "Audio", "MonitoringDeviceName");
  647. const char *device_id = config_get_string(
  648. activeConfiguration, "Audio", "MonitoringDeviceId");
  649. obs_set_audio_monitoring_device(device_name, device_id);
  650. blog(LOG_INFO, "Audio monitoring device:\n\tname: %s\n\tid: %s",
  651. device_name, device_id);
  652. }
  653. }
  654. std::vector<std::string>
  655. OBSBasic::GetRestartRequirements(const ConfigFile &config) const
  656. {
  657. std::vector<std::string> result;
  658. const char *oldSpeakers =
  659. config_get_string(activeConfiguration, "Audio", "ChannelSetup");
  660. const char *newSpeakers =
  661. config_get_string(config, "Audio", "ChannelSetup");
  662. uint64_t oldSampleRate =
  663. config_get_uint(activeConfiguration, "Audio", "SampleRate");
  664. uint64_t newSampleRate = config_get_uint(config, "Audio", "SampleRate");
  665. if (oldSpeakers != NULL && newSpeakers != NULL) {
  666. if (std::string_view{oldSpeakers} !=
  667. std::string_view{newSpeakers}) {
  668. result.emplace_back(
  669. Str("Basic.Settings.Audio.Channels"));
  670. }
  671. }
  672. if (oldSampleRate != 0 && newSampleRate != 0) {
  673. if (oldSampleRate != newSampleRate) {
  674. result.emplace_back(
  675. Str("Basic.Settings.Audio.SampleRate"));
  676. }
  677. }
  678. return result;
  679. }
  680. void OBSBasic::CheckForSimpleModeX264Fallback()
  681. {
  682. const char *curStreamEncoder = config_get_string(
  683. activeConfiguration, "SimpleOutput", "StreamEncoder");
  684. const char *curRecEncoder = config_get_string(
  685. activeConfiguration, "SimpleOutput", "RecEncoder");
  686. bool qsv_supported = false;
  687. bool qsv_av1_supported = false;
  688. bool amd_supported = false;
  689. bool nve_supported = false;
  690. #ifdef ENABLE_HEVC
  691. bool amd_hevc_supported = false;
  692. bool nve_hevc_supported = false;
  693. bool apple_hevc_supported = false;
  694. #endif
  695. bool amd_av1_supported = false;
  696. bool apple_supported = false;
  697. bool changed = false;
  698. size_t idx = 0;
  699. const char *id;
  700. while (obs_enum_encoder_types(idx++, &id)) {
  701. if (strcmp(id, "h264_texture_amf") == 0)
  702. amd_supported = true;
  703. else if (strcmp(id, "obs_qsv11") == 0)
  704. qsv_supported = true;
  705. else if (strcmp(id, "obs_qsv11_av1") == 0)
  706. qsv_av1_supported = true;
  707. else if (strcmp(id, "ffmpeg_nvenc") == 0)
  708. nve_supported = true;
  709. #ifdef ENABLE_HEVC
  710. else if (strcmp(id, "h265_texture_amf") == 0)
  711. amd_hevc_supported = true;
  712. else if (strcmp(id, "ffmpeg_hevc_nvenc") == 0)
  713. nve_hevc_supported = true;
  714. #endif
  715. else if (strcmp(id, "av1_texture_amf") == 0)
  716. amd_av1_supported = true;
  717. else if (strcmp(id,
  718. "com.apple.videotoolbox.videoencoder.ave.avc") ==
  719. 0)
  720. apple_supported = true;
  721. #ifdef ENABLE_HEVC
  722. else if (strcmp(id,
  723. "com.apple.videotoolbox.videoencoder.ave.hevc") ==
  724. 0)
  725. apple_hevc_supported = true;
  726. #endif
  727. }
  728. auto CheckEncoder = [&](const char *&name) {
  729. if (strcmp(name, SIMPLE_ENCODER_QSV) == 0) {
  730. if (!qsv_supported) {
  731. changed = true;
  732. name = SIMPLE_ENCODER_X264;
  733. return false;
  734. }
  735. } else if (strcmp(name, SIMPLE_ENCODER_QSV_AV1) == 0) {
  736. if (!qsv_av1_supported) {
  737. changed = true;
  738. name = SIMPLE_ENCODER_X264;
  739. return false;
  740. }
  741. } else if (strcmp(name, SIMPLE_ENCODER_NVENC) == 0) {
  742. if (!nve_supported) {
  743. changed = true;
  744. name = SIMPLE_ENCODER_X264;
  745. return false;
  746. }
  747. } else if (strcmp(name, SIMPLE_ENCODER_NVENC_AV1) == 0) {
  748. if (!nve_supported) {
  749. changed = true;
  750. name = SIMPLE_ENCODER_X264;
  751. return false;
  752. }
  753. #ifdef ENABLE_HEVC
  754. } else if (strcmp(name, SIMPLE_ENCODER_AMD_HEVC) == 0) {
  755. if (!amd_hevc_supported) {
  756. changed = true;
  757. name = SIMPLE_ENCODER_X264;
  758. return false;
  759. }
  760. } else if (strcmp(name, SIMPLE_ENCODER_NVENC_HEVC) == 0) {
  761. if (!nve_hevc_supported) {
  762. changed = true;
  763. name = SIMPLE_ENCODER_X264;
  764. return false;
  765. }
  766. #endif
  767. } else if (strcmp(name, SIMPLE_ENCODER_AMD) == 0) {
  768. if (!amd_supported) {
  769. changed = true;
  770. name = SIMPLE_ENCODER_X264;
  771. return false;
  772. }
  773. } else if (strcmp(name, SIMPLE_ENCODER_AMD_AV1) == 0) {
  774. if (!amd_av1_supported) {
  775. changed = true;
  776. name = SIMPLE_ENCODER_X264;
  777. return false;
  778. }
  779. } else if (strcmp(name, SIMPLE_ENCODER_APPLE_H264) == 0) {
  780. if (!apple_supported) {
  781. changed = true;
  782. name = SIMPLE_ENCODER_X264;
  783. return false;
  784. }
  785. #ifdef ENABLE_HEVC
  786. } else if (strcmp(name, SIMPLE_ENCODER_APPLE_HEVC) == 0) {
  787. if (!apple_hevc_supported) {
  788. changed = true;
  789. name = SIMPLE_ENCODER_X264;
  790. return false;
  791. }
  792. #endif
  793. }
  794. return true;
  795. };
  796. if (!CheckEncoder(curStreamEncoder))
  797. config_set_string(activeConfiguration, "SimpleOutput",
  798. "StreamEncoder", curStreamEncoder);
  799. if (!CheckEncoder(curRecEncoder))
  800. config_set_string(activeConfiguration, "SimpleOutput",
  801. "RecEncoder", curRecEncoder);
  802. if (changed) {
  803. activeConfiguration.SaveSafe("tmp");
  804. }
  805. }