Revision: 27274
          http://sourceforge.net/p/bibdesk/svn/27274
Author:   hofman
Date:     2022-03-07 10:11:36 +0000 (Mon, 07 Mar 2022)
Log Message:
-----------
Only set promised pasteboard data when the changeCount hasn't changed since the 
promise was written. Should never be an issue, as we should have had a delegate 
message to finish before this changes.

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

Modified: trunk/bibdesk/BDSKItemPasteboardHelper.m
===================================================================
--- trunk/bibdesk/BDSKItemPasteboardHelper.m    2022-03-07 07:30:26 UTC (rev 
27273)
+++ trunk/bibdesk/BDSKItemPasteboardHelper.m    2022-03-07 10:11:36 UTC (rev 
27274)
@@ -49,10 +49,12 @@
 
 - (BOOL)pasteboardIsValid:(NSPasteboard *)pboard;
 - (NSMutableArray *)promisedTypesForPasteboard:(NSPasteboard *)pboard;
+- (NSInteger)promisedChangeCountForPasteboard:(NSPasteboard *)pboard;
 - (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)setChangeCountForPasteboard:(NSPasteboard *)pboard;
 - (void)removePromisedType:(NSString *)type forPasteboard:(NSPasteboard 
*)pboard;
 - (void)removePromisedTypesForPasteboard:(NSPasteboard *)pboard;
 
@@ -66,10 +68,8 @@
 
 - (id)initWithDelegate:(id<BDSKItemPasteboardHelperDelegate>)aDelegate {
     if (self = [super init]) {
-        promisedPboardTypes = [[NSMutableDictionary alloc] initWithCapacity:2];
+        promisedPboardTypes = nil;
         delegate = aDelegate;
-        
-        [self retain]; // we should stay around as pboard owner
     }
     return self;
 }
@@ -87,9 +87,12 @@
             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] 
containsObject:BDSKPasteboardTypePublications]) {
+                if ([pboard changeCount] == [self 
promisedChangeCountForPasteboard:pboard])
+                    [self pasteboard:pboard item:pasteboardItemForType(pboard, 
BDSKPasteboardTypePublications) 
provideDataForType:BDSKPasteboardTypePublications];
+                else
+                    [self removePromisedType:BDSKPasteboardTypePublications 
forPasteboard:pboard];
+            }
             if ([[self promisedTypesForPasteboard:pboard] count]) {
                 // the remaining type needs a texTask
                 // get intermediate data so we don't need the delegate and the 
items
@@ -141,6 +144,8 @@
     
     if ([types count])
         [self setPromisedTypes:types items:items dragCopyType:dragCopyType 
forPasteboard:pboard];
+    else
+        [self removePromisedTypesForPasteboard:pboard];
     
     NSPasteboardItem *item = nil;
     BOOL needsWrite = NO;
@@ -161,6 +166,8 @@
         [item setDataProvider:self forTypes:types];
     if (needsWrite)
         [pboard writeObjects:[NSArray arrayWithObjects:item, nil]];
+    if ([types count])
+        [self setChangeCountForPasteboard:pboard];
 }
 
 #pragma mark NSPasteboardItemDataProvider protocol methods
@@ -272,13 +279,15 @@
 
 - (void)clearPromisedTypesForPasteboard:(NSPasteboard *)pboard {
     for (NSString *type in [[[self promisedTypesForPasteboard:pboard] copy] 
autorelease]) {
-        @try {
-            // can raise NSPasteboardCommunicationException
-            [pasteboardItemForType(pboard, type) setData:[NSData data] 
forType:type];
+        if ([pboard changeCount] == [self 
promisedChangeCountForPasteboard:pboard]) {
+            @try {
+                // can raise NSPasteboardCommunicationException
+                [pasteboardItemForType(pboard, type) setData:[NSData data] 
forType:type];
+            }
+            @catch(id exception) {
+                NSLog(@"ignoring exception %@ in -[%@ %@]", exception, [self 
class], NSStringFromSelector(_cmd));
+            }
         }
-        @catch(id exception) {
-            NSLog(@"ignoring exception %@ in -[%@ %@]", exception, [self 
class], NSStringFromSelector(_cmd));
-        }
     }
     [self removePromisedTypesForPasteboard:pboard];
 }
@@ -322,8 +331,12 @@
        return [self pasteboardIsValid:pboard] ? [[promisedPboardTypes 
objectForKey:[pboard name]] objectForKey:@"types"] : nil;
 }
 
+- (NSInteger)promisedChangeCountForPasteboard:(NSPasteboard *)pboard {
+    return [self pasteboardIsValid:pboard] ? [[[promisedPboardTypes 
objectForKey:[pboard name]] objectForKey:@"changeCount"] integerValue] : 0;
+}
+
 - (BDSKDragCopyType)promisedDragCopyTypeForPasteboard:(NSPasteboard *)pboard {
-       return [self pasteboardIsValid:pboard] ? [[[promisedPboardTypes 
objectForKey:[pboard name]] objectForKey:@"dragCopyType"] integerValue] : -1;
+    return [self pasteboardIsValid:pboard] ? [[[promisedPboardTypes 
objectForKey:[pboard name]] objectForKey:@"dragCopyType"] integerValue] : -1;
 }
 
 - (NSString *)promisedBibTeXStringForPasteboard:(NSPasteboard *)pboard {
@@ -336,7 +349,7 @@
 
 - (void)setPromisedTypes:(NSMutableArray *)types items:(NSArray *)items 
dragCopyType:(BDSKDragCopyType)dragCopyType forPasteboard:(NSPasteboard 
*)pboard {
     if ([self pasteboardIsValid:pboard]) {
-        NSMutableDictionary *dict = [NSMutableDictionary 
dictionaryWithObjectsAndKeys:types, @"types", [NSNumber 
numberWithInteger:dragCopyType], @"dragCopyType", nil];
+        NSMutableDictionary *dict = [NSMutableDictionary 
dictionaryWithObjectsAndKeys:types, @"types", [NSNumber 
numberWithInteger:dragCopyType], @"dragCopyType", [NSNumber 
numberWithInteger:[pboard changeCount]], @"changeCount", nil];
         if ([types containsObject:BDSKPasteboardTypePublications]) {
             [dict setObject:items forKey:@"items"];
         } else {
@@ -356,6 +369,11 @@
     }
 }
 
+- (void)setChangeCountForPasteboard:(NSPasteboard *)pboard {
+    if ([self pasteboardIsValid:pboard])
+        [[promisedPboardTypes objectForKey:[pboard name]] setObject:[NSNumber 
numberWithInteger:[pboard changeCount]] forKey:@"changeCount"];
+}
+
 - (void)removePromisedType:(NSString *)type forPasteboard:(NSPasteboard 
*)pboard {
        NSMutableArray *types = [self promisedTypesForPasteboard:pboard];
        [types removeObject:type];

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