# Copyright (c) 2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program 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 2 of the License, or (at your option) any later
# version.
#
# 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.

"""
Connection window is the dialog window proposed when
gui part of Horn is launched. Three possibilities are offered to user
1. connect to Narval
2. get local data
3. do nothing
"""

__revision__ = "$Id: ConnectionWindow.py,v 1.23 2002/01/09 16:24:12 zoe Exp $"

from HornActioner import RPC_PROTO
from DialogWindow import *
from narval import config

class ConnectionWindow(DialogWindow):
    """
    ConnectionWindow class, parent is DialogWindow
    """

    def __init__(self):
        #print unicode_to_gtk(_("Connection Window"))
        DialogWindow.__init__(self,unicode_to_gtk(_("Connection Window")))
        self.connect("delete_event",mainquit)
        
        # connect to narval part
        hbox1 = GtkHBox()
        hostentry = GtkEntry()
        import socket
        hostentry.set_text(socket.gethostname())
        label = GtkLabel(unicode_to_gtk(_("Host : ")))
        hbox1.pack_start(label,expand=FALSE)
        hbox1.pack_start(hostentry)
        portentry = GtkEntry()
        portentry.set_text("8787")
        portentry.set_usize(50,0)
        label = GtkLabel(unicode_to_gtk(_(" Port : ")))
        hbox1.pack_start(label,expand=FALSE)
        hbox1.pack_start(portentry)
        # choose between pyro and xmlrpc
        typeentry = GtkCombo()
        self._rpc_protos = ['pyro', 'xmlrpc']
        typeentry.entry.set_editable(FALSE)
        typeentry.set_popdown_strings(self._rpc_protos)
        typeentry.entry.set_text(self._rpc_protos[0])
        typeentry.entry.set_usize(50,0)
        typeentry.disable_activate()

        label = GtkLabel(unicode_to_gtk(_(" Type : ")))
        hbox1.pack_start(label,expand=FALSE)
        hbox1.pack_start(typeentry)
        

        narval_button = GtkRadioButton()
        narval_button.add(hbox1)

        self.append_dialog(narval_button)

        # get local datas
        hbox2 = GtkHBox()
        label = GtkLabel(unicode_to_gtk(_("Get local data, home : ")))
        hbox2.pack_start(label)
        local_button = GtkRadioButton(narval_button)
        local_button.add(hbox2)
        homeentry = GtkEntry()
        from narval.config import get_home
        homeentry.set_text(get_home())
        homeentry.set_sensitive(FALSE)
        hbox2.pack_start(homeentry)

        self.append_dialog(local_button)

        # do nothing
        hbox3 = GtkHBox()
        label = GtkLabel(unicode_to_gtk(_("Do nothing at launch.")))
        hbox3.pack_start(label,expand=FALSE)
        nothing_button = GtkRadioButton(narval_button)
        nothing_button.add(hbox3)

        self.append_dialog(nothing_button)

        narval_button.connect("toggled",self._narval_button_toggled,
                              local_button,hostentry,portentry,homeentry,
                              typeentry.entry)
        local_button.connect("toggled",self._local_button_toggled,
                             nothing_button,hostentry,portentry,homeentry,
                              typeentry.entry)
        nothing_button.connect("toggled",self._nothing_button_toggled,
                               nothing_button,hostentry,portentry,homeentry,
                              typeentry.entry)
        narval_button.connect("button-press-event",self._button_clicked,narval_button,
                              local_button,hostentry,portentry,homeentry,
                              typeentry.entry)
        local_button.connect("button-press-event",self._button_clicked,narval_button,
                             local_button,hostentry,portentry,homeentry,
                              typeentry.entry)
        nothing_button.connect("button-press-event",self._button_clicked,narval_button,
                                    local_button,hostentry,portentry,homeentry,
                              typeentry.entry)

        self.ok_button.show()
        self.ok_button.set_flags(CAN_DEFAULT|HAS_DEFAULT)
        self.ok_button.connect("clicked",self._ok_clicked,narval_button,
                               local_button,hostentry,portentry,homeentry,
                              typeentry.entry)

        self.cancel_button.show()
        self.cancel_button.connect("clicked",mainquit)
        
    def _narval_button_toggled(self,*args):
        if args[0].get_active() == 1:
            args[2].set_sensitive(TRUE)
            args[3].set_sensitive(TRUE)
            args[4].set_sensitive(FALSE)
            args[5].set_sensitive(TRUE)
        elif args[1].get_active() == 1:
            args[2].set_sensitive(FALSE)
            args[3].set_sensitive(FALSE)
            args[4].set_sensitive(TRUE)
            args[5].set_sensitive(FALSE)
        else:
            args[2].set_sensitive(FALSE)
            args[3].set_sensitive(FALSE)
            args[4].set_sensitive(FALSE)
            args[5].set_sensitive(FALSE)

    def _local_button_toggled(self,*args):
        if args[0].get_active() ==1:
            args[2].set_sensitive(FALSE)
            args[3].set_sensitive(FALSE)
            args[4].set_sensitive(TRUE)
            args[5].set_sensitive(FALSE)
        elif args[1].get_active() ==1:
            args[2].set_sensitive(TRUE)
            args[3].set_sensitive(TRUE)
            args[4].set_sensitive(FALSE)
            args[5].set_sensitive(TRUE)
        else:
            args[2].set_sensitive(FALSE)
            args[3].set_sensitive(FALSE)
            args[4].set_sensitive(FALSE)
            args[5].set_sensitive(FALSE)

    def _nothing_button_toggled(self,*args):
        args[2].set_sensitive(FALSE)
        args[3].set_sensitive(FALSE)
        args[4].set_sensitive(FALSE)
        args[5].set_sensitive(FALSE)
        
    def _button_clicked(self, *args):
        if args[1].button == 1 and args[1].type == GDK._2BUTTON_PRESS:
            apply(self._ok_clicked, args[1:])
            
    def _ok_clicked(self,*args):  
        host = args[3].get_text()
        port = args[4].get_text()
        home = args[5].get_text()
        type = args[6].get_text()
        self.destroy()

        from HornGuiController import HornGuiController
        if args[1].get_active() == 1:
            # start HornGui with connection
            HornGuiController().start(host, int(port), None, type)
        elif args[2].get_active() == 1:
            from narval.bibal import check_for_home
            from narval import config
            old_home = config.get_home()
            if not config.set_home(home):
	        if old_home == home:
		    check_for_home()
		    HornGuiController().start(None,None,home, None)
		else:
                    HornGuiController()._handle_wrong_home(None,old_home)
                return
            else:
                from narval.gui import init_gettext
                init_gettext()
                # start HornGui with local data
                HornGuiController().start(None,None,home,None)
        else:
            HornGuiController().start(None,None,None,None)


