LibreOffice Module vcl (master)  1
unohelp2.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/log.hxx>
21 #include <vcl/unohelp2.hxx>
22 #include <sot/exchange.hxx>
23 #include <sot/formats.hxx>
24 #include <vcl/svapp.hxx>
25 #include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
26 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
27 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
29 
30 using namespace ::com::sun::star;
31 
32 namespace vcl::unohelper {
33 
34  TextDataObject::TextDataObject( const OUString& rText ) : maText( rText )
35  {
36  }
37 
39  {
40  }
41 
42  void TextDataObject::CopyStringTo( const OUString& rContent,
43  const uno::Reference< datatransfer::clipboard::XClipboard >& rxClipboard,
44  std::function<void (int, const char*)> *callback)
45  {
46  SAL_WARN_IF( !rxClipboard.is(), "vcl", "TextDataObject::CopyStringTo: invalid clipboard!" );
47  if ( !rxClipboard.is() )
48  return;
49 
50  TextDataObject* pDataObj = new TextDataObject( rContent );
51 
52  SolarMutexReleaser aReleaser;
53  try
54  {
55  rxClipboard->setContents( pDataObj, nullptr );
56 
57  uno::Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( rxClipboard, uno::UNO_QUERY );
58  if( xFlushableClipboard.is() )
59  xFlushableClipboard->flushClipboard();
60 
61  if (callback != nullptr && comphelper::LibreOfficeKit::isActive())
62  {
63  boost::property_tree::ptree aTree;
64  aTree.put("content", rContent);
65  aTree.put("mimeType", "text/plain");
66  std::stringstream aStream;
67  boost::property_tree::write_json(aStream, aTree);
68  (*callback)(LOK_CALLBACK_CLIPBOARD_CHANGED, aStream.str().c_str());
69  }
70  }
71  catch( const uno::Exception& )
72  {
73  }
74  }
75 
76  // css::uno::XInterface
78  {
79  uno::Any aRet = ::cppu::queryInterface( rType, static_cast< datatransfer::XTransferable* >(this) );
80  return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
81  }
82 
83  // css::datatransfer::XTransferable
84  uno::Any TextDataObject::getTransferData( const datatransfer::DataFlavor& rFlavor )
85  {
87  if ( nT != SotClipboardFormatId::STRING )
88  {
89  throw datatransfer::UnsupportedFlavorException();
90  }
91  return uno::Any(maText);
92  }
93 
94  uno::Sequence< datatransfer::DataFlavor > TextDataObject::getTransferDataFlavors( )
95  {
96  uno::Sequence< datatransfer::DataFlavor > aDataFlavors(1);
97  SotExchange::GetFormatDataFlavor( SotClipboardFormatId::STRING, aDataFlavors.getArray()[0] );
98  return aDataFlavors;
99  }
100 
101  sal_Bool TextDataObject::isDataFlavorSupported( const datatransfer::DataFlavor& rFlavor )
102  {
104  return ( nT == SotClipboardFormatId::STRING );
105  }
106 
107 } // namespace vcl::unohelper
108 
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool hasValue()
virtual ~TextDataObject() override
Definition: unohelp2.cxx:38
TextDataObject(const OUString &rText)
Definition: unohelp2.cxx:34
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
Definition: unohelp2.cxx:77
A helper class that calls Application::ReleaseSolarMutex() in its constructor and restores the mutex ...
Definition: svapp.hxx:1404
SotClipboardFormatId
css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor &aFlavor) override
Definition: unohelp2.cxx:84
css::uno::Sequence< css::datatransfer::DataFlavor > SAL_CALL getTransferDataFlavors() override
Definition: unohelp2.cxx:94
unsigned char sal_Bool
static void CopyStringTo(const OUString &rContent, const css::uno::Reference< css::datatransfer::clipboard::XClipboard > &rxClipboard, std::function< void(int, const char *)> *callback=nullptr)
copies a given string to a given clipboard
Definition: unohelp2.cxx:42
static SotClipboardFormatId GetFormat(const css::datatransfer::DataFlavor &rFlavor)
Text maText
#define SAL_WARN_IF(condition, area, stream)
sal_Bool SAL_CALL isDataFlavorSupported(const css::datatransfer::DataFlavor &aFlavor) override
Definition: unohelp2.cxx:101
static bool GetFormatDataFlavor(SotClipboardFormatId nFormat, css::datatransfer::DataFlavor &rFlavor)