Files
Py-Backup/pyimage_ui/user_backup_content_frame.py
Désiré Werner Menrath 2d685e1d97 refactor: Rework encrypted backup UI and logic
- Centralizes backup content view logic into a single BackupContentFrame.
- Removes the separate, now obsolete EncryptedBackupContentFrame.
- Adds a toggle button within the BackupContentFrame to switch between viewing normal and encrypted backups.
- Centralizes the Restore, Delete, and Edit Comment buttons into a single button bar in BackupContentFrame.
- Corrects the path resolution logic to find backups and encrypted containers within the /pybackup subdirectory.
- Fixes UI bugs where action buttons would disappear when switching tabs.
2025-09-04 23:22:12 +02:00

108 lines
4.3 KiB
Python

import tkinter as tk
from tkinter import ttk
import os
import shutil
from pbp_app_config import Msg
from pyimage_ui.comment_editor_dialog import CommentEditorDialog
from shared_libs.message import MessageDialog
class UserBackupContentFrame(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.user_backups_list = []
self.backup_path = None
columns = ("date", "time", "size", "comment", "folder_name")
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("size", text=Msg.STR["size"])
self.content_tree.heading("comment", text=Msg.STR["comment"])
self.content_tree.heading("folder_name", text=Msg.STR["folder"])
self.content_tree.column("date", width=100, anchor="w")
self.content_tree.column("time", width=80, anchor="center")
self.content_tree.column("size", width=100, anchor="e")
self.content_tree.column("comment", width=250, anchor="w")
self.content_tree.column("folder_name", width=200, 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):
self.backup_path = backup_path
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.backup_path or not os.path.isdir(self.backup_path):
return
self.user_backups_list = self.backup_manager.list_user_backups(self.backup_path)
for backup_info in self.user_backups_list:
self.content_tree.insert("", "end", values=(
backup_info.get("date", "N/A"),
backup_info.get("time", "N/A"),
backup_info.get("size", "N/A"),
backup_info.get("comment", ""),
backup_info.get("folder_name", "N/A")
), 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
info_file_path = os.path.join(self.backup_path, f"{selected_item_id}.txt")
if not os.path.exists(info_file_path):
self.backup_manager.update_comment(info_file_path, "")
CommentEditorDialog(self, info_file_path, self.backup_manager)
self._load_backup_content()
def _restore_selected(self):
selected_item_id = self.content_tree.focus()
if not selected_item_id:
return
MessageDialog(master=self, message_type="info", title="Info", text="User restore not implemented yet.")
def _delete_selected(self):
selected_item_id = self.content_tree.focus()
if not selected_item_id:
return
folder_to_delete = os.path.join(self.backup_path, selected_item_id)
info_file_to_delete = os.path.join(self.backup_path, f"{selected_item_id}.txt")
dialog = MessageDialog(master=self, message_type="warning",
title=Msg.STR["confirm_delete_title"],
text=Msg.STR["confirm_delete_text"].format(
folder_name=selected_item_id),
buttons=["ok_cancel"])
if dialog.get_result() != "ok":
return
try:
if os.path.isdir(folder_to_delete):
shutil.rmtree(folder_to_delete)
if os.path.exists(info_file_to_delete):
os.remove(info_file_to_delete)
self._load_backup_content()
except Exception as e:
MessageDialog(master=self, message_type="error",
title=Msg.STR["error"], text=str(e))