#!/usr/bin/python

# configlets-druid -- GNOME Druid ("wizard") frontend

# $Progeny$

# Copyright (C) 2001  Progeny Linux Systems, Inc.
# AUTHORS: Jeff Licquia <jlicquia@progeny.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,  as
# published by the Free Software Foundation.

# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

###############################################################################
# Parse command line options

# We do this here because pygtk provides no way to hook into GNOME's
# popt mechanism.

import sys
import os
import string
import getopt

ask_only = 0
commit_only = 0

try:
    (opts, args) = getopt.getopt(sys.argv[1:], None,
                                 ["ask-only", "commit-only"])
except getopt.GetoptError:
    pass

for i in opts:
    if i[0] == "--ask-only":
        ask_only = 1
    elif i[0] == "--commit-only":
        commit_only = 1

sys.argv = [sys.argv[0],"--sm-disable"]

###############################################################################

import pygtk
pygtk.require("2.0")

import gtk
import gtk.glade
import gnome.ui

import configlet

def set_page(page_id, is_forward):
    global druid
    global picked_configlets
    global druid_pages
    global druid_page_end
    global druid_select_page
    global cancel_button_state

    configlet.debug("configlets-druid", "current page id: %s" % (page_id,))

    if not page_id:
        if is_forward:
            page_index = -1
        else:
            page_index = len(druid_pages)
    else:
        for page_index in range(0, len(druid_pages)):
            if druid_pages[page_index][0] == page_id:
                break

    configlet_name = ""
    while configlet_name not in picked_configlets:
        if is_forward:
            page_index = page_index + 1
        else:
            page_index = page_index - 1
        if page_index < 0 or page_index >= len(druid_pages):
            configlet_name = ""
            configlet.debug("configlets-druid", "off the edge...")
            break

        found_page_id = druid_pages[page_index][0]
        configlet.debug("configlets-druid",
                        "considering page id %s" % found_page_id)
        configlet_name = found_page_id

    if not configlet_name:
        if is_forward:
            druid.set_page(druid_page_end)
            druid.set_buttons_sensitive(True, True, cancel_button_state, False)
        else:
            druid.set_page(druid_select_page)
            druid.set_buttons_sensitive(False, True, cancel_button_state, False)
    else:
        configlet.debug("configlets-druid",
                        "setting page to %s" % found_page_id)
        druid.set_page(druid_pages[page_index][1])
        druid.set_buttons_sensitive(True, True, cancel_button_state, False)

def toggle_configlet(widget, page_id):
    global picked_configlets

    if widget.get_active():
        if page_id not in picked_configlets:
            picked_configlets.append(page_id)
    else:
        while page_id in picked_configlets:
            picked_configlets.remove(page_id)

    configlet.debug("configlets-druid",
                    "picked: %s" % string.join(picked_configlets, ", "))

    return False

def next_from_start(*args):
    global druid
    global druid_select_page
    global cancel_button_state

    druid.set_page(druid_select_page)
    druid.set_buttons_sensitive(False, True, cancel_button_state, False)
    return True

def finished(*args):
    global cfggroup
    global completed
    completed = True
    cfggroup.on_gnome_close()
    gtk.mainquit()
    return True

def canceled(*args):
    gtk.mainquit()
    return True

def back_page(*args):
    set_page(args[-1], False)
    return True

def next_page(*args):
    set_page(args[-1], True)
    return True

def configlet_done(druid, druidpage, page_id):
    global not_validated
    global cfggroup

    configlet.debug("configlets-druid",
                    "entering configlet_done for %s" % (page_id,))
    for cfglet in cfggroup:
        if cfglet.get_name() == page_id:
            configlet.debug("configlets-druid", "found page")
            try:
                validation_result = cfglet.validate()
            except:
                validation_result = False
            if not validation_result:
                configlet.debug("configlets-druid", "oops - bad validation")
                button = GnomeMessageBox("""
The \"%s\" configuration screen failed to validate.  If you continue,
the configuration information you entered may be lost.  Do you want
to continue?
""" % (cfglet.get_display_title(),), "warning",
                                         STOCK_BUTTON_YES,
                                         STOCK_BUTTON_NO).run_and_close()
                if button == 0:
                    configlet.debug("configlets-druid", "going ahead anyway")
                    not_validated[page_id] = False
                else:
                    configlet.debug("configlets-druid", "user wants to try again")
                    return True
            else:
                configlet.debug("configlets-druid", "validation ok")
                if not_validated.has_key(page_id):
                    del not_validated[page_id]

            set_page(page_id, True)
            return True

    raise RuntimeError, "configlet %s not found for validation" % (page_id,)

start_title = "System Configuration"
start_text = """
You can now configure important aspects of the system.

Click Next to cycle through the following configuration screens.  Each
screen provides context-sensitive help for the controls it uses.  Simply
let the cursor of your mouse or other pointing device "hover" over the
control you need help with.

If problems occur with the information you provide, the system will ask for
clarification or will point out the problem.
"""

end_title = "Finish System Configuration"
end_text = """
Congratulations!  You've completed the steps to configure your system.

Click Finish to complete the configuration stage.  The system will be set
up according to your specifications.  This could take a few minutes, so
please be patient.
"""

default_rc = "/usr/share/themes/Raleigh/gtk/gtkrc"
config_filename = "/etc/configlets-druid.cfg"

not_validated = {}
completed = False
cancel_button_state = False
picked_configlets = []
druid_pages = []
config = {}
required_configlet_names = []
recommended_configlet_names = []
optional_configlet_names = []
omitted_configlet_names = []

class DruidConfigGroup(configlet.BasicConfigGroup):
    def debug(self, message):
        configlet.debug("DruidConfigGroup", message)

    def warn(self, message):
        configlet.warn("DruidConfigGroup", message)

    def error(self, message):
        configlet.error("DruidConfigGroup", message)

    def show(self):
        global required_configlet_names
        global recommended_configlet_names
        global optional_configlet_names
        global omitted_configlet_names
        global picked_configlets
        global druid
        global druid_pages
        global druid_page_end
        global druid_select_page
        global cancel_button_state
        global config

        self.no_logos = 0
        image_path = "/usr/share/pixmaps/debian-logo.xpm"
        if config.has_key("logo"):
            image_path = config["logo"]

        image = None
        try:
            image = gtk.gdk.Pixbuf(image_path)
        except:
            self.no_logos = 1
        if not image:
            self.no_logos = 1

        watermark = None
        if config.has_key("watermark") and not self.no_logos:
            try:
                watermark = gtk.gdk.Pixbuf(config["watermark"])
            except:
                self.no_logos = 1
            if not watermark:
                self.no_logos = 1

        druid = gnome.ui.Druid()
        druid.connect("cancel", canceled)

        druid_page = gnome.ui.DruidPageEdge(gnome.ui.EDGE_START)
        druid_page.connect("next", next_from_start, "")
        druid_page.set_title(start_title)
        druid_page.set_text(start_text)
        if not self.no_logos:
            try:
                druid_page.set_logo(image)
                if watermark:
                    druid_page.set_watermark(watermark)
            except:
                self.no_logos = 1
        druid_page.show()
        druid.append_page(druid_page)

        # First page - allow the user to select config.
        druid_select_page = gnome.ui.DruidPageStandard()
        druid_select_page.set_title("Select Options To Configure")
        if not self.no_logos:
            druid_select_page.set_logo(image)
        druid_select_page.connect("next", next_page, "")
        label = gtk.Label("Please select the options you want to configure:")
        label.set_alignment(0.0, 0.5)
        label.show()
        druid_select_page.vbox.pack_start(label, False, False, 0)

        select_view = gtk.ScrolledWindow()
        select_view.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        select_view.set_property("width-request", 700)
        select_view.set_property("height-request", 400)

        configlet_table = gtk.Table(1, 2, False)
        configlet_table.set_row_spacings(10)
        configlet_table.set_col_spacings(20)

        rows = 1
        configlets = 0
        for cfglet in self:
            if cfglet.get_name() in omitted_configlet_names:
                continue
            elif config.has_key("dont_show_required_configlets") and \
                 cfglet.get_name() in required_configlet_names:
                continue
            elif config.has_key("show_only_listed_configlets") and \
                 cfglet.get_name() not in required_configlet_names and \
                 cfglet.get_name() not in recommended_configlet_names and \
                 cfglet.get_name() not in optional_configlet_names:
                continue

            configlets = configlets + 1
            if configlets > rows:
                rows = rows + 1
                configlet_table.resize(rows, 2)

            #hbox = GtkHBox(FALSE, 0)

            checkbox = gtk.CheckButton()
            checkbox.connect_after("toggled", toggle_configlet,
                                   cfglet.get_name())
            checkbox.show()
            #hbox.pack_start(checkbox, FALSE, FALSE, 0)

            # keep the label width from raging out of control
            label_str = cfglet.get_display_title()
            if cfglet.get_name() in required_configlet_names:
                label_str = label_str + " (mandatory)"
            label = gtk.Label(label_str)
            # XXX: having to do this GtkWidget.set is a bug in the Python GTK+
            # bindings -- FILE A BUG REPORT XXX
            label.set_property("width-request", 200)
            label.set_justify(gtk.JUSTIFY_LEFT)
            label.set_line_wrap(True)
            label.set_alignment(0.0, -1)
            label.show()

            checkbox.add(label)
            #hbox.pack_start(label, True, TRUE, 0)
            #hbox.show()

            if not cfglet.get_name() in optional_configlet_names:
                checkbox.set_active(True)
                picked_configlets.append(cfglet.get_name())

            if cfglet.get_name() in required_configlet_names:
                checkbox.set_sensitive(False)
                label.set_sensitive(False)
            configlet_table.attach(checkbox, 0, 1, rows - 1, rows,
                                   gtk.FILL, 0, 0, 10)

            configlet_desc = gtk.Label(cfglet.get_description())
            configlet_desc.set_usize(450, -1)
            configlet_desc.set_line_wrap(True)
            configlet_desc.set_alignment(0.0, 0.0)
            configlet_desc.show()
            configlet_table.attach(configlet_desc, 1, 2, rows - 1, rows,
                                   gtk.FILL, 0, 0, 10)

        configlet_table.show()

        select_view.add_with_viewport(configlet_table)
        select_view.show()

        druid_select_page.vbox.pack_start(select_view, True, True, 0)
        druid_select_page.show()
        druid.append_page(druid_select_page)

        for cfglet in self:
            pages = cfglet.get_page_names()
            if len(pages) > 1:
                notebook = gtk.Notebook()

                for page in pages:
                    pagetitle = cfglet.get_page_display_title(page)
                    pagewidget = cfglet.get_widget(page)
                    pagewidget.unparent()
                    pagewidget.show()

                    notebook.append_page(pagewidget,
                                         gtk.Label(pagetitle))

                widget = notebook

            else:
                widget = cfglet.get_widget()
                widget.unparent()

            widget.show()
            druid_page = gnome.ui.DruidPageStandard()
            druid_page.set_title(cfglet.get_display_title())
            if not self.no_logos:
                try:
                    druid_page.set_logo(image)
                    if watermark:
                        druid_page.set_watermark(watermark)
                except:
                    self.no_logos = 1
            druid_page.vbox.pack_start(widget, True, True)
            druid_page.show()

            page_id = cfglet.get_name()
            druid_page.connect("next", configlet_done, page_id)
            druid_page.connect("back", back_page, page_id)
            druid_pages.append((page_id, druid_page))

            druid.append_page(druid_page)

        druid_page_end = gnome.ui.DruidPageEdge(gnome.ui.EDGE_FINISH)
        druid_page_end.set_title(end_title)
        druid_page_end.set_text(end_text)
        if not self.no_logos:
            try:
                druid_page_end.set_logo(image)
                if watermark:
                    druid_page_end.set_watermark(watermark)
            except:
                pass
        druid_page_end.connect("back", back_page, "")
        druid_page_end.connect("finish", finished)
        druid_page_end.show()
        druid.append_page(druid_page_end)

        druid.set_buttons_sensitive(False, True, cancel_button_state, False)
        druid.show()

        window = gtk.Window()
        window.set_title("System Configuration")
        window.add(druid)
        window.connect("delete_event", canceled)
        window.set_position(gtk.WIN_POS_CENTER)
        window.show()

        gtk.mainloop()

        self.shown = True

    def debconf(self):
        global not_validated
        results = []
        for cfglet in self.configlets:
            try:
                if not not_validated.has_key(cfglet.get_name()):
                    results = results + cfglet.report_debconf()
            except:
                debug("configlet %s failed to report debconf info"
                      % (cfglet.get_name(),))
                if os.environ.has_key("DEBUG"):
                    configlet.print_traceback()

        return results

# Main program.

# Load configuration file, if it exists.
if os.path.exists(config_filename):
    config_file = open(config_filename)

    for line in config_file.readlines():
        line_strip = string.strip(line)
        if len(line_strip) < 1:
            continue
        if line_strip[0] == "#":
            continue

        config_items = string.split(line_strip, None, 1)
        if len(config_items) == 1:
            config[config_items[0]] = None
        elif len(config_items) == 2:
            config[config_items[0]] = config_items[1]

    config_file.close()

# Apply some of the configuration information.
if config.has_key("debug"):
    os.environ["DEBUG"] = "true"

if config.has_key("cancelable"):
    cancel_button_state = True

if config.has_key("required_configlets"):
    required_configlet_names = string.split(config["required_configlets"])

if config.has_key("standard_configlets"):
    recommended_configlet_names = string.split(config["standard_configlets"])

if config.has_key("unchecked_configlets"):
    optional_configlet_names = string.split(config["unchecked_configlets"])

if config.has_key("omit_configlets"):
    omitted_configlet_names = string.split(config["omit_configlets"])

# Load gtkrc.  If no gtkrc is given in the config file, load GTK2
# default engine so checkbuttons and other widgets actually make
# sense visually.
if config.has_key("gtkrc"):
    if os.path.exists(config["gtkrc"]):
        gtk.rc_parse(config["gtkrc"])
elif os.path.exists(default_rc):
    gtk.rc_parse(default_rc)

# I'm not in a mood to hook this up with getopt likeshould have been
# done long ago, so i'm just bumping the index to 2 to account for
# --sm-disable.
configlet_dir = "/usr/share/configlets"
if len(sys.argv) > 2:
    configlet_dir = sys.argv[2]

cfggroup = DruidConfigGroup(configlet_dir)

cfggroup.gnome_setup()
cfggroup.load_all_debconf()

if commit_only:
    completed = True
else:
    cfggroup.show()

if ask_only:
    sys.exit(0)

if completed:
    # Get list of packages to configure.
    finished_configlets = []
    finished_configlet_data = {}
    for cfglet in cfggroup:
        configlet_name = cfglet.get_name()
        if configlet_name in picked_configlets:
            try:
                finished_configlet_data[configlet_name] = cfglet.report_debconf()
                finished_configlets.append(configlet_name)
            except:
                pass

    # Set up configure status window.
    status_win_vbox = GtkVBox(False, 5)
    status_win_vbox.show()

    status_label = GtkLabel("Configuring package:                                   ")
    status_label.set_justify(JUSTIFY_LEFT)
    status_label.show()
    status_win_vbox.pack_start(status_label)

    status_bar = GtkProgressBar()
    status_bar_adj = GtkAdjustment(0, 0, len(finished_configlets) + 1, 1, 1, 1)
    status_bar.set_adjustment(status_bar_adj)
    status_bar.show()
    status_win_vbox.pack_start(status_bar)

    gtk._root_window().set_cursor(gtk.cursor_new(GDK.WATCH))

    status_win = GtkWindow(gtk.WINDOW_TOPLEVEL, "Configuring Packages")
    status_win.add(status_win_vbox)
    status_win.set_position(WIN_POS_CENTER)
    status_win.show()

    while events_pending():
        mainiteration()

    # Configure each package, reporting on status.
    for configlet_name in finished_configlets:
        status_label.set_text("Configuring: %s" % configlet_name)
        while events_pending():
            mainiteration()

        cfggroup.get_debconf().set(finished_configlet_data[configlet_name])

        status_bar_adj.set_value(status_bar_adj.value + 1)
        while events_pending():
            mainiteration()

    status_label.set_text("Committing changes...")
    while events_pending():
        mainiteration()

    cfggroup.get_debconf().commit()

    status_bar_adj.set_value(status_bar_adj.value + 1)
    while events_pending():
        mainiteration()

    # Done - get rid of status window.
    gtk._root_window().set_cursor(gtk.cursor_new(GDK.LEFT_PTR))

    status_win.hide()
    while events_pending():
        mainiteration()

# vim:ai:et:sts=4:sw=4:tw=0:
