Esempio n. 1
0
		def got_items(result, items):
			def created_item(result, id):
				if result is None: # Item successfully created
					keyring_data['id'] = id
					keyring.item_get_info(None, id, got_item)
				else:
					print "Couldn't create keyring item: " + str(result)
					fill_account_details()
					dialog.present()
			def got_item(result, item):
				if result is None: # Item retrieved successfully
					keyring_data['item'] = item
				else:
					print "Couldn't retrieve keyring item: " + str(result)
				fill_account_details()
				dialog.present()


			if result is None and len(items) != 0: # Got list of search results
				keyring_data['id'] = items[0].item_id
				keyring.item_get_info(None, keyring_data['id'], got_item)
			elif result == keyring.NoMatchError or len(items) == 0: # No items were found, so we'll create one
				keyring.item_create(None,
				                    keyring.ITEM_GENERIC_SECRET,
				                    "Rhythmbox: Magnatune account information",
				                    {'rhythmbox-plugin': 'magnatune'},
				                    "", # Empty secret for now
				                    True,
				                    created_item)
			else: # Some other error occurred
				print "Couldn't access keyring: " + str(result)
				fill_account_details()
				dialog.present()
Esempio n. 2
0
def get_login_password():
    keyring = gnomekeyring.get_default_keyring_sync()
    auth_token = gconf.client_get_default().get_int(GCONF_AUTH_KEY)
    if auth_token > 0:
        def item_get_info_cb(result, item, data):
            if result is None:
                secret = item.get_secret()
                data.append(secret)
            else:
                print "get_item_info result:", result
            gtk.main_quit()
        data = []
        gnomekeyring.item_get_info(keyring, auth_token, item_get_info_cb, data)
        gtk.main()
        try:
            secret, = data
        except ValueError:
            login = None
            password = None
            auth_token = 0
        else:
            login, password = secret.split('\n')
    else:
        login = None
        password = None
    
    dialog = gtk.Dialog("Enter login", None, 0,
                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                         gtk.STOCK_OK, gtk.RESPONSE_OK))
    dialog.props.has_separator = False
    dialog.set_default_response(gtk.RESPONSE_OK)

    hbox = gtk.HBox(False, 8)
    hbox.set_border_width(8)
    dialog.vbox.pack_start(hbox, False, False, 0)

    stock = gtk.image_new_from_stock(gtk.STOCK_DIALOG_AUTHENTICATION,
                                     gtk.ICON_SIZE_DIALOG)
    hbox.pack_start(stock, False, False, 0)

    table = gtk.Table(2, 2)
    table.set_row_spacings(4)
    table.set_col_spacings(4)
    hbox.pack_start(table, True, True, 0)

    label = gtk.Label("_Login")
    label.set_use_underline(True)
    table.attach(label, 0, 1, 0, 1)
    local_entry1 = gtk.Entry()
    local_entry1.set_activates_default(True)
    if login is not None:
        local_entry1.set_text(login)
    table.attach(local_entry1, 1, 2, 0, 1)
    label.set_mnemonic_widget(local_entry1)

    label = gtk.Label("_Password")
    label.set_use_underline(True)
    table.attach(label, 0, 1, 1, 2)
    local_entry2 = gtk.Entry()
    local_entry2.set_visibility(False)
    local_entry2.set_activates_default(True)
    if password is not None:
        local_entry2.set_text(password)
    table.attach(local_entry2, 1, 2, 1, 2)
    label.set_mnemonic_widget(local_entry2)

    dialog.show_all()
    while 1:
        response = dialog.run()

        if response == gtk.RESPONSE_OK:
            login = local_entry1.get_text()
            password = local_entry2.get_text()
            if not login or not password:
                continue
            def item_create_callback(result, auth_token, user_data):
                assert user_data == "user data"
                if result is None:
                    print "result is ok"
                    gconf.client_get_default().set_int(GCONF_AUTH_KEY, auth_token)
                else:
                    print "result:", result
                gtk.main_quit()
            auth_token = gnomekeyring.item_create(
                keyring,
                gnomekeyring.ITEM_GENERIC_SECRET,
                "GnomePythonDesktop keyring example, login information",
                dict(appname="GnomePythonDesktop, sync keyring example"),
                "\n".join((login, password)), True,
                item_create_callback, "user data")
            gtk.main()
            return login, password
        else:
            raise SystemExit