/* 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 "AppDefaults.h" #import #import "MessageDefs.h" #import "AppUtils.h" #import "AppEnvironment.h" #import "PerforceConfigurationsList.h" #import "PerforceConfiguration.h" #import "PerforceAction.h" static NSString* kDiffAppKey = @"Diff App"; static NSString* kEditAppKey = @"Edit App"; static NSString* kMergeAppKey = @"Merge App"; static NSString* kShowEntireDepotKey = @"Show Entire Depot"; static NSString* kShowDeletedFilesKey = @"Show Deleted Files"; static NSString* kNumChangesKey = @"Num Changes To Fetch"; static NSString* kConfigurationsKey = @"Perforce Configurations"; static NSString* kCommandLineConfigName = @"Command Line Override"; @interface AppDefaults (PrivateMethods) - (void) updateCommandLineConfig; - (void) setConfigurations; @end @implementation AppDefaults + (void) initialize { NSString* envValue; NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary]; // extract the current environment variables and set those as the // standard defaults envValue = [AppEnvironment perforceDiff]; [defaultValues setObject:[NSString stringWithString:envValue] forKey:kDiffAppKey]; envValue = [AppEnvironment perforceEditor]; [defaultValues setObject:[NSString stringWithString:envValue] forKey:kEditAppKey]; envValue = [AppEnvironment perforceMerge]; [defaultValues setObject:[NSString stringWithString:envValue] forKey:kMergeAppKey]; [defaultValues setObject:[NSNumber numberWithBool:NO] forKey:kShowEntireDepotKey]; [defaultValues setObject:[NSNumber numberWithBool:NO] forKey:kShowDeletedFilesKey]; [defaultValues setObject:[NSString stringWithString:@"1500"] forKey:kNumChangesKey]; PerforceConfigurationsList* theList = [[[PerforceConfigurationsList alloc] init] autorelease]; PerforceConfiguration* defaultConfig = [[[PerforceConfiguration alloc] initWithName:@"Default" perforcePort:[AppEnvironment perforcePort] perforceHost:[AppEnvironment perforceHost] perforceUser:[AppEnvironment perforceUser] perforceClient:[AppEnvironment perforceClient] perforcePasswd:[AppEnvironment perforcePasswd] unicodeServer:[AppEnvironment unicodeServer]] autorelease]; [theList addConfiguration:defaultConfig]; [theList setActiveConfiguration:0]; NSData* listData = [NSArchiver archivedDataWithRootObject:theList]; [defaultValues setObject:listData forKey:kConfigurationsKey]; [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues]; } + (AppDefaults*) defaultAppDefaults { static AppDefaults* gAppDefaults = nil; if ( !gAppDefaults ) { gAppDefaults = [[AppDefaults alloc] init]; } return gAppDefaults; } - (id) init { NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; self = [super init]; fDiffApp = [[userDefaults objectForKey:kDiffAppKey] retain]; fEditApp = [[userDefaults objectForKey:kEditAppKey] retain]; fMergeApp = [[userDefaults objectForKey:kMergeAppKey] retain]; fShowEntireDepot = [userDefaults boolForKey:kShowEntireDepotKey]; fShowDeletedFiles = [userDefaults boolForKey:kShowDeletedFilesKey]; fNumChanges = [[userDefaults objectForKey:kNumChangesKey] retain]; NSData* configData = [userDefaults dataForKey:kConfigurationsKey]; fConfigurations = [[NSUnarchiver unarchiveObjectWithData:configData] retain]; // If somebody started us up from the command line, then create a command line perforce configuration [self updateCommandLineConfig]; return self; } - (void) dealloc { [fDiffApp release]; [fEditApp release]; [fMergeApp release]; [fNumChanges release]; [fConfigurations release]; [super dealloc]; } - (void) setEnvVars { // set these so that the p4api can use them PerforceConfiguration* config = [fConfigurations activeConfiguration]; [AppEnvironment setPerforcePort:[config perforcePort]]; [AppEnvironment setPerforceHost:[config perforceHost]]; [AppEnvironment setPerforceUser:[config perforceUser]]; [AppEnvironment setPerforceClient:[config perforceClient]]; [AppEnvironment setPerforcePasswd:[config perforcePasswd]]; [AppEnvironment setUnicodeServer:[config unicodeServer]]; [AppEnvironment setPerforceDiff:fDiffApp]; [AppEnvironment setPerforceEditor:fEditApp]; [AppEnvironment setPerforceMerge:fMergeApp]; } - (NSString*) getPerforcePort { PerforceConfiguration* config = [fConfigurations activeConfiguration]; return [config perforcePort]; } - (NSString*) getPerforceUser { PerforceConfiguration* config = [fConfigurations activeConfiguration]; return [config perforceUser]; } - (NSString*) getPerforceClient { PerforceConfiguration* config = [fConfigurations activeConfiguration]; return [config perforceClient]; } - (NSString*) getDiffApp { return fDiffApp; } - (NSString*) getEditApp { return fEditApp; } - (NSString*) getMergeApp { return fMergeApp; } - (BOOL) getShowEntireDepot { return fShowEntireDepot; } - (BOOL) getShowDeletedFiles { return fShowDeletedFiles; } - (NSString*) getNumChangesToFetch { return fNumChanges; } - (PerforceConfigurationsList*) getConfigurations { return fConfigurations; } // // // - (void) setPerforcePort: (NSString*) perforcePort { PerforceConfiguration* config = [fConfigurations activeConfiguration]; if ( ![perforcePort isEqualTo:[config perforcePort]] ) { [config setPerforcePort:perforcePort]; [AppEnvironment setPerforcePort:perforcePort]; [self setConfigurations]; } } - (void) setPerforceUser: (NSString*) perforceUser { PerforceConfiguration* config = [fConfigurations activeConfiguration]; if ( ![perforceUser isEqualTo:[config perforceUser]] ) { [config setPerforceUser:perforceUser]; [AppEnvironment setPerforceUser:perforceUser]; [self setConfigurations]; } } - (void) setPerforceClient: (NSString*) perforceClient { PerforceConfiguration* config = [fConfigurations activeConfiguration]; if ( ![perforceClient isEqualTo:[config perforceClient]] ) { [config setPerforceClient:perforceClient]; [AppEnvironment setPerforceClient:perforceClient]; [self setConfigurations]; } } - (void) setDiffApp: (NSString*) diffApp { if ( ![diffApp isEqualTo:fDiffApp] ) { [fDiffApp release]; fDiffApp = [diffApp retain]; [AppEnvironment setPerforceDiff:fDiffApp]; [[NSUserDefaults standardUserDefaults] setObject:fDiffApp forKey:kDiffAppKey]; } } - (void) setEditApp: (NSString*) editApp { if ( ![editApp isEqualTo:fEditApp] ) { [fEditApp release]; fEditApp = [editApp retain]; [AppEnvironment setPerforceEditor:fEditApp]; [[NSUserDefaults standardUserDefaults] setObject:fEditApp forKey:kEditAppKey]; } } - (void) setMergeApp: (NSString*) mergeApp { if ( ![mergeApp isEqualTo:fMergeApp] ) { [fMergeApp release]; fMergeApp = [mergeApp retain]; [AppEnvironment setPerforceMerge:fMergeApp]; [[NSUserDefaults standardUserDefaults] setObject:fMergeApp forKey:kMergeAppKey]; } } - (void) setShowEntireDepot: (BOOL) showEntireDepot { if ( showEntireDepot != fShowEntireDepot ) { fShowEntireDepot = showEntireDepot; [[NSUserDefaults standardUserDefaults] setBool:fShowEntireDepot forKey:kShowEntireDepotKey]; SendNotification (kDefaultsShowEntireDepotChanged); } } - (void) setShowDeletedFiles: (BOOL) showDeletedFiles { if ( showDeletedFiles != fShowDeletedFiles ) { fShowDeletedFiles = showDeletedFiles; [[NSUserDefaults standardUserDefaults] setBool:fShowDeletedFiles forKey:kShowDeletedFilesKey]; SendNotification (kDefaultsShowDeletedFilesChanged); } } - (void) setNumChangesToFetch: (NSString*) numChanges { if ( ![numChanges isEqualTo:fNumChanges] ) { [fNumChanges release]; fNumChanges = [numChanges retain]; [[NSUserDefaults standardUserDefaults] setObject:fNumChanges forKey:kNumChangesKey]; SendNotification (kDefaultsNumChangesToFetchChanged); } } - (void) setConfigurations: (PerforceConfigurationsList*) newConfigurations { [fConfigurations release]; fConfigurations = [newConfigurations copy]; [self setConfigurations]; } - (void) setActiveConfiguration: (NSString*) configName { int index = [fConfigurations indexOfConfigurationNamed:configName]; int activeIndex = [fConfigurations indexOfActiveConfiguration]; if ( (index > -1) && (index != activeIndex) ) { [fConfigurations setActiveConfiguration:index]; [self setConfigurations]; } } @end @implementation AppDefaults (PrivateMethods) - (void) updateCommandLineConfig { // Only do something if we have command line arguments NSArray *arguments = [[NSProcessInfo processInfo] arguments]; int numArguments = [arguments count]; int i; if ( numArguments > 1 ) { // Get all the values from the arguments NSString* perforcePort = nil; NSString* perforceHost = nil; NSString* perforceUser = nil; NSString* perforceClient = nil; NSString* perforcePasswd = nil; BOOL didChange = NO; for (i = 1; i < numArguments; i++) { NSString* arg = [arguments objectAtIndex:i]; int nexti = i + 1; if ( nexti < numArguments ) { if ([arg isEqualToString:@"-c"] ) { perforceClient = [arguments objectAtIndex:nexti]; didChange = YES; } else if ([arg isEqualToString:@"-u"] ) { perforceUser = [arguments objectAtIndex:nexti]; didChange = YES; } else if ([arg isEqualToString:@"-p"] ) { perforcePort = [arguments objectAtIndex:nexti]; didChange = YES; } else if ([arg isEqualToString:@"-P"] ) { perforcePasswd = [arguments objectAtIndex:nexti]; didChange = YES; } else if ([arg isEqualToString:@"-H"] ) { perforceHost = [arguments objectAtIndex:nexti]; didChange = YES; } } } if ( didChange ) { // If there is already a command line config in the list, grab it. Otherwise create one PerforceConfiguration* commandLineConfig = nil; int clIndex = [fConfigurations indexOfConfigurationNamed:kCommandLineConfigName]; if ( clIndex >= 0 ) { commandLineConfig = [fConfigurations configurationAtIndex:clIndex]; // Only set those values which were set via the command line arguments if ( perforcePort ) { [commandLineConfig setPerforcePort:perforcePort]; } if ( perforceHost ) { [commandLineConfig setPerforceHost:perforceHost]; } if ( perforceUser ) { [commandLineConfig setPerforceUser:perforceUser]; } if ( perforceClient ) { [commandLineConfig setPerforceClient:perforceClient]; } if ( perforcePasswd ) { [commandLineConfig setPerforcePasswd:perforcePasswd]; } [fConfigurations setActiveConfiguration:clIndex]; } else { // Let's make one commandLineConfig = [[[PerforceConfiguration alloc] initWithName:kCommandLineConfigName perforcePort:perforcePort perforceHost:perforceHost perforceUser:perforceUser perforceClient:perforceClient perforcePasswd:perforcePasswd unicodeServer:NO] autorelease]; [fConfigurations addConfiguration:commandLineConfig]; [fConfigurations setActiveConfiguration: ([fConfigurations count] - 1)]; } // Since we had arguments change the configurations [self setConfigurations]; } } } - (void) setConfigurations { [PerforceAction abortAllActions]; [self setEnvVars]; NSData* listData = [NSArchiver archivedDataWithRootObject:fConfigurations]; [[NSUserDefaults standardUserDefaults] setObject:listData forKey:kConfigurationsKey]; SendNotification (kDefaultsConfigurationsChanged); } @end