def row_pwd_entry(self, label_name, table=None):
        label = self.__label(label_name)
        entry = PasswordEntry()
        entry.set_size(self.right_width, WIDGET_HEIGHT)
        show_key = CheckButton(_("Show password"), padding_x=0)
        show_key.connect("toggled", lambda w: show_key.show_password(w.get_active()))
        self._wrap_align((label, entry), table)
        self._wrap_align((None, show_key), table)

        return entry
    def row_pwd_entry(self, label_name, table=None):
        label = self.__label(label_name)
        entry = PasswordEntry()
        entry.set_size(self.right_width, WIDGET_HEIGHT)
        show_key = CheckButton(_("Show password"), padding_x=0)
        show_key.connect("toggled",
                         lambda w: show_key.show_password(w.get_active()))
        self._wrap_align((label, entry), table)
        self._wrap_align((None, show_key), table)

        return entry
Exemplo n.º 3
0
    volume_frame.add(volume_button)
    tab_1_box.pack_start(volume_frame, False, False)

    # Add entry widget.
    entry_box = gtk.HBox(spacing=10)

    text_entry = dtk.ui.entry.TextEntry()

    text_entry.set_size(100, 22)
    input_entry = InputEntry()
    input_entry.entry.place_holder = "哈哈中"
    input_entry.set_size(100, 22)
    shortcust_entry = ShortcutKeyEntry()
    shortcust_entry.set_size(100, 22)
    password_entry = PasswordEntry()
    password_entry.set_size(100, 22)

    combo_box = ComboBox([("测试测试测试%d" % i, i) for i in range(10)],
                         fixed_width=100)

    spin_box = SpinBox(3000, 0, 5000, 100)

    # mac_entry = MACEntry()
    mac_entry = IPV4Entry()
    mac_entry.connect("changed", print_address)
    # mac_entry = IPV4Entry()
    # mac_entry.set_ip("255.255.255.andy")
    # mac_entry.set_ip("192.168..12")
    # mac_entry.set_ip("192.168..12")
    # mac_entry.set_ip("...")
Exemplo n.º 4
0
class Security(gtk.VBox):
    ENTRY_WIDTH = 222

    def __init__(self,
                 connection,
                 set_button_cb,
                 need_ssid=False,
                 settings_obj=None):
        gtk.VBox.__init__(self)
        self.tab_name = _("Security")
        self.connection = connection
        self.set_button = set_button_cb
        # 新增settings_obj变量,用于访问shared_methods.Settings对象
        self.settings_obj = settings_obj
        self.settings_obj.initial_lock = True

        self.need_ssid = need_ssid
        self.presave_index = None

        if self.need_ssid:
            log.info("enter hidden network settings")
            self.add_ssid_entry()

        if self.connection.get_setting(
                "802-11-wireless").security == "802-11-wireless-security":
            self.has_security = True
            self.setting = self.connection.get_setting(
                "802-11-wireless-security")
        else:
            self.has_security = False
        self.security_label = Label(_("Security:"),
                                    enable_select=False,
                                    enable_double_click=False)
        self.key_label = Label(_("Key:"),
                               enable_select=False,
                               enable_double_click=False)
        self.wep_index_label = Label(_("Wep index:"),
                                     enable_select=False,
                                     enable_double_click=False)

        self.auth_label = Label(_("Authentication:"),
                                enable_select=False,
                                enable_double_click=False)
        self.password_label = Label(_("Password:"******"None"), None),
                           (_("WEP (Hex or ASCII)"), "none"),
                           (_("WEP 104/128-bit Passphrase"), "none"),
                           (_("WPA WPA2 Personal"), "wpa-psk")]
        #entry_item = map(lambda l: (l[1],l[0]), enumerate(self.encry_list))
        self.security_combo = ComboBox(self.encry_list,
                                       fixed_width=self.ENTRY_WIDTH)
        #self.security_combo.set_size_request(self.ENTRY_WIDTH, 22)

        self.key_entry = PasswordEntry()
        self.password_entry = PasswordEntry()
        self.show_key_check = CheckButton(_("Show password"), padding_x=0)
        self.show_key_check.connect("toggled", self.show_key_check_button_cb)
        self.wep_index_spin = SpinBox(0, 1, 4, 1, self.ENTRY_WIDTH)
        self.auth_combo = ComboBox([(_("Shared Key"), "shared"),
                                    (_("Open System"), "open")],
                                   fixed_width=self.ENTRY_WIDTH)

        ## advance button
        self.align = gtk.Alignment(0, 1.0, 0, 0)
        self.align.set_padding(0, 0, 376, 0)
        self.align.set_size_request(-1, 30)
        self.button = Button(_("Advanced"))
        self.align.add(self.button)

        ## Create table
        self.table = gtk.Table(5, 4)
        #TODO UI change
        label_list = [
            "security_label", "key_label", "wep_index_label", "auth_label",
            "password_label"
        ]
        widget_list = [
            "password_entry", "key_entry", "wep_index_spin", "auth_combo",
            "security_combo"
        ]
        for label in label_list:
            l = getattr(self, label)
            l.set_can_focus(False)
            align = style.wrap_with_align(l, width=210)
            setattr(self, label + "_align", align)

        for w in widget_list:
            l = getattr(self, w)
            align = style.wrap_with_align(l, align="left")
            setattr(self, w + "_align", align)

        self.show_key_check_align = style.wrap_with_align(self.show_key_check,
                                                          align="left")

        self.reset(self.has_security)
        self.security_combo.connect("item-selected", self.change_encry_type)
        self.key_entry.entry.connect("changed", self.save_wep_pwd)
        self.password_entry.entry.connect("changed", self.save_wpa_pwd)
        self.wep_index_spin.connect("value-changed", self.wep_index_spin_cb)
        self.auth_combo.connect("item-selected", self.save_auth_cb)

        style.set_table(self.table)
        table_align = gtk.Alignment(0, 0, 0, 0)
        table_align.add(self.table)
        style.draw_background_color(self)
        width, height = self.ENTRY_WIDTH, 22
        self.key_entry.set_size(width, height)
        self.password_entry.set_size(width, height)
        self.wep_index_spin.set_size_request(width, height)
        self.auth_combo.set_size_request(width, height)
        self.security_combo.set_size_request(width, height)
        self.pack_start(table_align, False, False)
        self.pack_start(self.align, False, False, 0)
        self.settings_obj.initial_lock = False

    def add_ssid_entry(self):
        self.wireless = self.connection.get_setting("802-11-wireless")
        self.ssid_label = Label(_("SSID:"),
                                enable_select=False,
                                enable_double_click=False)
        self.ssid_label_align = style.wrap_with_align(self.ssid_label,
                                                      width=210)
        self.ssid_entry = InputEntry()
        self.ssid_entry.set_size(self.ENTRY_WIDTH, 22)
        self.ssid_entry_align = style.wrap_with_align(self.ssid_entry,
                                                      align="left")
        self.ssid_entry.entry.connect("changed", self.set_ssid)
        self.ssid_entry.set_text(self.wireless.ssid)

        #self.add(align)

    def set_ssid(self, widget, content):
        self.wireless.ssid = content
        check_settings(self.connection, None)

    def advand_cb(self, widget):
        pass

    def reset(self, security=True):
        ## Add security
        container_remove_all(self.table)
        if self.need_ssid:
            self.table.attach(self.ssid_label_align, 0, 1, 0, 1)
            self.table.attach(self.ssid_entry_align, 1, 4, 0, 1)

        self.table.resize(2, 4)
        self.table.attach(self.security_label_align, 0, 1, 1, 2)
        self.table.attach(self.security_combo_align, 1, 4, 1, 2)

        if not security:
            self.presave_index = self.security_combo.get_select_index()
            return

        keys = [None, "none", "none", "wpa-psk"]

        self.key_mgmt = self.setting.key_mgmt
        if self.key_mgmt == "none":
            key_type = self.setting.wep_key_type
            self.security_combo.set_select_index(key_type)
        else:
            self.security_combo.set_select_index(keys.index(self.key_mgmt))

        if not self.security_combo.get_current_item()[1] == None:
            try:
                (setting_name, method) = self.connection.guess_secret_info()
                secret = nm_module.secret_agent.agent_get_secrets(
                    self.connection.object_path, setting_name, method)
                if secret == None:
                    secret = ''
                log.debug("get secret", setting_name, method, "secret")
            except Exception, e:
                log.error("get secret error", e)
                secret = ""

            if self.security_combo.get_current_item()[1] == "wpa-psk":
                self.table.resize(4, 4)
                self.table.attach(self.password_label_align, 0, 1, 2, 3)
                self.table.attach(self.password_entry_align, 1, 4, 2, 3)
                self.table.attach(self.show_key_check_align, 1, 4, 3, 4)

                self.password_entry.entry.set_text(secret)
                if secret:
                    #Dispatcher.set_button("save", True)
                    ###########
                    self.settings_obj.wlan_encry_is_valid = True
                    self.settings_obj.set_button("save", True)
                self.setting.psk = secret

            elif self.security_combo.get_current_item()[1] == "none":
                self.table.resize(6, 4)
                # Add Key
                self.table.attach(self.key_label_align, 0, 1, 2, 3)
                self.table.attach(self.key_entry_align, 1, 4, 2, 3)
                self.table.attach(self.show_key_check_align, 1, 4, 3, 4)
                # Add wep index
                self.table.attach(self.wep_index_label_align, 0, 1, 4, 5)
                self.table.attach(self.wep_index_spin_align, 1, 4, 4, 5)
                # Add Auth
                self.table.attach(self.auth_label_align, 0, 1, 5, 6)
                self.table.attach(self.auth_combo_align, 1, 4, 5, 6)

                # Retrieve wep properties
                try:
                    index = self.setting.wep_tx_keyidx
                    auth = self.setting.auth_alg
                    log.debug(auth, index)
                    self.auth_combo.set_select_index(["shared",
                                                      "open"].index(auth))
                except Exception, e:
                    log.error(e)
                    index = 0
                    auth = "shared"
                # must convert long int to int
                index = int(index)

                #init_key = True
                #if isinstance(self.connection, NMRemoteConnection):
                #init_setting = self.connection.get_setting("802-11-wireless-security")
                #if init_setting.wep_key_type != self.setting.wep_key_type:
                #init_key = False

                #if init_key:
                self.key_entry.entry.set_text(secret)
                self.setting.set_wep_key(index, secret)
                self.wep_index_spin.set_value(index + 1)
                self.auth_combo.set_select_index(["shared",
                                                  "open"].index(auth))
