2023-04-15 05:16:46 +00:00
# Python imports
2023-04-16 07:47:26 +00:00
import os
2023-04-15 05:16:46 +00:00
# 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
2023-04-16 20:16:03 +00:00
from . . . . io_widget import IOWidget
2023-04-15 05:16:46 +00:00
2023-04-16 04:41:06 +00:00
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
2023-04-15 05:16:46 +00:00
class FileWidget ( WidgetSaveLoadController , Gtk . Box ) :
def __init__ ( self ) :
super ( FileWidget , self ) . __init__ ( )
2023-04-16 20:16:03 +00:00
self . label = None
2023-04-16 07:47:26 +00:00
self . _file = None
2023-04-16 04:41:06 +00:00
self . _file_name = " No File Selected... "
2023-04-15 05:16:46 +00:00
self . _setup_styling ( )
self . _setup_signals ( )
self . _subscribe_to_events ( )
self . _load_widgets ( )
2023-04-16 04:41:06 +00:00
self . set_file_path ( )
2023-04-15 05:16:46 +00:00
event_system . emit ( " register_to_query_controller " , ( self , self . get_query_data ) )
self . show_all ( )
def new ( self ) :
widget = FileWidget ( )
return widget
2023-04-16 20:16:03 +00:00
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 , ) )
2023-04-15 05:16:46 +00:00
def _setup_styling ( self ) :
2023-04-16 04:41:06 +00:00
self . set_orientation ( 1 )
2023-04-15 05:16:46 +00:00
def _setup_signals ( self ) :
self . connect ( " key-release-event " , self . _key_released )
def _subscribe_to_events ( self ) :
. . .
def _load_widgets ( self ) :
2023-04-16 04:41:06 +00:00
box = Gtk . Box ( )
eve_box = Gtk . EventBox ( )
2023-04-16 20:47:32 +00:00
image = Gtk . Image . new_from_icon_name ( " gtk-file " , 6 )
2023-04-16 04:41:06 +00:00
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 ( )
2023-04-15 05:16:46 +00:00
def _key_released ( self , widget = None , eve = None ) :
if eve . type == 9 :
2023-04-16 20:47:32 +00:00
if eve . keyval in [ 32 , 65293 ] :
event_system . emit ( " open_files " , ( [ self . get_file_path ( ) ] , ) )
2023-04-15 05:16:46 +00:00
2023-04-16 04:41:06 +00:00
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
2023-04-15 05:16:46 +00:00
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 ) :
2023-04-16 07:47:26 +00:00
self . set_file_path ( self . save_collection [ " data " ] , True )
2023-04-15 05:16:46 +00:00
2023-04-16 07:47:26 +00:00
def set_file_path ( self , path = settings . get_home_path ( ) , is_loading = False ) :
2023-04-15 05:16:46 +00:00
try :
2023-04-16 07:47:26 +00:00
self . _file = Gio . File . new_for_uri ( path )
if not self . _file . get_path ( ) :
2023-04-16 04:41:06 +00:00
raise FileWidgetException ( " Not URI based path... " )
except FileWidgetException as e :
2023-04-16 07:47:26 +00:00
self . _file = Gio . File . new_for_path ( path )
2023-04-15 05:16:46 +00:00
2023-04-16 04:41:06 +00:00
self . _file_name = self . get_file_name ( )
self . label . set_label ( self . _file_name )
2023-04-16 07:47:26 +00:00
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 \n File: \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 ,
2023-04-16 20:16:03 +00:00
flags = Gio . FileCopyFlags . BACKUP ,
io_priority = 75 ,
cancellable = io_widget . cancle_eve ,
progress_callback = io_widget . update_progress ,
callback = io_widget . finish_callback )
2023-04-16 07:47:26 +00:00
event_system . emit ( " append_to_io_list " , ( io_widget , ) )
self . _file = gio_copy_file
2023-04-16 04:41:06 +00:00
2023-04-15 05:16:46 +00:00
def get_file ( self ) :
2023-04-16 07:47:26 +00:00
return self . _file
def get_file_path ( self , file = None ) :
if not file :
file = self . _file
2023-04-15 05:16:46 +00:00
2023-04-16 07:47:26 +00:00
path = file . get_path ( )
2023-04-15 05:16:46 +00:00
if not path :
2023-04-16 07:47:26 +00:00
path = file . get_uri ( )
2023-04-15 05:16:46 +00:00
return path
2023-04-16 07:47:26 +00:00
def get_file_name ( self , file = None ) :
if not file :
file = self . _file
info = file . query_info ( " standard::* " , 0 , cancellable = None )
2023-04-15 05:16:46 +00:00
return info . get_display_name ( )
2023-04-16 04:41:06 +00:00
2023-04-16 07:47:26 +00:00
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 ( )
2023-04-16 04:41:06 +00:00
def set_current_file ( self ) :
dlg = FileChooser ( )
response = dlg . run ( )
2023-04-16 07:47:26 +00:00
2023-04-16 04:41:06 +00:00
if response == 0 :
widget = dlg . get_file_chooser_widget ( )
uris = widget . get_uris ( )
2023-04-16 07:47:26 +00:00
if len ( uris ) == 1 :
2023-04-16 20:47:32 +00:00
try :
self . delete_cleanup ( )
except Exception as e :
. . .
2023-04-16 07:47:26 +00:00
self . set_file_path ( uris [ 0 ] )
2023-04-16 04:41:06 +00:00
dlg . destroy ( )