Revision: 22142 http://sourceforge.net/p/bibdesk/svn/22142 Author: hofman Date: 2018-03-21 21:58:22 +0000 (Wed, 21 Mar 2018) Log Message: ----------- Use file search info and notifications to update notes search index. Also update on linked file changes. Avoid removing search documents we know are not present.
Modified Paths: -------------- trunk/bibdesk/BDSKFileSearchIndex.m trunk/bibdesk/BDSKNotesSearchIndex.h trunk/bibdesk/BDSKNotesSearchIndex.m trunk/bibdesk/BibDocument.m Modified: trunk/bibdesk/BDSKFileSearchIndex.m =================================================================== --- trunk/bibdesk/BDSKFileSearchIndex.m 2018-03-21 17:26:15 UTC (rev 22141) +++ trunk/bibdesk/BDSKFileSearchIndex.m 2018-03-21 21:58:22 UTC (rev 22142) @@ -494,9 +494,6 @@ - (void)processAddItemNotification:(NSNotification *)note { - if (canceled) - return; - NSArray *searchIndexInfo = [[note userInfo] valueForKeyPath:@"publications.searchIndexInfo"]; dispatch_async(queue, ^{ @@ -508,9 +505,6 @@ - (void)processDelItemNotification:(NSNotification *)note { - if (canceled) - return; - NSArray *searchIndexInfo = [[note userInfo] valueForKeyPath:@"publications.searchIndexInfo"]; dispatch_async(queue, ^{ @@ -532,9 +526,6 @@ - (void)processInfoChangedNotification:(NSNotification *)note { - if (canceled) - return; - NSArray *searchIndexInfo = [[note userInfo] valueForKeyPath:@"publications.searchIndexInfo"]; dispatch_async(queue, ^{ Modified: trunk/bibdesk/BDSKNotesSearchIndex.h =================================================================== --- trunk/bibdesk/BDSKNotesSearchIndex.h 2018-03-21 17:26:15 UTC (rev 22141) +++ trunk/bibdesk/BDSKNotesSearchIndex.h 2018-03-21 21:58:22 UTC (rev 22142) @@ -39,6 +39,8 @@ #import <Cocoa/Cocoa.h> +@protocol BDSKOwner; + @interface BDSKNotesSearchIndex : NSObject { SKIndexRef skIndex; volatile int32_t shouldClear; @@ -49,8 +51,8 @@ NSFileManager *fileManager; } -- (void)removePublications:(NSArray *)pubs; -- (void)addPublications:(NSArray *)pubs; +- (id)initForOwner:(id <BDSKOwner>)owner; + - (void)resetWithPublications:(NSArray *)pubs; // Warning: it is /not/ safe to write to this SKIndexRef directly; use it only for reading. Modified: trunk/bibdesk/BDSKNotesSearchIndex.m =================================================================== --- trunk/bibdesk/BDSKNotesSearchIndex.m 2018-03-21 17:26:15 UTC (rev 22141) +++ trunk/bibdesk/BDSKNotesSearchIndex.m 2018-03-21 21:58:22 UTC (rev 22142) @@ -42,15 +42,20 @@ #import "NSURL_BDSKExtensions.h" #import <libkern/OSAtomic.h> #import <SkimNotesBase/SkimNotesBase.h> +#import "BibDocument.h" @interface BDSKNotesSearchIndex (BDSKPrivate) -- (void)indexItemForIdentifierURL:(NSURL *)identifierURL fileURLs:(NSArray *)fileURLs; + +- (void)indexItemForIdentifierURL:(NSURL *)identifierURL fileURLs:(NSArray *)fileURLs removeEmpty:(BOOL)removeEmpty; +- (void)processAddItemNotification:(NSNotification *)note; +- (void)processDelItemNotification:(NSNotification *)note; + @end @implementation BDSKNotesSearchIndex -- (id)init +- (id)initForOwner:(id <BDSKOwner>)owner { self = [super init]; if (self) { @@ -64,6 +69,12 @@ fileManager = [[NSFileManager alloc] init]; [self resetWithPublications:nil]; + + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; + [nc addObserver:self selector:@selector(processAddItemNotification:) name:BDSKFileSearchIndexInfoChangedNotification object:owner]; + [nc addObserver:self selector:@selector(processAddItemNotification:) name:BDSKDocAddItemNotification object:owner]; + [nc addObserver:self selector:@selector(processDelItemNotification:) name:BDSKDocDelItemNotification object:owner]; + } return self; } @@ -88,32 +99,32 @@ - (void)terminate { terminated = YES; + [[NSNotificationCenter defaultCenter] removeObserver:self]; [self clearQueue]; } -- (void)addPublications:(NSArray *)pubs +- (void)processAddItemNotification:(NSNotification *)note { - if (terminated) - return; - for (BibItem *pub in pubs) { - NSURL *identifierURL = [pub identifierURL]; - NSArray *fileURLs = [[pub existingLocalFiles] valueForKey:@"URL"]; - dispatch_async(queue, ^{ - [self indexItemForIdentifierURL:identifierURL fileURLs:fileURLs]; - }); - } + NSArray *items = [[note userInfo] valueForKeyPath:@"publications.searchIndexInfo"]; + + dispatch_async(queue, ^{ + for (NSDictionary *item in items) { + NSURL *identifierURL = [item objectForKey:@"identifierURL"]; + NSArray *fileURLs = [item objectForKey:@"urls"]; + [self indexItemForIdentifierURL:identifierURL fileURLs:fileURLs removeEmpty:YES]; + } + }); } -- (void)removePublications:(NSArray *)pubs -{ - if (terminated) - return; - for (BibItem *pub in pubs) { - NSURL *identifierURL = [pub identifierURL]; - dispatch_async(queue, ^{ - [self indexItemForIdentifierURL:identifierURL fileURLs:nil]; - }); - } +- (void)processDelItemNotification:(NSNotification *)note { + NSArray *items = [[note userInfo] valueForKeyPath:@"publications.searchIndexInfo"]; + + dispatch_async(queue, ^{ + for (NSDictionary *item in items) { + NSURL *identifierURL = [item objectForKey:@"identifierURL"]; + [self indexItemForIdentifierURL:identifierURL fileURLs:nil removeEmpty:YES]; + } + }); } - (void)resetWithPublications:(NSArray *)pubs @@ -137,7 +148,15 @@ [options release]; // this will handle the index flush after adding all the pubs - [self addPublications:pubs]; + NSArray *items = [pubs valueForKey:@"searchIndexInfo"]; + + dispatch_async(queue, ^{ + for (NSDictionary *item in items) { + NSURL *identifierURL = [item objectForKey:@"identifierURL"]; + NSArray *fileURLs = [item objectForKey:@"urls"]; + [self indexItemForIdentifierURL:identifierURL fileURLs:fileURLs removeEmpty:NO]; + } + }); } @@ -158,7 +177,7 @@ return (SKIndexRef)[(id)theIndex autorelease]; } -- (void)indexItemForIdentifierURL:(NSURL *)identifierURL fileURLs:(NSArray *)fileURLs +- (void)indexItemForIdentifierURL:(NSURL *)identifierURL fileURLs:(NSArray *)fileURLs removeEmpty:(BOOL)removeEmpty { OSMemoryBarrier(); if (shouldClear) @@ -207,7 +226,7 @@ if (theIndex) { if ([searchText length]) SKIndexAddDocumentWithText(theIndex, doc, (CFStringRef)searchText, TRUE); - else + else if (removeEmpty) SKIndexRemoveDocument(theIndex, doc); CFRelease(theIndex); OSAtomicCompareAndSwap32Barrier(0, 1, &needsFlushing); Modified: trunk/bibdesk/BibDocument.m =================================================================== --- trunk/bibdesk/BibDocument.m 2018-03-21 17:26:15 UTC (rev 22141) +++ trunk/bibdesk/BibDocument.m 2018-03-21 21:58:22 UTC (rev 22142) @@ -242,7 +242,7 @@ [self registerForNotifications]; searchIndexes = [[BDSKItemSearchIndexes alloc] init]; - notesSearchIndex = [[BDSKNotesSearchIndex alloc] init]; + notesSearchIndex = [[BDSKNotesSearchIndex alloc] initForOwner:self]; rowToSelectAfterDelete = -1; } return self; @@ -871,8 +871,7 @@ [pubs setValue:self forKey:@"owner"]; [searchIndexes addPublications:pubs]; - [notesSearchIndex addPublications:pubs]; - + NSDictionary *notifInfo = [NSDictionary dictionaryWithObjectsAndKeys:pubs, BDSKDocumentPublicationsKey, nil]; [[NSNotificationCenter defaultCenter] postNotificationName:BDSKDocAddItemNotification object:self @@ -900,7 +899,6 @@ [[groups lastImportGroup] removePublicationsInArray:pubs]; [[groups staticGroups] makeObjectsPerformSelector:@selector(removePublicationsInArray:) withObject:pubs]; [searchIndexes removePublications:pubs]; - [notesSearchIndex removePublications:pubs]; [publications removeObjectsAtIndexes:indexes]; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit