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

Added the possibility to edit contacts and remove them

parent ac3b27ba
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AddContactDialog</class>
<widget class="QDialog" name="AddContactDialog">
<class>ConfigureContactDialog</class>
<widget class="QDialog" name="ConfigureContactDialog">
<property name="geometry">
<rect>
<x>0</x>
......@@ -59,7 +59,7 @@
<connection>
<sender>button_box</sender>
<signal>accepted()</signal>
<receiver>AddContactDialog</receiver>
<receiver>ConfigureContactDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
......@@ -75,7 +75,7 @@
<connection>
<sender>button_box</sender>
<signal>rejected()</signal>
<receiver>AddContactDialog</receiver>
<receiver>ConfigureContactDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
......@@ -91,7 +91,7 @@
<connection>
<sender>edit_pubkey</sender>
<signal>textChanged(QString)</signal>
<receiver>AddContactDialog</receiver>
<receiver>ConfigureContactDialog</receiver>
<slot>pubkey_edited()</slot>
<hints>
<hint type="sourcelabel">
......@@ -107,7 +107,7 @@
<connection>
<sender>edit_name</sender>
<signal>textChanged(QString)</signal>
<receiver>AddContactDialog</receiver>
<receiver>ConfigureContactDialog</receiver>
<slot>name_edited()</slot>
<hints>
<hint type="sourcelabel">
......
......@@ -176,6 +176,11 @@
<string>Set as default</string>
</property>
</action>
<action name="actionAbout">
<property name="text">
<string>About</string>
</property>
</action>
</widget>
<resources/>
<connections>
......@@ -335,5 +340,6 @@
<slot>export_account()</slot>
<slot>open_certification_dialog()</slot>
<slot>set_as_default_account()</slot>
<slot>open_about_dialog()</slot>
</slots>
</ui>
......@@ -18,7 +18,7 @@ import time
from .wallet import Wallet
from .community import Community
from .person import Person
from ..tools.exceptions import NoPeerAvailable
from ..tools.exceptions import NoPeerAvailable, ContactAlreadyExists
def quantitative(units, community):
......@@ -119,11 +119,12 @@ class Account(object):
return (key.pubkey == self.pubkey)
def add_contact(self, person):
same_contact = [contact for contact in self.contacts if person.pubkey == contact.pubkey]
if len(same_contact) == 0:
self.contacts.append(person)
return True
return False
same_contact = [contact for contact in self.contacts
if person.pubkey == contact.pubkey]
if len(same_contact) > 0:
raise ContactAlreadyExists(person.uid, same_contact[0].uid)
self.contacts.append(person)
def add_community(self, community):
logging.debug("Adding a community")
......
......@@ -11,7 +11,7 @@ from PyQt5.QtWidgets import QWidget, QMessageBox, QAction, QMenu, QDialog, \
QAbstractItemView
from ..models.members import MembersFilterProxyModel, MembersTableModel
from ..gen_resources.community_tab_uic import Ui_CommunityTabWidget
from .add_contact import AddContactDialog
from cutecoin.gui.contact import ConfigureContactDialog
from .wot_tab import WotTabWidget
from .transfer import TransferMoneyDialog
from .password_asker import PasswordAskerDialog
......@@ -88,11 +88,11 @@ class CommunityTabWidget(QWidget, Ui_CommunityTabWidget):
menu.exec_(self.table_community_members.mapToGlobal(point))
def add_member_as_contact(self):
dialog = AddContactDialog(self.account, self.window())
person = self.sender().data()
dialog.edit_name.setText(person.name)
dialog.edit_pubkey.setText(person.pubkey)
dialog.exec_()
dialog = ConfigureContactDialog(self.account, self.window(), person)
result = dialog.exec_()
if result == QDialog.Accepted:
self.window().refresh_contacts()
def send_money_to_member(self):
dialog = TransferMoneyDialog(self.account, self.password_asker)
......
......@@ -4,21 +4,19 @@ Created on 2 févr. 2014
@author: inso
'''
import re
from PyQt5.QtWidgets import QDialog, QDialogButtonBox
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QMessageBox
from ..core.person import Person
from ..tools.exceptions import ContactAlreadyExists
from ..gen_resources.contact_uic import Ui_ConfigureContactDialog
from cutecoin.core.person import Person
from cutecoin.gen_resources.add_contact_uic import Ui_AddContactDialog
class AddContactDialog(QDialog, Ui_AddContactDialog):
class ConfigureContactDialog(QDialog, Ui_ConfigureContactDialog):
'''
classdocs
'''
def __init__(self, account, parent=None):
def __init__(self, account, parent=None, contact=None):
'''
Constructor
'''
......@@ -26,15 +24,28 @@ class AddContactDialog(QDialog, Ui_AddContactDialog):
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)
self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
def accept(self):
name = self.edit_name.text()
pubkey = self.edit_pubkey.text()
result = self.account.add_contact(Person(name, pubkey))
if result:
self.main_window.menu_contacts_list.addAction(name)
self.main_window.app.save(self.account)
if self.contact:
self.contact.name = name
self.contact.pubkey = pubkey
else:
try:
self.account.add_contact(Person(name, 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()
def name_edited(self, new_name):
......
......@@ -12,7 +12,7 @@ from PyQt5.QtGui import QIcon
from .process_cfg_account import ProcessConfigureAccount
from .transfer import TransferMoneyDialog
from .currency_tab import CurrencyTabWidget
from .add_contact import AddContactDialog
from cutecoin.gui.contact import ConfigureContactDialog
from .import_account import ImportAccountDialog
from .certification import CertificationDialog
from .password_asker import PasswordAskerDialog
......@@ -129,6 +129,20 @@ class MainWindow(QMainWindow, Ui_MainWindow):
timer.timeout.connect(self.update_time)
timer.start(next_time - current_time)
@pyqtSlot()
def delete_contact(self):
contact = self.sender().data()
self.app.current_account.contacts.remove(contact)
self.refresh_contacts()
@pyqtSlot()
def edit_contact(self):
contact = self.sender().data()
dialog = ConfigureContactDialog(self.app.current_account, self, contact)
result = dialog.exec_()
if result == QDialog.Accepted:
self.window().refresh_contacts()
def action_change_account(self, account_name):
self.busybar.show()
self.status_label.setText("Loading account {0}".format(account_name))
......@@ -149,7 +163,10 @@ class MainWindow(QMainWindow, Ui_MainWindow):
dialog.exec_()
def open_add_contact_dialog(self):
AddContactDialog(self.app.current_account, self).exec_()
dialog = ConfigureContactDialog(self.app.current_account, self)
result = dialog.exec_()
if result == QDialog.Accepted:
self.window().refresh_contacts()
def open_configure_account_dialog(self):
dialog = ProcessConfigureAccount(self.app, self.app.current_account)
......@@ -173,6 +190,17 @@ class MainWindow(QMainWindow, Ui_MainWindow):
QIcon(":/icons/currency_icon"),
community.name())
def refresh_contacts(self):
self.menu_contacts_list.clear()
for contact in 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)
delete_action = contact_menu.addAction("Delete")
delete_action.setData(contact)
delete_action.triggered.connect(self.delete_contact)
def set_as_default_account(self):
self.app.default_account = self.app.current_account.name
logging.debug(self.app.current_account)
......@@ -237,10 +265,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
QMessageBox.critical(self, ":(",
str(e),
QMessageBox.Ok)
self.menu_contacts_list.clear()
for contact in self.app.current_account.contacts:
self.menu_contacts_list.addAction(contact.name)
self.refresh_contacts()
def import_account(self):
dialog = ImportAccountDialog(self.app, self)
......
......@@ -9,10 +9,11 @@ from ..gen_resources.wot_tab_uic import Ui_WotTabWidget
from cutecoin.gui.views.wot import NODE_STATUS_HIGHLIGHTED, NODE_STATUS_SELECTED, NODE_STATUS_OUT, ARC_STATUS_STRONG, ARC_STATUS_WEAK
from ucoinpy.api import bma
from .certification import CertificationDialog
from .add_contact import AddContactDialog
from cutecoin.gui.contact import ConfigureContactDialog
from .transfer import TransferMoneyDialog
from cutecoin.core.person import Person
class WotTabWidget(QWidget, Ui_WotTabWidget):
def __init__(self, account, community, password_asker, parent=None):
"""
......@@ -361,7 +362,7 @@ class WotTabWidget(QWidget, Ui_WotTabWidget):
# check if contact already exists...
if metadata['id'] == self.account.pubkey or metadata['id'] in [contact.pubkey for contact in self.account.contacts]:
return False
dialog = AddContactDialog(self.account, self.window())
dialog = ConfigureContactDialog(self.account, self.window())
dialog.edit_name.setText(metadata['text'])
dialog.edit_pubkey.setText(metadata['id'])
dialog.exec_()
......
......@@ -165,3 +165,17 @@ class NoPeerAvailable(Error):
super() .__init__(
"No peer answered in {0} community ({1} peers available)"
.format(currency, peers))
class ContactAlreadyExists(Error):
'''
Exception raised when a community doesn't have any
peer available.
'''
def __init__(self, new_contact, already_contact):
'''
Constructor
'''
super() .__init__(
"Cannot add {0}, he/she has the same pubkey as {1} contact"
.format(new_contact, already_contact))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment