proc profile_clean {} {
	global use_tls have_compress have_sasl have_http_poll have_proxy
	set loginconf(accname) "Some Profile"
 	set loginconf(user) ""
	set loginconf(server) "localhost"
	set loginconf(password) ""
	set loginconf(resource) "Tkabbur"
	set loginconf(autologin) 0
	set loginconf(connect_forever) 0
	set loginconf(allowauthplain) 0
	set loginconf(allowgoogletoken) 1
	if {$have_sasl} {
		set loginconf(usesasl) 1
	}
	set so "plaintext"
	set values [list plaintext [::msgcat::mc "Plaintext"]]
	if {$have_compress} {
	    lappend values compressed [::msgcat::mc "Compression"]
	    set so "compressed"
	}
	if {$use_tls} {
	    lappend values encrypted [::msgcat::mc "Encryption (STARTTLS)"] \
			   ssl       [::msgcat::mc "Encryption (legacy SSL)"]
	    if {$have_compress} {
	    	lappend values tlsandcompress [::msgcat::mc "Encryption (STARTTLS) + Compression"]

	    }
	    set so "encrypted"
	}
	set loginconf(stream_options) $so
	if {$use_tls} {
		set loginconf(sslcertfile) ""
		set loginconf(sslcacertstore) ""
		set loginconf(sslkeyfile) ""
	}
	if {$have_proxy} {
	    set values {}
	    foreach type [jlib::capabilities proxy] {
		switch -- $type {
		    none   {lappend values none   [::msgcat::mc "None"]}
		    socks4 {lappend values socks4 [::msgcat::mc "SOCKS4a"]}
		    socks5 {lappend values socks5 [::msgcat::mc "SOCKS5"]}
		    https  {lappend values https  [::msgcat::mc "HTTPS"]}
		}
	    }
		set loginconf(proxy) none
		set loginconf(proxyhost) "localhost"
		set loginconf(proxyport) 3128
		set loginconf(proxyusername) ""
		set loginconf(proxypassword) ""
		set loginconf(proxyuseragent) "Mozilla/4.0 (compatible; MSIE 6.0;\
	 $::tcl_platform(os) $::tcl_platform(osVersion))"
	}
	set loginconf(usealtserver) 0
	set loginconf(altserver) ""
	set loginconf(altport) "5222"
	if {$have_http_poll} {
		set loginconf(usehttppoll) 0
		set loginconf(pollurl) ""
		set loginconf(usepollkeys) 1
		set loginconf(numberofpollkeys) 100
		set loginconf(polltimeout) 0
		set loginconf(pollmin) 6000
		set loginconf(pollmax) 60000
	}
	return [array get loginconf]
}

proc profile_add_save {} {
	global ltmp
	set file [file join $::configdir profiles.tcl]
	set ultmp {}
	if {[file exists $file]} {
		catch {
			set fd [open $file "r"]
			fconfigure $fd -encoding utf-8
			set ultmp [read $fd]
			close $fd
		}
	}
	set ur {}
	foreach {u1 u2} [array get ltmp] {
		lappend ur [list $u1 $u2]
	}
	lappend ultmp $ur
	catch {
		set fd [open $file "w"]
		fconfigure $fd -encoding utf-8
		puts $fd $ultmp
		close $fd
	}
}


proc profile_edit_save {} {
	global ltmp
	global llindex
	set file [file join $::configdir profiles.tcl]
	set ultmp {}
	if {[file exists $file]} {
		catch {
			set fd [open $file "r"]
			fconfigure $fd -encoding utf-8
			set ultmp [read $fd]
			close $fd
		}
	}

	set nltmp {}
	set n 0
	foreach item $ultmp {
		if {$n==$llindex} {
			set ur {}
			foreach {u1 u2} [array get ltmp] {
				lappend ur [list $u1 $u2]
			}
			lappend nltmp $ur
		} else {
			lappend nltmp $item
		}
		set n [expr $n+1]
	}
	set ultmp $nltmp
	catch {
		set fd [open $file "w"]
		fconfigure $fd -encoding utf-8
		puts $fd $ultmp
		close $fd
	}
	unset llindex
}

proc profile_add_update {l} {
    foreach ent {accname autologin username server port password resource \
            altserver proxyhost proxyport proxyusername proxypassword \
            sslcertfile pollurl} {
        if {[winfo exists $l.$ent]} {
            catch { $l.$ent icursor end }
        }
    }

    foreach {check enable disable} { \
            usehttppoll {lpollurl pollurl usepollkeys} \
			{dontusessl usecompress legacyssl encrypted tlsandcompress \
			 sslcertfile lsslcertfile bsslcertfile} \
            usealtserver {altserver laltserver port lport} {} \
	    usesasl {allowgoogletoken} {} \
	    } {
        if {![info exists ltmp($check)] || ![winfo exists $l.$check]} {
            continue
        }

        if {$ltmp($check) && ![cequal [$l.$check cget -state] disabled]} {
            set state1 normal
            set state2 disabled
        } else {
            set state1 disabled
            set state2 normal
        }
        foreach ent $enable {
            if {[winfo exists $l.$ent]} {
                $l.$ent configure -state $state1
                if {[cequal [focus] $l.$ent] && [cequal $state1 "disabled"]} {
                    focus [Widget::focusPrev $l.$ent]
                }
            }
        }
        foreach ent $disable {
            if {[winfo exists $l.$ent]} {
                $l.$ent configure -state $state2
                if {[cequal [focus] $l.$ent] && [cequal $state2 "disabled"]} {
                    focus [Widget::focusPrev $l.$ent]
                }
	    }
	}
    }
    catch {
	if {[cequal $ltmp(proxy) none]} {
	    foreach ent {proxyhost proxyport proxyusername proxypassword \
			 lproxyhost lproxyport lproxyusername lproxypassword} {
		$l.$ent configure -state disabled
                if {[cequal [focus] $l.$ent]} {
                    focus [Widget::focusPrev $l.$ent]
                }
	    }
	} else {
	    foreach ent {proxyhost proxyport proxyusername proxypassword \
			 lproxyhost lproxyport lproxyusername lproxypassword} {
		$l.$ent configure -state normal
	    }
	}
    }
    catch {
	if {![cequal [$l.dontusessl cget -state] disabled] && \
		($ltmp(stream_options) == "ssl" || \
		 $ltmp(stream_options) == "encrypted")} {
	    $l.sslcertfile configure -state normal
	    $l.lsslcertfile configure -state normal
	    $l.bsslcertfile configure -state normal
	} else {
	    $l.sslcertfile configure -state disabled
	    $l.lsslcertfile configure -state disabled
	    $l.bsslcertfile configure -state disabled
	    if {[cequal [focus] $l.sslcertfile] || \
		    [cequal [focus] $l.bsslcertfile]} {
		focus [Widget::focusPrev $l.sslcertfile]
	    }
	}
    }
}



proc profile_add {$lf} {
	global ltmp
	global use_tls have_compress have_sasl have_http_poll have_proxy

	destroy .login

    if {[winfo exists .lugin]} {
	focus -force .lugin
	return
    }

    array set ltmp [profile_clean]

    Dialog .lugin -title [::msgcat::mc "Add profile"] \
	-separator 1 -anchor e -default 0 -cancel 1

    wm resizable .lugin 0 0

    set l [.lugin getframe]

    set nb [NoteBook $l.nb]

    set account_page [$nb insert end account_page -text [::msgcat::mc "Account"]]

    label $l.laccname -text [::msgcat::mc "Account name:"]
    entry $l.accname -textvariable ltmp(accname)

    checkbutton $l.lautologin \
	-text [::msgcat::mc "Auto-login"] \
	-variable ltmp(autologin) \
	-command [list [namespace current]::profile_add_update $l]


    label $l.lusername -text [::msgcat::mc "Username:"]
    entry $l.username -textvariable ltmp(user)
    label $l.lserver -text [::msgcat::mc "Server:"]
    entry $l.server -textvariable ltmp(server)
    label $l.lpassword -text [::msgcat::mc "Password:"]
    entry $l.password -show * -textvariable ltmp(password)
    label $l.lresource -text [::msgcat::mc "Resource:"]
    entry $l.resource -textvariable ltmp(resource)

    grid $l.laccname -row 0 -column 0 -sticky e -in $account_page
    grid $l.accname  -row 0 -column 1 -sticky ew -in $account_page
    grid $l.lautologin -row 0 -column 3 -sticky ew -in $account_page

    grid $l.lusername -row 1 -column 0 -sticky e -in $account_page
    grid $l.username  -row 1 -column 1 -sticky ew -in $account_page
    grid $l.lserver   -row 1 -column 2 -sticky e -in $account_page
    grid $l.server    -row 1 -column 3 -sticky ew -in $account_page
    grid $l.lpassword -row 2 -column 0 -sticky e -in $account_page
    grid $l.password  -row 2 -column 1 -sticky ew -in $account_page
    grid $l.lresource -row 2 -column 2 -sticky e -in $account_page
    grid $l.resource  -row 2 -column 3 -sticky ew -in $account_page

    grid columnconfigure $account_page 1 -weight 3
    grid columnconfigure $account_page 2 -weight 1
    grid columnconfigure $account_page 3 -weight 3

    set connection_page [$nb insert end connection_page -text [::msgcat::mc "Connection"]]

    checkbutton $l.usealtserver -text [::msgcat::mc "Explicitly specify host and port to connect"] \
	-variable ltmp(usealtserver) \
	-command [list [namespace current]::profile_add_update $l]
    label $l.laltserver -text [::msgcat::mc "Host:"]
    entry $l.altserver -textvariable ltmp(altserver)
    label $l.lport -text [::msgcat::mc "Port:"]
    Spinbox $l.port 0 65535 1 ltmp(altport)

    grid $l.usealtserver -row 0 -column 0 -sticky w -columnspan 4 -in $connection_page
    grid $l.laltserver -row 1 -column 0 -sticky e -in $connection_page
    grid $l.altserver -row 1 -column 1 -sticky ew -in $connection_page
    grid $l.lport     -row 1 -column 2 -sticky e -in $connection_page
    grid $l.port      -row 1 -column 3 -sticky we -in $connection_page

    grid columnconfigure $connection_page 1 -weight 6
    grid columnconfigure $connection_page 2 -weight 1


    set auth_page [$nb insert end auth_page -text [::msgcat::mc "Authentication"]]

    checkbutton $l.allowauthplain \
	-text [::msgcat::mc "Allow plaintext authentication mechanisms"] \
	-variable ltmp(allowauthplain) \
	-command [list [namespace current]::profile_add_update $l]

    grid $l.allowauthplain -row 0 -column 0 -sticky w -in $auth_page

    if {$have_sasl} {
	checkbutton $l.usesasl \
	    -text [::msgcat::mc "Use SASL authentication"] \
	    -variable ltmp(usesasl) \
	    -command [list [namespace current]::profile_add_update $l]

	grid $l.usesasl -row 1 -column 0 -sticky w -in $auth_page

	checkbutton $l.allowgoogletoken \
	    -text [::msgcat::mc "Allow X-GOOGLE-TOKEN SASL mechanism"] \
	    -variable ltmp(allowgoogletoken) \
	    -command [list [namespace current]::profile_add_update $l]

	grid $l.allowgoogletoken -row 2 -column 0 -sticky w -in $auth_page
    }

    grid columnconfigure $auth_page 0 -weight 1

    if {$use_tls || $have_compress} {
	if {$use_tls && $have_compress} {
		set page_label [::msgcat::mc "SSL & Compression"]
	} elseif {$have_compress} {
	    set page_label [::msgcat::mc "Compression"]
	} else {
	    set page_label [::msgcat::mc "SSL"]
	}

	set ssl_page [$nb insert end ssl_page -text $page_label]

	radiobutton $l.dontusessl -text [::msgcat::mc "Plaintext"] \
	    -variable ltmp(stream_options) -value plaintext \
	    -command [list [namespace current]::profile_add_update $l]
        $l.dontusessl configure -state normal
	if {$have_compress} {
	    radiobutton $l.usecompress -text [::msgcat::mc "Compression"] \
		-variable ltmp(stream_options) -value compressed \
		-command [list [namespace current]::profile_add_update $l]
	}
	if {$use_tls} {
	    radiobutton $l.encrypted -text [::msgcat::mc "Encryption (STARTTLS)"] \
		-variable ltmp(stream_options) -value encrypted \
		-command [list [namespace current]::profile_add_update $l]
	    radiobutton $l.legacyssl -text [::msgcat::mc "Encryption (legacy SSL)"] \
		-variable ltmp(stream_options) -value ssl \
		-command [list [namespace current]::profile_add_update $l]
	    if {$have_compress} {
	    	radiobutton $l.tlsandcompress -text [::msgcat::mc "Encryption (STARTTLS) + Compression"] \
			-variable ltmp(stream_options) -value tlsandcompress \
			-command [list [namespace current]::profile_add_update $l]
	    }
	    label $l.lsslcertfile -text [::msgcat::mc "SSL certificate:"]
	    entry $l.sslcertfile -textvariable ltmp(sslcertfile)
	    button $l.bsslcertfile -text [::msgcat::mc "Browse..."] \
		-command [list eval set ltmp(sslcertfile) {[tk_getOpenFile]}]
	}

	set column 0
	grid $l.dontusessl -row 0 -column $column -sticky w -in $ssl_page
	
	if {$have_compress} {
	    grid $l.usecompress -row 0 -column [incr column] -sticky w -in $ssl_page
	}
	if {$use_tls} {
	    grid $l.encrypted     -row 0 -column [incr column] -sticky w -in $ssl_page
	    grid $l.legacyssl    -row 0 -column [incr column] -sticky w -in $ssl_page
	    if {$have_compress} {
		    grid $l.tlsandcompress    -row 0 -column [incr column] -sticky w -in $ssl_page
	    }

	    grid $l.lsslcertfile -row 1 -column 0 -sticky e -in $ssl_page
	    grid $l.sslcertfile  -row 1 -column 1 -sticky ew -columnspan 2 -in $ssl_page
	    grid $l.bsslcertfile -row 1 -column 3 -sticky w -in $ssl_page
	}

	grid columnconfigure $ssl_page 1 -weight 1
	grid columnconfigure $ssl_page 2 -weight 1
    }

    if {$have_http_poll} {
	set httppoll_page [$nb insert end httpoll_page -text [::msgcat::mc "HTTP Poll"]]

	checkbutton $l.usehttppoll -text [::msgcat::mc "Connect via HTTP polling"] \
	    -variable ltmp(usehttppoll) \
	    -command [list [namespace current]::profile_add_update $l]
	label $l.lpollurl -text [::msgcat::mc "URL to poll:"]
	entry $l.pollurl -textvariable ltmp(pollurl)
	checkbutton $l.usepollkeys -text [::msgcat::mc "Use client security keys"] \
	    -state disabled \
	    -variable ltmp(usepollkeys) \
	    -command [list [namespace current]::profile_add_update $l]
    
	grid $l.usehttppoll -row 0 -column 0 -sticky w -columnspan 3 -in $httppoll_page
	grid $l.lpollurl -row 1 -column 0 -sticky e -in $httppoll_page
	grid $l.pollurl -row 1 -column 1 -sticky ew -in $httppoll_page
	grid $l.usepollkeys -row 2 -column 0 -sticky w -columnspan 3 -in $httppoll_page

	grid columnconfigure $httppoll_page 1 -weight 1
    }
    
    if {$have_proxy} {
	set proxy_page [$nb insert end proxy_page -text [::msgcat::mc "Proxy"]]

	label $l.lproxy -text [::msgcat::mc "Proxy type:"]
	grid $l.lproxy -row 0 -column 0 -sticky e -in $proxy_page
	frame $l.proxy
	grid $l.proxy -row 0 -column 1 -columnspan 3 -sticky w -in $proxy_page

	set col 0
	foreach type [jlib::capabilities proxy] {
	    switch -- $type {
		none {
		   radiobutton $l.proxy.none -text [::msgcat::mc "None"] \
			-variable ltmp(proxy) -value none \
			-command [list [namespace current]::profile_add_update $l]
		    grid $l.proxy.none -row 0 -column [incr col] -sticky w
		}
		socks4 {
		   radiobutton $l.proxy.socks4 -text [::msgcat::mc "SOCKS4a"] \
			-variable ltmp(proxy) -value socks4 \
			-command [list [namespace current]::profile_add_update $l]
		    grid $l.proxy.socks4 -row 0 -column [incr col] -sticky w
		}
		socks5 {
		   radiobutton $l.proxy.socks5 -text [::msgcat::mc "SOCKS5"] \
			-variable ltmp(proxy) -value socks5 \
			-command [list [namespace current]::profile_add_update $l]
		    grid $l.proxy.socks5 -row 0 -column [incr col] -sticky w
		}
		https {
		   radiobutton $l.proxy.https -text [::msgcat::mc "HTTPS"] \
			-variable ltmp(proxy) -value https \
			-command [list [namespace current]::profile_add_update $l]
		    grid $l.proxy.https -row 0 -column [incr col] -sticky w
		}
	    }
	}

	label $l.lproxyhost -text [::msgcat::mc "Proxy server:"]
	entry $l.proxyhost -textvariable ltmp(proxyhost)
	label $l.lproxyport -text [::msgcat::mc "Proxy port:"]
	Spinbox $l.proxyport 0 65535 1 ltmp(proxyport)

	grid $l.lproxyhost     -row 1 -column 0 -sticky e -in $proxy_page
	grid $l.proxyhost      -row 1 -column 1 -sticky ew -in $proxy_page
	grid $l.lproxyport -row 1 -column 2 -sticky e -in $proxy_page
	grid $l.proxyport  -row 1 -column 3 -sticky ew -in $proxy_page

	label $l.lproxyusername -text [::msgcat::mc "Proxy username:"]
	ecursor_entry [entry $l.proxyusername -textvariable ltmp(proxyusername)]
	label $l.lproxypassword -text [::msgcat::mc "Proxy password:"]
	ecursor_entry [entry $l.proxypassword -show * -textvariable ltmp(proxypassword)]

	grid $l.lproxyusername    -row 2 -column 0 -sticky e -in $proxy_page
	grid $l.proxyusername     -row 2 -column 1 -sticky ew -in $proxy_page
	grid $l.lproxypassword -row 2 -column 2 -sticky e -in $proxy_page
	grid $l.proxypassword  -row 2 -column 3 -sticky ew -in $proxy_page

	grid columnconfigure $proxy_page 1 -weight 3
	grid columnconfigure $proxy_page 2 -weight 1
	grid columnconfigure $proxy_page 3 -weight 3
    }


    $nb compute_size
    $nb raise account_page
    bind .lugin <Control-Prior> [list [namespace current]::tab_move $nb -1]
    bind .lugin <Control-Next> [list [namespace current]::tab_move $nb 1]
    grid $nb -row 1 -column 0

    .lugin add -text [::msgcat::mc "Save"] -command {
	ifacetk::profile_add_save
	#save ?
	destroy .lugin
	array set ltmp {}

	ifacetk::login_dialog
    }
    .lugin add -text [::msgcat::mc "Cancel"] -command {
		destroy .lugin
	array set ltmp {}
		ifacetk::login_dialog
	}

    profile_add_update $l
    .lugin draw
}

proc profile_delete {index} {
	set file [file join $::configdir profiles.tcl]
	set ultmp {}
	if {[file exists $file]} {
		catch {
			set fd [open $file "r"]
			fconfigure $fd -encoding utf-8
			set ultmp [read $fd]
			close $fd
		}
	}

	set nltmp {}
	set n 0
	foreach item $ultmp {
		if {$n!=$index} {
			lappend nltmp $item
		}
		set n [expr $n+1]
	}
	set ultmp $nltmp
	catch {
		set fd [open $file "w"]
		fconfigure $fd -encoding utf-8
		puts $fd $ultmp
		close $fd
	}
	destroy .login
	login_dialog
}

proc profile_edit {index} {
	global ltmp
	global use_tls have_compress have_sasl have_http_poll have_proxy
	global llindex

	destroy .login

    if {[winfo exists .lugin]} {
	focus -force .lugin
	return
    }
	set llindex $index


    set tmp {}
    if {[file exists [set file [file join $::configdir profiles.tcl]]]} {
	catch {
		set fd [open $file "r"]
		fconfigure $fd -encoding utf-8
		set tmp [read $fd]
		close $fd
	}
    }


	set n 0
	set s 0
	foreach item $tmp {
		if {$n==$index} {
			set arr $item
			array set ar2 {}
			foreach a $arr {
				foreach {b c} $a {
					set ltmp($b) $c
				}
			}
			set s 1
			break
		}
		set n [expr $n+1]
	}

    Dialog .lugin -title [::msgcat::mc "Edit profile"] \
	-separator 1 -anchor e -default 0 -cancel 1

    wm resizable .lugin 0 0

    set l [.lugin getframe]

    set nb [NoteBook $l.nb]

    set account_page [$nb insert end account_page -text [::msgcat::mc "Account"]]

    label $l.laccname -text [::msgcat::mc "Account name:"]
    entry $l.accname -textvariable ltmp(accname)

    checkbutton $l.lautologin \
	-text [::msgcat::mc "Auto-login"] \
	-variable ltmp(autologin) \
	-command [list [namespace current]::profile_add_update $l]

    label $l.lusername -text [::msgcat::mc "Username:"]
    entry $l.username -textvariable ltmp(user)
    label $l.lserver -text [::msgcat::mc "Server:"]
    entry $l.server -textvariable ltmp(server)
    label $l.lpassword -text [::msgcat::mc "Password:"]
    entry $l.password -show * -textvariable ltmp(password)
    label $l.lresource -text [::msgcat::mc "Resource:"]
    entry $l.resource -textvariable ltmp(resource)

    grid $l.laccname -row 0 -column 0 -sticky e -in $account_page
    grid $l.accname  -row 0 -column 1 -sticky ew -in $account_page
    grid $l.lautologin -row 0 -column 3 -sticky ew -in $account_page

    grid $l.lusername -row 1 -column 0 -sticky e -in $account_page
    grid $l.username  -row 1 -column 1 -sticky ew -in $account_page
    grid $l.lserver   -row 1 -column 2 -sticky e -in $account_page
    grid $l.server    -row 1 -column 3 -sticky ew -in $account_page
    grid $l.lpassword -row 2 -column 0 -sticky e -in $account_page
    grid $l.password  -row 2 -column 1 -sticky ew -in $account_page
    grid $l.lresource -row 2 -column 2 -sticky e -in $account_page
    grid $l.resource  -row 2 -column 3 -sticky ew -in $account_page

    grid columnconfigure $account_page 1 -weight 3
    grid columnconfigure $account_page 2 -weight 1
    grid columnconfigure $account_page 3 -weight 3

    set connection_page [$nb insert end connection_page -text [::msgcat::mc "Connection"]]

    checkbutton $l.usealtserver -text [::msgcat::mc "Explicitly specify host and port to connect"] \
	-variable ltmp(usealtserver) \
	-command [list [namespace current]::profile_add_update $l]
    label $l.laltserver -text [::msgcat::mc "Host:"]
    entry $l.altserver -textvariable ltmp(altserver)
    label $l.lport -text [::msgcat::mc "Port:"]
    Spinbox $l.port 0 65535 1 ltmp(altport)

    grid $l.usealtserver -row 0 -column 0 -sticky w -columnspan 4 -in $connection_page
    grid $l.laltserver -row 1 -column 0 -sticky e -in $connection_page
    grid $l.altserver -row 1 -column 1 -sticky ew -in $connection_page
    grid $l.lport     -row 1 -column 2 -sticky e -in $connection_page
    grid $l.port      -row 1 -column 3 -sticky we -in $connection_page

    grid columnconfigure $connection_page 1 -weight 6
    grid columnconfigure $connection_page 2 -weight 1


    set auth_page [$nb insert end auth_page -text [::msgcat::mc "Authentication"]]

    checkbutton $l.allowauthplain \
	-text [::msgcat::mc "Allow plaintext authentication mechanisms"] \
	-variable ltmp(allowauthplain) \
	-command [list [namespace current]::profile_add_update $l]

    grid $l.allowauthplain -row 0 -column 0 -sticky w -in $auth_page

    if {$have_sasl} {
	checkbutton $l.usesasl \
	    -text [::msgcat::mc "Use SASL authentication"] \
	    -variable ltmp(usesasl) \
	    -command [list [namespace current]::profile_add_update $l]

	grid $l.usesasl -row 1 -column 0 -sticky w -in $auth_page

	checkbutton $l.allowgoogletoken \
	    -text [::msgcat::mc "Allow X-GOOGLE-TOKEN SASL mechanism"] \
	    -variable ltmp(allowgoogletoken) \
	    -command [list [namespace current]::profile_add_update $l]

	grid $l.allowgoogletoken -row 2 -column 0 -sticky w -in $auth_page
    }

    grid columnconfigure $auth_page 0 -weight 1

    if {$use_tls || $have_compress} {
	if {$use_tls && $have_compress} {
		set page_label [::msgcat::mc "SSL & Compression"]
	} elseif {$have_compress} {
	    set page_label [::msgcat::mc "Compression"]
	} else {
	    set page_label [::msgcat::mc "SSL"]
	}

	set ssl_page [$nb insert end ssl_page -text $page_label]

	radiobutton $l.dontusessl -text [::msgcat::mc "Plaintext"] \
	    -variable ltmp(stream_options) -value plaintext \
	    -command [list [namespace current]::profile_add_update $l]
        $l.dontusessl configure -state normal
	if {$have_compress} {
	    radiobutton $l.usecompress -text [::msgcat::mc "Compression"] \
		-variable ltmp(stream_options) -value compressed \
		-command [list [namespace current]::profile_add_update $l]
	}
	if {$use_tls} {
	    radiobutton $l.encrypted -text [::msgcat::mc "Encryption (STARTTLS)"] \
		-variable ltmp(stream_options) -value encrypted \
		-command [list [namespace current]::profile_add_update $l]
	    radiobutton $l.legacyssl -text [::msgcat::mc "Encryption (legacy SSL)"] \
		-variable ltmp(stream_options) -value ssl \
		-command [list [namespace current]::profile_add_update $l]
	    if {$have_compress} {
  		 radiobutton $l.tlsandcompress -text [::msgcat::mc "Encryption (STARTTLS) + Compression"] \
		-variable ltmp(stream_options) -value tlsandcompress \
		-command [list [namespace current]::profile_add_update $l]

	    }
	    label $l.lsslcertfile -text [::msgcat::mc "SSL certificate:"]
	    entry $l.sslcertfile -textvariable ltmp(sslcertfile)
	    button $l.bsslcertfile -text [::msgcat::mc "Browse..."] \
		-command [list eval set ltmp(sslcertfile) {[tk_getOpenFile]}]
	}

	set column 0
	grid $l.dontusessl -row 0 -column $column -sticky w -in $ssl_page
	
	if {$have_compress} {
	    grid $l.usecompress -row 0 -column [incr column] -sticky w -in $ssl_page
	}
	if {$use_tls} {
	    grid $l.encrypted     -row 0 -column [incr column] -sticky w -in $ssl_page
	    grid $l.legacyssl    -row 0 -column [incr column] -sticky w -in $ssl_page
	    if {$have_compress} {
		    grid $l.tlsandcompress    -row 0 -column [incr column] -sticky w -in $ssl_page
	    }

	    grid $l.lsslcertfile -row 1 -column 0 -sticky e -in $ssl_page
	    grid $l.sslcertfile  -row 1 -column 1 -sticky ew -columnspan 2 -in $ssl_page
	    grid $l.bsslcertfile -row 1 -column 3 -sticky w -in $ssl_page
	}

	grid columnconfigure $ssl_page 1 -weight 1
	grid columnconfigure $ssl_page 2 -weight 1
    }

    if {$have_http_poll} {
	set httppoll_page [$nb insert end httpoll_page -text [::msgcat::mc "HTTP Poll"]]

	checkbutton $l.usehttppoll -text [::msgcat::mc "Connect via HTTP polling"] \
	    -variable ltmp(usehttppoll) \
	    -command [list [namespace current]::profile_add_update $l]
	label $l.lpollurl -text [::msgcat::mc "URL to poll:"]
	entry $l.pollurl -textvariable ltmp(pollurl)
	checkbutton $l.usepollkeys -text [::msgcat::mc "Use client security keys"] \
	    -state disabled \
	    -variable ltmp(usepollkeys) \
	    -command [list [namespace current]::profile_add_update $l]
    
	grid $l.usehttppoll -row 0 -column 0 -sticky w -columnspan 3 -in $httppoll_page
	grid $l.lpollurl -row 1 -column 0 -sticky e -in $httppoll_page
	grid $l.pollurl -row 1 -column 1 -sticky ew -in $httppoll_page
	grid $l.usepollkeys -row 2 -column 0 -sticky w -columnspan 3 -in $httppoll_page

	grid columnconfigure $httppoll_page 1 -weight 1
    }
    
    if {$have_proxy} {
	set proxy_page [$nb insert end proxy_page -text [::msgcat::mc "Proxy"]]

	label $l.lproxy -text [::msgcat::mc "Proxy type:"]
	grid $l.lproxy -row 0 -column 0 -sticky e -in $proxy_page
	frame $l.proxy
	grid $l.proxy -row 0 -column 1 -columnspan 3 -sticky w -in $proxy_page

	set col 0
	foreach type [jlib::capabilities proxy] {
	    switch -- $type {
		none {
		   radiobutton $l.proxy.none -text [::msgcat::mc "None"] \
			-variable ltmp(proxy) -value none \
			-command [list [namespace current]::profile_add_update $l]
		    grid $l.proxy.none -row 0 -column [incr col] -sticky w
		}
		socks4 {
		   radiobutton $l.proxy.socks4 -text [::msgcat::mc "SOCKS4a"] \
			-variable ltmp(proxy) -value socks4 \
			-command [list [namespace current]::profile_add_update $l]
		    grid $l.proxy.socks4 -row 0 -column [incr col] -sticky w
		}
		socks5 {
		   radiobutton $l.proxy.socks5 -text [::msgcat::mc "SOCKS5"] \
			-variable ltmp(proxy) -value socks5 \
			-command [list [namespace current]::profile_add_update $l]
		    grid $l.proxy.socks5 -row 0 -column [incr col] -sticky w
		}
		https {
		   radiobutton $l.proxy.https -text [::msgcat::mc "HTTPS"] \
			-variable ltmp(proxy) -value https \
			-command [list [namespace current]::profile_add_update $l]
		    grid $l.proxy.https -row 0 -column [incr col] -sticky w
		}
	    }
	}

	label $l.lproxyhost -text [::msgcat::mc "Proxy server:"]
	entry $l.proxyhost -textvariable ltmp(proxyhost)
	label $l.lproxyport -text [::msgcat::mc "Proxy port:"]
	Spinbox $l.proxyport 0 65535 1 ltmp(proxyport)

	grid $l.lproxyhost     -row 1 -column 0 -sticky e -in $proxy_page
	grid $l.proxyhost      -row 1 -column 1 -sticky ew -in $proxy_page
	grid $l.lproxyport -row 1 -column 2 -sticky e -in $proxy_page
	grid $l.proxyport  -row 1 -column 3 -sticky ew -in $proxy_page

	label $l.lproxyusername -text [::msgcat::mc "Proxy username:"]
	ecursor_entry [entry $l.proxyusername -textvariable ltmp(proxyusername)]
	label $l.lproxypassword -text [::msgcat::mc "Proxy password:"]
	ecursor_entry [entry $l.proxypassword -show * -textvariable ltmp(proxypassword)]

	grid $l.lproxyusername    -row 2 -column 0 -sticky e -in $proxy_page
	grid $l.proxyusername     -row 2 -column 1 -sticky ew -in $proxy_page
	grid $l.lproxypassword -row 2 -column 2 -sticky e -in $proxy_page
	grid $l.proxypassword  -row 2 -column 3 -sticky ew -in $proxy_page

	grid columnconfigure $proxy_page 1 -weight 3
	grid columnconfigure $proxy_page 2 -weight 1
	grid columnconfigure $proxy_page 3 -weight 3
    }


    $nb compute_size
    $nb raise account_page
    bind .lugin <Control-Prior> [list [namespace current]::tab_move $nb -1]
    bind .lugin <Control-Next> [list [namespace current]::tab_move $nb 1]
    grid $nb -row 1 -column 0

    .lugin add -text [::msgcat::mc "Save"] -command {
	ifacetk::profile_edit_save
	#save ?
	destroy .lugin
	array set ltmp {}

	ifacetk::login_dialog
    }
    .lugin add -text [::msgcat::mc "Cancel"] -command {
		destroy .lugin
	array set ltmp {}
		ifacetk::login_dialog
	}

    profile_add_update $l
    .lugin draw
}

proc pre_login {} {
	if {![info exists ::noautologin]} {
		set tmp {}
		if {[file exists [set file [file join $::configdir profiles.tcl]]]} {
			catch {
				set fd [open $file "r"]
				fconfigure $fd -encoding utf-8
				set tmp [read $fd]
				close $fd
			}
		}
		set al 0
		set n 0
		foreach item $tmp {
			array set ltump {}
			set arr $item
			array set ar2 {}
			foreach a $arr {
				foreach {b c} $a {
					set ltump($b) $c
				}
			}
			if {$ltump(autologin)} {
				set al 1
				update
				login [array get ltump]
			}
			set n [expr $n+1]
		}
		if {$al} { return }
	}
	ifacetk::login_dialog
}

proc login_dialog {} {
    global use_tls have_compress have_sasl have_http_poll have_proxy

    if {[winfo exists .login]} {
	focus -force .login
	return
    }
    Dialog .login -title [::msgcat::mc "Login"] \
	-separator 1 -anchor e -default 0 -cancel 1

#    wm resizable .login 0 0

    set l [.login getframe]
    set tools [frame $l.tools]
    pack $tools -side bottom -fill x

    set sw [ScrolledWindow .login.sw]
    set lf [listbox .login.fields]
    pack $sw -side top -expand yes -fill both -in $l -pady 1m -padx 1m
    $sw setwidget $lf
    bind $lf <3> [list [namespace current]::select_and_popup_menu %W %x %y]

    set tmp {}
    if {[file exists [set file [file join $::configdir profiles.tcl]]]} {
	catch {
		set fd [open $file "r"]
		fconfigure $fd -encoding utf-8
		set tmp [read $fd]
		close $fd
	}
    }
    	array set ltump  {}
	set n 0
	foreach item $tmp {
		set arr $item
		array set ar2 {}
		foreach a $arr {
			foreach {b c} $a {
				set ltump($n,$b) $c
			}
		}
		set accname $ltump($n,accname)
		set jid "$ltump($n,user)@$ltump($n,server)/$ltump($n,resource)"
 		$lf insert end "$accname - $jid"
		set n [expr $n+1]
	}


    set padd [button $tools.padd \
	     -text [::msgcat::mc "Add"] \
	     -command \
	     [list [namespace current]::profile_add $lf]]

    pack $padd -side right -padx 1m

    set plogin [button $tools.lugin -text [::msgcat::mc "Log in"] \
	-command [list [namespace current]::profile_login $lf]]

    pack $padd -side right -padx 1m
    pack $plogin -side right -padx 1m

    .login draw
}

