/*
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 "PerforceUser.h"
#import "PerforceActionUser.h"
#import "AppController.h"
#import "ReadOnlyEditorController.h"
#import "AppDefaults.h"
@interface PerforceUser(Private)
- (NSComparisonResult) comparegetUserNameAscending: (PerforceUser*) rhs;
- (NSComparisonResult) comparegetUserNameDescending: (PerforceUser*) rhs;
- (NSComparisonResult) comparegetUserEmailAscending: (PerforceUser*) rhs;
- (NSComparisonResult) comparegetUserEmailDescending: (PerforceUser*) rhs;
- (NSComparisonResult) comparegetUserFullNameAscending: (PerforceUser*) rhs;
- (NSComparisonResult) comparegetUserFullNameDescending: (PerforceUser*) rhs;
- (NSComparisonResult) comparegetUserAccessAscending: (PerforceUser*) rhs;
- (NSComparisonResult) comparegetUserAccessDescending: (PerforceUser*) rhs;
@end
@implementation PerforceUser
+(PerforceUser *) userWithString: (NSString *) string
{
return [[[PerforceUser alloc] initWithString: string] autorelease];
}
- (id) initWithString: (NSString*) string
{
if (self = [super init])
{
NSScanner * scanner = [NSScanner scannerWithString: string];
[scanner scanUpToString: @" " intoString: &fName] &&
[scanner scanString: @"<" intoString: nil] &&
[scanner scanUpToString: @">" intoString: &fEmail] &&
[scanner scanString: @"> (" intoString: nil] &&
[scanner scanUpToString: @")" intoString: &fFullName] &&
[scanner scanString: @") accessed" intoString: nil] &&
[scanner scanUpToString: @"\n" intoString: &fAccess];
[fName retain];
[fEmail retain];
[fFullName retain];
[fAccess retain];
}
return self;
}
- (void) dealloc
{
[fName release];
[fEmail release];
[fFullName release];
[fAccess release];
[super dealloc];
}
- (NSString*) getUserName
{
return fName;
}
- (NSString*) getUserEmail
{
return fEmail;
}
- (NSString*) getUserFullName
{
return fFullName;
}
- (NSString*) getUserAccess
{
return fAccess;
}
- (NSComparisonResult) comparegetUserNameAscending: (PerforceUser*) rhs
{
return [fName caseInsensitiveCompare: rhs->fName];
}
- (NSComparisonResult) comparegetUserNameDescending: (PerforceUser*) rhs
{
return [rhs->fName caseInsensitiveCompare: fName];
}
- (NSComparisonResult) comparegetUserEmailAscending: (PerforceUser*) rhs
{
return [fEmail caseInsensitiveCompare: rhs->fEmail];
}
- (NSComparisonResult) comparegetUserEmailDescending: (PerforceUser*) rhs
{
return [rhs->fEmail caseInsensitiveCompare: fEmail];
}
- (NSComparisonResult) comparegetUserFullNameAscending: (PerforceUser*) rhs
{
return [fFullName caseInsensitiveCompare: rhs->fFullName];
}
- (NSComparisonResult) comparegetUserFullNameDescending: (PerforceUser*) rhs
{
return [rhs->fFullName caseInsensitiveCompare: fFullName];
}
- (NSComparisonResult) comparegetUserAccessAscending: (PerforceUser*) rhs
{
return [fAccess compare: rhs->fAccess];
}
- (NSComparisonResult) comparegetUserAccessDescending: (PerforceUser*) rhs
{
return [rhs->fAccess compare: fAccess];
}
// The enablement
- (BOOL) canEdit
{
AppDefaults* appDef = [AppDefaults defaultAppDefaults];
return [fName isEqualToString:[appDef getPerforceUser]];
}
- (BOOL) canDescribe
{
return YES;
}
- (BOOL) canDelete
{
AppDefaults* appDef = [AppDefaults defaultAppDefaults];
return [fName isEqualToString:[appDef getPerforceUser]];
}
- (BOOL) canSwitchTo
{
AppDefaults* appDef = [AppDefaults defaultAppDefaults];
return ![fName isEqualToString:[appDef getPerforceUser]];
}
// The actions
- (void) doEdit
{
[PerforceActionUserGet defaultRunFor:[NSApp delegate] selector:@selector(resultFromPerforceUserOut:) identifier:fName];
}
- (void) doDescribe
{
[PerforceActionUserGet defaultRunFor:self selector:@selector(resultFromDescribe:) identifier:fName];
}
- (void) doDelete
{
int isOK = NSRunAlertPanel (@"Delete",
@"Are you sure you want to delete user %s?",
@"OK",
@"Cancel",
nil,
[fName cString]);
if ( isOK )
{
[PerforceActionUserDelete defaultRunFor:nil selector:nil identifier:fName];
}
}
- (void) doSwitchTo
{
AppDefaults* appDef = [AppDefaults defaultAppDefaults];
[appDef setPerforceUser:fName];
}
- (void) resultFromDescribe:(PerforceActionUserGet*)action
{
if ( [action wasSuccess] )
{
NSMutableString* titleString = [NSMutableString stringWithString:@"Description of User "];
[titleString appendString:[action getIdentifier]];
[ReadOnlyEditorController showWindowWith:[action getOutput] title:titleString];
}
}
@end