store passwords in gnome keyring in another way. silently upgrade to the new way

This commit is contained in:
Yann Leboulanger 2009-01-20 21:24:22 +00:00
parent cd9c041585
commit 49925786c2
1 changed files with 22 additions and 8 deletions

View File

@ -77,11 +77,23 @@ class GnomePasswordStorage(PasswordStorage):
set_storage(SimplePasswordStorage()) set_storage(SimplePasswordStorage())
return password return password
try: try:
attributes = dict(account_name=str(account_name), gajim=1) server = gajim.config.get_per('accounts', account_name, 'hostname')
user = gajim.config.get_per('accounts', account_name, 'name')
attributes1 = dict(server=str(server), user=str(user), protocol='xmpp')
attributes2 = dict(account_name=str(account_name), gajim=1)
try: try:
items = gnomekeyring.find_items_sync( items = gnomekeyring.find_items_sync(
gnomekeyring.ITEM_GENERIC_SECRET, gnomekeyring.ITEM_NETWORK_PASSWORD, attributes1)
attributes) except gnomekeyring.Error:
try:
items = gnomekeyring.find_items_sync(
gnomekeyring.ITEM_GENERIC_SECRET, attributes2)
if items:
# We found an old item, move it to new way of storing
password = items[0].secret
self.save_password(account_name, password)
gnomekeyring.item_delete_sync(items[0].keyring,
int(items[0].item_id))
except gnomekeyring.Error: except gnomekeyring.Error:
items = [] items = []
if len(items) > 1: if len(items) > 1:
@ -100,12 +112,14 @@ class GnomePasswordStorage(PasswordStorage):
return None return None
def save_password(self, account_name, password, update=True): def save_password(self, account_name, password, update=True):
display_name = _('Gajim account %s') % account_name server = gajim.config.get_per('accounts', account_name, 'hostname')
attributes = dict(account_name=str(account_name), gajim=1) user = gajim.config.get_per('accounts', account_name, 'name')
display_name = _('XMPP account %s@%s') % (user, server)
attributes1 = dict(server=str(server), user=str(user), protocol='xmpp')
try: try:
auth_token = gnomekeyring.item_create_sync( auth_token = gnomekeyring.item_create_sync(
self.keyring, gnomekeyring.ITEM_GENERIC_SECRET, self.keyring, gnomekeyring.ITEM_NETWORK_PASSWORD,
display_name, attributes, password, update) display_name, attributes1, password, update)
except gnomekeyring.DeniedError: except gnomekeyring.DeniedError:
set_storage(SimplePasswordStorage()) set_storage(SimplePasswordStorage())
storage.save_password(account_name, password) storage.save_password(account_name, password)