#!/usr/bin/python

from dogtail.procedural import *
from time import sleep
config.fatalErrors = True
config.logDebugToFile = False

focus.application('gnome-panel')
# We're not using raw clicks here because sometimes we get bogus extents from 
# gnome-panel
click('System', raw = False)
click('Log Out .*', raw = False)

# The following sometimes doesn't work. I'm not sure why, but sometimes
# gnome-session doesn't show up as accessible. The good news is that even if
# we can't click the 'Log Out' button, newer versions of gnome-session log you
# out automatically after 60 seconds if you don't cancel. Also, the session
# implementations in dogtail.sessions will by default terminate the X server if
# this script fails to log out.
sleep(1)
try:
    focus.application('gnome-session')
    click('Log Out', raw = False)
except FocusError:
    # In earlier versions of GNOME, the logout dialog belonged to gnome-panel.
    focus.application('gnome-panel')
    # Really? alert? That's a new one for me.
    focus.widget(roleName = 'alert')
    click('Log Out', raw = False)

# Give the session some time to end before we kill it.
sleep(10)
