// // 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
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 15071 | alan_petersen |
Populate -o //guest/perforce_software/piper/... //guest/alan_petersen/piper/.... |
||
//guest/perforce_software/piper/mac/main/Perforce/Classes/Items/P4UnifiedItem.m | |||||
#1 | 12961 | alan_petersen |
Piper 2.0 Mega Update New Features/Functionality - Added help menu redirecting to URL. - Added readonly property for creating new workspaces. - Added html hyperlinks for Copy link functionality. - Added functionality for managing Finder Favorite items in sidebar. - Redesigned the way mapping is stored in Piper. - First version of syncing finder sidebar items with workspace mapping. - Small sorting improvements. - Creating Projects directory inside users home folder. - Adding Projects folder to finder sidebar item. - Creating and removing symbolic links accordingly to mapped folders. - Preventing duplicate names in symbolic links. - Refreshing symbolic links on mapping change inside application. - Storing workspace and server details in p4 configuration for other applications to use. - Added contextual menu items for Finder integration. - Added services menu for Adobe Illustrator integration. - Keyboard shortcuts for Illustrator integration. - Code refactoring and fixes for mapping issues. - Added Finder functionality to edit all files in folder. - Added user friendly message when editing a file using Finder outside the workspace. - Implemented hidden automatic login when opening application using Finder integration. - Logging to file in ~/Library/Logs - Unified workspace and all files views to show both local and depot files and folders. - Removed my workspace view references and logic. - Editing unmapped files on server. - First version of adding file to unmapped folders. - Showing opened by and edit actions in column details for all depot files. - Improved mappings functionality. - Enabled same feature options for mapped and unmapped folders and files. - Redesigned from scratch mapping and unmapping procedures for adding and removing files. - Implemented cleaning workspace using new mapping functionality. Removed debug overlay coloring. - Automated workspace creation - Improvements in editing files already mapped to workspace. - Implemented deleting remote files. - Implemented first version of move operation for remote files. - Removing last workspace information when disconnecting from workspace using app menu. - Implemented editing and submitting using symbolic links in project folder. New finder menu service for symbolic links Show in Piper which acts like share link functionality. - New icons for files and folders not tracked in the filesystem. - Improvements in showing file using share link. - Switched to new way of retrieving files in order to show user changes. - Redesigned and implemented new functionality for chaining operations with mapping. - Improvements and redesign of Edit/add actions to use new chaining logic . Fixed issue with file edit. - Improvements in window showing when using services. - Simplified file loading so the local files appears only when remote are also loaded. - Improved deleting of untracked files to avoid mapping and marking for delete. - Enabling simple copy paste and moving of remote and local files. - Added abort for exception handling in order to force crashing application on critical failures - Added custom exception handling for catching runtime errors to log and crash instead of continuing in unstable state. - Changed file copying to use mark for add . - Simplified and fixed responding file representations to mapping changes. Bug Fixes - Fixed crash when synchronizing. - Fixed sync issue when downloading directory without file size information. - Fixed issue with unread list crashing when file is not existing on disk. - Fixed incorrect sync progress calculation. - Removed relative path issues. - Fixed many of case-sensitivity problems. - Fixed deprecated methods and related issues in OS X 10.10. - Fixed folder rename not updating in column view. Revised and fixed many potential problems from implicit casting. - Fixed missing sync button on fast sync completion. - Refreshing mapping on synchronization. Fixed symbolic links not appearing until app is restarted. - Fixed latest crashing of autosync. - Fixed loading indicator issues. - Fixed and redesigned submit dialog to work correctly with Submit All Files option in Finder. - Fixed multiple error messages on network outage. Redesigned showing errors in main window. - Fixed opening random locations when using Finder integration. - Fixed issue when panel was detached from parent window. - Fixed bug when creating new workspace wouldn't store default settings. - Fixed memory issues with network operations. - Fixes in relogging mappings and file listing. - Improvements in editing unmapped files. - Fixed crash when adding file outside workspace. - Fixed breadcrumbs control issue. - Fixed issue with double parent folders when opening unmapped files. - Fixed crashes on sync after mapping new files. - Fixed issue with editing file using Finder -- Merging code and additional fixes in add button functionality. - Fixed unsync not working - Fixed submit panel issue not selecting files with different name case. - Fixed missing revert and sync to workspace actions in some cases. - Fixed issue with Submit and Edit finder actions. Improvements in stability of finder integration. - Fixed issue with unsubmitted folders breaking status of files inside. - Fixed issue with added files not showing correct icon and status. - Fixed bug with file edit resulting in a new directory named exactly like a file. - Fixed issue with reloading of subpath resulting in untracked folders. - Fixed mapping issue when result was always view mapping not relative. - Fixed submit panel showing more than once. - Fixed illustrator services not working. - Fixed userdefaults preferences problem with workspace name being null. - Fixed userdefaults keypath problem of dot-containing workspace names. - Forcing recreating of browser to possibly prevent pre-10.10 errors with automatic workspace selection. - Fixed adding file to depot not presenting correct icon. - Fixed issues with reverting a file that was marked for add. - Presenting error when trying to submit untracked files. - Fixed issue when submit files service crashed when using unmapped files. - Fixed file representation disappearing when removing file. - Fixed issue with symlinks resolving working on 10.10 only. Issue related to workspace selection not showing. - Fixed error panel method calls unavailable in Mac OS versions before 10.10. Issue related to hanging error panels. - Fixed removing a local file resulting in action progress freezing. - Fixed open file not working after edit. - Fixing crash when mapping changed. Issue related to moving local file to unmapped folder and other similar cases. |