#browser.tcl
# Copyright (c) 2001-2004 Digi International Inc., All Rights Reserved
# 
# This software contains proprietary and confidential information of Digi
# International Inc.  By accepting transfer of this copy, Recipient agrees
# to retain this software in confidence, to prevent disclosure to others,
# and to make no use of this software other than that for which it was
# delivered.  This is an unpublished copyrighted work of Digi International
# Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
# prohibited.
# 
# Restricted Rights Legend
#
# Use, duplication, or disclosure by the Government is subject to
# restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
# Technical Data and Computer Software clause at DFARS 252.227-7031 or
# subparagraphs (c)(1) and (2) of the Commercial Computer Software -
# Restricted Rights at 48 CFR 52.227-19, as applicable.
#
# Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
#
# ***************************************************************************

namespace eval ::dg::browser {}

proc ::dg::browser::_encode {str} {
    
    regsub -all {\+} $str {%2b} str
    regsub -all {&} $str {%26} str
    regsub -all {=} $str {%3d} str
    regsub -all {\"} $str {%22} str
    regsub -all {#} $str {%23} str
    regsub -all {;} $str {%3b} str
    regsub -all {\?} $str {%3f} str
#   regsub -all {:} $str {%3a} str
    regsub -all {\$} $str {%24} str
    regsub -all {!} $str {%21} str
    regsub -all {,} $str {%2c} str
    regsub -all {'} $str {%27} str
    regsub -all {\(} $str {%28} str
    regsub -all {\)} $str {%29} str
    regsub -all { } $str {%20} str

    return $str
}

proc ::dg::browser::ViewFile {path} {
    set pathBrowser $::dg::pathBrowser
    if {[string first nets [string tolower $::dg::pathBrowser]] != -1} {
        # any version of netscape
        set strFile "file:"
        if {$::tcl_platform(platform) == "windows"} {
            append strFile /
        }
        if {[string first netscp6 [string tolower $::dg::pathBrowser]] != -1} {
            # netscape version 6
            set encode 0
        } else {
            # previous version of netscape
            set encode 1
        }
    } else {
        # not netscape
        set strFile "file:"
        set encode 1
    }
    set strPath [file join $::dg::dirRoot $path]
    if {$encode} {
        set strPath [_encode $strPath]
    }
    ViewUrl "$strFile$strPath"
}

proc ::dg::browser::ViewUrl {strURL} {
    global ok
    regsub -- {^file:} $strURL {} strPath
    if { [regexp -- {\.pdf$|\.PDF$} $strURL] } {
        ::view::okcancel [mc all.view.pdf.okcancel.title]  \
                        "[mc all.view.pdf.okcancel.message] $strPath \n" \
                        [mc all.view.pdf.okcancel.command] \
                        $strPath
        clipboard clear
        clipboard append $strPath       
        if {$ok == 0} {
            return
        }
    }
    #TODO: Else this if
    if {$::dg::pathBrowser != ""} {

        if {$::dg::strIssue == "Debian"} {
            set pid [eval exec -- $::dg::pathBrowser $strURL &]

        } else {
            set pid [exec -- $::dg::pathBrowser $strURL &]
        }
    }
}

proc ::dg::browser::_Ask {} {

    if {[string equal $::tcl_platform(platform) windows]} {
        set strFileTypes {{"Executable Files" .exe} {"All Files" *}}
    } else {
        set strFileTypes {}
    }
    tk_messageBox -title [mc browser.ask.title] -message [mc browser.ask.message]
    return [tk_getOpenFile -filetypes $strFileTypes -parent . -title [mc browser.open.title]]
}

proc ::dg::browser::Path {} {
    set filePath [_Find]
    if {$filePath == ""} {
            set filePath [_Ask]
    }
    return $filePath
}

proc ::dg::browser::_Find {} {
    set pathResult ""
    switch -exact -- $::tcl_platform(platform) {
        unix {      
            set lstPath [split $::env(PATH) ":"]
            foreach dir $lstPath {
                set path [file join $dir netscape]
                if {[file executable $path]} {set pathResult $path; break}
                set path [file join $dir Mosaic]
                if {[file executable $path]} {set pathResult $path; break}
                set path [file join $dir hotjava]
                if {[file executable $path]} {set pathResult $path; break}
            }   
        }   
        windows {
            package require registry
            set keyRoot HKEY_CLASSES_ROOT
            if {![catch "set keyClass [registry get $keyRoot\\.html {}]"]} {            
                if {$keyClass != ""} {
                    set strCommand [registry get "$keyRoot\\$keyClass\\shell\\open\\command" ""]                    
                    if {[string equal [string index $strCommand 0] "\""]} {
                        set end [string first "\"" [string range $strCommand 1 end]]
                        set pathResult [string range $strCommand 1 $end]
                    } else {
                        set pathResult [lindex [split $strCommand] 0]
                    }
                }
            } else {
                set fileExplorer "iexplorer.exe"
                set fileNetscape "netscape.exe"

                set lstPath [split $::env(PATH) ";"]
                foreach dir $lstPath {
                    set path [file join $dir $fileExplorer]
                    if {[file executable $path]} {set pathResult $path; break}
                    set path [file join $dir $fileNetscape]
                    if {[file executable $path]} {set pathResult $path; break}
                }
            }
        }
    } ;#end switch      
        return $pathResult
}

