Changeset 19764 in webkit


Ignore:
Timestamp:
Feb 20, 2007 10:00:26 PM (17 years ago)
Author:
bdakin
Message:

WebCore:

Reviewed by Maciej.

Implement writeImage() on Pasteboard so that the context menus can
call into the editor for the "Copy image" command instead of
calling across the clients.

  • WebCore.exp:
  • WebCore.xcodeproj/project.pbxproj:
  • editing/Editor.cpp: (WebCore::Editor::copyImage): Call into the Pasteboard.
  • editing/Editor.h:
  • loader/mac/LoaderNSURLExtras.h: A new NSURL extra needed inside WebCore.
  • loader/mac/LoaderNSURLExtras.m: Made this file Objective-C++ (from Objective-C) since the call into the MIMETypeRegistry will return a Vector. (urlByRemovingComponent): Cast to build as Obj-C++ (urlWithDataRelativeToURL): Same. (vectorContainsString): Helper. (suggestedFilenameWithMIMEType): New function.
  • page/ContextMenuClient.h: Remove copyImageToClipboard()
  • page/ContextMenuController.cpp: (WebCore::ContextMenuController::contextMenuItemSelected): Call into the editor instead of the client.
  • page/mac/WebCoreViewFactory.h: New localized String.
  • platform/LocalizedStrings.h: Same.
  • platform/MimeTypeRegistry.h: Expose two additional functions for MIMEType information from WebCore System Interface.
  • platform/Pasteboard.h:
  • platform/SharedBuffer.h: Make platformData() and platformDataSize () public since they are needed to write the image to the pasteboard.
  • platform/mac/LocalizedStringsMac.mm: New localized string. (WebCore::copyImageUnknownFileLabel):
  • platform/mac/MimeTypeRegistryMac.mm: (WebCore::MimeTypeRegistry::getExtensionsForMIMEType): (WebCore::MimeTypeRegistry::getPreferredExtensionForMIMEType):
  • platform/mac/PasteboardMac.mm: (WebCore::writableTypesForImage): (WebCore::Pasteboard::writeURL): Write the correct types if the url is for an image. (WebCore::fileWrapperForImage): (WebCore::Pasteboard::writeFileWrapperAsRTFDAttachment): (WebCore::Pasteboard::writeImage):
  • platform/mac/WebCoreNSStringExtras.h: Added. Some the the NSStringExtras that were in WebKit are now needed by WebCore. To avoid having two copies of these functions, I just moved the implementations to WebCore. The WebKit functions just call into WebCore.
  • platform/mac/WebCoreNSStringExtras.mm: Added. (hasCaseInsensitiveSuffix): (hasCaseInsensitiveSubstring): (filenameByFixingIllegalCharacters):
  • platform/mac/WebCoreSystemInterface.h: wkGetPreferredExtensionForMIMEType and wkGetExtensionsForMIMEType are now needed in WebCore as well as WebKit.
  • platform/mac/WebCoreSystemInterface.mm:

WebKit:

Reviewed by Maciej.

WebKit changes needed to implement writeImage() in WebCore's
Pasteboard class.

  • Misc/WebKitNSStringExtras.m: Call into WebCore for these implementations. (-[NSString _webkit_hasCaseInsensitiveSuffix:]): (-[NSString _webkit_hasCaseInsensitiveSubstring:]): (-[NSString _webkit_filenameByFixingIllegalCharacters]):
  • Misc/WebNSURLExtras.m: Same. (-[NSURL _webkit_suggestedFilenameWithMIMEType:]):
  • WebCoreSupport/WebContextMenuClient.h: Remove copyImageToClipboard()
  • WebCoreSupport/WebContextMenuClient.mm:
  • WebCoreSupport/WebSystemInterface.m: Expose GetExtensionsForMIMEType and GetPreferredExtensionForMIMEType to WebCore. (InitWebCoreSystemInterface):
  • WebCoreSupport/WebViewFactory.mm: New localized string for WebCore. (-[WebViewFactory copyImageUnknownFileLabel]):
Location:
trunk
Files:
2 added
26 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r19763 r19764  
     12007-02-20  Beth Dakin  <bdakin@apple.com>
     2
     3        Reviewed by Maciej.
     4
     5        Implement writeImage() on Pasteboard so that the context menus can
     6        call into the editor for the "Copy image" command instead of
     7        calling across the clients.
     8
     9        * WebCore.exp:
     10        * WebCore.xcodeproj/project.pbxproj:
     11        * editing/Editor.cpp:
     12        (WebCore::Editor::copyImage): Call into the Pasteboard.
     13        * editing/Editor.h:
     14        * loader/mac/LoaderNSURLExtras.h: A new NSURL extra needed inside
     15        WebCore.
     16        * loader/mac/LoaderNSURLExtras.m: Made this file Objective-C++
     17        (from Objective-C) since the call into the MIMETypeRegistry will
     18        return a Vector.
     19        (urlByRemovingComponent): Cast to build as Obj-C++
     20        (urlWithDataRelativeToURL): Same.
     21        (vectorContainsString): Helper.
     22        (suggestedFilenameWithMIMEType): New function.
     23        * page/ContextMenuClient.h: Remove copyImageToClipboard()
     24        * page/ContextMenuController.cpp:
     25        (WebCore::ContextMenuController::contextMenuItemSelected): Call
     26        into the editor instead of the client.
     27        * page/mac/WebCoreViewFactory.h: New localized String.
     28        * platform/LocalizedStrings.h: Same.
     29        * platform/MimeTypeRegistry.h: Expose two additional functions for
     30        MIMEType information from WebCore System Interface.
     31        * platform/Pasteboard.h:
     32        * platform/SharedBuffer.h: Make platformData() and platformDataSize
     33        () public since they are needed to write the image to the
     34        pasteboard.
     35        * platform/mac/LocalizedStringsMac.mm: New localized string.
     36        (WebCore::copyImageUnknownFileLabel):
     37        * platform/mac/MimeTypeRegistryMac.mm:
     38        (WebCore::MimeTypeRegistry::getExtensionsForMIMEType):
     39        (WebCore::MimeTypeRegistry::getPreferredExtensionForMIMEType):
     40        * platform/mac/PasteboardMac.mm:
     41        (WebCore::writableTypesForImage):
     42        (WebCore::Pasteboard::writeURL): Write the correct types if the url
     43        is for an image.
     44        (WebCore::fileWrapperForImage):
     45        (WebCore::Pasteboard::writeFileWrapperAsRTFDAttachment):
     46        (WebCore::Pasteboard::writeImage):
     47        * platform/mac/WebCoreNSStringExtras.h: Added. Some the the
     48        NSStringExtras that were in WebKit are now needed by WebCore. To
     49        avoid having two copies of these functions, I just moved the
     50        implementations to WebCore. The WebKit functions just call into
     51        WebCore.
     52        * platform/mac/WebCoreNSStringExtras.mm: Added.
     53        (hasCaseInsensitiveSuffix):
     54        (hasCaseInsensitiveSubstring):
     55        (filenameByFixingIllegalCharacters):
     56        * platform/mac/WebCoreSystemInterface.h:
     57        wkGetPreferredExtensionForMIMEType and wkGetExtensionsForMIMEType
     58        are now needed in WebCore as well as WebKit.
     59        * platform/mac/WebCoreSystemInterface.mm:
     60
    1612007-02-20  Adam Roben  <aroben@apple.com>
    262
  • trunk/WebCore/WebCore.exp

    r19762 r19764  
    602602__ZNK7WebCore9TimerBase8isActiveEv
    603603_canonicalURL
     604_filenameByFixingIllegalCharacters
     605_hasCaseInsensitiveSubstring
     606_hasCaseInsensitiveSuffix
    604607_stringIsFileURL
     608_suggestedFilenameWithMIMEType
    605609_urlByRemovingComponent
    606610_urlByRemovingFragment
     
    627631_wkGetCGFontFromNSFont
    628632_wkGetDefaultGlyphForChar
     633_wkGetExtensionsForMIMEType
    629634_wkGetFontInLanguageForCharacter
    630635_wkGetFontInLanguageForRange
     
    639644_wkGetNSURLResponseLastModifiedDate
    640645_wkGetNSURLResponseMustRevalidate
     646_wkGetPreferredExtensionForMIMEType
    641647_wkInitializeGlyphVector
    642648_wkNSURLProtocolClassForReqest
  • trunk/WebCore/WebCore.xcodeproj/project.pbxproj

    r19762 r19764  
    14571457                933A14B90B7D1D5200A53FFD /* JSTextEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = 933A14B70B7D1D5200A53FFD /* JSTextEvent.h */; };
    14581458                934706AB0AACD809002C1D43 /* TextDecoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 932CA7420AAA198E00AD1FAD /* TextDecoder.h */; settings = {ATTRIBUTES = (Private, ); }; };
     1459                934D9BA50B8C116B007B42A9 /* WebCoreNSStringExtras.mm in Sources */ = {isa = PBXBuildFile; fileRef = 934D9BA40B8C116B007B42A9 /* WebCoreNSStringExtras.mm */; };
     1460                934D9BA70B8C1175007B42A9 /* WebCoreNSStringExtras.h in Headers */ = {isa = PBXBuildFile; fileRef = 934D9BA60B8C1175007B42A9 /* WebCoreNSStringExtras.h */; settings = {ATTRIBUTES = (Private, ); }; };
    14591461                934FE9E50B5CA539003E4A73 /* FileChooser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 934FE9E40B5CA539003E4A73 /* FileChooser.cpp */; };
    14601462                9352071909BD3BA500F2038D /* StaticConstructors.h in Headers */ = {isa = PBXBuildFile; fileRef = 9352071709BD3BA500F2038D /* StaticConstructors.h */; };
     
    35013503                656B85370AEA1F9A00A095B4 /* ResourceHandle.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = ResourceHandle.h; sourceTree = "<group>"; };
    35023504                656D371A0ADBA5DE00A4554D /* LoaderNSURLExtras.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LoaderNSURLExtras.h; sourceTree = "<group>"; };
    3503                 656D371B0ADBA5DE00A4554D /* LoaderNSURLExtras.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = LoaderNSURLExtras.m; sourceTree = "<group>"; };
     3505                656D371B0ADBA5DE00A4554D /* LoaderNSURLExtras.m */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 30; path = LoaderNSURLExtras.m; sourceTree = "<group>"; };
    35043506                656D371E0ADBA5DE00A4554D /* DocumentLoader.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = DocumentLoader.h; sourceTree = "<group>"; };
    35053507                656D371F0ADBA5DE00A4554D /* DocumentLoaderMac.mm */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 30; path = DocumentLoaderMac.mm; sourceTree = "<group>"; };
     
    45264528                933A14B60B7D1D5200A53FFD /* JSTextEvent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSTextEvent.cpp; sourceTree = "<group>"; };
    45274529                933A14B70B7D1D5200A53FFD /* JSTextEvent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSTextEvent.h; sourceTree = "<group>"; };
     4530                934D9BA40B8C116B007B42A9 /* WebCoreNSStringExtras.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = WebCoreNSStringExtras.mm; sourceTree = "<group>"; };
     4531                934D9BA60B8C1175007B42A9 /* WebCoreNSStringExtras.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = WebCoreNSStringExtras.h; sourceTree = "<group>"; };
    45284532                934FE9E40B5CA539003E4A73 /* FileChooser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FileChooser.cpp; sourceTree = "<group>"; };
    45294533                9352071709BD3BA500F2038D /* StaticConstructors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StaticConstructors.h; sourceTree = "<group>"; };
     
    68746878                                84B2B1F7056BEF3A00D2B771 /* WebCoreKeyGenerator.h */,
    68756879                                84B2B1F8056BEF3A00D2B771 /* WebCoreKeyGenerator.m */,
     6880                                934D9BA60B8C1175007B42A9 /* WebCoreNSStringExtras.h */,
     6881                                934D9BA40B8C116B007B42A9 /* WebCoreNSStringExtras.mm */,
    68766882                                DD05FE0B0B8BA3C6009ACDFE /* WebCoreObjCExtras.h */,
    68776883                                DD05FE0C0B8BA3C6009ACDFE /* WebCoreObjCExtras.c */,
     
    1114311149                                DD05FE0D0B8BA3C6009ACDFE /* WebCoreObjCExtras.h in Headers */,
    1114411150                                06A6A73D0B8BA44800DF1703 /* StringTruncator.h in Headers */,
     11151                                934D9BA70B8C1175007B42A9 /* WebCoreNSStringExtras.h in Headers */,
    1114511152                        );
    1114611153                        runOnlyForDeploymentPostprocessing = 0;
     
    1250612513                                DD05FE0E0B8BA3C6009ACDFE /* WebCoreObjCExtras.c in Sources */,
    1250712514                                06A6A73C0B8BA44800DF1703 /* StringTruncator.cpp in Sources */,
     12515                                934D9BA50B8C116B007B42A9 /* WebCoreNSStringExtras.mm in Sources */,
    1250812516                        );
    1250912517                        runOnlyForDeploymentPostprocessing = 0;
  • trunk/WebCore/editing/Editor.cpp

    r19702 r19764  
    13831383}
    13841384
     1385void Editor::copyImage(const HitTestResult& result)
     1386{
     1387    Pasteboard::generalPasteboard()->writeImage(result);
     1388}
     1389
    13851390bool Editor::isContinuousSpellCheckingEnabled()
    13861391{
  • trunk/WebCore/editing/Editor.h

    r19702 r19764  
    9191
    9292    void copyURL(const KURL&, const String&);
     93    void copyImage(const HitTestResult&);
    9394
    9495    void indent();
  • trunk/WebCore/loader/mac/LoaderNSURLExtras.h

    r17025 r19764  
    4444BOOL urlIsEmpty(NSURL *url);
    4545NSURL *canonicalURL(NSURL *url);
     46NSString *suggestedFilenameWithMIMEType(NSURL *url, NSString *MIMEType);
    4647
    4748#ifdef __cplusplus
  • trunk/WebCore/loader/mac/LoaderNSURLExtras.m

    r17259 r19764  
    3232
    3333#import <wtf/Assertions.h>
     34#import <wtf/Vector.h>
     35#import "KURL.h"
     36#import "LocalizedStrings.h"
     37#import "MimeTypeRegistry.h"
     38#import "PlatformString.h"
     39#import "WebCoreNSStringExtras.h"
    3440#import "WebCoreSystemInterface.h"
     41
     42using namespace WebCore;
    3543
    3644NSURL *urlByRemovingComponent(NSURL *url, CFURLComponentType component)
     
    4856    if (numBytes == -1) {
    4957        numBytes = CFURLGetBytes((CFURLRef)url, NULL, 0);
    50         urlBytes = malloc(numBytes);
     58        urlBytes = static_cast<UInt8*>(malloc(numBytes));
    5159        CFURLGetBytes((CFURLRef)url, urlBytes, numBytes);
    5260    } else
     
    125133        baseURL = urlByRemovingResourceSpecifier(baseURL);
    126134       
    127         const UInt8 *bytes = [data bytes];
     135        const UInt8 *bytes = static_cast<const UInt8*>([data bytes]);
    128136        // NOTE: We use UTF-8 here since this encoding is used when computing strings when returning URL components
    129137        // (e.g calls to NSURL -path). However, this function is not tolerant of illegal UTF-8 sequences, which
     
    187195    return result;
    188196}
     197
     198static bool vectorContainsString(Vector<String> vector, String string)
     199{
     200    int size = vector.size();
     201    for (int i = 0; i < size; i++)
     202        if (vector[i] == string)
     203            return true;
     204    return false;
     205}
     206
     207NSString *suggestedFilenameWithMIMEType(NSURL *url, NSString *MIMEType)
     208{
     209    // Get the filename from the URL. Try the lastPathComponent first.
     210    NSString *lastPathComponent = [[url path] lastPathComponent];
     211    NSString *filename = filenameByFixingIllegalCharacters(lastPathComponent);
     212    NSString *extension = nil;
     213
     214    if ([filename length] == 0 || [lastPathComponent isEqualToString:@"/"]) {
     215        // lastPathComponent is no good, try the host.
     216        NSString *host = (NSString *)(KURL(url).host());
     217        filename = filenameByFixingIllegalCharacters(host);
     218        if ([filename length] == 0) {
     219            // Can't make a filename using this URL, use "unknown".
     220            filename = copyImageUnknownFileLabel();
     221        }
     222    } else {
     223        // Save the extension for later correction. Only correct the extension of the lastPathComponent.
     224        // For example, if the filename ends up being the host, we wouldn't want to correct ".com" in "www.apple.com".
     225        extension = [filename pathExtension];
     226    }
     227
     228    // No mime type reported. Just return the filename we have now.
     229    if (!MIMEType) {
     230        return filename;
     231    }
     232
     233    // Do not correct filenames that are reported with a mime type of tar, and
     234    // have a filename which has .tar in it or ends in .tgz
     235    if (([MIMEType isEqualToString:@"application/tar"] || [MIMEType isEqualToString:@"application/x-tar"])
     236        && (hasCaseInsensitiveSubstring(filename, @".tar")
     237        || hasCaseInsensitiveSuffix(filename, @".tgz"))) {
     238        return filename;
     239    }
     240
     241    // I don't think we need to worry about this for the image case
     242    // If the type is known, check the extension and correct it if necessary.
     243    if (![MIMEType isEqualToString:@"application/octet-stream"] && ![MIMEType isEqualToString:@"text/plain"]) {
     244        Vector<String> extensions = MimeTypeRegistry::getExtensionsForMIMEType(MIMEType);
     245
     246        if (!extensions.size() || (extensions && !vectorContainsString(extensions, extension))) {
     247            // The extension doesn't match the MIME type. Correct this.
     248            NSString *correctExtension = MimeTypeRegistry::getPreferredExtensionForMIMEType(MIMEType);
     249            if ([correctExtension length] != 0) {
     250                // Append the correct extension.
     251                filename = [filename stringByAppendingPathExtension:correctExtension];
     252            }
     253        }
     254    }
     255
     256    return filename;
     257}
  • trunk/WebCore/page/ContextMenuClient.h

    r18748 r19764  
    4646
    4747        virtual void downloadURL(const KURL& url) = 0;
    48         virtual void copyImageToClipboard(const HitTestResult&) = 0;
    4948        virtual void searchWithGoogle(const Frame*) = 0;
    5049        virtual void lookUpInDictionary(Frame*) = 0;
  • trunk/WebCore/page/ContextMenuController.cpp

    r19547 r19764  
    135135            // FIXME: The Pasteboard class is not written yet
    136136            // For now, call into the client. This is temporary!
    137             m_client->copyImageToClipboard(result);
     137            frame->editor()->copyImage(result);
    138138            break;
    139139        case ContextMenuItemTagOpenFrameInNewWindow: {
  • trunk/WebCore/page/mac/WebCoreViewFactory.h

    r18554 r19764  
    4545- (NSString *)fileButtonChooseFileLabel;
    4646- (NSString *)fileButtonNoFileSelectedLabel;
     47- (NSString *)copyImageUnknownFileLabel;
    4748
    4849// Context menu item titles
  • trunk/WebCore/platform/LocalizedStrings.h

    r18554 r19764  
    3737    String fileButtonChooseFileLabel();
    3838    String fileButtonNoFileSelectedLabel();
     39    String copyImageUnknownFileLabel();
    3940    String contextMenuItemTagOpenLinkInNewWindow();
    4041    String contextMenuItemTagDownloadLinkToDisk();
  • trunk/WebCore/platform/MimeTypeRegistry.h

    r19702 r19764  
    3030#include "StringHash.h"
    3131#include <wtf/HashSet.h>
     32#include <wtf/Vector.h>
    3233
    3334namespace WebCore {
     
    3637public:
    3738    static String getMIMETypeForExtension(const String& ext);
     39    static Vector<String> getExtensionsForMIMEType(const String& type);
     40    static String getPreferredExtensionForMIMEType(const String& type);
    3841    static String getMIMETypeForPath(const String& path);
    3942   
  • trunk/WebCore/platform/Pasteboard.h

    r19689 r19764  
    3636
    3737#if PLATFORM(MAC)
     38class NSFileWrapper;
    3839class NSPasteboard;
    3940class NSArray;
     
    5859class DocumentFragment;
    5960class Frame;
     61class HitTestResult;
    6062class KURL;
    6163class Range;
     
    6769    //Helper functions to allow Clipboard to share code
    6870    static void writeSelection(NSPasteboard* pasteboard, Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame);
    69     static void writeURL(NSPasteboard* pasteboard, NSArray* types, const KURL& url, const String& titleStr, Frame* frame);
     71    static void writeURL(NSPasteboard* pasteboard, NSArray* types, const KURL& url, const String& titleStr, Frame* frame, bool isImage = false);
    7072#endif
    7173   
    7274    static Pasteboard* generalPasteboard();
    7375    void writeSelection(Range*, bool canSmartCopyOrDelete, Frame*);
    74     void writeURL(const KURL&, const String&, Frame* = 0);
     76    void writeURL(const KURL&, const String&, Frame* = 0, bool isImage = false);
     77    void writeImage(const HitTestResult&);
     78#if PLATFORM(MAC)
     79    void writeFileWrapperAsRTFDAttachment(NSFileWrapper*);
     80#endif
    7581    void clear();
    7682    bool canSmartReplace();
  • trunk/WebCore/platform/SharedBuffer.h

    r18728 r19764  
    5757    void append(const char*, int);
    5858    void clear();
     59    const char* platformData() const;
     60    unsigned platformDataSize() const;
    5961
    6062private:
     
    6264    void maybeTransferPlatformData();
    6365    bool hasPlatformData() const;
    64     const char* platformData() const;
    65     unsigned platformDataSize() const;
    6666   
    6767    Vector<char> m_buffer;
  • trunk/WebCore/platform/mac/LocalizedStringsMac.mm

    r18554 r19764  
    8181}
    8282
     83String copyImageUnknownFileLabel()
     84{
     85    BEGIN_BLOCK_OBJC_EXCEPTIONS;
     86    return [[WebCoreViewFactory sharedFactory] copyImageUnknownFileLabel];
     87    END_BLOCK_OBJC_EXCEPTIONS;
     88    return String();
     89}
     90
    8391String contextMenuItemTagOpenLinkInNewWindow()
    8492{
  • trunk/WebCore/platform/mac/MimeTypeRegistryMac.mm

    r19702 r19764  
    4747}
    4848
     49Vector<String> MimeTypeRegistry::getExtensionsForMIMEType(const String& type)
     50{
     51    NSArray *stringsArray = wkGetExtensionsForMIMEType(type);
     52    Vector<String> stringsVector = Vector<String>();
     53    unsigned count = [stringsArray count];
     54    if (count > 0) {
     55        NSEnumerator* enumerator = [stringsArray objectEnumerator];
     56        NSString* string;
     57        while ((string = [enumerator nextObject]) != nil)
     58            stringsVector.append(string);
     59    }
     60    return stringsVector;
    4961}
     62
     63String MimeTypeRegistry::getPreferredExtensionForMIMEType(const String& type)
     64{
     65    return wkGetPreferredExtensionForMIMEType(type);
     66}
     67
     68}
  • trunk/WebCore/platform/mac/PasteboardMac.mm

    r19689 r19764  
    2727#import "Pasteboard.h"
    2828
     29#import "Cache.h"
     30#import "CachedResource.h"
    2931#import "CharacterNames.h"
    3032#import "DOMRangeInternal.h"
     33#import "Document.h"
    3134#import "DocumentFragment.h"
    3235#import "Editor.h"
    3336#import "EditorClient.h"
     37#import "HitTestResult.h"
     38#import "Image.h"
    3439#import "KURL.h"
     40#import "LoaderNSURLExtras.h"
     41#import "MimeTypeRegistry.h"
    3542#import "RetainPtr.h"
     43#import "WebCoreNSStringExtras.h"
    3644#import "WebCoreSystemInterface.h"
    3745#import "markup.h"
     
    7785            NSStringPboardType,
    7886            nil];
     87    }
     88    return types;
     89}
     90
     91static NSArray* writableTypesForImage()
     92{
     93    static NSMutableArray *types = nil;
     94    if (!types) {
     95        types = [[NSMutableArray alloc] initWithObjects:NSTIFFPboardType, nil];
     96        [types addObjectsFromArray:writableTypesForURL()];
     97        [types addObject:NSRTFDPboardType];
    7998    }
    8099    return types;
     
    182201}
    183202
    184 void Pasteboard::writeURL(NSPasteboard* pasteboard, NSArray* types, const KURL& url, const String& titleStr, Frame* frame)
     203void Pasteboard::writeURL(NSPasteboard* pasteboard, NSArray* types, const KURL& url, const String& titleStr, Frame* frame, bool isImage)
    185204{
    186205    if (WebArchivePboardType == nil)
     
    188207   
    189208    if (types == nil) {
    190         types = writableTypesForURL();
     209        if (isImage)
     210            types = writableTypesForImage();
     211        else
     212            types = writableTypesForURL();
    191213        [pasteboard declareTypes:types owner:nil];
    192214    }
     
    219241}
    220242   
    221 void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame)
    222 {
    223     Pasteboard::writeURL(m_pasteboard, nil, url, titleStr, frame);
    224 }
    225 
     243void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame, bool isImage)
     244{
     245    Pasteboard::writeURL(m_pasteboard, nil, url, titleStr, frame, isImage);
     246}
     247
     248static NSFileWrapper* fileWrapperForImage(CachedResource* resource, NSURL *URL)
     249{
     250    SharedBuffer* coreData = resource->allData();
     251    NSData *data = [[[NSData alloc] initWithBytes:coreData->platformData()
     252        length:coreData->platformDataSize()] autorelease];
     253    NSFileWrapper *wrapper = [[[NSFileWrapper alloc] initRegularFileWithContents:data] autorelease];
     254    String coreMIMEType = resource->response().mimeType();
     255    NSString *MIMEType = nil;
     256    if (!coreMIMEType.isNull())
     257        MIMEType = coreMIMEType;
     258    [wrapper setPreferredFilename:suggestedFilenameWithMIMEType(URL, MIMEType)];
     259    return wrapper;
     260}
     261
     262void Pasteboard::writeFileWrapperAsRTFDAttachment(NSFileWrapper* wrapper)
     263{
     264    NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper:wrapper];
     265   
     266    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attachment];
     267    [attachment release];
     268   
     269    NSData *RTFDData = [string RTFDFromRange:NSMakeRange(0, [string length]) documentAttributes:nil];
     270    [m_pasteboard setData:RTFDData forType:NSRTFDPboardType];
     271}
     272
     273void Pasteboard::writeImage(const HitTestResult& result)
     274{   
     275    KURL coreURL = result.absoluteLinkURL();
     276    if (coreURL.isEmpty())
     277        coreURL = result.absoluteImageURL();
     278    NSURL *URL = coreURL.getNSURL();
     279    ASSERT(URL);
     280
     281    NSString *title = result.altDisplayString().isNull() ? nil : (NSString*)(result.altDisplayString());
     282    Frame* frame = result.innerNonSharedNode()->document()->frame();
     283
     284    writeURL(URL, title, frame, true);
     285    NSArray *types = [m_pasteboard types];
     286    [m_pasteboard declareTypes:types owner:nil];
     287
     288    NSImage *image = (NSImage *)(result.image() ? result.image()->getNSImage() : nil);
     289    ASSERT(image);
     290    [m_pasteboard setData:[image TIFFRepresentation] forType:NSTIFFPboardType];
     291
     292    CachedResource* imageResource = WebCore::cache()->resourceForURL(result.absoluteImageURL().url());
     293    ASSERT(imageResource);
     294    String MIMEType = imageResource->response().mimeType();
     295    ASSERT(MimeTypeRegistry::isSupportedImageResourceMIMEType(MIMEType));
     296
     297    if (imageResource)
     298        writeFileWrapperAsRTFDAttachment(fileWrapperForImage(imageResource, URL));
     299}
    226300
    227301bool Pasteboard::canSmartReplace()
  • trunk/WebCore/platform/mac/WebCoreSystemInterface.h

    r18541 r19764  
    4545@class NSURLRequest;
    4646#else
     47typedef struct NSArray NSArray;
    4748typedef struct NSDate NSDate;
    4849typedef struct NSFont NSFont;
     
    9798extern int (*wkGetGlyphVectorNumGlyphs)(void* glyphVector);
    9899extern size_t (*wkGetGlyphVectorRecordSize)(void* glyphVector);
     100extern NSString* (*wkGetPreferredExtensionForMIMEType)(NSString*);
     101extern NSArray* (*wkGetExtensionsForMIMEType)(NSString*);
    99102extern NSString* (*wkGetMIMETypeForExtension)(NSString*);
    100103extern ATSUFontID (*wkGetNSFontATSUFontId)(NSFont*);
  • trunk/WebCore/platform/mac/WebCoreSystemInterface.mm

    r18541 r19764  
    4747int (*wkGetGlyphVectorNumGlyphs)(void* glyphVector);
    4848size_t (*wkGetGlyphVectorRecordSize)(void* glyphVector);
     49NSString* (*wkGetPreferredExtensionForMIMEType)(NSString*);
     50NSArray* (*wkGetExtensionsForMIMEType)(NSString*);
    4951NSString* (*wkGetMIMETypeForExtension)(NSString*);
    5052ATSUFontID (*wkGetNSFontATSUFontId)(NSFont*);
  • trunk/WebKit/ChangeLog

    r19762 r19764  
     12007-02-20  Beth Dakin  <bdakin@apple.com>
     2
     3        Reviewed by Maciej.
     4
     5        WebKit changes needed to implement writeImage() in WebCore's
     6        Pasteboard class.
     7
     8        * Misc/WebKitNSStringExtras.m: Call into WebCore for these
     9        implementations.
     10        (-[NSString _webkit_hasCaseInsensitiveSuffix:]):
     11        (-[NSString _webkit_hasCaseInsensitiveSubstring:]):
     12        (-[NSString _webkit_filenameByFixingIllegalCharacters]):
     13        * Misc/WebNSURLExtras.m: Same.
     14        (-[NSURL _webkit_suggestedFilenameWithMIMEType:]):
     15        * WebCoreSupport/WebContextMenuClient.h: Remove
     16        copyImageToClipboard()
     17        * WebCoreSupport/WebContextMenuClient.mm:
     18        * WebCoreSupport/WebSystemInterface.m: Expose
     19        GetExtensionsForMIMEType and GetPreferredExtensionForMIMEType to
     20        WebCore.
     21        (InitWebCoreSystemInterface):
     22        * WebCoreSupport/WebViewFactory.mm: New localized string for
     23        WebCore.
     24        (-[WebViewFactory copyImageUnknownFileLabel]):
     25
    1262007-02-20  Adam Roben  <aroben@apple.com>
    227
  • trunk/WebKit/Misc/WebKitNSStringExtras.m

    r19744 r19764  
    3232#import <WebKit/WebNSFileManagerExtras.h>
    3333
     34#import <WebCore/WebCoreNSStringExtras.h>
    3435#import <WebCore/WebCoreTextRenderer.h>
    3536
     
    200201-(BOOL)_webkit_hasCaseInsensitiveSuffix:(NSString *)suffix
    201202{
    202     return [self rangeOfString:suffix options:(NSCaseInsensitiveSearch | NSBackwardsSearch | NSAnchoredSearch)].location != NSNotFound;
     203    return hasCaseInsensitiveSuffix(self, suffix);
    203204}
    204205
    205206-(BOOL)_webkit_hasCaseInsensitiveSubstring:(NSString *)substring
    206207{
    207     return [self rangeOfString:substring options:NSCaseInsensitiveSearch].location != NSNotFound;
     208    return hasCaseInsensitiveSubstring(self, substring);
    208209}
    209210
    210211-(NSString *)_webkit_filenameByFixingIllegalCharacters
    211212{
    212     NSMutableString *filename = [[self mutableCopy] autorelease];
    213 
    214     // Strip null characters.
    215     unichar nullChar = 0;
    216     [filename replaceOccurrencesOfString:[NSString stringWithCharacters:&nullChar length:0] withString:@"" options:0 range:NSMakeRange(0, [filename length])];
    217 
    218     // Replace "/" with "-".
    219     [filename replaceOccurrencesOfString:@"/" withString:@"-" options:0 range:NSMakeRange(0, [filename length])];
    220 
    221     // Replace ":" with "-".
    222     [filename replaceOccurrencesOfString:@":" withString:@"-" options:0 range:NSMakeRange(0, [filename length])];
    223    
    224     // Strip leading dots.
    225     while ([filename hasPrefix:@"."]) {
    226         [filename deleteCharactersInRange:NSMakeRange(0,1)];
    227     }
    228    
    229     return filename;
     213    return filenameByFixingIllegalCharacters(self);
    230214}
    231215
  • trunk/WebKit/Misc/WebNSURLExtras.m

    r19439 r19764  
    682682- (NSString *)_webkit_suggestedFilenameWithMIMEType:(NSString *)MIMEType
    683683{
    684     // Get the filename from the URL. Try the lastPathComponent first.
    685     NSString *lastPathComponent = [[self path] lastPathComponent];
    686     NSString *filename = [lastPathComponent _webkit_filenameByFixingIllegalCharacters];
    687     NSString *extension = nil;
    688 
    689     if ([filename length] == 0 || [lastPathComponent isEqualToString:@"/"]) {
    690         // lastPathComponent is no good, try the host.
    691         filename = [[self _web_hostString] _webkit_filenameByFixingIllegalCharacters];
    692         if ([filename length] == 0) {
    693             // Can't make a filename using this URL, use "unknown".
    694             filename = UI_STRING("unknown", "Unknown filename");
    695         }
    696     } else {
    697         // Save the extension for later correction. Only correct the extension of the lastPathComponent.
    698         // For example, if the filename ends up being the host, we wouldn't want to correct ".com" in "www.apple.com".
    699         extension = [filename pathExtension];
    700     }
    701 
    702     // No mime type reported. Just return the filename we have now.
    703     if (!MIMEType) {
    704         return filename;
    705     }
    706 
    707     // Do not correct filenames that are reported with a mime type of tar, and
    708     // have a filename which has .tar in it or ends in .tgz
    709     if (([MIMEType isEqualToString:@"application/tar"] || [MIMEType isEqualToString:@"application/x-tar"]) &&
    710         ([filename _webkit_hasCaseInsensitiveSubstring:@".tar"] || [filename _webkit_hasCaseInsensitiveSuffix:@".tgz"])) {
    711         return filename;
    712     }
    713 
    714     // If the type is known, check the extension and correct it if necessary.
    715     if (![MIMEType isEqualToString:@"application/octet-stream"] && ![MIMEType isEqualToString:@"text/plain"]) {
    716         NSArray *extensions = WKGetExtensionsForMIMEType(MIMEType);
    717 
    718         if (![extension length] || (extensions && ![extensions containsObject:extension])) {
    719             // The extension doesn't match the MIME type. Correct this.
    720             NSString *correctExtension = WKGetPreferredExtensionForMIMEType(MIMEType);
    721             if ([correctExtension length] != 0) {
    722                 // Append the correct extension.
    723                 filename = [filename stringByAppendingPathExtension:correctExtension];
    724             }
    725         }
    726     }
    727 
    728     return filename;
     684    return suggestedFilenameWithMIMEType(self, MIMEType);
    729685}
    730686
  • trunk/WebKit/WebCoreSupport/WebContextMenuClient.h

    r18748 r19764  
    4343   
    4444    virtual void downloadURL(const WebCore::KURL&);
    45     virtual void copyImageToClipboard(const WebCore::HitTestResult&);
    4645    virtual void searchWithGoogle(const WebCore::Frame*);
    4746    virtual void lookUpInDictionary(WebCore::Frame*);
  • trunk/WebKit/WebCoreSupport/WebContextMenuClient.mm

    r19529 r19764  
    242242}
    243243
    244 void WebContextMenuClient::copyImageToClipboard(const HitTestResult& hitTestResult)
    245 {
    246     NSDictionary *element = [[[WebElementDictionary alloc] initWithHitTestResult:hitTestResult] autorelease];
    247     NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
    248     NSArray *types = [NSPasteboard _web_writableTypesForImageIncludingArchive:(hitTestResult.innerNonSharedNode() != 0)];
    249     [pasteboard declareTypes:types owner:m_webView];
    250     [m_webView _writeImageForElement:element withPasteboardTypes:types toPasteboard:pasteboard];
    251 }
    252 
    253244void WebContextMenuClient::searchWithSpotlight()
    254245{
  • trunk/WebKit/WebCoreSupport/WebSystemInterface.m

    r18541 r19764  
    4545    INIT(CreateCustomCFReadStream);
    4646    INIT(CreateNSURLConnectionDelegateProxy);
     47    INIT(CreateURLNPasteboardFlavorTypeName);
    4748    INIT(CreateURLPasteboardFlavorTypeName);
    48     INIT(CreateURLNPasteboardFlavorTypeName);
    4949    INIT(DrawBezeledTextArea);
    5050    INIT(DrawBezeledTextFieldCell);
     51    INIT(DrawFocusRing);
    5152    INIT(DrawTextFieldCellFocusRing);
    52     INIT(DrawFocusRing);
    5353    INIT(FontSmoothingModeIsLCD);
    5454    INIT(GetATSStyleGroup);
    5555    INIT(GetCGFontFromNSFont);
    5656    INIT(GetDefaultGlyphForChar);
     57    INIT(GetExtensionsForMIMEType);
    5758    INIT(GetFontInLanguageForCharacter);
    5859    INIT(GetFontInLanguageForRange);
     
    6768    INIT(GetNSURLResponseLastModifiedDate);
    6869    INIT(GetNSURLResponseMustRevalidate);
     70    INIT(GetPreferredExtensionForMIMEType);
    6971    INIT(InitializeGlyphVector);
     72    INIT(NSURLProtocolClassForReqest);
    7073    INIT(PathFromFont);
    7174    INIT(PopupMenu);
    7275    INIT(ReleaseStyleGroup);
     76    INIT(SecondsSinceLastInputEvent);
    7377    INIT(SetCGFontRenderingMode);
    7478    INIT(SetDragImage);
     
    8084    INIT(SignalCFReadStreamHasBytes);
    8185    INIT(SupportsMultipartXMixedReplace);
    82     INIT(NSURLProtocolClassForReqest);
    83     INIT(SecondsSinceLastInputEvent);
     86
    8487   
    8588    didInit = true;
  • trunk/WebKit/WebCoreSupport/WebViewFactory.mm

    r18725 r19764  
    129129}
    130130
     131- (NSString *)copyImageUnknownFileLabel
     132{
     133    return UI_STRING("unknown", "Unknown filename");
     134}
     135
    131136- (NSMenu *)cellMenuForSearchField
    132137{
Note: See TracChangeset for help on using the changeset viewer.