
//======================================================================
//
//	Portions written by FreemanSoft Inc.
//
//	FreemanSoft disclaims any warranty of any kind, expressed or implied,
//	as to this source code's fitness for any particular use.
//
//	For more information, use the following electronic mail addresses:
//	 
//		info@FreemanSoft.com	general questions
//		support@FreemanSoft.com	technical questions
//
//======================================================================


/* Written by
 *	Joe Freeman	jfreeman@next.com	
 *	PreferencesPanel
 *
 *	This code has no warranty.  
 *	It is provided so that the consumer may maintain and modify it
 *	at their own risk.  Use this code at your own risk.
 */

#import "PreferencesPanel.h"
#import "common.h"

#define	RDIST_SOURCE	1
#define	RDIST_DEST	2
#define	RDIST_ABSOLUTE	0
#define	RDIST_RELATIVE	1
#define	DUP_UPDATE	0
#define	DUP_EXACT	1

@implementation PreferencesPanel

+ new
{
    static id 	thePanel;
    
    if (!thePanel)
    {
	thePanel  = [NXApp loadNibSection:"PrefPanel.nib" owner:self];
	[thePanel setFrameUsingName:pref_frame];
	[thePanel setFrameAutosaveName:pref_frame];
	[thePanel loadPatternFile:thePanel];
    }
    
    return thePanel;
}

- loadPatternFile:sender
{
    NXStream *	ourStream;
    
    ourStream = [[NXApp delegate] mapResource:pat_file];
    if (ourStream){
	[exceptPatText readText:ourStream];
	[exceptPatText sizeToFit];
	NXClose(ourStream);
    }
    return self;
}

- savePatternFile:sender
{
    char 	buf[MAXPATHLEN];
    NXStream *	ourStream;
    
    /* assume directory already built by app somewhere else */
    strcpy(buf,NXHomeDirectory());
    strcat(buf,"/");
    strcat(buf,lib_dir);
    strcat(buf,"/");
    strcat(buf,pat_file);
    ourStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
    if (ourStream){
    	[exceptPatText writeText:ourStream];
	NXSaveToFile(ourStream, buf);
	NXCloseMemory(ourStream, NX_FREEBUFFER);
    }
    return self;
}

- makeKeyAndOrderFront:sender
{
    const char *temp;

    /* now load path preferences UI from the defaults */
    temp = NXGetDefaultValue([NXApp appName],path_default);
    if 	(!strcmp(temp, rdist_absolute))
			[pathPreference selectCellWithTag:RDIST_ABSOLUTE];
    else if (!strcmp(temp, rdist_relative))
			[pathPreference selectCellWithTag:RDIST_RELATIVE];

    /* now load method preferences UI from the defaults */
    temp = NXGetDefaultValue([NXApp appName],dup_default);
    if	(!strcmp(temp, dup_update))	
			[methodPreference selectCellWithTag:DUP_UPDATE];
    else if (!strcmp(temp, dup_exact))
			[methodPreference selectCellWithTag:DUP_EXACT];
    
    /* should we do a find before each backup */
    temp = NXGetDefaultValue([NXApp appName], find_no_write);
    if (!strcmp(temp, yes_str))
    	[findWritable setState:1];
    else 
    	[findWritable setState:0];
    
    [super makeKeyAndOrderFront:self];
    return self;
}

/*================================================================
 *	preferences panel 
 *================================================================*/
 

- changePathPrefer:sender
{
    if ([sender selectedTag] == RDIST_RELATIVE)
		NXWriteDefault([NXApp appName], path_default, rdist_relative);
    else if ([sender selectedTag] == RDIST_ABSOLUTE)
		NXWriteDefault([NXApp appName], path_default, rdist_absolute);
    [[NXApp delegate] preferencesChanged:self];
    NXWriteDefault([NXApp appName], info_default, no_str);
    return self;
}

- changeMethodPrefer:sender
{
    if ([sender selectedTag] == DUP_UPDATE)
		NXWriteDefault([NXApp appName], dup_default, dup_update);
    else if ([sender selectedTag] == DUP_EXACT)
		NXWriteDefault([NXApp appName], dup_default, dup_exact);
    NXWriteDefault([NXApp appName], info_default, no_str);
    return self;
}

- changeFindWritablePrefer:sender
{
    if ([findWritable state] == 0)
		NXWriteDefault([NXApp appName], find_no_write, no_str);
    else 
    		NXWriteDefault([NXApp appName], find_no_write, yes_str);
    return self;
}

- getExemptString:(char **)bufPtr length:(int *)length
{
    NXStream *	ourStream;
    int		maxlen;
    
    ourStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
    if (ourStream){
    	[exceptPatText writeText:ourStream];
	NXGetMemoryBuffer(ourStream, bufPtr, length, &maxlen);
	NXCloseMemory(ourStream, NX_SAVEBUFFER);
    }
    /* always save last run's pattern file */
    [self savePatternFile:self];
    return self;
}

   
@end
