from controller.MainController import MainController from util.AsyncTask import AsyncTask class HomeController: """ Controller - HomeController desc... @author Jérémi Nihart / EndMove @link https://git.endmove.eu/EndMove/WebPicDownloader @version 1.0.0 @since 2022-08-30 """ # Variables __main_controller = None __view = None __webpic = None __download_task = None # Constructor def __init__(self, controller: MainController, webpic) -> None: """ Constructor * :controller: -> The main application cpntroller. * :webpic: -> The webpicdownloader instance. """ # Setub variables self.__main_controller = controller self.__webpic = webpic # Subscribe to events controller.subscribe_to_quite_event(self.on_quit) # 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. """ self.__view = view self.__webpic.set_messenger_callback(view.add_log) # END View method # START View events def on_change_view(self, frame) -> None: """ [event function for view] => Call this event method when the user requests to change the window. * :frame: -> The frame we want to launch. """ self.__main_controller.change_frame(frame) def on_download_requested(self, url: str, name: str) -> None: """ [event function for view] => Call this event method when the user requests to download * :url: -> The url of the website to use for pic-download. * :name: -> The name of the folder in which put pictures. """ 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() else: self.__view.show_error_message("Opss, the url or folder name are not valid!") # END View events # START Controller methods def on_quit(self) -> bool: """ [event function for controller] => Call this event when a request to exit is thrown. """ 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 # 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. """ print("start callback called") # REMOVE 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. """ print("stop callback called") # REMOVE self.__webpic.stop() # END Task methods