
# META NAME Plugin selector
# META DESCRIPTION This window. If you disable it, you can enable it later manually.
# META AUTHOR <András Murányi> muranyia@gmail.com
# META VERSION 0.1.20130120

package require Tcl 8.5
package require Tk
package require pdwindow 0.1
package require pd_menus 0.1

package provide plugwindow 0.1.20130120

namespace eval ::plugins:: {
	variable startup_plugins {}
}

proc ::plugins::disable_startup_plugin {Filename} {
	variable Newfilename ""
	if { [file writable [file dirname $Filename] ] } {
	  set Newfilename [file join [file dirname $Filename] "disabled" [file tail $Filename]]
		if { ![file exists [file dirname $Newfilename]] } {
			file mkdir [file dirname $Newfilename]
		}
		file rename -force -- $Filename $Newfilename
		::pdwindow::verbose 1 [_ "Disabled startup plugin $Filename\n"]
		return $Newfilename
	} else {
		::pdwindow::verbose 1 [_ "Cannot disable startup plugin $Filename (not writable)\n"]
		return 0
	}
}

proc ::plugins::enable_startup_plugin {Filename} {
	variable Newfilename ""
	if { [file writable [file dirname $Filename] ] } {
	  set Newfilename [file join [file normalize [file join [file dirname $Filename] ".."]] [file tail $Filename]]
		file rename -force -- $Filename $Newfilename
		::pdwindow::verbose 1 [_ "Enabled startup plugin $Filename\n"]
		return $Newfilename
	} else {
		::pdwindow::verbose 1 [_ "Cannot enable startup plugin $Filename (not writable)\n"]
		return 0
	}
}

proc ::plugins::toggle_startup_plugin {Shortname} {
	variable startup_plugins
	variable Newfilename ""
	set Plugfilename [lindex $startup_plugins [lsearch -index 0 -exact $startup_plugins $Shortname] 5]
	if { $::plugins_loaded($Shortname) } {
		set Newfilename [::plugins::enable_startup_plugin $Plugfilename]
	} else {
		set Newfilename [::plugins::disable_startup_plugin $Plugfilename]
	}
	lset startup_plugins [lsearch -index 5 -subindices -exact $startup_plugins $Plugfilename] $Newfilename
}

proc ::plugins::extract_startup_plugin_info {Filename} {
	variable Plugfile ""
	variable Plugtext ""
	variable Plugname ""
	variable Plugdesc ""
	variable Plugauth ""
	variable Plugvers ""
	set Plugfile [open $Filename]
	set Plugtext [read $Plugfile 512]
	close $Plugfile
	if { [regexp {\n[ ]*\#[ ]*META NAME[ :=]+([^\n]+)\n} $Plugtext Unused Plugname] eq 0 } {
		set Plugname 0
	}
	if { [regexp {\n[ ]*\#[ ]*META DESCRIPTION[ :=]+([^\n]+)\n} $Plugtext Unused Plugdesc] eq 0 } {
		set Plugdesc 0
	}
	if { [regexp {\n[ ]*\#[ ]*META AUTHOR[ :=]+([^\n]+)\n} $Plugtext Unused Plugauth] eq 0 } {
		set Plugauth 0
	}
	if { [regexp {\n[ ]*\#[ ]*META VERSION[ :=]+([^\n]+)\n} $Plugtext Unused Plugvers] eq 0 } {
		set Plugvers 0
	}
	return [list $Plugname $Plugdesc $Plugauth $Plugvers]
}

proc ::plugins::create_plugins_window {} {
	variable startup_plugins
	variable Plugmeta {}
	variable Plugfilename ""
	variable Version ""
	variable Shortname ""
	if { [winfo exists .plugwindow] } {
		wm deiconify .plugwindow
	} else {
		foreach Pathdir [lsort -unique [concat $::sys_searchpath $::sys_staticpath]] {
			# read enabled plugins
			set Dir [file normalize $Pathdir]
			if { ! [file isdirectory $Dir]} {continue}
			foreach Plugfilename [glob -directory $Dir -nocomplain -types {f} -- *-plugin.tcl] {
				regexp {([^/\\]+)-plugin.tcl$} [file tail $Plugfilename] Unused Shortname
				set Plugmeta [::plugins::extract_startup_plugin_info $Plugfilename]
				set Shortname [string map { . "" \[ "" \] "" ( "" ) "" \{ "" \} "" - "" } $Shortname]
				lappend startup_plugins [list $Shortname 1 [lindex $Plugmeta 0] [lindex $Plugmeta 1] [lindex $Plugmeta 2] $Plugfilename]
				if { [lindex $Plugmeta 3] ne 0 } { set Version [concat "(version " [lindex $Plugmeta 3] ")"] } else { set Version "" }
				::pdwindow::verbose 1 [_ "Enumerated startup plugin $Shortname $Version\n"]
				puts [_ "Enumerated startup plugin $Shortname $Version"]
			}
			# read disabled plugins
			set Dir [file normalize [file join $Pathdir disabled]]
			if { ! [file isdirectory $Dir]} {continue}
			foreach Plugfilename [glob -directory $Dir -nocomplain -types {f} -- *-plugin.tcl] {
				regexp {([^/\\]+)-plugin.tcl$} [file tail $Plugfilename] Unused Shortname
				if { [lsearch -index 0 -exact $startup_plugins $Shortname] eq -1 } {
					set Plugmeta [extract_startup_plugin_info $Plugfilename]
					set Shortname [string map { . "" \[ "" \] "" ( "" ) "" \{ "" \} "" - "" } $Shortname]
					lappend startup_plugins [list $Shortname 0 [lindex $Plugmeta 0] [lindex $Plugmeta 1] [lindex $Plugmeta 2] $Plugfilename]
					::pdwindow::verbose 1 "Enumerated startup plugin $Shortname (disabled)\n"
					puts "Enumerated startup plugin $Shortname (disabled)"
#					puts [file join [file normalize [file join [file dirname $Plugfilename] ".."]] [file tail  $Plugfilename]]
				}
			}
		}
		puts [concat [array size startup_plugins] "Plugins"]
		set startup_plugins [lsort -index 0 -dictionary -increasing $startup_plugins]

		#build window
		toplevel .plugwindow -class DialogWindow
		wm title .plugwindow [_ "Plugins"]
		wm minsize .plugwindow 300 75
		wm geometry .plugwindow =300x400+530+50
		wm resizable .plugwindow 0 1
	
		.plugwindow configure -padx 0 -pady 0

		frame .plugwindow.footer -borderwidth 0 -relief flat
		pack .plugwindow.footer -side bottom -fill x -padx 1 -pady 1
		button .plugwindow.footer.ok -text [_ "Close"] -underline 0 -command { wm withdraw .plugwindow }
		pack .plugwindow.footer.ok -fill both -padx 1 -pady 1
	
		frame .plugwindow.body -borderwidth 0 -relief flat
		scrollbar .plugwindow.body.scroll -command ".plugwindow.body.canvas yview"
		
		pack .plugwindow.body -fill both -expand 1 -padx 1 -ipady 1
		pack .plugwindow.body.scroll -side right -fill y

		canvas .plugwindow.body.canvas -yscrollcommand ".plugwindow.body.scroll set"

		pack .plugwindow.body.canvas -side left -fill both -expand 1

		# create plugins list
		variable Boxheight 25
		variable Boxx 0
		foreach Plugdata $startup_plugins {
			set Shortname [lindex $Plugdata 0]
			if { [lindex $Plugdata 2] eq 0 } {
				lset Plugdata 2 $Shortname
			}
			set Longname [string range [lindex $Plugdata 2] 0 32]
			
			frame .plugwindow.body.canvas.$Shortname -borderwidth 2 -relief ridge
	
			checkbutton .plugwindow.body.canvas.$Shortname.check -variable "::plugins_loaded($Shortname)" -command "::plugins::toggle_startup_plugin $Shortname"
			if { [lindex $Plugdata 1] ne 0 } {
				.plugwindow.body.canvas.$Shortname.check select
			}
			pack .plugwindow.body.canvas.$Shortname.check -side left

			label .plugwindow.body.canvas.$Shortname.name -text $Longname -anchor w -font {-size 8 -weight bold} -justify left
			pack .plugwindow.body.canvas.$Shortname.name -side left
	
			if { [lindex $Plugdata 3] ne 0 || [lindex $Plugdata 4] ne 0 } {
				variable Pluginfo
				if { [lindex $Plugdata 3] ne 0 } {
					set Pluginfo [lindex $Plugdata 3]
				}
				if { [lindex $Plugdata 3] ne 0 && [lindex $Plugdata 4] ne 0 } {
					# no line break allowed, so we try this:
					set Pluginfo "$Pluginfo     __________________________ "
				}
				if { [lindex $Plugdata 4] ne 0 } {
					set Pluginfo [concat $Pluginfo [lindex $Plugdata 4]]
				}
				button .plugwindow.body.canvas.$Shortname.more -text "..." -font {-size 7 -weight bold} -width 3 \
					-command "tk_messageBox -message {$Pluginfo} -parent .plugwindow -title {$Longname} -type ok"
			} else {
				button .plugwindow.body.canvas.$Shortname.more -text "..." -font {-size 7 -weight bold} -width 3 -state disabled
			}
			pack .plugwindow.body.canvas.$Shortname.more -side right

			.plugwindow.body.canvas create window 0 $Boxx -window .plugwindow.body.canvas.$Shortname -width 273 -height $Boxheight
			set Boxx [expr $Boxx + $Boxheight]
		}
		.plugwindow.body.canvas configure -scrollregion [.plugwindow.body.canvas bbox all]
	}
}


if { $::windowingsystem eq "aqua"} {
    set Mymenu .menubar.apple
} else {
    set Mymenu .menubar.media
}
$Mymenu add command -label [_ "Plugins..."] -command {::plugins::create_plugins_window}

pdtk_post "Finished loading plugins-plugin.tcl\n"