/* Copyright (C) 2002-2003, Jeffrey D. Argast. The authors make NO WARRANTY or representation, either express or implied, with respect to this software, its quality, accuracy, merchantability, or fitness for a particular purpose. This software is provided "AS IS", and you, its user, assume the entire risk as to its quality and accuracy. Permission is hereby granted to use, copy, modify, and distribute this software or portions thereof for any purpose, without fee, subject to these conditions: (1) If any part of the source code is distributed, then this statement must be included, with this copyright and no-warranty notice unaltered. (2) Permission for use of this software is granted only if the user accepts full responsibility for any undesirable consequences; the authors accept NO LIABILITY for damages of any kind. */ #import "PreferenceController.h" #import "AppDefaults.h" #import "AppUtils.h" #import "AppEnvironment.h" static NSString* GetChosenApplication (NSString* title); @implementation PreferenceController - (id) init { self = [super initWithWindowNibName:@"Preferences"]; return self; } - (void) dealloc { [fStartupPath release]; [fOverridePath release]; } - (void) windowDidLoad { [self aboutToShowWindow]; CenterWindowInScreen ([self window]); } - (void) aboutToShowWindow { AppDefaults* appDefaults = [AppDefaults defaultAppDefaults]; [fDiffAppEditBox setStringValue:[appDefaults getDiffApp]]; [fEditAppEditBox setStringValue:[appDefaults getEditApp]]; [fMergeAppEditBox setStringValue:[appDefaults getMergeApp]]; [fNumChangesToFetchBox setStringValue:[appDefaults getNumChangesToFetch]]; fStartupPath = [[AppEnvironment startupPath] copy]; fOverridePath = [[AppEnvironment overridePath] copy]; [fPathField setStringValue:fOverridePath]; fIsOverridden = [AppEnvironment isPathOverridden]; if ( fIsOverridden ) { [fOverridePathButton setState:NSOnState]; [fPathField setStringValue:fOverridePath]; [fPathField setEnabled:YES]; } else { [fOverridePathButton setState:NSOffState]; [fPathField setStringValue:fStartupPath]; [fPathField setEnabled:NO]; } } - (IBAction) okayButton: (id) sender { AppDefaults* appDefaults = [AppDefaults defaultAppDefaults]; [appDefaults setDiffApp:[fDiffAppEditBox stringValue]]; [appDefaults setEditApp:[fEditAppEditBox stringValue]]; [appDefaults setMergeApp:[fMergeAppEditBox stringValue]]; [appDefaults setNumChangesToFetch:[fNumChangesToFetchBox stringValue]]; if ( [fOverridePathButton state] == NSOnState ) { [AppEnvironment setPathOverride:[fPathField stringValue] makeActive:fIsOverridden]; } else { [AppEnvironment setPathOverride:fOverridePath makeActive:fIsOverridden]; } [self close]; } - (IBAction) chooseDiffApp: (id) sender { NSString* newApp = GetChosenApplication(@"Choose Diff Application"); if ( newApp ) { [fDiffAppEditBox setStringValue:newApp]; } } - (IBAction) chooseEditApp: (id) sender { NSString* newApp = GetChosenApplication(@"Choose Editor Application"); if ( newApp ) { [fEditAppEditBox setStringValue:newApp]; } } - (IBAction) chooseMergeApp: (id) sender { NSString* newApp = GetChosenApplication(@"Choose Merge Application"); if ( newApp ) { [fMergeAppEditBox setStringValue:newApp]; } } - (IBAction) toggleOverride: (id) sender { int theState = [sender state]; if ( theState == NSOnState ) { fIsOverridden = YES; [fPathField setStringValue:fOverridePath]; [fPathField setEnabled:YES]; } else { fIsOverridden = NO; [fOverridePath release]; fOverridePath = [[fPathField stringValue] copy]; [fPathField setStringValue:fStartupPath]; [fPathField setEnabled:NO]; } } @end NSString* GetChosenApplication (NSString* title) { NSOpenPanel* openPanel = [NSOpenPanel openPanel]; [openPanel setAllowsMultipleSelection:NO]; [openPanel setCanChooseDirectories:NO]; [openPanel setCanChooseFiles:YES]; [openPanel setResolvesAliases:NO]; [openPanel setTreatsFilePackagesAsDirectories:NO]; [openPanel setPrompt:@"Choose"]; [openPanel setTitle:title]; if ( [openPanel runModal] == NSFileHandlingPanelOKButton ) { NSString* filePath = [openPanel filename]; NSArray* fileComponents = [filePath componentsSeparatedByString:@"/"]; return [fileComponents objectAtIndex:([fileComponents count] - 1)]; } return nil; }