bump product version to 7.2.5.1
[LibreOffice.git] / vcl / ios / iOSTransferable.cxx
blob6e1bd00b3a974207e83ff3094bf4e352645ab80b
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 std;
35 using namespace osl;
36 using namespace cppu;
37 using namespace com::sun::star::uno;
38 using namespace com::sun::star::datatransfer;
39 using namespace com::sun::star::lang;
41 namespace
43 bool isValidFlavor(const DataFlavor& aFlavor)
45 size_t len = aFlavor.MimeType.getLength();
46 Type dtype = aFlavor.DataType;
47 return ((len > 0)
48 && ((dtype == cppu::UnoType<Sequence<sal_Int8>>::get())
49 || (dtype == cppu::UnoType<OUString>::get())));
52 bool cmpAllContentTypeParameter(const Reference<XMimeContentType>& xLhs,
53 const Reference<XMimeContentType>& xRhs)
55 Sequence<OUString> xLhsFlavors = xLhs->getParameters();
56 Sequence<OUString> xRhsFlavors = xRhs->getParameters();
58 // Stop here if the number of parameters is different already
59 if (xLhsFlavors.getLength() != xRhsFlavors.getLength())
60 return false;
62 try
64 OUString pLhs;
65 OUString pRhs;
67 for (sal_Int32 i = 0; i < xLhsFlavors.getLength(); i++)
69 pLhs = xLhs->getParameterValue(xLhsFlavors[i]);
70 pRhs = xRhs->getParameterValue(xLhsFlavors[i]);
72 if (!pLhs.equalsIgnoreAsciiCase(pRhs))
74 return false;
78 catch (IllegalArgumentException&)
80 return false;
83 return true;
86 } // unnamed namespace
88 iOSTransferable::iOSTransferable(const Reference<XMimeContentTypeFactory>& rXMimeCntFactory,
89 std::shared_ptr<DataFlavorMapper> pDataFlavorMapper)
90 : mrXMimeCntFactory(rXMimeCntFactory)
91 , mDataFlavorMapper(pDataFlavorMapper)
93 initClipboardItemList();
96 iOSTransferable::~iOSTransferable() {}
98 Any SAL_CALL iOSTransferable::getTransferData(const DataFlavor& aFlavor)
100 if (!isValidFlavor(aFlavor) || !isDataFlavorSupported(aFlavor))
102 throw UnsupportedFlavorException("Unsupported data flavor",
103 static_cast<XTransferable*>(this));
106 bool bInternal(false);
107 NSString* sysFormat = (aFlavor.MimeType.startsWith("image/png"))
108 ? DataFlavorMapper::openOfficeImageToSystemFlavor()
109 : mDataFlavorMapper->openOfficeToSystemFlavor(aFlavor, bInternal);
110 DataProviderPtr_t dp;
112 NSData* sysData = [[UIPasteboard generalPasteboard] dataForPasteboardType:sysFormat];
113 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
115 if (dp.get() == nullptr)
117 throw UnsupportedFlavorException("Unsupported data flavor",
118 static_cast<XTransferable*>(this));
121 return dp->getOOoData();
124 Sequence<DataFlavor> SAL_CALL iOSTransferable::getTransferDataFlavors() { return mFlavorList; }
126 sal_Bool SAL_CALL iOSTransferable::isDataFlavorSupported(const DataFlavor& aFlavor)
128 for (sal_Int32 i = 0; i < mFlavorList.getLength(); i++)
129 if (compareDataFlavors(aFlavor, mFlavorList[i]))
130 return true;
132 return false;
135 void iOSTransferable::initClipboardItemList()
137 NSArray* pboardFormats = [[UIPasteboard generalPasteboard] pasteboardTypes];
139 if (pboardFormats == nullptr)
141 throw RuntimeException("Cannot get clipboard data", static_cast<XTransferable*>(this));
144 SAL_INFO("vcl.ios.clipboard", "Types on clipboard: " << NSStringArrayToOUString(pboardFormats));
146 mFlavorList = mDataFlavorMapper->typesArrayToFlavorSequence(pboardFormats);
149 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
150 and the number of parameter and all parameter values do match otherwise false
151 is returned.
153 bool iOSTransferable::compareDataFlavors(const DataFlavor& lhs, const DataFlavor& rhs)
157 Reference<XMimeContentType> xLhs(mrXMimeCntFactory->createMimeContentType(lhs.MimeType));
158 Reference<XMimeContentType> xRhs(mrXMimeCntFactory->createMimeContentType(rhs.MimeType));
160 if (!xLhs->getFullMediaType().equalsIgnoreAsciiCase(xRhs->getFullMediaType())
161 || !cmpAllContentTypeParameter(xLhs, xRhs))
163 return false;
166 catch (IllegalArgumentException&)
168 OSL_FAIL("Invalid content type detected");
169 return false;
172 return true;
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */