class ProxyConfig(gtk.VBox):
    ENTRY_WIDTH = 222

    def __init__(self):

        gtk.VBox.__init__(self)

        self.proxysetting = ProxySettings()
        self.__init_widget()
        #self.init()

    def __row_entry_spin(self, label_name, table, types):
        label = Label(
            label_name,
            text_size=CONTENT_FONT_SIZE,
            enable_select=False,
            enable_double_click=False)
        label.set_can_focus(False)

        label_align = style.wrap_with_align(label, width=260)

        entry = InputEntry()
        entry.set_size(self.ENTRY_WIDTH, 22)
        spin = SpinBox(0, 0, 49151, 1, 60)
        spin.value_entry.connect("changed",
                                 lambda w, v: spin.update_and_emit(int(v)))

        hbox = gtk.HBox(spacing=10)
        hbox.set_size_request(-1, 22)
        hbox_align = style.wrap_with_align(hbox, align="left")
        hbox.pack_start(entry)
        hbox.pack_start(spin)

        table.row_attach((label_align, hbox_align))
        return [entry, spin]

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect, exclude=["left", "right", "top"])

    def __row_check(self, label_name, table, main=None):
        check = RadioButton(main, label_name)
        check_align = style.wrap_with_align(check, align="left", width=260)
        check_align.set_padding(0, 0, 200, 1)
        table.row_attach(check_align)
        return check

    def __init_widget(self):
        self.manual_table = TableAsm()

        self.manual_radio = self.__row_check(
            _("Manual"), self.manual_table, None)
        self.http_entry, self.http_spin = self.__row_entry_spin(
            _("Http Proxy"), self.manual_table, "http")
        self.https_entry, self.https_spin = self.__row_entry_spin(
            _("Https Proxy"), self.manual_table, "https")
        self.ftp_entry, self.ftp_spin = self.__row_entry_spin(
            _("FTP Proxy"), self.manual_table, "ftp")
        self.socks_entry, self.socks_spin = self.__row_entry_spin(
            _("Socks Proxy"), self.manual_table, "socks")

        self.auto_table = TableAsm(
            left_width=STANDARD_LINE, right_width=self.ENTRY_WIDTH)
        self.auto_radio = self.__row_check(
            _("Automatic"), self.auto_table, self.manual_radio)
        self.conf_entry = self.auto_table.row_input_entry(
            _("Configuration url"))

        auto_align = gtk.Alignment(0, 0, 0, 0)
        auto_align.add(self.auto_table)

        table_box = gtk.VBox(spacing=15)
        table_box.pack_start(self.manual_table, False, False)
        table_box.pack_start(auto_align, False, False)

        align = gtk.Alignment(0, 0, 0, 0)
        align.set_padding(35, 0, 0, 0)
        align.add(table_box)
        self.pack_start(align)

        apply_button = Button(_("Apply"))
        apply_button.connect("clicked", self.save_changes)
        foot_box = FootBox()
        align.connect("expose-event", self.expose_line)
        foot_box.set_buttons([apply_button])
        self.pack_end(foot_box, False, False)
        #self.connect("expose-event", self.expose_event)

        self.manual_radio.connect("toggled", self.manual_radio_selected_cb)
        self.auto_radio.connect("toggled", self.auto_radio_selected_cb)
        self.manual_table.table_build()
        self.auto_table.table_build()

    def manual_radio_selected_cb(self, widget):
        log.debug("")
        sensitive = widget.get_active()
        self.table_set_sensitive(self.manual_table, sensitive)

    def auto_radio_selected_cb(self, widget):
        log.debug("")
        sensitive = widget.get_active()
        self.table_set_sensitive(self.auto_table, sensitive)

    def table_set_sensitive(self, table, sensitive):
        items = table.shared
        for item in items:
            if item[1] != None:
                map(lambda i: i.set_sensitive(sensitive), item)
            else:
                log.debug("radio button", item)

    def init(self):
        mode = self.proxysetting.get_proxy_mode()
        if mode == "manual":
            self.manual_radio.set_active(True)
            self.table_set_sensitive(self.auto_table, False)
            self.proxysetting.set_http_enabled(True)

            http_host = self.proxysetting.get_http_host()
            http_port = self.proxysetting.get_http_port() or 8080
            https_host = self.proxysetting.get_https_host()
            https_port = self.proxysetting.get_https_port()
            ftp_host = self.proxysetting.get_ftp_host()
            ftp_port = self.proxysetting.get_ftp_port()
            socks_host = self.proxysetting.get_socks_host()
            socks_port = self.proxysetting.get_socks_port()

            self.http_entry.set_text(http_host)
            self.http_spin.set_value(int(http_port))
            self.https_entry.set_text(https_host)
            self.https_spin.set_value(int(https_port))
            self.ftp_entry.set_text(ftp_host)
            self.ftp_spin.set_value(int(ftp_port))
            self.socks_entry.set_text(socks_host)
            self.socks_spin.set_value(int(socks_port))
        elif mode == "auto":
            self.auto_radio.set_active(True)
            self.table_set_sensitive(self.manual_table, False)
            conf_url = self.proxysetting.get_proxy_authconfig_url()
            self.conf_entry.set_text(conf_url)

    def save_changes(self, widget):

        log.debug(self.manual_radio.get_active())
        #self.proxysetting.set_proxy_mode("none")
        if self.manual_radio.get_active():
            http_host = self.http_entry.get_text()
            http_port = self.http_spin.get_value()
            https_host = self.https_entry.get_text()
            https_port = self.https_spin.get_value()
            ftp_host = self.ftp_entry.get_text()
            ftp_port = self.ftp_spin.get_value()
            socks_host = self.socks_entry.get_text()
            socks_port = self.socks_spin.get_value()

            mode = "manual"
            self.proxysetting.set_proxy_mode(mode)
            self.proxysetting.set_http_host(http_host)
            self.proxysetting.set_http_port(http_port)
            self.proxysetting.set_https_host(https_host)
            self.proxysetting.set_https_port(https_port)
            self.proxysetting.set_ftp_host(ftp_host)
            self.proxysetting.set_ftp_port(ftp_port)

            self.proxysetting.set_socks_host(socks_host)
            self.proxysetting.set_socks_port(socks_port)

        else:
            conf_url = self.conf_entry.get_text()
            self.proxysetting.set_proxy_mode("auto")
            self.proxysetting.set_proxy_autoconfig_url(conf_url)

        Dispatcher.to_main_page()
