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 #include <boost/property_tree/json_parser.hpp>
30 #include <comphelper/lok.hxx>
31 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
32 
33 using namespace ::com::sun::star;
34 
35 namespace vcl::unohelper {
36 
37  TextDataObject::TextDataObject( const OUString& rText ) : maText( rText )
38  {
39  }
40 
42  {
43  }
44 
45  void TextDataObject::CopyStringTo( const OUString& rContent,
46  const uno::Reference< datatransfer::clipboard::XClipboard >& rxClipboard,
47  const vcl::ILibreOfficeKitNotifier* pNotifier)
48  {
49  SAL_WARN_IF( !rxClipboard.is(), "vcl", "TextDataObject::CopyStringTo: invalid clipboard!" );
50  if ( !rxClipboard.is() )
51  return;
52 
53  rtl::Reference<TextDataObject> pDataObj = new TextDataObject( rContent );
54 
55  SolarMutexReleaser aReleaser;
56  try
57  {
58  rxClipboard->setContents( pDataObj, nullptr );
59 
60  uno::Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( rxClipboard, uno::UNO_QUERY );
61  if( xFlushableClipboard.is() )
62  xFlushableClipboard->flushClipboard();
63 
64  if (pNotifier != nullptr && comphelper::LibreOfficeKit::isActive())
65  {
66  boost::property_tree::ptree aTree;
67  aTree.put("content", rContent);
68  aTree.put("mimeType", "text/plain");
69  std::stringstream aStream;
70  boost::property_tree::write_json(aStream, aTree);
71  pNotifier->libreOfficeKitViewCallback(LOK_CALLBACK_CLIPBOARD_CHANGED, aStream.str().c_str());
72  }
73  }
74  catch( const uno::Exception& )
75  {
76  }
77  }
78 
79  // css::uno::XInterface
81  {
82  uno::Any aRet = ::cppu::queryInterface( rType, static_cast< datatransfer::XTransferable* >(this) );
83  return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
84  }
85 
86  // css::datatransfer::XTransferable
87  uno::Any TextDataObject::getTransferData( const datatransfer::DataFlavor& rFlavor )
88  {
90  if ( nT != SotClipboardFormatId::STRING )
91  {
92  throw datatransfer::UnsupportedFlavorException();
93  }
94  return uno::Any(maText);
95  }
96 
97  uno::Sequence< datatransfer::DataFlavor > TextDataObject::getTransferDataFlavors( )
98  {
99  uno::Sequence< datatransfer::DataFlavor > aDataFlavors(1);
100  SotExchange::GetFormatDataFlavor( SotClipboardFormatId::STRING, aDataFlavors.getArray()[0] );
101  return aDataFlavors;
102  }
103 
104  sal_Bool TextDataObject::isDataFlavorSupported( const datatransfer::DataFlavor& rFlavor )
105  {
107  return ( nT == SotClipboardFormatId::STRING );
108  }
109 
110 } // namespace vcl::unohelper
111 
112 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void CopyStringTo(const OUString &rContent, const css::uno::Reference< css::datatransfer::clipboard::XClipboard > &rxClipboard, const vcl::ILibreOfficeKitNotifier *pNotifier=nullptr)
copies a given string to a given clipboard
Definition: unohelp2.cxx:45
bool hasValue()
virtual ~TextDataObject() override
Definition: unohelp2.cxx:41
TextDataObject(const OUString &rText)
Definition: unohelp2.cxx:37
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
Definition: unohelp2.cxx:80
A helper class that calls Application::ReleaseSolarMutex() in its constructor and restores the mutex ...
Definition: svapp.hxx:1421
SotClipboardFormatId
css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor &aFlavor) override
Definition: unohelp2.cxx:87
css::uno::Sequence< css::datatransfer::DataFlavor > SAL_CALL getTransferDataFlavors() override
Definition: unohelp2.cxx:97
unsigned char sal_Bool
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:104
static bool GetFormatDataFlavor(SotClipboardFormatId nFormat, css::datatransfer::DataFlavor &rFlavor)