2001-11-28 23:44:26 +00:00
|
|
|
/*
|
2002-05-26 11:24:00 +00:00
|
|
|
MainPrefs.m
|
2001-11-28 23:44:26 +00:00
|
|
|
|
|
|
|
Controller class for this bundle
|
|
|
|
|
|
|
|
Copyright (C) 2001 Dusk to Dawn Computing, Inc.
|
|
|
|
Additional copyrights here
|
|
|
|
|
|
|
|
Author: Jeff Teunissen <deek@d2dc.net>
|
|
|
|
Date: 24 Nov 2001
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "Config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-05-06 21:52:58 +00:00
|
|
|
#include <Foundation/NSDebug.h>
|
|
|
|
#include <Foundation/NSPathUtilities.h>
|
|
|
|
#include <Foundation/NSUserDefaults.h>
|
|
|
|
#include <Foundation/NSValue.h>
|
|
|
|
|
|
|
|
#include <AppKit/NSButton.h>
|
|
|
|
#include <AppKit/NSImage.h>
|
|
|
|
#include <AppKit/NSNibLoading.h>
|
|
|
|
#include <AppKit/NSOpenPanel.h>
|
|
|
|
|
|
|
|
#include "PrefsController.h"
|
|
|
|
#include "MainPrefs.h"
|
2001-11-28 23:44:26 +00:00
|
|
|
|
2002-01-25 08:45:28 +00:00
|
|
|
@interface MainPrefs (Private)
|
|
|
|
|
|
|
|
- (NSDictionary *) preferencesFromDefaults;
|
|
|
|
- (void) savePreferencesToDefaults: (NSDictionary *) dict;
|
|
|
|
|
|
|
|
- (void) commitDisplayedValues;
|
|
|
|
- (void) discardDisplayedValues;
|
|
|
|
|
|
|
|
- (void) updateUI;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MainPrefs (Private)
|
|
|
|
|
|
|
|
static NSDictionary *currentValues = nil;
|
|
|
|
static NSMutableDictionary *displayedValues = nil;
|
|
|
|
|
|
|
|
static NSMutableDictionary *
|
|
|
|
defaultValues (void) {
|
|
|
|
static NSMutableDictionary *dict = nil;
|
|
|
|
|
|
|
|
if (!dict) {
|
|
|
|
dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
|
2002-01-26 22:09:15 +00:00
|
|
|
@"/Local/Forge/Projects", @"ProjectPath",
|
|
|
|
[NSNumber numberWithBool: YES], @"BundlesFromUser",
|
|
|
|
[NSNumber numberWithBool: YES], @"BundlesFromLocal",
|
|
|
|
[NSNumber numberWithBool: YES], @"BundlesFromNetwork",
|
|
|
|
[NSNumber numberWithBool: YES], @"BundlesFromSystem",
|
2002-01-25 08:45:28 +00:00
|
|
|
nil];
|
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2002-01-26 22:09:15 +00:00
|
|
|
static BOOL
|
|
|
|
getBoolDefault (NSMutableDictionary *dict, NSString *name)
|
|
|
|
{
|
|
|
|
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name];
|
|
|
|
NSNumber *num;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
str = [[defaultValues() objectForKey: name] stringValue];
|
|
|
|
|
|
|
|
num = [NSNumber numberWithBool: [str hasPrefix: @"Y"]];
|
|
|
|
[dict setObject: num forKey: name];
|
|
|
|
|
|
|
|
return [num boolValue];
|
|
|
|
}
|
|
|
|
|
2002-01-25 08:45:28 +00:00
|
|
|
static NSString *
|
|
|
|
getStringDefault (NSMutableDictionary *dict, NSString *name)
|
|
|
|
{
|
|
|
|
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name];
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
str = [defaultValues() objectForKey: name];
|
|
|
|
|
|
|
|
[dict setObject: str forKey: name];
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) preferencesFromDefaults
|
|
|
|
{
|
|
|
|
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 5];
|
|
|
|
|
2002-01-26 22:09:15 +00:00
|
|
|
getStringDefault (dict, @"ProjectPath");
|
|
|
|
getBoolDefault (dict, @"BundlesFromLocal");
|
|
|
|
getBoolDefault (dict, @"BundlesFromNetwork");
|
|
|
|
getBoolDefault (dict, @"BundlesFromSystem");
|
|
|
|
getBoolDefault (dict, @"BundlesFromUser");
|
2002-01-25 08:45:28 +00:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) savePreferencesToDefaults: (NSDictionary *) dict
|
|
|
|
{
|
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
|
|
#define setStringDefault(name) \
|
|
|
|
[defaults setObject: [dict objectForKey: (name)] forKey: (name)]
|
2002-01-26 22:09:15 +00:00
|
|
|
#define setBoolDefault(name) \
|
|
|
|
[defaults setBool: [[dict objectForKey: (name)] boolValue] forKey: (name)]
|
2002-01-25 08:45:28 +00:00
|
|
|
|
|
|
|
NSDebugLog (@"Updating Main Preferences...");
|
2002-01-26 22:09:15 +00:00
|
|
|
setStringDefault (@"ProjectPath");
|
|
|
|
setBoolDefault (@"BundlesFromLocal");
|
|
|
|
setBoolDefault (@"BundlesFromNetwork");
|
|
|
|
setBoolDefault (@"BundlesFromSystem");
|
|
|
|
setBoolDefault (@"BundlesFromUser");
|
2002-01-25 08:45:28 +00:00
|
|
|
[defaults synchronize];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) commitDisplayedValues
|
|
|
|
{
|
|
|
|
[currentValues release];
|
|
|
|
currentValues = [[displayedValues copy] retain];
|
|
|
|
[self savePreferencesToDefaults: currentValues];
|
|
|
|
[self updateUI];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) discardDisplayedValues
|
|
|
|
{
|
|
|
|
[displayedValues release];
|
|
|
|
displayedValues = [[currentValues mutableCopy] retain];
|
|
|
|
[self updateUI];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) updateUI
|
|
|
|
{
|
2002-01-26 22:09:15 +00:00
|
|
|
[projectPathField setStringValue: [displayedValues objectForKey: @"ProjectPath"]];
|
|
|
|
[bundlesFromLocalButton setIntValue: [[displayedValues objectForKey: @"BundlesFromLocal"] intValue]];
|
|
|
|
[bundlesFromNetworkButton setIntValue: [[displayedValues objectForKey: @"BundlesFromNetwork"] intValue]];
|
|
|
|
[bundlesFromSystemButton setIntValue: [[displayedValues objectForKey: @"BundlesFromSystem"] intValue]];
|
|
|
|
[bundlesFromUserButton setIntValue: [[displayedValues objectForKey: @"BundlesFromUser"] intValue]];
|
|
|
|
[view setNeedsDisplay: YES];
|
2002-01-25 08:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end // MainPrefs (Private)
|
|
|
|
|
2001-11-28 23:44:26 +00:00
|
|
|
@implementation MainPrefs
|
|
|
|
|
|
|
|
static MainPrefs *sharedInstance = nil;
|
|
|
|
static id <BundleDelegate> owner = nil;
|
|
|
|
|
|
|
|
- (id) initWithOwner: (id <BundleDelegate>) anOwner
|
|
|
|
{
|
|
|
|
if (sharedInstance) {
|
|
|
|
[self dealloc];
|
|
|
|
} else {
|
|
|
|
self = [super init];
|
|
|
|
owner = anOwner;
|
|
|
|
[owner registerPrefsController: self];
|
2002-05-26 11:24:00 +00:00
|
|
|
if (![NSBundle loadNibNamed: @"MainPrefs" owner: self])
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
// window can be any size, as long as it's 486x228 :)
|
|
|
|
view = [[window contentView] retain];
|
|
|
|
[window setExcludedFromWindowsMenu: YES];
|
2002-01-25 08:45:28 +00:00
|
|
|
|
|
|
|
[self loadPrefs: self];
|
|
|
|
|
2001-11-28 23:44:26 +00:00
|
|
|
sharedInstance = self;
|
|
|
|
}
|
|
|
|
return sharedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) loadPrefs: (id) sender
|
|
|
|
{
|
2002-01-25 08:45:28 +00:00
|
|
|
if (currentValues)
|
|
|
|
[currentValues release];
|
|
|
|
|
|
|
|
currentValues = [[[self preferencesFromDefaults] copyWithZone: [self zone]] retain];
|
|
|
|
[self discardDisplayedValues];
|
2001-11-28 23:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) savePrefs: (id) sender
|
|
|
|
{
|
2002-01-25 08:45:28 +00:00
|
|
|
[self commitDisplayedValues];
|
2001-11-28 23:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) resetPrefsToDefault: (id) sender
|
|
|
|
{
|
2002-01-25 08:45:28 +00:00
|
|
|
if (currentValues)
|
|
|
|
[currentValues release];
|
|
|
|
|
|
|
|
currentValues = [[defaultValues () copyWithZone: [self zone]] retain];
|
|
|
|
|
|
|
|
[self discardDisplayedValues];
|
2001-11-28 23:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) showView: (id) sender;
|
|
|
|
{
|
2002-05-26 11:24:00 +00:00
|
|
|
[[[owner prefsController] prefsViewBox] setContentView: view];
|
2001-11-28 23:44:26 +00:00
|
|
|
[view setNeedsDisplay: YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSView *) view
|
|
|
|
{
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) buttonCaption
|
|
|
|
{
|
|
|
|
return @"Main";
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSImage *) buttonImage
|
|
|
|
{
|
|
|
|
return [NSImage imageNamed: @"NSApplicationIcon"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (SEL) buttonAction
|
|
|
|
{
|
|
|
|
return @selector(showView:);
|
|
|
|
}
|
|
|
|
|
2002-01-25 08:45:28 +00:00
|
|
|
- (id) projectPath
|
|
|
|
{
|
2002-01-26 22:09:15 +00:00
|
|
|
return [displayedValues objectForKey: @"ProjectPath"];
|
2002-01-25 08:45:28 +00:00
|
|
|
}
|
|
|
|
|
2002-01-26 22:09:15 +00:00
|
|
|
/*
|
|
|
|
Action methods
|
|
|
|
*/
|
|
|
|
- (IBAction) projectPathFindButtonClicked: (id) sender
|
2002-01-25 08:45:28 +00:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
|
|
|
|
|
|
|
|
[oPanel setAllowsMultipleSelection: NO];
|
|
|
|
[oPanel setCanChooseFiles: NO];
|
|
|
|
[oPanel setCanChooseDirectories: YES];
|
|
|
|
|
|
|
|
result = [oPanel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
|
|
|
|
if (result == NSOKButton) { // got a new dir
|
|
|
|
NSArray *pathArray = [oPanel filenames];
|
|
|
|
|
2002-01-26 22:09:15 +00:00
|
|
|
[displayedValues setObject: [pathArray objectAtIndex: 0] forKey: @"ProjectPath"];
|
2002-01-25 08:45:28 +00:00
|
|
|
[self updateUI];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-26 22:09:15 +00:00
|
|
|
- (IBAction) bundlesFromLocalButtonChanged: (id) sender
|
2002-01-25 08:45:28 +00:00
|
|
|
{
|
2002-01-26 22:09:15 +00:00
|
|
|
[displayedValues setObject: [NSNumber numberWithBool: [sender intValue]] forKey: @"BundlesFromLocal"];
|
2002-01-25 08:45:28 +00:00
|
|
|
[self updateUI];
|
|
|
|
}
|
|
|
|
|
2002-01-26 22:09:15 +00:00
|
|
|
- (IBAction) bundlesFromNetworkButtonChanged: (id) sender
|
|
|
|
{
|
|
|
|
[displayedValues setObject: [NSNumber numberWithBool: [sender intValue]] forKey: @"BundlesFromNetwork"];
|
|
|
|
[self updateUI];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) bundlesFromSystemButtonChanged: (id) sender
|
|
|
|
{
|
|
|
|
[displayedValues setObject: [NSNumber numberWithBool: [sender intValue]] forKey: @"BundlesFromSystem"];
|
|
|
|
[self updateUI];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) bundlesFromUserButtonChanged: (id) sender
|
|
|
|
{
|
|
|
|
[displayedValues setObject: [NSNumber numberWithBool: [sender intValue]] forKey: @"BundlesFromUser"];
|
|
|
|
[self updateUI];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) projectPathChanged: (id) sender
|
|
|
|
{
|
|
|
|
[displayedValues setObject: [sender stringValue] forKey: @"ProjectPath"];
|
|
|
|
[self updateUI];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end // MainPrefs
|