2021-10-10 06:45:55 +00:00
|
|
|
# Python imports
|
2022-02-20 05:15:02 +00:00
|
|
|
import os, gc, threading, time
|
2021-10-10 06:45:55 +00:00
|
|
|
|
2021-12-07 04:44:39 +00:00
|
|
|
# Lib imports
|
2021-10-14 02:30:08 +00:00
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
2021-12-07 04:44:39 +00:00
|
|
|
from gi.repository import Gtk, GLib
|
2021-10-10 06:45:55 +00:00
|
|
|
|
|
|
|
# Application imports
|
2022-02-20 07:32:51 +00:00
|
|
|
from .mixins.exception_hook_mixin import ExceptionHookMixin
|
|
|
|
from .mixins.ui_mixin import UIMixin
|
|
|
|
from .signals.ipc_signals_mixin import IPCSignalsMixin
|
|
|
|
from .signals.keyboard_signals_mixin import KeyboardSignalsMixin
|
|
|
|
from .controller_data import Controller_Data
|
2021-10-10 06:45:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def threaded(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
2021-12-25 05:58:57 +00:00
|
|
|
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
|
2021-10-10 06:45:55 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2021-12-07 04:44:39 +00:00
|
|
|
|
|
|
|
|
2022-02-02 03:08:02 +00:00
|
|
|
class Controller(UIMixin, KeyboardSignalsMixin, IPCSignalsMixin, ExceptionHookMixin, Controller_Data):
|
2022-02-20 07:32:51 +00:00
|
|
|
""" Controller coordinates the mixins and is somewhat the root hub of it all. """
|
2021-11-28 02:01:20 +00:00
|
|
|
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()
|
2022-02-11 06:58:49 +00:00
|
|
|
|
2021-10-10 06:45:55 +00:00
|
|
|
self.generate_windows(self.state)
|
2022-01-27 04:29:22 +00:00
|
|
|
self.plugins.launch_plugins()
|
2021-10-10 06:45:55 +00:00
|
|
|
|
2022-02-11 06:58:49 +00:00
|
|
|
if debug:
|
|
|
|
self.window.set_interactive_debugging(True)
|
|
|
|
|
2022-01-20 05:49:19 +00:00
|
|
|
if not trace_debug:
|
|
|
|
self.gui_event_observer()
|
2021-11-25 08:21:10 +00:00
|
|
|
|
2022-01-20 05:49:19 +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-12-02 01:22:03 +00:00
|
|
|
|
2021-10-14 02:30:08 +00:00
|
|
|
@threaded
|
|
|
|
def gui_event_observer(self):
|
2021-12-25 05:58:57 +00:00
|
|
|
while True:
|
2021-10-14 02:30:08 +00:00
|
|
|
time.sleep(event_sleep_time)
|
|
|
|
event = event_system.consume_gui_event()
|
|
|
|
if event:
|
2021-11-16 07:28:08 +00:00
|
|
|
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,))
|
2021-11-16 07:28:08 +00:00
|
|
|
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)
|
|
|
|
|
2022-02-15 03:12:07 +00:00
|
|
|
def open_terminal(self, widget=None, eve=None):
|
|
|
|
wid, tid = self.window_controller.get_active_wid_and_tid()
|
|
|
|
view = self.get_fm_window(wid).get_view_by_id(tid)
|
|
|
|
dir = view.get_current_directory()
|
|
|
|
view.execute(f"{view.terminal_app}", dir)
|
2021-11-29 08:01:51 +00:00
|
|
|
|
2022-02-02 05:29:42 +00:00
|
|
|
def save_load_session(self, action="save_session"):
|
2022-02-11 06:58:49 +00:00
|
|
|
wid, tid = self.window_controller.get_active_wid_and_tid()
|
2022-02-02 05:29:42 +00:00
|
|
|
view = self.get_fm_window(wid).get_view_by_id(tid)
|
|
|
|
save_load_dialog = self.builder.get_object("save_load_dialog")
|
|
|
|
|
|
|
|
if action == "save_session":
|
2022-02-05 00:14:11 +00:00
|
|
|
self.window_controller.save_state()
|
|
|
|
return
|
|
|
|
elif action == "save_session_as":
|
2022-02-02 05:29:42 +00:00
|
|
|
save_load_dialog.set_action(Gtk.FileChooserAction.SAVE)
|
|
|
|
elif action == "load_session":
|
|
|
|
save_load_dialog.set_action(Gtk.FileChooserAction.OPEN)
|
2022-02-05 00:14:11 +00:00
|
|
|
else:
|
|
|
|
raise Exception(f"Unknown action given: {action}")
|
2022-02-02 05:29:42 +00:00
|
|
|
|
2022-02-04 23:46:03 +00:00
|
|
|
save_load_dialog.set_current_folder(view.get_current_directory())
|
|
|
|
save_load_dialog.set_current_name("session.json")
|
2022-02-02 05:29:42 +00:00
|
|
|
response = save_load_dialog.run()
|
|
|
|
if response == Gtk.ResponseType.OK:
|
|
|
|
if action == "save_session":
|
2022-02-04 23:46:03 +00:00
|
|
|
path = f"{save_load_dialog.get_current_folder()}/{save_load_dialog.get_current_name()}"
|
2022-02-02 05:29:42 +00:00
|
|
|
self.window_controller.save_state(path)
|
|
|
|
elif action == "load_session":
|
2022-02-04 23:46:03 +00:00
|
|
|
path = f"{save_load_dialog.get_file().get_path()}"
|
2022-02-02 05:29:42 +00:00
|
|
|
session_json = self.window_controller.load_state(path)
|
2022-02-04 23:46:03 +00:00
|
|
|
self.load_session(session_json)
|
2022-02-02 05:29:42 +00:00
|
|
|
if (response == Gtk.ResponseType.CANCEL) or (response == Gtk.ResponseType.DELETE_EVENT):
|
|
|
|
pass
|
|
|
|
|
|
|
|
save_load_dialog.hide()
|
|
|
|
|
2022-02-04 23:46:03 +00:00
|
|
|
def load_session(self, session_json):
|
|
|
|
if debug:
|
|
|
|
print(f"Session Data: {session_json}")
|
|
|
|
|
|
|
|
self.ctrlDown = False
|
|
|
|
self.shiftDown = False
|
|
|
|
self.altDown = False
|
|
|
|
for notebook in self.notebooks:
|
|
|
|
self.clear_children(notebook)
|
|
|
|
|
|
|
|
self.window_controller.unload_views_and_windows()
|
|
|
|
self.generate_windows(session_json)
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
2022-02-20 06:28:34 +00:00
|
|
|
def do_action_from_menu_controls(self, widget, event_button):
|
2022-02-20 04:46:54 +00:00
|
|
|
action = widget.get_name()
|
2021-11-22 22:24:44 +00:00
|
|
|
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()
|
2021-12-01 22:26:20 +00:00
|
|
|
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()
|
2021-11-29 08:01:51 +00:00
|
|
|
if action == "archive":
|
|
|
|
self.show_archiver_dialogue()
|
2021-11-22 22:24:44 +00:00
|
|
|
if action == "delete":
|
2021-12-02 01:22:03 +00:00
|
|
|
self.delete_files()
|
|
|
|
if action == "trash":
|
2021-11-22 22:24:44 +00:00
|
|
|
self.trash_files()
|
2021-11-29 08:01:51 +00:00
|
|
|
if action == "go_to_trash":
|
2022-02-02 05:29:42 +00:00
|
|
|
self.path_entry.set_text(self.trash_files_path)
|
2021-12-29 00:07:11 +00:00
|
|
|
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":
|
2022-02-15 03:12:07 +00:00
|
|
|
self.show_new_file_menu()
|
2022-02-05 00:14:11 +00:00
|
|
|
if action in ["save_session", "save_session_as", "load_session"]:
|
2022-02-02 05:29:42 +00:00
|
|
|
self.save_load_session(action)
|