Fix: app crashes when no internet connection

This commit is contained in:
Vinícius Moreira
2019-07-11 18:04:12 -03:00
committed by GitHub
parent 8e930d57ea
commit b6bfecf4ef
8 changed files with 55 additions and 23 deletions

View File

@@ -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:

View File

@@ -0,0 +1,3 @@
class NoInternetException(Exception):
pass

View File

@@ -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 = []

View File

@@ -74,4 +74,5 @@ latest_version=latest version
description=description
type=type
installed=installed
others=others
others=others
internet.required=Internet connection is required

View File

@@ -75,4 +75,5 @@ latest_version=ultima versión
description=descripción
type=tipo
installed=instalado
others=otros
others=otros
internet.required=Se requiere conexión a internet

View File

@@ -75,4 +75,5 @@ latest_version=última versão
description=descrição
type=tipo
installed=instalado
others=outros
others=outros
internet.required=É necessário estar conectado a Internet

View File

@@ -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):

View File

@@ -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):