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