Related: tdf#137748 "Update" should have use-underline
[LibreOffice.git] / vcl / osx / OSXTransferable.cxx
blobf3ed1ec83faeba77dfb9f212ccae506165d9826f
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 osl;
34 using namespace cppu;
35 using namespace com::sun::star::uno;
36 using namespace com::sun::star::datatransfer;
37 using namespace com::sun::star::lang;
39 namespace
41 bool isValidFlavor( const DataFlavor& aFlavor )
43 size_t len = aFlavor.MimeType.getLength();
44 Type dtype = aFlavor.DataType;
45 return ((len > 0) && ((dtype == cppu::UnoType<Sequence<sal_Int8>>::get()) || (dtype == cppu::UnoType<OUString>::get())));
48 bool cmpAllContentTypeParameter(const Reference<XMimeContentType> & xLhs,
49 const Reference<XMimeContentType> & xRhs)
51 Sequence<OUString> xLhsFlavors = xLhs->getParameters();
52 Sequence<OUString> xRhsFlavors = xRhs->getParameters();
54 // Stop here if the number of parameters is different already
55 if (xLhsFlavors.getLength() != xRhsFlavors.getLength())
56 return false;
58 try
60 OUString pLhs;
61 OUString pRhs;
63 for (sal_Int32 i = 0; i < xLhsFlavors.getLength(); i++)
65 pLhs = xLhs->getParameterValue(xLhsFlavors[i]);
66 pRhs = xRhs->getParameterValue(xLhsFlavors[i]);
68 if (!pLhs.equalsIgnoreAsciiCase(pRhs))
70 return false;
74 catch(IllegalArgumentException&)
76 return false;
79 return true;
82 } // unnamed namespace
84 OSXTransferable::OSXTransferable(const Reference<XMimeContentTypeFactory> & rXMimeCntFactory,
85 DataFlavorMapperPtr_t pDataFlavorMapper,
86 NSPasteboard* pasteboard) :
87 mrXMimeCntFactory(rXMimeCntFactory),
88 mDataFlavorMapper(pDataFlavorMapper),
89 mPasteboard(pasteboard)
91 [mPasteboard retain];
93 initClipboardItemList();
96 OSXTransferable::~OSXTransferable()
98 [mPasteboard release];
101 Any SAL_CALL OSXTransferable::getTransferData( const DataFlavor& aFlavor )
103 if (!isValidFlavor(aFlavor) || !isDataFlavorSupported(aFlavor))
105 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
106 static_cast<XTransferable*>(this));
109 bool bInternal(false);
110 NSString const * sysFormat =
111 (aFlavor.MimeType.startsWith("image/png"))
112 ? DataFlavorMapper::openOfficeImageToSystemFlavor( mPasteboard )
113 : mDataFlavorMapper->openOfficeToSystemFlavor(aFlavor, bInternal);
114 DataProviderPtr_t dp;
116 SAL_WNODEPRECATED_DECLARATIONS_PUSH
117 // "'NSFilenamesPboardType' is deprecated: first deprecated in macOS 10.14 - Create multiple
118 // pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead"
119 if ([sysFormat caseInsensitiveCompare: NSFilenamesPboardType] == NSOrderedSame)
120 SAL_WNODEPRECATED_DECLARATIONS_POP
122 NSArray* sysData = [mPasteboard propertyListForType: const_cast<NSString *>(sysFormat)];
123 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
125 else
127 NSData* sysData = [mPasteboard dataForType: const_cast<NSString *>(sysFormat)];
128 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
131 if (!dp)
133 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
134 static_cast<XTransferable*>(this));
137 return dp->getOOoData();
140 Sequence< DataFlavor > SAL_CALL OSXTransferable::getTransferDataFlavors( )
142 return mFlavorList;
145 sal_Bool SAL_CALL OSXTransferable::isDataFlavorSupported(const DataFlavor& aFlavor)
147 for (const DataFlavor& rFlavor : std::as_const(mFlavorList))
148 if (compareDataFlavors(aFlavor, rFlavor))
149 return true;
151 return false;
154 void OSXTransferable::initClipboardItemList()
156 NSArray* pboardFormats = [mPasteboard types];
158 if (pboardFormats == nullptr)
160 throw RuntimeException("AquaClipboard: Cannot get clipboard data",
161 static_cast<XTransferable*>(this));
164 mFlavorList = mDataFlavorMapper->typesArrayToFlavorSequence(pboardFormats);
167 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
168 and the number of parameter and all parameter values do match otherwise false
169 is returned.
171 bool OSXTransferable::compareDataFlavors(const DataFlavor& lhs, const DataFlavor& rhs )
175 Reference<XMimeContentType> xLhs(mrXMimeCntFactory->createMimeContentType(lhs.MimeType));
176 Reference<XMimeContentType> xRhs(mrXMimeCntFactory->createMimeContentType(rhs.MimeType));
178 if (!xLhs->getFullMediaType().equalsIgnoreAsciiCase(xRhs->getFullMediaType()) ||
179 !cmpAllContentTypeParameter(xLhs, xRhs))
181 return false;
184 catch( IllegalArgumentException& )
186 OSL_FAIL( "Invalid content type detected" );
187 return false;
190 return true;
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */