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 "WinClipboard.hxx"
27 #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
28 #include "../dtobj/XNotifyingDataObject.hxx"
31 #pragma warning(push,1)
40 // namespace directives
46 using namespace com::sun::star::uno
;
47 using namespace com::sun::star::datatransfer
;
48 using namespace com::sun::star::datatransfer::clipboard
;
49 using namespace com::sun::star::datatransfer::clipboard::RenderingCapabilities
;
51 // definition of static members
52 CWinClipbImpl
* CWinClipbImpl::s_pCWinClipbImpl
= NULL
;
53 osl::Mutex
CWinClipbImpl::s_aMutex
;
55 CWinClipbImpl::CWinClipbImpl( const OUString
& aClipboardName
, CWinClipboard
* theWinClipboard
) :
56 m_itsName( aClipboardName
),
57 m_pWinClipboard( theWinClipboard
),
58 m_pCurrentClipContent( NULL
)
60 OSL_ASSERT( NULL
!= m_pWinClipboard
);
62 // necessary to reassociate from
63 // the static callback function
64 s_pCWinClipbImpl
= this;
65 registerClipboardViewer( );
68 CWinClipbImpl::~CWinClipbImpl( )
70 ClearableMutexGuard
aGuard( s_aMutex
);
71 s_pCWinClipbImpl
= NULL
;
74 unregisterClipboardViewer( );
79 Reference
< XTransferable
> SAL_CALL
CWinClipbImpl::getContents( ) throw( RuntimeException
)
81 // use the shotcut or create a transferable from
83 ClearableMutexGuard
aGuard( m_ClipContentMutex
);
85 if ( NULL
!= m_pCurrentClipContent
)
87 return m_pCurrentClipContent
->m_XTransferable
;
90 // release the mutex, so that the variable may be
91 // changed by other threads
94 Reference
< XTransferable
> rClipContent
;
96 // get the current dataobject from clipboard
97 IDataObjectPtr pIDataObject
;
98 HRESULT hr
= m_MtaOleClipboard
.getClipboard( &pIDataObject
);
100 if ( SUCCEEDED( hr
) )
102 // create an apartment neutral dataobject and initialize it with a
103 // com smart pointer to the IDataObject from clipboard
104 IDataObjectPtr
pIDo( new CAPNDataObject( pIDataObject
) );
106 CDTransObjFactory objFactory
;
108 // remember pIDo destroys itself due to the smart pointer
109 rClipContent
= objFactory
.createTransferableFromDataObj( m_pWinClipboard
->m_xContext
, pIDo
);
117 void SAL_CALL
CWinClipbImpl::setContents(
118 const Reference
< XTransferable
>& xTransferable
,
119 const Reference
< XClipboardOwner
>& xClipboardOwner
)
120 throw( RuntimeException
)
122 CDTransObjFactory objFactory
;
123 IDataObjectPtr pIDataObj
;
125 if ( xTransferable
.is( ) )
127 ClearableMutexGuard
aGuard( m_ClipContentMutex
);
129 m_pCurrentClipContent
= new CXNotifyingDataObject(
130 objFactory
.createDataObjFromTransferable( m_pWinClipboard
->m_xContext
, xTransferable
),
137 pIDataObj
= IDataObjectPtr( m_pCurrentClipContent
);
140 m_MtaOleClipboard
.setClipboard(pIDataObj
.get());
143 OUString SAL_CALL
CWinClipbImpl::getName( ) throw( RuntimeException
)
148 sal_Int8 SAL_CALL
CWinClipbImpl::getRenderingCapabilities( ) throw( RuntimeException
)
150 return ( Delayed
| Persistant
);
153 void SAL_CALL
CWinClipbImpl::flushClipboard( ) throw( RuntimeException
)
155 // actually it should be ClearableMutexGuard aGuard( m_ClipContentMutex );
156 // but it does not work since FlushClipboard does a callback and frees DataObject
157 // which results in a deadlock in onReleaseDataObject.
158 // FlushClipboard had to be synchron in order to prevent shutdown until all
159 // clipboard-formats are redered.
160 // The request is needed to prevent flushing if we are not clipboard owner (it is
161 // not known what happens if we flush but aren't clipoard owner).
162 // It may be possible to move the request to the clipboard STA thread by saving the
163 // DataObject and call OleIsCurrentClipboard bevore flushing.
165 if ( NULL
!= m_pCurrentClipContent
)
166 m_MtaOleClipboard
.flushClipboard( );
169 void SAL_CALL
CWinClipbImpl::registerClipboardViewer( )
171 m_MtaOleClipboard
.registerClipViewer( CWinClipbImpl::onClipboardContentChanged
);
174 void SAL_CALL
CWinClipbImpl::unregisterClipboardViewer( )
176 m_MtaOleClipboard
.registerClipViewer( NULL
);
179 void SAL_CALL
CWinClipbImpl::dispose() throw( RuntimeException
)
181 OSL_ENSURE( !m_pCurrentClipContent
, "Clipboard was not flushed before shutdown!" );
184 void WINAPI
CWinClipbImpl::onClipboardContentChanged()
186 MutexGuard
aGuard( s_aMutex
);
188 // reassocition to instance through static member
189 if ( NULL
!= s_pCWinClipbImpl
)
190 s_pCWinClipbImpl
->m_pWinClipboard
->notifyAllClipboardListener( );
193 void SAL_CALL
CWinClipbImpl::onReleaseDataObject( CXNotifyingDataObject
* theCaller
)
195 OSL_ASSERT( NULL
!= theCaller
);
198 theCaller
->lostOwnership( );
200 // if the current caller is the one we currently
201 // hold, then set it to NULL because an external
202 // source must be the clipboardowner now
203 MutexGuard
aGuard( m_ClipContentMutex
);
205 if ( m_pCurrentClipContent
== theCaller
)
206 m_pCurrentClipContent
= NULL
;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */