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