Source

vasm / src / vasm / ui / gtk2 / modules / networking.py

Full commit
#!/usr/bin/env python

#    This file is part of VASM.
#
#    VASM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    VASM 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 VASM.  If not, see <http://www.gnu.org/licenses/>.

__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]

import gtk
import os
from vasm.ui.gtk2.support import widgets, dialogs
from vasm.backend import networking
from vasm.backend.utils import _, get_popen, datapath
import logging

logger = logging.getLogger('vasm')

class NetworkingUI(widgets.VasmModule):
    def __init__(self, parent):
        self.netmodel = networking.NetConfModel()
        widgets.VasmModule.__init__(self, parent,
            _("Networking"), _("Network settings"))
        
        netchoice = widgets.OptionsBox(
            label=_("Select how this computer networks."),
            options = (
                ("None", networking.NET_NONE),
                ("Automatic", networking.NET_DAEMON)),
                #("Manual", networking.NET_INET)),
            orientation=widgets.vertical)
        if not self.netmodel.hostname: self.netmodel.hostname = 'vector.linux.vnet'
        netchoice.add_observer(self.netchoice_observer)
        hostnamebox = widgets.TextEntry("Hostname", entry_fill_box=False)
        hostnamebox.set_text(self.netmodel.hostname)
        
        # daemon mode
        self.frmdaemon = gtk.Frame(label=_("Preferred Network Managing Application"))
        _daemons = [("None", networking.NET_INET)]
        for app in self.netmodel.listNetworkingDaemons():
            _daemons.append((app.name, app.name))
        dpicker = widgets.OptionsBox(
            label = None,
            options = _daemons,
            orientation = widgets.horizontal)
        dpicker.add_observer(self.daemon_observer)
        self.dpicker = dpicker
        self.frmdaemon.add(dpicker)
        # inet mode
        self.inetpicker = gtk.Frame(
            label = _("Automatic networking - INET based")
            )
        hbox = gtk.VBox()
        inets = self.netmodel.listNetworkAdapters()
        self.nicpicker = widgets.OptionsBox(
            label = _("Network Adaptor"),
            orientation=widgets.horizontal,
            options = [(nic.devname, nic.devname) for nic in inets])
        hbox.pack_start(self.nicpicker, True, True, 2)
        self.inetpicker.add(hbox)
        self.nmethodpicker = widgets.OptionsBox(
            label = _("Networking Method"),
            options = [("DHCP", networking.NET_INET_DHCP), 
                        ("STATIC", networking.NET_INET_STATIC)],
            orientation = widgets.horizontal)
        self.nmethodpicker.add_observer(self.inet_adaptor_observer)
        hbox.pack_start(self.nmethodpicker, True, True, 2)
        self.staticsettings = gtk.Frame(_("Static networking settings."))
        hbox.pack_start(self.staticsettings, False, True, 4)
        sthbox = gtk.VBox()
        sthbox.pack_start(gtk.Label())
        self.entry_IP = widgets.TextEntry(_("IP Address") + ":", 
            entry_fill_box=False,
            label_width_chars=10)
        sthbox.pack_start(self.entry_IP, False, True, 2)
        self.entry_GW = widgets.TextEntry(_("Gateway") + ":",
            entry_fill_box=False, label_width_chars=10)
        self.entry_NM = widgets.TextEntry(_("Netmask") + ":",
            entry_fill_box=False, label_width_chars=10)
        sthbox.pack_start(self.entry_GW, False, True, 2)
        sthbox.pack_start(self.entry_NM, False, True, 2)
        self.staticsettings.add(sthbox)
        self.staticsettings.set_property('sensitive', False)
        
        
        # pack the widgets on the module
        self.pack_start(hostnamebox, False, True, 4)
        self.pack_start(netchoice, False, True, 4)
        self.pack_start(self.frmdaemon, False, True, 4)
        self.pack_start(self.inetpicker, False, True, 4)
        self.add_button((gtk.Button(stock=gtk.STOCK_CLOSE), self.close_module))
        self.add_button((gtk.Button(stock=gtk.STOCK_APPLY),
            self._save_changes))
        
        # Read the current status
        method, _nobject = self.netmodel.getCurrentMethod()
        for wid in netchoice._options:
            if wid.user_data == method:
                wid.set_active(True)

    def _save_changes(self, caller=None):
        print "Saaving"
    
    def _set_ui_for_no_network(self, caller=None):
        self.frmdaemon.set_property('sensitive', False)
        self.inetpicker.set_property('sensitive', False)
    
    def _set_ui_for_daemon_networking(self, caller=None):
        self.frmdaemon.set_property('sensitive', True)
        self.inetpicker.set_property('sensitive', False)
    
    def _set_ui_for_inet_networking(self, caller=None):
        self.inetpicker.set_property('sensitive', True)
        method, adaptor = self.netmodel.getCurrentMethod()
        for nic in self.nicpicker._options:
            if nic.user_data == adaptor:
                # toggle the active NIC
                nic.set_active(True)
                return self._display_nic_settings(nic)                

    def _display_nic_settings(self, nic):
        """ Display the settings details on how this nic is setup """
        # See if we are using dhcp
        dhcp = nic.inet.config['DHCP'] == 'yes'
        self.staticsettings.set_property('sensitive', not dhcp)
        # Display the rest of the information here.
        # If it is using dhcp, read the live data on the nic
    
        if dhcp:
            _ip = nic.livedata['IPADDR']
            _gw = nic.livedata['GATEWAY']
            _nm = nic.livedata['NETMASK']
            _bc = nic.livedata['BROADCAST']
        else:
            _ip = nic.inet.config['IPADDR']
            _gw = nic.inet.config['GATEWAY']
            _nm = nic.inet.config['NETMASK']
            _bc = nic.inet.config['BROADCAST']
        self.entry_IP.set_text(_ip)
        self.entry_GW.set_text(_gw)
        self.entry_NM.set_text(_nm)
    
    def daemon_observer(self, data):
        """ Trigger events when the networking daemon is selected """
        seld = data.user_data
        if seld == networking.NET_INET:
            self._set_ui_for_inet_networking()
            # get the active nic
            niclist = self.netmodel.getActiveNics()
            # FIXME:  ^^ will return more than one value if more than one
            #           rc.inet script is set +x
            #           We use the first one here.
            if len(niclist) == 0: return
            anic = niclist[0]
            return self._display_nic_settings(anic)
                
        else:
            self._set_ui_for_daemon_networking()

    def inet_adaptor_observer(self, data):
        nmethod = data.user_data
        if nmethod == networking.NET_INET_DHCP:
            self.staticsettings.set_property('sensitive', False)
        else:
            self.staticsettings.set_property('sensitive', True)
        #print data.user_data
    
    def netchoice_observer(self, data):
        nval =  data.user_data
        self.networkingmethod = nval
        if nval == networking.NET_NONE:
            return self._set_ui_for_no_network()
        elif nval == networking.NET_DAEMON:
            self._set_ui_for_daemon_networking()
            method, _object = self.netmodel.getCurrentMethod()
            for wid in self.dpicker._options:
                # Select the current selected object from the method
                if wid.user_data == _object:
                    wid.set_active(True)
                    return
                if method == networking.NET_INET:
                    active_nic = self.netmodel.getNic(_object)
                    if active_nic.config['DHCP'] == 'yes':
                        for radio in self.nmethodpicker._options:
                            if radio.user_data == networking.NET_INET_DHCP:
                                return radio.set_active(True)
                        METHOD = networking.NET_INET_DHCP
                    else:
                        for radio in self.netchoice_observer._options:
                            if radio.user_data == networking.NET_INET_STATIC:
                                return radio.set_active(True)
                    # Set the radio to either dhcp or static """

    
def __vasm_test__():
    return True
    return os.geteuid() == 0

def __run__(parent=None):
    w = NetworkingUI(parent)
    return w.launch()

VASM_CATEGORY = _("Networking")
VASM_LABEL=_("Networking")
VASM_ICON = os.path.join(datapath, 'category_networking.png')