#!/usr/bin/env python
#
# Test opening a new session
#

import os
import gobject
import dbus
import dbus.glib

print "uid=%d pid=%d" % (os.geteuid (), os.getpid ())

bus = dbus.SystemBus ()

manager_obj = bus.get_object ('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')

manager = dbus.Interface (manager_obj, 'org.freedesktop.ConsoleKit.Manager')

cookie = manager.OpenSession ()
os.environ['XDG_SESSION_COOKIE'] = cookie
print "Got cookie: %s" % cookie

seats = manager.GetSeats ()
for sid in seats:
    seat_obj = bus.get_object ('org.freedesktop.ConsoleKit', sid)
    seat = dbus.Interface (seat_obj, 'org.freedesktop.ConsoleKit.Seat')
    sessions = seat.GetSessions ()
    for ssid in sessions:
            session_obj = bus.get_object ('org.freedesktop.ConsoleKit', ssid)
            session = dbus.Interface (session_obj, 'org.freedesktop.ConsoleKit.Session')
            uid = session.GetUser ()

# Hmm looks like the proc environment doesn't get updated when
# os.environ is set.  So, we won't use GetCurrentSession.
#current_session = manager.GetCurrentSession ()
current_session = manager.GetSessionForCookie (cookie)
print "Current session: %s" % current_session

session_obj = bus.get_object ('org.freedesktop.ConsoleKit', ssid)
session = dbus.Interface (session_obj, 'org.freedesktop.ConsoleKit.Session')

mainloop = gobject.MainLoop ()

gobject.timeout_add (30000, mainloop.quit)

mainloop.run()
