Skip to content

Commit a55a82e

Browse files
authored
add support for saving a configuration without creating an archive in the GUI
1 parent ce22811 commit a55a82e

File tree

1 file changed

+66
-34
lines changed

1 file changed

+66
-34
lines changed

src/main_window.py

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,24 @@ def create_pb_desktop():
162162
# Action after closing dialog for showing more options
163163
def msDialog_closed(w, response):
164164
if response == 'ok':
165-
# Save the periodic backup settings
166-
settings["filename-format"] = self.filefrmtEntry.get_text()
167-
settings["periodic-saving-folder"] = self.dirRow.get_subtitle()
165+
settings["filename-format"] = self.filefrmtEntry.get_text() # save the file name format entry
166+
settings["periodic-saving-folder"] = self.dirRow.get_subtitle() # save the selected periodic saving folder
167+
# save the periodic saving interval
168168
selected_item = self.pbRow.get_selected_item()
169169
backup_mapping = {_["never"]: "Never", _["daily"]: "Daily", _["weekly"]: "Weekly", _["monthly"]: "Monthly"}
170170
backup_item = backup_mapping.get(selected_item.get_string(), "Never")
171171
create_pb_desktop() if not backup_item == "Never" else None
172172
settings["periodic-saving"] = backup_item
173-
# Save Archive encryption creditionals
174-
settings["enable-encryption"] = self.encryptSwitch.get_active()
173+
settings["enable-encryption"] = self.encryptSwitch.get_active() # save the archive encryption's switch state
174+
settings["save-without-archive"] = self.archSwitch.get_active() # save the switch state of the "Save a configuration without creating the configuration archive" option
175+
# save the entered password to the file
175176
if self.cpwdRow.get_text():
176177
password = self.cpwdRow.get_text()
177178
PasswordStore(password)
178179
else:
179180
os.remove(f"{DATA}/password")
180181

182+
# restart the SetDialog() after closing this dialog
181183
if self.open_setdialog_tf:
182184
self.setDialog.close()
183185
w = ""
@@ -191,7 +193,7 @@ def open_pb_wiki(w):
191193
def reset_fileformat(w):
192194
self.filefrmtEntry.set_text("Latest_configuration")
193195

194-
# Dialog for showing more options itself
196+
# Dialog itself
195197
self.msDialog = Adw.AlertDialog.new()
196198
self.msDialog.set_heading(_["more_options"])
197199
self.msDialog.choose(self, None, None, None)
@@ -272,11 +274,11 @@ def reset_fileformat(w):
272274
self.saving_eRow.add_row(self.cpwdRow)
273275

274276
# Manual saving section
275-
# action row and switch for showing options of the archive encryption
276277
self.manRow = Adw.ExpanderRow.new()
277278
self.manRow.set_title("Manual saving")
278279
self.msBox.append(self.manRow)
279280

281+
# action row and switch for showing options of the archive encryption
280282
self.encryptSwitch = Gtk.Switch.new()
281283
self.encryptSwitch.set_valign(Gtk.Align.CENTER)
282284
if settings["enable-encryption"] == True:
@@ -289,6 +291,20 @@ def reset_fileformat(w):
289291
self.encryptRow.add_suffix(self.encryptSwitch)
290292
self.encryptRow.set_activatable_widget(self.encryptSwitch)
291293
self.manRow.add_row(self.encryptRow)
294+
295+
# action row and switch for showing the "Save a configuration without creating the archive" option
296+
self.archSwitch = Gtk.Switch.new()
297+
self.archSwitch.set_valign(Gtk.Align.CENTER)
298+
if settings["save-without-archive"] == True:
299+
self.archSwitch.set_active(True)
300+
301+
self.archRow = Adw.ActionRow.new()
302+
self.archRow.set_title("Save a configuration without creating the archive")
303+
#self.archRow.set_subtitle()
304+
#self.archRow.set_subtitle_lines(15)
305+
self.archRow.add_suffix(self.archSwitch)
306+
self.archRow.set_activatable_widget(self.archSwitch)
307+
self.manRow.add_row(self.archRow)
292308

