1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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"
36 using namespace com::sun::star::uno
;
37 using namespace com::sun::star::datatransfer
;
38 using namespace com::sun::star::lang
;
42 bool isValidFlavor(const DataFlavor
& aFlavor
)
44 size_t len
= aFlavor
.MimeType
.getLength();
45 Type dtype
= aFlavor
.DataType
;
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())
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
))
77 catch (IllegalArgumentException
&)
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
];
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
]))
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
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
))
175 catch (IllegalArgumentException
&)
177 OSL_FAIL("Invalid content type detected");
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */