package Type::Dir; ###### 
# 	$Id: Dir.pm,v 1.1 2003/05/09 19:48:50 steve Exp $
#	This file is part of Combo and is licensed under the GPL.
#	Copyright 2001-2002 by the authors.  See README and LICENSE
#	for more details or see http://penguinsong.org/.

### Type::Dir is the Type for "normal" directories, and the base class for 
#	other, more specialized, directories.  The general idea is to allow
#	directories to be presented and managed in specialized ways, e.g., 
#	as a document plus images, multipart document, blog, wiki, and so on.
#	

use Context;
use Combo;
use Site::Config;
use Site::Util;

BEGIN {
    use vars qw($VERSION @ISA);
    
    $VERSION=1.00;
    @ISA = qw(Type::Html Type::Base);
}



#############################################################################
###
### Information Functions:
###

### $t->getContentType($cxt, $realpath)
#	Returns the content type (MIME type) of the resource.  In this case,
#	it's guaranteed to be text/html.
sub getContentType {
    my ($self, $cxt, $realpath) = @_;

    return "text/html";
}


### $t->canDetermineSize($cxt)
#	Returns true if the size if the content can be determined in advance.
#	Should return 0 if processing is done on the content.
#
sub canDetermineSize {
    my ($self, $cxt) = @_;
    return 0;
}



#############################################################################
###
### Processing Functions:
###

# Note:	We assume that by the time we get to a processing function, all error
#	and permission checking has already been done by the caller, so
#	that all the Type handler has to do is perform the operation.

# Note:	All processing functions take a (url)path as well as a Context.  In
#	some cases, the path of the resource to be retrieved, queried, or 
#	processed is different from that of the resource that was the subject
#	of the original request.  


### $s = $t->editForm($cxt, $realpath)
#	Returns the contents of a form suitable for editing the resource.  
#	Does _not_ include the <form...> tags because those need to be
#	computed by the caller.  Returns null if the object can't be edited.
sub editForm {
    my ($self, $cxt, $realpath) = @_;
    my $root = $self->getDocRoot;

    my $form = "";

    opendir(DIR, "$root$path");
    my @files = readdir(DIR);
    closedir DIR;

    # Print the directory contents as a table.
    # Each line in the table includes a radio button for selecting the file.

    # === came from dir-index; the links are broken.

    $form .= "<table width='95%' border='1' align='center' cellpadding='5'>\n";
    #$form .= "  <tr><th align='center'>  </th></tr>\n";
    $form .= "<tr><td>\n";
    $form .= "<table>\n";
    $form .= "<tr><th>sel</th>\n";
    $form .= "    <th align='right'>size</th>\n";
    $form .= "    <th>edit</th>\n";
    $form .= "    <th>view</th>\n";
    $form .= "    <th align='left'>download</th>\n";
    $form .= "</tr>\n";
    # === need parent link here. ===
    foreach my $f (sort @files) {
	next if $f =~ /^\.|CVS|~$/;
	$f .= '/' if -d "$root$path$f";
	my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, 
	    $atime, $mtime, $ctime, $blksiz, $blks) 
	    = stat "$root$path$f";
	$form .= "<tr>\n";
	$form .= "    <td><input type='radio' name='file' value='$f'></td>\n";
	$form .= "    <td align='right'>$size</td>\n";
	# "preview" may want to be "edit" instead
	$form .= "    <td><a href='/Ccgi/edit.cgi$path$f'>[edit]</a></td>\n";
	$form .= "    <td><a href='$path$f'>[view]</a></td>\n";
	$form .= "    <td><code><a href='$f'>$f</a></code></td></tr>\n";
    }
    $form .= "</table>\n";

    # Actions to perform on selected files.
    # === grab info from $q and insert into form where appropriate ===

    $form .= "</td></tr><tr><td>\n";
    $form .= "<a name='forms'>&nbsp;operate on selected file:</a> \n";
    $form .= "<input type='submit' name='op' value='Delete'>\n";
    $form .= "<input type='submit' name='op' value='Rename'>\n";
    $form .= "to <input name='to'>\n";

    $form .= "</td></tr></table>\n";

    # form for upload
    if (0) {
    print "<hr>\n";
    print "<form method='POST' enctype='multipart/form-data'>\n";
    print "  <input type='submit' name='op' value='Upload'>\n";
    print "  <input type='file' name='file'>\n";
    print " Upload a file from your local computer. \n";
    print "</form>\n";
    print "<hr>\n";
    print "<form method='POST'>\n";
    print " new: \n";
    print "  <input type='submit' name='op' value='directory'>\n";
    print "  <input type='submit' name='op' value='text file'>\n";
    print "  <input name='file'>\n";
    print "</form>\n";
}
    return $form;
}


### $s = $t->preview($cxt, $realpath)
#	prints a preview, given the POST response to an edit form.
sub preview {
    my ($self, $cxt, $realpath) = @_;
}


### $s = $t->previewString($cxt, $realpath)
#	Returns a preview string, given the POST response to an edit form.
sub previewString {
    my ($self, $cxt, $realpath) = @_;
}


### $t->process($cxt, $text)
#	output the result of processing the content of the resource.
#	The default, of course, is simply to copy the file to STDOUT.
sub process {
    my ($self, $cxt, $realpath) = @_;

    if (!open(F, $filePath)) {
	return "Cannot open file '$realpath' for reading.";
    }

    my $chunk = '';
    while(read(F, $chunk, 4096)) {
	print $chunk;
    }
    close(F);
    return '';
}


### $t->processText($cxt, $text)
#	output the result of processing $text as if it had been the
#	content of the resource; typically done for a preview.
sub processText {
    my ($self, $cxt, $text) = @_;
    print $text;
}



#############################################################################
###
### HTTP Request Handlers:
###


### $t->handleQuery($cxt, $realpath, $queryString)
#	Handle a (direct) GET request with a query string.
sub handleQuery {
    my ($self, $cxt, $realpath) = @_;
}

### $t->handlePOST($cxt, $realpath)
#
sub handlePOST {
    my ($self, $cxt, $realpath) = @_;
}

### $t->handlePUT($cxt, $realpath)
#	Handle a PUT request to the resource. 
sub handlePUT {
    my ($self, $cxt, $realpath) = @_;
}



#############################################################################
1;
