window-basic-main-profiles.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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.SaveSafe("tmp");
  186. config.Swap(basicConfig);
  187. InitBasicConfigDefaults();
  188. RefreshProfiles();
  189. if (create_new)
  190. ResetProfileData();
  191. blog(LOG_INFO, "Created profile '%s' (%s, %s)", newName.c_str(),
  192. create_new ? "clean" : "duplicate", newDir.c_str());
  193. blog(LOG_INFO, "------------------------------------------------");
  194. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  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. blog(LOG_INFO, "------------------------------------------------");
  237. }
  238. void OBSBasic::RefreshProfiles()
  239. {
  240. QList<QAction*> menuActions = ui->profileMenu->actions();
  241. int count = 0;
  242. for (int i = 0; i < menuActions.count(); i++) {
  243. QVariant v = menuActions[i]->property("file_name");
  244. if (v.typeName() != nullptr)
  245. delete menuActions[i];
  246. }
  247. const char *curName = config_get_string(App()->GlobalConfig(),
  248. "Basic", "Profile");
  249. auto addProfile = [&](const char *name, const char *path)
  250. {
  251. std::string file = strrchr(path, '/') + 1;
  252. QAction *action = new QAction(QT_UTF8(name), this);
  253. action->setProperty("file_name", QT_UTF8(path));
  254. connect(action, &QAction::triggered,
  255. this, &OBSBasic::ChangeProfile);
  256. action->setCheckable(true);
  257. action->setChecked(strcmp(name, curName) == 0);
  258. ui->profileMenu->addAction(action);
  259. count++;
  260. return true;
  261. };
  262. EnumProfiles(addProfile);
  263. ui->actionRemoveProfile->setEnabled(count > 1);
  264. }
  265. void OBSBasic::ResetProfileData()
  266. {
  267. ResetVideo();
  268. service = nullptr;
  269. InitService();
  270. ResetOutputs();
  271. ClearHotkeys();
  272. CreateHotkeys();
  273. }
  274. void OBSBasic::on_actionNewProfile_triggered()
  275. {
  276. AddProfile(true, Str("AddProfile.Title"), Str("AddProfile.Text"));
  277. }
  278. void OBSBasic::on_actionDupProfile_triggered()
  279. {
  280. AddProfile(false, Str("AddProfile.Title"), Str("AddProfile.Text"));
  281. }
  282. void OBSBasic::on_actionRenameProfile_triggered()
  283. {
  284. std::string curDir = config_get_string(App()->GlobalConfig(),
  285. "Basic", "ProfileDir");
  286. std::string curName = config_get_string(App()->GlobalConfig(),
  287. "Basic", "Profile");
  288. /* Duplicate and delete in case there are any issues in the process */
  289. bool success = AddProfile(false, Str("RenameProfile.Title"),
  290. Str("AddProfile.Text"), curName.c_str());
  291. if (success) {
  292. DeleteProfile(curName.c_str(), curDir.c_str());
  293. RefreshProfiles();
  294. }
  295. }
  296. void OBSBasic::on_actionRemoveProfile_triggered()
  297. {
  298. std::string newName;
  299. std::string newPath;
  300. ConfigFile config;
  301. std::string oldDir = config_get_string(App()->GlobalConfig(),
  302. "Basic", "ProfileDir");
  303. std::string oldName = config_get_string(App()->GlobalConfig(),
  304. "Basic", "Profile");
  305. auto cb = [&](const char *name, const char *filePath)
  306. {
  307. if (strcmp(oldName.c_str(), name) != 0) {
  308. newName = name;
  309. newPath = filePath;
  310. return false;
  311. }
  312. return true;
  313. };
  314. EnumProfiles(cb);
  315. /* this should never be true due to menu item being grayed out */
  316. if (newPath.empty())
  317. return;
  318. QString text = QTStr("ConfirmRemove.Text");
  319. text.replace("$1", QT_UTF8(oldName.c_str()));
  320. QMessageBox::StandardButton button = QMessageBox::question(this,
  321. QTStr("ConfirmRemove.Title"), text);
  322. if (button == QMessageBox::No)
  323. return;
  324. size_t newPath_len = newPath.size();
  325. newPath += "/basic.ini";
  326. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  327. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  328. newPath.c_str());
  329. return;
  330. }
  331. newPath.resize(newPath_len);
  332. const char *newDir = strrchr(newPath.c_str(), '/') + 1;
  333. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  334. newName.c_str());
  335. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  336. newDir);
  337. config.Swap(basicConfig);
  338. InitBasicConfigDefaults();
  339. ResetProfileData();
  340. DeleteProfile(oldName.c_str(), oldDir.c_str());
  341. RefreshProfiles();
  342. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  343. blog(LOG_INFO, "Switched to profile '%s' (%s)",
  344. newName.c_str(), newDir);
  345. blog(LOG_INFO, "------------------------------------------------");
  346. UpdateTitleBar();
  347. }
  348. void OBSBasic::ChangeProfile()
  349. {
  350. QAction *action = reinterpret_cast<QAction*>(sender());
  351. ConfigFile config;
  352. std::string path;
  353. if (!action)
  354. return;
  355. path = QT_TO_UTF8(action->property("file_name").value<QString>());
  356. if (path.empty())
  357. return;
  358. const char *oldName = config_get_string(App()->GlobalConfig(),
  359. "Basic", "Profile");
  360. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  361. action->setChecked(true);
  362. return;
  363. }
  364. size_t path_len = path.size();
  365. path += "/basic.ini";
  366. if (config.Open(path.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  367. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  368. path.c_str());
  369. return;
  370. }
  371. path.resize(path_len);
  372. const char *newName = config_get_string(config, "General", "Name");
  373. const char *newDir = strrchr(path.c_str(), '/') + 1;
  374. config_set_string(App()->GlobalConfig(), "Basic", "Profile", newName);
  375. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  376. newDir);
  377. config.Swap(basicConfig);
  378. InitBasicConfigDefaults();
  379. ResetProfileData();
  380. RefreshProfiles();
  381. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  382. UpdateTitleBar();
  383. blog(LOG_INFO, "Switched to profile '%s' (%s)",
  384. newName, newDir);
  385. blog(LOG_INFO, "------------------------------------------------");
  386. }