#!/usr/bin/env python

import sys
import os

from gi.repository import GObject
GObject.threads_init()

from gi.repository import TelepathyGLib as Tp

def manager_prepared_cb(manager, result, loop):
    manager.prepare_finish(result)

    for account in manager.get_valid_accounts():
        t, s, m = account.get_current_presence()
        print "account %s: (%u, %s, %s)" % (account.get_path_suffix(), t, s, m)

    if len(sys.argv) == 2:
        print ""
        if (sys.argv[1]) == 'online':
            print "Change presence to online"
            manager.set_all_requested_presences(Tp.ConnectionPresenceType.AVAILABLE, "available", "")
        elif (sys.argv[1]) == 'offline':
            print "Change presence to offline"
            manager.set_all_requested_presences(Tp.ConnectionPresenceType.OFFLINE, "offline", "")
    loop.quit()

if __name__ == '__main__':
    Tp.debug_set_flags(os.getenv('EXAMPLE_DEBUG', ''))

    loop = GObject.MainLoop()
    manager = Tp.AccountManager.dup()
    factory = manager.get_factory()

    manager.prepare_async(None, manager_prepared_cb, loop)
    loop.run()
