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/HomeController.py

119 lines
4.1 KiB
Python
Raw Permalink Normal View History

2022-09-11 11:52:41 +02:00
from webpicdownloader.controller.MainController import MainController
from webpicdownloader.model.WebPicDownloader import MessageType, WebPicDownloader
class HomeController:
"""
2022-08-30 21:12:04 +02:00
Controller - HomeController
This controller handles all the interaction directly related to the download.
2022-08-30 21:12:04 +02:00
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-08-30
"""
# Variables
__main_controller: MainController = None
2022-08-30 21:12:04 +02:00
__view = None
__webpic: WebPicDownloader = None
# Constructor
def __init__(self, controller: MainController, webpic: WebPicDownloader) -> None:
"""
Constructor
* :controller: -> The main application cpntroller.
* :webpic: -> The webpicdownloader instance.
"""
# Setub variables
2022-08-30 21:12:04 +02:00
self.__main_controller = controller
self.__webpic = webpic
# setup webpic event
webpic.set_messenger_callback(self.on_webpic_messenger)
webpic.set_success_callback(self.on_webpic_success)
webpic.set_failure_callback(self.on_webpic_failure)
# Subscribe to events
controller.subscribe_to_quite_event(self.on_quit)
2022-08-30 21:12:04 +02:00
# START View methods
def set_view(self, view) -> None:
"""
[function for view]
=> Define the view of this controller.
* :view: -> The view that this controller manage.
"""
2022-08-30 21:12:04 +02:00
self.__view = view
# END View method
# START View events
def on_download_requested(self, url: str, name: str) -> None:
2022-08-30 21:12:04 +02:00
"""
[event function for view]
=> Call this event method when the user requests to download
2022-08-30 21:12:04 +02:00
* :url: -> The url of the website to use for pic-download.
* :name: -> The name of the folder in which put pictures.
2022-08-30 21:12:04 +02:00
"""
if url.strip() and name.strip():
self.__view.set_interface_state(True)
self.__view.clear_logs()
self.__webpic.start_downloading(url, name)
else:
self.__view.show_error_message("Opss, the url or folder name are not valid!")
2022-08-31 12:07:43 +02:00
# END View events
2022-08-31 23:53:24 +02:00
# START Webpic events
def on_webpic_messenger(self, message: str, type) -> None:
"""
[event function for webpic]
=> This event is called to communicate a message.
* :message: -> Message that webpic send to the controller.
* :type: -> Type of message that webpic send to the controller.
"""
match type:
case MessageType.LOG:
self.__view.add_log(message)
case MessageType.ERROR:
self.__view.show_error_message(message)
case MessageType.SUCCESS:
self.__view.show_success_message(message)
def on_webpic_success(self) -> None:
"""
[event function for webpic]
=> This event is called to indicate that the download has finished successfully.
"""
self.__view.show_success_message("The download has been successfully completed.")
self.__view.set_interface_state(False)
def on_webpic_failure(self) -> None:
"""
[event function for webpic]
=> This event is called to indicate that there was a problem during the download.
"""
self.__view.show_error_message("A critical error preventing the download occurred, check the logs.")
self.__view.set_interface_state(False)
# END Webpic events
2022-08-31 23:53:24 +02:00
# START Controller methods
2022-09-04 17:38:45 +02:00
def on_quit(self) -> bool:
"""
[event function for controller]
=> Call this event when a request to exit is thrown.
"""
if self.__webpic.is_download_running():
2022-09-04 17:38:45 +02:00
if self.__main_controller.show_question_dialog(
"Are you sure?",
"Do you really want to quit while the download is running?\nThis will stop the download."
):
self.__webpic.stop_downloading() # hot stop deamon
2022-09-04 17:38:45 +02:00
return False
return True
self.__webpic.stop_downloading(block=True)
# END Controller methods