SolarFM/src/versions/solarfm-0.0.1/SolarFM/solarfm/controller/Controller.py

117 lines
3.8 KiB
Python
Raw Normal View History

2021-10-10 06:45:55 +00:00
# Python imports
import traceback, threading, inspect, os, time
2021-10-10 06:45:55 +00:00
# Lib imports
2021-10-14 02:30:08 +00:00
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib
2021-10-10 06:45:55 +00:00
# Application imports
from .mixins import ExceptionHookMixin, UIMixin
2022-01-31 00:09:00 +00:00
from .signals import IPCSignalsMixin, KeyboardSignalsMixin
from . import Controller_Data
2021-10-10 06:45:55 +00:00
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
2021-10-10 06:45:55 +00:00
return wrapper
class Controller(UIMixin, KeyboardSignalsMixin, IPCSignalsMixin, ExceptionHookMixin, Controller_Data):
''' Controller coordinates the mixins and is somewhat the root hub of it all. '''
def __init__(self, args, unknownargs, _settings):
2022-01-24 16:24:55 +00:00
self.setup_controller_data(_settings)
2021-10-10 06:45:55 +00:00
self.window.show()
self.generate_windows(self.state)
self.plugins.launch_plugins()
2021-10-10 06:45:55 +00:00
if not trace_debug:
self.gui_event_observer()
2021-11-25 08:21:10 +00:00
if unknownargs:
for arg in unknownargs:
if os.path.isdir(arg):
message = f"FILE|{arg}"
event_system.send_ipc_message(message)
if args.new_tab and os.path.isdir(args.new_tab):
message = f"FILE|{args.new_tab}"
event_system.send_ipc_message(message)
2021-11-25 08:21:10 +00:00
2021-11-22 03:52:46 +00:00
def tear_down(self, widget=None, eve=None):
2021-11-25 08:21:10 +00:00
event_system.send_ipc_message("close server")
2021-11-26 22:50:58 +00:00
self.window_controller.save_state()
2021-11-22 03:52:46 +00:00
time.sleep(event_sleep_time)
Gtk.main_quit()
2021-10-14 02:30:08 +00:00
@threaded
def gui_event_observer(self):
while True:
2021-10-14 02:30:08 +00:00
time.sleep(event_sleep_time)
event = event_system.consume_gui_event()
if event:
try:
type, target, data = event
2022-02-01 07:43:09 +00:00
if type:
method = getattr(self.__class__, "handle_gui_event_and_set_message")
GLib.idle_add(method, *(self, type, target, data))
else:
method = getattr(self.__class__, target)
GLib.idle_add(method, *(self, *data,))
except Exception as e:
print(repr(e))
2022-02-01 07:43:09 +00:00
def handle_gui_event_and_set_message(self, type, target, parameters):
method = getattr(self.__class__, f"{target}")
data = method(*(self, *parameters))
self.plugins.set_message_on_plugin(type, data)
def do_action_from_menu_controls(self, widget, eventbutton):
action = widget.get_name()
2021-11-22 22:24:44 +00:00
self.ctrlDown = True
self.hide_context_menu()
2021-11-26 22:50:58 +00:00
self.hide_new_file_menu()
self.hide_edit_file_menu()
2021-11-22 22:24:44 +00:00
2021-12-05 00:23:00 +00:00
if action == "open":
self.open_files()
if action == "open_with":
self.show_appchooser_menu()
if action == "execute":
self.execute_files()
if action == "execute_in_terminal":
self.execute_files(in_terminal=True)
2021-11-26 22:50:58 +00:00
if action == "rename":
self.rename_files()
2021-11-22 22:24:44 +00:00
if action == "cut":
self.to_copy_files.clear()
self.cut_files()
if action == "copy":
self.to_cut_files.clear()
self.copy_files()
if action == "paste":
self.paste_files()
if action == "archive":
self.show_archiver_dialogue()
2021-11-22 22:24:44 +00:00
if action == "delete":
self.delete_files()
if action == "trash":
2021-11-22 22:24:44 +00:00
self.trash_files()
if action == "go_to_trash":
self.builder.get_object("path_entry").set_text(self.trash_files_path)
if action == "restore_from_trash":
self.restore_trash_files()
if action == "empty_trash":
self.empty_trash()
2021-11-22 22:24:44 +00:00
2021-12-05 00:23:00 +00:00
if action == "create":
2021-12-06 23:21:19 +00:00
self.create_files()
2021-12-05 00:23:00 +00:00
2021-11-22 22:24:44 +00:00
self.ctrlDown = False