Revision: 27615
          http://sourceforge.net/p/bibdesk/svn/27615
Author:   hofman
Date:     2022-06-12 16:36:47 +0000 (Sun, 12 Jun 2022)
Log Message:
-----------
Use temporary web group to get items using web parser when passing a URL to the 
import script command

Modified Paths:
--------------
    trunk/bibdesk/BDSKWebGroup.h
    trunk/bibdesk/BDSKWebGroup.m
    trunk/bibdesk/BibDocument+Scripting.m

Modified: trunk/bibdesk/BDSKWebGroup.h
===================================================================
--- trunk/bibdesk/BDSKWebGroup.h        2022-06-12 13:58:32 UTC (rev 27614)
+++ trunk/bibdesk/BDSKWebGroup.h        2022-06-12 16:36:47 UTC (rev 27615)
@@ -57,3 +57,15 @@
 @property (nonatomic, readonly, getter=isWebViewLoaded) BOOL webViewLoaded;
 
 @end
+
+#pragma mark -
+
+@interface BDSKWebSearch : NSObject {
+    BDSKWebGroup *group;
+    BDSKMacroResolver *macroResolver;
+    void(^completionHandler)(NSArray *);
+}
+
++ (NSArray *)itemsFromURL:(NSURL *)aURL macroResolver:(BDSKMacroResolver 
*)macroResolver completionHandler:(void(^)(NSArray 
*publications))completionHandler;
+
+@end

Modified: trunk/bibdesk/BDSKWebGroup.m
===================================================================
--- trunk/bibdesk/BDSKWebGroup.m        2022-06-12 13:58:32 UTC (rev 27614)
+++ trunk/bibdesk/BDSKWebGroup.m        2022-06-12 16:36:47 UTC (rev 27615)
@@ -48,6 +48,7 @@
 #import "BibDocument_Groups.h"
 #import "BDSKGroupsArray.h"
 #import "BDSKMacroResolver.h"
+#import "BDSKPublicationsArray.h"
 #import "NSString_BDSKExtensions.h"
 #import "NSError_BDSKExtensions.h"
 #import "NSArray_BDSKExtensions.h"
@@ -172,6 +173,14 @@
         [[self webView] setURL:newURL];
 }
 
+// this is only used by the BDSKWebSearch
+- (void)setMacroResolver:(BDSKMacroResolver *)newMacroResolver {
+    if (newMacroResolver != macroResolver) {
+        [macroResolver release];
+        macroResolver = [newMacroResolver retain];
+    }
+}
+
 #pragma mark BDSKWebViewDelegate protocol
 
 - (void)webView:(WebView *)sender setTitle:(NSString *)title {
@@ -376,3 +385,82 @@
 }
 
 @end
+
+#pragma mark -
+
+@implementation BDSKWebSearch
+
+- (id)initWithMacroResolver:(BDSKMacroResolver *)aMacroResolver {
+    self = [super init];
+    if (self) {
+        group = [[BDSKWebGroup alloc] init];
+        macroResolver = [aMacroResolver retain];
+        completionHandler = NULL;
+    }
+    return self;
+}
+
+- (void)dealloc {
+    BDSKDESTROY(group);
+    BDSKDESTROY(macroResolver);
+    [super dealloc];
+}
+
+- (NSArray *)publications {
+    if ([[group publications] count] == 0)
+        return nil;
+    NSMutableArray *pubs = [NSMutableArray array];
+    for (BibItem *pub in [group publications]) {
+        pub = [pub copyWithMacroResolver:macroResolver];
+        [pubs addObject:pub];
+        [pub release];
+    }
+    return pubs;
+}
+
+- (void)handleGroupUpdatedNotification:(NSNotification *)notification {
+    if ([group isRetrieving] == NO && completionHandler) {
+        completionHandler([self publications]);
+        BDSKBLOCKDESTROY(completionHandler);
+        [[NSNotificationCenter defaultCenter] removeObserver:self];
+        [self release];
+    }
+}
+
+- (NSArray *)itemsFromURL:(NSURL *)aURL completionHandler:(void(^)(NSArray 
*publications))aCompletionHandler {
+    
+    completionHandler = Block_copy(aCompletionHandler);
+    
+    [self retain];
+    
+    [group setURL:aURL];
+    
+    if (completionHandler != NULL) {
+        
+        if ([group isRetrieving])
+            [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(handleGroupUpdatedNotification:) 
name:BDSKExternalGroupUpdatedNotification object:group];
+        else
+            [self handleGroupUpdatedNotification:nil];
+        
+        return nil;
+        
+    } else {
+        
+        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
+        while ([group isRetrieving] && [runLoop runMode:NSDefaultRunLoopMode 
beforeDate:[NSDate distantFuture]]);
+        
+        NSArray *pubs = [self publications];
+        
+        [self release];
+        
+        return pubs;
+        
+    }
+}
+
++ (NSArray *)itemsFromURL:(NSURL *)aURL macroResolver:(BDSKMacroResolver 
*)macroResolver completionHandler:(void(^)(NSArray 
*publications))completionHandler {
+    BDSKWebSearch *search = [[[self alloc] 
initWithMacroResolver:macroResolver] autorelease];
+    return [search itemsFromURL:aURL completionHandler:completionHandler];
+}
+
+@end

Modified: trunk/bibdesk/BibDocument+Scripting.m
===================================================================
--- trunk/bibdesk/BibDocument+Scripting.m       2022-06-12 13:58:32 UTC (rev 
27614)
+++ trunk/bibdesk/BibDocument+Scripting.m       2022-06-12 16:36:47 UTC (rev 
27615)
@@ -1153,9 +1153,7 @@
     }
     
     // finally get the pubs and process them
-    if ([source isKindOfClass:[NSString class]]) {
-        pubs = [BDSKStringParser itemsFromString:source 
ofType:BDSKStringTypeUnknown owner:self error:NULL];
-    } else if ([source isKindOfClass:[NSArray class]]) {
+    if ([source isKindOfClass:[NSArray class]]) {
         NSMutableArray *tmpPubs = [NSMutableArray array];
         for (BibItem *pub in source) {
             // get complex strings with the correct macroResolver
@@ -1164,6 +1162,20 @@
             [pub release];
         }
         pubs = tmpPubs;
+    } else if ([source isKindOfClass:[NSString class]]) {
+        NSURL *aURL = nil;
+        if ([source rangeOfString:@"://"].location != NSNotFound) {
+            aURL = [NSURL URLWithString:source];
+            if ([[aURL host] isCaseInsensitiveEqual:@"doi.org"] || [[aURL 
host] isCaseInsensitiveEqual:@"dx.doi.org"])
+                aURL = nil;
+        }
+        if (aURL) {
+            pubs = [BDSKWebSearch itemsFromURL:aURL macroResolver:[self 
macroResolver] completionHandler:synchronous ? nil : handler];
+            if (synchronous == NO)
+                return nil;
+        } else {
+            pubs = [BDSKStringParser itemsFromString:source 
ofType:BDSKStringTypeUnknown owner:self error:NULL];
+        }
     } else if (searchTerm) {
         pubs = [BDSKGroupSearch searchUsingSearchTerm:searchTerm 
serverInfo:serverInfo macroResolver:[self macroResolver] 
completionHandler:synchronous ? nil : handler];
         if (synchronous == NO)

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