class AskPasswordDialog(DialogBox):
    '''
    Simple input dialog.
    @undocumented: click_confirm_button
    @undocumented: click_cancel_button
    '''
    
    def __init__(self,
                 connection,
                 ssid,
                 key_mgmt=None,
                 default_width=330,
                 default_height=120,
                 confirm_callback=None, 
                 cancel_callback=None,
                 ):
        '''
        Initialize InputDialog class.
        
        @param title: Input dialog title.
        @param init_text: Initialize input text.
        @param default_width: Width of dialog, default is 330 pixel.
        @param default_height: Height of dialog, default is 330 pixel.
        @param confirm_callback: Callback when user click confirm button, this callback accept one argument that return by user input text.
        @param cancel_callback: Callback when user click cancel button, this callback not need argument.
        '''
        # Init.
        #DialogBox.__init__(self, _("Set password"), default_width, default_height, DIALOG_MASK_SINGLE_PAGE)
        DialogBox.__init__(self,
                           _("Please input password for %s") % ssid,
                           default_width,
                           default_height,
                           DIALOG_MASK_SINGLE_PAGE)

        #self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_NORMAL)
        self.confirm_callback = confirm_callback
        self.cancel_callback = cancel_callback
    
        self.connection = connection
        
        self.hint_align = gtk.Alignment()
        self.hint_align.set(0.5, 0.5, 0, 0)
        self.hint_align.set_padding(0, 0, 10, 10)
        self.hint_text = Label(_("Please input password for %s")%ssid,
                               enable_select=False,
                               enable_double_click=False)
        self.hint_align.add(self.hint_text)

        self.entry_align = gtk.Alignment()
        self.entry_align.set(0.0, 0.5, 0, 0)
        self.entry_align.set_padding(10, 0, 5, 9)
        if self.connection and isinstance(self.connection, NMRemoteConnection):
            (setting_name, method) = self.connection.guess_secret_info()  
            if setting_name and method:
                init_text = nm_module.secret_agent.agent_get_secrets(self.connection.object_path,
                                                        setting_name,
                                                        method)
            else:
                init_text = ""
        else:
            tray_log.debug()
            self.connection = nm_module.nm_remote_settings.new_wireless_connection(ssid, key_mgmt)
            init_text = ''
        self.entry = PasswordEntry(init_text)
        self.show_key_check = CheckButton(_("Show password"), 0)
        self.show_key_check.connect("toggled", self.show_key_check_button_cb)

        self.entry.set_size(default_width - 22, 25)
        self.main_box = gtk.VBox()
        entry_align = gtk.Alignment(0.0, 0.5, 0, 0)
        entry_align.set_padding(0, 0, 5, 0)
        entry_align.set_size_request(-1, 30)
        entry_align.add(self.entry)
        self.main_box.pack_start(entry_align, False, False)
        self.main_box.pack_start(self.show_key_check, False, False)
        
        self.confirm_button = Button(_("OK"))
        self.cancel_button = Button(_("Cancel"))
        
        self.entry.entry.connect("press-return", lambda w: self.click_confirm_button())
        self.confirm_button.connect("clicked", lambda w: self.click_confirm_button())
        self.cancel_button.connect("clicked", lambda w: self.click_cancel_button())
        
        self.entry_align.add(self.main_box)
        #self.body_box.pack_start(self.hint_align, True, True)
        self.body_box.pack_start(self.entry_align, True, True)

        #self.body_box.pack_start(self.main_box, True, True)
        
        self.right_button_box.set_buttons([self.cancel_button, self.confirm_button])
        self.connect("show", self.focus_input)

    def __set_label(self, name):
        return Label(name, enable_select=False, enable_double_click=False)

    def show_key_check_button_cb(self, widget):
        if widget.get_active():
            self.entry.show_password(True)
        else:
            self.entry.show_password(False)

    def focus_input(self, widget):
        '''
        Grab focus on input entry.
        @param widget: InputDialog widget.
        '''
        self.entry.entry.grab_focus()        

        
    def click_confirm_button(self):
        '''
        Inernal fucntion to handle click confirm button.
        '''
        print self.connection
        if self.confirm_callback != None:
            self.confirm_callback(self.entry.entry.get_text(), self.connection)        
        self.destroy()
        
    def click_cancel_button(self):
        '''
        Inernal fucntion to handle click cancel button.
        '''
        if self.cancel_callback != None:
            self.cancel_callback()
        
        self.destroy()
class Security(gtk.VBox):
    ENTRY_WIDTH = 222

    def __init__(self, connection, set_button_cb, need_ssid=False, settings_obj=None):
        gtk.VBox.__init__(self)
        self.tab_name = _("Security")
        self.connection = connection
        self.set_button = set_button_cb
        # 新增settings_obj变量,用于访问shared_methods.Settings对象
        self.settings_obj = settings_obj
        self.settings_obj.initial_lock = True

        self.need_ssid = need_ssid
        self.presave_index = None

        if self.need_ssid:
            log.info("enter hidden network settings")
            self.add_ssid_entry()
        
        if self.connection.get_setting("802-11-wireless").security == "802-11-wireless-security":
            self.has_security = True
            self.setting = self.connection.get_setting("802-11-wireless-security")
        else:
            self.has_security = False
        self.security_label = Label(_("Security:"),
                                    enable_select=False,
                                    enable_double_click=False)
        self.key_label = Label(_("Key:"),
                               enable_select=False,
                               enable_double_click=False)
        self.wep_index_label = Label(_("Wep index:"),
                                     enable_select=False,
                                     enable_double_click=False)

        self.auth_label = Label(_("Authentication:"),
                                enable_select=False,
                                enable_double_click=False)
        self.password_label = Label(_("Password:"******"None"), None),
                      (_("WEP (Hex or ASCII)"), "none"),
                      (_("WEP 104/128-bit Passphrase"), "none"),
                      (_("WPA WPA2 Personal"), "wpa-psk")]
        #entry_item = map(lambda l: (l[1],l[0]), enumerate(self.encry_list))
        self.security_combo = ComboBox(self.encry_list, fixed_width=self.ENTRY_WIDTH)
        #self.security_combo.set_size_request(self.ENTRY_WIDTH, 22)

        self.key_entry = PasswordEntry()
        self.password_entry = PasswordEntry()
        self.show_key_check = CheckButton(_("Show password"), padding_x=0)
        self.show_key_check.connect("toggled", self.show_key_check_button_cb)
        self.wep_index_spin = SpinBox(0, 1, 4, 1, self.ENTRY_WIDTH)
        self.auth_combo = ComboBox([
                                    (_("Shared Key"), "shared"),
                                    (_("Open System"), "open")],fixed_width=self.ENTRY_WIDTH)

        ## advance button
        self.align = gtk.Alignment(0, 1.0, 0, 0)
        self.align.set_padding(0, 0, 376, 0)
        self.align.set_size_request(-1 ,30)
        self.button = Button(_("Advanced"))
        self.align.add(self.button)

        ## Create table
        self.table = gtk.Table(5, 4)
        #TODO UI change
        label_list = ["security_label", "key_label", "wep_index_label", "auth_label", "password_label"]
        widget_list = ["password_entry", "key_entry", "wep_index_spin", "auth_combo", "security_combo"]
        for label in label_list:
            l = getattr(self, label)
            l.set_can_focus(False)
            align = style.wrap_with_align(l, width=210)
            setattr(self, label+"_align", align)

        for w in widget_list:
            l = getattr(self, w)
            align = style.wrap_with_align(l, align="left")
            setattr(self, w+"_align", align)

        self.show_key_check_align = style.wrap_with_align(self.show_key_check, align="left")

        self.reset(self.has_security)
        self.security_combo.connect("item-selected", self.change_encry_type)
        self.key_entry.entry.connect("changed", self.save_wep_pwd)
        self.password_entry.entry.connect("changed", self.save_wpa_pwd)
        self.wep_index_spin.connect("value-changed", self.wep_index_spin_cb)
        self.auth_combo.connect("item-selected", self.save_auth_cb)
        
        style.set_table(self.table)
        table_align = gtk.Alignment(0, 0, 0, 0)
        table_align.add(self.table)
        style.draw_background_color(self)
        width, height = self.ENTRY_WIDTH, 22
        self.key_entry.set_size(width, height)
        self.password_entry.set_size(width, height)
        self.wep_index_spin.set_size_request(width, height)
        self.auth_combo.set_size_request(width, height)
        self.security_combo.set_size_request(width, height)
        self.pack_start(table_align, False, False)
        self.pack_start(self.align, False, False, 0)
        self.settings_obj.initial_lock = False

    def add_ssid_entry(self):
        self.wireless = self.connection.get_setting("802-11-wireless")
        self.ssid_label = Label(_("SSID:"),
                                enable_select=False,
                                enable_double_click=False)
        self.ssid_label_align = style.wrap_with_align(self.ssid_label, width=210)
        self.ssid_entry = InputEntry()
        self.ssid_entry.set_size(self.ENTRY_WIDTH, 22)
        self.ssid_entry_align = style.wrap_with_align(self.ssid_entry, align="left")
        self.ssid_entry.entry.connect("changed", self.set_ssid)
        self.ssid_entry.set_text(self.wireless.ssid)

        #self.add(align)

    def set_ssid(self, widget, content):
        self.wireless.ssid = content
        check_settings(self.connection, None)

    def advand_cb(self, widget):
        pass

    def reset(self, security=True):
        ## Add security
        container_remove_all(self.table)
        if self.need_ssid:
            self.table.attach(self.ssid_label_align, 0, 1, 0, 1)
            self.table.attach(self.ssid_entry_align, 1, 4, 0, 1)
        
        self.table.resize(2, 4)
        self.table.attach(self.security_label_align, 0, 1, 1, 2)
        self.table.attach(self.security_combo_align, 1, 4, 1, 2)

        if not security:
            self.presave_index = self.security_combo.get_select_index()
            return 
        
        keys = [None, "none", "none","wpa-psk"]
        
        self.key_mgmt = self.setting.key_mgmt
        if self.key_mgmt == "none":
            key_type = self.setting.wep_key_type
            self.security_combo.set_select_index(key_type)
        else:
            self.security_combo.set_select_index(keys.index(self.key_mgmt))

        if not self.security_combo.get_current_item()[1] == None: 
            try:
                (setting_name, method) = self.connection.guess_secret_info() 
                secret = nm_module.secret_agent.agent_get_secrets(self.connection.object_path,
                                                        setting_name,
                                                        method)
                if secret == None:
                    secret = ''
                log.debug("get secret", setting_name, method, "secret")
            except Exception, e:
                log.error("get secret error", e)
                secret = ""

            if self.security_combo.get_current_item()[1] == "wpa-psk":
                self.table.resize(4, 4)
                self.table.attach(self.password_label_align, 0, 1, 2, 3)
                self.table.attach(self.password_entry_align, 1, 4, 2, 3)
                self.table.attach(self.show_key_check_align, 1, 4, 3, 4)
                
                self.password_entry.entry.set_text(secret)
                if secret:
                    #Dispatcher.set_button("save", True)
                    ###########
                    self.settings_obj.wlan_encry_is_valid = True
                    self.settings_obj.set_button("save", True)
                self.setting.psk = secret

            elif self.security_combo.get_current_item()[1] == "none":
                self.table.resize(6, 4)
                # Add Key
                self.table.attach(self.key_label_align, 0, 1, 2, 3)
                self.table.attach(self.key_entry_align, 1, 4, 2, 3)
                self.table.attach(self.show_key_check_align, 1, 4, 3, 4)
                # Add wep index
                self.table.attach(self.wep_index_label_align, 0, 1, 4, 5)
                self.table.attach(self.wep_index_spin_align, 1, 4, 4, 5)
                # Add Auth
                self.table.attach(self.auth_label_align, 0, 1, 5, 6)
                self.table.attach(self.auth_combo_align, 1, 4, 5, 6)

                # Retrieve wep properties
                try:
                    index = self.setting.wep_tx_keyidx
                    auth = self.setting.auth_alg
                    log.debug(auth, index)
                    self.auth_combo.set_select_index(["shared", "open"].index(auth))
                except Exception, e:
                    log.error(e)
                    index = 0
                    auth = "shared"
                # must convert long int to int 
                index = int(index)
                
                #init_key = True
                #if isinstance(self.connection, NMRemoteConnection):
                    #init_setting = self.connection.get_setting("802-11-wireless-security")
                    #if init_setting.wep_key_type != self.setting.wep_key_type:
                        #init_key = False

                #if init_key:
                self.key_entry.entry.set_text(secret)
                self.setting.set_wep_key(index, secret)
                self.wep_index_spin.set_value(index + 1)
                self.auth_combo.set_select_index(["shared", "open"].index(auth))
class AskPasswordDialog(DialogBox):
    '''
    Simple input dialog.
    @undocumented: click_confirm_button
    @undocumented: click_cancel_button
    '''
    def __init__(
        self,
        connection,
        ssid,
        key_mgmt=None,
        default_width=330,
        default_height=120,
        confirm_callback=None,
        cancel_callback=None,
    ):
        '''
        Initialize InputDialog class.
        
        @param title: Input dialog title.
        @param init_text: Initialize input text.
        @param default_width: Width of dialog, default is 330 pixel.
        @param default_height: Height of dialog, default is 330 pixel.
        @param confirm_callback: Callback when user click confirm button, this callback accept one argument that return by user input text.
        @param cancel_callback: Callback when user click cancel button, this callback not need argument.
        '''
        # Init.
        #DialogBox.__init__(self, _("Set password"), default_width, default_height, DIALOG_MASK_SINGLE_PAGE)
        DialogBox.__init__(self,
                           _("Please input password for %s") % ssid,
                           default_width, default_height,
                           DIALOG_MASK_SINGLE_PAGE)

        #self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_NORMAL)
        self.confirm_callback = confirm_callback
        self.cancel_callback = cancel_callback

        self.connection = connection

        self.hint_align = gtk.Alignment()
        self.hint_align.set(0.5, 0.5, 0, 0)
        self.hint_align.set_padding(0, 0, 10, 10)
        self.hint_text = Label(_("Please input password for %s") % ssid,
                               enable_select=False,
                               enable_double_click=False)
        self.hint_align.add(self.hint_text)

        self.entry_align = gtk.Alignment()
        self.entry_align.set(0.0, 0.5, 0, 0)
        self.entry_align.set_padding(10, 0, 5, 9)
        if self.connection and isinstance(self.connection, NMRemoteConnection):
            (setting_name, method) = self.connection.guess_secret_info()
            if setting_name and method:
                init_text = nm_module.secret_agent.agent_get_secrets(
                    self.connection.object_path, setting_name, method)
            else:
                init_text = ""
        else:
            tray_log.debug()
            self.connection = nm_module.nm_remote_settings.new_wireless_connection(
                ssid, key_mgmt)
            init_text = ''
        self.entry = PasswordEntry(init_text)
        self.show_key_check = CheckButton(_("Show password"), 0)
        self.show_key_check.connect("toggled", self.show_key_check_button_cb)

        self.entry.set_size(default_width - 22, 25)
        self.main_box = gtk.VBox()
        entry_align = gtk.Alignment(0.0, 0.5, 0, 0)
        entry_align.set_padding(0, 0, 5, 0)
        entry_align.set_size_request(-1, 30)
        entry_align.add(self.entry)
        self.main_box.pack_start(entry_align, False, False)
        self.main_box.pack_start(self.show_key_check, False, False)

        self.confirm_button = Button(_("OK"))
        self.cancel_button = Button(_("Cancel"))

        self.entry.entry.connect("press-return",
                                 lambda w: self.click_confirm_button())
        self.confirm_button.connect("clicked",
                                    lambda w: self.click_confirm_button())
        self.cancel_button.connect("clicked",
                                   lambda w: self.click_cancel_button())

        self.entry_align.add(self.main_box)
        #self.body_box.pack_start(self.hint_align, True, True)
        self.body_box.pack_start(self.entry_align, True, True)

        #self.body_box.pack_start(self.main_box, True, True)

        self.right_button_box.set_buttons(
            [self.cancel_button, self.confirm_button])
        self.connect("show", self.focus_input)

    def __set_label(self, name):
        return Label(name, enable_select=False, enable_double_click=False)

    def show_key_check_button_cb(self, widget):
        if widget.get_active():
            self.entry.show_password(True)
        else:
            self.entry.show_password(False)

    def focus_input(self, widget):
        '''
        Grab focus on input entry.
        @param widget: InputDialog widget.
        '''
        self.entry.entry.grab_focus()

    def click_confirm_button(self):
        '''
        Inernal fucntion to handle click confirm button.
        '''
        print self.connection
        if self.confirm_callback != None:
            self.confirm_callback(self.entry.entry.get_text(), self.connection)
        self.destroy()

    def click_cancel_button(self):
        '''
        Inernal fucntion to handle click cancel button.
        '''
        if self.cancel_callback != None:
            self.cancel_callback()

        self.destroy()
Exemplo n.º 8
0
    volume_frame.add(volume_button)
    tab_1_box.pack_start(volume_frame, False, False)
    
    # Add entry widget.
    entry_box = gtk.HBox(spacing=10)
    
    text_entry = dtk.ui.entry.TextEntry()

    text_entry.set_size(100, 22)
    input_entry = InputEntry()
    input_entry.entry.place_holder = "哈哈中"    
    input_entry.set_size(100, 22)
    shortcust_entry = ShortcutKeyEntry()
    shortcust_entry.set_size(100, 22)
    password_entry = PasswordEntry()
    password_entry.set_size(100, 22)
    
    combo_box = ComboBox([("测试测试测试%d" % i, i)  for i in range(10)], fixed_width=100)
    
    spin_box = SpinBox(3000, 0, 5000, 100)
    
    # mac_entry = MACEntry()
    mac_entry = IPV4Entry()
    mac_entry.connect("changed", print_address)
    # mac_entry = IPV4Entry()
    # mac_entry.set_ip("255.255.255.andy")
    # mac_entry.set_ip("192.168..12")
    # mac_entry.set_ip("192.168..12")
    # mac_entry.set_ip("...")
    
    # entry_box.pack_start(text_entry, False, False)
