LibreOffice Module vcl (master) 1
dtranscomp.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/config.h>
21
22#include <comphelper/lok.hxx>
24#include <osl/mutex.hxx>
25#include <tools/debug.hxx>
26#include <vcl/svapp.hxx>
27
28#include <factory.hxx>
29#include <svdata.hxx>
30#include <salinst.hxx>
31
32#include <com/sun/star/lang/IllegalArgumentException.hpp>
33#include <com/sun/star/lang/XMultiServiceFactory.hpp>
34#include <com/sun/star/lang/XServiceInfo.hpp>
35#include <com/sun/star/lang/XSingleServiceFactory.hpp>
36#include <com/sun/star/lang/XInitialization.hpp>
37#include <com/sun/star/datatransfer/XTransferable.hpp>
38#include <com/sun/star/datatransfer/clipboard/LokClipboard.hpp>
39#include <com/sun/star/datatransfer/clipboard/XClipboardListener.hpp>
40#include <com/sun/star/datatransfer/clipboard/XSystemClipboard.hpp>
41#include <com/sun/star/datatransfer/dnd/XDragSource.hpp>
42#include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
43#include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
44
47
48using namespace com::sun::star;
49using namespace com::sun::star::uno;
50using namespace com::sun::star::lang;
51
52namespace vcl
53{
54namespace {
55
56// generic implementation to satisfy SalInstance
57class GenericClipboard :
59 datatransfer::clipboard::XSystemClipboard,
60 XServiceInfo
61 >
62{
65 std::vector< Reference< css::datatransfer::clipboard::XClipboardListener > > m_aListeners;
66
67public:
68
69 GenericClipboard()
70 {}
71
72 /*
73 * XServiceInfo
74 */
75
76 virtual OUString SAL_CALL getImplementationName() override;
77 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
78 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
79
80 static Sequence< OUString > getSupportedServiceNames_static();
81
82 /*
83 * XClipboard
84 */
85
86 virtual Reference< css::datatransfer::XTransferable > SAL_CALL getContents() override;
87
88 virtual void SAL_CALL setContents(
90 const Reference< css::datatransfer::clipboard::XClipboardOwner >& xClipboardOwner ) override;
91
92 virtual OUString SAL_CALL getName() override;
93
94 /*
95 * XClipboardEx
96 */
97
98 virtual sal_Int8 SAL_CALL getRenderingCapabilities() override;
99
100 /*
101 * XClipboardNotifier
102 */
103 virtual void SAL_CALL addClipboardListener(
105
106 virtual void SAL_CALL removeClipboardListener(
108};
109
110}
111
112Sequence< OUString > GenericClipboard::getSupportedServiceNames_static()
113{
114 Sequence< OUString > aRet { "com.sun.star.datatransfer.clipboard.SystemClipboard" };
115 return aRet;
116}
117
118OUString GenericClipboard::getImplementationName()
119{
120 return "com.sun.star.datatransfer.VCLGenericClipboard";
121}
122
123Sequence< OUString > GenericClipboard::getSupportedServiceNames()
124{
125 return getSupportedServiceNames_static();
126}
127
128sal_Bool GenericClipboard::supportsService( const OUString& ServiceName )
129{
130 return cppu::supportsService(this, ServiceName);
131}
132
133Reference< css::datatransfer::XTransferable > GenericClipboard::getContents()
134{
135 return m_aContents;
136}
137
138void GenericClipboard::setContents(
141{
142 std::unique_lock aGuard( m_aMutex );
145 m_aContents = xTrans;
146 m_aOwner = xClipboardOwner;
147
148 std::vector< Reference< datatransfer::clipboard::XClipboardListener > > aListeners( m_aListeners );
149 datatransfer::clipboard::ClipboardEvent aEv;
150 aEv.Contents = m_aContents;
151
152 aGuard.unlock();
153
154 if( xOldOwner.is() && xOldOwner != xClipboardOwner )
155 xOldOwner->lostOwnership( this, xOldContents );
156 for (auto const& listener : aListeners)
157 {
158 listener->changedContents( aEv );
159 }
160}
161
162OUString GenericClipboard::getName()
163{
164 return "CLIPBOARD";
165}
166
167sal_Int8 GenericClipboard::getRenderingCapabilities()
168{
169 return 0;
170}
171
172void GenericClipboard::addClipboardListener( const Reference< datatransfer::clipboard::XClipboardListener >& listener )
173{
174 std::unique_lock aGuard(m_aMutex);
175
176 m_aListeners.push_back( listener );
177}
178
179void GenericClipboard::removeClipboardListener( const Reference< datatransfer::clipboard::XClipboardListener >& listener )
180{
181 std::unique_lock aGuard(m_aMutex);
182
183 m_aListeners.erase(std::remove(m_aListeners.begin(), m_aListeners.end(), listener), m_aListeners.end());
184}
185
186
187
188extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
190 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const& args)
191{
192 SolarMutexGuard aGuard;
193 auto xClipboard = ImplGetSVData()->mpDefInst->CreateClipboard( args );
194 if (xClipboard.is())
195 xClipboard->acquire();
196 return xClipboard.get();
197}
198
199namespace {
200
201/*
202* generic DragSource dummy
203*/
204class GenericDragSource : public ::comphelper::WeakComponentImplHelper<
205 datatransfer::dnd::XDragSource,
206 XInitialization,
207 css::lang::XServiceInfo
208 >
209{
210 css::uno::Reference<css::datatransfer::XTransferable> m_xTrans;
211public:
212 GenericDragSource() {}
213
214 // XDragSource
215 virtual sal_Bool SAL_CALL isDragImageSupported() override;
216 virtual sal_Int32 SAL_CALL getDefaultCursor( sal_Int8 dragAction ) override;
217 virtual void SAL_CALL startDrag(
218 const datatransfer::dnd::DragGestureEvent& trigger,
219 sal_Int8 sourceActions, sal_Int32 cursor, sal_Int32 image,
222 ) override;
223
224 // XInitialization
225 virtual void SAL_CALL initialize( const Sequence< Any >& arguments ) override;
226
227 OUString SAL_CALL getImplementationName() override
228 { return "com.sun.star.datatransfer.dnd.VclGenericDragSource"; }
229
230 sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
231 { return cppu::supportsService(this, ServiceName); }
232
233 css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
234 { return getSupportedServiceNames_static(); }
235
236 static Sequence< OUString > getSupportedServiceNames_static()
237 {
238 return { "com.sun.star.datatransfer.dnd.GenericDragSource" };
239 }
240};
241
242}
243
244sal_Bool GenericDragSource::isDragImageSupported()
245{
246 return false;
247}
248
249sal_Int32 GenericDragSource::getDefaultCursor( sal_Int8 )
250{
251 return 0;
252}
253
254void GenericDragSource::startDrag( const datatransfer::dnd::DragGestureEvent&,
255 sal_Int8 /*sourceActions*/, sal_Int32 /*cursor*/, sal_Int32 /*image*/,
258 )
259{
261 m_xTrans = rTrans;
262 return;
263 }
264
265 datatransfer::dnd::DragSourceDropEvent aEv;
266 aEv.DropAction = datatransfer::dnd::DNDConstants::ACTION_COPY;
267 aEv.DropSuccess = false;
268 listener->dragDropEnd( aEv );
269}
270
271void GenericDragSource::initialize( const Sequence< Any >& )
272{
273}
274
276{
277#if defined MACOSX
278 return { "com.sun.star.datatransfer.dnd.OleDragSource" };
279#elif defined UNX
280 return { "com.sun.star.datatransfer.dnd.X11DragSource" };
281#else
282 return { "com.sun.star.datatransfer.dnd.VclGenericDragSource" };
283#endif
284}
285
287{
288#if defined MACOSX
289 return "com.sun.star.comp.datatransfer.dnd.OleDragSource_V1";
290#elif defined UNX
291 return "com.sun.star.datatransfer.dnd.XdndSupport";
292#else
293 return "com.sun.star.datatransfer.dnd.VclGenericDragSource";
294#endif
295}
296
298{
299 SolarMutexGuard aGuard;
301 return xResult;
302}
303
304/*
305* generic DragSource dummy
306*/
307
308namespace {
309
310class GenericDropTarget : public comphelper::WeakComponentImplHelper<
311 datatransfer::dnd::XDropTarget,
312 XInitialization,
313 css::lang::XServiceInfo
314 >
315{
316public:
317 GenericDropTarget() {}
318
319 // XInitialization
320 virtual void SAL_CALL initialize( const Sequence< Any >& args ) override;
321
322 // XDropTarget
323 virtual void SAL_CALL addDropTargetListener( const Reference< css::datatransfer::dnd::XDropTargetListener >& ) override;
324 virtual void SAL_CALL removeDropTargetListener( const Reference< css::datatransfer::dnd::XDropTargetListener >& ) override;
325 virtual sal_Bool SAL_CALL isActive() override;
326 virtual void SAL_CALL setActive( sal_Bool active ) override;
327 virtual sal_Int8 SAL_CALL getDefaultActions() override;
328 virtual void SAL_CALL setDefaultActions( sal_Int8 actions ) override;
329
330 OUString SAL_CALL getImplementationName() override
331 { return "com.sun.star.datatransfer.dnd.VclGenericDropTarget"; }
332
333 sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
334 { return cppu::supportsService(this, ServiceName); }
335
336 css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
337 { return getSupportedServiceNames_static(); }
338
339 static Sequence< OUString > getSupportedServiceNames_static()
340 {
341 return { "com.sun.star.datatransfer.dnd.GenericDropTarget" };
342 }
343};
344
345}
346
347void GenericDropTarget::initialize( const Sequence< Any >& )
348{
349}
350
351void GenericDropTarget::addDropTargetListener( const Reference< css::datatransfer::dnd::XDropTargetListener >& )
352{
353}
354
355void GenericDropTarget::removeDropTargetListener( const Reference< css::datatransfer::dnd::XDropTargetListener >& )
356{
357}
358
359sal_Bool GenericDropTarget::isActive()
360{
361 return false;
362}
363
364void GenericDropTarget::setActive( sal_Bool )
365{
366}
367
368sal_Int8 GenericDropTarget::getDefaultActions()
369{
370 return 0;
371}
372
373void GenericDropTarget::setDefaultActions( sal_Int8)
374{
375}
376
378{
379#if defined MACOSX
380 return { "com.sun.star.datatransfer.dnd.OleDropTarget" };
381#elif defined UNX
382 return { "com.sun.star.datatransfer.dnd.X11DropTarget" };
383#else
384 return GenericDropTarget::getSupportedServiceNames_static();
385#endif
386}
387
389{
390 return
391 #if defined MACOSX
392 "com.sun.star.comp.datatransfer.dnd.OleDropTarget_V1"
393 #elif defined UNX
394 "com.sun.star.datatransfer.dnd.XdndDropTarget"
395 #else
396 "com.sun.star.datatransfer.dnd.VclGenericDropTarget"
397 #endif
398 ;
399}
400
402{
403 SolarMutexGuard aGuard;
405 return xResult;
406}
407
408} // namespace vcl
409
410/*
411* SalInstance generic
412*/
414{
415 if (arguments.hasElements()) {
416 throw css::lang::IllegalArgumentException(
417 "non-empty SalInstance::CreateClipboard arguments", {}, -1);
418 }
419#ifdef IOS
420 return getXWeak(new vcl::GenericClipboard());
421#else
423 // In LOK, each document view shall have its own clipboard instance (whereas
424 // in non-LOK below we keep handing out one single instance; see also
425 // <https://lists.freedesktop.org/archives/libreoffice/2020-April/084824.html> "Re: Linux
426 // SAL_USE_VCLPLUGIN=svp and the clipboard"):
427 css::uno::Reference<css::datatransfer::clipboard::XClipboard> xClipboard =
428 css::datatransfer::clipboard::LokClipboard::create(
430 return xClipboard;
431 }
432#endif
434 if (!m_clipboard.is()) {
435 m_clipboard = getXWeak(new vcl::GenericClipboard());
436 }
437 return m_clipboard;
438}
439
440uno::Reference<uno::XInterface> SalInstance::ImplCreateDragSource(const SystemEnvData*)
441{
442 return css::uno::Reference<css::uno::XInterface>();
443}
444
446{
447 // We run unit tests in parallel, which is a problem when touching a shared resource
448 // the system clipboard, so rather use the dummy GenericClipboard.
450 return getXWeak(new vcl::GenericDragSource());
451 return ImplCreateDragSource(pSysEnv);
452}
453
454uno::Reference<uno::XInterface> SalInstance::ImplCreateDropTarget(const SystemEnvData*)
455{
456 return css::uno::Reference<css::uno::XInterface>();
457}
458
460{
461 // see SalInstance::CreateDragSource
463 return getXWeak(new vcl::GenericDropTarget());
464 return ImplCreateDropTarget(pSysEnv);
465}
466
467/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool IsHeadlessModeEnabled()
Determines if headless mode is enabled.
Definition: svapp.cxx:1539
virtual css::uno::Reference< css::uno::XInterface > CreateClipboard(const css::uno::Sequence< css::uno::Any > &i_rArguments)
Definition: dtranscomp.cxx:413
static bool IsRunningUnitTest()
Definition: salinst.hxx:216
css::uno::Reference< css::uno::XInterface > CreateDropTarget(const SystemEnvData *=nullptr)
Definition: dtranscomp.cxx:459
virtual css::uno::Reference< css::uno::XInterface > ImplCreateDropTarget(const SystemEnvData *)
Definition: dtranscomp.cxx:454
virtual css::uno::Reference< css::uno::XInterface > ImplCreateDragSource(const SystemEnvData *)
Definition: dtranscomp.cxx:440
css::uno::Reference< css::uno::XInterface > CreateDragSource(const SystemEnvData *=nullptr)
Definition: dtranscomp.cxx:445
css::uno::Reference< css::uno::XInterface > m_clipboard
Definition: salinst.hxx:80
#define DBG_TESTSOLARMUTEX()
Listeners aListeners
Reference< css::datatransfer::clipboard::XClipboardOwner > m_aOwner
Definition: dtranscomp.cxx:64
css::uno::Reference< css::datatransfer::XTransferable > m_xTrans
Definition: dtranscomp.cxx:210
Reference< css::datatransfer::XTransferable > m_aContents
Definition: dtranscomp.cxx:63
std::vector< Reference< css::datatransfer::clipboard::XClipboardListener > > m_aListeners
Definition: dtranscomp.cxx:65
void setActive(bool bActive)
Reference< XComponentContext > getProcessComponentContext()
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
args
OUString DragSource_getImplementationName()
Definition: dtranscomp.cxx:286
Reference< XInterface > DragSource_createInstance(const Reference< XMultiServiceFactory > &)
Definition: dtranscomp.cxx:297
Sequence< OUString > DropTarget_getSupportedServiceNames()
Definition: dtranscomp.cxx:377
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * vcl_SystemClipboard_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &args)
Definition: dtranscomp.cxx:189
Sequence< OUString > DragSource_getSupportedServiceNames()
Definition: dtranscomp.cxx:275
Reference< XInterface > DropTarget_createInstance(const Reference< XMultiServiceFactory > &)
Definition: dtranscomp.cxx:401
OUString DropTarget_getImplementationName()
Definition: dtranscomp.cxx:388
SalInstance * mpDefInst
Definition: svdata.hxx:389
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:77
unsigned char sal_Bool
signed char sal_Int8