1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/log.hxx>
25 #include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <sal/types.h>
28 #include <osl/diagnose.h>
30 #include "OSXTransferable.hxx"
32 #include "DataFlavorMapping.hxx"
34 #include <quartz/utils.h>
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
;
47 return ((len
> 0) && ((dtype
== cppu::UnoType
<Sequence
<sal_Int8
>>::get()) || (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())
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
))
76 catch(IllegalArgumentException
&)
84 } // unnamed namespace
86 OSXTransferable::OSXTransferable(const Reference
<XMimeContentTypeFactory
> & rXMimeCntFactory
,
87 DataFlavorMapperPtr_t pDataFlavorMapper
,
88 NSPasteboard
* pasteboard
) :
89 mrXMimeCntFactory(rXMimeCntFactory
),
90 mDataFlavorMapper(pDataFlavorMapper
),
91 mPasteboard(pasteboard
)
95 initClipboardItemList();
98 OSXTransferable::~OSXTransferable()
100 [mPasteboard release
];
103 Any SAL_CALL
OSXTransferable::getTransferData( const DataFlavor
& aFlavor
)
105 if (!isValidFlavor(aFlavor
) || !isDataFlavorSupported(aFlavor
))
107 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
108 static_cast<XTransferable
*>(this));
111 bool bInternal(false);
112 NSString
const * sysFormat
=
113 (aFlavor
.MimeType
.startsWith("image/png"))
114 ? DataFlavorMapper::openOfficeImageToSystemFlavor( mPasteboard
)
115 : mDataFlavorMapper
->openOfficeToSystemFlavor(aFlavor
, bInternal
);
116 DataProviderPtr_t dp
;
118 SAL_WNODEPRECATED_DECLARATIONS_PUSH
119 // "'NSFilenamesPboardType' is deprecated: first deprecated in macOS 10.14 - Create multiple
120 // pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead"
121 if ([sysFormat caseInsensitiveCompare
: NSFilenamesPboardType
] == NSOrderedSame
)
122 SAL_WNODEPRECATED_DECLARATIONS_POP
124 NSArray
* sysData
= [mPasteboard propertyListForType
: const_cast<NSString
*>(sysFormat
)];
125 dp
= DataFlavorMapper::getDataProvider(sysFormat
, sysData
);
129 NSData
* sysData
= [mPasteboard dataForType
: const_cast<NSString
*>(sysFormat
)];
130 dp
= DataFlavorMapper::getDataProvider(sysFormat
, sysData
);
135 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
136 static_cast<XTransferable
*>(this));
139 return dp
->getOOoData();
142 Sequence
< DataFlavor
> SAL_CALL
OSXTransferable::getTransferDataFlavors( )
147 sal_Bool SAL_CALL
OSXTransferable::isDataFlavorSupported(const DataFlavor
& aFlavor
)
149 for (const DataFlavor
& rFlavor
: std::as_const(mFlavorList
))
150 if (compareDataFlavors(aFlavor
, rFlavor
))
156 void OSXTransferable::initClipboardItemList()
158 NSArray
* pboardFormats
= [mPasteboard types
];
160 if (pboardFormats
== nullptr)
162 throw RuntimeException("AquaClipboard: Cannot get clipboard data",
163 static_cast<XTransferable
*>(this));
166 SAL_INFO("vcl.osx.clipboard", "Types on pasteboard: " << NSStringArrayToOUString(pboardFormats
));
169 mFlavorList
= mDataFlavorMapper
->typesArrayToFlavorSequence(pboardFormats
);
172 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
173 and the number of parameter and all parameter values do match otherwise false
176 bool OSXTransferable::compareDataFlavors(const DataFlavor
& lhs
, const DataFlavor
& rhs
)
180 Reference
<XMimeContentType
> xLhs(mrXMimeCntFactory
->createMimeContentType(lhs
.MimeType
));
181 Reference
<XMimeContentType
> xRhs(mrXMimeCntFactory
->createMimeContentType(rhs
.MimeType
));
183 if (!xLhs
->getFullMediaType().equalsIgnoreAsciiCase(xRhs
->getFullMediaType()) ||
184 !cmpAllContentTypeParameter(xLhs
, xRhs
))
189 catch( IllegalArgumentException
& )
191 OSL_FAIL( "Invalid content type detected" );
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */