Revision: 28274
          http://sourceforge.net/p/bibdesk/svn/28274
Author:   hofman
Date:     2023-06-01 15:46:27 +0000 (Thu, 01 Jun 2023)
Log Message:
-----------
Add downloads directory selected in prefs to recent downloads persistent search 
query. Let the persistent search query add it, and observe defaults to update 
it when te downloads folder changes.

Modified Paths:
--------------
    trunk/bibdesk/BDSKEditor.m
    trunk/bibdesk/BDSKPersistentSearch.h
    trunk/bibdesk/BDSKPersistentSearch.m

Modified: trunk/bibdesk/BDSKEditor.m
===================================================================
--- trunk/bibdesk/BDSKEditor.m  2023-06-01 14:58:39 UTC (rev 28273)
+++ trunk/bibdesk/BDSKEditor.m  2023-06-01 15:46:27 UTC (rev 28274)
@@ -110,9 +110,6 @@
 
 #define FIELD_COLUMNID @"field"
 
-// this was copied verbatim from a Finder saved search for all items of kind 
document modified in the last week
-static NSString * const recentDownloadsQuery = @"(kMDItemContentTypeTree = 
'public.content') && (kMDItemFSContentChangeDate >= $time.today(-7)) && 
(kMDItemContentType != com.apple.mail.emlx) && (kMDItemContentType != 
public.vcard)";
-
 enum { BDSKMoveToTrashAsk = -1, BDSKMoveToTrashNo = 0, BDSKMoveToTrashYes = 1 
};
 enum { BDSKUpdateTable = -1, BDSKNoReload = 0, BDSKReloadTable = 1 };
 
@@ -1464,14 +1461,10 @@
     
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
-        // limit the scope to the default downloads directory
-        NSURL *downloadURL = [[NSFileManager defaultManager] 
downloadFolderURL];
-        if(downloadURL){
-            [[BDSKPersistentSearch sharedSearch] addQuery:recentDownloadsQuery 
scopes:[NSArray arrayWithObject:downloadURL]];
-        }
+        [[BDSKPersistentSearch sharedSearch] addRecentDownloadsQuery];
     });
     
-    NSArray *paths = [[BDSKPersistentSearch sharedSearch] 
resultsForQuery:recentDownloadsQuery attribute:(NSString *)kMDItemPath];
+    NSArray *paths = [[BDSKPersistentSearch sharedSearch] 
resultsForQuery:[BDSKPersistentSearch recentDownloadsQuery] attribute:(NSString 
*)kMDItemPath];
     NSMenuItem *item;
     
     for (NSString *filePath in paths) {

Modified: trunk/bibdesk/BDSKPersistentSearch.h
===================================================================
--- trunk/bibdesk/BDSKPersistentSearch.h        2023-06-01 14:58:39 UTC (rev 
28273)
+++ trunk/bibdesk/BDSKPersistentSearch.h        2023-06-01 15:46:27 UTC (rev 
28274)
@@ -56,4 +56,8 @@
 // returns nil if no results (yet)
 - (NSArray *)resultsForQuery:(NSString *)queryString attribute:(NSString 
*)attribute;
 
++ (NSString *)recentDownloadsQuery;
+
+- (BOOL)addRecentDownloadsQuery;
+
 @end

Modified: trunk/bibdesk/BDSKPersistentSearch.m
===================================================================
--- trunk/bibdesk/BDSKPersistentSearch.m        2023-06-01 14:58:39 UTC (rev 
28273)
+++ trunk/bibdesk/BDSKPersistentSearch.m        2023-06-01 15:46:27 UTC (rev 
28274)
@@ -37,10 +37,17 @@
  */
 
 #import "BDSKPersistentSearch.h"
+#import "BDSKStringConstants.h"
+#import "NSFileManager_BDSKExtensions.h"
 
 static id sharedSearch = nil;
 static void *nullQueryMarker = @"Null MDQuery Marker"; /* any CFTypeRef would 
work here */
 
+// this was copied verbatim from a Finder saved search for all items of kind 
document modified in the last week
+static NSString * const recentDownloadsQuery = @"(kMDItemContentTypeTree = 
'public.content') && (kMDItemFSContentChangeDate >= $time.today(-7)) && 
(kMDItemContentType != com.apple.mail.emlx) && (kMDItemContentType != 
public.vcard)";
+
+static char BDSKPersistentSearchDefaultsObservationContext;
+
 @implementation BDSKPersistentSearch
 
 + (id)sharedSearch;
@@ -52,14 +59,26 @@
 
 - (id)init
 {    
-    if(self = [super init])
+    if(self = [super init]) {
         queries = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, 
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
-    
+        [[NSUserDefaultsController sharedUserDefaultsController]
+            addObserver:self
+             forKeyPath:[@"values." 
stringByAppendingString:BDSKDownloadsDirectoryKey]
+                options:0
+                context:&BDSKPersistentSearchDefaultsObservationContext];
+    }
     return self;
 }
 
 - (void)dealloc
 {
+    @try {
+        [[NSUserDefaultsController sharedUserDefaultsController]
+            removeObserver:self
+                forKeyPath:[@"values." 
stringByAppendingString:BDSKDownloadsDirectoryKey]
+                   context:&BDSKPersistentSearchDefaultsObservationContext];
+    }
+    @catch (id e) {}
     BDSKCFDESTROY(queries);
     [super dealloc];
 }
@@ -147,4 +166,33 @@
     return results;
 }
 
++ (NSString *)recentDownloadsQuery {
+    return recentDownloadsQuery;
+}
+
+- (BOOL)addRecentDownloadsQuery {
+    // limit the scope to the default downloads directory and our downloads 
directory
+    NSURL *downloadURL = [[NSFileManager defaultManager] downloadFolderURL];
+    NSString *downloadsDirectory = [[[NSUserDefaults standardUserDefaults] 
stringForKey:BDSKDownloadsDirectoryKey] stringByExpandingTildeInPath];
+    NSMutableArray *downloadURLs = [NSMutableArray 
arrayWithObjects:downloadURL, nil];
+    if (downloadsDirectory) {
+        NSURL *downloadsURL = [NSURL fileURLWithPath:downloadsDirectory 
isDirectory:YES];
+        if ([downloadsURL isEqual:downloadURL] == NO)
+            [downloadURLs addObject:downloadsURL];
+    }
+    if ([downloadURLs count])
+        return [self addQuery:recentDownloadsQuery scopes:downloadURLs];
+    else
+        return NO;
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
+    if (context == &BDSKPersistentSearchDefaultsObservationContext) {
+        if ([self hasQuery:[BDSKPersistentSearch recentDownloadsQuery]])
+            [self addRecentDownloadsQuery];
+    } else {
+        [super observeValueForKeyPath:keyPath ofObject:object change:change 
context:context];
+    }
+}
+
 @end

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



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to