diff --git a/src/core/controllers/__init__.py b/src/core/controllers/__init__.py new file mode 100644 index 0000000..c004b70 --- /dev/null +++ b/src/core/controllers/__init__.py @@ -0,0 +1,3 @@ +""" + Controllers Module +""" \ No newline at end of file diff --git a/src/core/controller.py b/src/core/controllers/base_controller.py similarity index 89% rename from src/core/controller.py rename to src/core/controllers/base_controller.py index 27f15e2..cdcf13e 100644 --- a/src/core/controller.py +++ b/src/core/controllers/base_controller.py @@ -10,13 +10,13 @@ from gi.repository import Gdk from gi.repository import GLib # Application imports -from .controller_data import ControllerData -from .containers.core_widget import CoreWidget -from .mixins.signals_mixins import SignalsMixins +from ..mixins.signals_mixins import SignalsMixins +from ..containers.core_widget import CoreWidget +from .base_controller_data import BaseControllerData -class Controller(SignalsMixins, ControllerData): +class BaseController(SignalsMixins, BaseControllerData): def __init__(self, args, unknownargs): messages = [] for arg in unknownargs + [args.new_tab,]: diff --git a/src/core/controller_data.py b/src/core/controllers/base_controller_data.py similarity index 94% rename from src/core/controller_data.py rename to src/core/controllers/base_controller_data.py index def6d80..61c4e11 100644 --- a/src/core/controller_data.py +++ b/src/core/controllers/base_controller_data.py @@ -10,8 +10,8 @@ from plugins.plugins_controller import PluginsController -class ControllerData: - ''' ControllerData contains most of the state of the app at ay given time. It also has some support methods. ''' +class BaseControllerData: + ''' BaseControllerData contains most of the state of the app at ay given time. It also has some support methods. ''' def setup_controller_data(self) -> None: self.window = settings_manager.get_main_window() diff --git a/src/core/controllers/bridge_controller.py b/src/core/controllers/bridge_controller.py new file mode 100644 index 0000000..4a527c7 --- /dev/null +++ b/src/core/controllers/bridge_controller.py @@ -0,0 +1,39 @@ +# Python imports +import base64 + +# Lib imports + +# Application imports + + + +class BridgeController: + def __init__(self): + + self.opened_files = {} + + self._setup_signals() + self._subscribe_to_events() + + + def _setup_signals(self): + ... + + def _subscribe_to_events(self): + event_system.subscribe("handle_bridge_event", self.handle_bridge_event) + + + def handle_bridge_event(self, event): + match event.topic: + case "save": + event_system.emit("handle_file_event", (event,)) + case "close": + event_system.emit("handle_file_event", (event,)) + case "error": + content = base64.b64decode( event.content.encode() ).decode("utf-8") + logger.info(content) + case _: + ... + + + diff --git a/src/core/controllers/files_controller.py b/src/core/controllers/files_controller.py new file mode 100644 index 0000000..9a626c0 --- /dev/null +++ b/src/core/controllers/files_controller.py @@ -0,0 +1,97 @@ +# Python imports +import os +import base64 + +# Lib imports +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk + +# Application imports + + + +class FilesController: + def __init__(self): + + self.opened_files = {} + + self._setup_signals() + self._subscribe_to_events() + + + def _setup_signals(self): + ... + + def _subscribe_to_events(self): + event_system.subscribe("set_pre_drop_dnd", self.set_pre_drop_dnd) + event_system.subscribe("handle_file_event", self.handle_file_event) + + def set_pre_drop_dnd(self, gfiles): + keys = self.opened_files.keys() + + for gfile in gfiles: + fhash = str(gfile.hash()) + if fhash in keys: continue + + ftype, fhash = self.insert_to_sessions(fhash, gfile) + + content = None + path = gfile.get_path() + with open(path) as f: + content = base64.b64encode( f.read().encode(), altchars = None ).decode("utf-8") + + event_system.emit("load_file", (ftype, fhash, gfile.get_basename(), content)) + + def handle_file_event(self, event): + match event.topic: + case "save": + content = base64.b64decode( event.content.encode() ).decode("utf-8") + self.save_session(event.target, content) + case "close": + self.close_session(event.target) + case _: + return + + def save_session(self, fhash, content): + keys = self.opened_files.keys() + gfile = event_system.emit_and_await( + "save_file_dialog", ("", None) + ) if not fhash in keys else self.opened_files[fhash]["file"] + + if not gfile: return + + file_written = self.write_to_file(fhash, gfile, content) + if not fhash in keys and file_written: + self.insert_to_sessions(fhash, gfile) + event_system.emit( + "updated_tab", + ( + self.opened_files[fhash]["ftype"], + gfile.get_basename(), + ) + ) + + + def close_session(self, target): + del self.opened_files[target] + + def insert_to_sessions(self, fhash, gfile): + info = gfile.query_info("standard::*", 0, cancellable = None) + ftype = info.get_content_type().replace("x-", "").split("/")[1] + + self.opened_files[fhash] = {"file": gfile, "ftype": ftype} + + return ftype, fhash + + def write_to_file(self, fhash, gfile, content): + with open(gfile.get_path(), 'w') as outfile: + try: + outfile.write(content) + except Exception as e: + message = f"[Error]: Could NOT save {gfile.get_basename()} ..." + event_system.emit("ui_message", (message, "error",)) + return False + + return True + diff --git a/src/core/window.py b/src/core/window.py index a30c852..96a9193 100644 --- a/src/core/window.py +++ b/src/core/window.py @@ -1,5 +1,4 @@ # Python imports -import time import signal # Lib imports @@ -12,7 +11,8 @@ from gi.repository import Gdk from gi.repository import GLib # Application imports -from core.controller import Controller +from core.controllers.base_controller import BaseController + class ControllerStartExceptiom(Exception): @@ -62,9 +62,9 @@ class Window(Gtk.ApplicationWindow): if settings_manager.is_debug(): self.set_interactive_debugging(True) - self._controller = Controller(args, unknownargs) + self._controller = BaseController(args, unknownargs) if not self._controller: - raise ControllerStartException("Controller exited and doesn't exist...") + raise ControllerStartException("BaseController exited and doesn't exist...") self.add( self._controller.get_core_widget() )