#!/usr/bin/python
# nicked off hamster-service

import gobject
import dbus, dbus.service
import glib
from dbus.mainloop.glib import DBusGMainLoop

DBusGMainLoop(set_as_default=True)
loop = gobject.MainLoop()

if "org.gnome.Hamster.WindowServer" in dbus.SessionBus().list_names():
    print "Found hamster-window-service already running, exiting"
    quit()


# maintain just one instance. this code feels hackish again
class WindowServer(dbus.service.Object):
    __dbus_object_path__ = "/org/gnome/Hamster/WindowServer"

    def __init__(self, loop):
        self.app = True
        self.mainloop = loop
        self.bus = dbus.SessionBus()
        bus_name = dbus.service.BusName("org.gnome.Hamster.WindowServer", bus=self.bus)
        dbus.service.Object.__init__(self, bus_name, self.__dbus_object_path__)

    @dbus.service.method("org.gnome.Hamster")
    def Quit(self):
        """Shutdown the service"""
        self.mainloop.quit()


    @dbus.service.method("org.gnome.Hamster.WindowServer")
    def edit(self, id=None): dialogs.edit.show(self.app, fact_id = id)

    @dbus.service.method("org.gnome.Hamster.WindowServer")
    def overview(self): dialogs.overview.show(self.app)

    @dbus.service.method("org.gnome.Hamster.WindowServer")
    def about(self): dialogs.about.show(self.app)

    @dbus.service.method("org.gnome.Hamster.WindowServer")
    def statistics(self): dialogs.stats.show(self.app)

    @dbus.service.method("org.gnome.Hamster.WindowServer")
    def preferences(self): dialogs.prefs.show(self.app)




if __name__ == '__main__':
    from hamster.lib import i18n
    i18n.setup_i18n()

    glib.set_prgname(unicode(_("hamster-windows-service")).encode("utf-8"))

    from hamster.configuration import runtime, dialogs, conf, load_ui_file
    window_server = WindowServer(loop)

    print "hamster-window-service up"

    loop.run()
