MainWindow.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "MainWindow.hpp"
  2. #include <iostream>
  3. #include <filesystem>
  4. #include <archive.h>
  5. #include <archive_entry.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <cstdlib>
  9. namespace fs = std::filesystem;
  10. MainWindow::MainWindow()
  11. : m_MainLayout(Gtk::ORIENTATION_VERTICAL, 0),
  12. m_VBoxBackup(Gtk::ORIENTATION_VERTICAL, 15),
  13. m_LabelBackupInstruction("<span size='large' weight='bold'>Select what to backup:</span>"),
  14. m_CheckThemesBackup("GTK Themes (~/.themes)"),
  15. m_CheckIconsBackup("Icons (~/.icons)"),
  16. m_CheckDconfBackup("GNOME Settings (dconf dump)"),
  17. m_ButtonBackup("Start Backup"),
  18. m_VBoxRestore(Gtk::ORIENTATION_VERTICAL, 15),
  19. m_LabelRestoreInstruction("<span size='large' weight='bold'>Select what to restore:</span>"),
  20. m_CheckThemesRestore("GTK Themes"),
  21. m_CheckIconsRestore("Icons"),
  22. m_CheckDconfRestore("GNOME Settings"),
  23. m_ButtonRestore("Start Restore")
  24. {
  25. // --- Force Dark Theme ---
  26. auto settings = Gtk::Settings::get_default();
  27. if (settings) {
  28. settings->property_gtk_application_prefer_dark_theme() = true;
  29. }
  30. set_default_size(550, 450);
  31. set_border_width(0); // Removing window border to let layout handle it
  32. // --- HeaderBar Setup (Modern GNOME look) ---
  33. m_HeaderBar.set_title("Gnome-Vault");
  34. m_HeaderBar.set_subtitle("System State Manager");
  35. m_HeaderBar.set_show_close_button(true);
  36. set_titlebar(m_HeaderBar);
  37. // Allow markup in the instruction labels
  38. m_LabelBackupInstruction.set_use_markup(true);
  39. m_LabelBackupInstruction.set_halign(Gtk::ALIGN_START);
  40. m_LabelRestoreInstruction.set_use_markup(true);
  41. m_LabelRestoreInstruction.set_halign(Gtk::ALIGN_START);
  42. // --- Backup Page Setup ---
  43. m_VBoxBackup.set_border_width(20);
  44. m_VBoxBackup.pack_start(m_LabelBackupInstruction, Gtk::PACK_SHRINK);
  45. m_VBoxBackup.pack_start(m_CheckThemesBackup, Gtk::PACK_SHRINK);
  46. m_VBoxBackup.pack_start(m_CheckIconsBackup, Gtk::PACK_SHRINK);
  47. m_VBoxBackup.pack_start(m_CheckDconfBackup, Gtk::PACK_SHRINK);
  48. m_CheckThemesBackup.set_active(true);
  49. // Make the button chunkier
  50. m_ButtonBackup.set_size_request(-1, 45);
  51. m_ButtonBackup.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_button_backup_clicked));
  52. m_VBoxBackup.pack_end(m_ButtonBackup, Gtk::PACK_SHRINK);
  53. // --- Restore Page Setup ---
  54. m_VBoxRestore.set_border_width(20);
  55. m_VBoxRestore.pack_start(m_LabelRestoreInstruction, Gtk::PACK_SHRINK);
  56. m_VBoxRestore.pack_start(m_CheckThemesRestore, Gtk::PACK_SHRINK);
  57. m_VBoxRestore.pack_start(m_CheckIconsRestore, Gtk::PACK_SHRINK);
  58. m_VBoxRestore.pack_start(m_CheckDconfRestore, Gtk::PACK_SHRINK);
  59. m_ButtonRestore.set_size_request(-1, 45);
  60. m_ButtonRestore.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_button_restore_clicked));
  61. m_VBoxRestore.pack_end(m_ButtonRestore, Gtk::PACK_SHRINK);
  62. // --- Notebook (Tabs) Setup ---
  63. m_Notebook.append_page(m_VBoxBackup, "Backup");
  64. m_Notebook.append_page(m_VBoxRestore, "Restore");
  65. // --- Status Label Setup ---
  66. m_StatusLabel.set_text("Ready.");
  67. m_StatusLabel.set_halign(Gtk::ALIGN_START);
  68. m_StatusLabel.set_margin_top(10);
  69. m_StatusLabel.set_margin_bottom(10);
  70. m_StatusLabel.set_margin_start(10);
  71. // --- Main Layout Assembly ---
  72. m_MainLayout.pack_start(m_Notebook, Gtk::PACK_EXPAND_WIDGET);
  73. m_MainLayout.pack_end(m_StatusLabel, Gtk::PACK_SHRINK);
  74. add(m_MainLayout);
  75. show_all_children();
  76. }
  77. MainWindow::~MainWindow() {}
  78. bool MainWindow::create_tar_archive(const std::string& source_dir, const std::string& out_filename) {
  79. struct archive *a;
  80. struct archive *disk;
  81. struct archive_entry *entry;
  82. char buff[8192];
  83. int len;
  84. int fd;
  85. a = archive_write_new();
  86. archive_write_add_filter_gzip(a);
  87. archive_write_set_format_pax_restricted(a);
  88. archive_write_open_filename(a, out_filename.c_str());
  89. disk = archive_read_disk_new();
  90. archive_read_disk_set_standard_lookup(disk);
  91. for (const auto& dirEntry : fs::recursive_directory_iterator(source_dir)) {
  92. std::string path = dirEntry.path().string();
  93. entry = archive_entry_new();
  94. archive_entry_copy_pathname(entry, path.c_str());
  95. archive_read_disk_entry_from_file(disk, entry, -1, nullptr);
  96. archive_write_header(a, entry);
  97. if (fs::is_regular_file(dirEntry)) {
  98. fd = ::open(path.c_str(), O_RDONLY);
  99. if (fd >= 0) {
  100. len = ::read(fd, buff, sizeof(buff));
  101. while (len > 0) {
  102. archive_write_data(a, buff, len);
  103. len = ::read(fd, buff, sizeof(buff));
  104. }
  105. ::close(fd);
  106. }
  107. }
  108. archive_entry_free(entry);
  109. }
  110. archive_read_free(disk);
  111. archive_write_close(a);
  112. archive_write_free(a);
  113. return true;
  114. }
  115. void MainWindow::on_button_backup_clicked() {
  116. m_StatusLabel.set_text("Initializing Backup Sequence...");
  117. // Keep the cout for the terminal debugger
  118. std::cout << ">>> Initializing Backup Sequence..." << std::endl;
  119. std::string home_dir = getenv("HOME");
  120. if (m_CheckThemesBackup.get_active()) {
  121. std::string target = home_dir + "/.themes";
  122. std::string out = home_dir + "/gnome_themes_backup.tar.gz";
  123. if (fs::exists(target)) {
  124. m_StatusLabel.set_text("Packing Themes into archive...");
  125. create_tar_archive(target, out);
  126. m_StatusLabel.set_text("Success: Packed ~/.themes into gnome_themes_backup.tar.gz");
  127. } else {
  128. m_StatusLabel.set_text("Warning: ~/.themes doesn't exist. Skipped.");
  129. }
  130. }
  131. if (m_CheckIconsBackup.get_active()) std::cout << " [x] Queuing Icons (TODO)..." << std::endl;
  132. if (m_CheckDconfBackup.get_active()) std::cout << " [x] Queuing Dconf (TODO)..." << std::endl;
  133. }
  134. void MainWindow::on_button_restore_clicked() {
  135. m_StatusLabel.set_text("Initializing Restore Sequence...");
  136. std::cout << ">>> Initializing Restore Sequence..." << std::endl;
  137. if (m_CheckThemesRestore.get_active()) std::cout << " [x] Preparing to overwrite Themes..." << std::endl;
  138. if (m_CheckIconsRestore.get_active()) std::cout << " [x] Preparing to overwrite Icons..." << std::endl;
  139. if (m_CheckDconfRestore.get_active()) std::cout << " [x] Preparing to inject Dconf..." << std::endl;
  140. }