From 5a9aeb78a2e549dfb1c4c020ff872fa451bf6d16 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Sat, 14 Mar 2015 15:26:19 +0100
Subject: [PATCH] Contacts are now json items

---
 src/cutecoin/core/account.py   | 17 ++++++-----------
 src/cutecoin/gui/contact.py    | 33 ++++++++++++++++++++++-----------
 src/cutecoin/gui/mainwindow.py | 23 +++++++++++------------
 src/cutecoin/gui/transfer.py   |  4 ++--
 4 files changed, 41 insertions(+), 36 deletions(-)

diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py
index 38622cff..de8f903a 100644
--- a/src/cutecoin/core/account.py
+++ b/src/cutecoin/core/account.py
@@ -115,7 +115,7 @@ class Account(QObject):
         contacts = []
 
         for contact_data in json_data['contacts']:
-            contacts.append(Person.from_json(contact_data))
+            contacts.append(contact_data)
 
         wallets = []
         for data in json_data['wallets']:
@@ -150,14 +150,13 @@ class Account(QObject):
         key = SigningKey(self.salt, password)
         return (key.pubkey == self.pubkey)
 
-#TODO: Contacts should be pure json, not Person objects
-    def add_contact(self, person):
+    def add_contact(self, new_contact):
         same_contact = [contact for contact in self.contacts
-                        if person.pubkey == contact.pubkey]
+                        if new_contact['pubkey'] == contact['pubkey']]
 
         if len(same_contact) > 0:
-            raise ContactAlreadyExists(person.uid, same_contact[0].uid)
-        self.contacts.append(person)
+            raise ContactAlreadyExists(new_contact['name'], same_contact[0]['name'])
+        self.contacts.append(new_contact)
 
     def add_community(self, community):
         '''
@@ -344,16 +343,12 @@ class Account(QObject):
         for w in self.wallets:
             data_wallets.append(w.jsonify())
 
-        data_contacts = []
-        for p in self.contacts:
-            data_contacts.append(p.jsonify())
-
         data = {'name': self.name,
                 'salt': self.salt,
                 'pubkey': self.pubkey,
                 'communities': data_communities,
                 'wallets': data_wallets,
-                'contacts': data_contacts}
+                'contacts': self.contacts}
         return data
 
     def get_person(self):
diff --git a/src/cutecoin/gui/contact.py b/src/cutecoin/gui/contact.py
index 1f2a419c..a753de20 100644
--- a/src/cutecoin/gui/contact.py
+++ b/src/cutecoin/gui/contact.py
@@ -4,6 +4,8 @@ Created on 2 févr. 2014
 @author: inso
 '''
 import re
+import logging
+
 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QMessageBox
 from ..core.person import Person
 from ..tools.exceptions import ContactAlreadyExists
@@ -16,7 +18,7 @@ class ConfigureContactDialog(QDialog, Ui_ConfigureContactDialog):
     classdocs
     '''
 
-    def __init__(self, account, parent=None, contact=None, edit=False):
+    def __init__(self, account, parent=None, contact=None, index_edit=None):
         '''
         Constructor
         '''
@@ -24,27 +26,36 @@ class ConfigureContactDialog(QDialog, Ui_ConfigureContactDialog):
         self.setupUi(self)
         self.account = account
         self.main_window = parent
-        self.contact = contact
-        if contact:
-            self.edit_name.setText(contact.name)
-            self.edit_pubkey.setText(contact.pubkey)
-        if edit:
+        self.index_edit = index_edit
+        if type(contact) is Person:
+            self.contact = {'name': contact.name,
+                            'pubkey': contact.pubkey}
+        elif type(contact) is dict:
+            self.contact = contact
+
+        if index_edit is not None:
+            self.contact = account.contacts[index_edit]
             self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
 
+        if self.contact:
+            self.edit_name.setText(self.contact['name'])
+            self.edit_pubkey.setText(self.contact['pubkey'])
+
     def accept(self):
         name = self.edit_name.text()
         pubkey = self.edit_pubkey.text()
-        if self.contact:
-            self.contact.name = name
-            self.contact.pubkey = pubkey
+        if self.index_edit is not None:
+            self.account.contacts[self.index_edit] = {'name': name,
+                          'pubkey': pubkey}
+            logging.debug(self.contact)
         else:
             try:
-                self.account.add_contact(Person.from_metadata(name, pubkey))
+                self.account.add_contact({'name': name,
+                                          'pubkey': pubkey})
             except ContactAlreadyExists as e:
                 QMessageBox.critical(self, "Contact already exists",
                             str(e),
                             QMessageBox.Ok)
-                return
         self.main_window.app.save(self.account)
         super().accept()
 
diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py
index f2c76aa6..c267bbd3 100644
--- a/src/cutecoin/gui/mainwindow.py
+++ b/src/cutecoin/gui/mainwindow.py
@@ -156,8 +156,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
 
     @pyqtSlot()
     def edit_contact(self):
-        contact = self.sender().data()
-        dialog = ConfigureContactDialog(self.app.current_account, self, contact, True)
+        index = self.sender().data()
+        dialog = ConfigureContactDialog(self.app.current_account, self, None, index)
         result = dialog.exec_()
         if result == QDialog.Accepted:
             self.window().refresh_contacts()
@@ -272,11 +272,11 @@ class MainWindow(QMainWindow, Ui_MainWindow):
     def refresh_contacts(self):
         self.menu_contacts_list.clear()
         if self.app.current_account:
-            for contact in self.app.current_account.contacts:
-                contact_menu = self.menu_contacts_list.addMenu(contact.name)
+            for index, contact in enumerate(self.app.current_account.contacts):
+                contact_menu = self.menu_contacts_list.addMenu(contact['name'])
                 edit_action = contact_menu.addAction("Edit")
                 edit_action.triggered.connect(self.edit_contact)
-                edit_action.setData(contact)
+                edit_action.setData(index)
                 delete_action = contact_menu.addAction("Delete")
                 delete_action.setData(contact)
                 delete_action.triggered.connect(self.delete_contact)
@@ -287,14 +287,13 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         self.app.save(self.app.current_account)
         self.action_set_as_default.setEnabled(False)
 
-    '''
-    Refresh main window
-    When the selected account changes, all the widgets
-    in the window have to be refreshed
-    '''
-
     def refresh(self):
-        logging.debug("Refresh finished")
+        '''
+        Refresh main window
+        When the selected account changes, all the widgets
+        in the window have to be refreshed
+        '''
+        logging.debug("Refresh started")
         self.menu_change_account.clear()
         signal_mapper = QSignalMapper(self)
 
diff --git a/src/cutecoin/gui/transfer.py b/src/cutecoin/gui/transfer.py
index 6546f9ab..9b21affc 100644
--- a/src/cutecoin/gui/transfer.py
+++ b/src/cutecoin/gui/transfer.py
@@ -44,7 +44,7 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
             self.combo_wallets.addItem(wallet.name)
 
         for contact in sender.contacts:
-            self.combo_contact.addItem(contact.name)
+            self.combo_contact.addItem(contact['name'])
 
         if len(self.account.contacts) == 0:
             self.combo_contact.setEnabled(False)
@@ -56,7 +56,7 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
 
         if self.radio_contact.isChecked():
             index = self.combo_contact.currentIndex()
-            recipient = self.account.contacts[index].pubkey
+            recipient = self.account.contacts[index]['pubkey']
         else:
             recipient = self.edit_pubkey.text()
         amount = self.spinbox_amount.value()
-- 
GitLab