MainWindow.cpp 21 KB

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