Revision: 28226
          http://sourceforge.net/p/bibdesk/svn/28226
Author:   hofman
Date:     2023-04-19 21:42:29 +0000 (Wed, 19 Apr 2023)
Log Message:
-----------
Run texTask for pboard async. Start on copy for clipboard. Run runloop when 
getting the data from a texTask if it was not yet (fully) generated.

Modified Paths:
--------------
    trunk/bibdesk/BDSKItemPasteboardHelper.h
    trunk/bibdesk/BDSKItemPasteboardHelper.m

Modified: trunk/bibdesk/BDSKItemPasteboardHelper.h
===================================================================
--- trunk/bibdesk/BDSKItemPasteboardHelper.h    2023-04-19 17:03:48 UTC (rev 
28225)
+++ trunk/bibdesk/BDSKItemPasteboardHelper.h    2023-04-19 21:42:29 UTC (rev 
28226)
@@ -50,6 +50,7 @@
 
 @interface BDSKItemPasteboardHelper : NSObject <NSPasteboardItemDataProvider> {
     NSMutableDictionary *promisedPboardTypes;
+    NSMutableDictionary *texTasks;
     id<BDSKItemPasteboardHelperDelegate> delegate;
 }
 

Modified: trunk/bibdesk/BDSKItemPasteboardHelper.m
===================================================================
--- trunk/bibdesk/BDSKItemPasteboardHelper.m    2023-04-19 17:03:48 UTC (rev 
28225)
+++ trunk/bibdesk/BDSKItemPasteboardHelper.m    2023-04-19 21:42:29 UTC (rev 
28226)
@@ -49,14 +49,16 @@
 #define ARCHIVEDDATA_KEY @"archivedData"
 #define BIBTEXSTRING_KEY @"bibTeXString"
 #define CITEKEYS_KEY @"citeKeys"
+#define TEXTASKRESULT_KEY @"texTaskResult"
 
 
-@interface BDSKItemPasteboardHelper (Private)
+@interface BDSKItemPasteboardHelper (Private) <BDSKTeXTaskDelegate>
 
 - (BOOL)pasteboardIsValid:(NSPasteboard *)pboard;
 - (void)setPromisedTypes:(NSMutableArray *)types items:(NSArray *)items 
dragCopyType:(BDSKDragCopyType)dragCopyType forPasteboard:(NSPasteboard 
*)pboard;
 - (void)removePromisedType:(NSString *)type forPasteboard:(NSPasteboard 
*)pboard;
 - (void)removePromisedTypesForPasteboard:(NSPasteboard *)pboard;
+- (void)startTeXTaskForPasteboard:(NSPasteboard *)pboard;
 
 @end
 
@@ -66,7 +68,9 @@
 
 - (void)dealloc {
     // we should have already cleared everything in the final call to 
removePromisedTypesForPasteboard:
+    [[texTasks allValues] makeObjectsPerformSelector:@selector(terminate)];
     BDSKDESTROY(promisedPboardTypes);
+    BDSKDESTROY(texTasks);
     [super dealloc];
 }
 
@@ -164,7 +168,8 @@
     
     NSData *data = nil;
     NSString *string = nil;
-    NSMutableDictionary *dict = [self pasteboardIsValid:pboard] ? 
[promisedPboardTypes objectForKey:[pboard name]] : nil;
+    NSString *pbName = [pboard name];
+    NSMutableDictionary *dict = [self pasteboardIsValid:pboard] ? 
[promisedPboardTypes objectForKey:pbName] : nil;
     NSArray *items = [dict objectForKey:ITEMS_KEY];
     
     if ([type isEqualToString:BDSKPasteboardTypePublications]) {
@@ -174,46 +179,39 @@
             data = [dict objectForKey:ARCHIVEDDATA_KEY];
         }
     } else {
-        NSString *bibString = nil;
-        NSArray *citeKeys = nil;
+        id result = [dict objectForKey:TEXTASKRESULT_KEY];
         
-        if (items != nil) {
-            bibString = [delegate pasteboardHelper:self 
bibTeXStringForItems:items];
-            citeKeys = [items valueForKey:@"citeKey"];
-        } else {
-            bibString = [dict objectForKey:BIBTEXSTRING_KEY];
-            citeKeys = [dict objectForKey:CITEKEYS_KEY];
-        }
-        if (bibString != nil) {
-            BDSKDragCopyType dragCopyType = [[dict 
objectForKey:DRAGCOPYTYPE_KEY] integerValue];
-            BDSKTeXTask *texTask = [[BDSKTeXTask alloc] 
initWithFileName:@"bibcopy" synchronous:YES];
+        if (result == nil) {
+            if ([texTasks objectForKey:pbName] == nil)
+                [self startTeXTaskForPasteboard:pboard];
             
             if ([delegate 
respondsToSelector:@selector(pasteboardHelperWillBeginGenerating:)])
                 [delegate pasteboardHelperWillBeginGenerating:self];
             
-            if ([texTask runWithBibTeXString:bibString citeKeys:citeKeys 
generatedType:generatedTypeForDragCopyType(dragCopyType)]) {
-                if (dragCopyType == BDSKDragCopyLaTeX || dragCopyType == 
BDSKDragCopyLTB) {
-                    BDSKASSERT([type isEqualToString:NSPasteboardTypeString]);
-                    string = [texTask laTeXString];
-                } else {
-                    data = [texTask PDFData];
-                    if (dragCopyType == BDSKDragCopyPDF){
-                        BDSKASSERT([type isEqualToString:NSPasteboardTypePDF]);
-                    } else if (data) {
-                        BDSKASSERT([type isEqualToString:NSPasteboardTypeRTF]);
-                        PDFDocument *pdfDoc = [[PDFDocument alloc] 
initWithData:data];
-                        NSAttributedString *attrString = [pdfDoc 
attributedString];
-                        data = [attrString RTFFromRange:NSMakeRange(0, 
[attrString length]) documentAttributes:[NSDictionary 
dictionaryWithObjectsAndKeys:NSRTFTextDocumentType, 
NSDocumentTypeDocumentAttribute, nil]];
-                        [pdfDoc release];
-                    }
+            NSDate *start = [[NSDate alloc] init];
+            while ([texTasks objectForKey:pbName]) {
+                NSDate *next = [[NSDate alloc] 
initWithTimeIntervalSinceNow:0.1];
+                if ([next timeIntervalSinceDate:start] > 60.0) {
+                    [next release];
+                    break;
                 }
+                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
beforeDate:next];
+                [next release];
             }
-
-            [texTask release];
-
+            [start release];
+            
+            // dict may become invalid in the meantime, so get it again
+            dict = [self pasteboardIsValid:pboard] ? [promisedPboardTypes 
objectForKey:pbName] : nil;
+            result = [dict objectForKey:TEXTASKRESULT_KEY];
+            
             if ([delegate 
respondsToSelector:@selector(pasteboardHelperDidEndGenerating:)])
                 [delegate pasteboardHelperDidEndGenerating:self];
         }
+        
+        if ([result isKindOfClass:[NSString class]])
+            string = result;
+        else
+            data = result;
     }
     
     if (string) {
@@ -288,12 +286,15 @@
             [self retain];
         }
         [promisedPboardTypes setObject:dict forKey:[pboard name]];
+        if ([types count] > 1 && pboard == [NSPasteboard generalPasteboard])
+            [self startTeXTaskForPasteboard:pboard];
     }
 }
 
 - (void)removePromisedType:(NSString *)type forPasteboard:(NSPasteboard 
*)pboard {
     if ([self pasteboardIsValid:pboard]) {
-        NSMutableDictionary *dict = [promisedPboardTypes objectForKey:[pboard 
name]];
+        NSString *pbName = [pboard name];
+        NSMutableDictionary *dict = [promisedPboardTypes objectForKey:pbName];
         if (dict) {
             NSMutableArray *types = [dict objectForKey:TYPES_KEY];
             [types removeObject:type];
@@ -300,6 +301,12 @@
             if ([type isEqualToString:BDSKPasteboardTypePublications]) {
                 [dict removeObjectForKey:ARCHIVEDDATA_KEY];
             } else {
+                BDSKTeXTask *texTask = [texTasks objectForKey:pbName];
+                if (texTask) {
+                    [texTask terminate];
+                    [texTasks removeObjectForKey:pbName];
+                }
+                [dict removeObjectForKey:TEXTASKRESULT_KEY];
                 [dict removeObjectForKey:BIBTEXSTRING_KEY];
                 [dict removeObjectForKey:CITEKEYS_KEY];
             }
@@ -311,7 +318,13 @@
 
 - (void)removePromisedTypesForPasteboard:(NSPasteboard *)pboard {
     if ([self pasteboardIsValid:pboard]) {
-        [promisedPboardTypes removeObjectForKey:[pboard name]];
+        NSString *pbName = [pboard name];
+        BDSKTeXTask *texTask = [texTasks objectForKey:pbName];
+        if (texTask) {
+            [texTask terminate];
+            [texTasks removeObjectForKey:pbName];
+        }
+        [promisedPboardTypes removeObjectForKey:pbName];
         if ([promisedPboardTypes count] == 0 && promisedPboardTypes != nil) {
             [promisedPboardTypes release];
             promisedPboardTypes = nil;
@@ -322,5 +335,55 @@
     }
 }
 
+- (void)startTeXTaskForPasteboard:(NSPasteboard *)pboard {
+    NSDictionary *dict = [promisedPboardTypes objectForKey:[pboard name]];
+    BDSKDragCopyType dragCopyType = [[dict objectForKey:DRAGCOPYTYPE_KEY] 
integerValue];
+    NSString *bibString = [dict objectForKey:BIBTEXSTRING_KEY];
+    NSArray *citeKeys = [dict objectForKey:CITEKEYS_KEY];
+    
+    if (bibString == nil && citeKeys == nil) {
+        NSArray *items = [dict objectForKey:ITEMS_KEY];
+        if (items != nil) {
+            bibString = [delegate pasteboardHelper:self 
bibTeXStringForItems:items];
+            citeKeys = [items valueForKey:@"citeKey"];
+        }
+    }
+    
+    BDSKTeXTask *texTask = [[BDSKTeXTask alloc] initWithFileName:@"bibcopy" 
synchronous:NO];
+    [texTask setDelegate:self];
+    
+    if (texTasks == nil)
+        texTasks = [[NSMutableDictionary alloc] init];
+    [texTasks setObject:texTask forKey:[pboard name]];
+    
+    [texTask runWithBibTeXString:bibString citeKeys:citeKeys 
generatedType:generatedTypeForDragCopyType(dragCopyType)];
+    
+    [texTask release];
+}
+
+- (void)texTask:(BDSKTeXTask *)texTask finishedWithResult:(BOOL)success {
+    NSString *pbName = [[texTasks allKeysForObject:texTask] firstObject];
+    if (pbName) {
+        NSMutableDictionary *dict = [promisedPboardTypes objectForKey:pbName];
+        BDSKDragCopyType dragCopyType = [[dict objectForKey:DRAGCOPYTYPE_KEY] 
integerValue];
+        id result = nil;
+        if (dragCopyType == BDSKDragCopyLaTeX || dragCopyType == 
BDSKDragCopyLTB) {
+            result = [texTask laTeXString];
+        } else {
+            result = [texTask PDFData];
+            if (dragCopyType == BDSKDragCopyRTF && result) {
+                PDFDocument *pdfDoc = [[PDFDocument alloc] 
initWithData:result];
+                NSAttributedString *attrString = [pdfDoc attributedString];
+                result = [attrString RTFFromRange:NSMakeRange(0, [attrString 
length]) documentAttributes:[NSDictionary 
dictionaryWithObjectsAndKeys:NSRTFTextDocumentType, 
NSDocumentTypeDocumentAttribute, nil]];
+                [pdfDoc release];
+            }
+        }
+        [dict setObject:result ?: [NSData data] forKey:TEXTASKRESULT_KEY];
+        [texTasks removeObjectForKey:pbName];
+    } else {
+        [texTask terminate];
+    }
+}
+
 @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