293309
# add response of this dialog
294310
self.msDialog.add_response('cancel', _["cancel"])
@@ -298,6 +314,7 @@ def reset_fileformat(w):
298314

299315
self.msDialog.present()
300316

317+
# open a dialog for selecting the items to include in the configuration archive
301318
def open_itemsDialog(w):
302319
self.itemsd = itemsDialog()
303320
self.itemsd.choose(self, None, None, None)
@@ -1018,9 +1035,10 @@ def open_selected(source, res, data):
10181035
self.file_chooser.set_title(_["import_fileshooser"].format(self.environment))
10191036
self.file_filter = Gtk.FileFilter.new()
10201037
self.file_filter.set_name(_["savedesktop_f"])
1021-
self.file_filter.add_pattern('*.sd.tgz')
1038+
self.file_filter.add_pattern('*.sd.tzst')
10221039
self.file_filter.add_pattern('*.sd.tar.gz')
10231040
self.file_filter.add_pattern('*.sd.zip')
1041+
self.file_filter.add_pattern('SELECT_THIS_FILE_TO_IMPORT_CFG')
10241042
self.file_filter_list = Gio.ListStore.new(Gtk.FileFilter);
10251043
self.file_filter_list.append(self.file_filter)
10261044
self.file_chooser.set_filters(self.file_filter_list)
@@ -1125,18 +1143,21 @@ def start_saving(self):
11251143
e_o = False
11261144
os.system(f"python3 {system_dir}/config.py --save")
11271145
print("creating and moving the configuration archive to the user-defined directory")
1128-
if settings["enable-encryption"] == True:
1129-
os.system(f"zip -9 -P \'{self.password}\' cfg.sd.zip . -r")
1130-
if self.cancel_process:
1131-
return
1132-
else:
1133-
shutil.copyfile('cfg.sd.zip', f"{self.folder}/{self.filename_text}.sd.zip")
1146+
if settings["save-without-archive"] == True:
1147+
os.system(f"echo > {CACHE}/save_config/SELECT_THIS_FILE_TO_IMPORT_CFG && mv {CACHE}/save_config '{self.folder}/{self.filename_text}'")
11341148
else:
1135-
os.system(f"tar --exclude='cfg.sd.tgz' --gzip -cf cfg.sd.tgz ./")
1136-
if self.cancel_process:
1137-
return
1149+
if settings["enable-encryption"] == True:
1150+
os.system(f"zip -9 -P \'{self.password}\' cfg.sd.zip . -r")
1151+
if self.cancel_process:
1152+
return
1153+
else:
1154+
shutil.copyfile('cfg.sd.zip', f"{self.folder}/{self.filename_text}.sd.zip")
11381155
else:
1139-
shutil.copyfile('cfg.sd.tgz', f"{self.folder}/{self.filename_text}.sd.tgz")
1156+
os.system(f"tar --exclude='cfg.sd.tzst' -I 'zstd -T0 -10' -cf cfg.sd.tzst ./")
1157+
if self.cancel_process:
1158+
return
1159+
else:
1160+
shutil.copyfile('cfg.sd.tzst', f"{self.folder}/{self.filename_text}.sd.tzst")
11401161
print("Configuration saved successfully.")
11411162
except Exception as e:
11421163
e_o = True
@@ -1186,9 +1207,9 @@ def cancel_save(w):
11861207
self.savewaitBox.append(self.sdoneImage)
11871208

11881209
# Use "sd.zip" if Archive Encryption is enabled
1189-
status = _["saving_config_status"].replace("sd.tar.gz", "sd.tgz")
1210+
status = _["saving_config_status"].replace("sd.tar.gz", "sd.tzst") if not settings["save-without-archive"] else _["saving_config_status"].replace("sd.tar.gz", "")
11901211
if settings["enable-encryption"]:
1191-
status = status.replace("sd.tgz", "sd.zip")
1212+
status = status.replace("sd.tzst", "sd.zip")
11921213

