LibreOffice Module test (master) 1
transferable.cxx
Go to the documentation of this file.
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.
4 *
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 */
9
11#include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
12#include <o3tl/string_view.hxx>
13
14using namespace css;
15
17{
19 const css::uno::Reference<css::datatransfer::XTransferable>& xTransferable, OString mimeType)
20{
21 if (!xTransferable.is())
22 return OString();
23
24 // Take care of UTF-8 text here.
25 bool bConvert = false;
26 sal_Int32 nIndex = 0;
27 if (o3tl::getToken(mimeType, 0, ';', nIndex) == "text/plain")
28 {
29 if (o3tl::getToken(mimeType, 0, ';', nIndex) == "charset=utf-8")
30 {
31 mimeType = "text/plain;charset=utf-16";
32 bConvert = true;
33 }
34 }
35
36 datatransfer::DataFlavor aFlavor;
37 aFlavor.MimeType = OUString::fromUtf8(mimeType);
38 if (mimeType == "text/plain;charset=utf-16")
39 aFlavor.DataType = cppu::UnoType<OUString>::get();
40 else
41 aFlavor.DataType = cppu::UnoType<uno::Sequence<sal_Int8>>::get();
42
43 if (!xTransferable.is() || !xTransferable->isDataFlavorSupported(aFlavor))
44 return OString();
45
46 uno::Any aAny;
47 try
48 {
49 aAny = xTransferable->getTransferData(aFlavor);
50 }
51 catch (const css::datatransfer::UnsupportedFlavorException&)
52 {
53 return OString();
54 }
55 catch (const css::uno::Exception&)
56 {
57 return OString();
58 }
59
60 OString aRet;
61 if (aFlavor.DataType == cppu::UnoType<OUString>::get())
62 {
63 OUString aString;
64 aAny >>= aString;
65 if (bConvert)
66 aRet = OUStringToOString(aString, RTL_TEXTENCODING_UTF8);
67 else
68 aRet = OString(reinterpret_cast<const char*>(aString.getStr()),
69 aString.getLength() * sizeof(sal_Unicode));
70 }
71 else
72 {
73 uno::Sequence<sal_Int8> aSequence;
74 aAny >>= aSequence;
75 aRet = OString(reinterpret_cast<char*>(aSequence.getArray()), aSequence.getLength());
76 }
77 return aRet;
78}
79
80} // namespace apitest::helper::transferable
81
82/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
css::uno::Type const & get()
sal_Int32 nIndex
OString OOO_DLLPUBLIC_TEST getTextSelection(const css::uno::Reference< css::datatransfer::XTransferable > &xTransferable, OString mimeType)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
#define OOO_DLLPUBLIC_TEST
Definition: testdllapi.hxx:28
sal_uInt16 sal_Unicode