from net.UnicastConnection import * from model.Protocol import * import traceback import os.path class AppController: def __init__(self, parser): self.parser = parser self.connection = UnicastConnection() self.window_controller = None def ssl_ca_path(self, ca_path): self.connection.set_ca_path(ca_path) def set_window_controller(self, window_controller): self.window_controller = window_controller def on_sign(self, host, port, login, passw, tls, register=False): print("[AppController::on_sign]") try: if not self.connection.is_connected: print("[AppController::on_sign] connecting ... calling UnicastConnection::connect") self.connect(host, port, tls) if not register: message = self.parser.build_SIGNIN(login, passw) else: message = self.parser.build_SIGNUP(login, passw) self.connection.send_message(message) line = self.connection.read_line() message_id = self.parser.parse(line, False) if message_id == Protocol.PARSE_SIGNOK: self.window_controller.switch_connected_mode() self.show_message("Connection done", False) self.on_filelist() elif message_id == Protocol.PARSE_SIGNERROR: self.show_message("Error during SIGN_IN / SIGN_UP", True) self.connection.close() except Exception as ex: print(ex) traceback.print_exc() def get_filename_size_from_item(self, item): tokens = item.split('!') if len(tokens) == 2: return (tokens[0], tokens[1]) else: return None def on_filelist(self): print("[AppController::on_filelist]") try: if self.connection.is_connected: message = self.parser.build_FILELIST() self.connection.send_message(message) line = self.connection.read_line() message_id = self.parser.parse(line, False) if message_id == Protocol.PARSE_FILES: tokens = self.parser.parse_FILES(line) if len(tokens) == 1: self.window_controller.clear_all_file_on_window() allfiles = tokens[0].split() for f in allfiles: (filename, filesize) = self.get_filename_size_from_item(f) self.window_controller.add_file_on_window([filename, filesize]) except Exception as ex: print(ex) traceback.print_exc() def on_quit(self): if self.connection.is_connected: self.connection.send_message(self.parser.build_SIGNOUT()) def on_fileupload(self, filepath): if os.path.isfile(filepath): size = os.path.getsize(filepath) print(f"[AppController::on_fileupload] file={filepath}; size={size}") message = self.parser.build_SAVEFILE(os.path.basename(filepath), size) self.connection.send_message(message) self.connection.send_file(filepath) line = self.connection.read_line() message_id = self.parser.parse(line, False) if message_id == Protocol.PARSE_SAVEFILEOK: self.show_message("File saved", False) self.on_filelist() elif message_id == Protocol.PARSE_SAVEFILEERROR: self.show_message("Error during saving file !", True) else: self.show_message(f"Error: file {filepath} not found !", True) def on_filedownload(self, filename, savepath): if os.path.isdir(savepath): print(f"[AppController::on_filedownload] file={filename}; path={savepath}") message = self.parser.build_GETFILE(filename) self.connection.send_message(message) line = self.connection.read_line() message_id = self.parser.parse(line, False) if message_id == Protocol.PARSE_GETFILEOK: tokens = self.parser.parse_GETFILEOK(line) if len(tokens) == 2: self.connection.receive_file(tokens[0], savepath, int(tokens[1])) self.show_message("File downloaded !", False) else: self.show_message(f"Bad file information\n{tokens}") elif message_id == Protocol.PARSE_GETFILEERROR: self.show_message(f"Cannot download the file", True) else: self.show_message(f"Error: directory {savepath} not found !", True) def on_filedelete(self, filename): print(f"[AppController::on_filedelete] file={filename}") message = self.parser.build_REMOVEFILE(filename) self.connection.send_message(message) line = self.connection.read_line() message_id = self.parser.parse(line, False) if message_id == Protocol.PARSE_REMOVEFILEOK: self.show_message("File deleted successfully !", False) self.on_filelist() elif message_id == Protocol.PARSE_REMOVEFILEERROR: self.show_message("Error while erasing file.", True) def show_message(self, message, is_error): self.window_controller.show_message(message, is_error) def connect(self, host, port, tls): try: self.connection.connect(host, port, tls) if tls and UnicastConnection.STRICT_SSL_VALIDATION: self.show_message(self.connection.ssl_info, False) elif tls and not UnicastConnection.STRICT_SSL_VALIDATION: self.show_message("SSL/TLS enabled, but certificate NOT checked !", False) else: self.show_message("No SSL connection", False) except Exception as ex: self.show_message(f"Error during connection !\n{ex}", True) self.connection = UnicastConnection()