Revision: 6952
          http://skim-app.svn.sourceforge.net/skim-app/?rev=6952&view=rev
Author:   hofman
Date:     2010-12-09 12:01:36 +0000 (Thu, 09 Dec 2010)

Log Message:
-----------
use class factory method to create bookmarks for paths

Modified Paths:
--------------
    trunk/SKBookmark.h
    trunk/SKBookmark.m
    trunk/SKBookmarkController.m

Modified: trunk/SKBookmark.h
===================================================================
--- trunk/SKBookmark.h  2010-12-09 11:14:24 UTC (rev 6951)
+++ trunk/SKBookmark.h  2010-12-09 12:01:36 UTC (rev 6952)
@@ -63,6 +63,8 @@
 + (id)bookmarkSessionWithSetups:(NSArray *)aSetupDicts label:(NSString 
*)aLabel;
 + (id)bookmarkSeparator;
 
++ (NSArray *)bookmarksForPaths:(NSArray *)paths relativeToPath:(NSString 
*)basePath;
+
 - (id)initWithPath:(NSString *)aPath pageIndex:(NSUInteger)aPageIndex 
label:(NSString *)aLabel;
 - (id)initWithSetup:(NSDictionary *)aSetupDict label:(NSString *)aLabel;
 - (id)initFolderWithChildren:(NSArray *)aChildren label:(NSString *)aLabel;
@@ -87,8 +89,6 @@
 - (void)insertObject:(SKBookmark *)child inChildrenAtIndex:(NSUInteger)anIndex;
 - (void)removeObjectFromChildrenAtIndex:(NSUInteger)anIndex;
 
-- (void)insertBookmarksForPaths:(NSArray *)paths relativeToPath:(NSString 
*)basePath atIndex:(NSUInteger)anIndex;
-
 @property (nonatomic, readonly) FourCharCode scriptingBookmarkType;
 @property (nonatomic, readonly) NSURL *scriptingFile;
 @property (nonatomic, readonly) SKBookmark *scriptingParent;

Modified: trunk/SKBookmark.m
===================================================================
--- trunk/SKBookmark.m  2010-12-09 11:14:24 UTC (rev 6951)
+++ trunk/SKBookmark.m  2010-12-09 12:01:36 UTC (rev 6952)
@@ -125,6 +125,35 @@
     return [[[self alloc] initSeparator] autorelease];
 }
 
++ (NSArray *)bookmarksForPaths:(NSArray *)paths relativeToPath:(NSString 
*)basePath {
+    NSFileManager *fm = [NSFileManager defaultManager];
+    NSDocumentController *dc = [NSDocumentController sharedDocumentController];
+    NSMutableArray *array = [NSMutableArray array];
+    
+    for (NSString *file in paths) {
+        if ([[file lastPathComponent] hasPrefix:@"."] == NO) {
+            NSString *path = [basePath stringByAppendingPathComponent:file] ?: 
file;
+            NSString *fileType = [dc typeForContentsOfURL:[NSURL 
fileURLWithPath:path] error:NULL];
+            Class docClass;
+            SKBookmark *bookmark;
+            if ([fileType isEqualToString:SKFolderDocumentType]) {
+                NSArray *children = [SKBookmark bookmarksForPaths:[fm 
contentsOfDirectoryAtPath:path error:NULL] relativeToPath:path];
+                if ([children count] && (bookmark = [[SKBookmark alloc] 
initFolderWithChildren:children label:[fm displayNameAtPath:path]])) {
+                    [array addObject:bookmark];
+                    [bookmark release];
+                }
+            } else if (docClass = [dc documentClassForType:fileType]) {
+                if (bookmark = [[SKBookmark alloc] initWithPath:path 
pageIndex:([docClass isPDFDocument] ? 0 : NSNotFound) label:[fm 
displayNameAtPath:path]]) {
+                    [array addObject:bookmark];
+                    [bookmark release];
+                }
+            }
+        }
+    }
+    
+    return array;
+}
+
 - (id)initWithPath:(NSString *)aPath pageIndex:(NSUInteger)aPageIndex 
label:(NSString *)aLabel {
     [self doesNotRecognizeSelector:_cmd];
     return nil;
@@ -208,8 +237,6 @@
     }
 }
 
-- (void)insertBookmarksForPaths:(NSArray *)paths relativeToPath:(NSString 
*)basePath atIndex:(NSUInteger)anIndex {}
-
 - (FourCharCode)scriptingBookmarkType { return 0; }
 
 - (NSURL *)scriptingFile {
@@ -606,9 +633,10 @@
             {
                 if (aLabel == nil && aPath)
                     aLabel = [[NSFileManager defaultManager] 
displayNameAtPath:aPath];
-                bookmark = [[SKBookmark alloc] initFolderWithLabel:aLabel ?: 
@""];
+                NSArray *aChildren = nil;
                 if (aPath)
-                    [bookmark insertBookmarksForPaths:[[NSFileManager 
defaultManager] contentsOfDirectoryAtPath:aPath error:NULL] 
relativeToPath:aPath atIndex:0];
+                    aChildren = [SKBookmark bookmarksForPaths:[[NSFileManager 
defaultManager] contentsOfDirectoryAtPath:aPath error:NULL] 
relativeToPath:aPath];
+                bookmark = [[SKBookmark alloc] 
initFolderWithChildren:aChildren label:aLabel ?: @""];
                 break;
             }
             case SKScriptingBookmarkTypeSession:
@@ -630,34 +658,6 @@
     return [super newScriptingObjectOfClass:objectClass forValueForKey:key 
withContentsValue:contentsValue properties:properties];
 }
 
-- (void)insertBookmarksForPaths:(NSArray *)paths relativeToPath:(NSString 
*)basePath atIndex:(NSUInteger)anIndex {
-    NSFileManager *fm = [NSFileManager defaultManager];
-    NSDocumentController *dc = [NSDocumentController sharedDocumentController];
-    NSUInteger insertIndex = anIndex;
-    
-    for (NSString *file in paths) {
-        if ([[file lastPathComponent] hasPrefix:@"."] == NO) {
-            NSString *path = [basePath stringByAppendingPathComponent:file] ?: 
file;
-            NSString *fileType = [dc typeForContentsOfURL:[NSURL 
fileURLWithPath:path] error:NULL];
-            Class docClass;
-            SKBookmark *bookmark;
-            if ([fileType isEqualToString:SKFolderDocumentType]) {
-                if (bookmark = [[SKBookmark alloc] initFolderWithLabel:[fm 
displayNameAtPath:path]]) {
-                    [bookmark insertBookmarksForPaths:[fm 
contentsOfDirectoryAtPath:path error:NULL] relativeToPath:path atIndex:0];
-                    if ([bookmark countOfChildren])
-                        [self insertObject:bookmark 
inChildrenAtIndex:insertIndex++];
-                    [bookmark release];
-                }
-            } else if (docClass = [dc documentClassForType:fileType]) {
-                if (bookmark = [[SKBookmark alloc] initWithPath:path 
pageIndex:([docClass isPDFDocument] ? 0 : NSNotFound) label:[fm 
displayNameAtPath:path]]) {
-                    [self insertObject:bookmark 
inChildrenAtIndex:insertIndex++];
-                    [bookmark release];
-                }
-            }
-        }
-    }
-}
-
 @end
 
 #pragma mark -
@@ -736,8 +736,6 @@
 - (void)insertObject:(SKBookmark *)child inChildrenAtIndex:(NSUInteger)anIndex 
{}
 - (void)removeObjectFromChildrenAtIndex:(NSUInteger)anIndex {}
 
-- (void)insertBookmarksForPaths:(NSArray *)paths relativeToPath:(NSString 
*)basePath atIndex:(NSUInteger)anIndex {}
-
 - (NSArray *)entireContents { return nil; }
 
 - (id)newScriptingObjectOfClass:(Class)objectClass forValueForKey:(NSString 
*)key withContentsValue:(id)contentsValue properties:(NSDictionary *)properties 
{

Modified: trunk/SKBookmarkController.m
===================================================================
--- trunk/SKBookmarkController.m        2010-12-09 11:14:24 UTC (rev 6951)
+++ trunk/SKBookmarkController.m        2010-12-09 12:01:36 UTC (rev 6952)
@@ -647,16 +647,17 @@
         return YES;
     } else if ([type isEqualToString:NSFilenamesPboardType]) {
         NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
-        [self endEditing];
-        if (item == nil) item = bookmarkRoot;
-        NSUInteger oldCount = [item countOfChildren];
-        [item insertBookmarksForPaths:paths relativeToPath:nil 
atIndex:anIndex];
-        NSUInteger newCount = [item countOfChildren];
-        if (newCount > oldCount && (item == bookmarkRoot || [outlineView 
isItemExpanded:item])) {
-            NSRange range = NSMakeRange((item == bookmarkRoot ? 0 : 
[outlineView rowForItem:item] + 1) + anIndex, newCount - oldCount);
-            [outlineView selectRowIndexes:[NSIndexSet 
indexSetWithIndexesInRange:range] byExtendingSelection:NO];
+        NSArray *newBookmarks = [SKBookmark bookmarksForPaths:paths 
relativeToPath:nil];
+        if ([newBookmarks count] > 0) {
+            [self endEditing];
+            if (item == nil) item = bookmarkRoot;
+            NSIndexSet *indexes = [NSIndexSet 
indexSetWithIndexesInRange:NSMakeRange(anIndex, [newBookmarks count])];
+            [[item mutableArrayValueForKey:@"children"] 
insertObjects:newBookmarks atIndexes:indexes];
+            if (item == bookmarkRoot || [outlineView isItemExpanded:item])
+                [outlineView selectRowIndexes:indexes byExtendingSelection:NO];
+            return YES;
         }
-        return newCount > oldCount;
+        return NO;
     }
     return NO;
 }
@@ -732,22 +733,19 @@
 }
 
 - (void)outlineView:(NSOutlineView *)ov pasteFromPasteboard:(NSPasteboard 
*)pboard {
-    NSUInteger oldCount = 0, newCount = 0;
     if ([pboard availableTypeFromArray:[NSArray 
arrayWithObjects:NSFilenamesPboardType, nil]]) {
         NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
-        SKBookmark *item = nil;
-        NSUInteger anIndex = 0;
-        [self getInsertionFolder:&item childIndex:&anIndex];
-        oldCount = [item countOfChildren];
-        [item insertBookmarksForPaths:paths relativeToPath:nil 
atIndex:anIndex];
-        newCount = [item countOfChildren];
-        if (newCount > oldCount && (item == bookmarkRoot || [outlineView 
isItemExpanded:item])) {
-            NSRange range = NSMakeRange((item == bookmarkRoot ? 0 : 
[outlineView rowForItem:item] + 1) + anIndex, newCount - oldCount);
-            [outlineView selectRowIndexes:[NSIndexSet 
indexSetWithIndexesInRange:range] byExtendingSelection:NO];
-        }
-    }
-    if (newCount == oldCount)
-        NSBeep();
+        NSArray *newBookmarks = [SKBookmark bookmarksForPaths:paths 
relativeToPath:nil];
+        if ([newBookmarks count] > 0) {
+            SKBookmark *item = nil;
+            NSUInteger anIndex = 0;
+            [self getInsertionFolder:&item childIndex:&anIndex];
+            NSIndexSet *indexes = [NSIndexSet 
indexSetWithIndexesInRange:NSMakeRange(anIndex, [newBookmarks count])];
+            [[item mutableArrayValueForKey:@"children"] 
insertObjects:newBookmarks atIndexes:indexes];
+            if (item == bookmarkRoot || [outlineView isItemExpanded:item])
+                [outlineView selectRowIndexes:indexes byExtendingSelection:NO];
+        } else NSBeep();
+    } else NSBeep();
 }
 
 - (BOOL)outlineView:(NSOutlineView *)ov canPasteFromPasteboard:(NSPasteboard 
*)pboard {


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
This SF Dev2Dev email is sponsored by:

WikiLeaks The End of the Free Internet
http://p.sf.net/sfu/therealnews-com
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to