From 0bf96934319c5ba20a315670a2b05428016ec20a Mon Sep 17 00:00:00 2001 From: Fred Kiefer Date: Wed, 8 Feb 2012 09:42:46 +0000 Subject: [PATCH] * Source/NSPasteboard.m (+pasteboardByFilteringFile:): Correct typo in Richard's change. * Source/NSPasteboard.m (-pasteboard:provideDataForType:): Use the file name directly instead of reading the file contents. * Source/NSImageRep.m (+imageRepsWithContentsOfFile:, +imageFileTypes): Start working on filter service support for images. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@34741 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 9 +++++ Source/NSImageRep.m | 82 +++++++++++++++++++++++++++++++++++-------- Source/NSPasteboard.m | 60 ++++++++++++++++--------------- 3 files changed, 108 insertions(+), 43 deletions(-) diff --git a/ChangeLog b/ChangeLog index abc70ef9b..9b471b429 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2012-02-08 Fred Kiefer + + * Source/NSPasteboard.m (+pasteboardByFilteringFile:): Correct + typo in Richard's change. + * Source/NSPasteboard.m (-pasteboard:provideDataForType:): Use the + file name directly instead of reading the file contents. + * Source/NSImageRep.m (+imageRepsWithContentsOfFile:, +imageFileTypes): + Start working on filter service support for images. + 2012-02-08 Richard Frith-Macdonald * Source/NSPasteboard.m: ([+pasteboardByFilteringFile:]) fixed to diff --git a/Source/NSImageRep.m b/Source/NSImageRep.m index f0c5a2daf..3661aab0f 100644 --- a/Source/NSImageRep.m +++ b/Source/NSImageRep.m @@ -203,6 +203,7 @@ implement, so we can't do that. */ { NSString *ext; Class rep; + NSData* data; // Is the file extension already the file type? ext = [filename pathExtension]; @@ -210,7 +211,6 @@ implement, so we can't do that. */ { /* With no extension, we can't tell the type from the file name. Use the data instead. */ - NSData *data; data = [NSData dataWithContentsOfFile: filename]; return [self _imageRepsWithData: data]; } @@ -227,20 +227,45 @@ implement, so we can't do that. */ else return nil; - { - NSData* data; + // filter non-native types to native format + if (![[rep imageUnfilteredFileTypes] containsObject: ext]) + { + NSPasteboard *p = [NSPasteboard pasteboardByFilteringFile: filename]; + NSArray *nativeTypes = [rep imageUnfilteredFileTypes]; + NSMutableArray *ptypes = [NSMutableArray arrayWithCapacity: [nativeTypes count]]; + NSEnumerator *enumerator = [nativeTypes objectEnumerator]; + NSString *type; - data = [NSData dataWithContentsOfFile: filename]; - if ([rep respondsToSelector: @selector(imageRepsWithData:)]) + // Convert the native types to pasteboard types + while ((type = [enumerator nextObject]) != nil) + { + NSString *type2 = NSCreateFileContentsPboardType(type); + [ptypes addObject: type2]; + } + + type = [p availableTypeFromArray: ptypes]; + data = [p dataForType: type]; + NSDebugLLog(@"NSImage", @"Filtering data for %@ from %@ of type %@ to %@", filename, p, type, data); + } + else + { + data = [NSData dataWithContentsOfFile: filename]; + } + + if (nil == data) + return nil; + + if ([rep respondsToSelector: @selector(imageRepsWithData:)]) + { return [rep imageRepsWithData: data]; - else if ([rep respondsToSelector: @selector(imageRepWithData:)]) - { - NSImageRep *imageRep = [rep imageRepWithData: data]; - - if (imageRep != nil) - return [NSArray arrayWithObject: imageRep]; - } - } + } + else if ([rep respondsToSelector: @selector(imageRepWithData:)]) + { + NSImageRep *imageRep = [rep imageRepWithData: data]; + + if (imageRep != nil) + return [NSArray arrayWithObject: imageRep]; + } return nil; } @@ -339,8 +364,35 @@ implement, so we can't do that. */ + (NSArray *) imageFileTypes { - // FIXME: We should check what conversions are defined by services. - return [self imageUnfilteredFileTypes]; + NSArray *nativeTypes = [self imageUnfilteredFileTypes]; + NSEnumerator *enumerator = [nativeTypes objectEnumerator]; + NSMutableArray *filteredTypes = [[NSMutableArray alloc] initWithCapacity: + [nativeTypes count]]; + NSString *type; + + while ((type = [enumerator nextObject]) != nil) + { + NSEnumerator *enum2 = [[NSPasteboard typesFilterableTo: + NSCreateFileContentsPboardType(type)] + objectEnumerator]; + NSString *type2; + + while ((type2 = [enum2 nextObject]) != nil) + { + NSString *fileType = NSGetFileType(type2); + + if (nil != fileType) + { + type2 = fileType; + } + if ([filteredTypes indexOfObject: type2] == NSNotFound) + { + [filteredTypes addObject: type2]; + } + } + } + + return AUTORELEASE(filteredTypes); } + (NSArray *) imagePasteboardTypes diff --git a/Source/NSPasteboard.m b/Source/NSPasteboard.m index 8bf18b79a..643d8deea 100644 --- a/Source/NSPasteboard.m +++ b/Source/NSPasteboard.m @@ -747,36 +747,40 @@ static NSString *namePrefix = @"NSTypedFilenamesPboardType:"; [sender setData: [NSData data] forType: type]; return; // Not the name of a file to filter. } - if (data != nil) - { - d = data; - } - else if (file != nil) - { - d = [NSData dataWithContentsOfFile: file]; - } - else - { - d = [pboard dataForType: fromType]; - } - o = [NSDeserializer deserializePropertyListFromData: d - mutableContainers: NO]; - if ([o isKindOfClass: [NSString class]] == YES) + if (file != nil) { - filename = o; - } - else if ([o isKindOfClass: [NSArray class]] == YES - && [o count] > 0 - && [[o objectAtIndex: 0] isKindOfClass: [NSString class]] == YES) - { - filename = [o objectAtIndex: 0]; + filename = file; } else - { - [sender setData: [NSData data] forType: type]; - return; // Not the name of a file to filter. - } + { + if (data != nil) + { + d = data; + } + else + { + d = [pboard dataForType: fromType]; + } + + o = [NSDeserializer deserializePropertyListFromData: d + mutableContainers: NO]; + if ([o isKindOfClass: [NSString class]] == YES) + { + filename = o; + } + else if ([o isKindOfClass: [NSArray class]] == YES + && [o count] > 0 + && [[o objectAtIndex: 0] isKindOfClass: [NSString class]] == YES) + { + filename = [o objectAtIndex: 0]; + } + else + { + [sender setData: [NSData data] forType: type]; + return; // Not the name of a file to filter. + } + } /* * Set up and launch task to filter the named file. @@ -1139,12 +1143,12 @@ static NSMapTable *mimeMap = NULL; { originalTypes = [NSArray arrayWithObjects: NSCreateFilenamePboardType(ext), - NSFilenamePboardType, + NSFilenamesPboardType, nil]; } else { - originalTypes = [NSArray arrayWithObject: NSFilenamePboardType]; + originalTypes = [NSArray arrayWithObject: NSFilenamesPboardType]; } types = [GSFiltered _typesFilterableFrom: originalTypes]; p = (GSFiltered*)[GSFiltered pasteboardWithUniqueName];