Revision: 11385
          http://bibdesk.svn.sourceforge.net/bibdesk/?rev=11385&view=rev
Author:   hofman
Date:     2007-10-25 09:19:18 -0700 (Thu, 25 Oct 2007)

Log Message:
-----------
Get the base url for alias file objects from a delegate, which is a bibitem, 
rather than passing it for every accessor.

Modified Paths:
--------------
    branches/TRY_ARM_FILE_INTERFACE/bibdesk/BDSKFile.h
    branches/TRY_ARM_FILE_INTERFACE/bibdesk/BDSKFile.m
    branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibEditor.m
    branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.h
    branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.m

Modified: branches/TRY_ARM_FILE_INTERFACE/bibdesk/BDSKFile.h
===================================================================
--- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BDSKFile.h  2007-10-25 13:09:30 UTC 
(rev 11384)
+++ branches/TRY_ARM_FILE_INTERFACE/bibdesk/BDSKFile.h  2007-10-25 16:19:18 UTC 
(rev 11385)
@@ -64,22 +64,19 @@
     NSString *relativePath;
 }
 
-- (id)initWithBase64String:(NSString *)base64String;
-- (id)initWithPath:(NSString *)aPath relativeToPath:(NSString *)basePath;
-- (id)initWithURL:(NSURL *)aURL relativeToURL:(NSURL *)baseURL;
+- (id)initWithBase64String:(NSString *)base64String delegate:(id)aDelegate;
+- (id)initWithPath:(NSString *)aPath delegate:(id)aDelegate;
+- (id)initWithURL:(NSURL *)aURL delegate:(id)aDelegate;
 
-- (const FSRef *)fsRefRelativeToURL:(NSURL *)baseURL;
 - (const FSRef *)fsRef;
-
-- (NSURL *)fileURLRelativeToURL:(NSURL *)baseURL;
 - (NSURL *)fileURL;
-
-- (NSString *)pathRelativeToPath:(NSString *)basePath;
 - (NSString *)path;
 
-- (NSString *)base64StringRelativeToPath:(NSString *)basePath 
convertedRelativeToPath:(NSString *)newBasePath;
+- (NSString *)base64StringRelativeToPath:(NSString *)newBasePath;
 
 - (NSString *)relativePath;
 - (void)setRelativePath:(NSString *)newRelativePath;
 
+- (void)update;
+
 @end

Modified: branches/TRY_ARM_FILE_INTERFACE/bibdesk/BDSKFile.m
===================================================================
--- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BDSKFile.m  2007-10-25 13:09:30 UTC 
(rev 11384)
+++ branches/TRY_ARM_FILE_INTERFACE/bibdesk/BDSKFile.m  2007-10-25 16:19:18 UTC 
(rev 11385)
@@ -391,39 +391,43 @@
 @implementation BDSKAliasFile
 
 // guaranteed to be called with a non-nil alias
