1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <osl/diagnose.h>
21 #include "WinClipbImpl.hxx"
23 #include <systools/win32/comtools.hxx>
24 #include "../../inc/DtObjFactory.hxx"
25 #include "../dtobj/APNDataObject.hxx"
26 #include "../dtobj/DOTransferable.hxx"
27 #include "WinClipboard.hxx"
28 #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
29 #include "../dtobj/XNotifyingDataObject.hxx"
32 #pragma warning(push,1)
45 using namespace com::sun::star::uno
;
46 using namespace com::sun::star::datatransfer
;
47 using namespace com::sun::star::datatransfer::clipboard
;
48 using namespace com::sun::star::datatransfer::clipboard::RenderingCapabilities
;
50 // definition of static members
51 CWinClipbImpl
* CWinClipbImpl::s_pCWinClipbImpl
= nullptr;
52 osl::Mutex
CWinClipbImpl::s_aMutex
;
54 CWinClipbImpl::CWinClipbImpl( const OUString
& aClipboardName
, CWinClipboard
* theWinClipboard
) :
55 m_itsName( aClipboardName
),
56 m_pWinClipboard( theWinClipboard
),
57 m_pCurrentClipContent( nullptr )
59 OSL_ASSERT( nullptr != m_pWinClipboard
);
61 // necessary to reassociate from
62 // the static callback function
63 s_pCWinClipbImpl
= this;
64 registerClipboardViewer( );
67 CWinClipbImpl::~CWinClipbImpl( )
69 ClearableMutexGuard
aGuard( s_aMutex
);
70 s_pCWinClipbImpl
= nullptr;
73 unregisterClipboardViewer( );
76 Reference
< XTransferable
> SAL_CALL
CWinClipbImpl::getContents( )
78 // use the shortcut or create a transferable from
80 ClearableMutexGuard
aGuard( m_ClipContentMutex
);
82 if ( nullptr != m_pCurrentClipContent
)
84 return m_pCurrentClipContent
->m_XTransferable
;
87 // release the mutex, so that the variable may be
88 // changed by other threads
91 Reference
< XTransferable
> rClipContent
;
93 // get the current dataobject from clipboard
94 IDataObjectPtr pIDataObject
;
95 HRESULT hr
= m_MtaOleClipboard
.getClipboard( &pIDataObject
);
97 if ( SUCCEEDED( hr
) )
99 // create an apartment neutral dataobject and initialize it with a
100 // com smart pointer to the IDataObject from clipboard
101 IDataObjectPtr
pIDo( new CAPNDataObject( pIDataObject
) );
103 // remember pIDo destroys itself due to the smart pointer
104 rClipContent
= CDOTransferable::create( m_pWinClipboard
->m_xContext
, pIDo
);
110 void SAL_CALL
CWinClipbImpl::setContents(
111 const Reference
< XTransferable
>& xTransferable
,
112 const Reference
< XClipboardOwner
>& xClipboardOwner
)
114 IDataObjectPtr pIDataObj
;
116 if ( xTransferable
.is( ) )
118 ClearableMutexGuard
aGuard( m_ClipContentMutex
);
120 m_pCurrentClipContent
= new CXNotifyingDataObject(
121 CDTransObjFactory::createDataObjFromTransferable( m_pWinClipboard
->m_xContext
, xTransferable
),
128 pIDataObj
= IDataObjectPtr( m_pCurrentClipContent
);
131 m_MtaOleClipboard
.setClipboard(pIDataObj
.get());
134 OUString SAL_CALL
CWinClipbImpl::getName( )
139 sal_Int8 SAL_CALL
CWinClipbImpl::getRenderingCapabilities( )
141 return ( Delayed
| Persistant
);
144 void SAL_CALL
CWinClipbImpl::flushClipboard( )
146 // actually it should be ClearableMutexGuard aGuard( m_ClipContentMutex );
147 // but it does not work since FlushClipboard does a callback and frees DataObject
148 // which results in a deadlock in onReleaseDataObject.
149 // FlushClipboard had to be synchron in order to prevent shutdown until all
150 // clipboard-formats are rendered.
151 // The request is needed to prevent flushing if we are not clipboard owner (it is
152 // not known what happens if we flush but aren't clipoard owner).
153 // It may be possible to move the request to the clipboard STA thread by saving the
154 // DataObject and call OleIsCurrentClipboard before flushing.
156 if ( nullptr != m_pCurrentClipContent
)
157 m_MtaOleClipboard
.flushClipboard( );
160 void SAL_CALL
CWinClipbImpl::registerClipboardViewer( )
162 m_MtaOleClipboard
.registerClipViewer( CWinClipbImpl::onClipboardContentChanged
);
165 void SAL_CALL
CWinClipbImpl::unregisterClipboardViewer( )
167 m_MtaOleClipboard
.registerClipViewer( nullptr );
170 void SAL_CALL
CWinClipbImpl::dispose()
172 OSL_ENSURE( !m_pCurrentClipContent
, "Clipboard was not flushed before shutdown!" );
175 void WINAPI
CWinClipbImpl::onClipboardContentChanged()
177 MutexGuard
aGuard( s_aMutex
);
179 // reassociation to instance through static member
180 if ( nullptr != s_pCWinClipbImpl
)
181 s_pCWinClipbImpl
->m_pWinClipboard
->notifyAllClipboardListener( );
184 void SAL_CALL
CWinClipbImpl::onReleaseDataObject( CXNotifyingDataObject
* theCaller
)
186 OSL_ASSERT( nullptr != theCaller
);
189 theCaller
->lostOwnership( );
191 // if the current caller is the one we currently
192 // hold, then set it to NULL because an external
193 // source must be the clipboardowner now
194 MutexGuard
aGuard( m_ClipContentMutex
);
196 if ( m_pCurrentClipContent
== theCaller
)
197 m_pCurrentClipContent
= nullptr;
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */