/* 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 "PerforceChanges.h" #import "PerforceChangeList.h" #import "PerforceAction.h" #import "IconController.h" #import "IconDefs.h" #import "AppUtils.h" #import "AppDefaults.h" #import "PerforceChangeFile.h" #import "PerforceActionChangesPending.h" #import "PerforceActionOpened.h" #import "MessageDefs.h" // local functions static void FilterOutOtherChanges (NSArray* changesArray, NSMutableArray* resultArray); static void FilterOutClientChanges (NSMutableArray* resultArray); static void IntersectWithOpened (NSArray* changeFileArray, NSMutableArray* resultArray); static void CopyToChildren (NSMutableArray* fromArray, NSMutableArray* toArray); @implementation PerforceChanges - (void) resultFromPerforceOpened: (PerforceActionOpened*) action { if ( [action wasSuccess] ) { NSArray* openedArray = [action getOpened]; if ( fClientOnly ) { NSMutableArray* newChildren = [[[NSMutableArray alloc] init] autorelease]; FilterOutOtherChanges (fChangesPendingArray, newChildren); IntersectWithOpened (openedArray, newChildren); CopyToChildren (newChildren, fChildren); } else { [fChildren removeAllObjects]; [fChildren addObjectsFromArray:fChangesPendingArray]; IntersectWithOpened (openedArray, fChildren); FilterOutClientChanges (fChildren); } } [fChangesPendingArray release]; fChangesPendingArray = nil; SendNotificationWithObject (kPendingChangesChildChanged, self); } - (void) resultFromPerforcePendingChanges: (PerforceActionChangesPending*) action { if ( [action wasSuccess] ) { fChangesPendingArray = [[action getChangesPending] retain]; [PerforceActionOpened defaultRunFor:self selector:@selector(resultFromPerforceOpened:) clientOnly:fClientOnly]; } } - (void) buildChildren { if ( !fChildren ) { fChildren = [[NSMutableArray alloc] init]; } [PerforceActionChangesPending defaultRunFor:self selector:@selector(resultFromPerforcePendingChanges:)]; fMustGatherChildren = NO; } - (void) dealloc { NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter]; [nCenter removeObserver:self]; [fChildren release]; [fChangesName release]; [super dealloc]; } - (id) initWithName: (NSString*) changesName clientChanges: (BOOL) clientOnly { if ( self = [super init] ) { fChangesName = [changesName copy]; fClientOnly = clientOnly; fMustGatherChildren = YES; if ( fClientOnly ) { fIconID = kClientPendingChangesIcon; NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter]; [nCenter addObserver:self selector:@selector(handleClientContentChanged:) name:kActionSyncCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleClientContentChanged:) name:kActionEditCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleClientContentChanged:) name:kActionDeleteCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleClientContentChanged:) name:kActionRevertCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleClientContentChanged:) name:kActionAddCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleClientContentChanged:) name:kActionSubmitCommandComplete object:nil]; } else { fIconID = kOtherPendingChangesIcon; } } return self; } - (int) getNumberOfChildren { if ( fMustGatherChildren ) { [self buildChildren]; } return [fChildren count]; } - (BOOL) hasChildren { return YES; } - (id) getChildAtIndex: (int) index { if ( fMustGatherChildren ) { [self buildChildren]; } return [fChildren objectAtIndex:index]; } - (id) getCellText { return fChangesName; } - (id) getCellImage { return [[IconController defaultIconController] getIcon:fIconID]; } - (void) handleClientContentChanged:(NSNotification*)aNotification { [self refreshClientData]; } - (void) refreshClientData { if ( !fClientOnly ) return; //[fChildren release]; //fChildren = nil; fMustGatherChildren = YES; SendNotificationWithObject (kPendingChangesChildChanged, self); } // ///// // // The enablement - (BOOL) canEdit { return NO; } - (BOOL) canDescribe { return NO; } - (BOOL) canDelete { return NO; } - (BOOL) canSubmit { return NO; } - (BOOL) canRevert { return NO; } - (BOOL) canDiff { return NO; } // The actions - (void) doEdit { } - (void) doDescribe { } - (void) doDelete { } - (void) doSubmit { } - (void) doRevert { } - (void) doDiff { } - (void) appendRevertPath: (NSMutableArray*) revertPathList { // Do Nothing } @end // // // /////////////////////////////////////// // // // void FilterOutOtherChanges (NSArray* changesArray, NSMutableArray* resultArray) { int numChanges = [changesArray count]; int n; for ( n = 0; n < numChanges; n++ ) { PerforceChangeList* pfcl = [changesArray objectAtIndex:n]; if ( [pfcl isClientChangelist] ) { [resultArray addObject:pfcl]; } } } void FilterOutClientChanges (NSMutableArray* resultArray) { int numChanges = [resultArray count]; int n; for ( n = numChanges - 1; n >= 0; n-- ) { PerforceChangeList* pfcl = [resultArray objectAtIndex:n]; if ( [pfcl isClientChangelist] ) { [resultArray removeObjectAtIndex:n]; } } } void IntersectWithOpened (NSArray* changeFileArray, NSMutableArray* resultArray) { int numItems = [changeFileArray count]; if ( numItems <= 0 ) return; // bail NSString* changeAndUser = [[changeFileArray objectAtIndex:0] getChangeAndUser]; int n = 0; int numChangeLists = [resultArray count]; int currentChange = 0; NSComparisonResult compareResult = NSOrderedDescending; PerforceChangeList* newChangeList = nil; while ( n < numItems ) { NSRange copyRange; NSArray* subArray; PerforceChangeFile* changeFile = nil; int startn = n; while ( (n < numItems) && ([changeAndUser compare:[[changeFileArray objectAtIndex:n] getChangeAndUser]] == NSOrderedSame) ) { n++; } changeFile = [changeFileArray objectAtIndex:startn]; newChangeList = [[[PerforceChangeList alloc] initWithNumber:[changeFile getChangeName] withUserInfo:[changeFile getUserInfo] withChangeDesc:nil isClient:[changeFile isClientChange]] autorelease]; copyRange.location = startn; copyRange.length = n - startn; subArray = [changeFileArray subarrayWithRange:copyRange]; while ( (compareResult == NSOrderedDescending) && (currentChange < numChangeLists) ) { compareResult = [newChangeList compare:[resultArray objectAtIndex:currentChange]]; if ( compareResult == NSOrderedDescending ) { currentChange++; } } // did we fall off the end of the list? if so just append a new change list if ( currentChange >= numChangeLists ) { [newChangeList setChildren:subArray]; [resultArray addObject:newChangeList]; } else { if ( compareResult == NSOrderedSame ) { [[resultArray objectAtIndex:currentChange] setChildren:subArray]; } else { [newChangeList setChildren:subArray]; [resultArray insertObject:newChangeList atIndex:currentChange]; currentChange++; numChangeLists++; } } if ( n < numItems ) { changeAndUser = [[changeFileArray objectAtIndex:n] getChangeAndUser]; } compareResult = NSOrderedDescending; } } void CopyToChildren (NSMutableArray* fromArray, NSMutableArray* toArray) { int numFrom = 0; int numTo = [toArray count]; int n, k; for ( n = (numTo - 1); n >= 0; n-- ) { PerforceChangeList* currentChangeList = [toArray objectAtIndex:n]; BOOL bFound = NO; numFrom = [fromArray count]; for ( k = (numFrom - 1); (k >= 0) && !bFound; k-- ) { PerforceChangeList* newChangeList = [fromArray objectAtIndex:k]; if ( [newChangeList compare:currentChangeList] == NSOrderedSame ) { [currentChangeList copyFrom:newChangeList]; [fromArray removeObjectAtIndex:k]; bFound = YES; } } if ( !bFound ) { [toArray removeObjectAtIndex:n]; } } [toArray addObjectsFromArray:fromArray]; [toArray sortUsingSelector:@selector(compare:)]; }