Skip to content
Snippets Groups Projects
Commit 5a9aeb78 authored by inso's avatar inso
Browse files

Contacts are now json items

parent 99c64a83
Branches
Tags
No related merge requests found
...@@ -115,7 +115,7 @@ class Account(QObject): ...@@ -115,7 +115,7 @@ class Account(QObject):
contacts = [] contacts = []
for contact_data in json_data['contacts']: for contact_data in json_data['contacts']:
contacts.append(Person.from_json(contact_data)) contacts.append(contact_data)
wallets = [] wallets = []
for data in json_data['wallets']: for data in json_data['wallets']:
...@@ -150,14 +150,13 @@ class Account(QObject): ...@@ -150,14 +150,13 @@ class Account(QObject):
key = SigningKey(self.salt, password) key = SigningKey(self.salt, password)
return (key.pubkey == self.pubkey) return (key.pubkey == self.pubkey)
#TODO: Contacts should be pure json, not Person objects def add_contact(self, new_contact):
def add_contact(self, person):
same_contact = [contact for contact in self.contacts same_contact = [contact for contact in self.contacts
if person.pubkey == contact.pubkey] if new_contact['pubkey'] == contact['pubkey']]
if len(same_contact) > 0: if len(same_contact) > 0:
raise ContactAlreadyExists(person.uid, same_contact[0].uid) raise ContactAlreadyExists(new_contact['name'], same_contact[0]['name'])
self.contacts.append(person) self.contacts.append(new_contact)
def add_community(self, community): def add_community(self, community):
''' '''
...@@ -344,16 +343,12 @@ class Account(QObject): ...@@ -344,16 +343,12 @@ class Account(QObject):
for w in self.wallets: for w in self.wallets:
data_wallets.append(w.jsonify()) data_wallets.append(w.jsonify())
data_contacts = []
for p in self.contacts:
data_contacts.append(p.jsonify())
data = {'name': self.name, data = {'name': self.name,
'salt': self.salt, 'salt': self.salt,
'pubkey': self.pubkey, 'pubkey': self.pubkey,
'communities': data_communities, 'communities': data_communities,
'wallets': data_wallets, 'wallets': data_wallets,
'contacts': data_contacts} 'contacts': self.contacts}
return data return data
def get_person(self): def get_person(self):
......
...@@ -4,6 +4,8 @@ Created on 2 févr. 2014 ...@@ -4,6 +4,8 @@ Created on 2 févr. 2014
@author: inso @author: inso
''' '''
import re import re
import logging
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QMessageBox from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QMessageBox
from ..core.person import Person from ..core.person import Person
from ..tools.exceptions import ContactAlreadyExists from ..tools.exceptions import ContactAlreadyExists
...@@ -16,7 +18,7 @@ class ConfigureContactDialog(QDialog, Ui_ConfigureContactDialog): ...@@ -16,7 +18,7 @@ class ConfigureContactDialog(QDialog, Ui_ConfigureContactDialog):
classdocs classdocs
''' '''
def __init__(self, account, parent=None, contact=None, edit=False): def __init__(self, account, parent=None, contact=None, index_edit=None):
''' '''
Constructor Constructor
''' '''
...@@ -24,27 +26,36 @@ class ConfigureContactDialog(QDialog, Ui_ConfigureContactDialog): ...@@ -24,27 +26,36 @@ class ConfigureContactDialog(QDialog, Ui_ConfigureContactDialog):
self.setupUi(self) self.setupUi(self)
self.account = account self.account = account
self.main_window = parent self.main_window = parent
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 self.contact = contact
if contact:
self.edit_name.setText(contact.name) if index_edit is not None:
self.edit_pubkey.setText(contact.pubkey) self.contact = account.contacts[index_edit]
if edit:
self.button_box.button(QDialogButtonBox.Ok).setEnabled(False) 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): def accept(self):
name = self.edit_name.text() name = self.edit_name.text()
pubkey = self.edit_pubkey.text() pubkey = self.edit_pubkey.text()
if self.contact: if self.index_edit is not None:
self.contact.name = name self.account.contacts[self.index_edit] = {'name': name,
self.contact.pubkey = pubkey 'pubkey': pubkey}
logging.debug(self.contact)
else: else:
try: try:
self.account.add_contact(Person.from_metadata(name, pubkey)) self.account.add_contact({'name': name,
'pubkey': pubkey})
except ContactAlreadyExists as e: except ContactAlreadyExists as e:
QMessageBox.critical(self, "Contact already exists", QMessageBox.critical(self, "Contact already exists",
str(e), str(e),
QMessageBox.Ok) QMessageBox.Ok)
return
self.main_window.app.save(self.account) self.main_window.app.save(self.account)
super().accept() super().accept()
......
...@@ -156,8 +156,8 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -156,8 +156,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
@pyqtSlot() @pyqtSlot()
def edit_contact(self): def edit_contact(self):
contact = self.sender().data() index = self.sender().data()
dialog = ConfigureContactDialog(self.app.current_account, self, contact, True) dialog = ConfigureContactDialog(self.app.current_account, self, None, index)
result = dialog.exec_() result = dialog.exec_()
if result == QDialog.Accepted: if result == QDialog.Accepted:
self.window().refresh_contacts() self.window().refresh_contacts()
...@@ -272,11 +272,11 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -272,11 +272,11 @@ class MainWindow(QMainWindow, Ui_MainWindow):
def refresh_contacts(self): def refresh_contacts(self):
self.menu_contacts_list.clear() self.menu_contacts_list.clear()
if self.app.current_account: if self.app.current_account:
for contact in self.app.current_account.contacts: for index, contact in enumerate(self.app.current_account.contacts):
contact_menu = self.menu_contacts_list.addMenu(contact.name) contact_menu = self.menu_contacts_list.addMenu(contact['name'])
edit_action = contact_menu.addAction("Edit") edit_action = contact_menu.addAction("Edit")
edit_action.triggered.connect(self.edit_contact) edit_action.triggered.connect(self.edit_contact)
edit_action.setData(contact) edit_action.setData(index)
delete_action = contact_menu.addAction("Delete") delete_action = contact_menu.addAction("Delete")
delete_action.setData(contact) delete_action.setData(contact)
delete_action.triggered.connect(self.delete_contact) delete_action.triggered.connect(self.delete_contact)
...@@ -287,14 +287,13 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -287,14 +287,13 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.app.save(self.app.current_account) self.app.save(self.app.current_account)
self.action_set_as_default.setEnabled(False) self.action_set_as_default.setEnabled(False)
def refresh(self):
''' '''
Refresh main window Refresh main window
When the selected account changes, all the widgets When the selected account changes, all the widgets
in the window have to be refreshed in the window have to be refreshed
''' '''
logging.debug("Refresh started")
def refresh(self):
logging.debug("Refresh finished")
self.menu_change_account.clear() self.menu_change_account.clear()
signal_mapper = QSignalMapper(self) signal_mapper = QSignalMapper(self)
......
...@@ -44,7 +44,7 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog): ...@@ -44,7 +44,7 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
self.combo_wallets.addItem(wallet.name) self.combo_wallets.addItem(wallet.name)
for contact in sender.contacts: for contact in sender.contacts:
self.combo_contact.addItem(contact.name) self.combo_contact.addItem(contact['name'])
if len(self.account.contacts) == 0: if len(self.account.contacts) == 0:
self.combo_contact.setEnabled(False) self.combo_contact.setEnabled(False)
...@@ -56,7 +56,7 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog): ...@@ -56,7 +56,7 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
if self.radio_contact.isChecked(): if self.radio_contact.isChecked():
index = self.combo_contact.currentIndex() index = self.combo_contact.currentIndex()
recipient = self.account.contacts[index].pubkey recipient = self.account.contacts[index]['pubkey']
else: else:
recipient = self.edit_pubkey.text() recipient = self.edit_pubkey.text()
amount = self.spinbox_amount.value() amount = self.spinbox_amount.value()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment