package Type::Text; ###### 
# 	$Id: Text.pm,v 1.3 2003/05/09 19:32:14 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::Text is the Type for text/plain and the base class for text/*
#
#	This is the base class for Type handlers for text of various 
#	sorts.  
#	

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

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



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

### $t->getContentType($cxt, $realpath)
#	Returns the content type (MIME type) of the resource.  (We have to 
#	pass the path because sometimes the type can't be determined without
#	looking at the filename -- some Type classes are used for multiple
#	MIME types.
sub getContentType {
    my ($self, $cxt, $realpath) = @_;

    return Type::Manager::getContentType($cxt, $realpath);
}


### $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 1;
}

### $t->canEdit($cxt)
#	Returns true if we know how to edit a resource of this type.
sub canEdit {
    my ($self, $cxt) = @_;

    return 1;
}

### $t->canCreate($cxt)
#	Returns true if we know how to create a resource of this type by
#	editing a nonexistent file.  Note that it does _not_ imply that
#	it is possible or permissible to create any _particular_ file.
sub canCreate {
    my ($self, $cxt) = @_;

    return 1;
}



#############################################################################
###
### 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 ($self, $cxt, $realpath) = @_;
    my $root = $self->getDocRoot;
    my $txt = entityEncode(getFile($self->getFilePath($cxt, $realpath)));

    # === get rows, cols from $cxt

    return ("<textarea name='text' rows='30' cols='72'>$txt</textarea>\n");
}


### $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;
