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