Fix crash when popover is dismissed while a view is presented modally.
[pithos-ios] / Classes / Folder.m
index fd3cad8..527a455 100755 (executable)
 
 @implementation Folder
 
-@synthesize name, parent, objects, folders, sharing, metadata, contentType, objectsAndFoldersCount;
+@synthesize folderObject, parent, objects, folders, objectsAndFoldersCount;
+@synthesize name, sharing, contentType, lastModifiedString, metadata;
 
 #pragma mark - Object lifecycle
 
-- (id)init {
+- (id)initWithObject:(StorageObject *)anObject {
     if ((self = [super init])) {
-        objects = [[NSMutableDictionary alloc] init];
-        folders = [[NSMutableDictionary alloc] init];
+        self.folderObject = anObject;
+        self.objects = [NSMutableDictionary dictionary];
+        self.folders = [NSMutableDictionary dictionary];
     }
     return self;
 }
 
+- (id)init {
+    StorageObject *anObject = [[[StorageObject alloc] init] autorelease];
+    return [self initWithObject:anObject];
+}
+
++ (id)folderWithObject:(StorageObject *)anObject {
+    return [[[self alloc] initWithObject:anObject] autorelease];
+}
+
 + (id)folder {
     return [[[self alloc] init] autorelease];
 }
 #pragma mark - Memory management
 
 -(void)dealloc {
-       [name release];
+    [folderObject release];
        [parent release];
        [folders release];
-    [sharing release];
        [objects release];
     [sortedContents release];
-    [contentType release];
        [super dealloc];
 }
 
 #pragma mark - Properties
 
+- (void)setName:(NSString *)aName {
+    self.folderObject.name = aName;
+}
+
+- (NSString *)name {
+    return self.folderObject.name;
+}
+
+- (void)setSharing:(NSString *)aSharing {
+    self.folderObject.sharing = aSharing;
+}
+
+- (NSString *)sharing {
+    return self.folderObject.sharing;
+}
+
+- (void)setContentType:(NSString *)aContentType {
+    self.folderObject.contentType = aContentType;
+}
+
+- (NSString *)contentType {
+    return self.folderObject.contentType;
+}
+
+- (void)setMetadata:(NSMutableDictionary *)aMetadata {
+    self.folderObject.metadata = aMetadata;
+}
+
+- (NSMutableDictionary *)metadata {
+    return self.folderObject.metadata;
+}
+
+- (void)setLastModifiedString:(NSString *)aLastModifiedString {
+    self.folderObject.lastModifiedString = aLastModifiedString;
+}
+
+- (NSString *)lastModifiedString {
+    return self.folderObject.lastModifiedString;
+}
+
 - (void)setObjects:(NSMutableDictionary *)objs {
     if (![objects isEqualToDictionary:objs]) {
         [objects release];
                 NSMutableDictionary *folderFiles = [folderedFiles objectForKey:folderName];
                 [folderFiles setObject:object forKey:object.name]; 
             } else if ([PithosUtilities isContentTypeDirectory:object.contentType]) {
-                Folder *folder = [Folder folder];
+                Folder *folder = [Folder folderWithObject:object];
                 if (objectHasTrailingSlash)
-                    folder.name = [object.name stringByAppendingString:@"/"];
-                else
-                    folder.name = object.name;
+                    folder.name = [folder.name stringByAppendingString:@"/"];
                 folder.parent = self;
-                folder.metadata = object.metadata;    
-                folder.sharing = object.sharing;
-                folder.contentType = object.contentType;
                 [self.folders setObject:folder forKey:folder.name];
             } else {
                 // put the files in this folder 
         
         // take the foldered files and recursively build the rest of the folder structure
         for (NSString *folderName in [folderedFiles allKeys]) {
-            NSMutableDictionary *folderFiles = [folderedFiles objectForKey:folderName];
-            Folder *folder = [Folder folder];
-            folder.name = folderName;
-            folder.parent = self;
-            folder.objects = folderFiles;
+            Folder *folder;
             StorageObject *object = [objs objectForKey:folderName];
             if (object && [PithosUtilities isContentTypeDirectory:object.contentType]) {
-                folder.metadata = object.metadata;
-                folder.sharing = object.sharing;
-                folder.contentType = object.contentType;
+                folder = [Folder folderWithObject:object];
             } else {
+                folder = [Folder folder];
+                folder.name = folderName;
                 folder.metadata = [NSMutableDictionary dictionary];
             }
+            folder.parent = self;
+            folder.objects = [folderedFiles objectForKey:folderName];
             [self.folders setObject:folder forKey:folder.name];
         }
         
 
 #pragma mark - Actions
 
+- (NSArray *)sortedContentsUsingFilter:(NSString *)filter andScope:(NSString *)scope {
+    if (!filter || !filter.length)
+        return self.sortedContents;
+    return [self.sortedContents objectsAtIndexes:
+            [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.sortedContents.count)] indexesPassingTest:^(NSUInteger idx, BOOL *stop) {
+        id item = [self.sortedContents objectAtIndex:idx];
+        if ([scope isEqualToString:@"name"] || [scope isEqualToString:@"all"]) {
+            NSString *itemName = [item name];
+            if (itemName) {
+                NSUInteger nameLocation = [itemName rangeOfString:filter
+                                                          options:NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch].location;
+                if (nameLocation != NSNotFound)
+                    return YES;
+            }
+        }
+        if ([scope isEqualToString:@"date"] || [scope isEqualToString:@"all"]) {
+            NSString *itemDate = [item lastModifiedString];
+            if (itemDate) {
+                NSUInteger dateLocation = [itemDate rangeOfString:filter
+                                                          options:NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch].location;
+                if (dateLocation != NSNotFound)
+                    return YES;
+            }
+        }
+        if ([scope isEqualToString:@"type"] || [scope isEqualToString:@"all"]) {
+            NSString *itemType = [item contentType];
+            if (itemType) {
+                NSUInteger typeLocation = [itemType rangeOfString:filter
+                                                          options:NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch].location;
+                if (typeLocation != NSNotFound)
+                    return YES;
+            }
+        }
+        // Consider searching additional metadata when the scope is all
+        return NO;
+    }]];
+}
+
 - (NSArray *)sortedContentsWithNameThatStartsWith:(NSString *)filter {
     if (!filter || !filter.length)
         return self.sortedContents;