From 6f9caa0605fe1ad4a648aac1d799f3f09f567993 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Sun, 25 Jan 2015 20:20:32 +0100
Subject: [PATCH] Better handling of unjoinable communities

---
 src/cutecoin/core/account.py   |  9 ++++++---
 src/cutecoin/core/community.py | 36 ++++++++++++++++++++++++++--------
 src/cutecoin/gui/mainwindow.py |  5 -----
 3 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py
index 90e20cfe..85f70ab2 100644
--- a/src/cutecoin/core/account.py
+++ b/src/cutecoin/core/account.py
@@ -69,9 +69,11 @@ class Account(object):
         dead_communities = []
         for data in json_data['communities']:
             try:
-                communities.append(Community.load(data))
+                community = Community.load(data)
+                communities.append(community)
             except NoPeerAvailable:
-                dead_communities.append(data['currency'])
+                community = Community.without_network(data)
+                dead_communities.append(community)
 
         account = cls(salt, pubkey, name, communities, wallets,
                       contacts, dead_communities)
@@ -210,7 +212,8 @@ class Account(object):
 
     def jsonify(self):
         data_communities = []
-        for c in self.communities:
+        communities = self.communities + self.dead_communities
+        for c in communities:
             data_communities.append(c.jsonify())
 
         data_wallets = []
diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py
index dda64837..9f83796a 100644
--- a/src/cutecoin/core/community.py
+++ b/src/cutecoin/core/community.py
@@ -59,18 +59,16 @@ class Community(object):
         self.peers = [p for p in peers if p.currency == currency]
         self._cache = Cache(self)
 
-        # After initializing the community from latest peers,
-        # we refresh its peers tree
-        logging.debug("Creating community")
-        self.peers = self.peering()
-        logging.debug("{0} peers found".format(len(self.peers)))
-        logging.debug([peer.pubkey for peer in peers])
-
         self._cache.refresh()
 
     @classmethod
     def create(cls, currency, peer):
-        return cls(currency, [peer])
+        community = cls(currency, [peer])
+        logging.debug("Creating community")
+        community.peers = community.peering()
+        logging.debug("{0} peers found".format(len(community.peers)))
+        logging.debug([peer.pubkey for peer in community.peers])
+        return community
 
     @classmethod
     def load(cls, json_data):
@@ -92,6 +90,28 @@ class Community(object):
                     except:
                         pass
 
+        community = cls(currency, peers)
+        logging.debug("Creating community")
+        community.peers = community.peering()
+        logging.debug("{0} peers found".format(len(community.peers)))
+        logging.debug([peer.pubkey for peer in community.peers])
+        return community
+
+    @classmethod
+    def without_network(cls, json_data):
+        peers = []
+
+        currency = json_data['currency']
+
+        for data in json_data['peers']:
+            endpoints = []
+            for e in data['endpoints']:
+                endpoints.append(Endpoint.from_inline(e))
+            peer = Peer(PROTOCOL_VERSION, currency, data['pubkey'],
+                        "0-DA39A3EE5E6B4B0D3255BFEF95601890AFD80709",
+                        endpoints, None)
+            peers.append(peer)
+
         community = cls(currency, peers)
         return community
 
diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py
index 66e90d29..675c4c4c 100644
--- a/src/cutecoin/gui/mainwindow.py
+++ b/src/cutecoin/gui/mainwindow.py
@@ -172,11 +172,6 @@ class MainWindow(QMainWindow, Ui_MainWindow):
             self.action_configure_parameters.setEnabled(False)
             self.action_set_as_default.setEnabled(False)
         else:
-            for dead in self.app.current_account.dead_communities:
-                QMessageBox.critical(self, ":(",
-                            "No {0} peers could be joined. Connection to its network is lost.".format(dead),
-                            QMessageBox.Ok)
-
             self.action_set_as_default.setEnabled(self.app.current_account.name
                                                   != self.app.default_account)
             self.password_asker = PasswordAskerDialog(self.app.current_account)
-- 
GitLab