odk: don't run compiler -dumpversion for eaach include of settings.mk
[LibreOffice.git] / vcl / ios / iOSTransferable.cxx
blobbfbc8a9afa8291be57deb2106dd861c51894d441
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/log.hxx>
25 #include <sal/types.h>
26 #include <osl/diagnose.h>
28 #include <quartz/utils.h>
30 #include "iOSTransferable.hxx"
32 #include "DataFlavorMapping.hxx"
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)
47 && ((dtype == cppu::UnoType<Sequence<sal_Int8>>::get())
48 || (dtype == cppu::UnoType<OUString>::get())));
51 bool cmpAllContentTypeParameter(const Reference<XMimeContentType>& xLhs,
52 const Reference<XMimeContentType>& xRhs)
54 Sequence<OUString> xLhsFlavors = xLhs->getParameters();
55 Sequence<OUString> xRhsFlavors = xRhs->getParameters();
57 // Stop here if the number of parameters is different already
58 if (xLhsFlavors.getLength() != xRhsFlavors.getLength())
59 return false;
61 try
63 OUString pLhs;
64 OUString pRhs;
66 for (sal_Int32 i = 0; i < xLhsFlavors.getLength(); i++)
68 pLhs = xLhs->getParameterValue(xLhsFlavors[i]);
69 pRhs = xRhs->getParameterValue(xLhsFlavors[i]);
71 if (!pLhs.equalsIgnoreAsciiCase(pRhs))
73 return false;
77 catch (IllegalArgumentException&)
79 return false;
82 return true;
85 } // unnamed namespace
87 iOSTransferable::iOSTransferable(const Reference<XMimeContentTypeFactory>& rXMimeCntFactory,
88 std::shared_ptr<DataFlavorMapper> pDataFlavorMapper)
89 : mrXMimeCntFactory(rXMimeCntFactory)
90 , mDataFlavorMapper(pDataFlavorMapper)
92 initClipboardItemList();
95 iOSTransferable::~iOSTransferable() {}
97 Any SAL_CALL iOSTransferable::getTransferData(const DataFlavor& aFlavor)
99 if (!isValidFlavor(aFlavor) || !isDataFlavorSupported(aFlavor))
101 throw UnsupportedFlavorException("Unsupported data flavor",
102 static_cast<XTransferable*>(this));
105 bool bInternal(false);
106 NSString* sysFormat = (aFlavor.MimeType.startsWith("image/png"))
107 ? DataFlavorMapper::openOfficeImageToSystemFlavor()
108 : mDataFlavorMapper->openOfficeToSystemFlavor(aFlavor, bInternal);
109 DataProviderPtr_t dp;
111 NSData* sysData = [[UIPasteboard generalPasteboard] dataForPasteboardType:sysFormat];
112 if (!sysData)
114 // Related: gh#5908 throw an exception if the data flavor is nil
115 // If nil is returned, it can mean that the user has selected the
116 // "disallow" option and so we can't access the current clipboard
117 // contents. Also, by throwing an exception, the "allow or disallow"
118 // dialog will display again the next time the user tries to paste.
119 throw UnsupportedFlavorException("Data flavor is nil", static_cast<XTransferable*>(this));
122 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
124 if (dp.get() == nullptr)
126 throw UnsupportedFlavorException("Unsupported data flavor",
127 static_cast<XTransferable*>(this));
130 return dp->getOOoData();
133 Sequence<DataFlavor> SAL_CALL iOSTransferable::getTransferDataFlavors() { return mFlavorList; }
135 sal_Bool SAL_CALL iOSTransferable::isDataFlavorSupported(const DataFlavor& aFlavor)
137 for (sal_Int32 i = 0; i < mFlavorList.getLength(); i++)
138 if (compareDataFlavors(aFlavor, mFlavorList[i]))
139 return true;
141 return false;
144 void iOSTransferable::initClipboardItemList()
146 NSArray* pboardFormats = [[UIPasteboard generalPasteboard] pasteboardTypes];
148 if (pboardFormats == nullptr)
150 throw RuntimeException("Cannot get clipboard data", static_cast<XTransferable*>(this));
153 SAL_INFO("vcl.ios.clipboard", "Types on clipboard: " << NSStringArrayToOUString(pboardFormats));
155 mFlavorList = mDataFlavorMapper->typesArrayToFlavorSequence(pboardFormats);
158 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
159 and the number of parameter and all parameter values do match otherwise false
160 is returned.
162 bool iOSTransferable::compareDataFlavors(const DataFlavor& lhs, const DataFlavor& rhs)
166 Reference<XMimeContentType> xLhs(mrXMimeCntFactory->createMimeContentType(lhs.MimeType));
167 Reference<XMimeContentType> xRhs(mrXMimeCntFactory->createMimeContentType(rhs.MimeType));
169 if (!xLhs->getFullMediaType().equalsIgnoreAsciiCase(xRhs->getFullMediaType())
170 || !cmpAllContentTypeParameter(xLhs, xRhs))
172 return false;
175 catch (IllegalArgumentException&)
177 OSL_FAIL("Invalid content type detected");
178 return false;
181 return true;
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */