LibreOffice Module desktop (master)  1
lokclipboard.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 
10 #include "lokclipboard.hxx"
11 #include <unordered_map>
12 #include <vcl/lazydelete.hxx>
13 #include <sfx2/lokhelper.hxx>
14 #include <sal/log.hxx>
16 
17 using namespace css;
18 using namespace css::uno;
19 
20 /* static */ osl::Mutex LOKClipboardFactory::gMutex;
22 gClipboards(new std::unordered_map<int, rtl::Reference<LOKClipboard>>);
23 
25 {
26  int nViewId = SfxLokHelper::getView(); // currently active.
27 
28  osl::MutexGuard aGuard(gMutex);
29 
30  auto it = gClipboards.get()->find(nViewId);
31  if (it != gClipboards.get()->end())
32  {
33  SAL_INFO("lok", "Got clip: " << it->second.get() << " from " << nViewId);
34  return it->second;
35  }
37  (*gClipboards.get())[nViewId] = xClip;
38  SAL_INFO("lok", "Created clip: " << xClip.get() << " for viewId " << nViewId);
39  return xClip;
40 }
41 
43 {
44  osl::MutexGuard aGuard(gMutex);
45 
46  if (nViewId < 0) // clear all
47  {
48  gClipboards.get()->clear();
49  SAL_INFO("lok", "Released all clipboards on doc destroy\n");
50  }
51  else if (gClipboards.get())
52  {
53  auto it = gClipboards.get()->find(nViewId);
54  if (it != gClipboards.get()->end())
55  {
56  SAL_INFO("lok", "Releasing clip: " << it->second.get() << " for destroyed " << nViewId);
57  gClipboards.get()->erase(it);
58  }
59  }
60 }
61 
62 uno::Reference<uno::XInterface>
63  SAL_CALL LOKClipboardFactory::createInstanceWithArguments(const Sequence<Any>& /* rArgs */)
64 {
65  return uno::Reference<uno::XInterface>(
66  static_cast<cppu::OWeakObject*>(getClipboardForCurView().get()));
67 }
68 
70  : cppu::WeakComponentImplHelper<css::datatransfer::clipboard::XSystemClipboard,
71  css::lang::XServiceInfo>(m_aMutex)
72 {
73  // Encourage 'paste' menu items to always show up.
74  uno::Reference<datatransfer::XTransferable> xTransferable(new LOKTransferable());
75  setContents(xTransferable, uno::Reference<datatransfer::clipboard::XClipboardOwner>());
76 }
77 
79 {
80  Sequence<OUString> aRet{ "com.sun.star.datatransfer.clipboard.SystemClipboard" };
81  return aRet;
82 }
83 
84 OUString LOKClipboard::getImplementationName() { return "com.sun.star.datatransfer.LOKClipboard"; }
85 
87 {
89 }
90 
91 sal_Bool LOKClipboard::supportsService(const OUString& ServiceName)
92 {
93  return cppu::supportsService(this, ServiceName);
94 }
95 
97 
101 {
102  osl::ClearableMutexGuard aGuard(m_aMutex);
103  Reference<datatransfer::clipboard::XClipboardOwner> xOldOwner(m_aOwner);
104  Reference<datatransfer::XTransferable> xOldContents(m_xTransferable);
105  m_xTransferable = xTrans;
106  m_aOwner = xClipboardOwner;
107 
108  std::vector<Reference<datatransfer::clipboard::XClipboardListener>> aListeners(m_aListeners);
109  datatransfer::clipboard::ClipboardEvent aEv;
110  aEv.Contents = m_xTransferable;
111  SAL_INFO("lok", "Clip: " << this << " set contents to " << m_xTransferable);
112 
113  aGuard.clear();
114 
115  if (xOldOwner.is() && xOldOwner != xClipboardOwner)
116  xOldOwner->lostOwnership(this, xOldContents);
117  for (auto const& listener : aListeners)
118  {
119  listener->changedContents(aEv);
120  }
121 }
122 
124  const Reference<datatransfer::clipboard::XClipboardListener>& listener)
125 {
126  osl::ClearableMutexGuard aGuard(m_aMutex);
127  m_aListeners.push_back(listener);
128 }
129 
131  const Reference<datatransfer::clipboard::XClipboardListener>& listener)
132 {
133  osl::ClearableMutexGuard aGuard(m_aMutex);
134  m_aListeners.erase(std::remove(m_aListeners.begin(), m_aListeners.end(), listener),
135  m_aListeners.end());
136 }
137 LOKTransferable::LOKTransferable(const OUString& sMimeType,
138  const css::uno::Sequence<sal_Int8>& aSequence)
139 {
140  m_aContent.reserve(1);
141  m_aFlavors = css::uno::Sequence<css::datatransfer::DataFlavor>(1);
142  initFlavourFromMime(m_aFlavors[0], sMimeType);
143 
144  uno::Any aContent;
146  {
147  auto pText = reinterpret_cast<const sal_Char*>(aSequence.getConstArray());
148  aContent <<= OUString(pText, aSequence.getLength(), RTL_TEXTENCODING_UTF8);
149  }
150  else
151  aContent <<= aSequence;
152  m_aContent.push_back(aContent);
153 }
154 
157 {
158  m_aContent.reserve(1);
159  m_aFlavors = css::uno::Sequence<css::datatransfer::DataFlavor>(1);
160  initFlavourFromMime(m_aFlavors[0], "text/plain");
161  uno::Any aContent;
162  aContent <<= OUString();
163  m_aContent.push_back(aContent);
164 }
165 
166 // cf. sot/source/base/exchange.cxx for these two exceptional types.
167 void LOKTransferable::initFlavourFromMime(css::datatransfer::DataFlavor& rFlavor,
168  OUString aMimeType)
169 {
170  if (aMimeType.startsWith("text/plain"))
171  {
172  aMimeType = "text/plain;charset=utf-16";
173  rFlavor.DataType = cppu::UnoType<OUString>::get();
174  }
175  else if (aMimeType == "application/x-libreoffice-tsvc")
176  rFlavor.DataType = cppu::UnoType<OUString>::get();
177  else
178  rFlavor.DataType = cppu::UnoType<uno::Sequence<sal_Int8>>::get();
179  rFlavor.MimeType = aMimeType;
180  rFlavor.HumanPresentableName = aMimeType;
181 }
182 
183 LOKTransferable::LOKTransferable(const size_t nInCount, const char** pInMimeTypes,
184  const size_t* pInSizes, const char** pInStreams)
185 {
186  m_aContent.reserve(nInCount);
187  m_aFlavors = css::uno::Sequence<css::datatransfer::DataFlavor>(nInCount);
188  for (size_t i = 0; i < nInCount; ++i)
189  {
190  initFlavourFromMime(m_aFlavors[i], OUString::fromUtf8(pInMimeTypes[i]));
191 
192  uno::Any aContent;
194  aContent <<= OUString(pInStreams[i], pInSizes[i], RTL_TEXTENCODING_UTF8);
195  else
196  aContent <<= css::uno::Sequence<sal_Int8>(
197  reinterpret_cast<const sal_Int8*>(pInStreams[i]), pInSizes[i]);
198  m_aContent.push_back(aContent);
199  }
200 }
201 
202 uno::Any SAL_CALL LOKTransferable::getTransferData(const datatransfer::DataFlavor& rFlavor)
203 {
204  assert(m_aContent.size() == static_cast<size_t>(m_aFlavors.getLength()));
205  for (size_t i = 0; i < m_aContent.size(); ++i)
206  {
207  if (m_aFlavors[i].MimeType == rFlavor.MimeType)
208  {
209  if (m_aFlavors[i].DataType != rFlavor.DataType)
210  SAL_WARN("lok", "Horror type mismatch!");
211  return m_aContent[i];
212  }
213  }
214  return uno::Any();
215 }
216 
217 uno::Sequence<datatransfer::DataFlavor> SAL_CALL LOKTransferable::getTransferDataFlavors()
218 {
219  return m_aFlavors;
220 }
221 
222 sal_Bool SAL_CALL LOKTransferable::isDataFlavorSupported(const datatransfer::DataFlavor& rFlavor)
223 {
224  return std::find_if(m_aFlavors.begin(), m_aFlavors.end(),
225  [&rFlavor](const datatransfer::DataFlavor& i) {
226  return i.MimeType == rFlavor.MimeType && i.DataType == rFlavor.DataType;
227  })
228  != m_aFlavors.end();
229 }
230 
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::datatransfer::clipboard::XClipboardOwner > m_aOwner
static void initFlavourFromMime(css::datatransfer::DataFlavor &rFlavor, OUString aMimeType)
::osl::Mutex m_aMutex
Sequence< OUString > SAL_CALL getSupportedServiceNames() override
signed char sal_Int8
void SAL_CALL setContents(const css::uno::Reference< css::datatransfer::XTransferable > &xTransferable, const css::uno::Reference< css::datatransfer::clipboard::XClipboardOwner > &xClipboardOwner) override
static rtl::Reference< LOKClipboard > getClipboardForCurView()
Fetch clipboard from the global pool.
void SAL_CALL removeClipboardListener(const css::uno::Reference< css::datatransfer::clipboard::XClipboardListener > &listener) override
LOKTransferable()
Use to ensure we have some dummy content on the clipboard to allow a 1st 'paste'. ...
css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor &rFlavor) override
DataType
char sal_Char
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
sal_Bool SAL_CALL isDataFlavorSupported(const css::datatransfer::DataFlavor &rFlavor) override
css::uno::Sequence< css::datatransfer::DataFlavor > SAL_CALL getTransferDataFlavors() override
Listeners aListeners
static Sequence< OUString > getSupportedServiceNames_static()
osl::Mutex m_aMutex
css::uno::Reference< css::datatransfer::XTransferable > SAL_CALL getContents() override
void SAL_CALL addClipboardListener(const css::uno::Reference< css::datatransfer::clipboard::XClipboardListener > &listener) override
static int getView(SfxViewShell *pViewShell=nullptr)
sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
int i
unsigned char sal_Bool
css::uno::Type const & get()
css::uno::Sequence< css::datatransfer::DataFlavor > m_aFlavors
std::vector< css::uno::Reference< css::datatransfer::clipboard::XClipboardListener > > m_aListeners
A clipboard implementation for LibreOfficeKit.
#define SAL_INFO(area, stream)
css::uno::Reference< css::datatransfer::XTransferable > m_xTransferable
static void releaseClipboardForView(int nViewId)
Release a clipboard before its document dies, nViewId of -1 clears all.
#define SAL_WARN(area, stream)
std::vector< css::uno::Any > m_aContent
static vcl::DeleteOnDeinit< std::unordered_map< int, rtl::Reference< LOKClipboard > > > gClipboards(new std::unordered_map< int, rtl::Reference< LOKClipboard >>)
static osl::Mutex gMutex
OUString SAL_CALL getImplementationName() override
Represents the contents of LOKClipboard.
css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(const css::uno::Sequence< css::uno::Any > &) override