Skip to content

Commit

Permalink
Refactored contacts change handling in account
Browse files Browse the repository at this point in the history
  • Loading branch information
Insoleet committed Jan 29, 2016
1 parent d715c40 commit 38b7b6c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/sakia/core/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Account(QObject):
loading_finished = pyqtSignal(Community, list)
wallets_changed = pyqtSignal()
certification_accepted = pyqtSignal()
contacts_changed = pyqtSignal()

def __init__(self, salt, pubkey, name, communities, wallets, contacts, identities_registry):
"""
Expand Down Expand Up @@ -155,6 +156,15 @@ def add_contact(self, new_contact):
if len(same_contact) > 0:
raise ContactAlreadyExists(new_contact['name'], same_contact[0]['name'])
self.contacts.append(new_contact)
self.contacts_changed.emit()

def edit_contact(self, index, new_data):
self.contacts[index] = new_data
self.contacts_changed.emit()

def remove_contact(self, contact):
self.contacts.remove(contact)
self.contacts_changed.emit()

def add_community(self, community):
"""
Expand Down
16 changes: 8 additions & 8 deletions src/sakia/gui/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class ConfigureContactDialog(QDialog, Ui_ConfigureContactDialog):
classdocs
"""

def __init__(self, account, parent=None, contact=None, index_edit=None):
def __init__(self, app, account, parent=None, contact=None, index_edit=None):
"""
Constructor
"""
super().__init__()
super().__init__(parent)
self.setupUi(self)
self.app = app
self.account = account
self.main_window = parent
self.index_edit = index_edit
self.contact = contact

Expand All @@ -38,19 +38,19 @@ def __init__(self, account, parent=None, contact=None, index_edit=None):
self.edit_pubkey.setText(self.contact['pubkey'])

@classmethod
def from_identity(cls, parent, account, identity):
def from_identity(cls, app, parent, account, identity):
contact = {
'name': identity.uid,
'pubkey': identity.pubkey
}
return ConfigureContactDialog(account, parent, contact)
return ConfigureContactDialog(app, account, parent, contact)

def accept(self):
name = self.edit_name.text()
pubkey = self.edit_pubkey.text()
if self.index_edit is not None:
self.account.contacts[self.index_edit] = {'name': name,
'pubkey': pubkey}
self.account.edit_contact(self.index_edit, {'name': name,
'pubkey': pubkey})
logging.debug(self.contact)
else:
try:
Expand All @@ -60,7 +60,7 @@ def accept(self):
QMessageBox.critical(self, self.tr("Contact already exists"),
str(e),
QMessageBox.Ok)
self.main_window.app.save(self.account)
self.app.save(self.account)
super().accept()

def name_edited(self, new_name):
Expand Down
20 changes: 9 additions & 11 deletions src/sakia/gui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def startup(self):
if self.app.current_account:
self.password_asker = PasswordAskerDialog(self.app.current_account)
self.community_view.change_account(self.app.current_account, self.password_asker)
self.app.current_account.contacts_changed.connect(self.refresh_contacts)
self.refresh()

@asyncify
Expand Down Expand Up @@ -143,21 +144,20 @@ def update_time(self):
@pyqtSlot()
def delete_contact(self):
contact = self.sender().data()
self.app.current_account.contacts.remove(contact)
self.refresh_contacts()
self.app.current_account.remove_contacts(contact)

@pyqtSlot()
def edit_contact(self):
index = self.sender().data()
dialog = ConfigureContactDialog(self.app.current_account, self, None, index)
result = dialog.exec_()
if result == QDialog.Accepted:
self.window().refresh_contacts()
dialog = ConfigureContactDialog(self.app, self.app.current_account, self, None, index)
dialog.exec_()

def action_change_account(self, account_name):
self.app.accounts.contacts_changed.disconnect(self.refresh_contacts)
self.app.change_current_account(self.app.get_account(account_name))
self.password_asker = PasswordAskerDialog(self.app.current_account)
self.community_view.change_account(self.app.current_account, self.password_asker)
self.app.accounts.contacts_changed.connect(self.refresh_contacts)
self.refresh()

@pyqtSlot()
Expand Down Expand Up @@ -185,14 +185,12 @@ def open_certification_dialog(self):
self.password_asker)

def open_add_contact_dialog(self):
dialog = ConfigureContactDialog(self.app.current_account, self)
result = dialog.exec_()
if result == QDialog.Accepted:
self.window().refresh_contacts()
dialog = ConfigureContactDialog(self.app, self.app.current_account, self)
dialog.exec_()

def open_preferences_dialog(self):
dialog = PreferencesDialog(self.app)
result = dialog.exec_()
dialog.exec_()

def open_about_popup(self):
"""
Expand Down
5 changes: 1 addition & 4 deletions src/sakia/gui/widgets/context_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,8 @@ def informations(self, identity):
MemberDialog.open_dialog(self._app, self._account, self._community, identity)

def add_as_contact(self, identity):
dialog = ConfigureContactDialog.from_identity( self.parent(), self._account, identity)
dialog = ConfigureContactDialog.from_identity(self._app, self.parent(), self._account, identity)
dialog.exec_()
#TODO: Send signal from account to refresh contacts
# if result == QDialog.Accepted:
# self.parent().window().refresh_contacts()

@asyncify
async def send_money(self, identity):
Expand Down

0 comments on commit 38b7b6c

Please sign in to comment.