From: <ama...@us...> - 2007-10-08 03:11:54
|
Revision: 11212 http://bibdesk.svn.sourceforge.net/bibdesk/?rev=11212&view=rev Author: amaxwell Date: 2007-10-07 20:11:57 -0700 (Sun, 07 Oct 2007) Log Message: ----------- Add support for testing BDSKFiles in BibItem, mainly to how they survive a round trip. This creates them from Bdsk-File-n fields if available, and from Local-Url if not. Bdsk-File-n fields are written out when saving, as are Local-Urls. BibEditor is currently only displaying the BDSKFiles. I'm not sure how to handle the combination of local and remote with editing, since they're in different arrays at the model level. Modified Paths: -------------- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibDocument.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/BibDocument.m =================================================================== --- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibDocument.m 2007-10-08 00:36:28 UTC (rev 11211) +++ branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibDocument.m 2007-10-08 03:11:57 UTC (rev 11212) @@ -2659,7 +2659,7 @@ static void addAllURLsToBag(const void *value, void *context) { - CFArrayRef fpaths = (CFArrayRef)[(BibItem *)value allFilePaths]; + CFArrayRef fpaths = (CFArrayRef)[(BibItem *)value sortedURLs]; CFArrayApplyFunction(fpaths, CFRangeMake(0, CFArrayGetCount(fpaths)), addValueFromArrayToBag, context); } @@ -2682,7 +2682,7 @@ static void addAllURLsToArray(const void *value, void *context) { - CFArrayRef fpaths = (CFArrayRef)[(BibItem *)value allFilePaths]; + CFArrayRef fpaths = (CFArrayRef)[(BibItem *)value sortedURLs]; CFArrayApplyFunction(fpaths, CFRangeMake(0, CFArrayGetCount(fpaths)), addValueFromArrayToArray, context); } @@ -2751,6 +2751,7 @@ [statusBar addSubview:fileviewSlider]; [fileviewSlider setMinValue:0.5]; [fileviewSlider setMaxValue:20]; + [fileviewSlider setAutoresizingMask:NSViewMinXMargin]; } view = fileviewBox; if (currentPreviewView != view) { Modified: branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibEditor.m =================================================================== --- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibEditor.m 2007-10-08 00:36:28 UTC (rev 11211) +++ branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibEditor.m 2007-10-08 03:11:57 UTC (rev 11212) @@ -83,6 +83,7 @@ #import "BDSKSkimReader.h" #import "BDSKSplitView.h" #import <FileView/FileView.h> +#import "BDSKFile.h" static NSString *BDSKBibEditorFrameAutosaveName = @"BibEditor window autosave name"; @@ -113,44 +114,58 @@ @implementation BibEditor -- (NSUInteger)numberOfIconsInFileView:(FileView *)aFileView { return [_files count]; } +- (NSUInteger)numberOfIconsInFileView:(FileView *)aFileView { return [publication countOfFiles]; } - (NSURL *)fileView:(FileView *)aFileView URLAtIndex:(NSUInteger)idx; { - return [_files objectAtIndex:idx]; + return [[publication fileAtIndex:idx] fileURL]; } - (BOOL)fileView:(FileView *)aFileView moveURLsAtIndexes:(NSIndexSet *)aSet toIndex:(NSUInteger)anIndex; { - NSArray *toMove = [[_files objectsAtIndexes:aSet] copy]; - [_files removeObjectsAtIndexes:aSet]; - - aSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(anIndex, [aSet count])]; - [_files insertObjects:toMove atIndexes:aSet]; - [toMove release]; + [publication moveFilesAtIndexes:aSet toIndex:anIndex]; return YES; } - (BOOL)fileView:(FileView *)fileView replaceURLsAtIndexes:(NSIndexSet *)aSet withURLs:(NSArray *)newURLs; { - if ([_files count] > [aSet count]) { - [_files replaceObjectsAtIndexes:aSet withObjects:newURLs]; - return YES; + BDSKFile *aFile; + NSEnumerator *enumerator = [newURLs objectEnumerator]; + NSURL *aURL; + NSUInteger idx = [aSet firstIndex]; + while ((aURL = [enumerator nextObject]) != nil && NSNotFound != idx) { + aFile = [BDSKFile fileWithURL:aURL]; + if (aFile) { + [publication removeObjectFromFilesAtIndex:idx]; + [publication insertObject:aFile inFilesAtIndex:idx]; + } + idx = [aSet indexGreaterThanIndex:idx]; } - return NO; + return YES; } - (BOOL)fileView:(FileView *)fileView deleteURLsAtIndexes:(NSIndexSet *)indexSet; { - if ([_files count] >= [indexSet count]) { - [_files removeObjectsAtIndexes:indexSet]; - return YES; + NSUInteger idx = [indexSet firstIndex]; + while (NSNotFound != idx) { + [publication removeObjectFromFilesAtIndex:idx]; + idx = [indexSet indexGreaterThanIndex:idx]; } - return NO; + return YES; } - (void)fileView:(FileView *)aFileView insertURLs:(NSArray *)absoluteURLs atIndexes:(NSIndexSet *)aSet; { - [_files insertObjects:absoluteURLs atIndexes:aSet]; + BDSKFile *aFile; + NSEnumerator *enumerator = [absoluteURLs objectEnumerator]; + NSURL *aURL; + NSUInteger idx = [aSet firstIndex]; + while ((aURL = [enumerator nextObject]) != nil && NSNotFound != idx) { + aFile = [BDSKFile fileWithURL:aURL]; + if (aFile) { + [publication insertObject:aFile inFilesAtIndex:idx]; + } + idx = [aSet indexGreaterThanIndex:idx]; + } } - (NSString *)windowNibName{ @@ -3145,6 +3160,7 @@ pdfSnoopViewLoaded = YES; } else if ([[documentSnoopDrawer contentView] isEqual:textSnoopContainerView]) { +#warning remove this NSMutableString *path = [[[lurl path] mutableCopy] autorelease]; if([NSString isEmptyString:path] == NO && [theUTI isEqualToUTI:(NSString *)kUTTypePDF]){ Modified: branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.h =================================================================== --- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.h 2007-10-08 00:36:28 UTC (rev 11211) +++ branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.h 2007-10-08 03:11:57 UTC (rev 11212) @@ -49,7 +49,7 @@ BDSKIsCrossreffedCrossrefError }; -@class BibDocument, BDSKGroup, BibAuthor, BDSKFieldCollection, BDSKTemplate, BDSKPublicationsArray, BDSKMacroResolver; +@class BibDocument, BDSKGroup, BibAuthor, BDSKFieldCollection, BDSKTemplate, BDSKPublicationsArray, BDSKMacroResolver, BDSKFile; @protocol BDSKParseableItem, BDSKOwner; /*! @@ -79,9 +79,17 @@ BOOL isImported; float searchScore; NSURL *identifierURL; - NSMutableArray *allFiles; + NSMutableArray *files; + NSMutableArray *sortedURLs; } +- (NSUInteger)countOfFiles; +- (BDSKFile *)fileAtIndex:(NSUInteger)idx; +- (void)insertObject:(BDSKFile *)aFile inFilesAtIndex:(NSUInteger)idx; +- (void)removeObjectFromFilesAtIndex:(NSUInteger)idx; +- (void)moveFilesAtIndexes:(NSIndexSet *)aSet toIndex:(NSUInteger)idx; +- (NSArray *)sortedURLs; + /*! @method init @abstract Initializes an alloc'd BibItem to a default type, empty authors array and createdDate the current date. Modified: branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.m =================================================================== --- branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.m 2007-10-08 00:36:28 UTC (rev 11211) +++ branches/TRY_ARM_FILE_INTERFACE/bibdesk/BibItem.m 2007-10-08 03:11:57 UTC (rev 11212) @@ -70,8 +70,8 @@ #import "NSData_BDSKExtensions.h" #import "BDSKSkimReader.h" #import "BDSKCitationFormatter.h" +#import "BDSKFile.h" - static NSString *BDSKDefaultCiteKey = @"cite-key"; static NSSet *fieldsToWriteIfEmpty = nil; @@ -287,6 +287,7 @@ groups = [[NSMutableDictionary alloc] initWithCapacity:5]; cachedURLs = [[NSMutableDictionary alloc] initWithCapacity:5]; + [self _createFilesArray]; templateFields = nil; // updateMetadataForKey with a nil argument will set the dates properly if we read them from a file @@ -328,6 +329,7 @@ [self setDateModified:[coder decodeObjectForKey:@"dateModified"]]; groups = [[NSMutableDictionary alloc] initWithCapacity:5]; cachedURLs = [[NSMutableDictionary alloc] initWithCapacity:5]; + files = [[coder decodeObjectForKey:@"files"] retain]; // set by the document, which we don't archive owner = nil; fileOrder = nil; @@ -353,6 +355,7 @@ [coder encodeObject:pubType forKey:@"pubType"]; [coder encodeObject:pubFields forKey:@"pubFields"]; [coder encodeBool:hasBeenEdited forKey:@"hasBeenEdited"]; + [coder encodeObject:files forKey:@"files"]; } else { [coder encodeDataObject:[NSKeyedArchiver archivedDataWithRootObject:self]]; } @@ -378,7 +381,8 @@ [dateModified release]; [fileOrder release]; [identifierURL release]; - [allFiles release]; + [sortedURLs release]; + [files release]; [super dealloc]; } @@ -1596,6 +1600,24 @@ #pragma mark - #pragma mark BibTeX strings +- (NSString *)filesAsBibTeXFragment +{ + // !!! inherit + NSUInteger i, iMax = [files count]; + + NSMutableString *string = nil; + if (iMax > 0) { + string = [NSMutableString string]; + for (i = 0; i < iMax; i++) { + NSString *key = [NSString stringWithFormat:@"Bdsk-File-%d", i]; + NSString *value = [[files objectAtIndex:i] base64StringRelativeTo:@""]; + OBPRECONDITION([value rangeOfCharacterFromSet:[NSCharacterSet curlyBraceCharacterSet]].length == 0); + [string appendFormat:@",\n\t%@ = {%@}", key, value]; + } + } + return string; +} + - (NSString *)bibTeXStringDroppingInternal:(BOOL)drop texify:(BOOL)shouldTeXify{ OFPreferenceWrapper *pw = [OFPreferenceWrapper sharedPreferenceWrapper]; NSMutableSet *knownKeys = nil; @@ -1659,6 +1681,10 @@ [s appendString:[value stringAsBibTeXString]]; } } + if (!drop) { + value = [self filesAsBibTeXFragment]; + if (value) [s appendString:value]; + } [knownKeys release]; [s appendString:@"}"]; @@ -1747,6 +1773,11 @@ } } [knownKeys release]; + if(isOK && !drop) { + value = [self filesAsBibTeXFragment]; + // assumes encoding is ascii-compatible, but btparse does as well + if (value) [data appendDataFromString:value encoding:encoding error:&error]; + } if(isOK) isOK = [data appendDataFromString:@"}" encoding:encoding error:&error]; @@ -2403,23 +2434,93 @@ else return NSOrderedDescending; } -- (NSArray *)allFilePaths +static void addURLForFieldToArrayIfNotNil(const void *key, void *context) { - if (nil == allFiles) { - NSEnumerator *fe = [[[BDSKTypeManager sharedManager] allURLFieldsSet] objectEnumerator]; - allFiles = [NSMutableArray new]; - NSString *field; - NSURL *aURL; - while (field = [fe nextObject]) { - aURL = [self URLForField:field]; - if (aURL) - [allFiles addObject:aURL]; + BibItem *self = (BibItem *)context; + NSURL *value = [self localFileURLForField:(id)key]; + if (value) { + BDSKFile *aFile = [[BDSKFile alloc] initWithURL:value]; + [self->files addObject:aFile]; + [aFile release]; + } +} + +- (void)_createFilesArray +{ + files = [NSMutableArray new]; + NSUInteger i = 0; + NSString *value, *key = [NSString stringWithFormat:@"Bdsk-File-%d", i]; + + NSMutableArray *keysToRemove = [NSMutableArray array]; + + while ((value = [pubFields objectForKey:key]) != nil) { + // !!! relative to what? + BDSKFile *aFile = [[BDSKFile alloc] initWithBase64String:value relativeTo:@""]; + if (aFile) { + [files addObject:aFile]; + [aFile release]; + [keysToRemove addObject:key]; } - [allFiles sortUsingFunction:sortURLsByType context:NULL]; + else { + NSLog(@"*** error *** -[BDSKFile initWithBase64String:relativeTo:] failed (%@ of %@)", key, [self citeKey]); + } + + // next key in the sequence; increment i first, so it's guaranteed correct + i++; + key = [NSString stringWithFormat:@"Bdsk-File-%d", i]; } - return allFiles; + + // !!! get these out of pubFields for now to avoid duplication when saving + [pubFields removeObjectsForKeys:keysToRemove]; + + // @@ temporary hack to create an array of BDSKFiles from Local-Urls + if ([files count] == 0) { + CFSetRef fileSet = (CFSetRef)[[BDSKTypeManager sharedManager] localFileFieldsSet]; + CFSetApplyFunction(fileSet, addURLForFieldToArrayIfNotNil, self); + } } +- (NSUInteger)countOfFiles { return [files count]; } + +- (BDSKFile *)fileAtIndex:(NSUInteger)idx +{ + return [files objectAtIndex:idx]; +} + +- (void)insertObject:(BDSKFile *)aFile inFilesAtIndex:(NSUInteger)idx +{ + [files insertObject:aFile atIndex:idx]; +} + +- (void)removeObjectFromFilesAtIndex:(NSUInteger)idx +{ + [files removeObjectAtIndex:idx]; +} + +- (void)moveFilesAtIndexes:(NSIndexSet *)aSet toIndex:(NSUInteger)idx +{ + NSArray *toMove = [[files objectsAtIndexes:aSet] copy]; + [files removeObjectsAtIndexes:aSet]; + [files insertObjects:toMove atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(idx, [toMove count])]]; + [toMove release]; +} + +- (NSArray *)sortedURLs +{ + NSMutableArray *combinedURLs = [NSMutableArray array]; + NSEnumerator *fe = [[[BDSKTypeManager sharedManager] remoteURLFieldsSet] objectEnumerator]; + NSString *field; + NSURL *aURL; + while (field = [fe nextObject]) { + aURL = [self remoteURLForField:field]; + if (aURL) + [combinedURLs addObject:aURL]; + } + [combinedURLs addObjectsFromArray:[self valueForKeyPath:@"files.fileURL"]]; + [combinedURLs sortUsingFunction:sortURLsByType context:NULL]; + return combinedURLs; +} + - (NSURL *)remoteURL{ return [self remoteURLForField:BDSKUrlString]; } @@ -3168,16 +3269,16 @@ // the URL cache is certainly invalid now [cachedURLs removeAllObjects]; - [allFiles release]; - allFiles = nil; + [sortedURLs release]; + sortedURLs = nil; }else if(key != nil){ [groups removeObjectForKey:key]; } if([key isURLField]) { [cachedURLs removeObjectForKey:key]; - [allFiles release]; - allFiles = nil; + [sortedURLs release]; + sortedURLs = nil; } NSCalendarDate *theDate = nil; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |