Files
Py-Backup/pyimage_ui/system_backup_content_frame.py

167 lines
6.2 KiB
Python

import tkinter as tk
from tkinter import ttk
import os
from core.pbp_app_config import Msg
from pyimage_ui.comment_editor_dialog import CommentEditorDialog
class SystemBackupContentFrame(ttk.Frame):
def __init__(self, master, backup_manager, actions, parent_view, **kwargs):
super().__init__(master, **kwargs)
self.backup_manager = backup_manager
self.actions = actions
self.parent_view = parent_view
self.system_backups_list = []
self.backup_path = None
columns = ("date", "time", "type", "size", "comment")
self.content_tree = ttk.Treeview(
self, columns=columns, show="headings")
self.content_tree.heading("date", text=Msg.STR["date"])
self.content_tree.heading("time", text=Msg.STR["time"])
self.content_tree.heading("type", text=Msg.STR["type"])
self.content_tree.heading("size", text=Msg.STR["size"])
self.content_tree.heading("comment", text=Msg.STR["comment"])
self.content_tree.column("date", width=100, anchor="center")
self.content_tree.column("time", width=100, anchor="center")
self.content_tree.column("type", width=180, anchor="w")
self.content_tree.column("size", width=90, anchor="center")
self.content_tree.column("comment", width=310, anchor="w")
self.content_tree.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
self.content_tree.bind("<<TreeviewSelect>>", self._on_item_select)
def show(self, backup_path, system_backups):
self.backup_path = backup_path
self.system_backups_list = sorted(system_backups, key=lambda b: (b.get('is_encrypted'), b.get('folder_name', '')))
self._load_backup_content()
def _load_backup_content(self):
for i in self.content_tree.get_children():
self.content_tree.delete(i)
if not self.system_backups_list:
return
colors = ["#0078D7", "#7e4818", "#8B107C", "#005A9E", "#2b3e4e"]
last_full_backup_tag = {}
color_index = 0
for backup_info in self.system_backups_list:
is_encrypted = backup_info.get("is_encrypted")
group_key = (is_encrypted,)
current_color_tag = ""
if backup_info.get("backup_type_base") == "Full":
current_color_tag = f"color_{color_index}"
self.content_tree.tag_configure(
current_color_tag, foreground=colors[color_index % len(colors)])
color_index += 1
last_full_backup_tag[group_key] = current_color_tag
else:
if group_key in last_full_backup_tag:
current_color_tag = last_full_backup_tag[group_key]
else:
current_color_tag = ""
backup_type_display = backup_info.get("type", "N/A")
if backup_info.get("backup_type_base") != "Full":
backup_type_display = f"{backup_type_display}"
self.content_tree.insert("", "end", values=(
backup_info.get("date", "N/A"),
backup_info.get("time", "N/A"),
backup_type_display,
backup_info.get("size", "N/A"),
backup_info.get("comment", ""),
), tags=(current_color_tag,), iid=backup_info.get("folder_name"))
self._on_item_select(None)
def _on_item_select(self, event):
is_selected = True if self.content_tree.focus() else False
self.parent_view.update_button_state(is_selected)
def _edit_comment(self):
selected_item_id = self.content_tree.focus()
if not selected_item_id:
return
selected_backup = next((b for b in self.system_backups_list if b.get(
"folder_name") == selected_item_id), None)
if not selected_backup:
return
info_file_path = selected_backup.get('info_file_path')
if not info_file_path or not os.path.isfile(info_file_path):
MessageDialog(self, message_type="error", title="Error",
text=f"Metadata file not found: {info_file_path}")
return
CommentEditorDialog(self, info_file_path, self.backup_manager)
self.parent_view.show(
self.backup_path, self.system_backups_list) # Refresh view
def _restore_selected(self):
selected_item_id = self.content_tree.focus()
if not selected_item_id:
return
selected_backup = next((b for b in self.system_backups_list if b.get(
"folder_name") == selected_item_id), None)
if not selected_backup:
return
main_app = self.winfo_toplevel()
restore_dest_path = main_app.config_manager.get_setting(
"restore_destination_path", "/")
if not restore_dest_path:
return
self.backup_manager.start_restore(
source_path=selected_backup['full_path'],
dest_path=restore_dest_path,
is_compressed=selected_backup.get('is_compressed', False)
)
def _delete_selected(self):
selected_item_id = self.content_tree.focus()
if not selected_item_id:
return
selected_backup = next((b for b in self.system_backups_list if b.get(
"folder_name") == selected_item_id), None)
if not selected_backup:
return
folder_to_delete = selected_backup['full_path']
is_encrypted = selected_backup['is_encrypted']
password = None
if is_encrypted:
username = os.path.basename(self.backup_path.rstrip('/'))
password = self.backup_manager.encryption_manager.get_password(
username, confirm=False)
if not password:
self.actions.logger.log(
"Password entry cancelled, aborting deletion.")
return
self.actions._set_ui_state(False)
self.parent_view.show_deletion_status(
Msg.STR["deleting_backup_in_progress"])
self.backup_manager.start_delete_backup(
path_to_delete=folder_to_delete,
is_encrypted=is_encrypted,
is_system=True,
base_dest_path=self.backup_path,
password=password,
queue=self.winfo_toplevel().queue
)