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"
31 #if !defined WIN32_LEAN_AND_MEAN
32 # define WIN32_LEAN_AND_MEAN
42 using namespace com::sun::star::uno
;
43 using namespace com::sun::star::datatransfer
;
44 using namespace com::sun::star::datatransfer::clipboard
;
45 using namespace com::sun::star::datatransfer::clipboard::RenderingCapabilities
;
47 // definition of static members
48 CWinClipbImpl
* CWinClipbImpl::s_pCWinClipbImpl
= nullptr;
49 osl::Mutex
CWinClipbImpl::s_aMutex
;
51 CWinClipbImpl::CWinClipbImpl( const OUString
& aClipboardName
, CWinClipboard
* theWinClipboard
) :
52 m_itsName( aClipboardName
),
53 m_pWinClipboard( theWinClipboard
),
54 m_pCurrentClipContent( nullptr )
56 OSL_ASSERT( nullptr != m_pWinClipboard
);
58 // necessary to reassociate from
59 // the static callback function
60 s_pCWinClipbImpl
= this;
61 registerClipboardViewer( );
64 CWinClipbImpl::~CWinClipbImpl( )
67 MutexGuard
aGuard(s_aMutex
);
68 s_pCWinClipbImpl
= nullptr;
71 unregisterClipboardViewer( );
74 Reference
< XTransferable
> CWinClipbImpl::getContents( )
76 // use the shortcut or create a transferable from
79 MutexGuard
aGuard(m_ClipContentMutex
);
81 if (nullptr != m_pCurrentClipContent
)
83 return m_pCurrentClipContent
->m_XTransferable
;
86 // release the mutex, so that the variable may be
87 // changed by other threads
90 Reference
< XTransferable
> rClipContent
;
92 // get the current dataobject from clipboard
93 IDataObjectPtr pIDataObject
;
94 HRESULT hr
= m_MtaOleClipboard
.getClipboard( &pIDataObject
);
96 if ( SUCCEEDED( hr
) )
98 // create an apartment neutral dataobject and initialize it with a
99 // com smart pointer to the IDataObject from clipboard
100 IDataObjectPtr
pIDo( new CAPNDataObject( pIDataObject
) );
102 // remember pIDo destroys itself due to the smart pointer
103 rClipContent
= CDOTransferable::create( m_pWinClipboard
->m_xContext
, pIDo
);
109 void CWinClipbImpl::setContents(
110 const Reference
< XTransferable
>& xTransferable
,
111 const Reference
< XClipboardOwner
>& xClipboardOwner
)
113 IDataObjectPtr pIDataObj
;
115 if ( xTransferable
.is( ) )
118 MutexGuard
aGuard(m_ClipContentMutex
);
120 m_pCurrentClipContent
121 = new CXNotifyingDataObject(CDTransObjFactory::createDataObjFromTransferable(
122 m_pWinClipboard
->m_xContext
, xTransferable
),
123 xTransferable
, xClipboardOwner
, this);
126 pIDataObj
= IDataObjectPtr( m_pCurrentClipContent
);
129 m_MtaOleClipboard
.setClipboard(pIDataObj
.get());
132 OUString
CWinClipbImpl::getName( )
137 sal_Int8
CWinClipbImpl::getRenderingCapabilities( )
139 return ( Delayed
| Persistant
);
142 void CWinClipbImpl::flushClipboard( )
144 // actually it should be ClearableMutexGuard aGuard( m_ClipContentMutex );
145 // but it does not work since FlushClipboard does a callback and frees DataObject
146 // which results in a deadlock in onReleaseDataObject.
147 // FlushClipboard had to be synchron in order to prevent shutdown until all
148 // clipboard-formats are rendered.
149 // The request is needed to prevent flushing if we are not clipboard owner (it is
150 // not known what happens if we flush but aren't clipboard owner).
151 // It may be possible to move the request to the clipboard STA thread by saving the
152 // DataObject and call OleIsCurrentClipboard before flushing.
154 if ( nullptr != m_pCurrentClipContent
)
155 m_MtaOleClipboard
.flushClipboard( );
158 void CWinClipbImpl::registerClipboardViewer( )
160 m_MtaOleClipboard
.registerClipViewer( CWinClipbImpl::onClipboardContentChanged
);
163 void CWinClipbImpl::unregisterClipboardViewer( )
165 m_MtaOleClipboard
.registerClipViewer( nullptr );
168 void CWinClipbImpl::dispose()
170 OSL_ENSURE( !m_pCurrentClipContent
, "Clipboard was not flushed before shutdown!" );
173 void WINAPI
CWinClipbImpl::onClipboardContentChanged()
175 MutexGuard
aGuard( s_aMutex
);
177 // reassociation to instance through static member
178 if ( nullptr != s_pCWinClipbImpl
)
179 s_pCWinClipbImpl
->m_pWinClipboard
->notifyAllClipboardListener( );
182 void CWinClipbImpl::onReleaseDataObject( CXNotifyingDataObject
* theCaller
)
184 OSL_ASSERT( nullptr != theCaller
);
187 theCaller
->lostOwnership( );
189 // if the current caller is the one we currently
190 // hold, then set it to NULL because an external
191 // source must be the clipboardowner now
192 MutexGuard
aGuard( m_ClipContentMutex
);
194 if ( m_pCurrentClipContent
== theCaller
)
195 m_pCurrentClipContent
= nullptr;
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */