// // P4DepotItem.m // Perforce // // Created by Adam Czubernat on 29.05.2013. // Copyright (c) 2013 Perforce Software, Inc. All rights reserved. // #import "P4DepotItem.h" @interface P4DepotItem () - (void)loadDepots; - (void)loadMappings; - (id)initFile:(NSDictionary *)dictionary parentItem:(P4DepotItem *)parent; - (id)initDirectory:(NSDictionary *)dictionary parentItem:(P4DepotItem *)parent; - (id)initDepot:(NSDictionary *)dictionary parentItem:(P4DepotItem *)parent; @end @implementation P4DepotItem #pragma mark - P4Item Override - (id)init { if (self = [super init]) { flags.directory = YES; name = @"Server"; self.remotePath = @"//"; self.localPath = [P4Workspace sharedInstance].root; [[P4Workspace sharedInstance] addObserver:self]; } return self; } - (NSString *)path { return self.remotePath; } - (BOOL)isEditable { return NO; } - (id)defaultAction { if (flags.directory) return nil; if (self.localPath.length) return [P4ItemAction actionForItem:self name:@"Open " selector:@selector(openWithCheckout)]; return [P4ItemAction actionForItem:self name:@"Open file from server" selector:@selector(openFromDepot)]; } - (NSArray *)actions { P4ItemAction *action; NSMutableArray *actions = [NSMutableArray array]; BOOL dir = NO; BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:self.localPath isDirectory:&dir]; if (self.localPath.length && !flags.directory) { action = [P4ItemAction actionForItem:self name:@"Open" selector:@selector(openWithCheckout)]; action.disabled = !exists || dir; [actions addObject:action]; action = [P4ItemAction actionForItem:self name:@"Open read only" selector:@selector(open)]; action.disabled = !exists || dir; [actions addObject:action]; } if (self.isDirectory && parent) { NSString *mapping = nil; SEL selector; if (self.isIgnored) { selector = @selector(mapToWorkspace); mapping = @"Stop ignoring folder"; } else if (self.isMapped) { selector = @selector(unmapFromWorkspace); mapping = @"Unsync from computer"; } else if (self.isTracked) { selector = @selector(unmapFromWorkspace); mapping = @"Ignore folder"; } else { selector = @selector(mapToWorkspace); mapping = @"Sync with computer"; } [actions insertObject:[P4ItemAction actionForItem:self name:mapping selector:selector] atIndex:0]; } if (!self.isDirectory){ [actions addObject:[P4ItemAction actionForItem:self name:@"Open file from server" selector:@selector(openFromDepot)]]; } if (self.localPath.length) { action = [P4ItemAction actionForItem:self name:@"Show in Finder" selector:@selector(showInFinder)]; action.disabled = !exists || dir != flags.directory; [actions addObject:action]; } if (!flags.directory) { [actions addObject:[P4ItemAction actionForItem:self name:@"Show Versions" selector:@selector(showVersions)]]; } if (parent) [actions addObject:[P4ItemAction actionForItem:self name:@"Copy link" selector:@selector(copyShareLink)]]; return actions; } - (void)loadPath:(NSString *)preloadPath { NSAssert([preloadPath hasSuffix:@"/"], @"Loading path without trailing slash"); NSAssert([preloadPath hasPrefix:@"//"], @"Loading depot path without // prefix"); flags.loading = YES; children = [NSMutableArray array]; NSString *selfPath = [self.remotePath lowercaseString]; NSString *relative = [preloadPath relativePath:self.remotePath]; relative = [[relative stringByRemovingSuffix:@"/"] lowercaseString]; NSString *subpath = selfPath; NSMutableArray *subpaths = [NSMutableArray arrayWithObject:subpath]; for (NSString *component in [relative pathComponents]) { subpath = [subpath stringByAppendingFormat:@"%@/", component]; [subpaths addObject:subpath]; } [[P4Workspace sharedInstance] listDepotFiles:subpaths response:^(P4Operation *operation, NSArray *response) { [operation ignoreErrorsWithCode:P4ErrorMustReferToClient]; if (operation.errors && ([self failWithError:operation.error], 1)) return; NSMutableDictionary *parents = [NSMutableDictionary dictionaryWithObject:self forKey:selfPath]; [subpaths removeObject:selfPath]; for (NSDictionary *record in response) { NSString *path = [[[record objectForKey:@"dir"] stringByAppendingString:@"/"] lowercaseString]; if ([subpaths containsObject:path]) { P4DepotItem *parentItem = [parents objectForKey:[path stringByDeletingPath]]; P4DepotItem *item = [[P4DepotItem alloc] initDirectory:record parentItem:parentItem]; item->children = [NSMutableArray array]; [(NSMutableArray *)parentItem->children addObject:item]; [parents setObject:item forKey:path]; [subpaths removeObject:path]; } } if (subpaths.count) { [self failWithError: [NSError errorWithFormat:@"Couldn't find directory at %@", subpaths.firstObject]]; children = nil; return; } for (NSDictionary *record in response) { NSString *path = [[record objectForKey:@"depotFile"] ?: [[record objectForKey:@"dir"] stringByAppendingString:@"/"] lowercaseString]; if (![parents objectForKey:path]) { NSString *parentPath = [path stringByDeletingPath]; P4DepotItem *itemParent = [parents objectForKey:parentPath]; P4DepotItem *item = nil; if ([record objectForKey:@"dir"]) item = [[P4DepotItem alloc] initDirectory:record parentItem:itemParent]; else item = [[P4DepotItem alloc] initFile:record parentItem:itemParent]; [(NSMutableArray *)itemParent->children addObject:item]; } } for (P4DepotItem *parentItem in parents.allValues) { [parentItem sortChildren]; [parentItem loadMappings]; } [self finishLoading]; }]; } #pragma mark - Private - (void)loadDepots { [[P4Workspace sharedInstance] listDepots:^(P4Operation *operation, NSArray *response) { if (operation.errors) { [self failWithError:operation.error]; return; } NSMutableArray *depots = [NSMutableArray array]; for (NSDictionary *depotDict in response) { P4DepotItem *depot = [[P4DepotItem alloc] initDepot:depotDict parentItem:self]; [depots addObject:depot]; } // Sort files alphanumerically children = depots; [self sortChildren]; [self loadMappings]; [self finishLoading]; }]; } - (void)loadMappings { P4Workspace *workspace = [P4Workspace sharedInstance]; for (P4Item *child in children) { if (!child->flags.directory) return; BOOL have, mapped; NSInteger viewIdx; NSString *clientFile = [workspace mappingForPath:child.path viewIndex:&viewIdx mapped:&mapped have:&have]; child->flags.tracked = NO; child->flags.hasMapped = NO; child->flags.mapped = NO; child->flags.ignored = NO; if (mapped && clientFile) child->flags.mapped = child->flags.tracked = YES; else if (clientFile) child->flags.tracked = YES; else if (have) child->flags.hasMapped = YES; else if (mapped && !clientFile) child->flags.ignored = YES; child.localPath = clientFile; }; } - (id)initFile:(NSDictionary *)dictionary parentItem:(P4DepotItem *)parentItem { if (self = [super init]) { parent = parentItem; metadata = dictionary; self.remotePath = [metadata objectForKey:@"depotFile"]; // Make path by appending name to parent name = self.remotePath.lastPathComponent; self.localPath = ([metadata objectForKey:@"clientFile"] ?: [parentItem.localPath stringByAppendingPath:name]); // 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]; } return self; } - (id)initDirectory:(NSDictionary *)dictionary parentItem:(P4DepotItem *)parentItem { if (self = [super init]) { parent = parentItem; metadata = dictionary; flags.directory = YES; self.remotePath = [metadata objectForKey:@"dir"]; self.remotePath = [self.remotePath stringByAppendingString:@"/"]; // Make path by appending name to parent name = self.remotePath.lastPathComponent; self.localPath = [parentItem.localPath stringByAppendingPath:name]; // Check if tracked BOOL directory; flags.tracked = [[NSFileManager defaultManager] fileExistsAtPath:self.localPath isDirectory:&directory] && directory; } return self; } - (id)initDepot:(NSDictionary *)dictionary parentItem:(P4DepotItem *)parentItem { if (self = [super init]) { parent = parentItem; metadata = dictionary; flags.directory = YES; // Make path by appending name to parent name = [metadata objectForKey:@"name"]; self.localPath = [parentItem.localPath stringByAppendingPath:name]; self.remotePath = [parentItem.remotePath stringByAppendingFormat:@"%@/", name]; } return self; } @end