From 677176985c405de89d53e1c0be8d3f03e5569027 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Sat, 1 Mar 2014 12:42:15 +0100 Subject: [PATCH] Contacts management --- res/ui/mainwindow.ui | 15 +++++++++++---- src/cutecoin/gui/addContactDialog.py | 18 +++++++++++++----- src/cutecoin/gui/mainWindow.py | 9 +++++++-- src/cutecoin/gui/transferMoneyDialog.py | 15 ++++++++++----- src/cutecoin/models/account/__init__.py | 16 ++++++++++++---- src/cutecoin/models/coin/listModel.py | 5 ++--- src/cutecoin/models/person/__init__.py | 10 ++++++++-- src/cutecoin/models/wallet/__init__.py | 1 + 8 files changed, 64 insertions(+), 25 deletions(-) diff --git a/res/ui/mainwindow.ui b/res/ui/mainwindow.ui index b31de7e4..d763df0b 100644 --- a/res/ui/mainwindow.ui +++ b/res/ui/mainwindow.ui @@ -117,7 +117,7 @@ <height>20</height> </rect> </property> - <widget class="QMenu" name="menuTr_File"> + <widget class="QMenu" name="menu_account"> <property name="title"> <string>Account</string> </property> @@ -132,12 +132,19 @@ <addaction name="actionSave"/> <addaction name="actionQuit"/> </widget> - <widget class="QMenu" name="menuEdit"> + <widget class="QMenu" name="menu_contacts"> <property name="title"> <string>Contacts</string> </property> + <widget class="QMenu" name="menu_contactsList"> + <property name="title"> + <string>Contacts</string> + </property> + <addaction name="separator"/> + </widget> <addaction name="separator"/> <addaction name="actionAdd_a_contact"/> + <addaction name="menu_contactsList"/> </widget> <widget class="QMenu" name="menuActions"> <property name="title"> @@ -145,8 +152,8 @@ </property> <addaction name="actionTransfer_money"/> </widget> - <addaction name="menuTr_File"/> - <addaction name="menuEdit"/> + <addaction name="menu_account"/> + <addaction name="menu_contacts"/> <addaction name="menuActions"/> </widget> <widget class="QStatusBar" name="statusbar"/> diff --git a/src/cutecoin/gui/addContactDialog.py b/src/cutecoin/gui/addContactDialog.py index e3e2c99c..ae9e7543 100644 --- a/src/cutecoin/gui/addContactDialog.py +++ b/src/cutecoin/gui/addContactDialog.py @@ -4,7 +4,7 @@ Created on 2 févr. 2014 @author: inso ''' import logging - +import re from PyQt5.QtWidgets import QDialog, QDialogButtonBox @@ -16,21 +16,29 @@ class AddContactDialog(QDialog, Ui_AddContactDialog): ''' classdocs ''' - def __init__(self, account): + def __init__(self, account, parent=None): ''' Constructor ''' super(AddContactDialog, self).__init__() self.setupUi(self) + self.account = account + self.mainWindow = parent + self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) def accept(self): - #TODO: Add a contact on acceptance - pass + name = self.edit_name.text() + fingerprint = self.edit_fingerprint.text() + email = self.edit_email.text() + self.account.addContact(Person(name, fingerprint, email)) + self.mainWindow.menu_contactsList.addAction(name) + self.close() def nameEdited(self, newName): self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( len(newName) > 0 ) def fingerprintEdited(self, newFingerprint): - self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( len(newFingerprint) == 32 ) + pattern = re.compile("([A-Z]|[0-9])+") + self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( pattern.match(newFingerprint) is not None ) diff --git a/src/cutecoin/gui/mainWindow.py b/src/cutecoin/gui/mainWindow.py index 65c2c9b4..8be75f17 100644 --- a/src/cutecoin/gui/mainWindow.py +++ b/src/cutecoin/gui/mainWindow.py @@ -45,7 +45,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.core.addAccount(self.addAccountDialog.account) except KeyAlreadyUsed as e: QErrorMessage(self).showMessage(e.message) - self.refreshMainWindow() def save(self): @@ -61,7 +60,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): TransferMoneyDialog(self.core.currentAccount).exec_() def openAddContactDialog(self): - AddContactDialog(self.core.currentAccount).exec_() + AddContactDialog(self.core.currentAccount, self).exec_() ''' Refresh main window @@ -86,10 +85,16 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.accountNameLabel.setText("Current account : " + self.core.currentAccount.name) self.walletsList.setModel(WalletsListModel(self.core.currentAccount)) self.walletContent.setModel(WalletListModel(self.core.currentAccount.wallets.walletsList[0])) + self.communitiesTab.clear() for community in self.core.currentAccount.communities.communitiesList: communityTab = CommunityTabWidget(self.core.currentAccount, community) self.communitiesTab.addTab(communityTab, community.name()) + + self.menu_contactsList.clear() + for contact in self.core.currentAccount.contacts: + self.menu_contactsList.addAction(contact.name) + self.transactionsSent.setModel(SentListModel(self.core.currentAccount)) self.transactionsReceived.setModel(ReceivedListModel(self.core.currentAccount)) diff --git a/src/cutecoin/gui/transferMoneyDialog.py b/src/cutecoin/gui/transferMoneyDialog.py index 442941e1..3ad80f97 100644 --- a/src/cutecoin/gui/transferMoneyDialog.py +++ b/src/cutecoin/gui/transferMoneyDialog.py @@ -6,7 +6,7 @@ Created on 2 févr. 2014 import logging from math import pow -from PyQt5.QtWidgets import QDialog,QFrame, QSlider, QLabel, QDialogButtonBox +from PyQt5.QtWidgets import QDialog,QFrame, QSlider, QLabel, QDialogButtonBox, QErrorMessage from PyQt5.QtCore import Qt, QSignalMapper @@ -31,6 +31,9 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog): for wallet in sender.wallets.walletsList: self.comboBox_wallets.addItem(wallet.getText()) + for contact in sender.contacts: + self.comboBox_contact.addItem(contact.name) + self.refreshTransaction(sender.wallets.walletsList[0]) @@ -62,14 +65,13 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog): pass def accept(self): - sentCoins = self.listView_coinsSent.model().toString() + sentCoins = self.listView_coinsSent.model().toList() recipient = None if self.radio_keyFingerprint.isChecked(): recipient = Person("", self.edit_keyFingerprint.text(), "") else: - #TODO: Manage contacts - recipient = Person("", self.edit_keyFingerprint.text(), "") + recipient = self.sender.contacts[self.comboBox_contact.currentIndex()] if self.radio_nodeAddress.isChecked(): node = Node(self.edit_nodeAddress.text(), int(self.edit_port.text())) @@ -79,7 +81,10 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog): message = self.edit_message.text() #TODO: Transfer money, and validate the window if no error happened - print(self.sender.transferCoins(node, recipient, sentCoins, message)) + if self.sender.transferCoins(node, recipient, sentCoins, message): + self.close() + else: + QErrorMessage(self).showMessage("Cannot transfer coins.") def changeDisplayedWallet(self, index): wallet = self.sender.wallets.walletsList[index] diff --git a/src/cutecoin/models/account/__init__.py b/src/cutecoin/models/account/__init__.py index abfa4e31..0a55405f 100644 --- a/src/cutecoin/models/account/__init__.py +++ b/src/cutecoin/models/account/__init__.py @@ -12,6 +12,7 @@ from cutecoin.models.account.wallets import Wallets from cutecoin.models.account.communities import Communities from cutecoin.models.community import Community from cutecoin.models.transaction import factory +from cutecoin.models.person import Person class Account(object): ''' @@ -59,10 +60,10 @@ class Account(object): return account - def addWallet(name, currency): + def addWallet(self, name, currency): self.wallets.addWallet(name, currency) - def addContact(person): + def addContact(self, person): self.contacts.append(person) def keyFingerprint(self): @@ -121,13 +122,20 @@ class Account(object): return issuance() def transferCoins(self, node, recipient, coins, message): - transfer = ucoin.wrappers.transactions.Transfer(self.keyFingerprint(), recipient.fingerprint, coins, message, keyid=self.pgpKeyId, server=node.server, port=node.port) + transfer = ucoin.wrappers.transactions.RawTransfer(self.keyFingerprint(), recipient.fingerprint, coins, message, keyid=self.pgpKeyId, server=node.server, port=node.port) return transfer() + def jsonifyContacts(self): + data = [] + for p in self.contacts: + data.append(p.jsonify()) + return data + def jsonify(self): data = {'name' : self.name, 'pgpKeyId' : self.pgpKeyId, - 'communities' : self.communities.jsonify(self.wallets)} + 'communities' : self.communities.jsonify(self.wallets), + 'contacts' : self.jsonifyContacts()} return data diff --git a/src/cutecoin/models/coin/listModel.py b/src/cutecoin/models/coin/listModel.py index 3b7d9735..f167e467 100644 --- a/src/cutecoin/models/coin/listModel.py +++ b/src/cutecoin/models/coin/listModel.py @@ -29,9 +29,8 @@ class CoinsListModel(QAbstractListModel): def flags(self,index): return Qt.ItemIsSelectable | Qt.ItemIsEnabled - def toString(self): + def toList(self): coinsList = [] for c in self.coins: coinsList.append(c.getId()) - coinsStr = ",".join(coinsList) - return coinsStr \ No newline at end of file + return coinsList \ No newline at end of file diff --git a/src/cutecoin/models/person/__init__.py b/src/cutecoin/models/person/__init__.py index dc913e7a..4c837608 100644 --- a/src/cutecoin/models/person/__init__.py +++ b/src/cutecoin/models/person/__init__.py @@ -46,7 +46,13 @@ class Person(object): Create a person from json data ''' name = jsonPerson['name'] - pgpFingerprint = jsonPerson['fingerprint'] + fingerprint = jsonPerson['fingerprint'] email = jsonPerson['email'] - return cls(name, pgpFingerprint, email) + return cls(name, fingerprint, email) + + def jsonify(self): + data = {'name' : self.name, + 'fingerprint' : self.fingerprint, + 'email' : self.email} + return data diff --git a/src/cutecoin/models/wallet/__init__.py b/src/cutecoin/models/wallet/__init__.py index 65386c51..1de4d1a6 100644 --- a/src/cutecoin/models/wallet/__init__.py +++ b/src/cutecoin/models/wallet/__init__.py @@ -46,6 +46,7 @@ class Wallet(object): value += coin.value() return value + #TODO: Refresh coins when changing current account def refreshCoins(self, pgpFingerprint): dataList = self.community.ucoinRequest(ucoin.hdc.coins.List({'pgp_fingerprint':pgpFingerprint})) for issaunces in dataList['coins']: -- GitLab