From b6bfecf4ef6299407a5f7a2c3d7e41f52738443b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Moreira?= Date: Thu, 11 Jul 2019 18:04:12 -0300 Subject: [PATCH] Fix: app crashes when no internet connection --- fpakman/core/controller.py | 2 ++ fpakman/core/exception.py | 3 +++ fpakman/core/flatpak.py | 10 ++++++++- fpakman/resources/locale/en | 3 ++- fpakman/resources/locale/es | 3 ++- fpakman/resources/locale/pt | 3 ++- fpakman/view/qt/thread.py | 43 +++++++++++++++++++++++-------------- fpakman/view/qt/window.py | 11 +++++++--- 8 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 fpakman/core/exception.py diff --git a/fpakman/core/controller.py b/fpakman/core/controller.py index 13f42cb7..1b1c4029 100755 --- a/fpakman/core/controller.py +++ b/fpakman/core/controller.py @@ -67,6 +67,7 @@ class ApplicationManager(ABC): def cache_to_disk(self, app: Application, icon_bytes: bytes, only_icon: bool): pass + from fpakman.core.worker import FlatpakAsyncDataLoaderManager @@ -173,6 +174,7 @@ class FlatpakManager(ApplicationManager): def downgrade_app(self, app: FlatpakApplication, root_password: str): commits = flatpak.get_app_commits(app.ref, app.origin) + commit_idx = commits.index(app.commit) # downgrade is not possible if the app current commit in the first one: diff --git a/fpakman/core/exception.py b/fpakman/core/exception.py new file mode 100644 index 00000000..8d00ac1a --- /dev/null +++ b/fpakman/core/exception.py @@ -0,0 +1,3 @@ + +class NoInternetException(Exception): + pass diff --git a/fpakman/core/flatpak.py b/fpakman/core/flatpak.py index 768c4a55..256c2ebc 100755 --- a/fpakman/core/flatpak.py +++ b/fpakman/core/flatpak.py @@ -3,6 +3,7 @@ import subprocess from typing import List from fpakman.core import system +from fpakman.core.exception import NoInternetException from fpakman.core.model import Application BASE_CMD = 'flatpak' @@ -128,12 +129,19 @@ def downgrade_and_stream(app_ref: str, commit: str, root_password: str): def get_app_commits(app_ref: str, origin: str) -> List[str]: log = system.run_cmd('{} remote-info --log {} {}'.format(BASE_CMD, origin, app_ref)) - return re.findall(r'Commit+:\s(.+)', log) + + if log: + return re.findall(r'Commit+:\s(.+)', log) + else: + raise NoInternetException() def get_app_commits_data(app_ref: str, origin: str) -> List[dict]: log = system.run_cmd('{} remote-info --log {} {}'.format(BASE_CMD, origin, app_ref)) + if not log: + raise NoInternetException() + res = re.findall(r'(Commit|Subject|Date):\s(.+)', log) commits = [] diff --git a/fpakman/resources/locale/en b/fpakman/resources/locale/en index 9198e09f..a486a107 100644 --- a/fpakman/resources/locale/en +++ b/fpakman/resources/locale/en @@ -74,4 +74,5 @@ latest_version=latest version description=description type=type installed=installed -others=others \ No newline at end of file +others=others +internet.required=Internet connection is required \ No newline at end of file diff --git a/fpakman/resources/locale/es b/fpakman/resources/locale/es index e022cc90..fab85ca0 100644 --- a/fpakman/resources/locale/es +++ b/fpakman/resources/locale/es @@ -75,4 +75,5 @@ latest_version=ultima versión description=descripción type=tipo installed=instalado -others=otros \ No newline at end of file +others=otros +internet.required=Se requiere conexión a internet \ No newline at end of file diff --git a/fpakman/resources/locale/pt b/fpakman/resources/locale/pt index a0e68b75..8fdbd388 100644 --- a/fpakman/resources/locale/pt +++ b/fpakman/resources/locale/pt @@ -75,4 +75,5 @@ latest_version=última versão description=descrição type=tipo installed=instalado -others=outros \ No newline at end of file +others=outros +internet.required=É necessário estar conectado a Internet \ No newline at end of file diff --git a/fpakman/view/qt/thread.py b/fpakman/view/qt/thread.py index 61eb7167..a54daa6b 100644 --- a/fpakman/view/qt/thread.py +++ b/fpakman/view/qt/thread.py @@ -2,9 +2,11 @@ import time from datetime import datetime, timedelta from typing import List +import requests from PyQt5.QtCore import QThread, pyqtSignal from fpakman.core.controller import ApplicationManager +from fpakman.core.exception import NoInternetException from fpakman.core.model import ApplicationStatus from fpakman.util.cache import Cache from fpakman.view.qt import dialog @@ -92,20 +94,23 @@ class DowngradeApp(QThread): def run(self): if self.app: - stream = self.manager.downgrade_app(self.app.model, self.root_password) + try: + stream = self.manager.downgrade_app(self.app.model, self.root_password) - if stream is None: - dialog.show_error(title=self.locale_keys['popup.downgrade.impossible.title'], - body=self.locale_keys['popup.downgrade.impossible.body']) - else: - for output in stream: - line = output.decode().strip() - if line: - self.signal_output.emit(line) - - self.app = None - self.root_password = None - self.signal_finished.emit() + if stream is None: + dialog.show_error(title=self.locale_keys['popup.downgrade.impossible.title'], + body=self.locale_keys['popup.downgrade.impossible.body']) + else: + for output in stream: + line = output.decode().strip() + if line: + self.signal_output.emit(line) + except (requests.exceptions.ConnectionError, NoInternetException): + self.signal_output.emit(self.locale_keys['internet.required']) + finally: + self.app = None + self.root_password = None + self.signal_finished.emit() class GetAppInfo(QThread): @@ -125,15 +130,21 @@ class GetAppInfo(QThread): class GetAppHistory(QThread): signal_finished = pyqtSignal(dict) - def __init__(self, manager: ApplicationManager, app: ApplicationView = None): + def __init__(self, manager: ApplicationManager, locale_keys: dict, app: ApplicationView = None): super(GetAppHistory, self).__init__() self.app = app self.manager = manager + self.locale_keys = locale_keys def run(self): if self.app: - self.signal_finished.emit({'model': self.app.model, 'history': self.manager.get_history(self.app.model)}) - self.app = None + try: + res = {'model': self.app.model, 'history': self.manager.get_history(self.app.model)} + self.signal_finished.emit(res) + except (requests.exceptions.ConnectionError, NoInternetException): + self.signal_finished.emit({'error': self.locale_keys['internet.required']}) + finally: + self.app = None class SearchApps(QThread): diff --git a/fpakman/view/qt/window.py b/fpakman/view/qt/window.py index 765d756e..9b416a01 100755 --- a/fpakman/view/qt/window.py +++ b/fpakman/view/qt/window.py @@ -145,7 +145,7 @@ class ManageWindow(QWidget): self.thread_get_info = GetAppInfo(self.manager) self.thread_get_info.signal_finished.connect(self._finish_get_info) - self.thread_get_history = GetAppHistory(self.manager) + self.thread_get_history = GetAppHistory(self.manager, self.locale_keys) self.thread_get_history.signal_finished.connect(self._finish_get_history) self.thread_search = SearchApps(self.manager) @@ -445,8 +445,13 @@ class ManageWindow(QWidget): self._release_lock() self.finish_action() self.change_update_state() - dialog_history = HistoryDialog(app, self.table_apps.get_selected_app_icon(), self.locale_keys) - dialog_history.exec_() + + if app.get('error'): + self._handle_console_option(True) + self.textarea_output.appendPlainText(app['error']) + else: + dialog_history = HistoryDialog(app, self.table_apps.get_selected_app_icon(), self.locale_keys) + dialog_history.exec_() def search(self):