#!./tclsh
# $Id: browse 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
#
# ========================================================================

#  browse
#
#  Purpose:  CGI script that generates a page displaying a message
#            list of the indicated folder. 
#
#  Input:    PATH_INFO: [/<col_number>]/<folder_name>[/<uid_of_first_msg>
#            along with possible search parameters:
set browse_args {
  {u		{}	0}
  {pageFirst	{}	""}
  {pagePrev	{}	""}
  {pageNext	{}	""}
  {pageLast	{}	""}
  {sort		{}	""}
  {rev		{}	""}
}

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

# default browse state
set c 0
set f INBOX

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

# for inserting debug comments at end of page
set dmsgs ""
proc dm {s} {
  global dmsgs
  lappend dmsgs $s
}

WPEval $browse_args {
  # grok PATH_INFO for collection, 'c', and folder 'f'
  if {[info exists env(PATH_INFO)] && [string length $env(PATH_INFO)]} {
    if {![regexp {^/([0-9]+)/(.*)$} $env(PATH_INFO) dummy c f]} {
      WPCmd PEInfo statmsg "Cannot open invalid path: $env(PATH_INFO)"
      set c 0
      set f INBOX
    }
  } elseif {[catch {WPCmd PEFolder current} curf]
	    || [lindex $curf 0] != 0
	    || [string compare -nocase [lindex $curf 1] inbox]} {
    WPCmd PEInfo statmsg "No Folder Specified, opening INBOX"
  }

  # verify or visit specified collection/folder

  if {[catch {setCurrentFolder c f u} result]} {
    set authlist [wpHandleAuthException $result [list $c "open folder $f"] $f]
    if {0 == [llength $authlist]} {
      WPCmd PEInfo statmsg "$result"
      set c 0
      set f INBOX
    } else {
      set opening_folder 1
    }
  }

  if {[info exists opening_folder]} {
    set n 1
    set u 0
  } else {
    # move anything deleted to Trash
    if {[catch {WPCmd PEMailbox trashdeleted current} result]} {
      WPCmd PEInfo statmsg "Trash move Failed: $result"
    }

    # set uid to "current" message?
    if {$u <= 0 && 0 == [catch {WPCmd PEMailbox current} cm]} {
      set u [lindex $cm 1]
    }
  }

  # get default sort
  if {[catch {WPCmd PEMailbox sort} cursort]} {
    set sort date
    set rev 1
  }

  # load index drawing routine for this session
  # save perpage source, proc overhead
  # call to reinstall during debugging:
  #catch {WPCmd rename drawMessageList {}}
  if {0 == [llength [WPCmd info commands drawMessageList]]} {
    set cgidir [file join $_wp(cgipath) $_wp(appdir) $_wp(ui2dir)]
    if {[catch {
      WPCmd source "$_wp(confdir)/alpine.tcl"
      WPCmd source "${cgidir}/common.tcl"
      WPCmd source "${cgidir}/messagelist.tcl"
      WPCmd source "${cgidir}/messageview.tcl"
      WPCmd rename WPCmd {}
      WPCmd rename WPEval {}
      WPCmd rename WPGetInputAndID {}
      WPCmd rename WPCmdEval WPCmd
    } result]} {
      error [list _action browse "cannot load index lister: $result"]
    }
  }

  # current context or folder changed?
  if {$c == 0 && 0 == [string compare -nocase $f inbox]} {
    set f INBOX
  }

  # gain situational awareness, UID and number of message that must be on page
  if {[catch {WPCmd PEMailbox focus} focused]} {
    set mc 0
    set focused 0
  } elseif {$focused > 0} {
    set mc $focused
  } else {
    set mc [WPCmd PEMailbox messagecount]
  }

  if {$u == 0} {
    # non given, set to first message in folder
    set n 1
    if {[catch {WPCmd PEMailbox uid 1} u]} {
      set n 0
      set u 0
    }
  } elseif {[catch {WPCmd PEMessage $u number} n]} {
    set n 0
    set u 0
  }

  # lines per page
  if {[catch {WPCmd PEInfo indexlines} ppg] || $ppg <= 0} {
    set ppg $_wp(indexlines)
  } elseif {$ppg > $_wp(indexlinesmax)} {
    set ppg $_wp(indexlinesmax)
  }

  # look for image button clicks
  foreach item $browse_args {
    if {0 == [catch {cgi_import [lindex $item 0].x}]} {
      set [lindex $item 0] 1
    }
  }

  # perform actions specified in browse_args
  # NOTE: basic stuff only to be executed in absence of JS
  if {[string length $pageFirst]} {
    set n 1
    set u [WPCmd PEMailbox uid 1]
  } elseif {[string length $pagePrev]} {
    if {$ppg >= $n} {
      set n 1
    } else {
      set n [expr {$n - $ppg}]
    }

    if {[catch {WPCmd PEMailbox uid $n} u]} {
      set u 0
    }
  } elseif {[string length $pageNext]} {
    if {($n + $ppg) <= $mc} {
      incr n $ppg
      if {[catch {WPCmd PEMailbox uid $n} u]} {
	set u 0
      }
    }
  } elseif {[string length $pageLast]} {
    set n $mc
    if {[catch {WPCmd PEMailbox uid $n} u]} {
      set u 0
    }
  } elseif {[string length $sort]} {
    switch $rev {
      0 -
      1 {
      }
      default {
	set rev [lindex $cursort 1]
      }
    }

    if {[catch {WPCmd PEMailbox sort $sort $rev} cursort]} {
      WPCmd PEInfo statmsg "Cannot set sort: $cursort"
      set cursort [list nonsense 0]
    } else {
      # store result 
      WPCmd set sort [list $sort $rev]
    }
  }

  # page framing
  if {[catch {
    set nm [WPCmd PEMailbox flagcount [list unseen undeleted]]
    set defcol [WPCmd PEFolder defaultcollection]
    wpInitPageFraming u n mc ppg pn pt
  }]} {
    set pn 1
    set pt 0
    set nm 0
    set defcol 0
  }

  cgi_http_head {
    WPStdHttpHdrs "text/html; charset=UTF-8"
  }

  cgi_html {
    cgi_head {
      #      WPStdHttpHdrs "text/html; charset=UTF-8"
      cgi_title [wpPageTitle "$f, $pn of $pt ($nm)"]
      cgi_base "href=$_wp(serverpath)/$_wp(appdir)/$_wp(ui2dir)/"
      cgi_stylesheet css/menu.css
      cgi_stylesheet css/cbn/screen.css
      cgi_stylesheet css/cbn/folderdialog.css
      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 type=text/javascript language="JavaScript" src="lib/common.js" {}
      cgi_script type=text/javascript language="JavaScript" src="lib/mailbox.js" {}
      # page specific JS
      cgi_javascript {
	cgi_puts "YAHOO.alpine.cgi_root = '$_wp(serverpath)';"
	cgi_puts "YAHOO.alpine.app_root = '$_wp(serverpath)/$_wp(appdir)/$_wp(ui2dir)';"
	cgi_puts "YAHOO.alpine.current.c = $c;"
	cgi_puts "YAHOO.alpine.current.f = \"$f\";"
	cgi_puts "YAHOO.alpine.current.u = $u;"
	cgi_puts "YAHOO.alpine.current.page = $pn;"
	cgi_puts "YAHOO.alpine.current.count = $mc;"
	if {[catch {
	  set mb_sel [WPCmd PEMailbox selected]
	  set mb_srch [WPCmd PEMailbox searched]
	}]} {
	    set mb_sel 0
	    set mb_srch 0
	}
	
	cgi_puts "YAHOO.alpine.current.selected = $mb_sel;"
	cgi_puts "YAHOO.alpine.current.searched = $mb_srch;"
	cgi_puts "YAHOO.alpine.current.focused = $focused;"
	cgi_puts "function bodyOnLoad(){"
	cgi_puts " initMenus();"
	cgi_puts " initMorcButton('listMorcButton');"
	cgi_puts " initSelection();"
	cgi_puts " if(YAHOO.env.ua.gecko > 0){ sizeVPHeight(); window.onresize = resizeVPHeight; }"
	wpSetMessageListNewMailCheck
	wpStatusAndNewmailJavascript
	wpSaveMenuJavascript "browse" $c $f $defcol morcInBrowseDone
	cgi_puts " if(self.loadDDElements) loadDDElements();"

	if {[info exists authlist]} {
	  cgi_puts "YAHOO.alpine.resubmitRequest = function(){ window.location.href = 'browse/$c/$f'; };"
	  cgi_puts "YAHOO.alpine.cancelRequest = function(){ window.location.href = 'browse/0/INBOX'; };"
	  reportAuthException $authlist
	}
	cgi_puts "}"
	cgi_puts "browserDetect();"
      }
    }

    cgi_body class="wap" "onLoad=bodyOnLoad()" {
      cgi_division id="skip" {
	cgi_put [cgi_url "Skip to Next Page" "\#" "onClick=return newMessageList({control:this,parms:{op:'next'}});"]
	cgi_put [cgi_url "Skip to Folders" "folders"]
	cgi_put [cgi_url "Skip to Compose" "compose"]
      }

      cgi_division id=msgDragProxy {
	cgi_put "<b class=b1></b><b class=b2></b><b class=b3></b><b class=b4></b><div id=msgDragProxyText></div><b class=b4></b><b class=b3></b><b class=b2></b><b class=b1></b>"
      }

      if {$focused} {
	set context "Search Results in [cgi_quote_html $f]"
      } else {
	set context [cgi_quote_html $f]
      }
      wpCommonPageLayout browse $c $f $u $context [list [cgi_cgi "$_wp(appdir)/$_wp(ui2dir)/browse/${c}/${f}"] "$f" 1 mailboxSearch()] "" {
	cgi_division class=hdrBtns {
	  cgi_javascript {
	    cgi_put "if(window.print) document.write('[cgi_buffer {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]" \# "onClick=return openMailboxHelp();" 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}"]
	}
      } {
	cgi_division id=listTopMenubar {
	  cgi_puts [WPCmd cgi_buffer "drawTopListMenuBar $c {$f}"]
	}
	cgi_division id=viewTopMenubar "style=\"display: none;\"" {
	  cgi_puts [WPCmd cgi_buffer "drawTopViewMenuBar $c {$f} $u $n"]
	}
      } {
	if {[info exists opening_folder]} {
	  cgi_puts "Opening $f..."
	} else {
	  cgi_puts [WPCmd cgi_buffer "drawMessageList $c {$f} $n $ppg"]
	}
      } {
	cgi_division id=listBottomMenubar {
	  cgi_puts [WPCmd cgi_buffer "drawBottomListMenuBar $c {$f} $pn $pt $mc"]
	}
	cgi_division id=viewBottomMenubar "style=\"display: none;\"" {
	  cgi_puts [WPCmd cgi_buffer "drawBottomViewMenuBar $c {$f} $u $n $mc"]
	}
      }

      foreach dmsg $dmsgs {
	cgi_html_comment "DEBUG: $dmsg"
	cgi_puts ""
      }
    }
  }
}
