#!/usr/bin/env python

import os
import sys

PACKAGE="firmware-usb-autoupdate"
LOCALEDIR="/usr/share/locale"
PYTHONDIR="/usr/lib/python2.3/site-packages"

sys.path.insert(0,PYTHONDIR)
sys.path.insert(0,os.path.join(PYTHONDIR, "gtk-2.0"))

import optparse
import pygtk
pygtk.require('2.0')
import gtk
import ConfigParser
import gettext, locale

from firmware_usb_autoupdate import util

locale.setlocale(locale.LC_ALL,'')
gettext.bindtextdomain(PACKAGE, os.environ.get("LOCALEDIR", LOCALEDIR))
gettext.textdomain(PACKAGE)
gettext.install(PACKAGE, LOCALEDIR, unicode=1)
_ = gettext.lgettext

APP_NAME = _("BIOS Auto-Update Configuration")
CONFIG = os.environ.get("CONFIG_PATH", "/etc/bios_autoupdate.ini")

class RadioButtons:
    def __init__(self, configFile):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.close_application)

        # prepopulate configuration with defaults
        # update_type in ("update-auto", "update-ask", "update-disable")
        # reboot_after_update in ("0", "1")
        self.cfg = util.initDefaultConfig()
        self.configFile = configFile
        self.cfg.read( configFile )

        self.window.set_title(APP_NAME)
        self.window.set_border_width(20)

        # main window box
        mainBox = gtk.VBox(False, 0)
        self.window.add(mainBox)
        mainBox.show()

        # tooltips
        self.tt = gtk.Tooltips()

        #
        # boxUpdateType ========================================================
        #

        frameUpdateType = gtk.Frame(_("Action on USB Key insertion:"))
        mainBox.pack_start(frameUpdateType, True, True, 0)
        frameUpdateType.show()

        boxUpdateType = gtk.VBox(False, 10)
        boxUpdateType.set_border_width(10)
        frameUpdateType.add(boxUpdateType)
        boxUpdateType.show()


        button = gtk.RadioButton(None, _("Automatic update"))
        self.tt.set_tip(button, _("Automatically apply update when USB key is inserted with applicable BIOS update."))
        if self.cfg.get("usb_auto_update", "update_type") == "update-auto": button.set_active(True)
        button.connect("toggled", self.update_callback, "update-auto")
        boxUpdateType.pack_start(button, True, True, 0)
        button.show()

        button = gtk.RadioButton(button, _("Ask"))
        self.tt.set_tip(button, _("Ask logged-in user for confirmation when USB key inserted with applicable BIOS update."))
        if self.cfg.get("usb_auto_update", "update_type") == "update-ask": button.set_active(True)
        button.connect("toggled", self.update_callback, "update-ask")
        boxUpdateType.pack_start(button, True, True, 0)
        button.show()

        button = gtk.RadioButton(button, _("No action"))
        self.tt.set_tip(button, _("Do nothing when USB key inserted."))
        if self.cfg.get("usb_auto_update", "update_type") == "update-disable": button.set_active(True)
        button.connect("toggled", self.update_callback, "update-disable")
        boxUpdateType.pack_start(button, True, True, 0)
        button.show()
        #
        # END boxUpdateType ====================================================
        #

        separator = gtk.HSeparator()
        mainBox.pack_start(separator, False, True, 0)
        separator.show()

        #
        # boxReboot ============================================================
        #
        frameReboot = gtk.Frame(_("After successful update:"))
        mainBox.pack_start(frameReboot, True, True, 0)
        frameReboot.show()

        boxReboot = gtk.VBox(False, 10)
        boxReboot.set_border_width(10)
        frameReboot.add(boxReboot)
        boxReboot.show()

        button = gtk.RadioButton(None, _("Automatic Reboot"))
        self.tt.set_tip(button, _("Automatically reboot system on successful update."))
        if self.cfg.get("usb_auto_update", "reboot_after_update") == "1": button.set_active(True)
        button.connect("toggled", self.reboot_callback, "1")
        boxReboot.pack_start(button, True, True, 0)
        button.show()

        button = gtk.RadioButton(button, _("Dont Reboot"))
        self.tt.set_tip(button, _("Dont reboot system, let user reboot after update."))
        if self.cfg.get("usb_auto_update", "reboot_after_update") == "0": button.set_active(True)
        button.connect("toggled", self.reboot_callback, "0")
        boxReboot.pack_start(button, True, True, 0)
        button.show()
        #
        # END boxReboot ========================================================
        #

        separator = gtk.HSeparator()
        mainBox.pack_start(separator, False, True, 0)
        separator.show()

        buttonBox = gtk.HButtonBox()
        buttonBox.set_border_width(10)
        mainBox.pack_start(buttonBox, False, True, 0)
        buttonBox.show()

        button = gtk.Button(None, stock=gtk.STOCK_CLOSE)
        button.connect_object("clicked", self.close_application, self.window, "close")
        buttonBox.pack_start(button, True, True, 0)
        button.show()

        button = gtk.Button(None, stock=gtk.STOCK_APPLY)
        button.connect_object("clicked", self.apply_config, self.window, "apply")
        buttonBox.pack_start(button, True, True, 0)
        button.set_flags(gtk.CAN_DEFAULT)
        button.grab_default()
        button.show()

        self.window.show()

    def update_callback(self, widget, data=None):
        if widget.get_active():
            self.cfg.set("usb_auto_update", "update_type", data)

    def reboot_callback(self, widget, data=None):
        if widget.get_active():
            self.cfg.set("usb_auto_update", "reboot_after_update", data)

    def apply_config(self, widget, event, data=None):
        if event == 'apply':
            self.save()

    def close_application(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def save(self):
        f = open(self.configFile, "w+")
        self.cfg.write(f)
        f.close()

def main():
    gtk.main()
    return 0        

if __name__ == "__main__":
    parser = optparse.OptionParser("usage: %prog [options]")
    parser.add_option("-c", "--config", dest="config_path",
                       type="string", action="store", default=CONFIG,
                       help="Put program into the background.")
    (options, args) = parser.parse_args()
    if len(args) != 0:
         parser.error("incorrect number of arguments")

    RadioButtons(options.config_path)
    main()
