#!./tclsh
# $Id: folders 1266 2009-07-14 18:39:12Z hubert@u.washington.edu $
# ========================================================================
# Copyright 2008 University of Washington
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# ========================================================================

#  view.tcl
#
#  Purpose:  CGI script folders for Web Alpine 2.0 pages
#
#  Input:    PATH_INFO: [/<col_number>]/<folder_name>[/<uid_of_viewed_msg>
#            along with possible search parameters:
set folders_args {
}

# inherit global config
source ./alpine.tcl
source ./foldercache.tcl
source ./common.tcl

# TEST
proc cgi_suffix {args} {
  return ""
}

# WHILE TESTING/DEBUGGING
proc noimp {s} {
  return "onClick=return noImp('${s}');"
}

set dmsgs ""
proc dm {s} {
  global dmsgs

  lappend dmsgs $s
}

# default location state
set c -1
set f ""

# grok PATH_INFO for collection 'c' and folder 'f'
if {[info exists env(PATH_INFO)] && [string length $env(PATH_INFO)]} {
  # look for collection number, c
  if {[regexp {^/([0-9]+)(/.*)} $env(PATH_INFO) dummy c r]} {
    if {![string length $r]} {
      set f $c
      set c -1
    }
  } else {
    set r $env(PATH_INFO)
  }

  # look for folder name (f) in remainder, r
  if {[regexp {^/([^/]+)((/.*)|$)} $r dummy f r]} {
    # look for uid of page's top message, u
    if {[regexp {^/([0-9a-fA-F]+)} $r dummy u]} {
    } else {
    }
  } else {
    if {[string length $r]} {
      # can't grok, reset context
      set c -1
    }
  }
}

WPEval $folders_args {
  # verify current collection/folder
  if {[catch {WPCmd PEFolder current} curfold]} {
    error [list _action browse "cannot determine default folder: $curfold"]
  } else {
    set curc [lindex $curfold 0]
    set curf [lindex $curfold 1]
  }

  # "current" folder's context
  if {$c < 0} {
    set c $curc
  }

  # "current" folder
  if {0 == [string length $f]} {
    set f $curf
  }

  # open a different folder?
  if {$c != $curc || [string compare $f $curf]} {
    # BUG: validate $u?

    error [list _action browse "NOT PREPARED TO OPEN NEW FOLDER $f YET"]
    if {[catch {eval WPCmd PEMailbox open [list $c $f]} reason]} {
    } else {
      # STATUS: Opened Folder $f
    }
  }

  set defc [WPCmd PEFolder defaultcollection]
  set cols [WPCmd PEFolder collections]
  
  set charset "UTF-8"
  set u 0

  cgi_http_head {
    WPStdHttpHdrs "text/html; charset=$charset"
  }

  cgi_html {
    cgi_head {
      cgi_content_type "text/html; charset=$charset"
      cgi_title [wpPageTitle "Folder Management"]
      cgi_base "href=$_wp(serverpath)/$_wp(appdir)/$_wp(ui2dir)/"
      cgi_stylesheet css/menu.css
      cgi_stylesheet css/cbn/screen.css
      cgi_stylesheet css/cbn/folders.css
      # Yahoo Styles
      cgi_stylesheet $_wp(yui)/build/container/assets/container-core.css
      cgi_stylesheet $_wp(yui)/build/menu/assets/skins/sam/menu.css
      cgi_stylesheet $_wp(yui)/build/button/assets/skins/sam/button.css
      # YahooUI libraries
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/utilities/utilities.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/container/container-min.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/datasource/datasource-min.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/menu/menu-min.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/button/button-min.js" {}
      # local libraries
      cgi_script language="JavaScript" src="lib/common.js" {}
      cgi_script language="JavaScript" src="lib/folders.js" {}
      cgi_javascript {
	cgi_puts "YAHOO.alpine.cgi_root = '$_wp(serverpath)';"
	cgi_puts "YAHOO.alpine.current.incoming = [WPCmd PEFolder isincoming $c];"
	cgi_puts "YAHOO.alpine.current.c = $c;"
	cgi_puts "YAHOO.alpine.current.f = \"$f\";"
	foreach col $cols {
	  regsub {'} [lindex $col 1] {\'} colname
	  lappend colnames "'$colname'"
	}
	cgi_puts "YAHOO.alpine.current.u = $u;"
	cgi_puts "YAHOO.alpine.current.collections = \[[join $colnames {,}]\];"
	cgi_puts "function bodyOnLoad() {"
	cgi_puts " initMenus();"
	cgi_puts " if(YAHOO.env.ua.gecko > 0){ sizeVPHeight(); window.onresize = resizeVPHeight; }"
	cgi_puts " setCheckMailFunction('gCheck', newMailCheck);"
	cgi_puts " setNewMailCheckInterval([WPCmd PEInfo inputtimeout]);"
	cgi_puts " drawFolderList('alpineContent',$defc);"
        cgi_puts "}"

	# WHILE TESTING/DEBUGGING
	cgi_puts "var impi = new Array(); function noImp(m){ var sm; if(impi\[m\]) {impi\[m\]++; if(impi\[m\] > 4) sm = '<b>Seriously,</b> ' + m + ' will <b>never</b> get implemented if you keep bothering me!'; else sm = m + ' is <b>still</b> not implemented!'; } else { sm = m + ' is not implemented yet!' ; impi\[m\] = 1 ;} showStatusMessage(sm,3); return false; }"

	cgi_puts "browserDetect();"
      }
    }

    cgi_body class=wap "onLoad=bodyOnLoad()" {
      cgi_puts {<iframe name="formResponse" id="formResponse" src="img/cbn/spritelib.gif"></iframe>}
      wpCommonPageLayout folders "$c" "$f" "$u" "View and Manage Folders" [list [cgi_cgi "$_wp(appdir)/$_wp(ui2dir)/browse/${c}/${f}"] "Folder List" 0 searchContent('folders','flistFolders')] "" {
	# CONTEXT COMMANDS
	cgi_division class=hdrBtns {
	  cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi1" ""][cgi_span "class=hdrBtnText" Print]" "print" "onClick=return printContent()"]
	  cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi2" ""][cgi_span "class=hdrBtnText" Settings]" "settings"]
	  cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi3" ""][cgi_span "class=hdrBtnText" Help]" "javascript:openHelpWindow('folders.html');" class=wap]
	  cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi4" ""][cgi_span "class=hdrBtnText" "Sign out"]" "../../session/logout.tcl?cid=[WPCmd PEInfo key]&sessid=${sessid}"]
	}
      } {
	# TO MENUBAR
	cgi_anchor_name "toolbar"
	cgi_table  class="toolbarTbl" cellpadding="0" cellspacing="0" {
	  cgi_puts "<tbody>"
	  cgi_table_row  {
	    cgi_table_data {
	      cgi_table class="toolbarTbl" cellpadding="0" cellspacing="0" {
		cgi_puts "<tbody>"
		cgi_table_row {
		  cgi_table_data class=wap {
		    cgi_puts [cgi_url [cgi_span "class=sp spmbi spmb18" "New Folder"] "#" title="Create new folder in this folder" "onClick=return addFolder();"]
		  }
		  cgi_table_data  class="dv1" {
		    cgi_puts [cgi_img "img/cbn/div.gif" class=wap]
		  }
		  cgi_table_data class=wap {
		    cgi_puts [cgi_url [cgi_span "class=sp spmbi spmb19" "View Messages"] "#" title="View Messages in selected folder" id=butChoose "onClick=return doOpen();"]
		  }
		  cgi_table_data class=wap {
		    cgi_puts [cgi_url [cgi_span "class=sp spmbi spmb7" "Delete"] "#" title="Delete selected folder" "onClick=return queryDelete();"]
		  }
		  cgi_table_data class=wap {
		    cgi_puts [cgi_url [cgi_span "class=sp spmbi spmb20" "Rename"] "#" title="Rename selected folder" "onClick=return renameFolder();"]
		  }
		  cgi_table_data  class="dv1" {
		    cgi_puts [cgi_img "img/cbn/div.gif" class=wap]
		  }
		  cgi_table_data class=wap {
		    cgi_bullet_list class="menu" {
		      cgi_put "<li class=\"menuHdr\">[cgi_url "More Actions [cgi_img "img/cbn/menu.gif" class="wap menuDn menuImg"]" "#" "onClick=this.blur(); return false;"]<div>"
		      cgi_bullet_list {
			cgi_li [cgi_url "Import Email into Folder" "#" "onClick=return importFolder();"]
			cgi_li [cgi_url "Export Email from Folder" "#" "onClick=return exportFolder();"]
		      }
		      cgi_puts "</div></li>"
		    }
		  }
		  cgi_table_data  width="100%" {
		    cgi_puts [cgi_nbspace]
		  }
		}
		cgi_puts "</tbody>"
	      }
	    }
	  }
	  cgi_puts "</tbody>"
	}
      } {
	cgi_puts "Loading..."
      } {
	# BOTTOM MENUBAR
	cgi_table class="wap toolbarTbl" cellpadding="0" cellspacing="0" {
	  cgi_puts "<tbody><tr><td>&nbsp;</td></tr></tbody>"
	}
      }

      cgi_text "pickFolderCollection=$defc" type=hidden id=pickFolderCollection notab
      cgi_text "pickFolderPath=" type=hidden id=pickFolderPath notab
      cgi_text "pickFolderName=" type=hidden id=pickFolderName notab

      # any debugging info to insert?
      foreach dmsg $dmsgs {
	cgi_html_comment "DEBUG: $dmsg"
	cgi_puts ""
      }
    }
  }
}
