update credits
[LibreOffice.git] / dtrans / source / win32 / clipb / WinClipbImpl.cxx
blobf10b3e5e66af03787f9985d073d6e61ddd96a023
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "WinClipboard.hxx"
27 #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
28 #include "../dtobj/XNotifyingDataObject.hxx"
30 #if defined _MSC_VER
31 #pragma warning(push,1)
32 #endif
33 #include <windows.h>
34 #include <ole2.h>
35 #include <objidl.h>
36 #if defined _MSC_VER
37 #pragma warning(pop)
38 #endif
40 //------------------------------------------------------------------------
41 // namespace directives
42 //------------------------------------------------------------------------
44 using namespace osl;
45 using namespace std;
46 using namespace cppu;
48 using namespace com::sun::star::uno;
49 using namespace com::sun::star::datatransfer;
50 using namespace com::sun::star::datatransfer::clipboard;
51 using namespace com::sun::star::datatransfer::clipboard::RenderingCapabilities;
54 // definition of static members
55 CWinClipbImpl* CWinClipbImpl::s_pCWinClipbImpl = NULL;
56 osl::Mutex CWinClipbImpl::s_aMutex;
58 //------------------------------------------------------------------------
60 //------------------------------------------------------------------------
62 CWinClipbImpl::CWinClipbImpl( const OUString& aClipboardName, CWinClipboard* theWinClipboard ) :
63 m_itsName( aClipboardName ),
64 m_pWinClipboard( theWinClipboard ),
65 m_pCurrentClipContent( NULL )
67 OSL_ASSERT( NULL != m_pWinClipboard );
69 // necessary to reassociate from
70 // the static callback function
71 s_pCWinClipbImpl = this;
72 registerClipboardViewer( );
75 //------------------------------------------------------------------------
77 //------------------------------------------------------------------------
79 CWinClipbImpl::~CWinClipbImpl( )
81 ClearableMutexGuard aGuard( s_aMutex );
82 s_pCWinClipbImpl = NULL;
83 aGuard.clear( );
85 unregisterClipboardViewer( );
88 //------------------------------------------------------------------------
89 // getContent
90 //------------------------------------------------------------------------
92 Reference< XTransferable > SAL_CALL CWinClipbImpl::getContents( ) throw( RuntimeException )
94 // use the shotcut or create a transferable from
95 // system clipboard
96 ClearableMutexGuard aGuard( m_ClipContentMutex );
98 if ( NULL != m_pCurrentClipContent )
100 return m_pCurrentClipContent->m_XTransferable;
103 // release the mutex, so that the variable may be
104 // changed by other threads
105 aGuard.clear( );
107 Reference< XTransferable > rClipContent;
109 // get the current dataobject from clipboard
110 IDataObjectPtr pIDataObject;
111 HRESULT hr = m_MtaOleClipboard.getClipboard( &pIDataObject );
113 if ( SUCCEEDED( hr ) )
115 // create an apartment neutral dataobject and initialize it with a
116 // com smart pointer to the IDataObject from clipboard
117 IDataObjectPtr pIDo( new CAPNDataObject( pIDataObject ) );
119 CDTransObjFactory objFactory;
121 // remember pIDo destroys itself due to the smart pointer
122 rClipContent = objFactory.createTransferableFromDataObj( m_pWinClipboard->m_xContext, pIDo );
125 return rClipContent;
128 //------------------------------------------------------------------------
129 // setContent
130 //------------------------------------------------------------------------
132 void SAL_CALL CWinClipbImpl::setContents(
133 const Reference< XTransferable >& xTransferable,
134 const Reference< XClipboardOwner >& xClipboardOwner )
135 throw( RuntimeException )
137 CDTransObjFactory objFactory;
138 IDataObjectPtr pIDataObj;
140 if ( xTransferable.is( ) )
142 ClearableMutexGuard aGuard( m_ClipContentMutex );
144 m_pCurrentClipContent = new CXNotifyingDataObject(
145 objFactory.createDataObjFromTransferable( m_pWinClipboard->m_xContext , xTransferable ),
146 xTransferable,
147 xClipboardOwner,
148 this );
150 aGuard.clear( );
152 pIDataObj = IDataObjectPtr( m_pCurrentClipContent );
155 m_MtaOleClipboard.setClipboard(pIDataObj.get());
158 //------------------------------------------------------------------------
160 //------------------------------------------------------------------------
162 OUString SAL_CALL CWinClipbImpl::getName( ) throw( RuntimeException )
164 return m_itsName;
167 //------------------------------------------------------------------------
169 //------------------------------------------------------------------------
171 sal_Int8 SAL_CALL CWinClipbImpl::getRenderingCapabilities( ) throw( RuntimeException )
173 return ( Delayed | Persistant );
176 //------------------------------------------------------------------------
178 //------------------------------------------------------------------------
180 void SAL_CALL CWinClipbImpl::flushClipboard( ) throw( RuntimeException )
182 // actually it should be ClearableMutexGuard aGuard( m_ClipContentMutex );
183 // but it does not work since FlushClipboard does a callback and frees DataObject
184 // which results in a deadlock in onReleaseDataObject.
185 // FlushClipboard had to be synchron in order to prevent shutdown until all
186 // clipboard-formats are redered.
187 // The request is needed to prevent flushing if we are not clipboard owner (it is
188 // not known what happens if we flush but aren't clipoard owner).
189 // It may be possible to move the request to the clipboard STA thread by saving the
190 // DataObject and call OleIsCurrentClipboard bevore flushing.
192 if ( NULL != m_pCurrentClipContent )
193 m_MtaOleClipboard.flushClipboard( );
196 //------------------------------------------------------------------------
198 //------------------------------------------------------------------------
200 void SAL_CALL CWinClipbImpl::registerClipboardViewer( )
202 m_MtaOleClipboard.registerClipViewer( CWinClipbImpl::onClipboardContentChanged );
205 //------------------------------------------------------------------------
207 //------------------------------------------------------------------------
209 void SAL_CALL CWinClipbImpl::unregisterClipboardViewer( )
211 m_MtaOleClipboard.registerClipViewer( NULL );
214 //------------------------------------------------------------------------
216 //------------------------------------------------------------------------
218 void SAL_CALL CWinClipbImpl::dispose() throw( RuntimeException )
220 OSL_ENSURE( !m_pCurrentClipContent, "Clipboard was not flushed before shutdown!" );
223 //------------------------------------------------------------------------
225 //------------------------------------------------------------------------
227 void WINAPI CWinClipbImpl::onClipboardContentChanged( void )
229 MutexGuard aGuard( s_aMutex );
231 // reassocition to instance through static member
232 if ( NULL != s_pCWinClipbImpl )
233 s_pCWinClipbImpl->m_pWinClipboard->notifyAllClipboardListener( );
236 //------------------------------------------------------------------------
238 //------------------------------------------------------------------------
240 void SAL_CALL CWinClipbImpl::onReleaseDataObject( CXNotifyingDataObject* theCaller )
242 OSL_ASSERT( NULL != theCaller );
244 if ( theCaller )
245 theCaller->lostOwnership( );
247 // if the current caller is the one we currently
248 // hold, then set it to NULL because an external
249 // source must be the clipboardowner now
250 MutexGuard aGuard( m_ClipContentMutex );
252 if ( m_pCurrentClipContent == theCaller )
253 m_pCurrentClipContent = NULL;
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */