/*
 *   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(Swapfile)

- swapfilenameClick:sender
{
    const char *str = [prefSwapfileTextField stringValue];
    
    if(*str == '\000') {
	if(swapfilename != NULL) {
	    free(swapfilename);
	    swapfilename = NULL;
	    [prefSwapfileSizeTextField setStringValue:""];
	    [self stopSpyIfNeeded];
	}
	return self;
    }
    if((swapfilename != NULL) && (strlen(swapfilename) < strlen(str))) {
	free(swapfilename);
	swapfilename = NULL;
    }
    if(swapfilename == NULL)  {
	swapfilename = (char *)malloc(strlen(str)+1);
    }
    strcpy(swapfilename, str);
    [self startSpyIfNeeded];
    return self;
}

- swapfileSetNameClick:sender
{
    id openPanel;
    const char *const *files;
    char tmpfile[MAXFILENAMELENGHT];
    const char *file = swapfilename;

    if(swapfilename == NULL)
	file = "/private/vm";
    openPanel = [OpenPanel new];
    [openPanel chooseDirectories:NO];
    [openPanel allowMultipleFiles:NO];
    if([openPanel runModalForDirectory:file file:NULL] == NX_OKTAG) {
	if((files = [openPanel filenames]) != (const char *const *)0) {
	    sprintf(tmpfile, "%s/%s", [openPanel directory], files[0]);
	    [prefSwapfileTextField setStringValue:tmpfile];
	    [self swapfilenameClick:self];
	}
    }
    return self;
}

- swapfilesizeClick:sender
{
    swapfilesize = [sender intValue] * 1024*1024;
    [self startSpyIfNeeded];
    return self;
}

- updateSwapfile
{
    struct stat st;
    
    if(stat(swapfilename, &st) < 0 && !swapfileError) {
	switch(errno) {
	    case ENOENT:
	    case ENOTDIR:
		printf("filespy: swapfile %s does not exist.\n", swapfilename);
		break;
	    case EACCES:
		printf("filespy: can't access swapfile %s.\n", swapfilename);
		break;
	    default:
		perror("filespy: stat");
	}
	swapfileError = YES;
    } else if(!swapfileTooBig && st.st_size > swapfilesize) {
	printf("filespy: swapfile %s reached size of %.2f MB.\n", swapfilename, (float)st.st_size/(1024*1024));
	swapfileTooBig = YES;
    } else if(swapfileTooBig && st.st_size <= swapfilesize) {
	printf("filespy: swapfile %s shrunk to %.2f MB.\n", swapfilename, (float)st.st_size/(1024*1024));
	swapfileTooBig = NO;
    }
    return self;
}

@end
