|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import ttk, messagebox, PhotoImage |
| 3 | +import ftplib |
| 4 | +import os |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +class FileExplorer(tk.Frame): |
| 8 | + def __init__(self, master, app): |
| 9 | + super().__init__(master) |
| 10 | + self.app = app # Save reference to FileCatApp instance |
| 11 | + self.ftp = None |
| 12 | + self.current_path = tk.StringVar() |
| 13 | + |
| 14 | + self.url_entry = tk.Entry(self) |
| 15 | + self.url_entry.pack(fill=tk.X) |
| 16 | + self.url_entry.bind("<Return>", self.change_directory) |
| 17 | + |
| 18 | + self.tree = ttk.Treeview(self) |
| 19 | + self.tree["columns"] = ("size", "type") |
| 20 | + self.tree.heading("#0", text="Name", anchor=tk.W) |
| 21 | + self.tree.heading("size", text="Size", anchor=tk.W) |
| 22 | + self.tree.heading("type", text="Type", anchor=tk.W) |
| 23 | + |
| 24 | + self.tree.column("size", stretch=tk.YES) |
| 25 | + self.tree.column("type", stretch=tk.YES) |
| 26 | + self.url_frame = tk.Frame(self) |
| 27 | + self.url_frame.pack(fill=tk.X) |
| 28 | + self.tree.pack(expand=True, fill=tk.BOTH) |
| 29 | + icon_path = os.path.join(os.path.dirname(__file__), "folder_icon.png") |
| 30 | + self.folder_icon = PhotoImage(file=icon_path) |
| 31 | + up_icon = PhotoImage(file="up_icon.png") |
| 32 | + self.up_button = tk.Button(self.url_frame, image=up_icon, command=self.up_directory) |
| 33 | + self.up_button.image = up_icon |
| 34 | + self.up_button.pack(side=tk.RIGHT, padx=5, pady=5) |
| 35 | + self.tree.bind("<Double-1>", self.on_double_click) |
| 36 | + |
| 37 | + # Add delete button |
| 38 | + self.delete_button = tk.Button(self.url_frame, text="Delete", command=self.delete_item) |
| 39 | + self.delete_button.pack(side=tk.RIGHT, padx=5, pady=5) |
| 40 | + |
| 41 | + # Add reconnect button |
| 42 | + self.reconnect_button = tk.Button(self.url_frame, text="Reconnect", command=self.reconnect) |
| 43 | + self.reconnect_button.pack(side=tk.RIGHT, padx=5, pady=5) |
| 44 | + |
| 45 | + # Add upload button next to delete button |
| 46 | + self.app.upload_button = tk.Button(self.url_frame, text="Upload", command=self.app.upload, state=tk.DISABLED) |
| 47 | + self.app.upload_button.pack(side=tk.RIGHT, padx=5, pady=5) |
| 48 | + |
| 49 | + def update_treeview(self, files): |
| 50 | + self.tree.delete(*self.tree.get_children()) |
| 51 | + self.tree["columns"] = ("size", "type", "last_modified") |
| 52 | + self.tree.heading("size", text="Size", anchor=tk.W) |
| 53 | + self.tree.heading("type", text="Type", anchor=tk.W) |
| 54 | + self.tree.heading("last_modified", text="Last Modified", anchor=tk.W) |
| 55 | + |
| 56 | + directories = [] |
| 57 | + files_list = [] |
| 58 | + |
| 59 | + for name, details in files: |
| 60 | + if details['type'] == 'dir': |
| 61 | + directories.append((name, details)) |
| 62 | + else: |
| 63 | + files_list.append((name, details)) |
| 64 | + |
| 65 | + for name, details in directories: |
| 66 | + last_modified = self._format_last_modified(details.get('modify', '')) |
| 67 | + self.tree.insert("", "end", text=name, open=False, image=self.folder_icon, |
| 68 | + values=("", "Directory", last_modified)) |
| 69 | + |
| 70 | + for name, details in files_list: |
| 71 | + size_kb = int(details.get('size', 0)) / 1024 |
| 72 | + last_modified = self._format_last_modified(details.get('modify', '')) |
| 73 | + self.tree.insert("", "end", values=(f"{size_kb:.2f} KB", "File", last_modified), text=name) |
| 74 | + |
| 75 | + def delete_item(self): |
| 76 | + item_id = self.tree.selection()[0] |
| 77 | + item = self.tree.item(item_id) |
| 78 | + item_text = item["text"] |
| 79 | + |
| 80 | + if messagebox.askyesno("Confirm Delete", f"Are you sure you want to delete '{item_text}'?"): |
| 81 | + try: |
| 82 | + if item["values"][1] == "File": |
| 83 | + self.ftp.delete(os.path.join(self.current_path.get(), item_text)) |
| 84 | + elif "directory" in item["tags"]: |
| 85 | + self.ftp.rmd(os.path.join(self.current_path.get(), item_text)) |
| 86 | + self.change_directory() |
| 87 | + except ftplib.error_perm as e: |
| 88 | + messagebox.showerror("Error", str(e)) |
| 89 | + |
| 90 | + def reconnect(self): |
| 91 | + self.app.reconnect() # Use the FileCatApp instance to call connect method |
| 92 | + |
| 93 | + def _format_last_modified(self, raw_timestamp): |
| 94 | + try: |
| 95 | + timestamp = datetime.strptime(raw_timestamp, "%Y%m%d%H%M%S") |
| 96 | + formatted_date = timestamp.strftime("%Y-%m-%d %H:%M:%S") |
| 97 | + return formatted_date |
| 98 | + except ValueError: |
| 99 | + return "" |
| 100 | + |
| 101 | + def change_directory(self, event=None): |
| 102 | + new_path = self.url_entry.get().strip() |
| 103 | + |
| 104 | + new_path = new_path.replace("\\", "/") |
| 105 | + |
| 106 | + if not new_path.startswith("/"): |
| 107 | + new_path = "/" + new_path |
| 108 | + |
| 109 | + try: |
| 110 | + files = self.ftp.mlsd(new_path) |
| 111 | + files = [(name, details) for name, details in files if name not in ['.', '..']] |
| 112 | + |
| 113 | + self.current_path.set(new_path) |
| 114 | + self.update_treeview(files) |
| 115 | + except ftplib.error_perm as e: |
| 116 | + messagebox.showerror("Error", str(e)) |
| 117 | + |
| 118 | + def on_double_click(self, event): |
| 119 | + item_id = self.tree.focus() |
| 120 | + item = self.tree.item(item_id) |
| 121 | + item_text = item["text"] |
| 122 | + item_type = item["values"][1] |
| 123 | + |
| 124 | + if item_text != "": |
| 125 | + if item_type == "Directory": |
| 126 | + self.url_entry.delete(0, tk.END) |
| 127 | + new_path = os.path.join(self.current_path.get(), item_text) |
| 128 | + self.url_entry.insert(0, new_path) |
| 129 | + self.change_directory() |
| 130 | + elif item_type == "File": |
| 131 | + self.download_file(item_text) |
| 132 | + |
| 133 | + def up_directory(self): |
| 134 | + current_path = self.url_entry.get().strip() |
| 135 | + if current_path != "/": |
| 136 | + parent_path = os.path.dirname(current_path) |
| 137 | + self.url_entry.delete(0, tk.END) |
| 138 | + self.url_entry.insert(0, parent_path) |
| 139 | + self.change_directory() |
| 140 | + |
| 141 | + def download_file(self, filename): |
| 142 | + remote_filename = os.path.join(self.current_path.get(), filename) |
| 143 | + remote_filename = remote_filename.replace("\\", "/") |
| 144 | + |
| 145 | + downloads_folder = os.path.join(os.getcwd(), "downloads") |
| 146 | + if not os.path.exists(downloads_folder): |
| 147 | + os.makedirs(downloads_folder) |
| 148 | + |
| 149 | + local_filename = os.path.join(downloads_folder, filename) |
| 150 | + |
| 151 | + with open(local_filename, 'wb') as local_file: |
| 152 | + try: |
| 153 | + self.ftp.retrbinary('RETR ' + remote_filename, local_file.write) |
| 154 | + messagebox.showinfo("Download", f"{filename} downloaded successfully.") |
| 155 | + except ftplib.error_perm as e: |
| 156 | + messagebox.showerror("Download Error", f"Failed to download {filename}: {str(e)}") |
0 commit comments