2022-09-04 17:38:45 +02:00
import os
2022-09-12 00:29:39 +02:00
import webbrowser
2022-09-11 11:52:41 +02:00
from webpicdownloader . controller . Frames import Frames
2022-09-12 00:29:39 +02:00
from webpicdownloader . util . UpdateUtils import check_for_update
2022-09-04 17:38:45 +02:00
2022-08-30 12:28:59 +02:00
class MainController :
"""
2022-08-30 21:12:04 +02:00
Controller - MainController
2022-08-30 12:28:59 +02:00
2022-09-05 23:13:38 +02:00
This controller manages all the main interaction , change of windows ,
dialogs , stop . . . It is the main controller .
2022-08-30 21:12:04 +02:00
@author Jérémi Nihart / EndMove
@link https : / / git . endmove . eu / EndMove / WebPicDownloader
2022-09-12 00:29:39 +02:00
@version 1.0 .1
@since 2022 - 09 - 11
2022-08-30 12:28:59 +02:00
"""
# Variables
2022-09-01 18:05:58 +02:00
__config : dict = None
2022-08-30 13:51:50 +02:00
__view = None
2022-09-01 22:07:14 +02:00
__quite_event_subscribers : list = None
2022-08-30 12:28:59 +02:00
# Constructor
2022-09-01 18:05:58 +02:00
def __init__ ( self , config : dict ) - > None :
"""
Constructor
2022-09-04 17:38:45 +02:00
* : config : - > The application configuration ( a dictionary ) .
2022-09-01 18:05:58 +02:00
"""
2022-09-01 22:07:14 +02:00
# Setup variables
2022-09-01 18:05:58 +02:00
self . __config = config
2022-09-01 22:07:14 +02:00
self . __quite_event_subscribers = [ ]
2022-08-30 12:28:59 +02:00
2022-08-30 21:12:04 +02:00
# START View methods
def set_view ( self , view ) - > None :
2022-08-30 12:28:59 +02:00
"""
2022-08-30 21:12:04 +02:00
[ function for view ]
2022-08-31 12:07:43 +02:00
= > Allow to define the controller view .
2022-08-30 13:51:50 +02:00
2022-09-04 17:38:45 +02:00
* : view : - > The view that this controller manage and setup it .
2022-08-30 12:28:59 +02:00
"""
2022-08-30 21:12:04 +02:00
self . __view = view
2022-09-04 17:38:45 +02:00
view . set_window_title ( self . get_config ( ' app_name ' ) )
2022-08-31 12:07:43 +02:00
2022-09-01 18:05:58 +02:00
def on_open_folder ( self ) - > None :
"""
[ event function for view ]
= > Event launch when you ask to open the current folder .
"""
2022-09-05 23:13:38 +02:00
os . startfile ( self . get_config ( ' app_folder ' ) ) # Open the file explorer on working dir
2022-09-01 18:05:58 +02:00
def on_quite ( self ) - > None :
"""
[ event function for view ]
2022-09-04 15:28:28 +02:00
= > Event launch when you ask to quit the program . This event is propagated
to the subscribers , they can eventually cancel the event
2022-09-01 18:05:58 +02:00
"""
2022-09-01 22:07:14 +02:00
for callback in self . __quite_event_subscribers :
2022-09-04 15:28:28 +02:00
if callback ( ) :
return
2022-09-01 18:05:58 +02:00
self . __view . close_window ( ) # End the program
2022-08-31 12:07:43 +02:00
def on_check_for_update ( self ) - > None :
"""
[ event function for view ]
= > Event launched when a check for available updates is requested .
"""
2022-09-12 00:29:39 +02:00
try :
if check_for_update ( self . get_config ( ' app_version ' ) , self . get_config ( ' app_depo_version ' ) , self . get_config ( ' sys_version_matcher ' ) ) :
if self . show_question_dialog ( self . get_config ( ' app_name ' ) , " An update is available! Would you like to visit the release page to download the latest version? " ) :
webbrowser . open ( self . get_config ( ' app_depo_releases ' ) )
else : self . show_information_dialog ( self . get_config ( ' app_name ' ) , " No update available. " )
except Exception as e :
self . show_information_dialog ( self . get_config ( ' app_name ' ) , f " An error occured: { e } . " , ' error ' )
2022-09-01 18:05:58 +02:00
def on_about ( self ) - > None :
"""
[ event function for view ]
= > Event launched when a request for more information arise .
"""
2022-09-05 23:13:38 +02:00
self . change_frame ( Frames . INFO )
2022-08-31 12:07:43 +02:00
# END View methods
2022-08-30 12:28:59 +02:00
2022-08-30 21:12:04 +02:00
# START Controller methods
2022-09-05 23:13:38 +02:00
def change_frame ( self , frame : Frames ) - > None :
2022-08-30 12:28:59 +02:00
"""
2022-08-30 21:12:04 +02:00
[ function for controller ]
2022-08-31 12:07:43 +02:00
= > Allows you to request a frame change in the main window .
2022-08-30 13:51:50 +02:00
2022-09-04 17:38:45 +02:00
* : frame : - > The frame we want to display on the window instead of the current frame .
2022-08-30 12:28:59 +02:00
"""
2022-08-30 21:12:04 +02:00
self . __view . show_frame ( frame )
2022-09-01 18:05:58 +02:00
2022-09-12 00:29:39 +02:00
def get_config ( self , name : str ) :
2022-09-01 18:05:58 +02:00
"""
[ function for controller ]
= > Allows controllers to access the application ' s configuration.
2022-09-04 17:38:45 +02:00
* : name : - > The name of the configuration parameter for which we want to access the configured value .
2022-09-12 00:29:39 +02:00
* THROWABLE - > If the key is wrong , throw a ValueError .
2022-09-01 18:05:58 +02:00
"""
if self . __config . get ( name ) :
return self . __config . get ( name )
else :
raise ValueError ( " Unable to find a configuration with this name " )
2022-09-04 17:38:45 +02:00
def show_question_dialog ( self , title : str = ' title ' , message : str = ' question? ' , icon : str = ' question ' ) - > bool :
"""
[ function for controller ]
= > Ask a question to the user and block until he answers with yes or no .
2022-09-06 12:59:37 +02:00
* : title : - > Title of the dialogue .
* : message : - > Message of the dialogue .
* : icon : - > Icon of the dialogue
2022-09-04 17:38:45 +02:00
"""
return self . __view . show_question_dialog ( title , message , icon )
2022-09-06 12:59:37 +02:00
def show_information_dialog ( self , title : str = ' title ' , message : str = ' informations! ' , icon : str = ' info ' ) - > bool :
"""
[ function for controller ]
= > Display a pop - up information dialog to the user .
* : title : - > Title of the dialogue .
* : message : - > Message of the dialogue .
* : icon : - > Icon of the dialogue
"""
return self . __view . show_information_dialog ( title , message , icon )
2022-08-31 12:07:43 +02:00
# END Controller methods
2022-09-01 22:07:14 +02:00
# START Controller events
def subscribe_to_quite_event ( self , callback ) - > None :
2022-09-04 17:38:45 +02:00
"""
[ function for controller ]
= > Subscription function allowing to be warned if a request to quit occurs .
In the case where the callback function returns False the process continues
but if the callback returns True the process is aborted .
* : callback : - > Callback function that will be called when a request to exit occurs .
"""
2022-09-01 22:07:14 +02:00
self . __quite_event_subscribers . append ( callback )
# END Controller events