/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- *//* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */#include <sal/config.h>#include <sal/log.hxx>#include "DataFlavorMapping.hxx"#include "HtmlFmtFlt.hxx"#include "PictToBmpFlt.hxx"#include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>#include <com/sun/star/datatransfer/XMimeContentType.hpp>#include <com/sun/star/datatransfer/MimeContentTypeFactory.hpp>#include <com/sun/star/lang/IllegalArgumentException.hpp>#include <com/sun/star/uno/Sequence.hxx>#include <comphelper/processfactory.hxx>#include <rtl/ustring.hxx>#include <osl/endian.h>#include <cassert>#include <string.h>#include <premac.h>#include <Cocoa/Cocoa.h>#include <postmac.h>usingnamespace::com::sun::star::datatransfer;usingnamespace::com::sun::star::uno;usingnamespacecom::sun::star::lang;usingnamespacecppu;usingnamespacestd;namespace{/* Determine whether or not a DataFlavor is valid. */boolisValidFlavor(constDataFlavor&aFlavor){size_tlen=aFlavor.MimeType.getLength();Typedtype=aFlavor.DataType;return((len>0)&&((dtype==cppu::UnoType<Sequence<sal_Int8>>::get())||(dtype==cppu::UnoType<OUString>::get())));}OUStringNSStringToOUString(constNSString*cfString){assert(cfString&&"Invalid parameter");constchar*utf8Str=[cfStringUTF8String];unsignedintlen=rtl_str_getLength(utf8Str);returnOUString(utf8Str,len,RTL_TEXTENCODING_UTF8);}NSString*OUStringToNSString(constOUString&ustring){OStringutf8Str=OUStringToOString(ustring,RTL_TEXTENCODING_UTF8);return[NSStringstringWithCString:utf8Str.getStr()encoding:NSUTF8StringEncoding];}NSString*PBTYPE_SODX=@"application/x-openoffice-objectdescriptor-xml;windows_formatname=\"Star Object Descriptor (XML)\"";NSString*PBTYPE_SESX=@"application/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\"";NSString*PBTYPE_SLSDX=@"application/x-openoffice-linksrcdescriptor-xml;windows_formatname=\"Star Link Source Descriptor (XML)\"";NSString*PBTYPE_ESX=@"application/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\"";NSString*PBTYPE_LSX=@"application/x-openoffice-link-source-xml;windows_formatname=\"Star Link Source (XML)\"";NSString*PBTYPE_EOX=@"application/x-openoffice-embedded-obj-xml;windows_formatname=\"Star Embedded Object (XML)\"";NSString*PBTYPE_SVXB=@"application/x-openoffice-svbx;windows_formatname=\"SVXB (StarView Bitmap/Animation)\"";NSString*PBTYPE_GDIMF=@"application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"";NSString*PBTYPE_WMF=@"application/x-openoffice-wmf;windows_formatname=\"Image WMF\"";NSString*PBTYPE_EMF=@"application/x-openoffice-emf;windows_formatname=\"Image EMF\"";NSString*PBTYPE_DUMMY_INTERNAL=@"application/x-openoffice-internal";constchar*FLAVOR_SODX="application/x-openoffice-objectdescriptor-xml;windows_formatname=\"Star Object Descriptor (XML)\"";constchar*FLAVOR_SESX="application/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\"";constchar*FLAVOR_SLSDX="application/x-openoffice-linksrcdescriptor-xml;windows_formatname=\"Star Link Source Descriptor (XML)\"";constchar*FLAVOR_ESX="application/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\"";constchar*FLAVOR_LSX="application/x-openoffice-link-source-xml;windows_formatname=\"Star Link Source (XML)\"";constchar*FLAVOR_EOX="application/x-openoffice-embedded-obj-xml;windows_formatname=\"Star Embedded Object (XML)\"";constchar*FLAVOR_SVXB="application/x-openoffice-svbx;windows_formatname=\"SVXB (StarView Bitmap/Animation)\"";constchar*FLAVOR_GDIMF="application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"";constchar*FLAVOR_WMF="application/x-openoffice-wmf;windows_formatname=\"Image WMF\"";constchar*FLAVOR_EMF="application/x-openoffice-emf;windows_formatname=\"Image EMF\"";constchar*FLAVOR_DUMMY_INTERNAL="application/x-openoffice-internal";structFlavorMap{constNSString*SystemFlavor;constchar*OOoFlavor;constchar*HumanPresentableName;boolDataTypeOUString;// sequence<byte> otherwise};/* At the moment it appears as if only MS Office pastes "public.html" to the clipboard. */staticconstFlavorMapflavorMap[]={{NSPasteboardTypeString,"text/plain;charset=utf-16","Unicode Text (UTF-16)",true},{NSPasteboardTypeRTF,"text/rtf","Rich Text Format",false},{NSPasteboardTypeTIFF,"image/png","Portable Network Graphics",false},{NSPasteboardTypeHTML,"text/html","Plain Html",false},SAL_WNODEPRECATED_DECLARATIONS_PUSH// "'NSFilenamesPboardType' is deprecated: first deprecated in macOS 10.14 - Create// multiple pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead"{NSFilenamesPboardType,"application/x-openoffice-filelist;windows_formatname=\"FileList\"","FileList",false},SAL_WNODEPRECATED_DECLARATIONS_POP{PBTYPE_SESX,FLAVOR_SESX,"Star Embed Source (XML)",false},{PBTYPE_SLSDX,FLAVOR_SLSDX,"Star Link Source Descriptor (XML)",false},{PBTYPE_ESX,FLAVOR_ESX,"Star Embed Source (XML)",false},{PBTYPE_LSX,FLAVOR_LSX,"Star Link Source (XML)",false},{PBTYPE_EOX,FLAVOR_EOX,"Star Embedded Object (XML)",false},{PBTYPE_SVXB,FLAVOR_SVXB,"SVXB (StarView Bitmap/Animation",false},{PBTYPE_GDIMF,FLAVOR_GDIMF,"GDIMetaFile",false},{PBTYPE_WMF,FLAVOR_WMF,"Windows MetaFile",false},{PBTYPE_EMF,FLAVOR_EMF,"Windows Enhanced MetaFile",false},{PBTYPE_SODX,FLAVOR_SODX,"Star Object Descriptor (XML)",false},{PBTYPE_DUMMY_INTERNAL,FLAVOR_DUMMY_INTERNAL,"internal data",false}};#defineSIZE_FLAVOR_MAP(sizeof(flavorMap)/sizeof(FlavorMap))boolisByteSequenceType(constType&theType){return(theType==cppu::UnoType<Sequence<sal_Int8>>::get());}boolisOUStringType(constType&theType){return(theType==cppu::UnoType<OUString>::get());}}// unnamed namespace/* A base class for other data provider. */classDataProviderBaseImpl:publicDataProvider{public:DataProviderBaseImpl(constAny&data);DataProviderBaseImpl(iddata);virtual~DataProviderBaseImpl()override;protected:AnymData;//NSData* mSystemData;idmSystemData;};DataProviderBaseImpl::DataProviderBaseImpl(constAny&data):mData(data),mSystemData(nil){}DataProviderBaseImpl::DataProviderBaseImpl(iddata):mSystemData(data){[mSystemDataretain];<--- syntax error}DataProviderBaseImpl::~DataProviderBaseImpl(){if(mSystemData){[mSystemDatarelease];}}classUniDataProvider:publicDataProviderBaseImpl{public:UniDataProvider(constAny&data);UniDataProvider(NSData*data);virtualNSData*getSystemData()override;virtualAnygetOOoData()override;};UniDataProvider::UniDataProvider(constAny&data):DataProviderBaseImpl(data){}UniDataProvider::UniDataProvider(NSData*data):DataProviderBaseImpl(data){}NSData*UniDataProvider::getSystemData(){OUStringustr;mData>>=ustr;OStringstrUtf8;ustr.convertToString(&strUtf8,RTL_TEXTENCODING_UTF8,OUSTRING_TO_OSTRING_CVTFLAGS);return[NSDatadataWithBytes:strUtf8.getStr()length:strUtf8.getLength()];}AnyUniDataProvider::getOOoData(){AnyoOOData;if(mSystemData){oOOData<<=OUString(static_cast<constsal_Char*>([mSystemDatabytes]),[mSystemDatalength],RTL_TEXTENCODING_UTF8);}else{oOOData=mData;}returnoOOData;}classByteSequenceDataProvider:publicDataProviderBaseImpl{public:ByteSequenceDataProvider(constAny&data);ByteSequenceDataProvider(NSData*data);virtualNSData*getSystemData()override;virtualAnygetOOoData()override;};ByteSequenceDataProvider::ByteSequenceDataProvider(constAny&data):DataProviderBaseImpl(data){}ByteSequenceDataProvider::ByteSequenceDataProvider(NSData*data):DataProviderBaseImpl(data){}NSData*ByteSequenceDataProvider::getSystemData(){Sequence<sal_Int8>rawData;mData>>=rawData;return[NSDatadataWithBytes:rawData.getArray()length:rawData.getLength()];}AnyByteSequenceDataProvider::getOOoData(){AnyoOOData;if(mSystemData){unsignedintflavorDataLength=[mSystemDatalength];Sequence<sal_Int8>byteSequence;byteSequence.realloc(flavorDataLength);memcpy(byteSequence.getArray(),[mSystemDatabytes],flavorDataLength);oOOData<<=byteSequence;}else{oOOData=mData;}returnoOOData;}classHTMLFormatDataProvider:publicDataProviderBaseImpl{public:HTMLFormatDataProvider(NSData*data);virtualNSData*getSystemData()override;virtualAnygetOOoData()override;};HTMLFormatDataProvider::HTMLFormatDataProvider(NSData*data):DataProviderBaseImpl(data){}NSData*HTMLFormatDataProvider::getSystemData(){Sequence<sal_Int8>textHtmlData;mData>>=textHtmlData;Sequence<sal_Int8>htmlFormatData=TextHtmlToHTMLFormat(textHtmlData);return[NSDatadataWithBytes:htmlFormatData.getArray()length:htmlFormatData.getLength()];}AnyHTMLFormatDataProvider::getOOoData(){AnyoOOData;if(mSystemData){unsignedintflavorDataLength=[mSystemDatalength];Sequence<sal_Int8>unkHtmlData;unkHtmlData.realloc(flavorDataLength);memcpy(unkHtmlData.getArray(),[mSystemDatabytes],flavorDataLength);Sequence<sal_Int8>*pPlainHtml=&unkHtmlData;Sequence<sal_Int8>plainHtml;if(isHTMLFormat(unkHtmlData)){plainHtml=HTMLFormatToTextHtml(unkHtmlData);pPlainHtml=&plainHtml;}oOOData<<=*pPlainHtml;}else{oOOData=mData;}returnoOOData;}classPNGDataProvider:publicDataProviderBaseImpl{NSBitmapImageFileTypemeImageType;public:PNGDataProvider(constAny&,NSBitmapImageFileType);PNGDataProvider(NSData*,NSBitmapImageFileType);virtualNSData*getSystemData()override;virtualAnygetOOoData()override;};PNGDataProvider::PNGDataProvider(constAny&data,NSBitmapImageFileTypeeImageType):DataProviderBaseImpl(data),meImageType(eImageType){}PNGDataProvider::PNGDataProvider(NSData*data,NSBitmapImageFileTypeeImageType):DataProviderBaseImpl(data),meImageType(eImageType){}NSData*PNGDataProvider::getSystemData(){Sequence<sal_Int8>pngData;mData>>=pngData;Sequence<sal_Int8>imgData;NSData*sysData=nullptr;if(PNGToImage(pngData,imgData,meImageType))sysData=[NSDatadataWithBytes:imgData.getArray()length:imgData.getLength()];returnsysData;}/* The AOO 'PCT' filter is not yet good enough to be used and there is no flavor defined for exchanging 'PCT' with AOO so we convert 'PCT' to a PNG and provide this to AOO*/AnyPNGDataProvider::getOOoData(){AnyoOOData;if(mSystemData){constunsignedintflavorDataLength=[mSystemDatalength];Sequence<sal_Int8>imgData(flavorDataLength);memcpy(imgData.getArray(),[mSystemDatabytes],flavorDataLength);Sequence<sal_Int8>pngData;if(ImageToPNG(imgData,pngData))oOOData<<=pngData;}else{oOOData=mData;}returnoOOData;}classFileListDataProvider:publicDataProviderBaseImpl{public:FileListDataProvider(constAny&data);FileListDataProvider(NSArray*data);virtualNSData*getSystemData()override;virtualAnygetOOoData()override;};FileListDataProvider::FileListDataProvider(constAny&data):DataProviderBaseImpl(data){}FileListDataProvider::FileListDataProvider(NSArray*data):DataProviderBaseImpl(data){}NSData*FileListDataProvider::getSystemData(){return[NSDatadata];}AnyFileListDataProvider::getOOoData(){AnyoOOData;if(mSystemData){size_tlength=[mSystemDatacount];size_tlenSeqRequired=0;for(size_ti=0;i<length;i++){NSString*fname=[mSystemDataobjectAtIndex:i];lenSeqRequired+=[fnamemaximumLengthOfBytesUsingEncoding:NSUnicodeStringEncoding]+sizeof(unichar);}Sequence<sal_Int8>oOOFileList(lenSeqRequired);unichar*pBuffer=reinterpret_cast<unichar*>(oOOFileList.getArray());memset(pBuffer,0,lenSeqRequired);for(size_ti=0;i<length;i++){NSString*fname=[mSystemDataobjectAtIndex:i];[fnamegetCharacters:pBuffer];size_tl=[fnamelength];pBuffer+=l+1;}oOOData<<=oOOFileList;}else{oOOData=mData;}returnoOOData;}DataFlavorMapper::DataFlavorMapper(){Reference<XComponentContext>xContext=comphelper::getProcessComponentContext();mrXMimeCntFactory=MimeContentTypeFactory::create(xContext);}DataFlavorMapper::~DataFlavorMapper(){// release potential NSStringsfor(OfficeOnlyTypes::iteratorit=maOfficeOnlyTypes.begin();it!=maOfficeOnlyTypes.end();++it){[it->secondrelease];it->second=nil;}}DataFlavorDataFlavorMapper::systemToOpenOfficeFlavor(constNSString*systemDataFlavor)const{DataFlavoroOOFlavor;for(size_ti=0;i<SIZE_FLAVOR_MAP;i++){if([systemDataFlavorcaseInsensitiveCompare:const_cast<NSString*>(flavorMap[i].SystemFlavor)]==NSOrderedSame){oOOFlavor.MimeType=OUString::createFromAscii(flavorMap[i].OOoFlavor);oOOFlavor.HumanPresentableName=OUString::createFromAscii(flavorMap[i].HumanPresentableName);oOOFlavor.DataType=flavorMap[i].DataTypeOUString?cppu::UnoType<OUString>::get():cppu::UnoType<Sequence<sal_Int8>>::get();returnoOOFlavor;}}// for// look if this might be an internal type; if it comes in here it must have// been through openOfficeToSystemFlavor before, so it should then be in the mapOUStringaTryFlavor(NSStringToOUString(systemDataFlavor));if(maOfficeOnlyTypes.find(aTryFlavor)!=maOfficeOnlyTypes.end()){oOOFlavor.MimeType=aTryFlavor;oOOFlavor.HumanPresentableName.clear();oOOFlavor.DataType=cppu::UnoType<Sequence<sal_Int8>>::get();}returnoOOFlavor;}constNSString*DataFlavorMapper::openOfficeToSystemFlavor(constDataFlavor&oOOFlavor,bool&rbInternal)const{constNSString*sysFlavor=nullptr;rbInternal=false;for(size_ti=0;i<SIZE_FLAVOR_MAP;++i){if(oOOFlavor.MimeType.startsWith(OUString::createFromAscii(flavorMap[i].OOoFlavor))){sysFlavor=flavorMap[i].SystemFlavor;}}if(!sysFlavor){rbInternal=true;OfficeOnlyTypes::const_iteratorit=maOfficeOnlyTypes.find(oOOFlavor.MimeType);if(it==maOfficeOnlyTypes.end())sysFlavor=maOfficeOnlyTypes[oOOFlavor.MimeType]=OUStringToNSString(oOOFlavor.MimeType);elsesysFlavor=it->second;}returnsysFlavor;}NSString*DataFlavorMapper::openOfficeImageToSystemFlavor(NSPasteboard*pPasteboard){NSArray*supportedTypes=[NSArrayarrayWithObjects:NSPasteboardTypeTIFF,nil];NSString*sysFlavor=[pPasteboardavailableTypeFromArray:supportedTypes];returnsysFlavor;}DataProviderPtr_tDataFlavorMapper::getDataProvider(constNSString*systemFlavor,Reference<XTransferable>const&rTransferable)const{DataProviderPtr_tdp;try{DataFlavoroOOFlavor=systemToOpenOfficeFlavor(systemFlavor);Anydata=rTransferable->getTransferData(oOOFlavor);if(isByteSequenceType(data.getValueType())){/* the HTMLFormatDataProvider prepends segment information to HTML this is useful for exchange with MS Word (which brings this stuff from Windows) but annoying for other applications. Since this extension is not a standard datatype on the Mac, let us not provide but provide normal HTML if ([systemFlavor caseInsensitiveCompare: NSHTMLPboardType] == NSOrderedSame) { dp = DataProviderPtr_t(new HTMLFormatDataProvider(data)); } else */if([systemFlavorcaseInsensitiveCompare: NSPasteboardTypeTIFF]==NSOrderedSame){dp=DataProviderPtr_t(newPNGDataProvider(data,NSBitmapImageFileTypeTIFF));}SAL_WNODEPRECATED_DECLARATIONS_PUSH// "'NSFilenamesPboardType' is deprecated: first deprecated in macOS 10.14 - Create// multiple pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead"elseif([systemFlavorcaseInsensitiveCompare: NSFilenamesPboardType]==NSOrderedSame)SAL_WNODEPRECATED_DECLARATIONS_POP{dp=DataProviderPtr_t(newFileListDataProvider(data));}else{dp=DataProviderPtr_t(newByteSequenceDataProvider(data));}}else// Must be OUString type{SAL_WARN_IF(!isOUStringType(data.getValueType()),"vcl","must be OUString type");dp=DataProviderPtr_t(newUniDataProvider(data));}}catch(UnsupportedFlavorException&){// Somebody violates the contract of the clipboard// interface @see XTransferable}returndp;}DataProviderPtr_tDataFlavorMapper::getDataProvider(constNSString*/*systemFlavor*/,NSArray*systemData){returnDataProviderPtr_t(newFileListDataProvider(systemData));}DataProviderPtr_tDataFlavorMapper::getDataProvider(constNSString*systemFlavor,NSData*systemData){DataProviderPtr_tdp;if([systemFlavorcaseInsensitiveCompare: NSPasteboardTypeString]==NSOrderedSame){dp=DataProviderPtr_t(newUniDataProvider(systemData));}elseif([systemFlavorcaseInsensitiveCompare: NSPasteboardTypeHTML]==NSOrderedSame){dp=DataProviderPtr_t(newHTMLFormatDataProvider(systemData));}elseif([systemFlavorcaseInsensitiveCompare: NSPasteboardTypeTIFF]==NSOrderedSame){dp=DataProviderPtr_t(newPNGDataProvider(systemData,NSBitmapImageFileTypeTIFF));}SAL_WNODEPRECATED_DECLARATIONS_PUSH// "'NSFilenamesPboardType' is deprecated: first deprecated in macOS 10.14 - Create multiple// pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead"elseif([systemFlavorcaseInsensitiveCompare: NSFilenamesPboardType]==NSOrderedSame)SAL_WNODEPRECATED_DECLARATIONS_POP{//dp = DataProviderPtr_t(new FileListDataProvider(systemData));}else{dp=DataProviderPtr_t(newByteSequenceDataProvider(systemData));}returndp;}boolDataFlavorMapper::isValidMimeContentType(constOUString&contentType)const{boolresult=true;try{Reference<XMimeContentType>xCntType(mrXMimeCntFactory->createMimeContentType(contentType));}catch(IllegalArgumentException&){result=false;}returnresult;}NSArray*DataFlavorMapper::flavorSequenceToTypesArray(constcss::uno::Sequence<css::datatransfer::DataFlavor>&flavors)const{sal_uInt32nFlavors=flavors.getLength();NSMutableArray*array=[[NSMutableArrayalloc]initWithCapacity:1];boolbNeedDummyInternalFlavor(false);for(sal_uInt32i=0;i<nFlavors;i++){if(flavors[i].MimeType.startsWith("image/bmp")){[arrayaddObject:NSPasteboardTypeTIFF];}else{constNSString*str=openOfficeToSystemFlavor(flavors[i],bNeedDummyInternalFlavor);if(str!=nullptr){[strretain];[arrayaddObject:str];}}}// #i89462# #i90747#// in case no system flavor was found to report// report at least one so D&D between OOo targets worksif([arraycount]==0||bNeedDummyInternalFlavor){[arrayaddObject:PBTYPE_DUMMY_INTERNAL];}return[arrayautorelease];}css::uno::Sequence<css::datatransfer::DataFlavor>DataFlavorMapper::typesArrayToFlavorSequence(NSArray*types)const{intnFormats=[typescount];Sequence<DataFlavor>flavors;for(inti=0;i<nFormats;i++){NSString*sysFormat=[typesobjectAtIndex:i];DataFlavoroOOFlavor=systemToOpenOfficeFlavor(sysFormat);if(isValidFlavor(oOOFlavor)){flavors.realloc(flavors.getLength()+1);flavors[flavors.getLength()-1]=oOOFlavor;}}returnflavors;}NSArray*DataFlavorMapper::getAllSupportedPboardTypes(){NSMutableArray*array=[[NSMutableArrayalloc]initWithCapacity:SIZE_FLAVOR_MAP];for(sal_uInt32i=0;i<SIZE_FLAVOR_MAP;i++){[arrayaddObject:flavorMap[i].SystemFlavor];}return[arrayautorelease];}/* vim:set shiftwidth=4 softtabstop=4 expandtab: */