#!/usr/bin/env python

try:
    import pygtk; pygtk.require("2.0")
except ImportError:
    pass

import gtk

from code import gdeskcalhome
import os
os.chdir(gdeskcalhome)

import code.i18n

from code.CalMediator import CalMediator
from code import values
from code import errormessage

import sys
import signal



#
# Ensures that EventBoxes look transparent, no matter what the current
# GTK theme is.
#
def ensure_transparency():

    rc_style = """
      style "transparent" { bg_pixmap[NORMAL] = "<parent>"
                            bg_pixmap[INSENSITIVE] = "<parent>"
                            bg_pixmap[PRELIGHT] = "<parent>"
                            bg_pixmap[SELECTED] = "<parent>"
                            bg_pixmap[ACTIVE] = "<parent>"
      }
      class "GtkEventBox" style "transparent"
    """

    #gtk.rc_parse_string(rc_style)



#
# Ensures that the application knows its home directories.
#
def ensure_home():

    os.environ[values.DESKCALHOME] = os.getcwd()
    os.environ[values.DESKCALUSERHOME] = os.path.expanduser(values.CONFIGDIR)

    



#
# Ensures that there is a directory for holding the user settings.
#
def ensure_configdir():

    try:
        userhomedir = os.environ[values.DESKCALUSERHOME]
        if (not os.path.exists(userhomedir)):
            os.mkdir(userhomedir)

        if (not os.path.exists(os.path.join(userhomedir, values.SKINDIR))):
            os.mkdir(os.path.join(userhomedir, values.SKINDIR))

        if (not os.path.exists(os.path.join(userhomedir, values.CONFIGFILE))):
            # install the default configuration
            os.system("cp " + values.CONFIGDEFAULT + " " + userhomedir)


    except:
        errormessage.error(_("Could not install the configuration file.\n" \
                           "gDeskCal will not be able to remember " \
                           "configuration changes."))



# install signal handler to quit on Ctrl-C
signal.signal(signal.SIGINT, gtk.mainquit)

print "gDeskCal version " + values.VERSION
print "copyright (C) 2002 - 2004 by Martin Grimme"
print "This software is licensed under the terms of the GNU GPL."
print ""


ensure_home()
ensure_configdir()
ensure_transparency()

try:
    index = sys.argv.index("--sm-client-id")
    cid = sys.argv[index + 1]

except:
    cid = "gdeskcal"	

gtk.gdk.set_sm_client_id(cid)

dcal = CalMediator()

gtk.mainloop()
