/*
 *   This sourcecode is part of FileSpy, a logfile observing utility.
 *   Copyright (C) 1996  Felix Rauch
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *   
 *   Notice that this program may not be possessed, used, copied,
 *   distributed or modified by people having to do with nuclear
 *   weapons. See the file CONDITIONS for details.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   To contact the original author, try:
 *   e-mail: Felix.Rauch@nice.ch
 *   Traditional mail:	Felix Rauch
 *			Sempacherstrasse 33
 *			8032 Zurich
 *			Switzerland
 */

#import "Controller.h"

@implementation Controller(Services)

- registerServices:sender
{
    const char *sendTypes[2];
    const char *returnTypes[1];
    sendTypes[0] = NXFilenamePboardType;
    sendTypes[1] = NULL;
    returnTypes[0] = NULL;
    [NXApp registerServicesMenuSendTypes:sendTypes andReturnTypes:returnTypes];

    return self;
}

- validRequestorForSendType:(NXAtom)typeSent andReturnType:(NXAtom)typeReturned
{
    if ((typeSent == NXFilenamePboardType || typeSent == NULL) && (typeReturned == NULL)) {
	List *cellList;
	
	if(!tmpList)
	    tmpList = [[List alloc] init];
	cellList = [tmpList empty];
	
	[myMatrix getSelectedCells:cellList];
        if (([cellList count] > 0 || typeSent == NULL) && (typeReturned == NULL))
            return self;
    }
    return nil;
}

- (BOOL)writeSelectionToPasteboard:(Pasteboard *)pboard types:(NXAtom *)types
{
    while (types && *types) {
	if (*types == NXFilenamePboardType)
	    break;
	types++;
    }

    if(types && *types)
    {
    	const char *types[2] = {NXFilenamePboardType, ""};
	List *cellList = [tmpList empty];
	unsigned int i, max, len = 0;
	char *string, *orig;
	const char *filename;
	
	[myMatrix getSelectedCells:cellList];
	max = [cellList count];
	for(i = 0; i < max; i++)
	    len += strlen([[cellList objectAt:i] stringValue]) + 1;
	string = orig = (char *)malloc(len*sizeof(char));
	*string = '\000';
	for(i = 0; i < max; i++) {
	    filename = [[cellList objectAt:i] stringValue];
	    strcat(string, filename);
	    strcat(string, "\t");
	    string += strlen(filename) + 1;
	}
	if(max > 0)
	    string[-1] = '\000';

	[pboard declareTypes:types num:1 owner:nil];
	[pboard writeType:NXFilenamePboardType data:orig length:len];
	return YES;
    } else {
	return NO;
    }
}

- spyFileFromService:pasteboard userData:(const char *)userData error:(char **)msg
{
    char *buffer, *data, c, *p, *q;
    int length;
    
    [pasteboard types];    // pretend to check the pasteboard types
    
    if ([pasteboard readType:NXFilenamePboardType data:&data length:&length])
    {
        buffer = malloc(length+1);
	
	p = buffer;
	strncpy(buffer, data, length);
	buffer[length] = '\000';
	while(*p != '\000') {
	    q = p;
	    while((*q != '\000') && (*q != '\t'))	// search end of current filename
		q++;
	    c = *q;					// remember tab or \0
	    if(c == '\t')				// if it's just a tab..
		*q = '\000';				// .. then terminate string temporarily
	    [self addFileWithName:p];
	    if(c == '\t')
		p = q + 1;
	    else
		p = q;
	}
        
        free(buffer);
	[pasteboard deallocatePasteboardData:data length:length];
    }
    else
	*msg = "Error: couldn't spy file.";	// eventually improve this
    
    return self;
}

@end
