1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dtrans.hxx"
30 
31 #include <generic_clipboard.hxx>
32 #include <com/sun/star/lang/DisposedException.hpp>
33 #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
34 
35 using namespace com::sun::star::datatransfer;
36 using namespace com::sun::star::datatransfer::clipboard;
37 using namespace com::sun::star::lang;
38 using namespace com::sun::star::uno;
39 using namespace cppu;
40 using namespace osl;
41 
42 using ::dtrans::GenericClipboard;
43 using ::rtl::OUString;
44 
45 GenericClipboard::GenericClipboard() :
46 	WeakComponentImplHelper4< XClipboardEx, XClipboardNotifier, XServiceInfo, XInitialization > (m_aMutex),
47 	m_bInitialized(sal_False)
48 {
49 }
50 
51 // ------------------------------------------------------------------------
52 
53 GenericClipboard::~GenericClipboard()
54 {
55 }
56 
57 // ------------------------------------------------------------------------
58 
59 void SAL_CALL GenericClipboard::initialize( const Sequence< Any >& aArguments )
60 	throw(Exception, RuntimeException)
61 {
62 	if (!m_bInitialized)
63 	{
64 		for (sal_Int32 n = 0, nmax = aArguments.getLength(); n < nmax; n++)
65 			if (aArguments[n].getValueType() == getCppuType((OUString *) 0))
66 			{
67 				aArguments[0] >>= m_aName;
68 				break;
69 			}
70 	}
71 }
72 
73 // ------------------------------------------------------------------------
74 
75 OUString SAL_CALL GenericClipboard::getImplementationName(  )
76 	throw(RuntimeException)
77 {
78 	return OUString::createFromAscii(GENERIC_CLIPBOARD_IMPLEMENTATION_NAME);
79 }
80 
81 // ------------------------------------------------------------------------
82 
83 sal_Bool SAL_CALL GenericClipboard::supportsService( const OUString& ServiceName )
84 	throw(RuntimeException)
85 {
86 	Sequence < OUString > SupportedServicesNames = GenericClipboard_getSupportedServiceNames();
87 
88 	for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
89 		if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
90 			return sal_True;
91 
92 	return sal_False;
93 }
94 
95 // ------------------------------------------------------------------------
96 
97 Sequence< OUString > SAL_CALL GenericClipboard::getSupportedServiceNames(	 )
98 	throw(RuntimeException)
99 {
100 	return GenericClipboard_getSupportedServiceNames();
101 }
102 
103 // ------------------------------------------------------------------------
104 
105 Reference< XTransferable > SAL_CALL GenericClipboard::getContents()
106     throw(RuntimeException)
107 {
108     MutexGuard aGuard(m_aMutex);
109     return m_aContents;
110 }
111 
112 // ------------------------------------------------------------------------
113 
114 void SAL_CALL GenericClipboard::setContents(const Reference< XTransferable >& xTrans,
115                                       const Reference< XClipboardOwner >& xClipboardOwner )
116     throw(RuntimeException)
117 {
118     // remember old values for callbacks before setting the new ones.
119     ClearableMutexGuard aGuard(m_aMutex);
120 
121     Reference< XClipboardOwner > oldOwner(m_aOwner);
122     m_aOwner = xClipboardOwner;
123 
124     Reference< XTransferable > oldContents(m_aContents);
125     m_aContents = xTrans;
126 
127     aGuard.clear();
128 
129     // notify old owner on loss of ownership
130     if( oldOwner.is() )
131     	oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents);
132 
133     // notify all listeners on content changes
134     OInterfaceContainerHelper *pContainer =
135         rBHelper.aLC.getContainer(getCppuType( (Reference < XClipboardListener > *) 0));
136     if (pContainer)
137     {
138         ClipboardEvent aEvent(static_cast < XClipboard * > (this), m_aContents);
139         OInterfaceIteratorHelper aIterator(*pContainer);
140 
141         while (aIterator.hasMoreElements())
142         {
143             Reference < XClipboardListener > xListener(aIterator.next(), UNO_QUERY);
144             if (xListener.is())
145                 xListener->changedContents(aEvent);
146         }
147     }
148 }
149 
150 // ------------------------------------------------------------------------
151 
152 OUString SAL_CALL GenericClipboard::getName()
153     throw(RuntimeException)
154 {
155     return m_aName;
156 }
157 
158 // ------------------------------------------------------------------------
159 
160 sal_Int8 SAL_CALL GenericClipboard::getRenderingCapabilities()
161     throw(RuntimeException)
162 {
163     return RenderingCapabilities::Delayed;
164 }
165 
166 
167 // ------------------------------------------------------------------------
168 
169 void SAL_CALL GenericClipboard::addClipboardListener( const Reference< XClipboardListener >& listener )
170     throw(RuntimeException)
171 {
172     MutexGuard aGuard( rBHelper.rMutex );
173     OSL_ENSURE( !rBHelper.bInDispose, "do not add listeners in the dispose call" );
174     OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
175     if (!rBHelper.bInDispose && !rBHelper.bDisposed)
176         rBHelper.aLC.addInterface( getCppuType( (const ::com::sun::star::uno::Reference< XClipboardListener > *) 0), listener );
177 }
178 
179 // ------------------------------------------------------------------------
180 
181 void SAL_CALL GenericClipboard::removeClipboardListener( const Reference< XClipboardListener >& listener )
182     throw(RuntimeException)
183 {
184     MutexGuard aGuard( rBHelper.rMutex );
185     OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
186     if (!rBHelper.bInDispose && !rBHelper.bDisposed)
187         rBHelper.aLC.removeInterface( getCppuType( (const Reference< XClipboardListener > *) 0 ), listener ); \
188 }
189 
190 // ------------------------------------------------------------------------
191 
192 Sequence< OUString > SAL_CALL GenericClipboard_getSupportedServiceNames()
193 {
194 	Sequence< OUString > aRet(1);
195 	aRet[0] = OUString::createFromAscii("com.sun.star.datatransfer.clipboard.GenericClipboard");
196 	return aRet;
197 }
198 
199 // ------------------------------------------------------------------------
200 
201 Reference< XInterface > SAL_CALL GenericClipboard_createInstance(
202 	const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/)
203 {
204 	return Reference < XInterface >( ( OWeakObject * ) new GenericClipboard());
205 }
206