Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / ios / iOSTransferable.cxx
blob9ec27867fb0b4cec57a78eaa815dec9cca92f67e
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 "iOSTransferable.hxx"
30 #include "DataFlavorMapping.hxx"
32 using namespace std;
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)
46 && ((dtype == cppu::UnoType<Sequence<sal_Int8>>::get())
47 || (dtype == cppu::UnoType<OUString>::get())));
50 bool cmpAllContentTypeParameter(const Reference<XMimeContentType>& xLhs,
51 const Reference<XMimeContentType>& xRhs)
53 Sequence<OUString> xLhsFlavors = xLhs->getParameters();
54 Sequence<OUString> xRhsFlavors = xRhs->getParameters();
56 // Stop here if the number of parameters is different already
57 if (xLhsFlavors.getLength() != xRhsFlavors.getLength())
58 return false;
60 try
62 OUString pLhs;
63 OUString pRhs;
65 for (sal_Int32 i = 0; i < xLhsFlavors.getLength(); i++)
67 pLhs = xLhs->getParameterValue(xLhsFlavors[i]);
68 pRhs = xRhs->getParameterValue(xLhsFlavors[i]);
70 if (!pLhs.equalsIgnoreAsciiCase(pRhs))
72 return false;
76 catch (IllegalArgumentException&)
78 return false;
81 return true;
84 } // unnamed namespace
86 iOSTransferable::iOSTransferable(const Reference<XMimeContentTypeFactory>& rXMimeCntFactory,
87 std::shared_ptr<DataFlavorMapper> pDataFlavorMapper,
88 UIPasteboard* pasteboard)
89 : mrXMimeCntFactory(rXMimeCntFactory)
90 , mDataFlavorMapper(pDataFlavorMapper)
91 , mPasteboard(pasteboard)
93 [mPasteboard retain];
95 initClipboardItemList();
98 iOSTransferable::~iOSTransferable() { [mPasteboard release]; }
100 Any SAL_CALL iOSTransferable::getTransferData(const DataFlavor& aFlavor)
102 if (!isValidFlavor(aFlavor) || !isDataFlavorSupported(aFlavor))
104 throw UnsupportedFlavorException("Unsupported data flavor",
105 static_cast<XTransferable*>(this));
108 bool bInternal(false);
109 NSString* sysFormat = (aFlavor.MimeType.startsWith("image/png"))
110 ? DataFlavorMapper::openOfficeImageToSystemFlavor(mPasteboard)
111 : mDataFlavorMapper->openOfficeToSystemFlavor(aFlavor, bInternal);
112 DataProviderPtr_t dp;
114 NSData* sysData = [mPasteboard dataForPasteboardType:sysFormat];
115 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
117 if (dp.get() == nullptr)
119 throw UnsupportedFlavorException("Unsupported data flavor",
120 static_cast<XTransferable*>(this));
123 return dp->getOOoData();
126 Sequence<DataFlavor> SAL_CALL iOSTransferable::getTransferDataFlavors() { return mFlavorList; }
128 sal_Bool SAL_CALL iOSTransferable::isDataFlavorSupported(const DataFlavor& aFlavor)
130 for (sal_Int32 i = 0; i < mFlavorList.getLength(); i++)
131 if (compareDataFlavors(aFlavor, mFlavorList[i]))
132 return true;
134 return false;
137 void iOSTransferable::initClipboardItemList()
139 NSArray* pboardFormats = [mPasteboard pasteboardTypes];
141 if (pboardFormats == nullptr)
143 throw RuntimeException("Cannot get clipboard data", static_cast<XTransferable*>(this));
146 #ifdef SAL_LOG_INFO
147 NSString* types = @"";
148 for (unsigned i = 0; i < [pboardFormats count]; i++)
150 if ([types length] > 0)
151 types = [types stringByAppendingString:@", "];
152 types = [types stringByAppendingString:[pboardFormats objectAtIndex:i]];
154 SAL_INFO("vcl.ios.clipboard", "Types on clipboard: " << [types UTF8String]);
155 #endif
157 mFlavorList = mDataFlavorMapper->typesArrayToFlavorSequence(pboardFormats);
160 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
161 and the number of parameter and all parameter values do match otherwise false
162 is returned.
164 bool iOSTransferable::compareDataFlavors(const DataFlavor& lhs, const DataFlavor& rhs)
168 Reference<XMimeContentType> xLhs(mrXMimeCntFactory->createMimeContentType(lhs.MimeType));
169 Reference<XMimeContentType> xRhs(mrXMimeCntFactory->createMimeContentType(rhs.MimeType));
171 if (!xLhs->getFullMediaType().equalsIgnoreAsciiCase(xRhs->getFullMediaType())
172 || !cmpAllContentTypeParameter(xLhs, xRhs))
174 return false;
177 catch (IllegalArgumentException&)
179 OSL_FAIL("Invalid content type detected");
180 return false;
183 return true;
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */