#!/usr/bin/python
# -*- coding: utf-8 -*-
########################################################################
#
#  Copyright (C) 2009, CZ.NIC, z.s.p.o.
# 
#  This file is part of dnstester.
#
#  dnstester 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 3 of the License, or
#  (at your option) any later version.
#
#  dnstester 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, see <http://www.gnu.org/licenses/>.
#
########################################################################

import sys
import os

if sys.platform == 'win32':
    os.environ['GTK2_RC_FILES'] = os.path.join(os.getcwd(), "share\\themes\\clearlooks\\gtk-2.0\\gtkrc")
    #print os.environ['GTK2_RC_FILES']

import pygtk
pygtk.require("2.0")
import gtk, gobject
import time
gtk.gdk.threads_init()

import dnstests
import messages, lang
import config
import upload
import combo    
import devices
from devices import devs
import images

if sys.platform == 'darwin':
    import darwin

from page_welcome import PageWelcome
from page_devupdateprogress import PageDevUpdateProgress
from page_selectnetwork import PageSelectNetwork
from page_selectdevice import PageSelectDevice
from page_testsprogress import PageTestsProgress
from page_report import PageReport
from page_uploadprogress import PageUploadProgress
from page_summary import PageSummary



#
# main class
#
class Wizard(gtk.Assistant):

    PAGE_WELCOME = 0
    PAGE_DEVUPDATEPROGRESS = 1
    PAGE_SELECTNETWORK = 2
    PAGE_SELECTDEVICE = 3
    PAGE_TESTSPROGRESS = 4
    PAGE_REPORT = 5
    PAGE_UPLOADPROGRESS = 6
    PAGE_SUMMARY = 7

    def get_current_page_object(self):
        index = self.get_current_page()
        return self.pages[index]

    def set_tester_title(self):
        self.set_title("%s (%d %s 8)" % (lang.get_string('main_window_title'),
            self.get_current_page_object().number, lang.get_string('of')))

    def setup_main_window(self):
        self.set_default_size(600,370)
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_border_width(10)
        self.connect('close', self.on_close)
        self.connect('cancel', self.on_cancel)
        self.connect('prepare', self.on_prepare)
        self.set_icon_from_file(images.fullname('icon16.png'))

        self.pages = [
            PageWelcome(self),
            PageDevUpdateProgress(self),
            PageSelectNetwork(self),
            PageSelectDevice(self),
            PageTestsProgress(self),
            PageReport(self),
            PageUploadProgress(self),
            PageSummary(self),
            ]

        for p in self.pages:
            self.append_page(p.get_widget())
            p.update_parent()

    def __init__(self):
        gtk.Assistant.__init__(self)
        self.setup_main_window()

    def reset_selectdevice(self):
        p = self.pages[self.PAGE_SELECTDEVICE]
        p.reset_form()

    def next(self):
        self.set_current_page(self.get_current_page() + 1)

    def activate_testsprogress(self):
        p = self.pages[self.PAGE_TESTSPROGRESS]
        p.activate()

    def deactivate_testsprogress(self):
        p = self.pages[self.PAGE_TESTSPROGRESS]
        p.deactivate()

    def get_upload_result(self):
        p = self.pages[self.PAGE_UPLOADPROGRESS]
        return p.result
        
    def prevent_last_button(self):
        p = self.get_nth_page(self.PAGE_TESTSPROGRESS)
        self.set_page_complete(p, False)
        p = self.get_nth_page(self.PAGE_DEVUPDATEPROGRESS)
        self.set_page_complete(p, False)
        
    ###################################
    # Events
    ###################################

    def on_cancel(self, assistant):
        gtk.main_quit()

    def on_close(self, assistant):
        self.emit('delete_event', gtk.gdk.Event(gtk.gdk.NOTHING))

    def on_prepare(self, assistant, widget):
        p = self.get_current_page_object()
        p.on_before_show()
        self.set_tester_title()
        if p.focus_label: self.set_action_focus(p.focus_label)

    def cb_set_fwd_focus(self, widget, data):
        if isinstance(widget, gtk.HBox):
            children = widget.get_children()
            for c in children:
                if isinstance(c, gtk.Button):
                    if c.get_label() == data: c.grab_focus()

    def set_action_focus(self, label):
        self.forall(self.cb_set_fwd_focus, label)

    def enable_back_button_cb(self, widget, data):
        if isinstance(widget, gtk.HBox):
            children = widget.get_children()
            for c in children:
                if isinstance(c, gtk.Button):
                    if c.get_label() == 'gtk-go-back': 
                        c.set_sensitive(data)

    def enable_back_button(self, enable):
        self.forall(self.enable_back_button_cb, enable)



#
# starting point
#
if __name__ == "__main__":

    if '--base-url' in sys.argv:
        index = sys.argv.index('--base-url')
        if index + 1 < len(sys.argv):
            config.base_url = sys.argv[index + 1]

    if sys.platform == 'darwin': darwin.macosx_init()
    lang.set_default_language()
    gtk.gdk.threads_enter()
    wizard = Wizard()

    wizard.show()
    wizard.set_keep_above(True)
    wizard.set_keep_above(False)
    gtk.main()
    gtk.gdk.threads_leave()
    sys.exit()


