#!/usr/bin/python
# -*- coding: utf-8 -*-
### BEGIN LICENSE
# This file is in the public domain
### END LICENSE

import sys
import os
import gtk
import subprocess
import re

# Check if we are working in the source tree or from the installed 
# package and mangle the python path accordingly
if os.path.dirname(sys.argv[0]) != ".":
    if sys.argv[0][0] == "/":
        fullPath = os.path.dirname(sys.argv[0])
    else:
        fullPath = os.getcwd() + "/" + os.path.dirname(sys.argv[0])
else:
    fullPath = os.getcwd()
sys.path.insert(0, os.path.dirname(fullPath))

from winboot.winbootconfig import getdatapath

class WinbootWindow(gtk.Dialog):
    __gtype_name__ = "WinbootWindow"

    def __init__(self):
        """__init__ - This function is typically not called directly.
        Creation a WinbootWindow requires redeading the associated ui
        file and parsing the ui definition extrenally,
        and then calling WinbootWindow.finish_initializing().

        Use the convenience function NewWinbootWindow to create
        WinbootWindow object.

        """
        pass

    def finish_initializing(self, builder):
        """finish_initalizing should be called after parsing the ui definition
        and creating a WinbootWindow object with it in order to finish
        initializing the start of the new WinbootWindow instance.

        """
        #get a reference to the builder and set up the signals
        self.builder = builder
        self.builder.connect_signals(self)

    def response_cb(self, dlg, response):
        if response > 0:
            os.system('gksudo /usr/bin/winboot-reboot')
        gtk.main_quit()

def NewWinbootWindow():
    """NewWinbootWindow - returns a fully instantiated
    WinbootWindow object. Use this function rather than
    creating a WinbootWindow directly.
    """

    #look for the ui file that describes the ui
    ui_filename = os.path.join(getdatapath(), 'ui', 'WinbootWindow.ui')
    if not os.path.exists(ui_filename):
        ui_filename = None

    builder = gtk.Builder()
    builder.add_from_file(ui_filename)
    window = builder.get_object("winboot_window")
    window.finish_initializing(builder)
    return window


if __name__ == "__main__":
    import locale, gtk, gtk.glade
    locale.setlocale(locale.LC_ALL, '')
    gtk.glade.bindtextdomain('winboot', '/usr/share/locale')
    gtk.glade.textdomain('winboot')


    #support for command line options
    import logging, optparse
    parser = optparse.OptionParser(version="%prog %ver")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Show debug messages")
    (options, args) = parser.parse_args()

    #set the logging level to show debug messages
    if options.verbose:
        logging.basicConfig(level=logging.DEBUG)
        logging.debug('logging enabled')

    #run the application
    window = NewWinbootWindow()
    window.show()
    gtk.main()
