From 5111c4cbb85111aeb50743e0c889b90fb3e363ac Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Sun, 25 Jan 2015 16:15:20 +0100
Subject: [PATCH] Better handling of connection failures

---
 src/cutecoin/core/app.py         |  5 ++---
 src/cutecoin/gui/currency_tab.py | 12 +++++++++---
 src/cutecoin/gui/mainwindow.py   | 16 +++++++++++-----
 3 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/cutecoin/core/app.py b/src/cutecoin/core/app.py
index ae2c1c5e..d6d1f611 100644
--- a/src/cutecoin/core/app.py
+++ b/src/cutecoin/core/app.py
@@ -63,7 +63,6 @@ class Application(object):
         if self.current_account is not None:
             self.save_cache(self.current_account)
         self.current_account = account
-        self.load_cache(account)
 
     def load(self):
         if (os.path.exists(config.parameters['data'])
@@ -71,7 +70,6 @@ class Application(object):
             logging.debug("Loading data...")
             with open(config.parameters['data'], 'r') as json_data:
                 data = json.load(json_data)
-                json_data.close()
                 if 'default_account' in data.keys():
                     self.default_account = data['default_account']
                 for account_name in data['local_accounts']:
@@ -83,6 +81,7 @@ class Application(object):
         with open(account_path, 'r') as json_data:
             data = json.load(json_data)
             account = Account.load(data)
+            self.load_cache(account)
             self.accounts[account_name] = account
 
     def load_cache(self, account):
@@ -92,7 +91,7 @@ class Application(object):
             if os.path.exists(wallet_path):
                 with open(wallet_path, 'r') as json_data:
                     data = json.load(json_data)
-                    wallet.load_caches(data)
+                wallet.load_caches(data)
             for community in account.communities:
                 wallet.refresh_cache(community)
 
diff --git a/src/cutecoin/gui/currency_tab.py b/src/cutecoin/gui/currency_tab.py
index 0bd4a8d9..3c53eec4 100644
--- a/src/cutecoin/gui/currency_tab.py
+++ b/src/cutecoin/gui/currency_tab.py
@@ -49,11 +49,10 @@ class BlockchainWatcher(QObject):
             except NoPeerAvailable:
                 return
             except requests.exceptions.RequestException as e:
-                QMessageBox.critical(self, ":(",
-                            str(e),
-                            QMessageBox.Ok)
+                self.connection_error.emit(str(e))
 
     new_block_mined = pyqtSignal(int)
+    connection_error = pyqtSignal(str)
 
 
 class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
@@ -78,6 +77,7 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
         self.bc_watcher = BlockchainWatcher(self.app.current_account,
                                                 community)
         self.bc_watcher.new_block_mined.connect(self.refresh_block)
+        self.bc_watcher.connection_error.connect(self.display_error)
 
         self.watcher_thread = QThread()
         self.bc_watcher.moveToThread(self.watcher_thread)
@@ -107,6 +107,12 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
             self.status_label.setText("Connected : Block {0}"
                                              .format(block_number))
 
+    @pyqtSlot(str)
+    def display_error(self, error):
+        QMessageBox.critical(self, ":(",
+                    error,
+                    QMessageBox.Ok)
+
     @pyqtSlot(int)
     def refresh_block(self, block_number):
         if self.list_wallets.model():
diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py
index f205d4cd..66e90d29 100644
--- a/src/cutecoin/gui/mainwindow.py
+++ b/src/cutecoin/gui/mainwindow.py
@@ -28,6 +28,7 @@ class Loader(QObject):
         self.account_name = ""
 
     loaded = pyqtSignal()
+    connection_error = pyqtSignal(str)
 
     def set_account_name(self, name):
         self.account_name = name
@@ -38,9 +39,9 @@ class Loader(QObject):
             try:
                 self.app.change_current_account(self.app.get_account(self.account_name))
             except requests.exceptions.RequestException as e:
-                QMessageBox.critical(self, ":(",
-                            str(e),
-                            QMessageBox.Ok)
+                self.connection_error.emit(str(e))
+                self.loaded.emit()
+
         self.loaded.emit()
 
 
@@ -75,6 +76,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         self.loader.moveToThread(self.loader_thread)
         self.loader.loaded.connect(self.loader_finished)
         self.loader.loaded.connect(self.loader_thread.quit)
+        self.loader.connection_error.connect(self.display_error)
         self.loader_thread.started.connect(self.loader.load)
         self.setWindowTitle("CuteCoin {0}".format(__version__))
         self.refresh()
@@ -89,9 +91,13 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         self.refresh()
         self.busybar.hide()
 
+    @pyqtSlot(str)
+    def display_error(self, error):
+        QMessageBox.critical(self, ":(",
+                    error,
+                    QMessageBox.Ok)
+
     def action_change_account(self, account_name):
-        if self.app.current_account:
-            self.app.save_cache(self.app.current_account)
         self.busybar.show()
         self.status_label.setText("Loading account {0}".format(account_name))
         self.loader.set_account_name(account_name)
-- 
GitLab