Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm writing an application for the Ubuntu app showdown and I try to read/write the user's profile picture using this

>>> from gi.repository import AccountsService, GLib
>>> current_user = GLib.get_user_name()
>>> user = AccountsService.UserManager.get_default().get_user(current_user)
>>> print user.get_icon_file()
None
>>> print user.get_user_name()
None

But apparently does not work. Is there another way to use AccountsService with Python? I could not find more documentation about AccountsService and Python.

PS: to test this example you need to install gir1.2-accountsservice-1.0 on Ubuntu 12.04.

$ sudo apt-get install gir1.2-accountsservice-1.0
share|improve this question
1  
Wow. There is NO documentation on AccountsService. No manpage, no docs from the .deb. Nothing. Hope someone who has intimate knowledge of it weighs in. –  senshikaze Jun 21 '12 at 18:03
add comment

1 Answer 1

After looking at the source to libaccountsservice you actually have to wait for the user to be loaded before accessing the user object. This acheives the result:

from gi.repository import AccountsService, GLib

current_user = GLib.get_user_name()
user = AccountsService.UserManager.get_default().get_user(current_user)
def is_loaded_cb(user, param):
    print user.get_icon_file()
user.connect('notify::is-loaded', is_loaded_cb)

main_loop = GLib.MainLoop()
main_loop.run()

Alternatively, you can contact the accounts service directly and synchronously over D-Bus:

from gi.repository import GLib, Gio

current_user = GLib.get_user_name ()
bus = Gio.bus_get_sync (Gio.BusType.SYSTEM, None)
result = bus.call_sync ('org.freedesktop.Accounts',
                        '/org/freedesktop/Accounts',
                        'org.freedesktop.Accounts',
                        'FindUserByName',
                        GLib.Variant ('(s)', (current_user,)),
                        GLib.VariantType.new ('(o)'),
                        Gio.DBusCallFlags.NONE,
                        -1,
                        None)
(path,) = result.unpack ()

result = bus.call_sync ('org.freedesktop.Accounts',
                        path,
                        'org.freedesktop.DBus.Properties',
                        'GetAll',
                        GLib.Variant ('(s)', ('org.freedesktop.Accounts.User',)),
                        GLib.VariantType.new ('(a{sv})'),
                        Gio.DBusCallFlags.NONE,
                        -1,
                        None)
(props,) = result.unpack ()
print props['IconFile']
print props['RealName']

If you want to set the icon file you need to call the appropriate method:

result = bus.call_sync ('org.freedesktop.Accounts',
                       path,
                       'org.freedesktop.Accounts.User',
                       'SetIconFile',
                       GLib.Variant ('(s)', ('/tmp/somefile.png',)),
                       GLib.VariantType.new ('()'),
                       Gio.DBusCallFlags.NONE,
                       -1,
                       None)

If you need to use any other methods, browse the D-Bus interface using D-Feet (in the software center).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.