window-basic-main-profiles.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 "window-basic-main.hpp"
  20. #include "window-namedialog.hpp"
  21. #include "qt-wrappers.hpp"
  22. template <typename Func> static void EnumProfiles(Func &&cb)
  23. {
  24. char path[512];
  25. os_glob_t *glob;
  26. int ret = GetConfigPath(path, sizeof(path),
  27. "obs-studio/basic/profiles/*");
  28. if (ret <= 0) {
  29. blog(LOG_WARNING, "Failed to get profiles config path");
  30. return;
  31. }
  32. if (os_glob(path, 0, &glob) != 0) {
  33. blog(LOG_WARNING, "Failed to glob profiles");
  34. return;
  35. }
  36. for (size_t i = 0; i < glob->gl_pathc; i++) {
  37. const char *filePath = glob->gl_pathv[i].path;
  38. const char *dirName = strrchr(filePath, '/') + 1;
  39. if (!glob->gl_pathv[i].directory)
  40. continue;
  41. if (strcmp(dirName, ".") == 0 ||
  42. strcmp(dirName, "..") == 0)
  43. continue;
  44. std::string file = filePath;
  45. file += "/basic.ini";
  46. ConfigFile config;
  47. int ret = config.Open(file.c_str(), CONFIG_OPEN_EXISTING);
  48. if (ret != CONFIG_SUCCESS)
  49. continue;
  50. const char *name = config_get_string(config, "General", "Name");
  51. if (!name)
  52. name = strrchr(filePath, '/') + 1;
  53. if (!cb(name, filePath))
  54. break;
  55. }
  56. os_globfree(glob);
  57. }
  58. static bool ProfileExists(const char *findName)
  59. {
  60. bool found = false;
  61. auto func = [&](const char *name, const char*)
  62. {
  63. if (strcmp(name, findName) == 0) {
  64. found = true;
  65. return false;
  66. }
  67. return true;
  68. };
  69. EnumProfiles(func);
  70. return found;
  71. }
  72. static bool GetProfileName(QWidget *parent, std::string &name,
  73. std::string &file, const char *title, const char *text,
  74. const char *oldName = nullptr)
  75. {
  76. char path[512];
  77. int ret;
  78. for (;;) {
  79. bool success = NameDialog::AskForName(parent, title, text,
  80. name, QT_UTF8(oldName));
  81. if (!success) {
  82. return false;
  83. }
  84. if (name.empty()) {
  85. QMessageBox::information(parent,
  86. QTStr("NoNameEntered.Title"),
  87. QTStr("NoNameEntered.Text"));
  88. continue;
  89. }
  90. if (ProfileExists(name.c_str())) {
  91. QMessageBox::information(parent,
  92. QTStr("NameExists.Title"),
  93. QTStr("NameExists.Text"));
  94. continue;
  95. }
  96. break;
  97. }
  98. if (!GetFileSafeName(name.c_str(), file)) {
  99. blog(LOG_WARNING, "Failed to create safe file name for '%s'",
  100. name.c_str());
  101. return false;
  102. }
  103. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles/");
  104. if (ret <= 0) {
  105. blog(LOG_WARNING, "Failed to get profiles config path");
  106. return false;
  107. }
  108. file.insert(0, path);
  109. if (!GetClosestUnusedFileName(file, nullptr)) {
  110. blog(LOG_WARNING, "Failed to get closest file name for %s",
  111. file.c_str());
  112. return false;
  113. }
  114. file.erase(0, ret);
  115. return true;
  116. }
  117. static bool CopyProfile(const char *fromPartial, const char *to)
  118. {
  119. os_glob_t *glob;
  120. char path[512];
  121. int ret;
  122. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles/");
  123. if (ret <= 0) {
  124. blog(LOG_WARNING, "Failed to get profiles config path");
  125. return false;
  126. }
  127. strcat(path, fromPartial);
  128. strcat(path, "/*");
  129. if (os_glob(path, 0, &glob) != 0) {
  130. blog(LOG_WARNING, "Failed to glob profile '%s'", fromPartial);
  131. return false;
  132. }
  133. for (size_t i = 0; i < glob->gl_pathc; i++) {
  134. const char *filePath = glob->gl_pathv[i].path;
  135. if (glob->gl_pathv[i].directory)
  136. continue;
  137. ret = snprintf(path, sizeof(path), "%s/%s",
  138. to, strrchr(filePath, '/') + 1);
  139. if (ret > 0) {
  140. if (os_copyfile(filePath, path) != 0) {
  141. blog(LOG_WARNING, "CopyProfile: Failed to "
  142. "copy file %s to %s",
  143. filePath, path);
  144. }
  145. }
  146. }
  147. os_globfree(glob);
  148. return true;
  149. }
  150. bool OBSBasic::AddProfile(bool create_new, const char *title, const char *text,
  151. const char *init_text)
  152. {
  153. std::string newName;
  154. std::string newDir;
  155. ConfigFile config;
  156. if (!GetProfileName(this, newName, newDir, title, text, init_text))
  157. return false;
  158. std::string curDir = config_get_string(App()->GlobalConfig(),
  159. "Basic", "ProfileDir");
  160. char newPath[512];
  161. int ret = GetConfigPath(newPath, 512, "obs-studio/basic/profiles/");
  162. if (ret <= 0) {
  163. blog(LOG_WARNING, "Failed to get profiles config path");
  164. return false;
  165. }
  166. strcat(newPath, newDir.c_str());
  167. if (os_mkdir(newPath) < 0) {
  168. blog(LOG_WARNING, "Failed to create profile directory '%s'",
  169. newDir.c_str());
  170. return false;
  171. }
  172. if (!create_new)
  173. CopyProfile(curDir.c_str(), newPath);
  174. strcat(newPath, "/basic.ini");
  175. if (config.Open(newPath, CONFIG_OPEN_ALWAYS) != 0) {
  176. blog(LOG_ERROR, "Failed to open new config file '%s'",
  177. newDir.c_str());
  178. return false;
  179. }
  180. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  181. newName.c_str());
  182. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  183. newDir.c_str());
  184. config_set_string(config, "General", "Name", newName.c_str());
  185. config.Save();
  186. config.Swap(basicConfig);
  187. InitBasicConfigDefaults();
  188. RefreshProfiles();
  189. if (create_new)
  190. ResetProfileData();
  191. blog(LOG_INFO, "------------------------------------------------");
  192. blog(LOG_INFO, "Created profile '%s' (%s, %s)", newName.c_str(),
  193. create_new ? "clean" : "duplicate", newDir.c_str());
  194. config_save(App()->GlobalConfig());
  195. UpdateTitleBar();
  196. return true;
  197. }
  198. void OBSBasic::DeleteProfile(const char *profileName, const char *profileDir)
  199. {
  200. char profilePath[512];
  201. char basePath[512];
  202. int ret = GetConfigPath(basePath, 512, "obs-studio/basic/profiles");
  203. if (ret <= 0) {
  204. blog(LOG_WARNING, "Failed to get profiles config path");
  205. return;
  206. }
  207. ret = snprintf(profilePath, 512, "%s/%s/*", basePath, profileDir);
  208. if (ret <= 0) {
  209. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  210. profileDir);
  211. return;
  212. }
  213. os_glob_t *glob;
  214. if (os_glob(profilePath, 0, &glob) != 0) {
  215. blog(LOG_WARNING, "Failed to glob profile dir '%s'",
  216. profileDir);
  217. return;
  218. }
  219. for (size_t i = 0; i < glob->gl_pathc; i++) {
  220. const char *filePath = glob->gl_pathv[i].path;
  221. if (glob->gl_pathv[i].directory)
  222. continue;
  223. os_unlink(filePath);
  224. }
  225. os_globfree(glob);
  226. ret = snprintf(profilePath, 512, "%s/%s", basePath, profileDir);
  227. if (ret <= 0) {
  228. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  229. profileDir);
  230. return;
  231. }
  232. os_rmdir(profilePath);
  233. blog(LOG_INFO, "------------------------------------------------");
  234. blog(LOG_INFO, "Removed profile '%s' (%s)",
  235. profileName, profileDir);
  236. }
  237. void OBSBasic::RefreshProfiles()
  238. {
  239. QList<QAction*> menuActions = ui->profileMenu->actions();
  240. int count = 0;
  241. for (int i = 0; i < menuActions.count(); i++) {
  242. QVariant v = menuActions[i]->property("file_name");
  243. if (v.typeName() != nullptr)
  244. delete menuActions[i];
  245. }
  246. const char *curName = config_get_string(App()->GlobalConfig(),
  247. "Basic", "Profile");
  248. auto addProfile = [&](const char *name, const char *path)
  249. {
  250. std::string file = strrchr(path, '/') + 1;
  251. QAction *action = new QAction(QT_UTF8(name), this);
  252. action->setProperty("file_name", QT_UTF8(path));
  253. connect(action, &QAction::triggered,
  254. this, &OBSBasic::ChangeProfile);
  255. action->setCheckable(true);
  256. action->setChecked(strcmp(name, curName) == 0);
  257. ui->profileMenu->addAction(action);
  258. count++;
  259. return true;
  260. };
  261. EnumProfiles(addProfile);
  262. ui->actionRemoveProfile->setEnabled(count > 1);
  263. }
  264. void OBSBasic::ResetProfileData()
  265. {
  266. ResetVideo();
  267. service = nullptr;
  268. InitService();
  269. ResetOutputs();
  270. ClearHotkeys();
  271. CreateHotkeys();
  272. }
  273. void OBSBasic::on_actionNewProfile_triggered()
  274. {
  275. AddProfile(true, Str("AddProfile.Title"), Str("AddProfile.Text"));
  276. }
  277. void OBSBasic::on_actionDupProfile_triggered()
  278. {
  279. AddProfile(false, Str("AddProfile.Title"), Str("AddProfile.Text"));
  280. }
  281. void OBSBasic::on_actionRenameProfile_triggered()
  282. {
  283. std::string curDir = config_get_string(App()->GlobalConfig(),
  284. "Basic", "ProfileDir");
  285. std::string curName = config_get_string(App()->GlobalConfig(),
  286. "Basic", "Profile");
  287. /* Duplicate and delete in case there are any issues in the process */
  288. bool success = AddProfile(false, Str("RenameProfile.Title"),
  289. Str("AddProfile.Text"), curName.c_str());
  290. if (success) {
  291. DeleteProfile(curName.c_str(), curDir.c_str());
  292. RefreshProfiles();
  293. }
  294. }
  295. void OBSBasic::on_actionRemoveProfile_triggered()
  296. {
  297. std::string newName;
  298. std::string newPath;
  299. ConfigFile config;
  300. std::string oldDir = config_get_string(App()->GlobalConfig(),
  301. "Basic", "ProfileDir");
  302. std::string oldName = config_get_string(App()->GlobalConfig(),
  303. "Basic", "Profile");
  304. auto cb = [&](const char *name, const char *filePath)
  305. {
  306. if (strcmp(oldName.c_str(), name) != 0) {
  307. newName = name;
  308. newPath = filePath;
  309. return false;
  310. }
  311. return true;
  312. };
  313. EnumProfiles(cb);
  314. /* this should never be true due to menu item being grayed out */
  315. if (newPath.empty())
  316. return;
  317. QString text = QTStr("ConfirmRemove.Text");
  318. text.replace("$1", QT_UTF8(oldName.c_str()));
  319. QMessageBox::StandardButton button = QMessageBox::question(this,
  320. QTStr("ConfirmRemove.Title"), text);
  321. if (button == QMessageBox::No)
  322. return;
  323. size_t newPath_len = newPath.size();
  324. newPath += "/basic.ini";
  325. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  326. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  327. newPath.c_str());
  328. return;
  329. }
  330. newPath.resize(newPath_len);
  331. const char *newDir = strrchr(newPath.c_str(), '/') + 1;
  332. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  333. newName.c_str());
  334. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  335. newDir);
  336. config.Swap(basicConfig);
  337. InitBasicConfigDefaults();
  338. ResetProfileData();
  339. DeleteProfile(oldName.c_str(), oldDir.c_str());
  340. RefreshProfiles();
  341. config_save(App()->GlobalConfig());
  342. blog(LOG_INFO, "------------------------------------------------");
  343. blog(LOG_INFO, "Switched to profile '%s' (%s)",
  344. newName.c_str(), newDir);
  345. UpdateTitleBar();
  346. }
  347. void OBSBasic::ChangeProfile()
  348. {
  349. QAction *action = reinterpret_cast<QAction*>(sender());
  350. ConfigFile config;
  351. std::string path;
  352. if (!action)
  353. return;
  354. path = QT_TO_UTF8(action->property("file_name").value<QString>());
  355. if (path.empty())
  356. return;
  357. const char *oldName = config_get_string(App()->GlobalConfig(),
  358. "Basic", "Profile");
  359. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  360. action->setChecked(true);
  361. return;
  362. }
  363. size_t path_len = path.size();
  364. path += "/basic.ini";
  365. if (config.Open(path.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  366. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  367. path.c_str());
  368. return;
  369. }
  370. path.resize(path_len);
  371. const char *newName = config_get_string(config, "General", "Name");
  372. const char *newDir = strrchr(path.c_str(), '/') + 1;
  373. config_set_string(App()->GlobalConfig(), "Basic", "Profile", newName);
  374. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  375. newDir);
  376. blog(LOG_INFO, "------------------------------------------------");
  377. blog(LOG_INFO, "Switched to profile '%s' (%s)",
  378. newName, newDir);
  379. config.Swap(basicConfig);
  380. InitBasicConfigDefaults();
  381. ResetProfileData();
  382. RefreshProfiles();
  383. config_save(App()->GlobalConfig());
  384. UpdateTitleBar();
  385. }