-- (id)initWithAlias:(BDAlias *)anAlias relativePath:(NSString *)relPath;
+- (id)initWithAlias:(BDAlias *)anAlias relativePath:(NSString *)relPath 
delegate:(id)aDelegate;
 {
+    NSParameterAssert(nil == aDelegate || [aDelegate 
repondsToSelector:@selector(baseURLForAliasFile:));
     if (self = [super init]) {
         fileRef = NULL; // this is updated lazily, as we don't know the base 
path at this point
         alias = [anAlias retain];
         relativePath = nil;
+        delegate = aDelegate;
     }
     return self;    
 }
 
-- (id)initWithAliasData:(NSData *)data relativePath:(NSString *)relPath;
+- (id)initWithAliasData:(NSData *)data relativePath:(NSString *)relPath 
delegate:(id)aDelegate;
 {
     NSParameterAssert(nil != data);
     BDAlias *anAlias = [[BDAlias alloc] initWithData:data];
-    self = [self initWithAlias:anAlias relativePath:relPath];
+    self = [self initWithAlias:anAlias relativePath:relPath 
delegate:aDelegate];
     [anAlias release];
     return self;
 }
 
-- (id)initWithBase64String:(NSString *)base64String;
+- (id)initWithBase64String:(NSString *)base64String delegate:(id)aDelegate;
 {
     NSParameterAssert(nil != base64String);
     NSData *data = [[NSData alloc] initWithBase64String:base64String];
     NSDictionary *dictionary = [NSKeyedUnarchiver 
unarchiveObjectWithData:data];
     [data release];
-    return [self initWithAliasData:[dictionary objectForKey:@"aliasData"] 
relativePath:[dictionary objectForKey:@"relativePath"]];
+    return [self initWithAliasData:[dictionary objectForKey:@"aliasData"] 
relativePath:[dictionary objectForKey:@"relativePath"] delegate:aDelegate];
 }
 
-- (id)initWithPath:(NSString *)aPath relativeToPath:(NSString *)basePath;
+- (id)initWithPath:(NSString *)aPath delegate:(id)aDelegate;
 {
     NSParameterAssert(nil != aPath);
+    NSParameterAssert(nil == aDelegate || [aDelegate 
repondsToSelector:@selector(baseURLForAliasFile:));
     BDAlias *anAlias = nil;
-    NSURL *baseURL;
+    NSURL *baseURL = [aDelegate baseURLForAliasFile:self];
+    NSString *basePath = [baseURL path];
     NSString *relPath = nil;
     // BDAlias has a different interpretation of aPath, which is inconsistent 
with the way it handles FSRef
     if (basePath) {
@@ -433,8 +437,8 @@
         anAlias = [[BDAlias alloc] initWithPath:aPath];
     }
     if (anAlias) {
-        if ((self = [self initWithAlias:anAlias relativePath:relPath])) {
-            if (basePath && (baseURL = [NSURL fileURLWithPath:basePath]))
+        if ((self = [self initWithAlias:anAlias relativePath:relPath 
delegate:aDelegate])) {
+            if (baseURL)
                 // this initalizes the FSRef and update the alias
                 [self fsRefRelativeToURL:baseURL];
         }
@@ -445,15 +449,15 @@
     return self;
 }
 
-- (id)initWithURL:(NSURL *)aURL relativeToURL:(NSURL *)baseURL;
+- (id)initWithURL:(NSURL *)aURL delegate:(id)aDelegate;
 {
-    return [self initWithPath:[aURL path] relativeToPath:[baseURL path]];
+    return [self initWithPath:[aURL path] delegate:aDelegate];
 }
 
 - (id)initWithCoder:(NSCoder *)coder
 {
     OBASSERT_NOT_REACHED("BDSKAliasFile needs a base path for encoding");
-    return [self initWithAliasData:[coder decodeObject] relativePath:[coder 
decodeObject]];
+    return [self initWithAliasData:[coder decodeObject] relativePath:[coder 
decodeObject] delegate:nil];
 }
 
 - (void)encodeWithCoder:(NSCoder *)coder
@@ -479,8 +483,31 @@
 
 // Should we implement -isEqual: and -hash?
 
-- (const FSRef *)fsRefRelativeToURL:(NSURL *)baseURL;
+- (void)update {
+    NSURL *baseURL = [delegate baseURLForAliasFile:self];
+    FSRef baseRef;
+    if (fileRef == NULL) {
+        // this does the updating if possible
+        [self fsRef];
+    } else if (baseURL && CFURLGetFSRef((CFURLRef)baseURL, &baseRef)) {
+        Boolean didUpdate;
+        if (alias)
+            FSUpdateAlias(&baseRef, fileRef, [alias alias], &didUpdate);
+        else
+            alias = [[BDAlias alloc] initWithFSRef:(FSRef *)fileRef 
relativeToFSRef:&baseRef];
+        if (baseURL) {
+            CFURLRef tmpURL = CFURLCreateFromFSRef(CFAllocatorGetDefault(), 
fileRef);
+            if (tmpURL) {
+                [self setRelativePath:[[baseURL path] 
relativePathToFilename:[(NSURL *)tmpURL path]]];
+                CFRelease(tmpURL);
+            }
+        }
+    }
+}
+
+- (const FSRef *)fsRef;
 {
+    NSURL *baseURL = [delegate baseURLForAliasFile:self];
     FSRef baseRef;
     Boolean hasBaseRef = baseURL && CFURLGetFSRef((CFURLRef)baseURL, &baseRef);
     Boolean shouldUpdate = false;
@@ -534,52 +561,32 @@
     return fileRef;
 }
 
-- (const FSRef *)fsRef;
+- (NSURL *)fileURL;
 {
-    return [self fsRefRelativeToURL:nil];
-}
-
-- (NSURL *)fileURLRelativeToURL:(NSURL *)baseURL;
-{
     BOOL hadFileRef = fileRef != NULL;
-    const FSRef *aRef = [self fsRefRelativeToURL:baseURL];
+    const FSRef *aRef = [self fsRef];
     NSURL *aURL = aRef == NULL ? nil : 
[(id)CFURLCreateFromFSRef(CFAllocatorGetDefault(), aRef) autorelease];
     if (aURL == nil && hadFileRef) {
         NSZoneFree([self zone], (void *)fileRef);
         fileRef = NULL;
-        aRef = [self fsRefRelativeToURL:baseURL];
+        aRef = [self fsRef];
         if (aRef != NULL)
             aURL = [(id)CFURLCreateFromFSRef(CFAllocatorGetDefault(), aRef) 
autorelease];
     }
     return aURL;
 }
 
-- (NSURL *)fileURL;
-{
-    return [self fileURLRelativeToURL:nil];
-}
-
-- (NSString *)fileName;
-{
-    return [[self path] lastPathComponent];
-}
-
 - (NSString *)path;
 {
-    return [self pathRelativeToPath:nil];
+    return [[self fileURL] path];
 }
 
-- (NSString *)pathRelativeToPath:(NSString *)basePath;
+- (NSData *)aliasDataRelativeToPath:(NSString *)newBasePath;
 {
-    return [[self fileURLRelativeToURL:basePath ? [NSURL 
fileURLWithPath:basePath] : nil] path];
-}
-
-- (NSData *)aliasDataRelativeToPath:(NSString *)basePath 
convertedRelativeToPath:(NSString *)newBasePath;
-{
     // make sure the fileRef is valid
-    [self fileURLRelativeToURL:basePath ? [NSURL fileURLWithPath:basePath] : 
nil];
+    [self fileURL];
     
-    FSRef *fsRef = (FSRef *)[self fsRefRelativeToURL:basePath ? [NSURL 
fileURLWithPath:basePath] : nil];
+    FSRef *fsRef = (FSRef *)[self fsRef];
     FSRef baseRef;
     NSURL *baseURL;
     BDAlias *anAlias = nil;
@@ -597,10 +604,10 @@
     return [anAlias aliasData];
 }
 
-- (NSString *)base64StringRelativeToPath:(NSString *)basePath 
convertedRelativeToPath:(NSString *)newBasePath;
+- (NSString *)base64StringRelativeToPath:(NSString *)newBasePath;
 {
-    NSData *data = [self aliasDataRelativeToPath:basePath 
convertedRelativeToPath:newBasePath];
-    NSString *path = [self pathRelativeToPath:basePath];
+    NSData *data = [self aliasDataRelativeToPath:newBasePath];
+    NSString *path = [self path];
     path = path && newBasePath ? [newBasePath relativePathToFilename:path] : 
relativePath;
     NSDictionary *dictionary = [NSDictionary 
dictionaryWithObjectsAndKeys:data, @"aliasData", path, @"relativePath", nil];
     return [[NSKeyedArchiver archivedDataWithRootObject:dictionary] 
base64String];
@@ -617,4 +624,13 @@
     }
 }
 
+- (id)delegate {
+    return delegate;
+}
+
+- (void)setDelegate:(id)newDelegate {
+    NSParameterAssert(nil == aDelegate || [aDelegate 
repondsToSelector:@selector(baseURLForAliasFile:));
+    delegate = newDelegate;
+}
+
 @end

Modified: branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibEditor.m
===================================================================
--- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibEditor.m 2007-10-25 13:09:30 UTC 
(rev 11384)
+++ branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibEditor.m 2007-10-25 16:19:18 UTC 
(rev 11385)
@@ -119,9 +119,10 @@
 - (NSURL *)fileView:(FileView *)aFileView URLAtIndex:(NSUInteger)idx;
 {
     BDSKAliasFile *file = [publication fileAtIndex:idx];
-    NSURL *aURL = [file fileURLRelativeToURL:[publication baseURL]];
-    if (aURL == nil && [file relativePath])
-        aURL = [NSURL fileURLWithPath:[file relativePath]];
+    NSURL *aURL = [file fileURL];
+    NSString *relativePath;
+    if (aURL == nil && (relativePath = [file relativePath]))
+        aURL = [NSURL fileURLWithPath:relativePath];
     return aURL;
 }
 
@@ -138,7 +139,7 @@
     NSURL *aURL;
     NSUInteger idx = [aSet firstIndex];
     while ((aURL = [enumerator nextObject]) != nil && NSNotFound != idx) {
-        aFile = [[BDSKAliasFile alloc] initWithURL:aURL 
relativeToURL:[publication baseURL]];
+        aFile = [[BDSKAliasFile alloc] initWithURL:aURL delegate:publication];
         if (aFile) {
             [publication removeObjectFromFilesAtIndex:idx];
             [publication insertObject:aFile inFilesAtIndex:idx];
@@ -166,7 +167,7 @@
     NSURL *aURL;
     NSUInteger idx = [aSet firstIndex], offset = 0;
     while ((aURL = [enumerator nextObject]) != nil && NSNotFound != idx) {
-        aFile = [[BDSKAliasFile alloc] initWithURL:aURL 
relativeToURL:[publication baseURL]];
+        aFile = [[BDSKAliasFile alloc] initWithURL:aURL delegate:publication];
         if (aFile) {
             [publication insertObject:aFile inFilesAtIndex:idx - offset];
             [aFile release];

Modified: branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.h
===================================================================
--- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.h   2007-10-25 13:09:30 UTC 
(rev 11384)
+++ branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.h   2007-10-25 16:19:18 UTC 
(rev 11385)
@@ -83,8 +83,6 @@
     NSMutableArray *sortedURLs;
 }
 
-- (NSString *)basePath;
-- (NSURL *)baseURL;
 - (NSUInteger)countOfFiles;
 - (BDSKAliasFile *)fileAtIndex:(NSUInteger)idx;
 - (void)insertObject:(BDSKAliasFile *)aFile inFilesAtIndex:(NSUInteger)idx;

Modified: branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.m
===================================================================
--- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.m   2007-10-25 13:09:30 UTC 
(rev 11384)
+++ branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.m   2007-10-25 16:19:18 UTC 
(rev 11385)
@@ -1613,7 +1613,7 @@
         string = [NSMutableString string];
         for (i = 0; i < iMax; i++) {
             NSString *key = [NSString stringWithFormat:@"Bdsk-File-%d", i];
-            NSString *value = [[files objectAtIndex:i] 
base64StringRelativeToPath:[self basePath] convertedRelativeToPath:basePath];
+            NSString *value = [[files objectAtIndex:i] 
base64StringRelativeToPath:basePath];
             OBPRECONDITION([value rangeOfCharacterFromSet:[NSCharacterSet 
curlyBraceCharacterSet]].length == 0);
             [string appendFormat:@",\n\t%@ = [EMAIL PROTECTED]", key, value];
         }
@@ -1685,7 +1685,7 @@
         }
     }
     if (!drop) {
-        value = [self filesAsBibTeXFragmentRelativeToPath:[self basePath]];
+        value = [self filesAsBibTeXFragmentRelativeToPath:[self 
baseURLForAliasFile:nil]];
         if (value) [s appendString:value];
     }
     [knownKeys release];
@@ -2425,15 +2425,11 @@
 #pragma mark -
 #pragma mark URL handling
 
-- (NSString *)basePath {
-    return [[[[self owner] fileURL] path] stringByDeletingLastPathComponent];
+- (NSString *)baseURLForAliasFile:(BDSKAliasFile *)file {
+    NSString *basePath = [[[[self owner] fileURL] path] 
stringByDeletingLastPathComponent];
+    return basePath ? [NSURL fileURLWithPath:basePath] : nil;
 }
 
-- (NSURL *)baseURL {
-    NSString *basePath = [self basePath];
-    return basePath ? [NSURL fileURLWithPath:[self basePath]] : nil;
-}
-
 static NSComparisonResult sortURLsByType(NSURL *first, NSURL *second, void 
*unused)
 {
     BOOL firstIsFile = [first isFileURL];
@@ -2452,7 +2448,7 @@
     NSURL *value = [self localFileURLForField:(id)key];
     if (value) {
         // !!! file URLs are always absolute but the init method here always 
returns nil, which probably means that we should do the first initialization 
with relative paths instead of expending them first
-        BDSKAliasFile *aFile = [[BDSKAliasFile alloc] initWithURL:value 
relativeToURL:nil];
+        BDSKAliasFile *aFile = [[BDSKAliasFile alloc] initWithURL:value 
delegate:self];
         if (aFile) {
             [self->files addObject:aFile];
             [aFile release];
@@ -2472,14 +2468,14 @@
     NSMutableArray *keysToRemove = [NSMutableArray array];
 
     while ((value = [pubFields objectForKey:key]) != nil) {
-        BDSKAliasFile *aFile = [[BDSKAliasFile alloc] 
initWithBase64String:value];
+        BDSKAliasFile *aFile = [[BDSKAliasFile alloc] 
initWithBase64String:value delegate:self];
         if (aFile) {
             [files addObject:aFile];
             [aFile release];
             [keysToRemove addObject:key];
         }
         else {
-            NSLog(@"*** error *** -[BDSKAliasFile initWithBase64String:] 
failed (%@ of %@)", key, [self citeKey]);
+            NSLog(@"*** error *** -[BDSKAliasFile 
initWithBase64String:delegate:] failed (%@ of %@)", key, [self citeKey]);
         }
         
         // next key in the sequence; increment i first, so it's guaranteed 
correct
@@ -2507,10 +2503,12 @@
 - (void)insertObject:(BDSKAliasFile *)aFile inFilesAtIndex:(NSUInteger)idx
 {
     [files insertObject:aFile atIndex:idx];
+    [aFile setDelegate:self];
 }
 
 - (void)removeObjectFromFilesAtIndex:(NSUInteger)idx
 {
+    [[files objectAtIndex:idx] setDelegate:nil];
     [files removeObjectAtIndex:idx];
 }
 
@@ -2518,8 +2516,8 @@
 {
     NSArray *toMove = [[files objectsAtIndexes:aSet] copy];
     unsigned anIdx = [aSet indexLessThanIndex:idx];
+    // reduce idx by the number of smaller indexes in aSet
     while (anIdx != NSNotFound) {
-#warning noop
         idx--;
         anIdx = [aSet indexLessThanIndex:anIdx];
     }
@@ -2541,9 +2539,12 @@
     }
     fe = [files objectEnumerator];
     BDSKAliasFile *file;
-    while (file = [fe nextObject])
-        if (aURL = [file fileURLRelativeToURL:[self baseURL]])
+    NSString *relPath;
+    while (file = [fe nextObject]) {
+        if ((aURL = [file fileURL]) || 
+            ((relPath = [file relativePath]) && (aURL = [NSURL 
fileURLWithPath:relPath])))
             [combinedURLs addObject:aURL];
+    }
     [combinedURLs sortUsingFunction:sortURLsByType context:NULL];
     return combinedURLs;
 }


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

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to