Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / osx / OSXTransferable.cxx
blob426b618c95bd3c7e462693152786795e2da4df9b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <sal/types.h>
25 #include <osl/diagnose.h>
27 #include "OSXTransferable.hxx"
29 #include "DataFlavorMapping.hxx"
31 using namespace std;
32 using namespace osl;
33 using namespace cppu;
34 using namespace com::sun::star::uno;
35 using namespace com::sun::star::datatransfer;
36 using namespace com::sun::star::lang;
38 namespace
40 bool isValidFlavor( const DataFlavor& aFlavor )
42 size_t len = aFlavor.MimeType.getLength();
43 Type dtype = aFlavor.DataType;
44 return ((len > 0) && ((dtype == cppu::UnoType<Sequence<sal_Int8>>::get()) || (dtype == cppu::UnoType<OUString>::get())));
47 bool cmpAllContentTypeParameter(const Reference<XMimeContentType> & xLhs,
48 const Reference<XMimeContentType> & xRhs)
50 Sequence<OUString> xLhsFlavors = xLhs->getParameters();
51 Sequence<OUString> xRhsFlavors = xRhs->getParameters();
53 // Stop here if the number of parameters is different already
54 if (xLhsFlavors.getLength() != xRhsFlavors.getLength())
55 return false;
57 try
59 OUString pLhs;
60 OUString pRhs;
62 for (sal_Int32 i = 0; i < xLhsFlavors.getLength(); i++)
64 pLhs = xLhs->getParameterValue(xLhsFlavors[i]);
65 pRhs = xRhs->getParameterValue(xLhsFlavors[i]);
67 if (!pLhs.equalsIgnoreAsciiCase(pRhs))
69 return false;
73 catch(IllegalArgumentException&)
75 return false;
78 return true;
81 } // unnamed namespace
83 OSXTransferable::OSXTransferable(const Reference<XMimeContentTypeFactory> & rXMimeCntFactory,
84 DataFlavorMapperPtr_t pDataFlavorMapper,
85 NSPasteboard* pasteboard) :
86 mrXMimeCntFactory(rXMimeCntFactory),
87 mDataFlavorMapper(pDataFlavorMapper),
88 mPasteboard(pasteboard)
90 [mPasteboard retain];
92 initClipboardItemList();
95 OSXTransferable::~OSXTransferable()
97 [mPasteboard release];
100 Any SAL_CALL OSXTransferable::getTransferData( const DataFlavor& aFlavor )
102 if (!isValidFlavor(aFlavor) || !isDataFlavorSupported(aFlavor))
104 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
105 static_cast<XTransferable*>(this));
108 bool bInternal(false);
109 NSString const * sysFormat =
110 (aFlavor.MimeType.startsWith("image/png"))
111 ? DataFlavorMapper::openOfficeImageToSystemFlavor( mPasteboard )
112 : mDataFlavorMapper->openOfficeToSystemFlavor(aFlavor, bInternal);
113 DataProviderPtr_t dp;
115 SAL_WNODEPRECATED_DECLARATIONS_PUSH
116 // "'NSFilenamesPboardType' is deprecated: first deprecated in macOS 10.14 - Create multiple
117 // pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead"
118 if ([sysFormat caseInsensitiveCompare: NSFilenamesPboardType] == NSOrderedSame)
119 SAL_WNODEPRECATED_DECLARATIONS_POP
121 NSArray* sysData = [mPasteboard propertyListForType: const_cast<NSString *>(sysFormat)];
122 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
124 else
126 NSData* sysData = [mPasteboard dataForType: const_cast<NSString *>(sysFormat)];
127 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
130 if (dp.get() == nullptr)
132 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
133 static_cast<XTransferable*>(this));
136 return dp->getOOoData();
139 Sequence< DataFlavor > SAL_CALL OSXTransferable::getTransferDataFlavors( )
141 return mFlavorList;
144 sal_Bool SAL_CALL OSXTransferable::isDataFlavorSupported(const DataFlavor& aFlavor)
146 for (sal_Int32 i = 0; i < mFlavorList.getLength(); i++)
147 if (compareDataFlavors(aFlavor, mFlavorList[i]))
148 return true;
150 return false;
153 void OSXTransferable::initClipboardItemList()
155 NSArray* pboardFormats = [mPasteboard types];
157 if (pboardFormats == nullptr)
159 throw RuntimeException("AquaClipboard: Cannot get clipboard data",
160 static_cast<XTransferable*>(this));
163 mFlavorList = mDataFlavorMapper->typesArrayToFlavorSequence(pboardFormats);
166 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
167 and the number of parameter and all parameter values do match otherwise false
168 is returned.
170 bool OSXTransferable::compareDataFlavors(const DataFlavor& lhs, const DataFlavor& rhs )
174 Reference<XMimeContentType> xLhs(mrXMimeCntFactory->createMimeContentType(lhs.MimeType));
175 Reference<XMimeContentType> xRhs(mrXMimeCntFactory->createMimeContentType(rhs.MimeType));
177 if (!xLhs->getFullMediaType().equalsIgnoreAsciiCase(xRhs->getFullMediaType()) ||
178 !cmpAllContentTypeParameter(xLhs, xRhs))
180 return false;
183 catch( IllegalArgumentException& )
185 OSL_FAIL( "Invalid content type detected" );
186 return false;
189 return true;
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */