Revision: 7727
          http://skim-app.svn.sourceforge.net/skim-app/?rev=7727&view=rev
Author:   hofman
Date:     2012-10-13 20:37:17 +0000 (Sat, 13 Oct 2012)
Log Message:
-----------
Allow reading URLs from old style pasteboard types NSURLPboardType and 
NSFilenamesPboardType, because apps that don't use the new API to write URLs or 
filenames don't write UTIs.

Modified Paths:
--------------
    trunk/NSURL_SKExtensions.h
    trunk/NSURL_SKExtensions.m
    trunk/SKBookmarkController.m
    trunk/SKDocumentController.m
    trunk/SKDownloadController.m

Modified: trunk/NSURL_SKExtensions.h
===================================================================
--- trunk/NSURL_SKExtensions.h  2012-10-12 13:16:32 UTC (rev 7726)
+++ trunk/NSURL_SKExtensions.h  2012-10-13 20:37:17 UTC (rev 7727)
@@ -41,18 +41,14 @@
 
 @interface NSURL (SKExtensions)
 
++ (BOOL)canReadURLFromPasteboard:(NSPasteboard *)pboard;
++ (NSArray *)readURLsFromPasteboard:(NSPasteboard *)pboard;
++ (BOOL)canReadFileURLFromPasteboard:(NSPasteboard *)pboard;
++ (NSArray *)readFileURLsFromPasteboard:(NSPasteboard *)pboard;
+
 - (NSString *)pathReplacingPathExtension:(NSString *)ext;
 
 - (NSAttributedString *)icon;
 - (NSAttributedString *)smallIcon;
 
 @end
-
-#pragma mark -
-
-// Dummy subclass for reading from pasteboard
-// Reads public.url before public.file-url, unlike NSURL, so it gets the 
target URL for webloc/fileloc files rather than its file location
-// Also tries to interpret a plain string as a URL
-// Don't use for anything else
-@interface SKURL : NSURL <NSPasteboardReading>
-@end

Modified: trunk/NSURL_SKExtensions.m
===================================================================
--- trunk/NSURL_SKExtensions.m  2012-10-12 13:16:32 UTC (rev 7726)
+++ trunk/NSURL_SKExtensions.m  2012-10-13 20:37:17 UTC (rev 7727)
@@ -39,7 +39,15 @@
 #import "NSURL_SKExtensions.h"
 #import "SKRuntime.h"
 
+// Dummy subclass for reading from pasteboard
+// Reads public.url before public.file-url, unlike NSURL, so it gets the 
target URL for webloc/fileloc files rather than its file location
+// Also tries to interpret a plain string as a URL
+// Don't use for anything else
+@interface SKURL : NSURL
+@end
 
+#pragma mark -
+
 @implementation NSURL (SKExtensions)
 
 static id (*original_initFileURLWithPath)(id, SEL, id) = NULL;
@@ -58,6 +66,49 @@
     original_initWithString = (id (*)(id, SEL, 
id))SKReplaceInstanceMethodImplementationFromSelector(self, 
@selector(initWithString:), @selector(replacement_initWithString:));
 }
 
++ (BOOL)canReadURLFromPasteboard:(NSPasteboard *)pboard {
+    return [pboard canReadObjectForClasses:[NSArray arrayWithObject:[SKURL 
class]] options:[NSDictionary dictionary]] ||
+           nil != [pboard availableTypeFromArray:[NSArray 
arrayWithObjects:NSURLPboardType, NSFilenamesPboardType, nil]];
+}
+
++ (NSArray *)readURLsFromPasteboard:(NSPasteboard *)pboard {
+    NSArray *URLs = [pboard readObjectsForClasses:[NSArray 
arrayWithObject:[SKURL class]] options:[NSDictionary dictionary]];
+    if ([URLs count] == 0) {
+        NSString *type = [pboard availableTypeFromArray:[NSArray 
arrayWithObjects:NSURLPboardType, NSFilenamesPboardType, nil]];
+        if ([type isEqualToString:NSURLPboardType]) {
+            URLs = [NSArray arrayWithObjects:[NSURL URLFromPasteboard:self], 
nil];
+        } else if ([type isEqualToString:NSFilenamesPboardType]) {
+            NSArray *filenames = [pboard 
propertyListForType:NSFilenamesPboardType];
+            if ([filenames count]  > 0) {
+                NSMutableArray *files = [NSMutableArray array];
+                for (NSString *filename in filenames)
+                    [files addObject:[NSURL fileURLWithPath:[filename 
stringByExpandingTildeInPath]]];
+                URLs = files;
+            }
+        }
+    }
+    return URLs;
+}
+
++ (BOOL)canReadFileURLFromPasteboard:(NSPasteboard *)pboard {
+    return [pboard canReadObjectForClasses:[NSArray arrayWithObject:[NSURL 
class]] options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber 
numberWithBool:YES], NSPasteboardURLReadingFileURLsOnlyKey, nil]] ||
+           nil != [pboard availableTypeFromArray:[NSArray 
arrayWithObjects:NSFilenamesPboardType, nil]];
+}
+
++ (NSArray *)readFileURLsFromPasteboard:(NSPasteboard *)pboard {
+    NSArray *fileURLs = [pboard readObjectsForClasses:[NSArray 
arrayWithObject:[NSURL class]] options:[NSDictionary 
dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], 
NSPasteboardURLReadingFileURLsOnlyKey, nil]];
+    if ([fileURLs count] == 0 && [[pboard types] 
containsObject:NSFilenamesPboardType]) {
+        NSArray *filenames = [pboard 
propertyListForType:NSFilenamesPboardType];
+        if ([filenames count]  > 0) {
+            NSMutableArray *files = [NSMutableArray array];
+            for (NSString *filename in filenames)
+                [files addObject:[NSURL fileURLWithPath:[filename 
stringByExpandingTildeInPath]]];
+            fileURLs = files;
+        }
+    }
+    return fileURLs;
+}
+
 - (NSString *)pathReplacingPathExtension:(NSString *)ext {
     return [[[self path] stringByDeletingPathExtension] 
stringByAppendingPathExtension:ext];
 }
@@ -121,7 +172,6 @@
     return NSPasteboardReadingAsData;
 }
 
-
 - (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type {
     [self release];
     self = nil;

Modified: trunk/SKBookmarkController.m
===================================================================
--- trunk/SKBookmarkController.m        2012-10-12 13:16:32 UTC (rev 7726)
+++ trunk/SKBookmarkController.m        2012-10-13 20:37:17 UTC (rev 7727)
@@ -197,7 +197,7 @@
     
     [outlineView setTypeSelectHelper:[SKTypeSelectHelper typeSelectHelper]];
     
-    [outlineView registerForDraggedTypes:[NSArray 
arrayWithObjects:SKPasteboardTypeBookmarkRows, (NSString *)kUTTypeFileURL, 
nil]];
+    [outlineView registerForDraggedTypes:[NSArray 
arrayWithObjects:SKPasteboardTypeBookmarkRows, (NSString *)kUTTypeFileURL, 
NSFilenamesPboardType, nil]];
     
     [outlineView setDoubleAction:@selector(doubleClickBookmark:)];
 }
@@ -629,7 +629,7 @@
         if ([pboard canReadItemWithDataConformingToTypes:[NSArray 
arrayWithObjects:SKPasteboardTypeBookmarkRows, nil]] &&
             [info draggingSource] == ov)
             dragOp = NSDragOperationMove;
-        else if ([pboard canReadObjectForClasses:[NSArray 
arrayWithObject:[NSURL class]] options:[NSDictionary 
dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], 
NSPasteboardURLReadingFileURLsOnlyKey, nil]])
+        else if ([NSURL canReadFileURLFromPasteboard:pboard])
             dragOp = NSDragOperationEvery;
     }
     return dragOp;
@@ -669,7 +669,7 @@
         
         return YES;
     } else {
-        NSArray *urls = [pboard readObjectsForClasses:[NSArray 
arrayWithObject:[NSURL class]] options:[NSDictionary 
dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], 
NSPasteboardURLReadingFileURLsOnlyKey, nil]];
+        NSArray *urls = [NSURL readFileURLsFromPasteboard:pboard];
         NSArray *newBookmarks = [SKBookmark bookmarksForPaths:[urls 
valueForKey:@"path"] relativeToPath:nil];
         if ([newBookmarks count] > 0) {
             [self endEditing];
@@ -757,7 +757,7 @@
 }
 
 - (void)outlineView:(NSOutlineView *)ov pasteFromPasteboard:(NSPasteboard 
*)pboard {
-    NSArray *urls = [pboard readObjectsForClasses:[NSArray 
arrayWithObject:[NSURL class]] options:[NSDictionary 
dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], 
NSPasteboardURLReadingFileURLsOnlyKey, nil]];
+    NSArray *urls = [NSURL readFileURLsFromPasteboard:pboard];
     if ([urls count] > 0) {
         NSArray *newBookmarks = [SKBookmark bookmarksForPaths:[urls 
valueForKey:@"path"] relativeToPath:nil];
         if ([newBookmarks count] > 0) {
@@ -776,7 +776,7 @@
 }
 
 - (BOOL)outlineView:(NSOutlineView *)ov canPasteFromPasteboard:(NSPasteboard 
*)pboard {
-    return [pboard canReadObjectForClasses:[NSArray arrayWithObject:[NSURL 
class]] options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber 
numberWithBool:YES], NSPasteboardURLReadingFileURLsOnlyKey, nil]];
+    return [NSURL canReadFileURLFromPasteboard:pboard];
 }
 
 - (NSArray *)outlineView:(NSOutlineView *)ov 
typeSelectHelperSelectionStrings:(SKTypeSelectHelper *)typeSelectHelper {

Modified: trunk/SKDocumentController.m
===================================================================
--- trunk/SKDocumentController.m        2012-10-12 13:16:32 UTC (rev 7726)
+++ trunk/SKDocumentController.m        2012-10-13 20:37:17 UTC (rev 7727)
@@ -249,7 +249,7 @@
 }
 
 - (id)openDocumentWithURLFromPasteboard:(NSPasteboard *)pboard 
showNotes:(BOOL)showNotes error:(NSError **)outError {
-    NSArray *theURLs = [pboard readObjectsForClasses:[NSArray 
arrayWithObject:[SKURL class]] options:[NSDictionary dictionary]];
+    NSArray *theURLs = [NSURL readURLsFromPasteboard:pboard];
     NSURL *theURL = [theURLs count] > 0 ? [theURLs objectAtIndex:0] : nil;
     id document = nil;
     
@@ -414,7 +414,9 @@
 
 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem {
     if ([anItem action] == @selector(newDocumentFromClipboard:)) {
-        return [[NSPasteboard generalPasteboard] 
canReadObjectForClasses:[NSArray arrayWithObjects:[NSImage class], [SKURL 
class], nil] options:[NSDictionary dictionary]];
+        NSPasteboard *pboard = [NSPasteboard generalPasteboard];
+        return [pboard canReadObjectForClasses:[NSArray 
arrayWithObject:[NSImage class]] options:[NSDictionary dictionary]] ||
+               [NSURL canReadURLFromPasteboard:pboard];
     } else if ([[SKDocumentController superclass] 
instancesRespondToSelector:_cmd]) {
         return [super validateUserInterfaceItem:anItem];
     } else

Modified: trunk/SKDownloadController.m
===================================================================
--- trunk/SKDownloadController.m        2012-10-12 13:16:32 UTC (rev 7726)
+++ trunk/SKDownloadController.m        2012-10-13 20:37:17 UTC (rev 7727)
@@ -112,7 +112,7 @@
     
     [tableView setTypeSelectHelper:[SKTypeSelectHelper typeSelectHelper]];
     
-    [tableView registerForDraggedTypes:[SKURL 
readableTypesForPasteboard:[NSPasteboard pasteboardWithName:NSDragPboard]]];
+    [tableView registerForDraggedTypes:[NSArray arrayWithObjects:(NSString 
*)kUTTypeURL, (NSString *)kUTTypeFileURL, NSURLPboardType, 
NSFilenamesPboardType, nil]];
 }
 
 - (SKDownload *)addDownloadForURL:(NSURL *)aURL showWindow:(BOOL)flag {
@@ -135,7 +135,7 @@
 
 - (BOOL)pasteFromPasteboard:(NSPasteboard *)pboard {
     BOOL success = NO;
-    NSArray *theURLs = [pboard readObjectsForClasses:[NSArray 
arrayWithObject:[SKURL class]] options:[NSDictionary dictionary]];
+    NSArray *theURLs = [NSURL readURLsFromPasteboard:pboard];
     for (NSURL *theURL in theURLs) {
         if ([theURL isFileURL]) {
             if ([[NSDocumentController sharedDocumentController] 
openDocumentWithContentsOfURL:theURL display:YES error:NULL])
@@ -375,7 +375,7 @@
 
 - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id 
<NSDraggingInfo>)info proposedRow:(NSInteger)row 
proposedDropOperation:(NSTableViewDropOperation)op {
     NSPasteboard *pboard = [info draggingPasteboard];
-    if ([pboard canReadObjectForClasses:[NSArray arrayWithObject:[SKURL 
class]] options:[NSDictionary dictionary]]) {
+    if ([NSURL canReadURLFromPasteboard:pboard]) {
         [tv setDropRow:-1 dropOperation:NSTableViewDropOn];
         return NSDragOperationEvery;
     }
@@ -453,7 +453,7 @@
 }
 
 - (BOOL)tableView:(NSTableView *)tv canPasteFromPasteboard:(NSPasteboard 
*)pboard {
-    return [pboard canReadObjectForClasses:[NSArray arrayWithObject:[SKURL 
class]] options:[NSDictionary dictionary]];
+    return [NSURL canReadURLFromPasteboard:pboard];
 }
 
 - (NSArray *)tableView:(NSTableView *)aTableView 
typeSelectHelperSelectionStrings:(SKTypeSelectHelper *)typeSelectHelper {

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


------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to