proc profile_login {lf} {
	global loginconf
	set index [$lf curselection]
    set tmp {}
    if {[file exists [set file [file join $::configdir profiles.tcl]]]} {
	catch {
		set fd [open $file "r"]
		fconfigure $fd -encoding utf-8
		set tmp [read $fd]
		close $fd
	}
    }
	set n 0
	set s 0
	foreach item $tmp {
		if {$n==$index} {
			set arr $item
			array set ar2 {}
			foreach a $arr {
				foreach {b c} $a {
					set loginconf($b) $c
				}
			}
			set s 1
			break
		}
		set n [expr $n+1]
	}
	if {!$s} { return }
	destroy .login
	update
	login [array get loginconf]
}

proc select_and_popup_menu {f x y} {
    set index [$f index @$x,$y]
    $f selection clear 0 end
    $f selection set $index

    if {[winfo exists [set m .login.lfpopup]]} {
	destroy $m
    }

    menu $m -tearoff 0
    $m add command -label [::msgcat::mc "Edit"] \
	-command [list [namespace current]::profile_edit $index]
    $m add command -label [::msgcat::mc "Remove"] \
	-command [list [namespace current]::profile_delete $index]
    tk_popup $m [winfo pointerx .] [winfo pointery .]
}

hook::add finload_hook ifacetk::pre_login 9999

proc logout_dialog {} {
    global logout_conn
    
    set w .logout
    if {[winfo exists $w]} {
	destroy $w
    }

    switch -- [llength [jlib::connections]] {
	0 {
	    return
	}
	1 {
	    logout
	    return
	}
    }

    set lnames {}
    foreach connid [jlib::connections] {
	lappend lnames $connid [jlib::connection_jid $connid]
    }

    if {[CbDialog $w [::msgcat::mc "Logout"] \
	    [list [::msgcat::mc "Log out"] [list $w enddialog 0] \
		  [::msgcat::mc "Cancel"] [list $w enddialog 1]] \
	    logout_conn $lnames {} -modal local] != 0} {
	return
    }

    foreach connid [array names logout_conn] {
	if {[lcontain [jlib::connections] $connid] && $logout_conn($connid)} {
	    logout $connid
	}
    }
}

# TODO
proc change_password_dialog {} {
    global oldpassword newpassword password

    set oldpassword ""
    set newpassword ""
    set password ""

    if {[winfo exists .passwordchange]} {
	destroy .passwordchange
    }
    
    Dialog .passwordchange -title [::msgcat::mc "Change password"] \
	-separator 1 -anchor e -default 0 -cancel 1

    .passwordchange add -text [::msgcat::mc "OK"] -command {
	destroy .passwordchange
	send_change_password
    }
    .passwordchange add -text [::msgcat::mc "Cancel"] -command [list destroy .passwordchange]


    set p [.passwordchange getframe]
    
    label $p.loldpass -text [::msgcat::mc "Old password:"]
    ecursor_entry [entry $p.oldpass -show * -textvariable oldpassword]
    label $p.lnewpass -text [::msgcat::mc "New password:"]
    ecursor_entry [entry $p.newpass -show * -textvariable newpassword]
    label $p.lpassword -text [::msgcat::mc "Repeat new password:"]
    ecursor_entry [entry $p.password -show * -textvariable password]

    grid $p.loldpass  -row 0 -column 0 -sticky e
    grid $p.oldpass   -row 0 -column 1 -sticky ew
    grid $p.lnewpass  -row 1 -column 0 -sticky e
    grid $p.newpass   -row 1 -column 1 -sticky ew
    grid $p.lpassword -row 2 -column 0 -sticky e
    grid $p.password  -row 2 -column 1 -sticky ew

    focus $p.oldpass
    .passwordchange draw

}



#
#
# REGUSTER
#
#



proc reg_profile_clean {} {
	global use_tls have_compress have_proxy
 	set loginconf(user) ""
	set loginconf(server) "localhost"
	set so "plaintext"
	set values [list plaintext [::msgcat::mc "Plaintext"]]
	if {$have_compress} {
	    lappend values compressed [::msgcat::mc "Compression"]
	    set so "compressed"
	}
	if {$use_tls} {
	    lappend values encrypted [::msgcat::mc "Encryption (STARTTLS)"] \
			   ssl       [::msgcat::mc "Encryption (legacy SSL)"]
	    if {$have_compress} {
		    lappend values tlsandcompress [::msgcat::mc "Encryption (STARTTLS) + Compression"] \
	    }
	    set so "encrypted"
	}
	set loginconf(stream_options) $so
	if {$use_tls} {
		set loginconf(sslcertfile) ""
		set loginconf(sslcacertstore) ""
		set loginconf(sslkeyfile) ""
	}
	if {$have_proxy} {
	    set values {}
	    foreach type [jlib::capabilities proxy] {
		switch -- $type {
		    none   {lappend values none   [::msgcat::mc "None"]}
		    socks4 {lappend values socks4 [::msgcat::mc "SOCKS4a"]}
		    socks5 {lappend values socks5 [::msgcat::mc "SOCKS5"]}
		    https  {lappend values https  [::msgcat::mc "HTTPS"]}
		}
	    }
		set loginconf(proxy) none
		set loginconf(proxyhost) "localhost"
		set loginconf(proxyport) 3128
		set loginconf(proxyusername) ""
		set loginconf(proxypassword) ""
		set loginconf(proxyuseragent) "Mozilla/4.0 (compatible; MSIE 6.0;\
	 $::tcl_platform(os) $::tcl_platform(osVersion))"
	}
	set loginconf(usealtserver) 0
	set loginconf(altserver) ""
	set loginconf(altport) "5222"
	return [array get loginconf]
}


proc reg_profile_update {l} {
    foreach ent {username server port password \
            altserver proxyhost proxyport proxyusername proxypassword \
            sslcertfile} {
        if {[winfo exists $l.$ent]} {
            catch { $l.$ent icursor end }
        }
    }

    foreach {check enable disable} { \
			{usecompress legacyssl encrypted tlsandcompress \
			 sslcertfile lsslcertfile bsslcertfile} \
            usealtserver {altserver laltserver port lport} {} \
	    usesasl {allowgoogletoken} {} \
	    } {
        if {![info exists rtmp($check)] || ![winfo exists $l.$check]} {
            continue
        }

        if {$rtmp($check) && ![cequal [$l.$check cget -state] disabled]} {
            set state1 normal
            set state2 disabled
        } else {
            set state1 disabled
            set state2 normal
        }
        foreach ent $enable {
            if {[winfo exists $l.$ent]} {
                $l.$ent configure -state $state1
                if {[cequal [focus] $l.$ent] && [cequal $state1 "disabled"]} {
                    focus [Widget::focusPrev $l.$ent]
                }
            }
        }
        foreach ent $disable {
            if {[winfo exists $l.$ent]} {
                $l.$ent configure -state $state2
                if {[cequal [focus] $l.$ent] && [cequal $state2 "disabled"]} {
                    focus [Widget::focusPrev $l.$ent]
                }
	    }
	}
    }
    catch {
	if {[cequal $rtmp(proxy) none]} {
	    foreach ent {proxyhost proxyport proxyusername proxypassword \
			 lproxyhost lproxyport lproxyusername lproxypassword} {
		$l.$ent configure -state disabled
                if {[cequal [focus] $l.$ent]} {
                    focus [Widget::focusPrev $l.$ent]
                }
	    }
	} else {
	    foreach ent {proxyhost proxyport proxyusername proxypassword \
			 lproxyhost lproxyport lproxyusername lproxypassword} {
		$l.$ent configure -state normal
	    }
	}
    }
    catch {
	if {![cequal [$l.dontusessl cget -state] disabled] && \
		($rtmp(stream_options) == "ssl" || \
		 $rtmp(stream_options) == "encrypted" || \
		 $rtmp(stream_options) == "tlsandcompress")} {
	    $l.sslcertfile configure -state normal
	    $l.lsslcertfile configure -state normal
	    $l.bsslcertfile configure -state normal
	} else {
	    $l.sslcertfile configure -state disabled
	    $l.lsslcertfile configure -state disabled
	    $l.bsslcertfile configure -state disabled
	    if {[cequal [focus] $l.sslcertfile] || \
		    [cequal [focus] $l.bsslcertfile]} {
		focus [Widget::focusPrev $l.sslcertfile]
	    }
	}
    }
}



proc reg_profile_do {} {
	global rtmp
	global use_tls have_compress have_proxy

    if {[winfo exists .regur]} {
	focus -force .regur
	return
    }

    array set rtmp [reg_profile_clean]

    Dialog .regur -title [::msgcat::mc "Register an account"] \
	-separator 1 -anchor e -default 0 -cancel 1

    wm resizable .regur 0 0

    set l [.regur getframe]

    set nb [NoteBook $l.nb]

    set account_page [$nb insert end account_page -text [::msgcat::mc "Account"]]


    label $l.lusername -text [::msgcat::mc "Username:"]
    entry $l.username -textvariable rtmp(user)
    label $l.lserver -text [::msgcat::mc "Server:"]
    entry $l.server -textvariable rtmp(server)
    label $l.lpassword -text [::msgcat::mc "Password:"]
    entry $l.password -show * -textvariable rtmp(password)

    grid $l.lusername -row 1 -column 0 -sticky e -in $account_page
    grid $l.username  -row 1 -column 1 -sticky ew -in $account_page
    grid $l.lserver   -row 1 -column 2 -sticky e -in $account_page
    grid $l.server    -row 1 -column 3 -sticky ew -in $account_page
    grid $l.lpassword -row 2 -column 0 -sticky e -in $account_page
    grid $l.password  -row 2 -column 1 -sticky ew -in $account_page

    grid columnconfigure $account_page 1 -weight 3
    grid columnconfigure $account_page 2 -weight 1
    grid columnconfigure $account_page 3 -weight 3

    set connection_page [$nb insert end connection_page -text [::msgcat::mc "Connection"]]

    checkbutton $l.usealtserver -text [::msgcat::mc "Explicitly specify host and port to connect"] \
	-variable rtmp(usealtserver) \
	-command [list [namespace current]::reg_profile_update $l]
    label $l.laltserver -text [::msgcat::mc "Host:"]
    entry $l.altserver -textvariable rtmp(altserver)
    label $l.lport -text [::msgcat::mc "Port:"]
    Spinbox $l.port 0 65535 1 rtmp(altport)

    grid $l.usealtserver -row 0 -column 0 -sticky w -columnspan 4 -in $connection_page
    grid $l.laltserver -row 1 -column 0 -sticky e -in $connection_page
    grid $l.altserver -row 1 -column 1 -sticky ew -in $connection_page
    grid $l.lport     -row 1 -column 2 -sticky e -in $connection_page
    grid $l.port      -row 1 -column 3 -sticky we -in $connection_page

    grid columnconfigure $connection_page 1 -weight 6
    grid columnconfigure $connection_page 2 -weight 1


    if {$use_tls || $have_compress} {
	if {$use_tls && $have_compress} {
		set page_label [::msgcat::mc "SSL & Compression"]
	} elseif {$have_compress} {
	    set page_label [::msgcat::mc "Compression"]
	} else {
	    set page_label [::msgcat::mc "SSL"]
	}

	set ssl_page [$nb insert end ssl_page -text $page_label]

	radiobutton $l.dontusessl -text [::msgcat::mc "Plaintext"] \
	    -variable rtmp(stream_options) -value plaintext \
	    -command [list [namespace current]::reg_profile_update $l]
        $l.dontusessl configure -state normal
	if {$have_compress} {
	    radiobutton $l.usecompress -text [::msgcat::mc "Compression"] \
		-variable rtmp(stream_options) -value compressed \
		-command [list [namespace current]::reg_profile_update $l]
	}
	if {$use_tls} {
	    radiobutton $l.encrypted -text [::msgcat::mc "Encryption (STARTTLS)"] \
		-variable rtmp(stream_options) -value encrypted \
		-command [list [namespace current]::reg_profile_update $l]
	    radiobutton $l.legacyssl -text [::msgcat::mc "Encryption (legacy SSL)"] \
		-variable rtmp(stream_options) -value ssl \
		-command [list [namespace current]::reg_profile_update $l]
	    if {$have_compress} {
	 	   radiobutton $l.tlsandcompress -text [::msgcat::mc "Encryption (STARTTLS) + Compression"] \
			-variable rtmp(stream_options) -value tlsandcompress \
			-command [list [namespace current]::reg_profile_update $l]

	    }
	    label $l.lsslcertfile -text [::msgcat::mc "SSL certificate:"]
	    entry $l.sslcertfile -textvariable rtmp(sslcertfile)
	    button $l.bsslcertfile -text [::msgcat::mc "Browse..."] \
		-command [list eval set rtmp(sslcertfile) {[tk_getOpenFile]}]
	}

	set column 0
	grid $l.dontusessl -row 0 -column $column -sticky w -in $ssl_page
	
	if {$have_compress} {
	    grid $l.usecompress -row 0 -column [incr column] -sticky w -in $ssl_page
	}
	if {$use_tls} {
	    grid $l.encrypted     -row 0 -column [incr column] -sticky w -in $ssl_page
	    grid $l.legacyssl    -row 0 -column [incr column] -sticky w -in $ssl_page
	    if {$have_compress} {
		    grid $l.tlsandcompress    -row 0 -column [incr column] -sticky w -in $ssl_page
	    }

	    grid $l.lsslcertfile -row 1 -column 0 -sticky e -in $ssl_page
	    grid $l.sslcertfile  -row 1 -column 1 -sticky ew -columnspan 2 -in $ssl_page
	    grid $l.bsslcertfile -row 1 -column 3 -sticky w -in $ssl_page
	}

	grid columnconfigure $ssl_page 1 -weight 1
	grid columnconfigure $ssl_page 2 -weight 1
    }

    if {$have_proxy} {
	set proxy_page [$nb insert end proxy_page -text [::msgcat::mc "Proxy"]]

	label $l.lproxy -text [::msgcat::mc "Proxy type:"]
	grid $l.lproxy -row 0 -column 0 -sticky e -in $proxy_page
	frame $l.proxy
	grid $l.proxy -row 0 -column 1 -columnspan 3 -sticky w -in $proxy_page

	set col 0
	foreach type [jlib::capabilities proxy] {
	    switch -- $type {
		none {
		   radiobutton $l.proxy.none -text [::msgcat::mc "None"] \
			-variable rtmp(proxy) -value none \
			-command [list [namespace current]::reg_profile_update $l]
		    grid $l.proxy.none -row 0 -column [incr col] -sticky w
		}
		socks4 {
		   radiobutton $l.proxy.socks4 -text [::msgcat::mc "SOCKS4a"] \
			-variable rtmp(proxy) -value socks4 \
			-command [list [namespace current]::reg_profile_update $l]
		    grid $l.proxy.socks4 -row 0 -column [incr col] -sticky w
		}
		socks5 {
		   radiobutton $l.proxy.socks5 -text [::msgcat::mc "SOCKS5"] \
			-variable rtmp(proxy) -value socks5 \
			-command [list [namespace current]::reg_profile_update $l]
		    grid $l.proxy.socks5 -row 0 -column [incr col] -sticky w
		}
		https {
		   radiobutton $l.proxy.https -text [::msgcat::mc "HTTPS"] \
			-variable rtmp(proxy) -value https \
			-command [list [namespace current]::reg_profile_update $l]
		    grid $l.proxy.https -row 0 -column [incr col] -sticky w
		}
	    }
	}

	label $l.lproxyhost -text [::msgcat::mc "Proxy server:"]
	entry $l.proxyhost -textvariable rtmp(proxyhost)
	label $l.lproxyport -text [::msgcat::mc "Proxy port:"]
	Spinbox $l.proxyport 0 65535 1 rtmp(proxyport)

	grid $l.lproxyhost     -row 1 -column 0 -sticky e -in $proxy_page
	grid $l.proxyhost      -row 1 -column 1 -sticky ew -in $proxy_page
	grid $l.lproxyport -row 1 -column 2 -sticky e -in $proxy_page
	grid $l.proxyport  -row 1 -column 3 -sticky ew -in $proxy_page

	label $l.lproxyusername -text [::msgcat::mc "Proxy username:"]
	ecursor_entry [entry $l.proxyusername -textvariable rtmp(proxyusername)]
	label $l.lproxypassword -text [::msgcat::mc "Proxy password:"]
	ecursor_entry [entry $l.proxypassword -show * -textvariable rtmp(proxypassword)]

	grid $l.lproxyusername    -row 2 -column 0 -sticky e -in $proxy_page
	grid $l.proxyusername     -row 2 -column 1 -sticky ew -in $proxy_page
	grid $l.lproxypassword -row 2 -column 2 -sticky e -in $proxy_page
	grid $l.proxypassword  -row 2 -column 3 -sticky ew -in $proxy_page

	grid columnconfigure $proxy_page 1 -weight 3
	grid columnconfigure $proxy_page 2 -weight 1
	grid columnconfigure $proxy_page 3 -weight 3
    }


    $nb compute_size
    $nb raise account_page
    bind .regur <Control-Prior> [list [namespace current]::tab_move $nb -1]
    bind .regur <Control-Next> [list [namespace current]::tab_move $nb 1]
    grid $nb -row 1 -column 0

    .regur add -text [::msgcat::mc "Save"] -command {
	destroy .regur
	update
	::aregister [array get rtmp]
    }
    .regur add -text [::msgcat::mc "Cancel"] -command {
	destroy .regur
	}

    .regur draw
}


