209 lines
8.6 KiB
Python
209 lines
8.6 KiB
Python
import tkinter as tk
|
|
from tkinter import ttk
|
|
from tkinter import messagebox
|
|
from tkinter import filedialog
|
|
|
|
class MainWindow:
|
|
def __init__(self, controller):
|
|
self.controller = controller
|
|
self.controller.register_window(self)
|
|
self.root = tk.Tk()
|
|
self.root.title("SecCon - © Louis SWINNEN 2022")
|
|
self.root.config(bd=5)
|
|
self.host = tk.StringVar()
|
|
self.host.set("ffe.labo.swilabus.com")
|
|
self.passw = tk.StringVar()
|
|
self.passw.set("aaaaa")
|
|
self.login = tk.StringVar()
|
|
self.login.set("aaaaa")
|
|
self.tls = tk.BooleanVar()
|
|
self.tls.set(True)
|
|
self.port = tk.IntVar()
|
|
self.port.set(8000)
|
|
self.draw_window()
|
|
|
|
def start_main_loop(self):
|
|
self.tf_host.focus()
|
|
self.root.mainloop()
|
|
|
|
def draw_window(self):
|
|
self.draw_top_pane()
|
|
self.draw_center_pane()
|
|
self.draw_footer_pane()
|
|
self.not_connected_mode()
|
|
|
|
def draw_top_pane(self):
|
|
top_pane = ttk.LabelFrame(self.root, padding=(3, 3, 12, 12), text=" Connection to FileFrontEnd ")
|
|
top_pane.pack(fill=tk.X)
|
|
top_pane.columnconfigure(1, weight=1)
|
|
|
|
ttk.Label(top_pane, text="Host:").grid(row=0, column=0, sticky=tk.E)
|
|
self.tf_host = ttk.Entry(top_pane, textvariable=self.host)
|
|
self.tf_host.grid(row=0, column=1, sticky=tk.EW, padx=5, pady=5)
|
|
|
|
ttk.Label(top_pane, text="Port:").grid(row=0, column=2, sticky=tk.E)
|
|
self.tf_port = ttk.Entry(top_pane, textvariable=self.port)
|
|
self.tf_port.grid(row=0, column=3, sticky=tk.EW, padx=5, pady=2)
|
|
|
|
ttk.Label(top_pane, text="Login:").grid(row=1, column=0, sticky=tk.E)
|
|
self.tf_login = ttk.Entry(top_pane, textvariable=self.login)
|
|
self.tf_login.grid(row=1,column=1, sticky=tk.EW, padx=5, pady=5)
|
|
|
|
ttk.Label(top_pane, text="Password:").grid(row=1, column=2, sticky=tk.E)
|
|
self.tf_password = ttk.Entry(top_pane, show="*", textvariable=self.passw)
|
|
self.tf_password.grid(row=1,column=3, sticky=tk.EW, padx=5, pady=2)
|
|
|
|
inner_frame = ttk.Frame(top_pane)
|
|
inner_frame.grid(row=2, column=0, columnspan=4, sticky=tk.NSEW)
|
|
inner_frame.columnconfigure(1,weight=1)
|
|
|
|
ttk.Label(inner_frame, text="Security:").grid(row=0, column=0, sticky=tk.E)
|
|
self.cb_tls = ttk.Checkbutton(inner_frame, text="with SSL/TLS", variable=self.tls)
|
|
self.cb_tls.grid(row=0, column=1, padx=5, pady=2, sticky=tk.W)
|
|
self.bt_signup = ttk.Button(inner_frame, text="Sign up", command=self.sign_up)
|
|
self.bt_signup.grid(row=0, column=2, padx=5, pady=2,sticky=tk.E)
|
|
self.bt_signin = ttk.Button(inner_frame, text="Sign in", command=self.sign_in)
|
|
self.bt_signin.grid(row=0, column=3, padx=5, pady=2, sticky=tk.E)
|
|
|
|
|
|
|
|
def draw_center_pane(self):
|
|
title_pane = ttk.Frame(self.root, padding=(5, 0, 5, 0))
|
|
ttk.Label(title_pane, text="My files:").grid(row=0, column=0, sticky=tk.E)
|
|
title_pane.pack(fill=tk.X)
|
|
center_pane = ttk.Frame(self.root, padding=(5, 0, 5, 0))
|
|
center_pane.pack(fill=tk.BOTH, expand=True)
|
|
center_pane.columnconfigure(1, weight=1)
|
|
|
|
self.scroll = ttk.Scrollbar(center_pane)
|
|
self.tv_files = ttk.Treeview(center_pane, yscrollcommand=self.scroll.set, height=6)
|
|
self.tv_files["columns"] = ["Filename", "Size"]
|
|
self.tv_files["show"] = "headings"
|
|
self.tv_files.heading("Filename", text="Filename")
|
|
self.tv_files.heading("Size", text="Filesize")
|
|
self.tv_files.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, pady=5)
|
|
self.scroll.config(command=self.tv_files.yview)
|
|
self.scroll.pack(side=tk.RIGHT, fill=tk.Y, pady=5)
|
|
|
|
bottom_pane = ttk.Frame(self.root, padding=(5, 0, 5, 0))
|
|
bottom_pane.columnconfigure(1, weight=1)
|
|
self.bt_filelist = ttk.Button(bottom_pane, text="Refresh list", command=self.refresh_list)
|
|
self.bt_filelist.grid(row=0, column=0, padx=10, pady=2,sticky=tk.E)
|
|
self.bt_savefile = ttk.Button(bottom_pane, text="Upload file", command=self.upload_file)
|
|
self.bt_savefile.grid(row=0, column=1, padx=10, pady=2,sticky=tk.E)
|
|
self.bt_getfile = ttk.Button(bottom_pane, text="Download file", command=self.get_file)
|
|
self.bt_getfile.grid(row=0, column=2, padx=10, pady=2,sticky=tk.E)
|
|
self.bt_removefile = ttk.Button(bottom_pane, text="Remove file", command=self.remove_file)
|
|
self.bt_removefile.grid(row=0, column=3, padx=10, pady=2,sticky=tk.E)
|
|
bottom_pane.pack(fill=tk.X)
|
|
|
|
def draw_footer_pane(self):
|
|
footer_pane = ttk.Frame(self.root, padding=(5,10,5,0))
|
|
footer_pane.pack(fill=tk.X)
|
|
footer_pane.columnconfigure(1, weight=1)
|
|
self.bt_about = ttk.Button(footer_pane, text="About", command=self.about)
|
|
self.bt_about.grid(row=0, column=0, pady=3, sticky=tk.E)
|
|
self.bt_quit = ttk.Button(footer_pane, text="Quit", command=self.quit)
|
|
self.bt_quit.grid(row=0, column=1, pady=3, sticky=tk.E)
|
|
|
|
def not_connected_mode(self):
|
|
self.bt_filelist.state(["disabled"])
|
|
self.bt_savefile.state(["disabled"])
|
|
self.bt_getfile.state(["disabled"])
|
|
self.bt_removefile.state(["disabled"])
|
|
|
|
def connected_mode(self):
|
|
self.tf_host.state(["disabled"])
|
|
self.tf_port.state(["disabled"])
|
|
self.tf_login.state(["disabled"])
|
|
self.tf_password.state(["disabled"])
|
|
self.bt_signin.state(["disabled"])
|
|
self.bt_signup.state(["disabled"])
|
|
self.cb_tls.state(["disabled"])
|
|
self.bt_filelist.state(["!disabled"])
|
|
self.bt_savefile.state(["!disabled"])
|
|
self.bt_getfile.state(["!disabled"])
|
|
self.bt_removefile.state(["!disabled"])
|
|
|
|
def sign_in(self):
|
|
if self.check_before_signin():
|
|
print("[MainWindow] Calling MainWindowController::on_signin()")
|
|
self.controller.on_signin(self.host.get(), int(self.port.get()), self.login.get(), self.passw.get(), self.tls.get())
|
|
else:
|
|
self.show_message("All fields are mandatory!\nPort is numeric (1025-65535)", True)
|
|
|
|
def sign_up(self):
|
|
if self.check_before_signin():
|
|
print("[MainWindow] Calling MainWindowController::on_signup()")
|
|
self.controller.on_signup(self.host.get(), int(self.port.get()), self.login.get(), self.passw.get(), self.tls.get())
|
|
else:
|
|
self.show_message("All fields are mandatory!\nPort is numeric (1025-65535)", True)
|
|
|
|
def check_before_signin(self):
|
|
try:
|
|
host = self.host.get()
|
|
port = int(self.port.get())
|
|
login = self.login.get()
|
|
passw = self.passw.get()
|
|
tls = self.tls.get()
|
|
if(not(host and host.strip())) or port <= 1024 or port >= 65536 or not (login and login.strip()) or not(passw and passw.strip()):
|
|
return False
|
|
else:
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
|
|
def show_message(self, message, is_error):
|
|
if is_error:
|
|
messagebox.showerror("Error", message)
|
|
else:
|
|
messagebox.showinfo("Information", message)
|
|
|
|
def show_select_file_dialog(self):
|
|
filepath = filedialog.askopenfilename()
|
|
return filepath
|
|
|
|
def show_save_directory_dialog(self):
|
|
filedir = filedialog.askdirectory()
|
|
return filedir
|
|
|
|
def upload_file(self):
|
|
path = self.show_select_file_dialog()
|
|
self.controller.on_fileupload(path)
|
|
pass
|
|
|
|
def get_file(self):
|
|
current_item = self.tv_files.focus()
|
|
selected_file = self.tv_files.item(current_item)['values']
|
|
if selected_file is None or selected_file == '':
|
|
self.show_message("No file selected !", True)
|
|
else:
|
|
path = self.show_save_directory_dialog()
|
|
if path is None or path == '':
|
|
self.show_message("No directory selected !", True)
|
|
else:
|
|
self.controller.on_filedownload(selected_file[0], path)
|
|
|
|
def remove_file(self):
|
|
current_item = self.tv_files.focus()
|
|
selected_file = self.tv_files.item(current_item)['values']
|
|
if selected_file is None or selected_file == '':
|
|
self.show_message("No file selected !", True)
|
|
else:
|
|
self.controller.on_filedelete(selected_file[0])
|
|
|
|
def refresh_list(self):
|
|
self.controller.on_refreshlist()
|
|
|
|
def about(self):
|
|
pass
|
|
|
|
def quit(self):
|
|
self.root.destroy()
|
|
self.controller.on_quit()
|
|
|
|
def remove_all_from_treeview(self):
|
|
self.tv_files.delete(*self.tv_files.get_children())
|
|
|
|
def add_file_in_treeview(self, fileinfo):
|
|
self.tv_files.insert('', tk.END, values = fileinfo) |