class PPPConf(gtk.VBox):
    TABLE_WIDTH = 1
    def __init__(self, connection, set_button_callback, settings_obj=None):
        gtk.VBox.__init__(self)
        self.tab_name = _("PPP")
        self.connection = connection
        self.set_button = set_button_callback
        # 新增settings_obj变量,用于访问shared_methods.Settings对象
        self.settings_obj = settings_obj
        self.ppp_setting = self.connection.get_setting("ppp")

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

        self.method_table = TableAsm()
        self.method_table.row_attach(self.method_title)
        self.refuse_eap = self.method_table.row_toggle(_("EAP"))
        self.refuse_pap = self.method_table.row_toggle(_("PAP"))
        self.refuse_chap = self.method_table.row_toggle(_("CHAP"))
        self.refuse_mschap = self.method_table.row_toggle(_("MSCHAP"))
        self.refuse_mschapv2 = self.method_table.row_toggle(_("MSCHAP v2"))
        # 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)

        self.comp_table = TableAsm()
        self.sub_item = []

        #compressio))n = Label(_("Compression"), text_size=TITLE_FONT_SIZE)
        self.comp_table.row_attach(self.compression_title)
        self.require_mppe = self.comp_table.row_toggle(_("Use point-to-point encryption(mppe)"))
        self.require_mppe_128 = self.comp_table.row_toggle(_("Require 128-bit encryption"), self.sub_item)
        self.mppe_stateful = self.comp_table.row_toggle(_("Use stateful MPPE"), self.sub_item)
        self.nobsdcomp = self.comp_table.row_toggle(_("Allow BSD data Compression"))
        self.nodeflate = self.comp_table.row_toggle(_("Allow Deflate data compression"))
        self.no_vj_comp = self.comp_table.row_toggle(_("Use TCP header compression"))

        self.echo_table = TableAsm()
        self.echo_table.row_attach(self.echo_title)
        self.ppp_echo = self.echo_table.row_toggle(_("Send PPP echo packets"))

        self.method_table.table_build()
        self.echo_table.table_build()
        vbox = gtk.VBox()
        table_align = gtk.Alignment(0, 0, 0, 0)
        
        table_align.add(vbox)
        self.pack_start(table_align)
        vbox.pack_start(self.method_table, False, False)
        vbox.pack_start(self.comp_table, False, False)
        vbox.pack_start(self.echo_table, False, False)

        self.refresh()

        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.check_button_cb, "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")


    def refresh_table(self, require_mppe):
        self.comp_table.table_clear()
        if require_mppe:
            #log.debug("comp_table items", self.comp_table.shared, self.sub_item)
            self.comp_table.table_build(self.sub_item, 2)
        else:
            #log.debug("comp_table items", self.comp_table.shared)
            self.comp_table.table_build()
            self.require_mppe_128.set_active(False)
            self.mppe_stateful.set_active(False)
        self.show_all()

    def refresh(self):
        #=========================
        # retreieve settings
        refuse_eap = self.ppp_setting.refuse_eap
        refuse_pap = self.ppp_setting.refuse_pap
        refuse_chap = self.ppp_setting.refuse_chap
        refuse_mschap = self.ppp_setting.refuse_mschap
        refuse_mschapv2 = self.ppp_setting.refuse_mschapv2

        require_mppe = self.ppp_setting.require_mppe
        require_mppe_128 = self.ppp_setting.require_mppe_128
        mppe_stateful = self.ppp_setting.mppe_stateful

        nobsdcomp = self.ppp_setting.nobsdcomp
        nodeflate = self.ppp_setting.nodeflate
        no_vj_comp = self.ppp_setting.no_vj_comp

        lcp_echo_failure = self.ppp_setting.lcp_echo_failure
        lcp_echo_interval = self.ppp_setting.lcp_echo_interval

        self.refuse_eap.set_active( not refuse_eap)
        self.refuse_pap.set_active(not refuse_pap)
        self.refuse_chap.set_active(not refuse_chap)
        self.refuse_mschap.set_active(not refuse_mschap)
        self.refuse_mschapv2.set_active(not refuse_mschapv2)

        self.require_mppe.set_active(require_mppe)
        self.require_mppe_128.set_active(require_mppe_128)
        self.mppe_stateful.set_active(mppe_stateful)
        self.nobsdcomp.set_active(not nobsdcomp)
        self.nodeflate.set_active(not nodeflate)
        self.no_vj_comp.set_active(not no_vj_comp)

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

        self.refresh_table(require_mppe)
        #==================================

    def check_button_cb(self, widget, key):
        active = widget.get_active()
        if key.startswith("refuse"):
            if active:
                setattr(self.ppp_setting, key, False)
            else:
                setattr(self.ppp_setting, key, True)
        elif key.startswith("no"):
            if active:
                setattr(self.ppp_setting, key, False)
            else:
                setattr(self.ppp_setting, key, True)
        elif key is "echo":
            if active:
                setattr(self.ppp_setting, "lcp_echo_failure", 5)
                setattr(self.ppp_setting, "lcp_echo_interval", 30)
            else:
                setattr(self.ppp_setting, "lcp_echo_failure", 0)
                setattr(self.ppp_setting, "lcp_echo_interval", 0)
        else:
            if active:
                setattr(self.ppp_setting, key, True)
            else:
                setattr(self.ppp_setting, key, False)
        #check_settings(self.connection, self.set_button)
        ##################
        is_valid = self.connection.check_setting_finish()
        #self.settings_obj.ppp_is_valid = is_valid
        self.settings_obj.set_button("save", is_valid)

        if key is "require_mppe":
            if active:
                self.refresh_table(1)
            else:
                self.refresh_table(None)
class ProxyConfig(gtk.VBox):
    ENTRY_WIDTH = 222

    def __init__(self):

        gtk.VBox.__init__(self)

        self.proxysetting = ProxySettings()
        self.__init_widget()
        #self.init()

    def __row_entry_spin(self, label_name, table, types):
        label = Label(label_name,
                      text_size=CONTENT_FONT_SIZE,
                      enable_select=False,
                      enable_double_click=False)
        label.set_can_focus(False)

        label_align = style.wrap_with_align(label, width=260)

        entry = InputEntry()
        entry.set_size(self.ENTRY_WIDTH, 22)
        spin = SpinBox(0, 0, 49151, 1, 60)
        spin.value_entry.connect("changed",
                                 lambda w, v: spin.update_and_emit(int(v)))

        hbox = gtk.HBox(spacing=10)
        hbox.set_size_request(-1, 22)
        hbox_align = style.wrap_with_align(hbox, align="left")
        hbox.pack_start(entry)
        hbox.pack_start(spin)

        table.row_attach((label_align, hbox_align))
        return [entry, spin]

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect, exclude=["left", "right", "top"])

    def __row_check(self, label_name, table, main=None):
        check = RadioButton(main, label_name)
        check_align = style.wrap_with_align(check, align="left", width=260)
        check_align.set_padding(0, 0, 200, 1)
        table.row_attach(check_align)
        return check

    def __init_widget(self):
        self.manual_table = TableAsm()

        self.manual_radio = self.__row_check(_("Manual"), self.manual_table,
                                             None)
        self.http_entry, self.http_spin = self.__row_entry_spin(
            _("Http Proxy"), self.manual_table, "http")
        self.https_entry, self.https_spin = self.__row_entry_spin(
            _("Https Proxy"), self.manual_table, "https")
        self.ftp_entry, self.ftp_spin = self.__row_entry_spin(
            _("FTP Proxy"), self.manual_table, "ftp")
        self.socks_entry, self.socks_spin = self.__row_entry_spin(
            _("Socks Proxy"), self.manual_table, "socks")

        self.auto_table = TableAsm(left_width=STANDARD_LINE,
                                   right_width=self.ENTRY_WIDTH)
        self.auto_radio = self.__row_check(_("Automatic"), self.auto_table,
                                           self.manual_radio)
        self.conf_entry = self.auto_table.row_input_entry(
            _("Configuration url"))

        auto_align = gtk.Alignment(0, 0, 0, 0)
        auto_align.add(self.auto_table)

        table_box = gtk.VBox(spacing=15)
        table_box.pack_start(self.manual_table, False, False)
        table_box.pack_start(auto_align, False, False)

        align = gtk.Alignment(0, 0, 0, 0)
        align.set_padding(35, 0, 0, 0)
        align.add(table_box)
        self.pack_start(align)

        apply_button = Button(_("Apply"))
        apply_button.connect("clicked", self.save_changes)
        foot_box = FootBox()
        align.connect("expose-event", self.expose_line)
        foot_box.set_buttons([apply_button])
        self.pack_end(foot_box, False, False)
        #self.connect("expose-event", self.expose_event)

        self.manual_radio.connect("toggled", self.manual_radio_selected_cb)
        self.auto_radio.connect("toggled", self.auto_radio_selected_cb)
        self.manual_table.table_build()
        self.auto_table.table_build()

    def manual_radio_selected_cb(self, widget):
        log.debug("")
        sensitive = widget.get_active()
        self.table_set_sensitive(self.manual_table, sensitive)

    def auto_radio_selected_cb(self, widget):
        log.debug("")
        sensitive = widget.get_active()
        self.table_set_sensitive(self.auto_table, sensitive)

    def table_set_sensitive(self, table, sensitive):
        items = table.shared
        for item in items:
            if item[1] != None:
                map(lambda i: i.set_sensitive(sensitive), item)
            else:
                log.debug("radio button", item)

    def init(self):
        mode = self.proxysetting.get_proxy_mode()
        if mode == "manual":
            self.manual_radio.set_active(True)
            self.table_set_sensitive(self.auto_table, False)
            self.proxysetting.set_http_enabled(True)

            http_host = self.proxysetting.get_http_host()
            http_port = self.proxysetting.get_http_port() or 8080
            https_host = self.proxysetting.get_https_host()
            https_port = self.proxysetting.get_https_port()
            ftp_host = self.proxysetting.get_ftp_host()
            ftp_port = self.proxysetting.get_ftp_port()
            socks_host = self.proxysetting.get_socks_host()
            socks_port = self.proxysetting.get_socks_port()

            self.http_entry.set_text(http_host)
            self.http_spin.set_value(int(http_port))
            self.https_entry.set_text(https_host)
            self.https_spin.set_value(int(https_port))
            self.ftp_entry.set_text(ftp_host)
            self.ftp_spin.set_value(int(ftp_port))
            self.socks_entry.set_text(socks_host)
            self.socks_spin.set_value(int(socks_port))
        elif mode == "auto":
            self.auto_radio.set_active(True)
            self.table_set_sensitive(self.manual_table, False)
            conf_url = self.proxysetting.get_proxy_authconfig_url()
            self.conf_entry.set_text(conf_url)

    def save_changes(self, widget):

        log.debug(self.manual_radio.get_active())
        #self.proxysetting.set_proxy_mode("none")
        if self.manual_radio.get_active():
            http_host = self.http_entry.get_text()
            http_port = self.http_spin.get_value()
            https_host = self.https_entry.get_text()
            https_port = self.https_spin.get_value()
            ftp_host = self.ftp_entry.get_text()
            ftp_port = self.ftp_spin.get_value()
            socks_host = self.socks_entry.get_text()
            socks_port = self.socks_spin.get_value()

            mode = "manual"
            self.proxysetting.set_proxy_mode(mode)
            self.proxysetting.set_http_host(http_host)
            self.proxysetting.set_http_port(http_port)
            self.proxysetting.set_https_host(https_host)
            self.proxysetting.set_https_port(https_port)
            self.proxysetting.set_ftp_host(ftp_host)
            self.proxysetting.set_ftp_port(ftp_port)

            self.proxysetting.set_socks_host(socks_host)
            self.proxysetting.set_socks_port(socks_port)

        else:
            conf_url = self.conf_entry.get_text()
            self.proxysetting.set_proxy_mode("auto")
            self.proxysetting.set_proxy_autoconfig_url(conf_url)

        Dispatcher.to_main_page()
