# Python imports import os # Lib imports import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk from gi.repository import Gio # Application imports from utils.widget_save_load_controller import WidgetSaveLoadController from ....io_widget import IOWidget class FileWidgetException(Exception): ... class FileChooser(Gtk.Dialog): """docstring for FileChooser.""" def __init__(self): super(FileChooser, self).__init__() self._file_chooser_widget = None self._setup_styling() self._setup_signals() self._subscribe_to_events() self._load_widgets() self.show_all() def _setup_styling(self): ... def _setup_signals(self): ... def _subscribe_to_events(self): ... def _load_widgets(self): hbox = self.get_content_area() self._file_chooser_widget = Gtk.FileChooserWidget() self._file_chooser_widget.set_select_multiple(True) hbox.add(self._file_chooser_widget) self.add_button("Cancel", 1) self.add_button("Select", 0) def get_file_chooser_widget(self): return self._file_chooser_widget class FileWidget(WidgetSaveLoadController, Gtk.Box): def __init__(self): super(FileWidget, self).__init__() self.label = None self._file = None self._file_name = "No File Selected..." self._setup_styling() self._setup_signals() self._subscribe_to_events() self._load_widgets() self.set_file_path() event_system.emit("register_to_query_controller", (self, self.get_query_data)) self.show_all() def new(self): widget = FileWidget() return widget def delete_cleanup(self): working_page = settings.get_active_page().replace('MANIFEST', '') path = self._file.get_path() if not os.path.isdir(path) and working_page in path: logger.debug(f"Deleteing: {path}") io_widget = IOWidget("delete", self._file) self._file.delete_async(io_priority = 75, cancellable = io_widget.cancle_eve, callback = io_widget.finish_callback) event_system.emit("append_to_io_list", (io_widget,)) def _setup_styling(self): self.set_orientation(1) def _setup_signals(self): self.connect("key-release-event", self._key_released) def _subscribe_to_events(self): ... def _load_widgets(self): box = Gtk.Box() eve_box = Gtk.EventBox() image = Gtk.Image.new_from_icon_name("gtk-file", 6) self.label = Gtk.Label(self._file_name) box.set_orientation(1) eve_box.connect('button-press-event', self._clicked) box.add(image) box.add(self.label) eve_box.add(box) self.add(eve_box) eve_box.show_all() def _key_released(self, widget = None, eve = None): if eve.type == 9: if eve.keyval in [32, 65293]: event_system.emit("open_files", ( [self.get_file_path()], ) ) def _clicked(self, widget = None, eve = None): if eve.button == 1 and eve.type == 5: # NOTE: Left dbl click event_system.emit("open_files", ( [self.get_file_path()], ) ) return if eve.button == 3 and eve.type == 4: # NOTE: Right click try: self.set_current_file() self.get_parent().save_needed = True except FileWidgetException as e: logger.debug(e) return def get_query_data(self): return self.get_file_name() def get_saveable_data(self): self.save_collection["data"] = self.get_file_path() self.save_collection["widget_type"] = str(type(self).__name__) return self.save_collection def set_saveable_data(self, data): self.save_collection = data self.load_saveable_data() def load_saveable_data(self): self.set_file_path( self.save_collection["data"], True) def set_file_path(self, path = settings.get_home_path(), is_loading = False): try: self._file = Gio.File.new_for_uri(path) if not self._file.get_path(): raise FileWidgetException("Not URI based path...") except FileWidgetException as e: self._file = Gio.File.new_for_path(path) self._file_name = self.get_file_name() self.label.set_label(self._file_name) self._process_path(is_loading) def _process_path(self, is_loading = False): fpath = self.get_file_path() if not is_loading and not os.path.isdir(fpath): size = self.get_file_size() if size > 25000000: # NOTE: Aproximatly 25MB response = event_system.emit_and_await("confirm_action", \ (None, None, f"File is greator than 25MB. Keep as link or copy to notebook?\n\nFile:\n\t\t{self._file_name}\n\n(No) Use Link Only\t\t\t\t(Yes) Copy To Notebook")) if response in [Gtk.ResponseType.NO, Gtk.ResponseType.DELETE_EVENT]: return self.copy_to_notebook() def copy_to_notebook(self): gio_copy_file = None io_widget = IOWidget("copy", self._file) working_page = settings.get_active_page().replace('MANIFEST', '') files_dir = os.path.join(working_page, "files") copy_tgt_pth = os.path.join(files_dir, self._file_name) if not os.path.exists(files_dir): os.mkdir(files_dir) if os.path.exists(copy_tgt_pth): return try: gio_copy_file = Gio.File.new_for_uri(copy_tgt_pth) if not gio_copy_file.get_path(): raise FileWidgetException("Not URI based path...") except FileWidgetException as e: gio_copy_file = Gio.File.new_for_path(copy_tgt_pth) self._file.copy_async(destination = gio_copy_file, flags = Gio.FileCopyFlags.BACKUP, io_priority = 75, cancellable = io_widget.cancle_eve, progress_callback = io_widget.update_progress, callback = io_widget.finish_callback) event_system.emit("append_to_io_list", (io_widget,)) self._file = gio_copy_file def get_file(self): return self._file def get_file_path(self, file = None): if not file: file = self._file path = file.get_path() if not path: path = file.get_uri() return path def get_file_name(self, file = None): if not file: file = self._file info = file.query_info("standard::*", 0, cancellable = None) return info.get_display_name() def get_file_size(self, file = None): if not file: file = self._file info = file.query_info("standard::*", 0, cancellable = None) return info.get_size() def set_current_file(self): dlg = FileChooser() response = dlg.run() if response == 0: widget = dlg.get_file_chooser_widget() uris = widget.get_uris() if len(uris) == 1: try: self.delete_cleanup() except Exception as e: ... self.set_file_path(uris[0]) dlg.destroy()