#!/usr/bin/python -Wall

import dbus
import dbus.mainloop.glib
import gtk
import gtk.glade
import gobject
import sys
import os
import subprocess
import platform
from optparse import OptionParser

SM_DBUS_NAME = "org.gnome.SessionManager"
SM_DBUS_PATH = "/org/gnome/SessionManager"
SM_DBUS_INTERFACE = "org.gnome.SessionManager"
SM_CLIENT_DBUS_INTERFACE = "org.gnome.SessionManager.ClientPrivate"
APP_ID = "debathena-branding"
GLADE_FILE="/usr/share/debathena-branding/debathena-branding.glade"
DEBATHENA_LOGO_FILE="/usr/share/pixmaps/debathena.png"

class Branding:
    def __init__(self, options):
        self.debug = options.debug
        self.sessionEnding = False
        self.sessionBus = dbus.SessionBus()
        try:
            self.register_with_sm()
            self.init_sm_client()
        except:
            print "Warning: Cannot register with session manager."
        
        try:
            self.xml = gtk.glade.XML(options.gladefile)
        except:
            print "Could not load Glade XML: %s" % (options.gladefile)
            sys.exit(1)

        
        self.winWelcome = self.xml.get_widget('winWelcome')
        self.winWelcome.set_property('can_focus', False)
        moveY = 200
        logoScale = 0.60
        if gtk.gdk.screen_height() <= 900:
            moveY = 10
            logoScale = 0.40
        self.winWelcome.move((gtk.gdk.screen_width() - self.winWelcome.get_size()[0]) / 2, moveY)
        self.imgDebathena = self.xml.get_widget('imgDebathena')
        self.imgDebathena.set_property('can_focus', False)
        try:
            pixbuf = gtk.gdk.pixbuf_new_from_file(DEBATHENA_LOGO_FILE)
            self.imgDebathena.set_from_pixbuf(pixbuf.scale_simple(int(pixbuf.get_width() * logoScale), int(pixbuf.get_height() * logoScale), gtk.gdk.INTERP_BILINEAR))
        except:
            # Just don't display the image if it's missing
            pass 
        self.winBranding = self.xml.get_widget('winBranding')
        self.winBranding.set_property('can_focus', False)
        self.lblBranding = self.xml.get_widget('lblBranding')
        self.lblBranding.set_property('can_focus', False)
        try:
            metapackage = subprocess.Popen(["machtype", "-L"], stdout=subprocess.PIPE).communicate()[0].rstrip()
        except OSError:
            metapackage = '(error)'
        try:
            baseos = subprocess.Popen(["machtype", "-E"], stdout=subprocess.PIPE).communicate()[0].rstrip()
        except OSError:
            baseos = '(error)'
        arch = platform.machine()
        if arch != "x86_64":
            arch = "<b>" + arch + "</b>"
        self.lblBranding.set_markup(metapackage + "\n" + baseos + "\n" + arch)
        self.winBranding.set_gravity(gtk.gdk.GRAVITY_SOUTH_EAST)
        width, height = self.winBranding.get_size()
        self.winBranding.move(gtk.gdk.screen_width() - width, gtk.gdk.screen_height() - height)

    # Connect to the session manager, and register our client.
    def register_with_sm(self):
        proxy = self.sessionBus.get_object(SM_DBUS_NAME, SM_DBUS_PATH)
        sm = dbus.Interface(proxy, SM_DBUS_INTERFACE)
        autostart_id = os.getenv("DESKTOP_AUTOSTART_ID", default="")
        self.smClientId = sm.RegisterClient(APP_ID, autostart_id)

    # Set up to handle signals from the session manager.
    def init_sm_client(self):
        proxy = self.sessionBus.get_object(SM_DBUS_NAME, self.smClientId)
        self.smClient = dbus.Interface(proxy, SM_CLIENT_DBUS_INTERFACE)
        self.smClient.connect_to_signal("QueryEndSession",
                                         self.sm_on_QueryEndSession)
        self.smClient.connect_to_signal("EndSession", self.sm_on_EndSession)
        self.smClient.connect_to_signal("CancelEndSession",
                                         self.sm_on_CancelEndSession)
        self.smClient.connect_to_signal("Stop", self.sm_on_Stop)

    # Here on a QueryEndSession signal from the session manager.
    def sm_on_QueryEndSession(self, flags):
        self.sessionEnding = True
        # Response args: is_ok, reason.
        self.smClient.EndSessionResponse(True, "")

    # Here on an EndSession signal from the session manager.
    def sm_on_EndSession(self, flags):
        self.sessionEnding = True
        # Response args: is_ok, reason.
        self.smClient.EndSessionResponse(True, "")

    # Here on a CancelEndSession signal from the session manager.
    def sm_on_CancelEndSession(self):
        self.sessionEnding = False

    # Here on a Stop signal from the session manager.
    def sm_on_Stop(self):
        subprocess.call(["/usr/bin/pkill", "-f", "gnome-settings-daemon"])
        gtk.main_quit()

def main(options):
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    Branding(options)
    gtk.main()

if __name__ == '__main__':
    parser = OptionParser()
    parser.set_defaults(debug=False, guitest=False)
    parser.add_option("--test", action="store_true", dest="debug")
    parser.add_option("--glade", action="store", type="string",
                      default=GLADE_FILE, dest="gladefile")
    (options, args) = parser.parse_args()
    main(options)