class Wired(gtk.VBox):
    ENTRY_WIDTH = 222

    def __init__(self, connection, set_button_callback=None, settings_obj=None):
        gtk.VBox.__init__(self)
        self.tab_name = _("Wired")
        
        self.ethernet = connection.get_setting("802-3-ethernet")
        self.connection = connection
        self.set_button = set_button_callback
        # 新增settings_obj变量,用于访问shared_methods.Settings对象
        self.settings_obj = settings_obj
        self.settings_obj.initial_lock = True
        #self.settings_obj.set_button("save", True)

        self.__init_table()
        self.__init_signals()

        (mac, clone_mac, mtu) = self.ethernet.mac_address, self.ethernet.cloned_mac_address, self.ethernet.mtu
        if mac != None:
            self.mac_entry.set_address(mac)
        if clone_mac !=None:
            self.clone_entry.set_address(clone_mac)
        if mtu != None:
            self.mtu_spin.set_value(int(mtu))
        
        # check valid for nmconnection init
        #if not type(self.connection) == NMRemoteConnection:
            #self.save_settings(None, None, None)
        self.settings_obj.initial_lock = False

    def __init_table(self):
        self.table = TableAsm()
        self.mac_entry = self.table.row_mac_entry(_("Device Mac Address:"))
        self.clone_entry = self.table.row_mac_entry(_("Cloned Mac Address:"))
        self.mtu_spin = self.table.row_spin(_("MTU:"), 0, 1500)
        self.table.table_build()
        # TODO UI change
        align = gtk.Alignment(0,0,0,0)
        align.add(self.table)
        self.pack_start(align)
   
    def __init_signals(self):
        self.mac_entry.connect("changed", self.save_settings, "mac_address")
        self.clone_entry.connect("changed", self.save_settings, "cloned_mac_address")
        self.mtu_spin.connect("value_changed", self.save_settings, "mtu")
        self.mtu_spin.value_entry.connect("changed", self.spin_user_set)

    def spin_user_set(self, widget, value):
        if value == "":
            return
        value = int(value)
        if self.mtu_spin.lower_value <= value <= self.mtu_spin.upper_value:
            self.mtu_spin.update_and_emit(value)
        elif value < self.mtu_spin.lower_value:
            self.mtu_spin.update_and_emit(self.mtu_spin.lower_value)
        else:
            self.mtu_spin.update_and_emit(self.mtu_spin.upper_value)

        ## retrieve wired info
    def save_settings(self, widget, content, types):
        #value = None
        if types:
            setattr(self.ethernet, types, content)
        if self.settings_obj is None:
            return
        
        # check mac address whether is valid
        if types == "mac_address":
            mac_address = content
            cloned_mac_address = self.clone_entry.get_address()
        elif types == "cloned_mac_address":
            mac_address = self.mac_entry.get_address()
            cloned_mac_address = content
        else:
            mac_address = self.mac_entry.get_address()
            cloned_mac_address = self.clone_entry.get_address()

        if (mac_address == ":::::") or \
                (mac_address == "") or \
                (TypeConvert.is_valid_mac_address(mac_address)):
            mac_address_is_valid = True
        else:
            mac_address_is_valid = False
        if (cloned_mac_address == ":::::") or \
                (cloned_mac_address == "") or \
                (TypeConvert.is_valid_mac_address(cloned_mac_address)):
            cloned_mac_address_is_valid = True
        else:
            cloned_mac_address_is_valid = False
        if mac_address_is_valid and cloned_mac_address_is_valid:
            self.settings_obj.mac_is_valid = True
        else:
            self.settings_obj.mac_is_valid = False

        # 统一调用shared_methods.Settings的set_button
        log.debug('set_button True')
        self.settings_obj.set_button("save", True)

        """