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 Section(gtk.HBox):
    TOGGLE_INSENSITIVE = 0
    TOGGLE_INACTIVE = 1
    TOGGLE_ACTIVE = 2

    def __init__(self, icon, text, has_separater=True):
        gtk.HBox.__init__(self)
        self.icon = icon
        self.text = text
        self.height = 30
        self.set_size_request(-1, self.height)
        self.has_separater = has_separater

        self.__init_ui()

    def __init_ui(self):
        icon = ImageBox(self.icon)
        self.label = Label(self.text)
        self.offbutton = SwitchButton()
        self.pack_start(self.__wrap_with_align(icon), False, False)
        self.pack_start(self.__wrap_with_align(self.label, align="left"),
                        False,
                        False,
                        padding=10)
        self.pack_end(self.__wrap_with_align(self.offbutton), False, False)
        self.show_all()

    def connect_to_toggle(self, toggle_func):
        self.offbutton.connect("toggled", lambda w: toggle_func())
        self.timer = Timer(200, toggle_func)

    def toggle_callback(self, widget):
        if self.timer.alive():
            self.timer.restart()
        else:
            self.timer.start()

    def __wrap_with_align(self, widget, align="right", h=25):
        if align is "left":
            align = gtk.Alignment(0, 0.5, 1, 0)
        elif align is "right":
            align = gtk.Alignment(1, 0.5, 0, 0)
            align.set_padding(0, 0, 1, 0)
        align.set_size_request(-1, h)
        align.add(widget)
        return align

    def get_active(self):
        return self.offbutton.get_active()

    def set_active(self, state, emit=False):
        '''
        state format : (dev_state, con_state)
        '''
        tray_log.debug("someone set button", state)
        #print "---------------------"
        #print "someone set off button"
        #print "state", state
        #print "active", self.get_active()
        #print "------------------------"
        (dev_state, con_state) = state
        if dev_state is False:
            self.offbutton.set_active(False)
            self.label.set_sensitive(dev_state)
            self.offbutton.set_sensitive(dev_state)
        else:
            self.label.set_sensitive(dev_state)
            self.offbutton.set_sensitive(dev_state)
            self.offbutton.set_active(con_state)
        if emit:
            self.offbutton.emit("toggled")

    def set_sensitive(self, state):
        self.offbutton.set_sensitive(state)

    def set_padding(self, child, padding):
        self.set_child_packing(child, False, False, padding, gtk.PACK_START)
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)