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 { namespace 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  {
45  SAL_WARN_IF( !rxClipboard.is(), "vcl", "TextDataObject::CopyStringTo: invalid clipboard!" );
46  if ( !rxClipboard.is() )
47  return;
48 
49  TextDataObject* pDataObj = new TextDataObject( rContent );
50 
51  SolarMutexReleaser aReleaser;
52  try
53  {
54  rxClipboard->setContents( pDataObj, nullptr );
55 
56  uno::Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( rxClipboard, uno::UNO_QUERY );
57  if( xFlushableClipboard.is() )
58  xFlushableClipboard->flushClipboard();
59  }
60  catch( const uno::Exception& )
61  {
62  }
63  }
64 
65  // css::uno::XInterface
67  {
68  uno::Any aRet = ::cppu::queryInterface( rType, static_cast< datatransfer::XTransferable* >(this) );
69  return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
70  }
71 
72  // css::datatransfer::XTransferable
73  uno::Any TextDataObject::getTransferData( const datatransfer::DataFlavor& rFlavor )
74  {
76  if ( nT != SotClipboardFormatId::STRING )
77  {
78  throw datatransfer::UnsupportedFlavorException();
79  }
80  return uno::Any(maText);
81  }
82 
83  uno::Sequence< datatransfer::DataFlavor > TextDataObject::getTransferDataFlavors( )
84  {
85  uno::Sequence< datatransfer::DataFlavor > aDataFlavors(1);
86  SotExchange::GetFormatDataFlavor( SotClipboardFormatId::STRING, aDataFlavors.getArray()[0] );
87  return aDataFlavors;
88  }
89 
90  sal_Bool TextDataObject::isDataFlavorSupported( const datatransfer::DataFlavor& rFlavor )
91  {
93  return ( nT == SotClipboardFormatId::STRING );
94  }
95 
96 }} // namespace vcl::unohelper
97 
98 /* 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:66
A helper class that calls Application::ReleaseSolarMutex() in its constructor and restores the mutex ...
Definition: svapp.hxx:1433
SotClipboardFormatId
css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor &aFlavor) override
Definition: unohelp2.cxx:73
css::uno::Sequence< css::datatransfer::DataFlavor > SAL_CALL getTransferDataFlavors() override
Definition: unohelp2.cxx:83
unsigned char sal_Bool
static void CopyStringTo(const OUString &rContent, const css::uno::Reference< css::datatransfer::clipboard::XClipboard > &rxClipboard)
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:90
static bool GetFormatDataFlavor(SotClipboardFormatId nFormat, css::datatransfer::DataFlavor &rFlavor)