########################################################################
#
#  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 pygtk
pygtk.require("2.0")
import gtk, gobject

from page import Page
from deviceform import DeviceFormControls
import lang
from page_selectnetwork import PageSelectNetwork
import devices
import labeltext

class PageSelectDevice(Page):

    title = 'seldev_title'
    number = 4

    dev1 = None
    dev2 = None

    config = PageSelectNetwork.CONFIG_SIMPLE
    focus_label = None

    def __init__(self, parent):
        Page.__init__(self, parent)

        self.widget = gtk.Alignment()
        self.widget.set_padding(20, 20, 20, 0)
        self.vbox = gtk.VBox()
        self.vbox.set_spacing(20)

        self.widget.add(self.vbox)
        self.widget.show()
        self.reset_form()


    def fill_vbox(self):
        if not devices.devs: return
        vbox = self.vbox

        lb = labeltext.LabelText('select_device', padding=0)
        vbox.pack_start(lb, False, False)


        if self.config == PageSelectNetwork.CONFIG_SIMPLE:

            self.dev1 = DeviceFormControls(self)
            self.dev2 = None
            vbox.pack_start(self.dev1.vbox, False, False)
        else:
            lb_dev1 = gtk.Label()
            lang.bind(lb_dev1, 'select_device1')
            lb_dev1.set_alignment(0, 0)
            lb_dev2 = gtk.Label()
            lang.bind(lb_dev2, 'select_device2')
            lb_dev2.set_alignment(0, 0)
            self.dev1 = DeviceFormControls(self)
            self.dev2 = DeviceFormControls(self)
            vbox.pack_start(lb_dev1, False, False)
            vbox.pack_start(self.dev1.vbox, False, False)
            vbox.pack_start(lb_dev2, False, False)
            vbox.pack_start(self.dev2.vbox, False, False)

        self.on_form_change()


    def reset_form(self):
        vbox = self.vbox
        children = vbox.get_children()
        for child in children: vbox.remove(child)
        self.fill_vbox()

        vbox.show_all()
        if self.dev1: self.dev1.hide_some()
        if self.dev2: self.dev2.hide_some()

    def on_before_show(self):
        
        current_config = self.parent.pages[self.parent.PAGE_SELECTNETWORK].get_config()
        if current_config != self.config:
                self.config = current_config
                self.parent.reset_selectdevice()

        self.dev1.grab_focus()
        self.parent.activate_testsprogress()

    def on_form_change(self):
        complete = True
        if self.dev1 and not self.dev1.get_form_complete(): complete = False
        if self.dev2 and not self.dev2.get_form_complete(): complete = False
        self.set_page_complete(complete)
        
