1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_dtrans.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <clipboardmanager.hxx> 32*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir using namespace com::sun::star::container; 35*cdf0e10cSrcweir using namespace com::sun::star::datatransfer; 36*cdf0e10cSrcweir using namespace com::sun::star::datatransfer::clipboard; 37*cdf0e10cSrcweir using namespace com::sun::star::lang; 38*cdf0e10cSrcweir using namespace com::sun::star::uno; 39*cdf0e10cSrcweir using namespace cppu; 40*cdf0e10cSrcweir using namespace osl; 41*cdf0e10cSrcweir using namespace std; 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir using ::dtrans::ClipboardManager; 44*cdf0e10cSrcweir using ::rtl::OUString; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir // ------------------------------------------------------------------------ 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir ClipboardManager::ClipboardManager(): 49*cdf0e10cSrcweir WeakComponentImplHelper3< XClipboardManager, XEventListener, XServiceInfo > (m_aMutex), 50*cdf0e10cSrcweir m_aDefaultName(OUString::createFromAscii("default")) 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir } 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir // ------------------------------------------------------------------------ 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir ClipboardManager::~ClipboardManager() 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir } 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir // ------------------------------------------------------------------------ 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir OUString SAL_CALL ClipboardManager::getImplementationName( ) 63*cdf0e10cSrcweir throw(RuntimeException) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir return OUString::createFromAscii(CLIPBOARDMANAGER_IMPLEMENTATION_NAME); 66*cdf0e10cSrcweir } 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir // ------------------------------------------------------------------------ 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir sal_Bool SAL_CALL ClipboardManager::supportsService( const OUString& ServiceName ) 71*cdf0e10cSrcweir throw(RuntimeException) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir Sequence < OUString > SupportedServicesNames = ClipboardManager_getSupportedServiceNames(); 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir for ( sal_Int32 n = 0, nmax = SupportedServicesNames.getLength(); n < nmax; n++ ) 76*cdf0e10cSrcweir if (SupportedServicesNames[n].compareTo(ServiceName) == 0) 77*cdf0e10cSrcweir return sal_True; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir return sal_False; 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir // ------------------------------------------------------------------------ 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir Sequence< OUString > SAL_CALL ClipboardManager::getSupportedServiceNames( ) 85*cdf0e10cSrcweir throw(RuntimeException) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir return ClipboardManager_getSupportedServiceNames(); 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir // ------------------------------------------------------------------------ 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir Reference< XClipboard > SAL_CALL ClipboardManager::getClipboard( const OUString& aName ) 93*cdf0e10cSrcweir throw(NoSuchElementException, RuntimeException) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir MutexGuard aGuard(m_aMutex); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir // object is disposed already 98*cdf0e10cSrcweir if (rBHelper.bDisposed) 99*cdf0e10cSrcweir throw DisposedException(OUString::createFromAscii("object is disposed."), 100*cdf0e10cSrcweir static_cast < XClipboardManager * > (this)); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir ClipboardMap::iterator iter = 103*cdf0e10cSrcweir m_aClipboardMap.find(aName.getLength() ? aName : m_aDefaultName); 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir if (iter != m_aClipboardMap.end()) 106*cdf0e10cSrcweir return iter->second; 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir throw NoSuchElementException(aName, static_cast < XClipboardManager * > (this)); 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir // ------------------------------------------------------------------------ 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir void SAL_CALL ClipboardManager::addClipboard( const Reference< XClipboard >& xClipboard ) 114*cdf0e10cSrcweir throw(IllegalArgumentException, ElementExistException, RuntimeException) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir OSL_ASSERT(xClipboard.is()); 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir // check parameter 119*cdf0e10cSrcweir if (!xClipboard.is()) 120*cdf0e10cSrcweir throw IllegalArgumentException(OUString::createFromAscii("empty reference"), 121*cdf0e10cSrcweir static_cast < XClipboardManager * > (this), 1); 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir // the name "default" is reserved for internal use 124*cdf0e10cSrcweir OUString aName = xClipboard->getName(); 125*cdf0e10cSrcweir if (m_aDefaultName.compareTo(aName) == 0) 126*cdf0e10cSrcweir throw IllegalArgumentException(OUString::createFromAscii("name reserved"), 127*cdf0e10cSrcweir static_cast < XClipboardManager * > (this), 1); 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir // try to add new clipboard to the list 130*cdf0e10cSrcweir ClearableMutexGuard aGuard(m_aMutex); 131*cdf0e10cSrcweir if (!rBHelper.bDisposed && !rBHelper.bInDispose) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir pair< const OUString, Reference< XClipboard > > value ( 134*cdf0e10cSrcweir aName.getLength() ? aName : m_aDefaultName, 135*cdf0e10cSrcweir xClipboard ); 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir pair< ClipboardMap::iterator, bool > p = m_aClipboardMap.insert(value); 138*cdf0e10cSrcweir aGuard.clear(); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir // insert failed, element must exist already 141*cdf0e10cSrcweir if (!p.second) 142*cdf0e10cSrcweir throw ElementExistException(aName, static_cast < XClipboardManager * > (this)); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir // request disposing notifications 145*cdf0e10cSrcweir Reference< XComponent > xComponent(xClipboard, UNO_QUERY); 146*cdf0e10cSrcweir if (xComponent.is()) 147*cdf0e10cSrcweir xComponent->addEventListener(static_cast < XEventListener * > (this)); 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir // ------------------------------------------------------------------------ 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir void SAL_CALL ClipboardManager::removeClipboard( const OUString& aName ) 154*cdf0e10cSrcweir throw(RuntimeException) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir MutexGuard aGuard(m_aMutex); 157*cdf0e10cSrcweir if (!rBHelper.bDisposed) 158*cdf0e10cSrcweir m_aClipboardMap.erase(aName.getLength() ? aName : m_aDefaultName ); 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir // ------------------------------------------------------------------------ 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir Sequence< OUString > SAL_CALL ClipboardManager::listClipboardNames() 164*cdf0e10cSrcweir throw(RuntimeException) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir MutexGuard aGuard(m_aMutex); 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir if (rBHelper.bDisposed) 169*cdf0e10cSrcweir throw DisposedException(OUString::createFromAscii("object is disposed."), 170*cdf0e10cSrcweir static_cast < XClipboardManager * > (this)); 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir if (rBHelper.bInDispose) 173*cdf0e10cSrcweir return Sequence< OUString > (); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir Sequence< OUString > aRet(m_aClipboardMap.size()); 176*cdf0e10cSrcweir ClipboardMap::iterator iter = m_aClipboardMap.begin(); 177*cdf0e10cSrcweir ClipboardMap::iterator imax = m_aClipboardMap.end(); 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir for (sal_Int32 n = 0; iter != imax; iter++) 180*cdf0e10cSrcweir aRet[n++] = iter->first; 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir return aRet; 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir // ------------------------------------------------------------------------ 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir void SAL_CALL ClipboardManager::dispose() 188*cdf0e10cSrcweir throw(RuntimeException) 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir ClearableMutexGuard aGuard( rBHelper.rMutex ); 191*cdf0e10cSrcweir if (!rBHelper.bDisposed && !rBHelper.bInDispose) 192*cdf0e10cSrcweir { 193*cdf0e10cSrcweir rBHelper.bInDispose = sal_True; 194*cdf0e10cSrcweir aGuard.clear(); 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir // give everyone a chance to save his clipboard instance 197*cdf0e10cSrcweir EventObject aEvt(static_cast < XClipboardManager * > (this)); 198*cdf0e10cSrcweir rBHelper.aLC.disposeAndClear( aEvt ); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir // removeClipboard is still allowed here, so make a copy of the 201*cdf0e10cSrcweir // list (to ensure integrety) and clear the original. 202*cdf0e10cSrcweir ClearableMutexGuard aGuard2( rBHelper.rMutex ); 203*cdf0e10cSrcweir ClipboardMap aCopy(m_aClipboardMap); 204*cdf0e10cSrcweir m_aClipboardMap.clear(); 205*cdf0e10cSrcweir aGuard2.clear(); 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir // dispose all clipboards still in list 208*cdf0e10cSrcweir ClipboardMap::iterator iter = aCopy.begin(); 209*cdf0e10cSrcweir ClipboardMap::iterator imax = aCopy.end(); 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir for (; iter != imax; iter++) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir Reference< XComponent > xComponent(iter->second, UNO_QUERY); 214*cdf0e10cSrcweir if (xComponent.is()) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir try 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir xComponent->removeEventListener(static_cast < XEventListener * > (this)); 219*cdf0e10cSrcweir xComponent->dispose(); 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir catch(Exception e) 223*cdf0e10cSrcweir { 224*cdf0e10cSrcweir // exceptions can be safely ignored here. 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir rBHelper.bDisposed = sal_True; 230*cdf0e10cSrcweir rBHelper.bInDispose = sal_False; 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir // ------------------------------------------------------------------------ 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir void SAL_CALL ClipboardManager::disposing( const EventObject& event ) 237*cdf0e10cSrcweir throw(RuntimeException) 238*cdf0e10cSrcweir { 239*cdf0e10cSrcweir Reference < XClipboard > xClipboard(event.Source, UNO_QUERY); 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir if (xClipboard.is()) 242*cdf0e10cSrcweir removeClipboard(xClipboard->getName()); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir // ------------------------------------------------------------------------ 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir Reference< XInterface > SAL_CALL ClipboardManager_createInstance( 248*cdf0e10cSrcweir const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/) 249*cdf0e10cSrcweir { 250*cdf0e10cSrcweir return Reference < XInterface >( ( OWeakObject * ) new ClipboardManager()); 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir // ------------------------------------------------------------------------ 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir Sequence< OUString > SAL_CALL ClipboardManager_getSupportedServiceNames() 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir Sequence < OUString > SupportedServicesNames( 1 ); 258*cdf0e10cSrcweir SupportedServicesNames[0] = 259*cdf0e10cSrcweir OUString::createFromAscii("com.sun.star.datatransfer.clipboard.ClipboardManager"); 260*cdf0e10cSrcweir return SupportedServicesNames; 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir 267