This repository has been archived on 2023-11-29. You can view files and clone it, but cannot push or open issues or pull requests.
WebPicDownloader/webpicdownloader/controller/MainController.py

143 lines
5.2 KiB
Python

import os
import webbrowser
from webpicdownloader.controller.Frames import Frames
from webpicdownloader.util.UpdateUtils import check_for_update
class MainController:
"""
Controller - MainController
This controller manages all the main interaction, change of windows,
dialogs, stop... It is the main controller.
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.1
@since 2022-09-11
"""
# Variables
__config: dict = None
__view = None
__quite_event_subscribers: list = None
# Constructor
def __init__(self, config: dict) -> None:
"""
Constructor
* :config: -> The application configuration (a dictionary).
"""
# Setup variables
self.__config = config
self.__quite_event_subscribers = []
# START View methods
def set_view(self, view) -> None:
"""
[function for view]
=> Allow to define the controller view.
* :view: -> The view that this controller manage and setup it.
"""
self.__view = view
view.set_window_title(self.get_config('app_name'))
def on_open_folder(self) -> None:
"""
[event function for view]
=> Event launch when you ask to open the current folder.
"""
os.startfile(self.get_config('app_folder')) # Open the file explorer on working dir
def on_quite(self) -> None:
"""
[event function for view]
=> Event launch when you ask to quit the program. This event is propagated
to the subscribers, they can eventually cancel the event
"""
for callback in self.__quite_event_subscribers:
if callback():
return
self.__view.close_window() # End the program
def on_check_for_update(self) -> None:
"""
[event function for view]
=> Event launched when a check for available updates is requested.
"""
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')
def on_about(self) -> None:
"""
[event function for view]
=> Event launched when a request for more information arise.
"""
self.change_frame(Frames.INFO)
# END View methods
# START Controller methods
def change_frame(self, frame: Frames) -> None:
"""
[function for controller]
=> Allows you to request a frame change in the main window.
* :frame: -> The frame we want to display on the window instead of the current frame.
"""
self.__view.show_frame(frame)
def get_config(self, name: str):
"""
[function for controller]
=> Allows controllers to access the application's configuration.
* :name: -> The name of the configuration parameter for which we want to access the configured value.
* THROWABLE -> If the key is wrong, throw a ValueError.
"""
if self.__config.get(name):
return self.__config.get(name)
else:
raise ValueError("Unable to find a configuration with this name")
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.
* :title: -> Title of the dialogue.
* :message: -> Message of the dialogue.
* :icon: -> Icon of the dialogue
"""
return self.__view.show_question_dialog(title, message, icon)
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)
# END Controller methods
# START Controller events
def subscribe_to_quite_event(self, callback) -> None:
"""
[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.
"""
self.__quite_event_subscribers.append(callback)
# END Controller events