bump product version to 6.4.0.3
[LibreOffice.git] / test / source / helper / transferable.cxx
blobec182f53e961bb7cc474c80dfa601ec91c4de20d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
10 #include <test/helper/transferable.hxx>
11 #include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
13 using namespace css;
15 namespace apitest
17 namespace helper
19 namespace transferable
21 OString OOO_DLLPUBLIC_TEST getTextSelection(
22 const css::uno::Reference<css::datatransfer::XTransferable>& xTransferable, OString mimeType)
24 if (!xTransferable.is())
25 return OString();
27 // Take care of UTF-8 text here.
28 bool bConvert = false;
29 sal_Int32 nIndex = 0;
30 if (mimeType.getToken(0, ';', nIndex) == "text/plain")
32 if (mimeType.getToken(0, ';', nIndex) == "charset=utf-8")
34 mimeType = "text/plain;charset=utf-16";
35 bConvert = true;
39 datatransfer::DataFlavor aFlavor;
40 aFlavor.MimeType = OUString::fromUtf8(mimeType.getStr());
41 if (mimeType == "text/plain;charset=utf-16")
42 aFlavor.DataType = cppu::UnoType<OUString>::get();
43 else
44 aFlavor.DataType = cppu::UnoType<uno::Sequence<sal_Int8>>::get();
46 if (!xTransferable.is() || !xTransferable->isDataFlavorSupported(aFlavor))
47 return OString();
49 uno::Any aAny;
50 try
52 aAny = xTransferable->getTransferData(aFlavor);
54 catch (const css::datatransfer::UnsupportedFlavorException&)
56 return OString();
58 catch (const css::uno::Exception&)
60 return OString();
63 OString aRet;
64 if (aFlavor.DataType == cppu::UnoType<OUString>::get())
66 OUString aString;
67 aAny >>= aString;
68 if (bConvert)
69 aRet = OUStringToOString(aString, RTL_TEXTENCODING_UTF8);
70 else
71 aRet = OString(reinterpret_cast<const sal_Char*>(aString.getStr()),
72 aString.getLength() * sizeof(sal_Unicode));
74 else
76 uno::Sequence<sal_Int8> aSequence;
77 aAny >>= aSequence;
78 aRet = OString(reinterpret_cast<sal_Char*>(aSequence.getArray()), aSequence.getLength());
80 return aRet;
83 } // namespace transferable
84 } // namespace helper
85 } // namespace apitest
87 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */