MainWindow.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * GnomeSettingsVault - A GNOME configuration backup utility v0.3.5
  3. * Copyright (C) 2026 Nicole Portas, nicole@equalmass.com
  4. */
  5. #include "MainWindow.hpp"
  6. #include <iostream>
  7. #include <filesystem>
  8. #include <archive.h>
  9. #include <archive_entry.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <cstdlib>
  13. #include <cstdio>
  14. #include <algorithm>
  15. #include <chrono>
  16. #include <iomanip>
  17. #include <sstream>
  18. #include <set>
  19. #include <glibmm/miscutils.h>
  20. #include <glibmm/fileutils.h>
  21. namespace fs = std::filesystem;
  22. MainWindow::MainWindow()
  23. : m_HeaderBar(),
  24. m_MainLayout(Gtk::ORIENTATION_VERTICAL, 0),
  25. m_Notebook(),
  26. m_BottomBox(Gtk::ORIENTATION_HORIZONTAL, 10),
  27. m_StatusLabel("Ready."),
  28. m_CheckDebugMode("Write Debug Log to Disk (~/GnomeSettingsVault.log)"),
  29. m_VBoxBackup(Gtk::ORIENTATION_VERTICAL, 10),
  30. m_LabelBackupInstruction("<span size='large' weight='bold'>Backup Settings</span>"),
  31. m_EntryVaultName(),
  32. m_ButtonSelectDest("Choose Destination Folder"),
  33. m_LabelBackupPath("No destination selected"),
  34. m_CheckThemesBackup("GTK Themes"),
  35. m_CheckIconsBackup("Icons"),
  36. m_CheckWallpapersBackup("Wallpapers"),
  37. m_CheckExtensionsBackup("GNOME Extensions"),
  38. m_CheckDconfBackup("GNOME Settings"),
  39. m_ButtonBackup("Start Backup"),
  40. m_VBoxRestore(Gtk::ORIENTATION_VERTICAL, 10),
  41. m_LabelRestoreInstruction("<span size='large' weight='bold'>Restore from Vault</span>"),
  42. m_ButtonSelectVault("Select .gbk Vault File"),
  43. m_LabelSelectedVault("No vault selected"),
  44. m_CheckThemesRestore("GTK Themes"),
  45. m_CheckIconsRestore("Icons"),
  46. m_CheckWallpapersRestore("Wallpapers"),
  47. m_CheckExtensionsRestore("GNOME Extensions"),
  48. m_CheckDconfRestore("GNOME Settings"),
  49. m_CheckDryRunRestore("Dry Run"),
  50. m_ButtonRestore("Start Restore"),
  51. m_WorkerRunning(false),
  52. m_JobSuccess(true),
  53. m_IsDebugMode(false),
  54. m_LastJobWasRestore(false)
  55. {
  56. m_Dispatcher.connect(sigc::mem_fun(*this, &MainWindow::on_dispatcher_ping));
  57. set_default_size(1000, 750);
  58. m_HeaderBar.set_title("GnomeSettingsVault 0.3.5");
  59. m_HeaderBar.set_show_close_button(true);
  60. set_titlebar(m_HeaderBar);
  61. set_wmclass("gnome-vault", "GnomeSettingsVault");
  62. m_LabelBackupInstruction.set_use_markup(true);
  63. m_LabelRestoreInstruction.set_use_markup(true);
  64. m_EntryVaultName.set_text("GnomeVaultBackup");
  65. m_EntryVaultName.set_placeholder_text("Vault filename...");
  66. m_RefTextBuffer = Gtk::TextBuffer::create();
  67. m_LogView.set_buffer(m_RefTextBuffer);
  68. m_LogView.set_editable(false);
  69. m_LogView.override_background_color(Gdk::RGBA("#1e1e1e"));
  70. m_LogView.override_color(Gdk::RGBA("#dcdcdc"));
  71. m_RefTextBuffer->create_mark("last_line", m_RefTextBuffer->end());
  72. m_ScrolledWindow.add(m_LogView);
  73. m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);
  74. m_VBoxBackup.set_border_width(20);
  75. m_VBoxBackup.pack_start(m_LabelBackupInstruction, Gtk::PACK_SHRINK);
  76. m_VBoxBackup.pack_start(m_EntryVaultName, Gtk::PACK_SHRINK);
  77. m_VBoxBackup.pack_start(m_ButtonSelectDest, Gtk::PACK_SHRINK);
  78. m_VBoxBackup.pack_start(m_LabelBackupPath, Gtk::PACK_SHRINK);
  79. m_VBoxBackup.pack_start(m_CheckThemesBackup, Gtk::PACK_SHRINK);
  80. m_VBoxBackup.pack_start(m_CheckIconsBackup, Gtk::PACK_SHRINK);
  81. m_VBoxBackup.pack_start(m_CheckWallpapersBackup, Gtk::PACK_SHRINK);
  82. m_VBoxBackup.pack_start(m_CheckExtensionsBackup, Gtk::PACK_SHRINK);
  83. m_VBoxBackup.pack_start(m_CheckDconfBackup, Gtk::PACK_SHRINK);
  84. m_VBoxBackup.pack_end(m_ButtonBackup, Gtk::PACK_SHRINK);
  85. m_VBoxRestore.set_border_width(20);
  86. m_VBoxRestore.pack_start(m_LabelRestoreInstruction, Gtk::PACK_SHRINK);
  87. m_VBoxRestore.pack_start(m_ButtonSelectVault, Gtk::PACK_SHRINK);
  88. m_VBoxRestore.pack_start(m_LabelSelectedVault, Gtk::PACK_SHRINK);
  89. m_VBoxRestore.pack_start(m_CheckThemesRestore, Gtk::PACK_SHRINK);
  90. m_VBoxRestore.pack_start(m_CheckIconsRestore, Gtk::PACK_SHRINK);
  91. m_VBoxRestore.pack_start(m_CheckWallpapersRestore, Gtk::PACK_SHRINK);
  92. m_VBoxRestore.pack_start(m_CheckExtensionsRestore, Gtk::PACK_SHRINK);
  93. m_VBoxRestore.pack_start(m_CheckDconfRestore, Gtk::PACK_SHRINK);
  94. m_VBoxRestore.pack_start(m_CheckDryRunRestore, Gtk::PACK_SHRINK);
  95. m_VBoxRestore.pack_end(m_ButtonRestore, Gtk::PACK_SHRINK);
  96. m_Notebook.append_page(m_VBoxBackup, "Backup");
  97. m_Notebook.append_page(m_VBoxRestore, "Restore");
  98. m_BottomBox.set_border_width(10);
  99. m_BottomBox.pack_start(m_StatusLabel, Gtk::PACK_EXPAND_WIDGET);
  100. m_BottomBox.pack_end(m_CheckDebugMode, Gtk::PACK_SHRINK);
  101. m_MainLayout.pack_start(m_Notebook, Gtk::PACK_SHRINK);
  102. m_MainLayout.pack_start(m_ScrolledWindow, Gtk::PACK_EXPAND_WIDGET);
  103. m_MainLayout.pack_start(m_BottomBox, Gtk::PACK_SHRINK);
  104. m_ButtonSelectDest.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_button_select_dest_clicked));
  105. m_ButtonBackup.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_button_backup_clicked));
  106. m_ButtonRestore.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_button_restore_clicked));
  107. m_ButtonSelectVault.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_button_select_vault_clicked));
  108. add(m_MainLayout);
  109. show_all_children();
  110. }
  111. MainWindow::~MainWindow() {}
  112. void MainWindow::on_button_select_dest_clicked() {
  113. Gtk::FileChooserDialog dialog("Select Destination Folder", Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
  114. dialog.set_transient_for(*this);
  115. dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
  116. dialog.add_button("_Select", Gtk::RESPONSE_OK);
  117. if (dialog.run() == Gtk::RESPONSE_OK) {
  118. m_TargetBackupPath = dialog.get_filename();
  119. m_LabelBackupPath.set_markup("<b>Target:</b> " + m_TargetBackupPath);
  120. }
  121. }
  122. void MainWindow::on_button_backup_clicked() {
  123. if (m_TargetBackupPath.empty()) {
  124. Gtk::MessageDialog err(*this, "Error", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);
  125. err.set_secondary_text("You haven't picked a destination folder, bloody hell.");
  126. err.run();
  127. return;
  128. }
  129. m_IsDebugMode = m_CheckDebugMode.get_active();
  130. m_LastJobWasRestore = false;
  131. set_ui_locked(true);
  132. m_WorkerRunning = true;
  133. m_StatusLabel.set_text("Backup in progress...");
  134. JobConfig cfg;
  135. cfg.themes = m_CheckThemesBackup.get_active();
  136. cfg.icons = m_CheckIconsBackup.get_active();
  137. cfg.wallpapers = m_CheckWallpapersBackup.get_active();
  138. cfg.extensions = m_CheckExtensionsBackup.get_active();
  139. cfg.dconf = m_CheckDconfBackup.get_active();
  140. cfg.dry_run = false;
  141. cfg.vault_name = m_EntryVaultName.get_text();
  142. cfg.backup_save_path = m_TargetBackupPath;
  143. std::thread(&MainWindow::run_backup_job, this, cfg, std::string(std::getenv("HOME"))).detach();
  144. }
  145. void MainWindow::send_notification(const std::string& title, const std::string& body) {
  146. auto app = Gtk::Application::get_default();
  147. auto notification = Gio::Notification::create(title);
  148. notification->set_body(body);
  149. app->send_notification(notification);
  150. }
  151. void MainWindow::scan_vault_contents(const std::string& path) {
  152. struct archive *a = archive_read_new();
  153. struct archive_entry *entry;
  154. archive_read_support_format_all(a);
  155. archive_read_support_filter_all(a);
  156. std::set<std::string> found;
  157. if (archive_read_open_filename(a, path.c_str(), 10240) == ARCHIVE_OK) {
  158. while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
  159. std::string p = archive_entry_pathname(entry);
  160. if (p.find("themes_") != std::string::npos) found.insert("themes");
  161. if (p.find("icons_") != std::string::npos) found.insert("icons");
  162. if (p.find("wallpapers.tar.gz") != std::string::npos) found.insert("wallpapers");
  163. if (p.find("extensions.tar.gz") != std::string::npos) found.insert("extensions");
  164. if (p.find("settings.ini") != std::string::npos) found.insert("dconf");
  165. }
  166. }
  167. archive_read_free(a);
  168. m_CheckThemesRestore.set_active(found.count("themes"));
  169. m_CheckIconsRestore.set_active(found.count("icons"));
  170. m_CheckWallpapersRestore.set_active(found.count("wallpapers"));
  171. m_CheckExtensionsRestore.set_active(found.count("extensions"));
  172. m_CheckDconfRestore.set_active(found.count("dconf"));
  173. }
  174. void MainWindow::on_button_select_vault_clicked() {
  175. Gtk::FileChooserDialog dialog("Choose vault", Gtk::FILE_CHOOSER_ACTION_OPEN);
  176. dialog.set_transient_for(*this);
  177. dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
  178. dialog.add_button("_Open", Gtk::RESPONSE_OK);
  179. if (dialog.run() == Gtk::RESPONSE_OK) {
  180. m_SelectedVaultPath = dialog.get_filename();
  181. m_LabelSelectedVault.set_text("Selected: " + fs::path(m_SelectedVaultPath).filename().string());
  182. scan_vault_contents(m_SelectedVaultPath);
  183. add_log_ui("Vault loaded.");
  184. }
  185. }
  186. void MainWindow::queue_log(const std::string& message) {
  187. if (m_IsDebugMode) {
  188. std::ofstream log_file(std::string(std::getenv("HOME")) + "/GnomeSettingsVault.log", std::ios::app);
  189. if (log_file.is_open()) {
  190. log_file << message << std::endl;
  191. log_file.close();
  192. }
  193. }
  194. std::lock_guard<std::mutex> lock(m_LogMutex);
  195. m_LogQueue.push(message);
  196. m_Dispatcher.emit();
  197. }
  198. void MainWindow::on_dispatcher_ping() {
  199. std::lock_guard<std::mutex> lock(m_LogMutex);
  200. while (!m_LogQueue.empty()) {
  201. std::string msg = m_LogQueue.front();
  202. m_LogQueue.pop();
  203. if (msg == "__UNLOCK_UI__") {
  204. set_ui_locked(false);
  205. send_notification("Job Finished", m_JobSuccess ? "Successfully completed." : "Errors detected.");
  206. if (m_JobSuccess && m_LastJobWasRestore) {
  207. Gtk::MessageDialog dialog(*this, "Restore Complete", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
  208. dialog.set_secondary_text("Settings restored. Restart GNOME session now?");
  209. if (dialog.run() == Gtk::RESPONSE_YES) {
  210. system("gnome-session-quit --logout --no-prompt &");
  211. }
  212. }
  213. } else {
  214. add_log_ui(msg);
  215. }
  216. }
  217. }
  218. void MainWindow::add_log_ui(const std::string& message) {
  219. m_RefTextBuffer->insert(m_RefTextBuffer->end(), message + "\n");
  220. auto mark = m_RefTextBuffer->get_mark("last_line");
  221. m_RefTextBuffer->move_mark(mark, m_RefTextBuffer->end());
  222. if (m_LogView.get_realized()) {
  223. m_LogView.scroll_to(mark);
  224. }
  225. }
  226. void MainWindow::set_ui_locked(bool locked) {
  227. m_ButtonBackup.set_sensitive(!locked);
  228. m_ButtonRestore.set_sensitive(!locked);
  229. m_ButtonSelectVault.set_sensitive(!locked);
  230. m_ButtonSelectDest.set_sensitive(!locked);
  231. m_CheckDebugMode.set_sensitive(!locked);
  232. if (!locked) {
  233. m_StatusLabel.override_color(m_JobSuccess ? Gdk::RGBA("#44ff44") : Gdk::RGBA("#ff4444"));
  234. m_StatusLabel.set_text(m_JobSuccess ? "Job Successful." : "Job Failed.");
  235. }
  236. }
  237. void MainWindow::run_backup_job(JobConfig config, std::string home_dir) {
  238. m_JobSuccess = true;
  239. std::error_code ec;
  240. queue_log("--- Starting Backup ---");
  241. char tpl[] = "/tmp/gvault_pack_XXXXXX";
  242. if (mkdtemp(tpl) == nullptr) {
  243. queue_log("!!! ERROR: Failed to create secure temp directory.");
  244. m_JobSuccess = false;
  245. m_WorkerRunning = false;
  246. queue_log("__UNLOCK_UI__");
  247. return;
  248. }
  249. std::string temp_dir(tpl);
  250. auto pack_h = [&](const std::string& p, const std::string& n) {
  251. if (fs::exists(p)) {
  252. if (!create_tar_archive(p, temp_dir + "/" + n, false)) {
  253. queue_log(" !!! Warning: Minor issues packing " + n + ". Archive still usable.");
  254. }
  255. }
  256. };
  257. if (config.themes) {
  258. pack_h(home_dir + "/.themes", "themes_legacy.tar.gz");
  259. pack_h(home_dir + "/.local/share/themes", "themes_xdg.tar.gz");
  260. }
  261. if (config.icons) {
  262. pack_h(home_dir + "/.icons", "icons_legacy.tar.gz");
  263. pack_h(home_dir + "/.local/share/icons", "icons_xdg.tar.gz");
  264. }
  265. if (config.wallpapers) {
  266. std::string w = get_wallpaper_directory();
  267. if (!w.empty()) pack_h(w, "wallpapers.tar.gz");
  268. }
  269. if (config.extensions) {
  270. pack_h(home_dir + "/.local/share/gnome-shell/extensions", "extensions.tar.gz");
  271. }
  272. if (config.dconf) {
  273. queue_log("[BACKUP] Exporting GNOME Settings...");
  274. if (system(("dconf dump / > '" + temp_dir + "/settings.ini'").c_str()) != 0) {
  275. m_JobSuccess = false;
  276. }
  277. }
  278. std::string final_vault = config.backup_save_path + "/" + config.vault_name + ".gbk";
  279. queue_log("[BACKUP] Compiling final vault to: " + final_vault);
  280. if (!create_tar_archive(temp_dir, final_vault, true)) {
  281. m_JobSuccess = false;
  282. }
  283. fs::remove_all(temp_dir, ec);
  284. m_WorkerRunning = false;
  285. queue_log("__UNLOCK_UI__");
  286. }
  287. void MainWindow::run_restore_job(JobConfig config, std::string home_dir) {
  288. m_JobSuccess = true;
  289. std::error_code ec;
  290. queue_log("--- Starting Restore ---");
  291. auto settings = Gio::Settings::create("org.gnome.shell");
  292. if (config.extensions && !config.dry_run) {
  293. settings->set_boolean("disable-user-extensions", true);
  294. }
  295. char tpl[] = "/tmp/gvault_unpack_XXXXXX";
  296. if (mkdtemp(tpl) == nullptr) {
  297. queue_log("!!! ERROR: Failed to create secure temp directory.");
  298. m_JobSuccess = false;
  299. if (config.extensions && !config.dry_run) {
  300. settings->set_boolean("disable-user-extensions", false);
  301. }
  302. m_WorkerRunning = false;
  303. queue_log("__UNLOCK_UI__");
  304. return;
  305. }
  306. std::string temp_unpack(tpl);
  307. if (!extract_tar_archive(config.custom_restore_path, temp_unpack, false, false)) {
  308. m_JobSuccess = false;
  309. }
  310. auto atomic_res = [&](const std::string& n, const std::string& lp, const std::string& lbl) {
  311. std::string arch = temp_unpack + "/" + n + ".tar.gz";
  312. if (fs::exists(arch)) {
  313. queue_log("[RESTORE] Swapping " + lbl + "...");
  314. std::string sh = lp + "_vault_new";
  315. std::string od = lp + "_vault_old";
  316. fs::remove_all(sh, ec);
  317. fs::remove_all(od, ec);
  318. fs::create_directories(sh);
  319. if (extract_tar_archive(arch, sh, config.dry_run, false)) {
  320. if (!config.dry_run) {
  321. sync();
  322. bool had_old = fs::exists(lp);
  323. if (had_old) fs::rename(lp, od, ec);
  324. fs::rename(sh, lp, ec);
  325. if (ec) {
  326. queue_log("!!! FAIL: Rolling back " + lbl);
  327. if (had_old) fs::rename(od, lp, ec);
  328. m_JobSuccess = false;
  329. } else {
  330. fs::remove_all(od, ec);
  331. }
  332. }
  333. } else {
  334. m_JobSuccess = false;
  335. }
  336. }
  337. };
  338. if (config.themes) {
  339. atomic_res("themes_legacy", home_dir + "/.themes", "Themes (Legacy)");
  340. atomic_res("themes_xdg", home_dir + "/.local/share/themes", "Themes (XDG)");
  341. }
  342. if (config.icons) {
  343. atomic_res("icons_legacy", home_dir + "/.icons", "Icons (Legacy)");
  344. atomic_res("icons_xdg", home_dir + "/.local/share/icons", "Icons (XDG)");
  345. }
  346. if (config.extensions) {
  347. atomic_res("extensions", home_dir + "/.local/share/gnome-shell/extensions", "Extensions");
  348. }
  349. if (config.wallpapers && fs::exists(temp_unpack + "/wallpapers.tar.gz")) {
  350. queue_log("[RESTORE] Restoring Wallpapers...");
  351. if (!extract_tar_archive(temp_unpack + "/wallpapers.tar.gz", "/", config.dry_run, true)) {
  352. m_JobSuccess = false;
  353. }
  354. }
  355. if (config.dconf && fs::exists(temp_unpack + "/settings.ini") && !config.dry_run) {
  356. queue_log("[RESTORE] Injecting Dconf...");
  357. if (system(("dconf load / < '" + temp_unpack + "/settings.ini'").c_str()) != 0) {
  358. m_JobSuccess = false;
  359. }
  360. }
  361. if (config.extensions && !config.dry_run) {
  362. settings->set_boolean("disable-user-extensions", false);
  363. }
  364. fs::remove_all(temp_unpack, ec);
  365. m_WorkerRunning = false; // FINALLY FIXED THIS BLOODY FLAG
  366. queue_log("__UNLOCK_UI__");
  367. }
  368. void MainWindow::on_button_restore_clicked() {
  369. if (m_SelectedVaultPath.empty()) return;
  370. m_IsDebugMode = m_CheckDebugMode.get_active();
  371. m_LastJobWasRestore = true;
  372. set_ui_locked(true);
  373. m_WorkerRunning = true;
  374. m_StatusLabel.set_text("Restore in progress...");
  375. JobConfig cfg;
  376. cfg.themes = m_CheckThemesRestore.get_active();
  377. cfg.icons = m_CheckIconsRestore.get_active();
  378. cfg.wallpapers = m_CheckWallpapersRestore.get_active();
  379. cfg.extensions = m_CheckExtensionsRestore.get_active();
  380. cfg.dconf = m_CheckDconfRestore.get_active();
  381. cfg.dry_run = m_CheckDryRunRestore.get_active();
  382. cfg.custom_restore_path = m_SelectedVaultPath;
  383. std::thread(&MainWindow::run_restore_job, this, cfg, std::string(std::getenv("HOME"))).detach();
  384. }
  385. bool MainWindow::on_delete_event(GdkEventAny* any_event) {
  386. if (m_WorkerRunning) {
  387. Gtk::MessageDialog dialog(*this, "Job in Progress", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK);
  388. dialog.set_secondary_text("A backup or restore is currently running. Closing the app now will corrupt your vault. Wait until it's bloody finished.");
  389. dialog.run();
  390. return true; // Stop the window from closing
  391. }
  392. return false; // Allow the window to close
  393. }
  394. bool MainWindow::create_tar_archive(const std::string& s, const std::string& o, bool strip) {
  395. struct archive *a = archive_write_new();
  396. archive_write_add_filter_gzip(a);
  397. archive_write_set_format_pax_restricted(a);
  398. if (archive_write_open_filename(a, o.c_str()) != ARCHIVE_OK) return false;
  399. fs::path bp = fs::absolute(s);
  400. bool ok = true;
  401. for (const auto& d : fs::recursive_directory_iterator(bp, fs::directory_options::skip_permission_denied)) {
  402. std::string f = d.path().string();
  403. std::string st = strip ? fs::relative(d.path(), bp).string() : f;
  404. if (st == "." || st == "..") continue;
  405. struct archive_entry *e = archive_entry_new();
  406. archive_entry_set_pathname(e, st.c_str());
  407. struct stat sbuf;
  408. if (lstat(f.c_str(), &sbuf) != 0) {
  409. queue_log(" -> Warning (stat): " + st);
  410. archive_entry_free(e);
  411. ok = false;
  412. continue;
  413. }
  414. archive_entry_copy_stat(e, &sbuf);
  415. if (S_ISLNK(sbuf.st_mode)) {
  416. char t[4096];
  417. ssize_t l = readlink(f.c_str(), t, 4095);
  418. if (l != -1) {
  419. t[l] = '\0';
  420. archive_entry_set_symlink(e, t);
  421. }
  422. }
  423. if (archive_write_header(a, e) != ARCHIVE_OK) {
  424. queue_log(" -> Warning (header): " + st);
  425. archive_entry_free(e);
  426. ok = false;
  427. continue;
  428. }
  429. queue_log(" -> Packed: " + st);
  430. if (S_ISREG(sbuf.st_mode)) {
  431. int fd = ::open(f.c_str(), O_RDONLY);
  432. if (fd >= 0) {
  433. char b[65536];
  434. ssize_t l;
  435. while ((l = ::read(fd, b, 65536)) > 0) {
  436. archive_write_data(a, b, l);
  437. }
  438. ::close(fd);
  439. } else {
  440. ok = false;
  441. queue_log(" -> Warning (read): " + st);
  442. }
  443. }
  444. archive_entry_free(e);
  445. }
  446. archive_write_close(a);
  447. archive_write_free(a);
  448. return ok;
  449. }
  450. bool MainWindow::extract_tar_archive(const std::string& ap, const std::string& ds, bool dr, bool abs) {
  451. struct archive *a = archive_read_new();
  452. archive_read_support_format_all(a);
  453. archive_read_support_filter_all(a);
  454. if (archive_read_open_filename(a, ap.c_str(), 10240) != ARCHIVE_OK) return false;
  455. struct archive_entry *e;
  456. bool ok = true;
  457. while (archive_read_next_header(a, &e) == ARCHIVE_OK) {
  458. std::string sp = archive_entry_pathname(e);
  459. if (!abs) {
  460. std::string to_strip = "";
  461. // Look precisely at the archive name to match the exact stripping rule
  462. if (ap.find("extensions") != std::string::npos) to_strip = "/extensions/";
  463. else if (ap.find("themes_legacy") != std::string::npos) to_strip = "/.themes/";
  464. else if (ap.find("themes_xdg") != std::string::npos) to_strip = "/themes/";
  465. else if (ap.find("icons_legacy") != std::string::npos) to_strip = "/.icons/";
  466. else if (ap.find("icons_xdg") != std::string::npos) to_strip = "/icons/";
  467. if (!to_strip.empty()) {
  468. size_t p = sp.find(to_strip);
  469. if (p != std::string::npos) {
  470. // Strip everything up to and including the matched directory string
  471. sp = sp.substr(p + to_strip.length());
  472. }
  473. }
  474. }
  475. std::string t = abs ? archive_entry_pathname(e) : ds + "/" + sp;
  476. archive_entry_set_pathname(e, t.c_str());
  477. if (dr) {
  478. queue_log(" -> [DRY] " + t);
  479. archive_read_data_skip(a);
  480. } else if (archive_read_extract(a, e, ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_UNLINK) != ARCHIVE_OK) {
  481. if (!fs::is_directory(t)) {
  482. ok = false;
  483. queue_log(" -> Warning (extract): " + t);
  484. }
  485. } else {
  486. queue_log(" -> Restored: " + t);
  487. }
  488. }
  489. archive_read_close(a);
  490. archive_read_free(a);
  491. return ok;
  492. }
  493. std::string MainWindow::get_wallpaper_directory() {
  494. char b[256];
  495. std::string r = "";
  496. FILE *p = popen("gsettings get org.gnome.desktop.background picture-uri-dark 2>/dev/null", "r");
  497. if (p) {
  498. while (fgets(b, 256, p))
  499. r += b;
  500. pclose(p);
  501. }
  502. size_t f = r.find("file://");
  503. if (f != std::string::npos) {
  504. std::string path = r.substr(f + 7);
  505. path.erase(std::remove(path.begin(), path.end(), '\''), path.end());
  506. path.erase(std::remove(path.begin(), path.end(), '\n'), path.end());
  507. return fs::path(path).parent_path().string();
  508. }
  509. return "";
  510. }