11931214
# Create label about selected directory for saving the configuration
11941215
self.savewaitLabel = Gtk.Label.new(str=status.format(self.folder, self.filename_text))
@@ -1292,21 +1313,26 @@ def start_importing(self):
12921313
try:
12931314
e_o = False
12941315
os.system("echo > import_status")
1295-
if ".sd.zip" in self.import_file:
1296-
with zipfile.ZipFile(self.import_file, "r") as zip_ar:
1297-
for member in zip_ar.namelist():
1298-
if self.cancel_process:
1299-
return
1300-
zip_ar.extract(member, path=f"{CACHE}/import_config", pwd=self.checkEntry.get_text().encode("utf-8"))
1316+
if "SELECT_THIS_FILE_TO_IMPORT_CFG" in self.import_file:
1317+
shutil.copytree(self.import_file[:-31], f"{CACHE}/import_config", dirs_exist_ok=True, ignore_dangling_symlinks=True)
13011318
else:
1302-
with tarfile.open(self.import_file, 'r:gz') as tar:
1303-
for member in tar.getmembers():
1304-
try:
1319+
if ".sd.zip" in self.import_file:
1320+
with zipfile.ZipFile(self.import_file, "r") as zip_ar:
1321+
for member in zip_ar.namelist():
13051322
if self.cancel_process:
13061323
return
1307-
tar.extract(member, path=f"{CACHE}/import_config")
1308-
except PermissionError as e:
1309-
print(f"Permission denied for {member.name}: {e}")
1324+
zip_ar.extract(member, path=f"{CACHE}/import_config", pwd=self.checkEntry.get_text().encode("utf-8"))
1325+
elif ".sd.tar.gz" in self.import_file:
1326+
with tarfile.open(self.import_file, 'r:gz') as tar:
1327+
for member in tar.getmembers():
1328+
try:
1329+
if self.cancel_process:
1330+
return
1331+
tar.extract(member, path=f"{CACHE}/import_config")
1332+
except PermissionError as e:
1333+
print(f"Permission denied for {member.name}: {e}")
1334+
else:
1335+
os.system(f"tar -xf {self.import_file} -C {CACHE}/import_config")
13101336
os.system(f"python3 {system_dir}/config.py --import_")
13111337
os.system("rm import_status") if all(not os.path.exists(app_path) for app_path in [f"{CACHE}/import_config/app", f"{CACHE}/import_config/installed_flatpaks.sh", f"{CACHE}/import_config/installed_user_flatpaks.sh"]) else None
13121338
print("Configuration imported successfully.")
@@ -1539,9 +1565,15 @@ def logout(self, action, param):
15391565
else:
15401566
os.system("gdbus call --session --dest org.gnome.SessionManager --object-path /org/gnome/SessionManager --method org.gnome.SessionManager.Logout 1")
15411567

1542-
# open directory with created configuration archive after clicking on the "Open the folder" button
1568+
# open a directory with created configuration archive after clicking on the "Open the folder" button
15431569
def open_dir(self, action, param):
1544-
Gtk.FileLauncher.new(Gio.File.new_for_path(f"{self.win.folder}/{self.win.filename_text}.sd.tgz" if not settings["enable-encryption"] else f"{self.win.folder}/{self.win.filename_text}.sd.zip")).open_containing_folder()
1570+
if settings["save-without-archive"]:
1571+
path = f"{self.win.folder}/{self.win.filename_text}"
1572+
else:
1573+
ext = "tzst" if not settings["enable-encryption"] else "zip"
1574+
path = f"{self.win.folder}/{self.win.filename_text}.sd.{ext}"
1575+
1576+
Gtk.FileLauncher.new(Gio.File.new_for_path(path)).open_containing_folder()
15451577

15461578
# "About app" dialog
15471579
def on_about_action(self, action, param):

0 commit comments

Comments
 (0)