bump product version to 7.2.5.1
[LibreOffice.git] / vcl / osx / OSXTransferable.cxx
blob6f86137991117d9bcea0c665fc7d59aa60493e02
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 <utility>
24 #include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include <sal/types.h>
27 #include <osl/diagnose.h>
29 #include "OSXTransferable.hxx"
31 #include "DataFlavorMapping.hxx"
33 using namespace std;
34 using namespace osl;
35 using namespace cppu;
36 using namespace com::sun::star::uno;
37 using namespace com::sun::star::datatransfer;
38 using namespace com::sun::star::lang;
40 namespace
42 bool isValidFlavor( const DataFlavor& aFlavor )
44 size_t len = aFlavor.MimeType.getLength();
45 Type dtype = aFlavor.DataType;
46 return ((len > 0) && ((dtype == cppu::UnoType<Sequence<sal_Int8>>::get()) || (dtype == cppu::UnoType<OUString>::get())));
49 bool cmpAllContentTypeParameter(const Reference<XMimeContentType> & xLhs,
50 const Reference<XMimeContentType> & xRhs)
52 Sequence<OUString> xLhsFlavors = xLhs->getParameters();
53 Sequence<OUString> xRhsFlavors = xRhs->getParameters();
55 // Stop here if the number of parameters is different already
56 if (xLhsFlavors.getLength() != xRhsFlavors.getLength())
57 return false;
59 try
61 OUString pLhs;
62 OUString pRhs;
64 for (sal_Int32 i = 0; i < xLhsFlavors.getLength(); i++)
66 pLhs = xLhs->getParameterValue(xLhsFlavors[i]);
67 pRhs = xRhs->getParameterValue(xLhsFlavors[i]);
69 if (!pLhs.equalsIgnoreAsciiCase(pRhs))
71 return false;
75 catch(IllegalArgumentException&)
77 return false;
80 return true;
83 } // unnamed namespace
85 OSXTransferable::OSXTransferable(const Reference<XMimeContentTypeFactory> & rXMimeCntFactory,
86 DataFlavorMapperPtr_t pDataFlavorMapper,
87 NSPasteboard* pasteboard) :
88 mrXMimeCntFactory(rXMimeCntFactory),
89 mDataFlavorMapper(pDataFlavorMapper),
90 mPasteboard(pasteboard)
92 [mPasteboard retain];
94 initClipboardItemList();
97 OSXTransferable::~OSXTransferable()
99 [mPasteboard release];
102 Any SAL_CALL OSXTransferable::getTransferData( const DataFlavor& aFlavor )
104 if (!isValidFlavor(aFlavor) || !isDataFlavorSupported(aFlavor))
106 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
107 static_cast<XTransferable*>(this));
110 bool bInternal(false);
111 NSString const * sysFormat =
112 (aFlavor.MimeType.startsWith("image/png"))
113 ? DataFlavorMapper::openOfficeImageToSystemFlavor( mPasteboard )
114 : mDataFlavorMapper->openOfficeToSystemFlavor(aFlavor, bInternal);
115 DataProviderPtr_t dp;
117 SAL_WNODEPRECATED_DECLARATIONS_PUSH
118 // "'NSFilenamesPboardType' is deprecated: first deprecated in macOS 10.14 - Create multiple
119 // pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead"
120 if ([sysFormat caseInsensitiveCompare: NSFilenamesPboardType] == NSOrderedSame)
121 SAL_WNODEPRECATED_DECLARATIONS_POP
123 NSArray* sysData = [mPasteboard propertyListForType: const_cast<NSString *>(sysFormat)];
124 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
126 else
128 NSData* sysData = [mPasteboard dataForType: const_cast<NSString *>(sysFormat)];
129 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
132 if (!dp)
134 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
135 static_cast<XTransferable*>(this));
138 return dp->getOOoData();
141 Sequence< DataFlavor > SAL_CALL OSXTransferable::getTransferDataFlavors( )
143 return mFlavorList;
146 sal_Bool SAL_CALL OSXTransferable::isDataFlavorSupported(const DataFlavor& aFlavor)
148 for (const DataFlavor& rFlavor : std::as_const(mFlavorList))
149 if (compareDataFlavors(aFlavor, rFlavor))
150 return true;
152 return false;
155 void OSXTransferable::initClipboardItemList()
157 NSArray* pboardFormats = [mPasteboard types];
159 if (pboardFormats == nullptr)
161 throw RuntimeException("AquaClipboard: Cannot get clipboard data",
162 static_cast<XTransferable*>(this));
165 mFlavorList = mDataFlavorMapper->typesArrayToFlavorSequence(pboardFormats);
168 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
169 and the number of parameter and all parameter values do match otherwise false
170 is returned.
172 bool OSXTransferable::compareDataFlavors(const DataFlavor& lhs, const DataFlavor& rhs )
176 Reference<XMimeContentType> xLhs(mrXMimeCntFactory->createMimeContentType(lhs.MimeType));
177 Reference<XMimeContentType> xRhs(mrXMimeCntFactory->createMimeContentType(rhs.MimeType));
179 if (!xLhs->getFullMediaType().equalsIgnoreAsciiCase(xRhs->getFullMediaType()) ||
180 !cmpAllContentTypeParameter(xLhs, xRhs))
182 return false;
185 catch( IllegalArgumentException& )
187 OSL_FAIL( "Invalid content type detected" );
188 return false;
191 return true;
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */