#!/usr/bin/python

# Ubuntu Tweak - PyGTK based desktop configuration tool
#
# Copyright (C) 2007-2008 TualatriX <tualatrix@gmail.com>
#
# Ubuntu Tweak 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.
#
# Ubuntu Tweak 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 Ubuntu Tweak; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

import os
import sys
import gtk
import gobject
import optparse
import dbus
import dbus.service
import dbus.mainloop.glib

gobject.threads_init()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
dbus.mainloop.glib.threads_init()

from ubuntutweak.common.consts import DATA_DIR, IS_INSTALLED, VERSION
from ubuntutweak.common.debug import run_traceback, enable_debugging
from ubuntutweak.common.config import TweakSettings

try:
    import dl
    libc = dl.open('/lib/libc.so.6')
    libc.call('prctl', 15, 'ubuntu-tweak', 0, 0, 0)
except:
    pass

LAUNCHER_DBUS_IFACE = 'com.ubuntu_tweak.TweakLauncher'
LAUNCHER_DBUS_PATH = '/com/ubuntu_tweak/TweakLauncher'

def show_splash():
    win = gtk.Window(gtk.WINDOW_POPUP)
    win.set_position(gtk.WIN_POS_CENTER)

    vbox = gtk.VBox(False, 0)
    image = gtk.Image()
    image.set_from_file(os.path.join(DATA_DIR, 'pixmaps/splash.png'))

    vbox.pack_start(image)
    win.add(vbox)

    win.show_all()

    while gtk.events_pending():
        gtk.main_iteration()

    win.destroy()

class TweakLauncher(dbus.service.Object):
    def __init__(self, session_bus, window):

        bus_name = dbus.service.BusName(LAUNCHER_DBUS_IFACE, bus=session_bus)
        dbus.service.Object.__init__(self, bus_name, LAUNCHER_DBUS_PATH)

        self.window = window

    def main(self):
        gtk.gdk.threads_enter()
        if TweakSettings.get_check_update():
            pass
#            os.system("exec python updatemanager.py &")
        gtk.main()
        gtk.gdk.threads_leave()

    @dbus.service.method(dbus_interface=LAUNCHER_DBUS_IFACE)
    def show_window(self):
        self.window.present()

if __name__ == "__main__":
    parser = optparse.OptionParser(prog="ubuntu-tweak",
                                   version="%%prog %s" % VERSION,
                                   description="Ubuntu Tweak is a tool for Ubuntu that makes it easy to configure your system and desktop settings.")
    parser.add_option("-d", "--debug", action="store_true", default=False,
                      help="Generate more debugging information.  [default: %default]")
    options, args = parser.parse_args()

    if options.debug or not IS_INSTALLED:
        enable_debugging()

    show_splash()
    session_bus = dbus.SessionBus()

    if session_bus.request_name(LAUNCHER_DBUS_IFACE) != \
            dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER:
        method = session_bus.get_object(LAUNCHER_DBUS_IFACE,
                                        LAUNCHER_DBUS_PATH)\
                                                .get_dbus_method("show_window")
        method()
        sys.exit(1)
    else:
        try:
            from ubuntutweak.mainwindow import MainWindow
            launcher = TweakLauncher(session_bus, MainWindow())
            launcher.main()
        except:
            run_traceback('fatal')
