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 <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <sal/types.h>
25 #include <osl/diagnose.h>
27 #include "OSXTransferable.hxx"
29 #include "DataFlavorMapping.hxx"
34 using namespace com::sun::star::uno
;
35 using namespace com::sun::star::datatransfer
;
36 using namespace com::sun::star::lang
;
40 bool isValidFlavor( const DataFlavor
& aFlavor
)
42 size_t len
= aFlavor
.MimeType
.getLength();
43 Type dtype
= aFlavor
.DataType
;
44 return ((len
> 0) && ((dtype
== cppu::UnoType
<Sequence
<sal_Int8
>>::get()) || (dtype
== cppu::UnoType
<OUString
>::get())));
47 bool cmpAllContentTypeParameter(const Reference
<XMimeContentType
> & xLhs
,
48 const Reference
<XMimeContentType
> & xRhs
)
50 Sequence
<OUString
> xLhsFlavors
= xLhs
->getParameters();
51 Sequence
<OUString
> xRhsFlavors
= xRhs
->getParameters();
53 // Stop here if the number of parameters is different already
54 if (xLhsFlavors
.getLength() != xRhsFlavors
.getLength())
62 for (sal_Int32 i
= 0; i
< xLhsFlavors
.getLength(); i
++)
64 pLhs
= xLhs
->getParameterValue(xLhsFlavors
[i
]);
65 pRhs
= xRhs
->getParameterValue(xLhsFlavors
[i
]);
67 if (!pLhs
.equalsIgnoreAsciiCase(pRhs
))
73 catch(IllegalArgumentException
&)
81 } // unnamed namespace
83 OSXTransferable::OSXTransferable(const Reference
<XMimeContentTypeFactory
> & rXMimeCntFactory
,
84 DataFlavorMapperPtr_t pDataFlavorMapper
,
85 NSPasteboard
* pasteboard
) :
86 mrXMimeCntFactory(rXMimeCntFactory
),
87 mDataFlavorMapper(pDataFlavorMapper
),
88 mPasteboard(pasteboard
)
92 initClipboardItemList();
95 OSXTransferable::~OSXTransferable()
97 [mPasteboard release
];
100 Any SAL_CALL
OSXTransferable::getTransferData( const DataFlavor
& aFlavor
)
102 if (!isValidFlavor(aFlavor
) || !isDataFlavorSupported(aFlavor
))
104 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
105 static_cast<XTransferable
*>(this));
108 bool bInternal(false);
109 NSString
const * sysFormat
=
110 (aFlavor
.MimeType
.startsWith("image/png"))
111 ? DataFlavorMapper::openOfficeImageToSystemFlavor( mPasteboard
)
112 : mDataFlavorMapper
->openOfficeToSystemFlavor(aFlavor
, bInternal
);
113 DataProviderPtr_t dp
;
115 if ([sysFormat caseInsensitiveCompare
: NSFilenamesPboardType
] == NSOrderedSame
)
117 NSArray
* sysData
= [mPasteboard propertyListForType
: const_cast<NSString
*>(sysFormat
)];
118 dp
= DataFlavorMapper::getDataProvider(sysFormat
, sysData
);
122 NSData
* sysData
= [mPasteboard dataForType
: const_cast<NSString
*>(sysFormat
)];
123 dp
= DataFlavorMapper::getDataProvider(sysFormat
, sysData
);
126 if (dp
.get() == nullptr)
128 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
129 static_cast<XTransferable
*>(this));
132 return dp
->getOOoData();
135 Sequence
< DataFlavor
> SAL_CALL
OSXTransferable::getTransferDataFlavors( )
140 sal_Bool SAL_CALL
OSXTransferable::isDataFlavorSupported(const DataFlavor
& aFlavor
)
142 for (sal_Int32 i
= 0; i
< mFlavorList
.getLength(); i
++)
143 if (compareDataFlavors(aFlavor
, mFlavorList
[i
]))
149 void OSXTransferable::initClipboardItemList()
151 NSArray
* pboardFormats
= [mPasteboard types
];
153 if (pboardFormats
== nullptr)
155 throw RuntimeException("AquaClipboard: Cannot get clipboard data",
156 static_cast<XTransferable
*>(this));
159 mFlavorList
= mDataFlavorMapper
->typesArrayToFlavorSequence(pboardFormats
);
162 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
163 and the number of parameter and all parameter values do match otherwise false
166 bool OSXTransferable::compareDataFlavors(const DataFlavor
& lhs
, const DataFlavor
& rhs
)
170 Reference
<XMimeContentType
> xLhs(mrXMimeCntFactory
->createMimeContentType(lhs
.MimeType
));
171 Reference
<XMimeContentType
> xRhs(mrXMimeCntFactory
->createMimeContentType(rhs
.MimeType
));
173 if (!xLhs
->getFullMediaType().equalsIgnoreAsciiCase(xRhs
->getFullMediaType()) ||
174 !cmpAllContentTypeParameter(xLhs
, xRhs
))
179 catch( IllegalArgumentException
& )
181 OSL_FAIL( "Invalid content type detected" );
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */