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"
37 using namespace com::sun::star::uno
;
38 using namespace com::sun::star::datatransfer
;
39 using namespace com::sun::star::lang
;
43 bool isValidFlavor(const DataFlavor
& aFlavor
)
45 size_t len
= aFlavor
.MimeType
.getLength();
46 Type dtype
= aFlavor
.DataType
;
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())
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
))
78 catch (IllegalArgumentException
&)
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
]))
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
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
))
166 catch (IllegalArgumentException
&)
168 OSL_FAIL("Invalid content type detected");
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */