1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OSXTransferable.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
33 #include <sal/types.h>
35 #ifndef _TRANSFERABLE_HXX_
36 #include "OSXTransferable.hxx"
39 #include "DataFlavorMapping.hxx"
45 using namespace com::sun::star::uno
;
46 using namespace com::sun::star::datatransfer
;
47 using namespace com::sun::star::io
;
48 using namespace com::sun::star::lang
;
49 using namespace com::sun::star::container
;
51 const Type CPPUTYPE_SEQINT8
= getCppuType((Sequence
<sal_Int8
>*)0);
52 const Type CPPUTYPE_OUSTRING
= getCppuType((OUString
*)0);
56 bool isValidFlavor( const DataFlavor
& aFlavor
)
58 size_t len
= aFlavor
.MimeType
.getLength();
59 Type dtype
= aFlavor
.DataType
;
60 return ((len
> 0) && ((dtype
== CPPUTYPE_SEQINT8
) || (dtype
== CPPUTYPE_OUSTRING
)));
63 } // namespace private
66 OSXTransferable::OSXTransferable(const Reference
<XMimeContentTypeFactory
> rXMimeCntFactory
,
67 DataFlavorMapperPtr_t pDataFlavorMapper
,
68 NSPasteboard
* pasteboard
) :
69 mrXMimeCntFactory(rXMimeCntFactory
),
70 mDataFlavorMapper(pDataFlavorMapper
),
71 mPasteboard(pasteboard
)
75 initClipboardItemList();
79 OSXTransferable::~OSXTransferable()
81 [mPasteboard release
];
85 Any SAL_CALL
OSXTransferable::getTransferData( const DataFlavor
& aFlavor
)
86 throw( UnsupportedFlavorException
, IOException
, RuntimeException
)
88 if (!isValidFlavor(aFlavor
) || !isDataFlavorSupported(aFlavor
))
90 throw UnsupportedFlavorException(OUString(RTL_CONSTASCII_USTRINGPARAM("AquaClipboard: Unsupported data flavor")),
91 static_cast<XTransferable
*>(this));
94 NSString
* sysFormat
= (NSString
*)mDataFlavorMapper
->openOfficeToSystemFlavor(aFlavor
);
97 if ([sysFormat caseInsensitiveCompare
: NSFilenamesPboardType
] == NSOrderedSame
)
99 NSArray
* sysData
= [mPasteboard propertyListForType
: sysFormat
];
100 dp
= mDataFlavorMapper
->getDataProvider(sysFormat
, sysData
);
104 NSData
* sysData
= [mPasteboard dataForType
: sysFormat
];
105 dp
= mDataFlavorMapper
->getDataProvider(sysFormat
, sysData
);
108 if (dp
.get() == NULL
)
110 throw UnsupportedFlavorException(OUString(RTL_CONSTASCII_USTRINGPARAM("AquaClipboard: Unsupported data flavor")),
111 static_cast<XTransferable
*>(this));
114 return dp
->getOOoData();
118 bool OSXTransferable::isUnicodeText(const DataFlavor
& flavor
)
120 return (flavor
.DataType
== CPPUTYPE_OUSTRING
);
124 Sequence
< DataFlavor
> SAL_CALL
OSXTransferable::getTransferDataFlavors( )
125 throw( RuntimeException
)
131 sal_Bool SAL_CALL
OSXTransferable::isDataFlavorSupported(const DataFlavor
& aFlavor
)
132 throw( RuntimeException
)
134 for (sal_Int32 i
= 0; i
< mFlavorList
.getLength(); i
++)
135 if (compareDataFlavors(aFlavor
, mFlavorList
[i
]))
142 void OSXTransferable::initClipboardItemList()
144 NSArray
* pboardFormats
= [mPasteboard types
];
146 if (pboardFormats
== NULL
)
148 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM("AquaClipboard: Cannot get clipboard data")),
149 static_cast<XTransferable
*>(this));
152 mFlavorList
= mDataFlavorMapper
->typesArrayToFlavorSequence(pboardFormats
);
156 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
157 and the number of parameter and all parameter values do match otherwise false
160 bool OSXTransferable::compareDataFlavors(const DataFlavor
& lhs
, const DataFlavor
& rhs
)
164 Reference
<XMimeContentType
> xLhs(mrXMimeCntFactory
->createMimeContentType(lhs
.MimeType
));
165 Reference
<XMimeContentType
> xRhs(mrXMimeCntFactory
->createMimeContentType(rhs
.MimeType
));
167 if (!xLhs
->getFullMediaType().equalsIgnoreAsciiCase(xRhs
->getFullMediaType()) ||
168 !cmpAllContentTypeParameter(xLhs
, xRhs
))
173 catch( IllegalArgumentException
& )
175 OSL_ENSURE( sal_False
, "Invalid content type detected" );
183 bool OSXTransferable::cmpAllContentTypeParameter(const Reference
<XMimeContentType
> xLhs
,
184 const Reference
<XMimeContentType
> xRhs
) const
186 Sequence
<OUString
> xLhsFlavors
= xLhs
->getParameters();
187 Sequence
<OUString
> xRhsFlavors
= xRhs
->getParameters();
189 // Stop here if the number of parameters is different already
190 if (xLhsFlavors
.getLength() != xRhsFlavors
.getLength())
198 for (sal_Int32 i
= 0; i
< xLhsFlavors
.getLength(); i
++)
200 pLhs
= xLhs
->getParameterValue(xLhsFlavors
[i
]);
201 pRhs
= xRhs
->getParameterValue(xLhsFlavors
[i
]);
203 if (!pLhs
.equalsIgnoreAsciiCase(pRhs
))
209 catch(IllegalArgumentException
&)