#!/usr/bin/env python
#
# Copyright (C) 2009 One Laptop per Child
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


import sys
import os
import os.path
import subprocess
import gettext

import pygtk
pygtk.require('2.0')
import gtk

_ = lambda msg: gettext.dgettext('olpc-switch-desktop', msg)

def do_switch():
	try:
		fd = open(os.path.join(os.environ['HOME'], ".olpc-active-desktop"), "w")
		fd.write("sugar")
		fd.close()
	except Exception, e:
		print "Error:", e
		dlg = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK,
			message_format=_("Could not switch desktop: an internal error has "
			"occurred."))
		dlg.run()
		dlg.destroy()
		return
	subprocess.call(['dbus-send','--session','--dest=org.gnome.SessionManager',
		'/org/gnome/SessionManager','org.gnome.SessionManager.Logout',
		'uint32:1'])

def main():
	dlg = gtk.MessageDialog(parent=None, type=gtk.MESSAGE_INFO,
		buttons=gtk.BUTTONS_OK_CANCEL,
		message_format=_("Click OK to switch to the Sugar learning "
		"environment.\nAll active applications will be closed."))
	dlg.set_title("Switch desktop")
	dlg.set_property("skip-taskbar-hint", False)

	icon_theme = gtk.icon_theme_get_default()
	pixbuf = icon_theme.load_icon("olpc-switch-to-sugar", 48, 0)
	dlg.set_icon(pixbuf)

	resp = dlg.run()
	dlg.destroy()
	if resp == gtk.RESPONSE_OK:
		do_switch()

if __name__ == "__main__":
	main()
