ofz#46905 Null-dereference
[LibreOffice.git] / vcl / ios / iOSTransferable.cxx
blobece771e7f60bf560127c00e6114ad168be25c1a5
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 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
114 if (dp.get() == nullptr)
116 throw UnsupportedFlavorException("Unsupported data flavor",
117 static_cast<XTransferable*>(this));
120 return dp->getOOoData();
123 Sequence<DataFlavor> SAL_CALL iOSTransferable::getTransferDataFlavors() { return mFlavorList; }
125 sal_Bool SAL_CALL iOSTransferable::isDataFlavorSupported(const DataFlavor& aFlavor)
127 for (sal_Int32 i = 0; i < mFlavorList.getLength(); i++)
128 if (compareDataFlavors(aFlavor, mFlavorList[i]))
129 return true;
131 return false;
134 void iOSTransferable::initClipboardItemList()
136 NSArray* pboardFormats = [[UIPasteboard generalPasteboard] pasteboardTypes];
138 if (pboardFormats == nullptr)
140 throw RuntimeException("Cannot get clipboard data", static_cast<XTransferable*>(this));
143 SAL_INFO("vcl.ios.clipboard", "Types on clipboard: " << NSStringArrayToOUString(pboardFormats));
145 mFlavorList = mDataFlavorMapper->typesArrayToFlavorSequence(pboardFormats);
148 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
149 and the number of parameter and all parameter values do match otherwise false
150 is returned.
152 bool iOSTransferable::compareDataFlavors(const DataFlavor& lhs, const DataFlavor& rhs)
156 Reference<XMimeContentType> xLhs(mrXMimeCntFactory->createMimeContentType(lhs.MimeType));
157 Reference<XMimeContentType> xRhs(mrXMimeCntFactory->createMimeContentType(rhs.MimeType));
159 if (!xLhs->getFullMediaType().equalsIgnoreAsciiCase(xRhs->getFullMediaType())
160 || !cmpAllContentTypeParameter(xLhs, xRhs))
162 return false;
165 catch (IllegalArgumentException&)
167 OSL_FAIL("Invalid content type detected");
168 return false;
171 return true;
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */