// // P4UnifiedItem.m // Perforce // // Created by Adam Czubernat on 20/01/15. // Copyright (c) 2015 Perforce Software, Inc. All rights reserved. // #import "P4UnifiedItem.h" @interface P4UnifiedItem () - (id)initWithURL:(NSURL *)url parentItem:(P4Item *)parent; - (id)initFile:(NSDictionary *)dictionary parentItem:(P4Item *)parent; - (id)initDirectory:(NSDictionary *)dictionary parentItem:(P4Item *)parent; - (void)loadMetadata:(NSDictionary *)dictionary; - (void)loadMappings; @end @implementation P4UnifiedItem - (NSString *)path { NSAssert(remotePath, @"P4Item doesn't have remote path"); return remotePath; } - (void)loadPath:(NSString *)preloadPath { NSAssert([preloadPath hasPrefix:@"//"], @"Loading local path"); NSAssert([preloadPath hasSuffix:@"/"], @"Loading path without trailing slash"); children = [NSMutableArray array]; flags.loading = YES; NSString *rootPath = [[P4Workspace sharedInstance] root]; NSMutableArray *subpaths = [NSMutableArray array]; for (NSString *subpath = preloadPath; subpath.length >= remotePath.length;) { [subpaths insertObject:subpath.lowercaseString atIndex:0]; subpath = [subpath stringByDeletingPath]; } // Remote files and metadata [[P4Workspace sharedInstance] listDepotFiles:subpaths response:^(P4Operation *operation, NSArray *response) { [operation ignoreErrorsWithCode:P4ErrorMustReferToClient]; if (operation.errors) return [self failWithError:operation.error]; NSMutableArray *localItems = @[ self ].mutableCopy; NSMutableDictionary *parents = @{ remotePath.lowercaseString : self }.mutableCopy; // Local files for (NSString *subpath in subpaths) { NSString *local = [rootPath stringByAppendingString:[subpath substringFromIndex:2]]; NSArray *urls = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[NSURL fileURLWithPath:local isDirectory:YES] includingPropertiesForKeys: @[ NSURLIsDirectoryKey, NSURLNameKey, NSURLLabelColorKey, ] options:NSDirectoryEnumerationSkipsHiddenFiles error:NULL]; if (!urls) continue; // Directory not in filesystem // Find next parent in children from previous iterations P4Item *subpathParent = [localItems firstObjectPassingTest:^BOOL(P4Item *obj, NSUInteger idx) { return [obj.localPath isEqualCaseInsensitive:local]; }]; [parents setObject:subpathParent forKey:subpath]; // Create children NSArray *subpathChildren = [urls arrayUsingBlock:^id(NSURL *url, NSUInteger idx) { return [[P4UnifiedItem alloc] initWithURL:url parentItem:subpathParent]; }]; subpathParent->children = subpathChildren.mutableCopy; [localItems addObjectsFromArray:subpathChildren]; } // Parents NSIndexSet *childrenIndexes = [response indexesOfObjectsPassingTest:^BOOL(NSDictionary *record, NSUInteger idx, BOOL *stop) { NSString *path = [[record objectForKey:@"dir"] directoryPath].lowercaseString; if (![subpaths containsObject:path]) return YES; // Child not parent P4UnifiedItem *subpathItem = [parents objectForKey:path]; if (!subpathItem) { P4UnifiedItem *subpathParent = [parents objectForKey:[path stringByDeletingPath]]; subpathItem = [[P4UnifiedItem alloc] initDirectory:record parentItem:subpathParent]; subpathItem->children = [NSMutableArray array]; [parents setObject:subpathItem forKey:path]; // Add to parent's children NSAssert(subpathParent->children, @"Parent has no children"); [(NSMutableArray *)subpathParent->children addObject:subpathItem]; } [subpathItem loadMetadata:record]; return NO; // Parent }]; // Children [response enumerateObjectsAtIndexes:childrenIndexes options:0 usingBlock:^(NSDictionary *record, NSUInteger idx, BOOL *stop) { NSString *path = ([record objectForKey:@"depotFile"] ?: [[record objectForKey:@"dir"] directoryPath]); NSString *local = ([record objectForKey:@"clientFile"] ?: [rootPath stringByAppendingString:[path substringFromIndex:2]]); // Find local counterpart P4UnifiedItem *item = [localItems firstObjectPassingTest:^BOOL(P4Item *obj, NSUInteger idx) { return [obj.localPath isEqualCaseInsensitive:local]; }]; if (!item) { // Create remote-only item NSString *parentPath = [path stringByDeletingPath].lowercaseString; P4Item *itemParent = [parents objectForKey:parentPath]; if ([record objectForKey:@"dir"]) item = [[P4UnifiedItem alloc] initDirectory:record parentItem:itemParent]; else item = [[P4UnifiedItem alloc] initFile:record parentItem:itemParent]; NSAssert(itemParent->children, @"Parent has no children"); [(NSMutableArray *)itemParent->children addObject:item]; } [item loadMetadata:record]; }]; for (P4UnifiedItem *parentItem in parents.allValues) { [parentItem sortChildren]; [parentItem->children makeObjectsPerformSelector:@selector(loadMappings)]; } // Mark unread and shelved items NSArray *unread = [[P4Workspace sharedInstance] unreadForPath:localPath]; for (NSString *path in unread) { [self traverseItemsUsingPath:path block:^(P4Item *item) { item->flags.unread = YES; }]; } NSArray *shelved = [[P4Workspace sharedInstance] shelvedForPath:remotePath]; for (NSString *path in shelved) { P4Item *item = [self itemAtPath:path]; if (item) item->flags.shelved = YES; } [self finishLoading]; }]; } #pragma mark - Private - (id)initWithURL:(NSURL *)url parentItem:(P4Item *)parentItem { if (self = [super initWithParent:parentItem]) { // Get directory info NSNumber *dir = nil; [url getResourceValue:&dir forKey:NSURLIsDirectoryKey error:NULL]; flags.directory = dir.boolValue; NSString *filename; [url getResourceValue:&filename forKey:NSURLNameKey error:NULL]; name = filename; NSColor *color; [url getResourceValue:&color forKey:NSURLLabelColorKey error:NULL]; overlay = color; localPath = url.path; remotePath = [parentItem.remotePath stringByAppendingPath:name]; if (flags.directory) { localPath = [localPath directoryPath]; remotePath = [remotePath directoryPath]; } } NSAssert(localPath.length && remotePath.length, @"Creating item without complete paths %@", self); return self; } - (id)initFile:(NSDictionary *)dictionary parentItem:(P4Item *)parentItem { if (self = [super initWithParent:parentItem]) { metadata = dictionary; remotePath = [metadata objectForKey:@"depotFile"]; name = remotePath.lastPathComponent; localPath = [metadata objectForKey:@"clientFile"]; if (!localPath || [localPath hasPrefix:@"//"]) localPath = [parentItem.localPath stringByAppendingPath:name]; // Make path by appending name to parent } NSAssert(localPath.length && remotePath.length, @"Creating item without complete paths %@", self); return self; } - (id)initDirectory:(NSDictionary *)dictionary parentItem:(P4Item *)parentItem { if (self = [super initWithParent:parentItem]) { metadata = dictionary; flags.directory = YES; remotePath = [[metadata objectForKey:@"dir"] directoryPath]; name = remotePath.lastPathComponent; localPath = [[parentItem.localPath stringByAppendingPath:name] directoryPath]; // Make path by appending name to parent } NSAssert(localPath.length && remotePath.length, @"Creating item without complete paths %@", self); return self; } - (void)loadMetadata:(NSDictionary *)dictionary { NSAssert(dictionary.count, @"P4Item loading empty metadata"); metadata = dictionary; if (!flags.directory) { // Set metadata status = [metadata objectForKey:@"action"]; NSString *user = [metadata objectForKey:@"otherOpen0"]; NSDictionary *info = [[P4Workspace sharedInstance] userInfo:user]; statusOwner = [info objectForKey:@"Email"] ?: user; flags.tracked = [metadata objectForKey:@"isMapped"] != nil; [self refreshTags]; } } - (void)loadMappings { P4Workspace *workspace = [P4Workspace sharedInstance]; // Mappings BOOL have, mapped; NSInteger viewIdx; NSString *mappingPath = [workspace mappingForPath:remotePath viewIndex:&viewIdx mapped:&mapped have:&have]; flags.mapped = mapped && mappingPath; flags.ignored = mapped && !mappingPath; flags.tracked = metadata && mappingPath; flags.hasMapped = have; } #pragma mark - P4Workspace Events - (void)file:(NSString *)filePath actionChanged:(NSDictionary *)info { P4UnifiedItem *item = [self itemAtPath:filePath]; P4UnifiedItem *itemParent = item.parent ?: [self itemAtPath:filePath.stringByDeletingPath]; NSString *action = [info objectForKey:@"action"]; NSString *fromFile = [info objectForKey:@"fromFile"]; if (fromFile && [action isEqualToString:@"move/add"]) { P4UnifiedItem *movedItem = [self itemAtPath:fromFile]; if (movedItem) movedItem->status = @"move/delete"; } else if (fromFile && [action isEqualToString:@"add"]) { P4UnifiedItem *movedItem = [self itemAtPath:fromFile]; P4Item *movedItemParent = movedItem.parent; if (movedItem && movedItemParent) { NSMutableArray *itemChildren = [movedItemParent->children mutableCopy]; [itemChildren removeObject:movedItem]; movedItemParent->children = itemChildren; [movedItemParent finishLoading]; [movedItem invalidate]; } } if (!itemParent || !itemParent->children) // Not loaded yet return; if (!item) { // Update children by adding new item item = [[P4UnifiedItem alloc] initFile:info parentItem:itemParent]; itemParent->children = [itemParent->children arrayByAddingObject:item]; [itemParent sortChildren]; [itemParent finishLoading]; } NSMutableDictionary *itemMetadata = [NSMutableDictionary dictionary]; [itemMetadata addEntriesFromDictionary:item->metadata]; [itemMetadata addEntriesFromDictionary:info]; [item loadMetadata:itemMetadata]; [item loadMappings]; [item finishLoading]; } - (void)file:(NSString *)filePath revertedAction:(NSDictionary *)info { #warning need to test all cases // NSString *oldAction = [info objectForKey:@"oldAction"]; P4Item *item = [self itemAtPath:filePath]; if (!item) return; NSString *action = [info objectForKey:@"action"]; NSNumber *revision = [info objectForKey:@"rev"]; BOOL reverted = ([action isEqualToString:@"reverted"] || [action isEqualToString:@"cleared"]); item->status = nil; item->flags.tracked = reverted || revision; // Reverted file marked for add if ([action isEqualToString:@"abandoned"]) item->metadata = nil; NSString *pendingTags = [item->metadata objectForKey:@"openattr-tags"]; if (pendingTags) { NSMutableDictionary *dict = [item->metadata mutableCopy]; [dict removeObjectForKey:@"openattr-tags"]; if (!reverted) [dict setObject:pendingTags forKey:@"attr-tags"]; item->metadata = dict; [item refreshTags]; } [item finishLoading]; } - (void)file:(NSString *)filePath updated:(NSDictionary *)info { BOOL remote = [filePath hasPrefix:@"//"]; NSString *parentPath = remote ? [remotePath substringFromIndex:1] : localPath; if ([filePath hasSuffix:@"/"]) filePath = [filePath substringToIndex:filePath.length-1]; if ([filePath isEqualCaseInsensitive:parentPath]) return; NSString *relative = [filePath substringFromIndex:parentPath.length]; NSArray *components = [relative pathComponents]; #warning need to review // Traversing P4Item *item = self; for (NSString *component in components) { if (!item->children.count) // Not loaded return; for (P4Item *child in item->children) { if ([child->name isEqualCaseInsensitive:component]) { item = child; if (item.isDirectory && !item.isUnread) { if (!item->flags.tracked) { // Directory wasn't tracked NSString *relativePath = [filePath relativePath:item.localPath]; NSString *depotPath = [info objectForKey:@"depotFile"]; depotPath = [depotPath stringByRemovingSuffix:relativePath]; item->remotePath = depotPath; item->flags.tracked = depotPath.length > 0; } // Mark as unread item->flags.unread = YES; [item finishLoading]; } break; } } } NSString *itemPath = remote ? item->remotePath : item->localPath; if ([itemPath isEqualCaseInsensitive:filePath]) { if (!item->metadata) { item->remotePath = [info objectForKey:@"depotFile"]; item->metadata = info; [item refreshTags]; } item->flags.tracked = YES; item->flags.unread = YES; [item finishLoading]; } } - (void)file:(NSString *)filePath mappingChanged:(NSDictionary *)info { // Propagate mapping change through parents [self traverseItemsUsingPath:filePath block:^(P4Item *parentItem) { [(P4UnifiedItem *)parentItem loadMappings]; [parentItem finishLoading]; }]; P4UnifiedItem *item = [self itemAtPath:filePath]; if (!item) return; // Propagate mapping change through children NSMutableArray *queue = [item->children mutableCopy]; while (queue.count) { P4UnifiedItem *child = [queue lastObject]; [queue removeLastObject]; if (child->flags.mapped || child->flags.ignored) continue; // Has own mapping if (child->children.count) [queue addObjectsFromArray:child->children]; [child loadMappings]; [child finishLoading]; } } //- (void)file:(NSString *)path shelved:(NSDictionary *)info { } #pragma mark - Filesystem Events - (void)fileCreated:(NSString *)filePath { P4Item *item = [self itemAtPath:filePath]; if (item) return [self fileModified:filePath]; P4Item *itemParent = [self itemAtPath:filePath.stringByDeletingPath]; if (!itemParent || !itemParent->children) return; // Not loaded yet // Update children by adding new item P4Item *newItem = [[P4UnifiedItem alloc] initWithURL:[NSURL fileURLWithPath:filePath] parentItem:itemParent]; itemParent->children = [itemParent->children arrayByAddingObject:newItem]; [itemParent sortChildren]; [itemParent finishLoading]; } - (void)fileRemoved:(NSString *)filePath { P4Item *item = [self itemAtPath:filePath]; P4Item *itemParent = item.parent; if (!itemParent || !itemParent->children || !item) // Not loaded yet return; [item markAsRead]; // Removing local file if (!item.metadata) { NSMutableArray *itemChildren = [itemParent->children mutableCopy]; [itemChildren removeObject:item]; itemParent->children = itemChildren; [itemParent finishLoading]; [item invalidate]; } } - (void)fileMoved:(NSString *)oldPath toPath:(NSString *)newPath { P4Item *oldItem = [self itemAtPath:oldPath]; P4Item *newItem = [self itemAtPath:newPath]; [oldItem markAsRead]; // Moving local file if (oldItem && !oldItem.metadata) { P4Item *oldItemParent = oldItem.parent ?: [self itemAtPath:oldPath.stringByDeletingPath]; if (oldItemParent && oldItemParent->children) { NSMutableArray *itemChildren = [oldItemParent->children mutableCopy]; [itemChildren removeObject:oldItem]; oldItemParent->children = itemChildren; [oldItemParent finishLoading]; [oldItem invalidate]; } } if (!newItem) { P4Item *newItemParent = newItem.parent ?: [self itemAtPath:newPath.stringByDeletingPath]; if (newItemParent && newItemParent->children) { newItem = [[P4UnifiedItem alloc] initWithURL:[NSURL fileURLWithPath:newPath] parentItem:newItemParent]; newItemParent->children = [newItemParent->children arrayByAddingObject:newItem]; [newItemParent sortChildren]; [newItemParent finishLoading]; } } } - (void)fileRenamed:(NSString *)oldPath toPath:(NSString *)newPath { P4Item *oldItem = [self itemAtPath:oldPath]; P4Item *newItem = [self itemAtPath:newPath]; P4Item *itemParent = oldItem.parent ?: [self itemAtPath:newPath.stringByDeletingPath]; if (!itemParent || !itemParent->children) return; // Not loaded yet if (oldItem.metadata) { // Moving mapped file if (!newItem) { newItem = [[P4UnifiedItem alloc] initWithURL:[NSURL fileURLWithPath:newPath] parentItem:itemParent]; itemParent->children = [itemParent->children arrayByAddingObject:newItem]; [itemParent sortChildren]; [itemParent finishLoading]; } } else if (!newItem) { // Renaming local file oldItem->name = newPath.lastPathComponent; oldItem->localPath = [oldItem->localPath stringByRenamingPath:oldItem->name]; oldItem->remotePath = [oldItem->remotePath stringByRenamingPath:oldItem->name]; // Traverse and rename children NSMutableArray *queue = [NSMutableArray array]; if (oldItem->children.count) [queue addObjectsFromArray:oldItem->children]; while (queue.count) { P4Item *child = [queue lastObject]; [queue removeLastObject]; child->localPath = [child.parent->localPath stringByAppendingPath:child->name]; child->remotePath = [child.parent->remotePath stringByAppendingPath:child->name]; if (child->flags.directory) { child->localPath = [child->localPath directoryPath]; child->remotePath = [child->remotePath directoryPath]; } if (child->children.count) [queue addObjectsFromArray:child->children]; } } [oldItem markAsRead]; [oldItem finishLoading]; [newItem finishLoading]; } - (void)fileModified:(NSString *)path { P4Item *item = [self itemAtPath:path]; if (!item) return; // Update overlay NSURL *url = [NSURL fileURLWithPath:path]; NSColor *color; [url getResourceValue:&color forKey:NSURLLabelColorKey error:NULL]; item->overlay = color; [item finishLoading]; } @end