2022-08-30 12:28:59 +02:00
|
|
|
from controller.MainController import MainController
|
2022-09-01 11:47:34 +02:00
|
|
|
from util.AsyncTask import AsyncTask
|
2022-08-30 12:28:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HomeController:
|
|
|
|
"""
|
2022-08-30 21:12:04 +02:00
|
|
|
Controller - HomeController
|
2022-08-30 12:28:59 +02:00
|
|
|
|
|
|
|
desc...
|
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
|
2022-08-30 12:28:59 +02:00
|
|
|
"""
|
|
|
|
# Variables
|
2022-08-30 21:12:04 +02:00
|
|
|
__main_controller = None
|
|
|
|
__view = None
|
2022-08-31 21:05:20 +02:00
|
|
|
__webpic = None
|
2022-09-04 15:28:28 +02:00
|
|
|
__download_task = None
|
2022-08-30 12:28:59 +02:00
|
|
|
|
|
|
|
# Constructor
|
2022-08-31 21:05:20 +02:00
|
|
|
def __init__(self, controller: MainController, webpic) -> None:
|
2022-09-01 11:47:34 +02:00
|
|
|
"""
|
|
|
|
Constructor
|
|
|
|
|
2022-09-04 15:28:28 +02:00
|
|
|
* :controller: -> The main application cpntroller.
|
|
|
|
* :webpic: -> The webpicdownloader instance.
|
2022-09-01 11:47:34 +02:00
|
|
|
"""
|
2022-09-01 22:07:14 +02:00
|
|
|
# Setub variables
|
2022-08-30 21:12:04 +02:00
|
|
|
self.__main_controller = controller
|
2022-08-31 21:05:20 +02:00
|
|
|
self.__webpic = webpic
|
2022-08-30 12:28:59 +02:00
|
|
|
|
2022-09-01 22:07:14 +02:00
|
|
|
# Subscribe to events
|
|
|
|
controller.subscribe_to_quite_event(self.on_quit)
|
|
|
|
|
2022-08-30 21:12:04 +02:00
|
|
|
# START View methods
|
2022-08-30 12:28:59 +02:00
|
|
|
def set_view(self, view) -> None:
|
|
|
|
"""
|
|
|
|
[function for view]
|
2022-09-01 11:47:34 +02:00
|
|
|
=> Define the view of this controller.
|
2022-08-30 12:28:59 +02:00
|
|
|
|
2022-09-04 15:28:28 +02:00
|
|
|
* :view: -> The view that this controller manage.
|
2022-08-30 12:28:59 +02:00
|
|
|
"""
|
2022-08-30 21:12:04 +02:00
|
|
|
self.__view = view
|
2022-08-31 23:53:24 +02:00
|
|
|
self.__webpic.set_messenger_callback(view.add_log)
|
2022-08-30 21:12:04 +02:00
|
|
|
# END View method
|
|
|
|
|
|
|
|
# START View events
|
|
|
|
def on_change_view(self, frame) -> None:
|
|
|
|
"""
|
|
|
|
[event function for view]
|
2022-09-01 11:47:34 +02:00
|
|
|
=> Call this event method when the user requests to change the window.
|
2022-08-30 21:12:04 +02:00
|
|
|
|
2022-09-04 15:28:28 +02:00
|
|
|
* :frame: -> The frame we want to launch.
|
2022-08-30 21:12:04 +02:00
|
|
|
"""
|
2022-09-01 22:07:14 +02:00
|
|
|
self.__main_controller.change_frame(frame)
|
2022-08-31 21:05:20 +02:00
|
|
|
|
|
|
|
def on_download_requested(self, url: str, name: str) -> None:
|
2022-08-30 21:12:04 +02:00
|
|
|
"""
|
|
|
|
[event function for view]
|
2022-09-01 11:47:34 +02:00
|
|
|
=> Call this event method when the user requests to download
|
2022-08-30 21:12:04 +02:00
|
|
|
|
2022-09-04 15:28:28 +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
|
|
|
"""
|
2022-09-04 15:28:28 +02:00
|
|
|
if url.strip() and name.strip():
|
|
|
|
self.__download_task = AsyncTask(
|
|
|
|
rcallback=self.__async_task_start,
|
|
|
|
rargs=(url, name),
|
|
|
|
qcallback=self.__async_task_stop
|
|
|
|
)
|
|
|
|
self.__download_task.start()
|
2022-08-31 21:05:20 +02:00
|
|
|
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 Controller methods
|
2022-09-04 17:38:45 +02:00
|
|
|
def on_quit(self) -> bool:
|
2022-09-01 22:07:14 +02:00
|
|
|
"""
|
|
|
|
[event function for controller]
|
|
|
|
=> Call this event when a request to exit is thrown.
|
|
|
|
"""
|
2022-09-04 17:38:45 +02:00
|
|
|
if self.__download_task and self.__download_task.is_alive():
|
|
|
|
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.__download_task.stop()
|
|
|
|
self.__download_task.join()
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
print("Quit... homecontroller END") # REMOVE
|
2022-09-04 15:28:28 +02:00
|
|
|
# END Controller methods
|
|
|
|
|
|
|
|
# START Task methods
|
|
|
|
def __async_task_start(self, url, name) -> None:
|
|
|
|
"""
|
|
|
|
[CallBack start function]
|
|
|
|
=> Start Callback function for asynctask, be careful once executed in asynctask this
|
|
|
|
function will keep its controller context. In short it's as if the thread was
|
|
|
|
launched in the controller and the execution never left it.
|
|
|
|
|
|
|
|
* :url: -> Url for webpic.
|
|
|
|
* :name: -> Working dir name for webpic.
|
|
|
|
"""
|
2022-09-04 17:38:45 +02:00
|
|
|
print("start callback called") # REMOVE
|
2022-09-04 15:28:28 +02:00
|
|
|
self.__view.clear_logs()
|
|
|
|
if self.__webpic.download(url, name):
|
|
|
|
self.__view.show_success_message("The download has been successfully completed.")
|
|
|
|
else:
|
|
|
|
self.__view.show_error_message("A critical error preventing the download occurred, check the logs.")
|
|
|
|
|
|
|
|
def __async_task_stop(self) -> None:
|
|
|
|
"""
|
|
|
|
[CallBack stop function]
|
|
|
|
=> End Callback function for asynctask, be careful once executed in asynctask this
|
|
|
|
function will keep its controller context. In short it's as if the thread was
|
|
|
|
launched in the controller and the execution never left it.
|
|
|
|
"""
|
2022-09-04 17:38:45 +02:00
|
|
|
print("stop callback called") # REMOVE
|
2022-09-04 15:28:28 +02:00
|
|
|
self.__webpic.stop()
|
|
|
|
# END Task methods
|