class DSLConf(gtk.VBox):
    LEFT_PADDING = 210

    def __init__(self, connection, set_button_callback=None, settings_obj=None):
        gtk.VBox.__init__(self)
        self.tab_name = _("DSL")
        self.connection = connection
        self.set_button = set_button_callback
        # 新增settings_obj变量,用于访问shared_methods.Settings对象
        self.settings_obj = settings_obj
        self.dsl_setting = self.connection.get_setting("pppoe")

        # UI
        dsl_table = gtk.Table(5, 3, False)
        ssid_label = Label(_("Connection Name:"),
                               text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        ssid_label.set_can_focus(False)

        username_label = Label(_("Username:"******"Service:"), 
                              text_size=CONTENT_FONT_SIZE,
                              enable_select=False,
                              enable_double_click=False)
        service_label.set_can_focus(False)
        password_label = Label(_("Password:"******"Show Password"), font_size=CONTENT_FONT_SIZE, padding_x=0)
        def show_password(widget):
            if widget.get_active():
                self.password_entry.show_password(True)
            else:
                self.password_entry.show_password(False)
        self.show_password.connect("toggled", show_password)

        #pack entries
        dsl_table.attach(style.wrap_with_align(self.ssid_entry, align="left"), 1, 3, 0, 1)
        dsl_table.attach(style.wrap_with_align(self.username_entry, align="left"), 1, 3, 1, 2)
        dsl_table.attach(style.wrap_with_align(self.service_entry, align="left"), 1, 3, 2, 3)
        dsl_table.attach(style.wrap_with_align(self.password_entry, align="left"), 1, 3, 3, 4)
        dsl_table.attach(style.wrap_with_align(self.show_password, align="left"), 1, 3, 4, 5)

        style.draw_background_color(self)
        style.set_table(dsl_table)
        # just make table postion looks right
        table_align = gtk.Alignment(0, 0, 0, 0)
        table_align.add(dsl_table)
        self.pack_start(table_align, False, False)
        #self.add(align)
        self.show_all()
        self.refresh()

        self.ssid_entry.entry.connect("changed", self.save_changes, "ssid")
        self.username_entry.entry.connect("changed", self.save_changes, "username")
        self.service_entry.entry.connect("changed", self.save_changes, "service")
        self.password_entry.entry.connect("changed", self.save_changes, "password")

    def refresh(self):
        #print ">>>",self.connection.settings_dict
        # get dsl settings
        ssid = self.connection.get_setting("connection").id
        if type(self.connection) == NMRemoteConnection:
            self.ssid_entry.set_text(ssid)
        username = self.dsl_setting.username
        service = self.dsl_setting.service
        (setting_name, method) = self.connection.guess_secret_info() 
        try:
            password = nm_module.secret_agent.agent_get_secrets(self.connection.object_path,
                                                    setting_name,
                                                    method)
        except:
            password = ""
        # check if empty
        if username == None:
            username = ""
        if service == None:
            service = ""
        if password == None:
            password = ""
            self.settings_obj.dsl_is_valid = False
        # fill entry
        self.username_entry.entry.set_text(str(username))
        self.service_entry.entry.set_text(str(service))
        self.password_entry.entry.set_text(str(password))
        setattr(self.dsl_setting, "password", str(password))

    def save_changes(self, widget, value, types):
        if types == "ssid":
            self.connection.get_setting("connection").id = value
        else:
            if value:
                
                setattr(self.dsl_setting, types, value)
            else:
                delattr(self.dsl_setting, types)
        #check_settings(self.connection, self.set_button)
        ############
        is_valid = self.connection.check_setting_finish()
        self.settings_obj.dsl_is_valid = is_valid
        self.settings_obj.set_button("save", is_valid)
class PPPConf(gtk.VBox):
    ENTRY = 0
    OFFBUTTON = 1

    TABLE_WIDTH = 150

    def __init__(self, settings_obj=None):
        gtk.VBox.__init__(self)

        #self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        #Dispatcher.set_button = set_button_callback
        #self.module_frame = module_frame

        # 新增settings_obj变量,用于访问shared_methods.Settings对象
        self.settings_obj = settings_obj

        self.method_title = TitleBar(None,
                                     _("Configure Method"),
                                     width=self.TABLE_WIDTH,
                                     has_separator=False)

        self.refuse_eap_label = Label(_("EAP"),
                                      text_size=CONTENT_FONT_SIZE,
                                      enable_select=False,
                                      enable_double_click=False)
        self.refuse_pap_label = Label(_("PAP"),
                                      text_size=CONTENT_FONT_SIZE,
                                      enable_select=False,
                                      enable_double_click=False)
        self.refuse_chap_label = Label(_("CHAP"),
                                       text_size=CONTENT_FONT_SIZE,
                                       enable_select=False,
                                       enable_double_click=False)
        self.refuse_mschap_label = Label(_("MSCHAP"),
                                         text_size=CONTENT_FONT_SIZE,
                                         enable_select=False,
                                         enable_double_click=False)
        self.refuse_mschapv2_label = Label(_("MSCHAP v2"),
                                           text_size=CONTENT_FONT_SIZE,
                                           enable_select=False,
                                           enable_double_click=False)
        self.refuse_eap = SwitchButton()
        self.refuse_pap = SwitchButton()
        self.refuse_chap = SwitchButton()
        self.refuse_mschap = SwitchButton()
        self.refuse_mschapv2 = SwitchButton()

        self.method_table = gtk.Table(23, 3, False)

        # visible settings

        self.compression_title = TitleBar(None,
                                          _("Compression"),
                                          width=self.TABLE_WIDTH,
                                          has_separator=False)

        self.echo_title = TitleBar(None,
                                   _("Echo"),
                                   width=self.TABLE_WIDTH,
                                   has_separator=False)

        #compressio))n = Label(_("Compression"), text_size=TITLE_FONT_SIZE)
        self.require_mppe_label = Label(
            _("Use point-to-point encryption(mppe)"),
            text_size=CONTENT_FONT_SIZE,
            enable_select=False,
            enable_double_click=False)
        self.require_mppe_128_label = Label(_("Require 128-bit encryption"),
                                            text_size=CONTENT_FONT_SIZE,
                                            enable_select=False,
                                            enable_double_click=False)
        self.mppe_stateful_label = Label(_("Use stateful MPPE"),
                                         text_size=CONTENT_FONT_SIZE,
                                         enable_select=False,
                                         enable_double_click=False)
        self.nobsdcomp_label = Label(_("Allow BSD data Compression"),
                                     text_size=CONTENT_FONT_SIZE,
                                     enable_select=False,
                                     enable_double_click=False)
        self.nodeflate_label = Label(_("Allow Deflate data compression"),
                                     text_size=CONTENT_FONT_SIZE,
                                     enable_select=False,
                                     enable_double_click=False)
        self.no_vj_comp_label = Label(_("Use TCP header compression"),
                                      text_size=CONTENT_FONT_SIZE,
                                      enable_select=False,
                                      enable_double_click=False)
        #echo = Label("Echo", text_size=TITLE_FONT_SIZE)
        self.ppp_echo_label = Label(_("Send PPP echo packets"),
                                    text_size=CONTENT_FONT_SIZE,
                                    enable_select=False,
                                    enable_double_click=False)
        self.nopcomp_label = Label(
            _("Use protocal field compression negotiation"),
            text_size=CONTENT_FONT_SIZE,
            enable_select=False,
            enable_double_click=False)
        self.noaccomp_label = Label(_("Use Address/Control compression"),
                                    text_size=CONTENT_FONT_SIZE,
                                    enable_select=False,
                                    enable_double_click=False)

        self.require_mppe = SwitchButton()
        self.require_mppe_128 = SwitchButton()
        self.mppe_stateful = SwitchButton()

        self.nobsdcomp = SwitchButton()
        self.nodeflate = SwitchButton()
        self.no_vj_comp = SwitchButton()
        self.nopcomp = SwitchButton()
        self.noaccomp = SwitchButton()
        self.ppp_echo = SwitchButton()
        self.ip_sec_enable = SwitchButton()

        ## Settings for IPSec
        self.ipsec_title = TitleBar(None,
                                    _("IPSec Setting"),
                                    width=self.TABLE_WIDTH,
                                    has_separator=False)

        self.ip_sec_enable_label = Label(_("Enable IPSec tunnel to l2tp host"),
                                         text_size=CONTENT_FONT_SIZE,
                                         enable_select=False,
                                         enable_double_click=False)
        self.group_name_label = Label(_("Group Name:"),
                                      text_size=CONTENT_FONT_SIZE,
                                      enable_select=False,
                                      enable_double_click=False)
        self.gateway_id_label = Label(_("Gateway ID:"),
                                      text_size=CONTENT_FONT_SIZE,
                                      enable_select=False,
                                      enable_double_click=False)
        self.pre_shared_key_label = Label(_("Pre_Shared_key"),
                                          text_size=CONTENT_FONT_SIZE,
                                          enable_select=False,
                                          enable_double_click=False)
        self.group_name = InputEntry()
        self.group_name.set_size(self.TABLE_WIDTH, 22)
        self.gateway_id = InputEntry()
        self.gateway_id.set_size(self.TABLE_WIDTH, 22)
        self.pre_shared_key = PasswordEntry()
        self.pre_shared_key.set_text = self.pre_shared_key.entry.set_text
        self.pre_shared_key.set_size(self.TABLE_WIDTH, 22)

        methods_list = [
            "refuse_eap", "refuse_eap_label", "refuse_pap", "refuse_pap_label",
            "refuse_chap", "refuse_chap_label", "refuse_mschap",
            "refuse_mschap_label", "refuse_mschapv2", "refuse_mschapv2_label"
        ]
        compression_list = [
            "require_mppe_label", "require_mppe", "require_mppe_128_label",
            "require_mppe_128", "mppe_stateful_label", "mppe_stateful",
            "nobsdcomp_label", "nobsdcomp", "nodeflate_label", "nodeflate",
            "no_vj_comp_label", "no_vj_comp", "nopcomp_label", "nopcomp",
            "noaccomp_label", "noaccomp"
        ]

        echo_list = ["ppp_echo_label", "ppp_echo"]

        ip_sec_list = [
            "ip_sec_enable_label", "ip_sec_enable", "group_name_label",
            "group_name", "gateway_id_label", "gateway_id",
            "pre_shared_key_label", "pre_shared_key"
        ]

        for name in (compression_list + echo_list + methods_list +
                     ip_sec_list):
            widget = getattr(self, name)
            if not name.endswith("label"):
                align = style.wrap_with_align(widget, align="left")
            else:
                align = style.wrap_with_align(widget, width=210)

            setattr(self, name + "_align", align)

        #vbox = gtk.VBox()
        table_align = gtk.Alignment(0, 0, 0, 0)
        table_align.add(self.method_table)
        style.set_table(self.method_table)

        self.pack_start(table_align, False, False)
        self.method_table.set_row_spacing(5, 20)
        self.method_table.set_row_spacing(15, 20)
        self.method_table.set_row_spacing(18, 20)

        Dispatcher.connect("vpn-type-change", lambda w, c: self.refresh(c))
        #align = style.set_box_with_align(vbox, "text")
        #self.add_with_viewport(align)
        #style.draw_background_color(align)

        #confirm_button = Button("Confirm")
        #confirm_button.connect("clicked", self.confirm_button_cb)
        #button_aligns = gtk.Alignment(0.5 , 1, 0, 0)
        #button_aligns.add(confirm_button)
        #self.add(button_aligns)

        #self.require_mppe_128.set_sensitive(False)
        #self.mppe_stateful.set_sensitive(False)
        ##self.refresh()

    def init_signal(self):
        self.refuse_eap.connect("toggled", self.check_button_cb, "refuse-eap")
        self.refuse_pap.connect("toggled", self.check_button_cb, "refuse-pap")
        self.refuse_chap.connect("toggled", self.check_button_cb,
                                 "refuse-chap")
        self.refuse_mschap.connect("toggled", self.check_button_cb,
                                   "refuse-mschap")
        self.refuse_mschapv2.connect("toggled", self.check_button_cb,
                                     "refuse-mschapv2")
        self.require_mppe.connect("toggled", self.click_mppe_callback,
                                  "require-mppe")
        self.require_mppe_128.connect("toggled", self.check_button_cb,
                                      "require-mppe-128")
        self.mppe_stateful.connect("toggled", self.check_button_cb,
                                   "mppe-stateful")
        self.nobsdcomp.connect("toggled", self.check_button_cb, "nobsdcomp")
        self.nodeflate.connect("toggled", self.check_button_cb, "nodeflate")
        self.no_vj_comp.connect("toggled", self.check_button_cb, "no-vj-comp")
        self.ppp_echo.connect("toggled", self.check_button_cb, "echo")
        self.nopcomp.connect("toggled", self.check_button_cb, "nopcomp")
        self.noaccomp.connect("toggled", self.check_button_cb, "noaccomp")

        self.ip_sec_enable.connect("toggled", self.enable_ipsec_cb)
        #self.group_name.entry.connect("focus-out-event", self.entry_focus_out_cb, "ipsec-group-name")
        #self.gateway_id.entry.connect("focus-out-event", self.entry_focus_out_cb, "ipsec-gataway-id")
        #self.pre_shared_key.entry.connect("focus-out-event", self.entry_focus_out_cb, "ipsec-psk")
        self.group_name.entry.connect("changed", self.entry_changed_cb,
                                      "ipsec-group-name")
        self.gateway_id.entry.connect("changed", self.entry_changed_cb,
                                      "ipsec-gateway-id")
        self.pre_shared_key.entry.connect("changed", self.entry_changed_cb,
                                          "ipsec-psk")

    def init_ui(self):

        self.service_type = self.vpn_setting.service_type.split(".")[-1]

        def table_attach(widget_name, row, padding=0):
            label = getattr(self, widget_name + "_label_align")
            widget = getattr(self, widget_name + "_align")
            self.method_table.attach(label, 0, 2, row, row + 1)
            self.method_table.attach(widget,
                                     2,
                                     3,
                                     row,
                                     row + 1,
                                     xpadding=padding)

        #print self.service_type
        container_remove_all(self.method_table)
        self.method_table.attach(self.method_title, 0, 3, 0, 1)
        table_attach("refuse_eap", 1)
        table_attach("refuse_pap", 2)
        table_attach("refuse_chap", 3)
        table_attach("refuse_mschap", 4)
        table_attach("refuse_mschapv2", 5)
        self.method_table.attach(self.compression_title, 0, 3, 6, 7)
        table_attach("require_mppe", 8)
        if self.require_mppe.get_active():
            table_attach("require_mppe_128", 9)
            table_attach("mppe_stateful", 10)
        table_attach("nobsdcomp", 11)
        table_attach("nodeflate", 12)
        table_attach("no_vj_comp", 13)
        self.method_table.attach(self.echo_title, 0, 3, 16, 17)
        table_attach("ppp_echo", 18)

        if self.service_type == "l2tp":
            #print "this is l2tp"
            table_attach("nopcomp", 14)
            table_attach("noaccomp", 15)

            self.method_table.attach(self.ipsec_title, 0, 3, 19, 20)
            table_attach("ip_sec_enable", 20)
            if self.ip_sec_enable.get_active():
                table_attach("group_name", 21)
                table_attach("gateway_id", 22)
                table_attach("pre_shared_key", 23)

        self.method_table.show_all()

    def refresh(self, connection):
        self.connection = connection
        self.vpn_setting = self.connection.get_setting("vpn")
        #=========================
        # retreieve settings
        self.service_type = self.vpn_setting.service_type.split(".")[-1]
        #print ">>",self.vpn_setting.data
        refuse_eap = self.vpn_setting.get_data_item("refuse-eap")
        refuse_pap = self.vpn_setting.get_data_item("refuse-pap")
        refuse_chap = self.vpn_setting.get_data_item("refuse-chap")
        refuse_mschap = self.vpn_setting.get_data_item("refuse-mschap")
        refuse_mschapv2 = self.vpn_setting.get_data_item("refuse-mschapv2")

        require_mppe = self.vpn_setting.get_data_item("require-mppe")
        require_mppe_128 = self.vpn_setting.get_data_item("require-mppe-128")
        mppe_stateful = self.vpn_setting.get_data_item("mppe-stateful")

        nobsdcomp = self.vpn_setting.get_data_item("nobsdcomp")
        nodeflate = self.vpn_setting.get_data_item("nodeflate")
        no_vj_comp = self.vpn_setting.get_data_item("no-vj-comp")

        lcp_echo_failure = self.vpn_setting.get_data_item("lcp-echo-failure")
        lcp_echo_interval = self.vpn_setting.get_data_item("lcp-echo-interval")

        self.refuse_mschap.set_active(refuse_mschap == None)
        self.refuse_mschapv2.set_active(refuse_mschapv2 == None)
        self.require_mppe.set_active(require_mppe != None)

        self.refuse_eap.set_active(refuse_eap == None)
        self.refuse_pap.set_active(refuse_pap == None)
        self.refuse_chap.set_active(refuse_chap == None)

        self.require_mppe_128.set_active(require_mppe_128 != None)
        if self.require_mppe_128.get_active() or self.mppe_stateful.get_active(
        ):
            self.require_mppe.set_active(True)
        self.mppe_stateful.set_active(mppe_stateful != None)
        self.nobsdcomp.set_active(nobsdcomp == None)
        self.nodeflate.set_active(nodeflate == None)
        self.no_vj_comp.set_active(no_vj_comp == None)

        if self.service_type == "l2tp":
            nopcomp = self.vpn_setting.get_data_item("nopcomp")
            noaccomp = self.vpn_setting.get_data_item("noaccomp")
            ipsec_enabled = self.vpn_setting.get_data_item("ipsec-enabled")

            self.nopcomp.set_active(nopcomp == None)
            self.noaccomp.set_active(noaccomp == None)

            if ipsec_enabled:
                self.ip_sec_enable.set_active(True)

                ipsec_group_name = self.vpn_setting.get_data_item(
                    "ipsec-group-name")
                ipsec_gateway_id = self.vpn_setting.get_data_item(
                    "ipsec-gateway-id")
                ipsec_psk = self.vpn_setting.get_data_item("ipsec-psk")
                self.group_name.set_text(ipsec_group_name)
                self.gateway_id.set_text(ipsec_gateway_id)
                self.pre_shared_key.set_text(ipsec_psk)

            else:
                self.ip_sec_enable.set_active(False)

        if lcp_echo_failure == None and lcp_echo_interval == None:
            self.ppp_echo.set_active(False)
        else:
            self.ppp_echo.set_active(True)

        self.init_signal()
        self.init_lock = False
        self.require_mppe.emit("toggled")
        self.init_lock = True
        self.init_ui()
        #==================================
        # Connect signal
    def enable_ipsec_cb(self, widget):
        active = widget.get_active()
        if active:
            self.vpn_setting.set_data_item("ipsec-enabled", "yes")
            self.group_name.set_sensitive(True)
            self.gateway_id.set_sensitive(True)
            self.pre_shared_key.set_sensitive(True)

            ipsec_group_name = self.vpn_setting.get_data_item(
                "ipsec-group-name")
            ipsec_gateway_id = self.vpn_setting.get_data_item(
                "ipsec-gateway-id")
            ipsec_psk = self.vpn_setting.get_data_item("ipsec-psk")

            #print ipsec_group_name
            self.group_name.set_text(ipsec_group_name)
            self.gateway_id.set_text(ipsec_gateway_id)
            self.pre_shared_key.set_text(ipsec_psk)
            self.init_ui()
        else:
            self.vpn_setting.delete_data_item("ipsec-enabled")
            self.group_name.set_text("")
            self.gateway_id.set_text("")
            self.pre_shared_key.set_text("")

            self.group_name.set_sensitive(False)
            self.gateway_id.set_sensitive(False)
            self.pre_shared_key.set_sensitive(False)
            self.init_ui()

    def entry_focus_out_cb(self, widget, event, key):
        text = widget.get_text()
        if text and key != "name":
            self.vpn_setting.set_data_item(key, text)
        else:
            self.vpn_setting.delete_data_item(key)

    def entry_changed_cb(self, widget, string, key):
        if string == "":
            #print key,"entry is empty"
            self.vpn_setting.delete_data_item(key)
        elif key != "name":
            self.vpn_setting.set_data_item(key, string)

        if self.connection.check_setting_finish():
            Dispatcher.set_button("save", True)
        else:
            Dispatcher.set_button("save", False)

    def check_button_cb(self, widget, key):
        auth_lock = self.auth_lock()
        active = widget.get_active()
        if key.startswith("refuse"):
            if active:
                self.vpn_setting.delete_data_item(key)
            else:
                self.vpn_setting.set_data_item(key, "yes")

            if auth_lock:
                self.require_mppe_label.set_sensitive(False)
                self.require_mppe.set_sensitive(False)

                self.set_group_sensitive(True)
            else:
                self.require_mppe_label.set_sensitive(True)
                self.require_mppe.set_sensitive(True)

        elif key.startswith("no"):
            if active:
                self.vpn_setting.delete_data_item(key)
            else:
                self.vpn_setting.set_data_item(key, "yes")

        elif key == "echo":
            if active:
                self.vpn_setting.set_data_item("lcp-echo-failure", "5")
                self.vpn_setting.set_data_item("lcp-echo-interval", "30")
            else:
                self.vpn_setting.delete_data_item("lcp-echo-failure")
                self.vpn_setting.delete_data_item("lcp-echo-interval")
        elif key.startswith("ipsec"):
            if active:
                self.vpn_setting.set_data_item(key, "yes")
            else:
                self.vpn_setting.delete_data_item(key)
        elif key.startswith("require") or key.startswith("mppe"):
            if active:
                self.vpn_setting.set_data_item(key, "yes")
            else:
                self.vpn_setting.delete_data_item(key)

        if self.connection.check_setting_finish():
            Dispatcher.set_button("save", True)
        else:
            Dispatcher.set_button("save", False)

    def click_mppe_callback(self, widget, key):
        active = widget.get_active()
        if active and key != "name":
            self.vpn_setting.set_data_item(key, "yes")
            #self.set_group_active(False)
            self.set_group_sensitive(False)

            self.mppe_group_set_sensitive(True)
            self.init_ui()
        else:
            #self.set_group_active(True)
            self.set_group_sensitive(True)
            self.vpn_setting.delete_data_item(key)
            self.mppe_group_set_active(False)
            self.mppe_group_set_sensitive(False)
            self.init_ui()

        if self.init_lock:
            if self.connection.check_setting_finish():
                Dispatcher.set_button("save", True)
            else:
                Dispatcher.set_button("save", False)

        if self.auth_lock():
            self.require_mppe_label.set_sensitive(False)
            self.require_mppe.set_sensitive(False)
            return

    def mppe_group_set_sensitive(self, boolean):
        self.require_mppe_128_label.set_sensitive(boolean)
        self.mppe_stateful_label.set_sensitive(boolean)
        self.require_mppe_128.set_sensitive(boolean)
        self.mppe_stateful.set_sensitive(boolean)

    def mppe_group_set_active(self, boolean):
        log.debug()
        self.require_mppe_128.set_active(boolean)
        self.mppe_stateful.set_active(boolean)

    def confirm_button_cb(self, widget):
        self.module_frame.send_message("change_crumb", 2)
        nm_module.slider._slide_to_page("vpn", "none")

    def auth_lock(self):
        if self.refuse_mschap.get_active() or self.refuse_mschapv2.get_active(
        ):
            return False
        else:
            return True

    def set_group_active(self, boolean):
        self.refuse_eap.set_active(boolean)
        self.refuse_pap.set_active(boolean)
        self.refuse_chap.set_active(boolean)

    def set_group_sensitive(self, boolean):
        self.refuse_eap.set_sensitive(boolean)
        self.refuse_pap.set_sensitive(boolean)
        self.refuse_chap.set_sensitive(boolean)
        self.refuse_eap_label.set_sensitive(boolean)
        self.refuse_pap_label.set_sensitive(boolean)
        self.refuse_chap_label.set_sensitive(boolean)
class PPTPConf(gtk.VBox):
    ENTRY_WIDTH = 222
    LEFT_PADDING = 210

    def __init__(self,
                 connection,
                 module_frame,
                 set_button_callback=None,
                 settings_obj=None):
        gtk.VBox.__init__(self)
        self.connection = connection
        self.tab_name = _("PPTP")
        self.module_frame = module_frame
        self.set_button = set_button_callback
        # 新增settings_obj变量,用于访问shared_methods.Settings对象
        self.settings_obj = settings_obj

        self.vpn_setting = self.connection.get_setting("vpn")

        # UI

        pptp_table = gtk.Table(7, 4, False)

        name_label = Label(_("Connection Name:"),
                           enable_select=False,
                           enable_double_click=False)
        name_label.set_can_focus(False)
        gateway_label = Label(_("Gateway:"),
                              enable_select=False,
                              enable_double_click=False)
        gateway_label.set_can_focus(False)
        user_label = Label(_("Username:"******"Password:"******"NT Domain:"),
                                enable_select=False,
                                enable_double_click=False)
        nt_domain_label.set_can_focus(False)
        # Radio Button
        self.pptp_radio = RadioButton(_("PPTP"))
        self.l2tp_radio = RadioButton(_("L2TP"))
        radio_box = gtk.HBox(spacing=30)
        radio_box.pack_start(self.pptp_radio, True, True)
        radio_box.pack_start(self.l2tp_radio, True, True)
        #pack labels
        pptp_table.attach(style.wrap_with_align(radio_box, align="left"), 2, 4,
                          0, 1)
        pptp_table.attach(
            style.wrap_with_align(name_label, width=self.LEFT_PADDING), 0, 2,
            1, 2)
        pptp_table.attach(
            style.wrap_with_align(gateway_label, width=self.LEFT_PADDING), 0,
            2, 2, 3)
        pptp_table.attach(
            style.wrap_with_align(user_label, width=self.LEFT_PADDING), 0, 2,
            3, 4)
        pptp_table.attach(
            style.wrap_with_align(password_label, width=self.LEFT_PADDING), 0,
            2, 4, 5)
        #pptp_table.attach(style.wrap_with_align(nt_domain_label), 0, 2, 5, 6)

        # entries
        self.name_entry = InputEntry()
        self.name_entry.set_size(self.ENTRY_WIDTH, 22)

        self.gateway_entry = InputEntry()
        self.gateway_entry.set_size(self.ENTRY_WIDTH, 22)
        self.user_entry = InputEntry()
        self.user_entry.set_size(self.ENTRY_WIDTH, 22)
        # FIXME should change to new_entry PasswordEntry
        self.password_entry = PasswordEntry()
        self.password_entry.set_size(self.ENTRY_WIDTH, 22)
        self.password_show = CheckButton(_("Show Password"), padding_x=0)
        self.password_show.set_active(False)
        self.password_show.connect("toggled", self.show_password)
        self.nt_domain_entry = InputEntry()
        self.nt_domain_entry.set_size(self.ENTRY_WIDTH, 22)

        #pack entries
        pptp_table.attach(style.wrap_with_align(self.name_entry, align="left"),
                          2, 4, 1, 2)
        pptp_table.attach(
            style.wrap_with_align(self.gateway_entry, align="left"), 2, 4, 2,
            3)
        pptp_table.attach(style.wrap_with_align(self.user_entry, align="left"),
                          2, 4, 3, 4)
        pptp_table.attach(
            style.wrap_with_align(self.password_entry, align="left"), 2, 4, 4,
            5)
        pptp_table.attach(
            style.wrap_with_align(self.password_show, align="left"), 2, 4, 5,
            6)
        #pptp_table.attach(style.wrap_with_align(self.nt_domain_entry), 2, 4, 5, 6)
        # Advance setting button
        #advanced_button = Button(_("Advanced Setting"))
        #advanced_button.connect("clicked", self.advanced_button_click)

        #pptp_table.attach(style.wrap_with_align(advanced_button), 3, 4, 6, 7)
        self.service_type = self.vpn_setting.service_type.split(".")[-1]
        if self.service_type == "l2tp":
            self.l2tp_radio.set_active(True)
        else:
            self.pptp_radio.set_active(True)
        self.pptp_radio.connect("toggled", self.radio_toggled, "pptp")
        self.l2tp_radio.connect("toggled", self.radio_toggled, "l2tp")
        # set signals

        #align = style.set_box_with_align(pptp_table, "text")
        table_align = gtk.Alignment(0, 0, 0, 0)
        table_align.add(pptp_table)
        #style.set_table_items(pptp_table, "entry")
        style.draw_background_color(self)
        style.set_table(pptp_table)
        self.add(table_align)
        self.show_all()
        self.refresh()
        self.name_entry.entry.connect("changed", self.entry_changed, "name")
        self.gateway_entry.entry.connect("changed", self.entry_changed,
                                         "gateway")
        self.user_entry.entry.connect("changed", self.entry_changed, "user")
        self.password_entry.entry.connect("changed", self.entry_changed,
                                          "password")
        self.nt_domain_entry.entry.connect("changed", self.entry_changed,
                                           "domain")

        #if self.connection.check_setting_finish():
        #print "in vpn"
        #Dispatcher.set_button("save", True)
        #else:
        #print "in vpn"
        #Dispatcher.set_button("save", False)
        ##############
        #is_valid = self.connection.check_setting_finish()
        #self.settings_obj.vpn_is_valid = is_valid
        #self.settings_obj.set_button("save", is_valid)

    def refresh(self):
        #print ">>>",self.vpn_setting.data
        #print self.vpn_setting.data
        name = self.connection.get_setting("connection").id
        gateway = self.vpn_setting.get_data_item("gateway")
        user = self.vpn_setting.get_data_item("user")
        domain = self.vpn_setting.get_data_item("domain")

        if type(self.connection) == NMRemoteConnection:
            self.name_entry.set_text(name)

        if gateway:
            self.gateway_entry.set_text(gateway)
        if user:
            self.user_entry.set_text(user)

        (setting_name, method) = self.connection.guess_secret_info()
        try:
            password = nm_module.secret_agent.agent_get_secrets(
                self.connection.object_path, setting_name, method)
            if password is None:
                #self.password_entry.entry.set_text("")
                self.password_entry.entry.set_text("")
            else:
                #self.password_entry.entry.set_text(password)
                self.password_entry.entry.set_text(password)
                self.vpn_setting.set_secret_item("password", password)
        except:
            pass

        if domain:
            self.nt_domain_entry.set_text(domain)

    def save_setting(self):
        pass

    def show_password(self, widget):
        if widget.get_active():
            self.password_entry.show_password(True)
        else:
            self.password_entry.show_password(False)

    def entry_changed(self, widget, content, item):
        text = content
        if item == "name":
            self.connection.get_setting("connection").id = content

        if text:
            if item == "password":
                self.vpn_setting.set_secret_item(item, text)
            elif item != "name":
                self.vpn_setting.set_data_item(item, text)

        else:
            if item == "password":
                self.vpn_setting.set_secret_item(item, "")
            else:
                self.vpn_setting.delete_data_item(item)

        #if self.connection.check_setting_finish():
        #Dispatcher.set_button("save", True)
        #else:
        #Dispatcher.set_button("save", False)
        ##############
        is_valid = self.connection.check_setting_finish()
        self.settings_obj.vpn_is_valid = is_valid
        self.settings_obj.set_button("save", is_valid)

    def radio_toggled(self, widget, service_type):
        if widget.get_active():
            self.vpn_setting.service_type = "org.freedesktop.NetworkManager." + service_type
            self.service_type = service_type

        if self.connection.check_setting_finish():
            Dispatcher.set_button("save", True)
        else:
            Dispatcher.set_button("save", False)
            self.refresh()

        Dispatcher.emit("vpn-type-change", self.connection)

    def advanced_button_click(self, widget):
        ppp = PPPConf(self.module_frame, Dispatcher.set_button)
        ppp.refresh(self.connection)
        Dispatcher.send_submodule_crumb(3, _("Advanced"))
        nm_module.slider.slide_to_page(ppp, "none")
class PPPConf(gtk.VBox):
    ENTRY = 0
    OFFBUTTON = 1

    TABLE_WIDTH = 150
    def __init__(self, settings_obj=None):
        gtk.VBox.__init__(self)
        
        #self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        #Dispatcher.set_button = set_button_callback
        #self.module_frame = module_frame

        # 新增settings_obj变量,用于访问shared_methods.Settings对象
        self.settings_obj = settings_obj
        
        self.method_title = TitleBar(None,
                                     _("Configure Method"),
                                     width=self.TABLE_WIDTH,
                                     has_separator=False)

        self.refuse_eap_label = Label(_("EAP"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.refuse_pap_label = Label(_("PAP"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.refuse_chap_label = Label(_("CHAP"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.refuse_mschap_label = Label(_("MSCHAP"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.refuse_mschapv2_label = Label(_("MSCHAP v2"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.refuse_eap = SwitchButton()
        self.refuse_pap = SwitchButton()
        self.refuse_chap = SwitchButton()
        self.refuse_mschap = SwitchButton()
        self.refuse_mschapv2 = SwitchButton()
        
        self.method_table = gtk.Table(23, 3, False)

        # visible settings

        self.compression_title = TitleBar(None,
                                          _("Compression"),
                                          width=self.TABLE_WIDTH,
                                          has_separator=False)

        self.echo_title = TitleBar(None,
                                   _("Echo"),
                                   width=self.TABLE_WIDTH,
                                   has_separator=False)


        #compressio))n = Label(_("Compression"), text_size=TITLE_FONT_SIZE)
        self.require_mppe_label = Label(_("Use point-to-point encryption(mppe)"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.require_mppe_128_label = Label(_("Require 128-bit encryption"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.mppe_stateful_label = Label(_("Use stateful MPPE"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.nobsdcomp_label = Label(_("Allow BSD data Compression"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.nodeflate_label = Label(_("Allow Deflate data compression"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.no_vj_comp_label = Label(_("Use TCP header compression"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        #echo = Label("Echo", text_size=TITLE_FONT_SIZE)
        self.ppp_echo_label = Label(_("Send PPP echo packets"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.nopcomp_label = Label(_("Use protocal field compression negotiation"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.noaccomp_label = Label(_("Use Address/Control compression"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)

        self.require_mppe = SwitchButton()
        self.require_mppe_128 = SwitchButton()
        self.mppe_stateful = SwitchButton()
        
        self.nobsdcomp = SwitchButton()
        self.nodeflate = SwitchButton()
        self.no_vj_comp = SwitchButton()
        self.nopcomp = SwitchButton()
        self.noaccomp = SwitchButton() 
        self.ppp_echo = SwitchButton()
        self.ip_sec_enable = SwitchButton()

        ## Settings for IPSec
        self.ipsec_title = TitleBar(None,
                                    _("IPSec Setting"),
                                    width=self.TABLE_WIDTH,
                                    has_separator=False)

        self.ip_sec_enable_label = Label(_("Enable IPSec tunnel to l2tp host"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.group_name_label = Label(_("Group Name:"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.gateway_id_label = Label(_("Gateway ID:"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.pre_shared_key_label = Label(_("Pre_Shared_key"), text_size=CONTENT_FONT_SIZE,
                               enable_select=False,
                               enable_double_click=False)
        self.group_name = InputEntry()
        self.group_name.set_size(self.TABLE_WIDTH, 22)
        self.gateway_id = InputEntry()
        self.gateway_id.set_size(self.TABLE_WIDTH, 22)
        self.pre_shared_key = PasswordEntry()
        self.pre_shared_key.set_text = self.pre_shared_key.entry.set_text
        self.pre_shared_key.set_size(self.TABLE_WIDTH, 22)
        
        methods_list = ["refuse_eap", "refuse_eap_label",
                        "refuse_pap", "refuse_pap_label",
                        "refuse_chap", "refuse_chap_label",
                        "refuse_mschap", "refuse_mschap_label",
                        "refuse_mschapv2", "refuse_mschapv2_label"]
        compression_list = ["require_mppe_label", "require_mppe",
                            "require_mppe_128_label", "require_mppe_128",
                            "mppe_stateful_label", "mppe_stateful",
                            "nobsdcomp_label", "nobsdcomp",
                            "nodeflate_label", "nodeflate",
                            "no_vj_comp_label", "no_vj_comp",
                            "nopcomp_label", "nopcomp",
                            "noaccomp_label", "noaccomp"]

        echo_list = ["ppp_echo_label","ppp_echo"]

        ip_sec_list = ["ip_sec_enable_label", "ip_sec_enable",
                       "group_name_label", "group_name",
                       "gateway_id_label", "gateway_id",
                       "pre_shared_key_label", "pre_shared_key"]

        for name in (compression_list+echo_list+methods_list + ip_sec_list):
            widget = getattr(self, name)
            if not name.endswith("label"):
                align = style.wrap_with_align(widget, align="left")
            else:
                align = style.wrap_with_align(widget, width=210)

            setattr(self, name + "_align", align)

        #vbox = gtk.VBox()
        table_align = gtk.Alignment(0, 0, 0, 0)
        table_align.add(self.method_table)
        style.set_table(self.method_table)

        self.pack_start(table_align, False, False)
        self.method_table.set_row_spacing(5, 20)
        self.method_table.set_row_spacing(15, 20)
        self.method_table.set_row_spacing(18, 20)

        Dispatcher.connect("vpn-type-change", lambda w,c:self.refresh(c))
        #align = style.set_box_with_align(vbox, "text")
        #self.add_with_viewport(align)
        #style.draw_background_color(align)


        #confirm_button = Button("Confirm")
        #confirm_button.connect("clicked", self.confirm_button_cb)
        #button_aligns = gtk.Alignment(0.5 , 1, 0, 0)
        #button_aligns.add(confirm_button)
        #self.add(button_aligns)

        #self.require_mppe_128.set_sensitive(False)
        #self.mppe_stateful.set_sensitive(False)
        ##self.refresh()
    
    def init_signal(self):
        self.refuse_eap.connect("toggled", self.check_button_cb, "refuse-eap")
        self.refuse_pap.connect("toggled", self.check_button_cb, "refuse-pap")
        self.refuse_chap.connect("toggled", self.check_button_cb, "refuse-chap")
        self.refuse_mschap.connect("toggled", self.check_button_cb, "refuse-mschap")
        self.refuse_mschapv2.connect("toggled", self.check_button_cb, "refuse-mschapv2")
        self.require_mppe.connect("toggled", self.click_mppe_callback, "require-mppe")
        self.require_mppe_128.connect("toggled", self.check_button_cb, "require-mppe-128")
        self.mppe_stateful.connect("toggled", self.check_button_cb,"mppe-stateful")
        self.nobsdcomp.connect("toggled", self.check_button_cb, "nobsdcomp")
        self.nodeflate.connect("toggled", self.check_button_cb, "nodeflate")
        self.no_vj_comp.connect("toggled", self.check_button_cb, "no-vj-comp")
        self.ppp_echo.connect("toggled", self.check_button_cb, "echo")
        self.nopcomp.connect("toggled", self.check_button_cb, "nopcomp")
        self.noaccomp.connect("toggled", self.check_button_cb, "noaccomp")

        self.ip_sec_enable.connect("toggled", self.enable_ipsec_cb)
        #self.group_name.entry.connect("focus-out-event", self.entry_focus_out_cb, "ipsec-group-name")
        #self.gateway_id.entry.connect("focus-out-event", self.entry_focus_out_cb, "ipsec-gataway-id")
        #self.pre_shared_key.entry.connect("focus-out-event", self.entry_focus_out_cb, "ipsec-psk")
        self.group_name.entry.connect("changed", self.entry_changed_cb, "ipsec-group-name")
        self.gateway_id.entry.connect("changed", self.entry_changed_cb, "ipsec-gateway-id")
        self.pre_shared_key.entry.connect("changed", self.entry_changed_cb, "ipsec-psk")

    def init_ui(self):

        self.service_type = self.vpn_setting.service_type.split(".")[-1]
        def table_attach(widget_name, row, padding=0):
            label = getattr(self, widget_name + "_label_align")
            widget = getattr(self, widget_name + "_align")
            self.method_table.attach(label, 0, 2, row, row + 1)
            self.method_table.attach(widget, 2, 3, row, row + 1, xpadding=padding)
        #print self.service_type
        container_remove_all(self.method_table)
        self.method_table.attach(self.method_title, 0, 3, 0, 1)
        table_attach( "refuse_eap", 1)
        table_attach( "refuse_pap", 2 )
        table_attach( "refuse_chap", 3)
        table_attach( "refuse_mschap", 4 )
        table_attach( "refuse_mschapv2", 5)
        self.method_table.attach( self.compression_title, 0, 3, 6 ,7)
        table_attach("require_mppe", 8)
        if self.require_mppe.get_active():
            table_attach("require_mppe_128", 9)
            table_attach("mppe_stateful", 10)
        table_attach("nobsdcomp", 11)
        table_attach("nodeflate", 12)
        table_attach("no_vj_comp", 13)
        self.method_table.attach(self.echo_title, 0, 3, 16, 17)
        table_attach("ppp_echo", 18)

        if self.service_type == "l2tp":
            #print "this is l2tp"
            table_attach("nopcomp", 14)
            table_attach("noaccomp", 15)
            
            self.method_table.attach(self.ipsec_title, 0, 3, 19, 20)
            table_attach("ip_sec_enable", 20)
            if self.ip_sec_enable.get_active():
                table_attach("group_name", 21)
                table_attach("gateway_id", 22)
                table_attach("pre_shared_key", 23)

        self.method_table.show_all()



    def refresh(self, connection):
        self.connection = connection
        self.vpn_setting = self.connection.get_setting("vpn")
        #=========================
        # retreieve settings
        self.service_type = self.vpn_setting.service_type.split(".")[-1]
        #print ">>",self.vpn_setting.data
        refuse_eap = self.vpn_setting.get_data_item("refuse-eap")
        refuse_pap = self.vpn_setting.get_data_item("refuse-pap")
        refuse_chap = self.vpn_setting.get_data_item("refuse-chap")
        refuse_mschap = self.vpn_setting.get_data_item("refuse-mschap")
        refuse_mschapv2 = self.vpn_setting.get_data_item("refuse-mschapv2")

        require_mppe = self.vpn_setting.get_data_item("require-mppe")
        require_mppe_128 = self.vpn_setting.get_data_item("require-mppe-128")
        mppe_stateful = self.vpn_setting.get_data_item("mppe-stateful")

        nobsdcomp = self.vpn_setting.get_data_item("nobsdcomp")
        nodeflate = self.vpn_setting.get_data_item("nodeflate")
        no_vj_comp = self.vpn_setting.get_data_item("no-vj-comp")


        lcp_echo_failure = self.vpn_setting.get_data_item("lcp-echo-failure")
        lcp_echo_interval = self.vpn_setting.get_data_item("lcp-echo-interval")
        
        self.refuse_mschap.set_active(refuse_mschap == None)
        self.refuse_mschapv2.set_active(refuse_mschapv2 == None)
        self.require_mppe.set_active(require_mppe != None)
        
        self.refuse_eap.set_active(refuse_eap == None)
        self.refuse_pap.set_active(refuse_pap == None)
        self.refuse_chap.set_active(refuse_chap == None)

        self.require_mppe_128.set_active(require_mppe_128 != None)
        if self.require_mppe_128.get_active() or self.mppe_stateful.get_active():
            self.require_mppe.set_active(True)
        self.mppe_stateful.set_active(mppe_stateful != None)
        self.nobsdcomp.set_active(nobsdcomp == None)
        self.nodeflate.set_active(nodeflate == None)
        self.no_vj_comp.set_active(no_vj_comp == None)

        if self.service_type == "l2tp":
            nopcomp = self.vpn_setting.get_data_item("nopcomp")
            noaccomp = self.vpn_setting.get_data_item("noaccomp")
            ipsec_enabled = self.vpn_setting.get_data_item("ipsec-enabled")

            self.nopcomp.set_active(nopcomp == None)
            self.noaccomp.set_active(noaccomp == None)
            
            if ipsec_enabled:
                self.ip_sec_enable.set_active(True)

                ipsec_group_name = self.vpn_setting.get_data_item("ipsec-group-name")
                ipsec_gateway_id = self.vpn_setting.get_data_item("ipsec-gateway-id")
                ipsec_psk = self.vpn_setting.get_data_item("ipsec-psk")
                self.group_name.set_text(ipsec_group_name)
                self.gateway_id.set_text(ipsec_gateway_id)
                self.pre_shared_key.set_text(ipsec_psk)

            else:
                self.ip_sec_enable.set_active(False)

        if lcp_echo_failure == None and lcp_echo_interval == None:
            self.ppp_echo.set_active(False)
        else:
            self.ppp_echo.set_active(True)

        self.init_signal()
        self.init_lock = False
        self.require_mppe.emit("toggled")
        self.init_lock = True
        self.init_ui()
        #==================================
        # Connect signal
    def enable_ipsec_cb(self, widget):
        active = widget.get_active()
        if active:
            self.vpn_setting.set_data_item("ipsec-enabled", "yes")
            self.group_name.set_sensitive(True)
            self.gateway_id.set_sensitive(True)
            self.pre_shared_key.set_sensitive(True)

            ipsec_group_name = self.vpn_setting.get_data_item("ipsec-group-name")
            ipsec_gateway_id = self.vpn_setting.get_data_item("ipsec-gateway-id")
            ipsec_psk = self.vpn_setting.get_data_item("ipsec-psk")
            
            #print ipsec_group_name
            self.group_name.set_text(ipsec_group_name)
            self.gateway_id.set_text(ipsec_gateway_id)
            self.pre_shared_key.set_text(ipsec_psk)
            self.init_ui()
        else:
            self.vpn_setting.delete_data_item("ipsec-enabled")
            self.group_name.set_text("")
            self.gateway_id.set_text("")
            self.pre_shared_key.set_text("")

            self.group_name.set_sensitive(False)
            self.gateway_id.set_sensitive(False)
            self.pre_shared_key.set_sensitive(False)
            self.init_ui()

    def entry_focus_out_cb(self, widget, event, key):
        text = widget.get_text()
        if text and key != "name":
            self.vpn_setting.set_data_item(key, text)
        else:
            self.vpn_setting.delete_data_item(key)
    def entry_changed_cb(self, widget, string, key):
        if string == "":
            #print key,"entry is empty"
            self.vpn_setting.delete_data_item(key)
        elif key != "name":
            self.vpn_setting.set_data_item(key, string)

        if self.connection.check_setting_finish():
            Dispatcher.set_button("save", True)
        else:
            Dispatcher.set_button("save", False)


    def check_button_cb(self, widget, key):
        auth_lock = self.auth_lock()
        active = widget.get_active()
        if key.startswith("refuse"):
            if active:
                self.vpn_setting.delete_data_item(key)
            else:
                self.vpn_setting.set_data_item(key, "yes")

            if auth_lock:
                self.require_mppe_label.set_sensitive(False)
                self.require_mppe.set_sensitive(False)

                self.set_group_sensitive(True)
            else:
                self.require_mppe_label.set_sensitive(True)
                self.require_mppe.set_sensitive(True)

        elif key.startswith("no"):
            if active:
                self.vpn_setting.delete_data_item(key)
            else:
                self.vpn_setting.set_data_item(key, "yes")

        elif key == "echo":
            if active:
                self.vpn_setting.set_data_item("lcp-echo-failure", "5")
                self.vpn_setting.set_data_item("lcp-echo-interval", "30")
            else:
                self.vpn_setting.delete_data_item("lcp-echo-failure")
                self.vpn_setting.delete_data_item("lcp-echo-interval")
        elif key.startswith("ipsec"):
            if active:
                self.vpn_setting.set_data_item(key, "yes")
            else:
                self.vpn_setting.delete_data_item(key)
        elif key.startswith("require") or key.startswith("mppe"):
            if active:
                self.vpn_setting.set_data_item(key, "yes")
            else:
                self.vpn_setting.delete_data_item(key)
            

        if self.connection.check_setting_finish():
            Dispatcher.set_button("save", True)
        else:
            Dispatcher.set_button("save", False)
    
    def click_mppe_callback(self, widget, key):
        active = widget.get_active()
        if active and key != "name":
            self.vpn_setting.set_data_item(key, "yes")
            #self.set_group_active(False)
            self.set_group_sensitive(False)

            self.mppe_group_set_sensitive(True)
            self.init_ui()
        else:
            #self.set_group_active(True)
            self.set_group_sensitive(True)
            self.vpn_setting.delete_data_item(key)
            self.mppe_group_set_active(False)
            self.mppe_group_set_sensitive(False)
            self.init_ui()
        
        if self.init_lock:
            if self.connection.check_setting_finish():
                Dispatcher.set_button("save", True)
            else:
                Dispatcher.set_button("save", False)

        if self.auth_lock():
            self.require_mppe_label.set_sensitive(False)
            self.require_mppe.set_sensitive(False)
            return


    def mppe_group_set_sensitive(self, boolean):
        self.require_mppe_128_label.set_sensitive(boolean)
        self.mppe_stateful_label.set_sensitive(boolean)
        self.require_mppe_128.set_sensitive(boolean)
        self.mppe_stateful.set_sensitive(boolean)

    def mppe_group_set_active(self, boolean):
        log.debug()
        self.require_mppe_128.set_active(boolean)
        self.mppe_stateful.set_active(boolean)

    def confirm_button_cb(self, widget):
        self.module_frame.send_message("change_crumb", 2)
        nm_module.slider._slide_to_page("vpn", "none")
        
    def auth_lock(self):
        if self.refuse_mschap.get_active() or self.refuse_mschapv2.get_active():
            return False
        else:
            return True

    def set_group_active(self, boolean):
        self.refuse_eap.set_active(boolean)
        self.refuse_pap.set_active(boolean)
        self.refuse_chap.set_active(boolean)

    def set_group_sensitive(self, boolean):
        self.refuse_eap.set_sensitive(boolean)
        self.refuse_pap.set_sensitive(boolean)
        self.refuse_chap.set_sensitive(boolean)
        self.refuse_eap_label.set_sensitive(boolean)
        self.refuse_pap_label.set_sensitive(boolean)
        self.refuse_chap_label.set_sensitive(boolean)
class PPTPConf(gtk.VBox):
    ENTRY_WIDTH = 222
    LEFT_PADDING = 210
    def __init__(self, connection, module_frame, set_button_callback=None, settings_obj=None):
        gtk.VBox.__init__(self)
        self.connection = connection
        self.tab_name = _("PPTP")
        self.module_frame = module_frame
        self.set_button = set_button_callback
        # 新增settings_obj变量,用于访问shared_methods.Settings对象
        self.settings_obj = settings_obj

        self.vpn_setting = self.connection.get_setting("vpn")

        # UI

        pptp_table = gtk.Table(7, 4, False)

        name_label = Label(_("Connection Name:"),
                               enable_select=False,
                               enable_double_click=False)
        name_label.set_can_focus(False)
        gateway_label = Label(_("Gateway:"),
                               enable_select=False,
                               enable_double_click=False)
        gateway_label.set_can_focus(False)
        user_label = Label(_("Username:"******"Password:"******"NT Domain:"),
                               enable_select=False,
                               enable_double_click=False)
        nt_domain_label.set_can_focus(False)
        # Radio Button
        self.pptp_radio = RadioButton(_("PPTP"))
        self.l2tp_radio = RadioButton(_("L2TP"))
        radio_box = gtk.HBox(spacing=30)
        radio_box.pack_start(self.pptp_radio, True, True)
        radio_box.pack_start(self.l2tp_radio, True, True)
        #pack labels
        pptp_table.attach(style.wrap_with_align(radio_box, align="left"), 2,4, 0, 1)
        pptp_table.attach(style.wrap_with_align(name_label, width=self.LEFT_PADDING), 0, 2, 1, 2)
        pptp_table.attach(style.wrap_with_align(gateway_label, width=self.LEFT_PADDING), 0, 2 , 2, 3)
        pptp_table.attach(style.wrap_with_align(user_label, width=self.LEFT_PADDING), 0, 2, 3, 4)
        pptp_table.attach(style.wrap_with_align(password_label, width=self.LEFT_PADDING), 0, 2, 4, 5)
        #pptp_table.attach(style.wrap_with_align(nt_domain_label), 0, 2, 5, 6)

        # entries
        self.name_entry = InputEntry()
        self.name_entry.set_size(self.ENTRY_WIDTH, 22)

        self.gateway_entry = InputEntry()
        self.gateway_entry.set_size(self.ENTRY_WIDTH,22)
        self.user_entry = InputEntry()
        self.user_entry.set_size(self.ENTRY_WIDTH, 22)
        # FIXME should change to new_entry PasswordEntry
        self.password_entry = PasswordEntry()
        self.password_entry.set_size(self.ENTRY_WIDTH, 22)
        self.password_show = CheckButton(_("Show Password"), padding_x=0)
        self.password_show.set_active(False)
        self.password_show.connect("toggled", self.show_password)
        self.nt_domain_entry = InputEntry()
        self.nt_domain_entry.set_size(self.ENTRY_WIDTH, 22)

        #pack entries
        pptp_table.attach(style.wrap_with_align(self.name_entry, align="left"), 2, 4, 1, 2)
        pptp_table.attach(style.wrap_with_align(self.gateway_entry, align="left"), 2, 4, 2, 3)
        pptp_table.attach(style.wrap_with_align(self.user_entry, align="left"), 2, 4, 3, 4)
        pptp_table.attach(style.wrap_with_align(self.password_entry, align="left"), 2, 4, 4, 5)
        pptp_table.attach(style.wrap_with_align(self.password_show, align="left"), 2, 4, 5, 6)
        #pptp_table.attach(style.wrap_with_align(self.nt_domain_entry), 2, 4, 5, 6)
        # Advance setting button
        #advanced_button = Button(_("Advanced Setting"))
        #advanced_button.connect("clicked", self.advanced_button_click)

        #pptp_table.attach(style.wrap_with_align(advanced_button), 3, 4, 6, 7)
        self.service_type = self.vpn_setting.service_type.split(".")[-1]
        if self.service_type == "l2tp":
            self.l2tp_radio.set_active(True)
        else:
            self.pptp_radio.set_active(True)
        self.pptp_radio.connect("toggled",self.radio_toggled, "pptp")
        self.l2tp_radio.connect("toggled",self.radio_toggled, "l2tp")
        # set signals

        #align = style.set_box_with_align(pptp_table, "text")
        table_align = gtk.Alignment(0, 0, 0, 0)
        table_align.add(pptp_table)
        #style.set_table_items(pptp_table, "entry")
        style.draw_background_color(self)
        style.set_table(pptp_table)
        self.add(table_align)
        self.show_all()
        self.refresh()
        self.name_entry.entry.connect("changed", self.entry_changed, "name")
        self.gateway_entry.entry.connect("changed", self.entry_changed, "gateway")
        self.user_entry.entry.connect("changed", self.entry_changed, "user")
        self.password_entry.entry.connect("changed", self.entry_changed, "password")
        self.nt_domain_entry.entry.connect("changed", self.entry_changed, "domain")

        #if self.connection.check_setting_finish():
            #print "in vpn"
            #Dispatcher.set_button("save", True)
        #else:
            #print "in vpn"
            #Dispatcher.set_button("save", False)
        ##############
        #is_valid = self.connection.check_setting_finish()
        #self.settings_obj.vpn_is_valid = is_valid
        #self.settings_obj.set_button("save", is_valid)

    def refresh(self):
        #print ">>>",self.vpn_setting.data
        #print self.vpn_setting.data
        name = self.connection.get_setting("connection").id
        gateway = self.vpn_setting.get_data_item("gateway")
        user = self.vpn_setting.get_data_item("user")
        domain = self.vpn_setting.get_data_item("domain")

        if type(self.connection) == NMRemoteConnection:
            self.name_entry.set_text(name)

        if gateway:
            self.gateway_entry.set_text(gateway)
        if user:
            self.user_entry.set_text(user)

        (setting_name, method) = self.connection.guess_secret_info() 
        try:
            password = nm_module.secret_agent.agent_get_secrets(self.connection.object_path,
                                                    setting_name,
                                                    method)
            if password is None:
                #self.password_entry.entry.set_text("")
                self.password_entry.entry.set_text("")
            else:
                #self.password_entry.entry.set_text(password)
                self.password_entry.entry.set_text(password)
                self.vpn_setting.set_secret_item("password", password)
        except:
            pass

        if domain:
            self.nt_domain_entry.set_text(domain)
                
    def save_setting(self):
        pass

    def show_password(self, widget):
        if widget.get_active():
            self.password_entry.show_password(True)
        else:
            self.password_entry.show_password(False)


    def entry_changed(self, widget, content, item):
        text = content
        if item == "name":
            self.connection.get_setting("connection").id = content

        if text:
            if item == "password":
                self.vpn_setting.set_secret_item(item, text)
            elif item != "name":
                self.vpn_setting.set_data_item(item, text)
            
        else:
            if item == "password":
                self.vpn_setting.set_secret_item(item, "")
            else:
                self.vpn_setting.delete_data_item(item)

        #if self.connection.check_setting_finish():
            #Dispatcher.set_button("save", True)
        #else:
            #Dispatcher.set_button("save", False)
        ##############
        is_valid = self.connection.check_setting_finish()
        self.settings_obj.vpn_is_valid = is_valid
        self.settings_obj.set_button("save", is_valid)
    
    def radio_toggled(self, widget, service_type):
        if widget.get_active():
            self.vpn_setting.service_type = "org.freedesktop.NetworkManager." + service_type
            self.service_type = service_type

        if self.connection.check_setting_finish():
            Dispatcher.set_button("save", True)
        else:
            Dispatcher.set_button("save", False)
            self.refresh()

        Dispatcher.emit("vpn-type-change", self.connection)
            

    def advanced_button_click(self, widget):
        ppp = PPPConf(self.module_frame, Dispatcher.set_button)
        ppp.refresh(self.connection)
        Dispatcher.send_submodule_crumb(3, _("Advanced"))
        nm_module.slider.slide_to_page(ppp, "none")