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_dtrans.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
< XComponentContext
>& context
,
67 const Reference
<XMimeContentTypeFactory
> rXMimeCntFactory
,
68 DataFlavorMapperPtr_t pDataFlavorMapper
,
69 NSPasteboard
* pasteboard
) :
70 mXComponentContext(context
),
71 mrXMimeCntFactory(rXMimeCntFactory
),
72 mDataFlavorMapper(pDataFlavorMapper
),
73 mPasteboard(pasteboard
)
77 initClipboardItemList();
81 OSXTransferable::~OSXTransferable()
83 [mPasteboard release
];
87 Any SAL_CALL
OSXTransferable::getTransferData( const DataFlavor
& aFlavor
)
88 throw( UnsupportedFlavorException
, IOException
, RuntimeException
)
90 if (!isValidFlavor(aFlavor
) || !isDataFlavorSupported(aFlavor
))
92 throw UnsupportedFlavorException(OUString(RTL_CONSTASCII_USTRINGPARAM("AquaClipboard: Unsupported data flavor")),
93 static_cast<XTransferable
*>(this));
96 NSString
* sysFormat
= (NSString
*)mDataFlavorMapper
->openOfficeToSystemFlavor(aFlavor
);
99 if ([sysFormat caseInsensitiveCompare
: NSFilenamesPboardType
] == NSOrderedSame
)
101 NSArray
* sysData
= [mPasteboard propertyListForType
: sysFormat
];
102 dp
= mDataFlavorMapper
->getDataProvider(sysFormat
, sysData
);
106 NSData
* sysData
= [mPasteboard dataForType
: sysFormat
];
107 dp
= mDataFlavorMapper
->getDataProvider(sysFormat
, sysData
);
110 if (dp
.get() == NULL
)
112 throw UnsupportedFlavorException(OUString(RTL_CONSTASCII_USTRINGPARAM("AquaClipboard: Unsupported data flavor")),
113 static_cast<XTransferable
*>(this));
116 return dp
->getOOoData();
120 bool OSXTransferable::isUnicodeText(const DataFlavor
& flavor
)
122 return (flavor
.DataType
== CPPUTYPE_OUSTRING
);
126 Sequence
< DataFlavor
> SAL_CALL
OSXTransferable::getTransferDataFlavors( )
127 throw( RuntimeException
)
133 sal_Bool SAL_CALL
OSXTransferable::isDataFlavorSupported(const DataFlavor
& aFlavor
)
134 throw( RuntimeException
)
136 for (sal_Int32 i
= 0; i
< mFlavorList
.getLength(); i
++)
137 if (compareDataFlavors(aFlavor
, mFlavorList
[i
]))
144 void OSXTransferable::initClipboardItemList()
146 NSArray
* pboardFormats
= [mPasteboard types
];
148 if (pboardFormats
== NULL
)
150 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM("AquaClipboard: Cannot get clipboard data")),
151 static_cast<XTransferable
*>(this));
154 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 OSXTransferable::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_ENSURE( sal_False
, "Invalid content type detected" );
185 bool OSXTransferable::cmpAllContentTypeParameter(const Reference
<XMimeContentType
> xLhs
,
186 const Reference
<XMimeContentType
> xRhs
) const
188 Sequence
<OUString
> xLhsFlavors
= xLhs
->getParameters();
189 Sequence
<OUString
> xRhsFlavors
= xRhs
->getParameters();
191 // Stop here if the number of parameters is different already
192 if (xLhsFlavors
.getLength() != xRhsFlavors
.getLength())
200 for (sal_Int32 i
= 0; i
< xLhsFlavors
.getLength(); i
++)
202 pLhs
= xLhs
->getParameterValue(xLhsFlavors
[i
]);
203 pRhs
= xRhs
->getParameterValue(xLhsFlavors
[i
]);
205 if (!pLhs
.equalsIgnoreAsciiCase(pRhs
))
211 catch(IllegalArgumentException
&)