Revision: 27270
          http://sourceforge.net/p/bibdesk/svn/27270
Author:   hofman
Date:     2022-03-06 19:41:55 +0000 (Sun, 06 Mar 2022)
Log Message:
-----------
Promise pboard data when copying URLs. Remember partially parsed items 
immediately rather than converting afterwards.

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

Modified: trunk/bibdesk/BDSKItemPasteboardHelper.h
===================================================================
--- trunk/bibdesk/BDSKItemPasteboardHelper.h    2022-03-06 07:30:17 UTC (rev 
27269)
+++ trunk/bibdesk/BDSKItemPasteboardHelper.h    2022-03-06 19:41:55 UTC (rev 
27270)
@@ -57,10 +57,9 @@
 
 - (void)terminate;
 
-- (void)writeItems:(NSArray *)items textRepresentation:(id)text 
forDragCopyType:(BDSKDragCopyType)dragCopyType toPasteboard:(NSPasteboard 
*)pboard;
+- (void)writeItems:(NSArray *)items textRepresentation:(id)text 
forDragCopyType:(BDSKDragCopyType)dragCopyType toPasteboard:(NSPasteboard 
*)pboard converted:(BOOL)converted;
 
 - (NSArray *)promisedItemsForPasteboard:(NSPasteboard *)pboard;
 - (void)clearPromisedTypesForPasteboard:(NSPasteboard *)pboard;
-- (void)clearPromisedItemsForPasteboard:(NSPasteboard *)pboard;
 
 @end

Modified: trunk/bibdesk/BDSKItemPasteboardHelper.m
===================================================================
--- trunk/bibdesk/BDSKItemPasteboardHelper.m    2022-03-06 07:30:17 UTC (rev 
27269)
+++ trunk/bibdesk/BDSKItemPasteboardHelper.m    2022-03-06 19:41:55 UTC (rev 
27270)
@@ -52,7 +52,7 @@
 - (BDSKDragCopyType)promisedDragCopyTypeForPasteboard:(NSPasteboard *)pboard;
 - (NSString *)promisedBibTeXStringForPasteboard:(NSPasteboard *)pboard;
 - (NSArray *)promisedCiteKeysForPasteboard:(NSPasteboard *)pboard;
-- (void)setPromisedTypes:(NSMutableArray *)types items:(NSArray *)items 
dragCopyType:(BDSKDragCopyType)dragCopyType forPasteboard:(NSPasteboard 
*)pboard;
+- (void)setPromisedTypes:(NSMutableArray *)types items:(NSArray *)items 
dragCopyType:(BDSKDragCopyType)dragCopyType forPasteboard:(NSPasteboard 
*)pboard converted:(BOOL)converted;
 - (void)removePromisedType:(NSString *)type forPasteboard:(NSPasteboard 
*)pboard;
 - (void)removePromisedTypesForPasteboard:(NSPasteboard *)pboard;
 
@@ -82,8 +82,30 @@
 
 - (void)terminate {
     if (delegate) {
-        for (NSString *name in [promisedPboardTypes allKeys])
-            [self clearPromisedItemsForPasteboard:[NSPasteboard 
pasteboardWithName:name]];
+        for (NSString *name in [promisedPboardTypes allKeys]) {
+            NSPasteboard *pboard = [NSPasteboard pasteboardWithName:name];
+            if (pboard == nil) continue;
+            NSArray *items = [self promisedItemsForPasteboard:pboard];
+            if (items == nil) continue;
+            if ([[self promisedTypesForPasteboard:pboard] 
containsObject:BDSKPasteboardTypePublications])
+                [self pasteboard:pboard item:pasteboardItemForType(pboard, 
BDSKPasteboardTypePublications) 
provideDataForType:BDSKPasteboardTypePublications];
+            
+            if ([[self promisedTypesForPasteboard:pboard] count]) {
+                // the remaining type needs a texTask
+                // get intermediate data so we don't need the delegate and the 
items
+                NSString *bibString = nil;
+                if (items != nil)
+                    bibString = [delegate pasteboardHelper:self 
bibTeXStringForItems:items];
+                if (bibString != nil) {
+                    NSMutableDictionary *dict = [promisedPboardTypes 
objectForKey:[pboard name]];
+                    [dict setObject:bibString forKey:@"bibTeXString"];
+                    [dict setObject:[items valueForKey:@"citeKey"] 
forKey:@"citeKeys"];
+                    [dict removeObjectForKey:@"items"];
+                } else {
+                    [self clearPromisedTypesForPasteboard:pboard];
+                }
+            }
+        }
     }
     delegate = nil;
 }
@@ -90,10 +112,13 @@
 
 #pragma mark Promising and adding data
 
-- (void)writeItems:(NSArray *)items textRepresentation:(id)text 
forDragCopyType:(BDSKDragCopyType)dragCopyType toPasteboard:(NSPasteboard 
*)pboard {
+- (void)writeItems:(NSArray *)items textRepresentation:(id)text 
forDragCopyType:(BDSKDragCopyType)dragCopyType toPasteboard:(NSPasteboard 
*)pboard converted:(BOOL)converted {
     
-    NSMutableArray *types = [NSMutableArray 
arrayWithObjects:BDSKPasteboardTypePublications, nil];
+    NSMutableArray *types = [NSMutableArray array];
     
+    if (converted == NO)
+        [types addObject:BDSKPasteboardTypePublications];
+    
     switch (dragCopyType) {
         case BDSKDragCopyPDF:
             [types addObject:NSPasteboardTypePDF];
@@ -113,9 +138,11 @@
     
     BDSKASSERT([promisedPboardTypes objectForKey:[pboard name]] == nil);
     
-    [self setPromisedTypes:types items:items dragCopyType:dragCopyType 
forPasteboard:pboard];
+    if ([types count])
+        [self setPromisedTypes:types items:items dragCopyType:dragCopyType 
forPasteboard:pboard converted:converted];
     
     NSPasteboardItem *item = nil;
+    BOOL needsWrite = NO;
     
     if (dragCopyType == BDSKDragCopyURL) {
         [pboard writeURLs:[items valueForKey:@"bdskURL"] names:[items 
valueForKey:@"citeKey"]];
@@ -123,11 +150,17 @@
     }
     if (item == nil) {
         item = [[[NSPasteboardItem alloc] init] autorelease];
-        if (text)
-            [item setString:text forType:NSPasteboardTypeString];
+        needsWrite = YES;
+    }
+    if (text)
+        [item setString:text forType:NSPasteboardTypeString];
+    if ([types count]) {
+        if (converted)
+            [item setData:[BibItem archivedPublications:items] 
forType:BDSKPasteboardTypePublications];
         [item setDataProvider:self forTypes:types];
+    }
+    if (needsWrite)
         [pboard writeObjects:[NSArray arrayWithObjects:item, nil]];
-    }
 }
 
 #pragma mark NSPasteboardItemDataProvider protocol methods
@@ -250,30 +283,6 @@
     [self removePromisedTypesForPasteboard:pboard];
 }
 
-- (void)clearPromisedItemsForPasteboard:(NSPasteboard *)pboard {
-    NSArray *items = [self promisedItemsForPasteboard:pboard];
-    if (items) {
-        if ([[self promisedTypesForPasteboard:pboard] 
containsObject:BDSKPasteboardTypePublications])
-            [self pasteboard:pboard item:pasteboardItemForType(pboard, 
BDSKPasteboardTypePublications) 
provideDataForType:BDSKPasteboardTypePublications];
-        
-        if ([[self promisedTypesForPasteboard:pboard] count]) {
-            // the remaining type needs a texTask
-            // get intermediate data so we don't need the delegate and the 
items
-            NSString *bibString = nil;
-            if (items != nil)
-                bibString = [delegate pasteboardHelper:self 
bibTeXStringForItems:items];
-            if (bibString != nil) {
-                NSMutableDictionary *dict = [promisedPboardTypes 
objectForKey:[pboard name]];
-                [dict setObject:bibString forKey:@"bibTeXString"];
-                [dict setObject:[items valueForKey:@"citeKey"] 
forKey:@"citeKeys"];
-                [dict removeObjectForKey:@"items"];
-            } else {
-                [self clearPromisedTypesForPasteboard:pboard];
-            }
-        }
-    }
-}
-
 @end
 
 
@@ -325,9 +334,18 @@
        return [self pasteboardIsValid:pboard] ? [[promisedPboardTypes 
objectForKey:[pboard name]] objectForKey:@"citeKeys"] : nil;
 }
 
-- (void)setPromisedTypes:(NSMutableArray *)types items:(NSArray *)items 
dragCopyType:(BDSKDragCopyType)dragCopyType forPasteboard:(NSPasteboard 
*)pboard {
+- (void)setPromisedTypes:(NSMutableArray *)types items:(NSArray *)items 
dragCopyType:(BDSKDragCopyType)dragCopyType forPasteboard:(NSPasteboard 
*)pboard converted:(BOOL)converted {
     if ([self pasteboardIsValid:pboard]) {
-        NSMutableDictionary *dict = [NSMutableDictionary 
dictionaryWithObjectsAndKeys:items, @"items", types, @"types", [NSNumber 
numberWithInteger:dragCopyType], @"dragCopyType", nil];
+        NSMutableDictionary *dict = [NSMutableDictionary 
dictionaryWithObjectsAndKeys:types, @"types", [NSNumber 
numberWithInteger:dragCopyType], @"dragCopyType", nil];
+        if (converted == NO) {
+            NSString *bibString = [delegate pasteboardHelper:self 
bibTeXStringForItems:items];
+            if (bibString != nil) {
+                [dict setObject:bibString forKey:@"bibTeXString"];
+                [dict setObject:[items valueForKey:@"citeKey"] 
forKey:@"citeKeys"];
+            }
+        } else {
+            [dict setObject:items forKey:@"items"];
+        }
         if (promisedPboardTypes == nil) {
             promisedPboardTypes = [[NSMutableDictionary alloc] 
initWithCapacity:2];
             // we should stay around as pboard owner until all types are 
delivered or cleared
@@ -352,7 +370,7 @@
             [promisedPboardTypes release];
             promisedPboardTypes = nil;
             // we used promisedPboardTypes != nil to signal we retained 
ourselves
-            // see setPromisedTypes:items:dragCopyType:forPasteboard:
+            // see setPromisedTypes:items:dragCopyType:forPasteboard:converted:
             [self autorelease];
         }
     }

Modified: trunk/bibdesk/BibDocument.m
===================================================================
--- trunk/bibdesk/BibDocument.m 2022-03-06 07:30:17 UTC (rev 27269)
+++ trunk/bibdesk/BibDocument.m 2022-03-06 19:41:55 UTC (rev 27270)
@@ -1591,7 +1591,7 @@
     NSPasteboard *pboard = [NSPasteboard pasteboardWithUniqueName];
     if (pboardHelper == nil)
         pboardHelper = [[BDSKItemPasteboardHelper alloc] 
initWithDelegate:self];
-    [pboardHelper writeItems:[self publicationsForSaving] 
textRepresentation:nil forDragCopyType:BDSKDragCopyLTB toPasteboard:pboard];
+    [pboardHelper writeItems:[self publicationsForSaving] 
textRepresentation:nil forDragCopyType:BDSKDragCopyLTB toPasteboard:pboard 
converted:NO];
     NSArray *ltbStrings = [pboard readObjectsForClasses:[NSArray 
arrayWithObject:[NSString class]] options:[NSDictionary dictionary]];
     [pboard clearContents]; // this will clear the pasteboard helper for this 
pasteboard
     [pboardHelper clearPromisedTypesForPasteboard:pboard]; // just to be sure

Modified: trunk/bibdesk/BibDocument_DataSource.m
===================================================================
--- trunk/bibdesk/BibDocument_DataSource.m      2022-03-06 07:30:17 UTC (rev 
27269)
+++ trunk/bibdesk/BibDocument_DataSource.m      2022-03-06 19:41:55 UTC (rev 
27270)
@@ -450,11 +450,8 @@
     
     if (pboardHelper == nil)
         pboardHelper = [[BDSKItemPasteboardHelper alloc] 
initWithDelegate:self];
-    [pboardHelper writeItems:pubs textRepresentation:text 
forDragCopyType:dragCopyType toPasteboard:pboard];
+    [pboardHelper writeItems:pubs textRepresentation:text 
forDragCopyType:dragCopyType toPasteboard:pboard converted:pboard == 
[NSPasteboard generalPasteboard]];
     
-    if (pboard == [NSPasteboard generalPasteboard])
-        [pboardHelper clearPromisedItemsForPasteboard:pboard];
-    
     return YES;
 }
 

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