MainWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * GnomeSettingsVault - A GNOME configuration backup utility.
  3. * Copyright (C) 2026 Nicole Portas, nicole@equalmass.com
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include "MainWindow.hpp"
  11. #include <iostream>
  12. #include <filesystem>
  13. #include <archive.h>
  14. #include <archive_entry.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <cstdlib>
  18. #include <cstdio>
  19. #include <algorithm>
  20. #include <chrono>
  21. #include <iomanip>
  22. #include <sstream>
  23. #include <set>
  24. namespace fs = std::filesystem;
  25. MainWindow::MainWindow()
  26. : m_HeaderBar(), m_MainLayout(Gtk::ORIENTATION_VERTICAL, 0), m_Notebook(),
  27. m_BottomBox(Gtk::ORIENTATION_HORIZONTAL, 10), m_StatusLabel("Ready."),
  28. m_CheckDebugMode("Enable Debug Logging"), m_VBoxBackup(Gtk::ORIENTATION_VERTICAL, 10),
  29. m_LabelBackupInstruction("<span size='large' weight='bold'>Backup</span>"),
  30. m_CheckThemesBackup("GTK Themes"), m_CheckIconsBackup("Icons"),
  31. m_CheckWallpapersBackup("Wallpapers"), m_CheckExtensionsBackup("GNOME Extensions"),
  32. m_CheckDconfBackup("GNOME Settings"), m_ButtonBackup("Start Backup"),
  33. m_VBoxRestore(Gtk::ORIENTATION_VERTICAL, 10),
  34. m_LabelRestoreInstruction("<span size='large' weight='bold'>Restore from Vault</span>"),
  35. m_ButtonSelectVault("Select .gbk Vault File"), m_LabelSelectedVault("No vault selected"),
  36. m_CheckThemesRestore("GTK Themes"), m_CheckIconsRestore("Icons"),
  37. m_CheckWallpapersRestore("Wallpapers"), m_CheckExtensionsRestore("GNOME Extensions"),
  38. m_CheckDconfRestore("GNOME Settings"), m_CheckDryRunRestore("Dry Run"),
  39. m_ButtonRestore("Start Restore"), m_WorkerRunning(false), m_JobSuccess(true),
  40. m_IsDebugMode(false), m_LastJobWasRestore(false)
  41. {
  42. m_Dispatcher.connect(sigc::mem_fun(*this, &MainWindow::on_dispatcher_ping));
  43. set_default_size(1000, 700);
  44. m_HeaderBar.set_title("GnomeSettingsVault 0.1");
  45. m_HeaderBar.set_show_close_button(true);
  46. set_titlebar(m_HeaderBar);
  47. m_LabelBackupInstruction.set_use_markup(true);
  48. m_LabelRestoreInstruction.set_use_markup(true);
  49. m_RefTextBuffer = Gtk::TextBuffer::create();
  50. auto selection_tag = m_RefTextBuffer->create_tag("selection_orange");
  51. selection_tag->property_background() = "#ff8c00";
  52. selection_tag->property_foreground() = "#000000";
  53. m_LogView.set_buffer(m_RefTextBuffer);
  54. m_LogView.set_editable(false);
  55. m_LogView.set_cursor_visible(true);
  56. m_LogView.set_can_focus(true);
  57. m_RefTextBuffer->signal_mark_set().connect([this, selection_tag](const Gtk::TextBuffer::iterator& iter, const Glib::RefPtr<Gtk::TextMark>& mark){
  58. Gtk::TextBuffer::iterator start, end;
  59. if (m_RefTextBuffer->get_selection_bounds(start, end)) {
  60. m_RefTextBuffer->apply_tag(selection_tag, start, end);
  61. } else {
  62. m_RefTextBuffer->remove_tag(selection_tag, m_RefTextBuffer->begin(), m_RefTextBuffer->end());
  63. }
  64. });
  65. m_LogView.override_background_color(Gdk::RGBA("#1e1e1e"));
  66. m_LogView.override_color(Gdk::RGBA("#dcdcdc"));
  67. m_RefTextBuffer->create_mark("last_line", m_RefTextBuffer->end());
  68. m_ScrolledWindow.add(m_LogView);
  69. m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);
  70. m_VBoxBackup.set_border_width(20);
  71. m_VBoxBackup.pack_start(m_LabelBackupInstruction, Gtk::PACK_SHRINK);
  72. m_VBoxBackup.pack_start(m_CheckThemesBackup, Gtk::PACK_SHRINK);
  73. m_VBoxBackup.pack_start(m_CheckIconsBackup, Gtk::PACK_SHRINK);
  74. m_VBoxBackup.pack_start(m_CheckWallpapersBackup, Gtk::PACK_SHRINK);
  75. m_VBoxBackup.pack_start(m_CheckExtensionsBackup, Gtk::PACK_SHRINK);
  76. m_VBoxBackup.pack_start(m_CheckDconfBackup, Gtk::PACK_SHRINK);
  77. m_VBoxBackup.pack_end(m_ButtonBackup, Gtk::PACK_SHRINK);
  78. m_VBoxRestore.set_border_width(20);
  79. m_VBoxRestore.pack_start(m_LabelRestoreInstruction, Gtk::PACK_SHRINK);
  80. m_VBoxRestore.pack_start(m_ButtonSelectVault, Gtk::PACK_SHRINK);
  81. m_VBoxRestore.pack_start(m_LabelSelectedVault, Gtk::PACK_SHRINK);
  82. m_VBoxRestore.pack_start(m_CheckThemesRestore, Gtk::PACK_SHRINK);
  83. m_VBoxRestore.pack_start(m_CheckIconsRestore, Gtk::PACK_SHRINK);
  84. m_VBoxRestore.pack_start(m_CheckWallpapersRestore, Gtk::PACK_SHRINK);
  85. m_VBoxRestore.pack_start(m_CheckExtensionsRestore, Gtk::PACK_SHRINK);
  86. m_VBoxRestore.pack_start(m_CheckDconfRestore, Gtk::PACK_SHRINK);
  87. m_VBoxRestore.pack_start(m_CheckDryRunRestore, Gtk::PACK_SHRINK);
  88. m_VBoxRestore.pack_end(m_ButtonRestore, Gtk::PACK_SHRINK);
  89. m_Notebook.append_page(m_VBoxBackup, "Backup");
  90. m_Notebook.append_page(m_VBoxRestore, "Restore");
  91. m_BottomBox.set_border_width(10);
  92. m_StatusLabel.set_halign(Gtk::ALIGN_START);
  93. m_BottomBox.pack_start(m_StatusLabel, Gtk::PACK_EXPAND_WIDGET);
  94. m_BottomBox.pack_end(m_CheckDebugMode, Gtk::PACK_SHRINK);
  95. m_MainLayout.pack_start(m_Notebook, Gtk::PACK_SHRINK);
  96. m_MainLayout.pack_start(m_ScrolledWindow, Gtk::PACK_EXPAND_WIDGET);
  97. m_MainLayout.pack_start(m_BottomBox, Gtk::PACK_SHRINK);
  98. m_ButtonBackup.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_button_backup_clicked));
  99. m_ButtonRestore.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_button_restore_clicked));
  100. m_ButtonSelectVault.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_button_select_vault_clicked));
  101. add(m_MainLayout);
  102. show_all_children();
  103. }
  104. MainWindow::~MainWindow() {}
  105. void MainWindow::scan_vault_contents(const std::string& path) {
  106. struct archive *a = archive_read_new();
  107. struct archive_entry *entry;
  108. archive_read_support_format_all(a);
  109. archive_read_support_filter_all(a);
  110. std::set<std::string> found;
  111. if (archive_read_open_filename(a, path.c_str(), 10240) == ARCHIVE_OK) {
  112. while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
  113. std::string p = archive_entry_pathname(entry);
  114. if (p.find("themes.tar.gz") != std::string::npos) found.insert("themes");
  115. if (p.find("icons.tar.gz") != std::string::npos) found.insert("icons");
  116. if (p.find("wallpapers.tar.gz") != std::string::npos) found.insert("wallpapers");
  117. if (p.find("extensions.tar.gz") != std::string::npos) found.insert("extensions");
  118. if (p.find("settings.ini") != std::string::npos) found.insert("dconf");
  119. }
  120. }
  121. archive_read_free(a);
  122. m_CheckThemesRestore.set_active(found.count("themes"));
  123. m_CheckIconsRestore.set_active(found.count("icons"));
  124. m_CheckWallpapersRestore.set_active(found.count("wallpapers"));
  125. m_CheckExtensionsRestore.set_active(found.count("extensions"));
  126. m_CheckDconfRestore.set_active(found.count("dconf"));
  127. }
  128. void MainWindow::on_button_select_vault_clicked() {
  129. Gtk::FileChooserDialog dialog("Choose vault", Gtk::FILE_CHOOSER_ACTION_OPEN);
  130. dialog.set_transient_for(*this);
  131. dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
  132. dialog.add_button("_Open", Gtk::RESPONSE_OK);
  133. if (dialog.run() == Gtk::RESPONSE_OK) {
  134. m_SelectedVaultPath = dialog.get_filename();
  135. m_LabelSelectedVault.set_text("Selected: " + fs::path(m_SelectedVaultPath).filename().string());
  136. scan_vault_contents(m_SelectedVaultPath);
  137. add_log_ui("Vault loaded.");
  138. }
  139. }
  140. void MainWindow::queue_log(const std::string& message) {
  141. // Only write to disk if debug mode is explicitly checked
  142. if (m_IsDebugMode) {
  143. std::ofstream log_file(std::string(std::getenv("HOME")) + "/GnomeSettingsVault.log", std::ios::app);
  144. if (log_file.is_open()) { log_file << message << std::endl; log_file.close(); }
  145. }
  146. std::lock_guard<std::mutex> lock(m_LogMutex);
  147. m_LogQueue.push(message);
  148. m_Dispatcher.emit();
  149. }
  150. void MainWindow::on_dispatcher_ping() {
  151. std::lock_guard<std::mutex> lock(m_LogMutex);
  152. while (!m_LogQueue.empty()) {
  153. std::string msg = m_LogQueue.front();
  154. m_LogQueue.pop();
  155. if (msg == "__UNLOCK_UI__") {
  156. set_ui_locked(false);
  157. // Trigger Logout Prompt if it was a successful restore
  158. if (m_JobSuccess && m_LastJobWasRestore) {
  159. Gtk::MessageDialog dialog(*this, "Restore Complete", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
  160. dialog.set_secondary_text("Settings and components have been successfully restored.\n\nGNOME needs to restart to apply all changes smoothly. Would you like to log out now?");
  161. if (dialog.run() == Gtk::RESPONSE_YES) {
  162. system("gnome-session-quit --logout --no-prompt &");
  163. }
  164. }
  165. } else {
  166. add_log_ui(msg);
  167. }
  168. }
  169. }
  170. void MainWindow::add_log_ui(const std::string& message) {
  171. m_RefTextBuffer->insert(m_RefTextBuffer->end(), message + "\n");
  172. auto mark = m_RefTextBuffer->get_mark("last_line");
  173. m_RefTextBuffer->move_mark(mark, m_RefTextBuffer->end());
  174. if (m_LogView.get_realized()) m_LogView.scroll_to(mark);
  175. }
  176. void MainWindow::set_ui_locked(bool locked) {
  177. m_ButtonBackup.set_sensitive(!locked);
  178. m_ButtonRestore.set_sensitive(!locked);
  179. m_ButtonSelectVault.set_sensitive(!locked);
  180. m_CheckDebugMode.set_sensitive(!locked);
  181. if (!locked) {
  182. m_StatusLabel.override_color(m_JobSuccess ? Gdk::RGBA("#44ff44") : Gdk::RGBA("#ff4444"));
  183. m_StatusLabel.set_text(m_JobSuccess ? "Job Completed Successfully." : "Job Completed with ERRORS.");
  184. }
  185. }
  186. void MainWindow::run_backup_job(JobConfig config, std::string home_dir) {
  187. m_JobSuccess = true; std::error_code ec;
  188. queue_log("--- Starting Backup ---");
  189. std::string temp_dir = "/tmp/gvault_pack";
  190. fs::remove_all(temp_dir, ec);
  191. fs::create_directories(temp_dir);
  192. if (config.themes) if(!create_tar_archive(home_dir + "/.themes", temp_dir + "/themes.tar.gz", false)) m_JobSuccess = false;
  193. if (config.icons) if(!create_tar_archive(home_dir + "/.icons", temp_dir + "/icons.tar.gz", false)) m_JobSuccess = false;
  194. if (config.wallpapers) {
  195. std::string w_dir = get_wallpaper_directory();
  196. if(!w_dir.empty()) if(!create_tar_archive(w_dir, temp_dir + "/wallpapers.tar.gz", false)) m_JobSuccess = false;
  197. }
  198. if (config.extensions) if(!create_tar_archive(home_dir + "/.local/share/gnome-shell/extensions", temp_dir + "/extensions.tar.gz", false)) m_JobSuccess = false;
  199. if (config.dconf) {
  200. queue_log("[BACKUP] Exporting GNOME Settings...");
  201. if (system(("dconf dump / > " + temp_dir + "/settings.ini").c_str()) != 0) m_JobSuccess = false;
  202. }
  203. std::string final_file = home_dir + "/" + config.vault_name + ".gbk";
  204. queue_log("[BACKUP] Compiling final vault...");
  205. if(!create_tar_archive(temp_dir, final_file, true)) m_JobSuccess = false;
  206. fs::remove_all(temp_dir, ec);
  207. queue_log("__UNLOCK_UI__");
  208. m_WorkerRunning = false;
  209. }
  210. void MainWindow::on_button_backup_clicked() {
  211. Gtk::MessageDialog dialog(*this, "Vault Name", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_OK_CANCEL);
  212. Gtk::Entry entry; entry.set_text("GnomeVaultBackup");
  213. dialog.get_content_area()->pack_start(entry, Gtk::PACK_SHRINK);
  214. dialog.show_all_children();
  215. if (dialog.run() == Gtk::RESPONSE_OK) {
  216. m_IsDebugMode = m_CheckDebugMode.get_active();
  217. m_LastJobWasRestore = false;
  218. set_ui_locked(true); m_WorkerRunning = true;
  219. m_StatusLabel.override_color(Gdk::RGBA("#ffffff"));
  220. m_StatusLabel.set_text("Backup in progress...");
  221. JobConfig cfg;
  222. cfg.themes = m_CheckThemesBackup.get_active(); cfg.icons = m_CheckIconsBackup.get_active();
  223. cfg.wallpapers = m_CheckWallpapersBackup.get_active(); cfg.extensions = m_CheckExtensionsBackup.get_active();
  224. cfg.dconf = m_CheckDconfBackup.get_active(); cfg.dry_run = false; cfg.vault_name = entry.get_text();
  225. std::thread(&MainWindow::run_backup_job, this, cfg, std::string(std::getenv("HOME"))).detach();
  226. }
  227. }
  228. void MainWindow::run_restore_job(JobConfig config, std::string home_dir) {
  229. m_JobSuccess = true; std::error_code ec;
  230. queue_log("--- Starting Clean Restore ---");
  231. auto nuke_leftovers = [&](const std::string& path) {
  232. fs::remove_all(path + "_vault_new", ec);
  233. fs::remove_all(path + "_vault_old", ec);
  234. };
  235. nuke_leftovers(home_dir + "/.themes");
  236. nuke_leftovers(home_dir + "/.icons");
  237. nuke_leftovers(home_dir + "/.local/share/gnome-shell/extensions");
  238. if (config.extensions && !config.dry_run) {
  239. queue_log("[SAFE] Disabling extensions to prevent GNOME crash...");
  240. system("gsettings set org.gnome.shell disable-user-extensions true");
  241. }
  242. std::string temp_unpack = "/tmp/gvault_unpack";
  243. fs::remove_all(temp_unpack, ec);
  244. fs::create_directories(temp_unpack);
  245. queue_log("[RESTORE] Unpacking vault structure...");
  246. if (!extract_tar_archive(config.custom_restore_path, temp_unpack, false, false)) m_JobSuccess = false;
  247. std::string work_path = temp_unpack;
  248. for (const auto& entry : fs::recursive_directory_iterator(temp_unpack)) {
  249. if (entry.path().filename() == "settings.ini") { work_path = entry.path().parent_path().string(); break; }
  250. }
  251. auto atomic_restore = [&](const std::string& arch_name, const std::string& live_path, const std::string& label) {
  252. std::string arch_full = work_path + "/" + arch_name + ".tar.gz";
  253. if (fs::exists(arch_full)) {
  254. queue_log("[RESTORE] Swapping " + label + "...");
  255. std::string shadow = live_path + "_vault_new";
  256. std::string old_path = live_path + "_vault_old";
  257. fs::remove_all(shadow, ec);
  258. fs::remove_all(old_path, ec);
  259. fs::create_directories(shadow);
  260. if (extract_tar_archive(arch_full, shadow, config.dry_run, false)) {
  261. if (!config.dry_run) {
  262. sync();
  263. if (fs::exists(live_path)) {
  264. fs::rename(live_path, old_path, ec);
  265. if (ec) { queue_log(" !!! Warning: Could not isolate old " + label + ". Retrying via delete."); fs::remove_all(live_path, ec); }
  266. }
  267. fs::rename(shadow, live_path, ec);
  268. if (ec) {
  269. queue_log(" !!! ERROR: Rename failed for " + label + ".");
  270. m_JobSuccess = false;
  271. } else {
  272. if (m_IsDebugMode) queue_log(" -> " + label + " swapped successfully.");
  273. fs::remove_all(old_path, ec);
  274. }
  275. }
  276. } else m_JobSuccess = false;
  277. }
  278. };
  279. if (config.themes) atomic_restore("themes", home_dir + "/.themes", "Themes");
  280. if (config.icons) atomic_restore("icons", home_dir + "/.icons", "Icons");
  281. if (config.extensions) atomic_restore("extensions", home_dir + "/.local/share/gnome-shell/extensions", "Extensions");
  282. if (config.wallpapers && fs::exists(work_path + "/wallpapers.tar.gz")) {
  283. queue_log("[RESTORE] Restoring Wallpapers...");
  284. if (!extract_tar_archive(work_path + "/wallpapers.tar.gz", "/", config.dry_run, true)) m_JobSuccess = false;
  285. }
  286. if (config.dconf && fs::exists(work_path + "/settings.ini") && !config.dry_run) {
  287. queue_log("[RESTORE] Injecting Dconf settings...");
  288. if (system(("dconf load / < " + work_path + "/settings.ini").c_str()) != 0) m_JobSuccess = false;
  289. }
  290. if (config.extensions && !config.dry_run) {
  291. queue_log("[SAFE] Re-enabling extensions...");
  292. system("gsettings set org.gnome.shell disable-user-extensions false");
  293. }
  294. fs::remove_all(temp_unpack, ec);
  295. queue_log("__UNLOCK_UI__");
  296. }
  297. void MainWindow::on_button_restore_clicked() {
  298. if (m_SelectedVaultPath.empty()) return;
  299. m_IsDebugMode = m_CheckDebugMode.get_active();
  300. m_LastJobWasRestore = true;
  301. set_ui_locked(true); m_WorkerRunning = true;
  302. m_StatusLabel.override_color(Gdk::RGBA("#ffffff"));
  303. m_StatusLabel.set_text("Restore in progress...");
  304. JobConfig cfg;
  305. cfg.themes = m_CheckThemesRestore.get_active(); cfg.icons = m_CheckIconsRestore.get_active();
  306. cfg.wallpapers = m_CheckWallpapersRestore.get_active(); cfg.extensions = m_CheckExtensionsRestore.get_active();
  307. cfg.dconf = m_CheckDconfRestore.get_active(); cfg.dry_run = m_CheckDryRunRestore.get_active();
  308. cfg.custom_restore_path = m_SelectedVaultPath;
  309. std::thread(&MainWindow::run_restore_job, this, cfg, std::string(std::getenv("HOME"))).detach();
  310. }
  311. bool MainWindow::create_tar_archive(const std::string& source_dir, const std::string& out_filename, bool strip_path) {
  312. struct archive *a = archive_write_new();
  313. archive_write_add_filter_gzip(a);
  314. archive_write_set_format_pax_restricted(a);
  315. archive_write_open_filename(a, out_filename.c_str());
  316. fs::path base_path = fs::absolute(source_dir);
  317. bool ok = true;
  318. if (m_IsDebugMode) queue_log(" -> Compressing: " + source_dir);
  319. for (const auto& dirEntry : fs::recursive_directory_iterator(base_path, fs::directory_options::skip_permission_denied)) {
  320. std::string full = dirEntry.path().string();
  321. std::string store = strip_path ? fs::relative(dirEntry.path(), base_path).string() : full;
  322. if (store == "." || store == "..") continue;
  323. struct archive_entry *entry = archive_entry_new();
  324. archive_entry_set_pathname(entry, store.c_str());
  325. struct stat st;
  326. if (lstat(full.c_str(), &st) != 0) { archive_entry_free(entry); ok = false; continue; }
  327. archive_entry_copy_stat(entry, &st);
  328. if (S_ISLNK(st.st_mode)) {
  329. char link_target[4096]; ssize_t len = readlink(full.c_str(), link_target, sizeof(link_target)-1);
  330. if (len != -1) { link_target[len]='\0'; archive_entry_set_symlink(entry, link_target); }
  331. }
  332. archive_write_header(a, entry);
  333. if (m_IsDebugMode) queue_log(" -> Packed: " + store);
  334. if (S_ISREG(st.st_mode)) {
  335. int fd = ::open(full.c_str(), O_RDONLY);
  336. if (fd >= 0) {
  337. char buff[65536]; ssize_t len;
  338. while ((len = ::read(fd, buff, sizeof(buff))) > 0) archive_write_data(a, buff, len);
  339. ::close(fd);
  340. } else ok = false;
  341. }
  342. archive_entry_free(entry);
  343. }
  344. archive_write_close(a); archive_write_free(a);
  345. return ok;
  346. }
  347. bool MainWindow::extract_tar_archive(const std::string& archive_path, const std::string& dest_dir, bool dry_run, bool use_absolute) {
  348. struct archive *a = archive_read_new();
  349. archive_read_support_format_all(a);
  350. archive_read_support_filter_all(a);
  351. if (archive_read_open_filename(a, archive_path.c_str(), 10240) != ARCHIVE_OK) return false;
  352. int flags = ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_UNLINK | ARCHIVE_EXTRACT_SECURE_NODOTDOT;
  353. struct archive_entry *entry;
  354. bool status = true;
  355. while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
  356. std::string stored_path = archive_entry_pathname(entry);
  357. if (!use_absolute) {
  358. size_t pos = stored_path.find("/.themes/");
  359. if (pos == std::string::npos) pos = stored_path.find("/.icons/");
  360. if (pos == std::string::npos) pos = stored_path.find("/extensions/");
  361. if (pos != std::string::npos) {
  362. size_t start = stored_path.find('/', pos + 1);
  363. if (start != std::string::npos) stored_path = stored_path.substr(start + 1);
  364. }
  365. }
  366. std::string target = use_absolute ? archive_entry_pathname(entry) : dest_dir + "/" + stored_path;
  367. archive_entry_set_pathname(entry, target.c_str());
  368. if (dry_run) {
  369. if (m_IsDebugMode) queue_log(" -> [DRY] " + target);
  370. archive_read_data_skip(a);
  371. } else {
  372. if (use_absolute && fs::exists(target) && fs::is_regular_file(target)) {
  373. int fd = ::open(target.c_str(), O_WRONLY | O_TRUNC);
  374. if (fd >= 0) ::close(fd);
  375. }
  376. if (archive_read_extract(a, entry, flags) != ARCHIVE_OK) {
  377. if (!fs::is_directory(target)) {
  378. if (m_IsDebugMode) queue_log(" !!! FAIL: " + std::string(archive_error_string(a)) + " -> " + target);
  379. status = false;
  380. }
  381. } else {
  382. if (m_IsDebugMode) queue_log(" -> Restored: " + target);
  383. }
  384. }
  385. }
  386. archive_read_close(a); archive_read_free(a);
  387. return status;
  388. }
  389. std::string MainWindow::get_wallpaper_directory() {
  390. char buffer[256]; std::string result = "";
  391. FILE* pipe = popen("gsettings get org.gnome.desktop.background picture-uri-dark 2>/dev/null", "r");
  392. if (pipe) { while (fgets(buffer, sizeof(buffer), pipe) != nullptr) result += buffer; pclose(pipe); }
  393. size_t file_pos = result.find("file://");
  394. if (file_pos != std::string::npos) {
  395. std::string path = result.substr(file_pos + 7);
  396. path.erase(std::remove(path.begin(), path.end(), '\''), path.end());
  397. path.erase(std::remove(path.begin(), path.end(), '\n'), path.end());
  398. return fs::path(path).parent_path().string();
  399. }
  400. return "";
  401. }