generated from itdominator/Python-With-Gtk-Template
Moved controllers and added some for upcoming webkit
This commit is contained in:
parent
f225f627c1
commit
58f00ea83e
3
src/core/controllers/__init__.py
Normal file
3
src/core/controllers/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
"""
|
||||||
|
Controllers Module
|
||||||
|
"""
|
@ -10,13 +10,13 @@ from gi.repository import Gdk
|
|||||||
from gi.repository import GLib
|
from gi.repository import GLib
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
from .controller_data import ControllerData
|
from ..mixins.signals_mixins import SignalsMixins
|
||||||
from .containers.core_widget import CoreWidget
|
from ..containers.core_widget import CoreWidget
|
||||||
from .mixins.signals_mixins import SignalsMixins
|
from .base_controller_data import BaseControllerData
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Controller(SignalsMixins, ControllerData):
|
class BaseController(SignalsMixins, BaseControllerData):
|
||||||
def __init__(self, args, unknownargs):
|
def __init__(self, args, unknownargs):
|
||||||
messages = []
|
messages = []
|
||||||
for arg in unknownargs + [args.new_tab,]:
|
for arg in unknownargs + [args.new_tab,]:
|
@ -10,8 +10,8 @@ from plugins.plugins_controller import PluginsController
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ControllerData:
|
class BaseControllerData:
|
||||||
''' ControllerData contains most of the state of the app at ay given time. It also has some support methods. '''
|
''' 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:
|
def setup_controller_data(self) -> None:
|
||||||
self.window = settings_manager.get_main_window()
|
self.window = settings_manager.get_main_window()
|
39
src/core/controllers/bridge_controller.py
Normal file
39
src/core/controllers/bridge_controller.py
Normal file
@ -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 _:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
97
src/core/controllers/files_controller.py
Normal file
97
src/core/controllers/files_controller.py
Normal file
@ -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
|
||||||
|
|
@ -1,5 +1,4 @@
|
|||||||
# Python imports
|
# Python imports
|
||||||
import time
|
|
||||||
import signal
|
import signal
|
||||||
|
|
||||||
# Lib imports
|
# Lib imports
|
||||||
@ -12,7 +11,8 @@ from gi.repository import Gdk
|
|||||||
from gi.repository import GLib
|
from gi.repository import GLib
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
from core.controller import Controller
|
from core.controllers.base_controller import BaseController
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ControllerStartExceptiom(Exception):
|
class ControllerStartExceptiom(Exception):
|
||||||
@ -62,9 +62,9 @@ class Window(Gtk.ApplicationWindow):
|
|||||||
if settings_manager.is_debug():
|
if settings_manager.is_debug():
|
||||||
self.set_interactive_debugging(True)
|
self.set_interactive_debugging(True)
|
||||||
|
|
||||||
self._controller = Controller(args, unknownargs)
|
self._controller = BaseController(args, unknownargs)
|
||||||
if not self._controller:
|
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() )
|
self.add( self._controller.get_core_widget() )
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user