Browse Source

use address as key in contacts

283
ThomasV 9 years ago
parent
commit
2259b741f6
  1. 20
      gui/qt/contact_list.py
  2. 17
      gui/qt/main_window.py
  3. 7
      lib/contacts.py

20
gui/qt/contact_list.py

@ -36,13 +36,13 @@ from util import MyTreeWidget, pr_tooltips, pr_icons
class ContactList(MyTreeWidget): class ContactList(MyTreeWidget):
def __init__(self, parent): def __init__(self, parent):
MyTreeWidget.__init__(self, parent, self.create_menu, [_('Name'), _('Value'), _('Type')], 1, [0, 1]) MyTreeWidget.__init__(self, parent, self.create_menu, [_('Name'), _('Type'), _('Value')], 0, [0])
self.setSelectionMode(QAbstractItemView.ExtendedSelection) self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setSortingEnabled(True) self.setSortingEnabled(True)
def on_permit_edit(self, item, column): def on_permit_edit(self, item, column):
# openalias items shouldn't be editable # openalias items shouldn't be editable
return item.text(2) != "openalias" return item.text(1) != "openalias"
def on_edited(self, item, column, prior): def on_edited(self, item, column, prior):
if column == 0: # Remove old contact if renamed if column == 0: # Remove old contact if renamed
@ -55,9 +55,9 @@ class ContactList(MyTreeWidget):
if not selected: if not selected:
menu.addAction(_("New contact"), lambda: self.parent.new_contact_dialog()) menu.addAction(_("New contact"), lambda: self.parent.new_contact_dialog())
else: else:
labels = [unicode(item.text(0)) for item in selected] names = [unicode(item.text(0)) for item in selected]
addrs = [unicode(item.text(1)) for item in selected] types = [unicode(item.text(1)) for item in selected]
types = [unicode(item.text(2)) for item in selected] keys = [unicode(item.text(2)) for item in selected]
column = self.currentColumn() column = self.currentColumn()
column_title = self.headerItem().text(column) column_title = self.headerItem().text(column)
column_data = '\n'.join([unicode(item.text(column)) for item in selected]) column_data = '\n'.join([unicode(item.text(column)) for item in selected])
@ -66,10 +66,10 @@ class ContactList(MyTreeWidget):
if column in self.editable_columns: if column in self.editable_columns:
menu.addAction(_("Edit %s")%column_title, lambda: self.editItem(item, column)) menu.addAction(_("Edit %s")%column_title, lambda: self.editItem(item, column))
menu.addAction(_("Pay to"), lambda: self.parent.payto_contacts(labels)) menu.addAction(_("Pay to"), lambda: self.parent.payto_contacts(keys))
menu.addAction(_("Delete"), lambda: self.parent.delete_contacts(labels)) menu.addAction(_("Delete"), lambda: self.parent.delete_contacts(keys))
URLs = [] URLs = []
for (addr, _type) in zip(addrs, types): for (addr, _type) in zip(keys, types):
if _type == 'address': if _type == 'address':
URLs.append(block_explorer_URL(self.config, 'addr', addr)) URLs.append(block_explorer_URL(self.config, 'addr', addr))
if URLs: if URLs:
@ -84,8 +84,8 @@ class ContactList(MyTreeWidget):
current_key = item.data(0, Qt.UserRole).toString() if item else None current_key = item.data(0, Qt.UserRole).toString() if item else None
self.clear() self.clear()
for key in sorted(self.parent.contacts.keys()): for key in sorted(self.parent.contacts.keys()):
_type, value = self.parent.contacts[key] _type, name = self.parent.contacts[key]
item = QTreeWidgetItem([key, value, _type]) item = QTreeWidgetItem([name, _type, key])
item.setData(0, Qt.UserRole, key) item.setData(0, Qt.UserRole, key)
self.addTopLevelItem(item) self.addTopLevelItem(item)
if key == current_key: if key == current_key:

17
gui/qt/main_window.py

@ -269,7 +269,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
wallet.thread = TaskThread(self, self.on_error) wallet.thread = TaskThread(self, self.on_error)
self.wallet = wallet self.wallet = wallet
self.update_recently_visited(wallet.storage.path) self.update_recently_visited(wallet.storage.path)
self.import_old_contacts()
# address used to create a dummy transaction and estimate transaction fee # address used to create a dummy transaction and estimate transaction fee
self.accounts_expanded = self.wallet.storage.get('accounts_expanded',{}) self.accounts_expanded = self.wallet.storage.get('accounts_expanded',{})
self.current_account = self.wallet.storage.get("current_account", None) self.current_account = self.wallet.storage.get("current_account", None)
@ -320,16 +319,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
]) ])
self.show_warning(msg, title=_('Information')) self.show_warning(msg, title=_('Information'))
def import_old_contacts(self):
# backward compatibility: import contacts
old_contacts = self.wallet.storage.get('contacts', [])
if old_contacts:
for k in set(old_contacts):
l = self.wallet.labels.get(k)
if bitcoin.is_address(k) and l:
self.contacts[l] = ('address', k)
self.wallet.storage.put('contacts', None)
def open_wallet(self): def open_wallet(self):
wallet_folder = self.get_wallet_folder() wallet_folder = self.get_wallet_folder()
filename = unicode(QFileDialog.getOpenFileName(self, "Select your wallet file", wallet_folder)) filename = unicode(QFileDialog.getOpenFileName(self, "Select your wallet file", wallet_folder))
@ -1106,8 +1095,8 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
self.from_list.addTopLevelItem(QTreeWidgetItem( [format(item), self.format_amount(item['value']) ])) self.from_list.addTopLevelItem(QTreeWidgetItem( [format(item), self.format_amount(item['value']) ]))
def get_contact_payto(self, key): def get_contact_payto(self, key):
_type, value = self.contacts.get(key) _type, label = self.contacts.get(key)
return key + ' <' + value + '>' if _type == 'address' else key return label + ' <' + key + '>' if _type == 'address' else key
def update_completions(self): def update_completions(self):
l = [self.get_contact_payto(key) for key in self.contacts.keys()] l = [self.get_contact_payto(key) for key in self.contacts.keys()]
@ -1486,7 +1475,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
self.show_error(_('Invalid Address')) self.show_error(_('Invalid Address'))
self.contact_list.update() # Displays original unchanged value self.contact_list.update() # Displays original unchanged value
return False return False
self.contacts[label] = ('address', address) self.contacts[address] = ('address', label)
self.contact_list.update() self.contact_list.update()
self.history_list.update() self.history_list.update()
self.update_completions() self.update_completions()

7
lib/contacts.py

@ -35,6 +35,13 @@ class Contacts(StoreDict):
def __init__(self, config): def __init__(self, config):
StoreDict.__init__(self, config, 'contacts') StoreDict.__init__(self, config, 'contacts')
# backward compatibility
for k, v in self.items():
_type, n = v
if _type == 'address' and bitcoin.is_address(n):
self.pop(k)
self[n] = ('address', k)
def resolve(self, k): def resolve(self, k):
if bitcoin.is_address(k): if bitcoin.is_address(k):

Loading…
Cancel
Save