1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dtrans.hxx"
26 
27 #include <generic_clipboard.hxx>
28 #include <com/sun/star/lang/DisposedException.hpp>
29 #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
30 
31 using namespace com::sun::star::datatransfer;
32 using namespace com::sun::star::datatransfer::clipboard;
33 using namespace com::sun::star::lang;
34 using namespace com::sun::star::uno;
35 using namespace cppu;
36 using namespace osl;
37 
38 using ::dtrans::GenericClipboard;
39 using ::rtl::OUString;
40 
GenericClipboard()41 GenericClipboard::GenericClipboard() :
42 	WeakComponentImplHelper4< XClipboardEx, XClipboardNotifier, XServiceInfo, XInitialization > (m_aMutex),
43 	m_bInitialized(sal_False)
44 {
45 }
46 
47 // ------------------------------------------------------------------------
48 
~GenericClipboard()49 GenericClipboard::~GenericClipboard()
50 {
51 }
52 
53 // ------------------------------------------------------------------------
54 
initialize(const Sequence<Any> & aArguments)55 void SAL_CALL GenericClipboard::initialize( const Sequence< Any >& aArguments )
56 	throw(Exception, RuntimeException)
57 {
58 	if (!m_bInitialized)
59 	{
60 		for (sal_Int32 n = 0, nmax = aArguments.getLength(); n < nmax; n++)
61 			if (aArguments[n].getValueType() == getCppuType((OUString *) 0))
62 			{
63 				aArguments[0] >>= m_aName;
64 				break;
65 			}
66 	}
67 }
68 
69 // ------------------------------------------------------------------------
70 
getImplementationName()71 OUString SAL_CALL GenericClipboard::getImplementationName(  )
72 	throw(RuntimeException)
73 {
74 	return OUString::createFromAscii(GENERIC_CLIPBOARD_IMPLEMENTATION_NAME);
75 }
76 
77 // ------------------------------------------------------------------------
78 
supportsService(const OUString & ServiceName)79 sal_Bool SAL_CALL GenericClipboard::supportsService( const OUString& ServiceName )
80 	throw(RuntimeException)
81 {
82 	Sequence < OUString > SupportedServicesNames = GenericClipboard_getSupportedServiceNames();
83 
84 	for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
85 		if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
86 			return sal_True;
87 
88 	return sal_False;
89 }
90 
91 // ------------------------------------------------------------------------
92 
getSupportedServiceNames()93 Sequence< OUString > SAL_CALL GenericClipboard::getSupportedServiceNames(	 )
94 	throw(RuntimeException)
95 {
96 	return GenericClipboard_getSupportedServiceNames();
97 }
98 
99 // ------------------------------------------------------------------------
100 
getContents()101 Reference< XTransferable > SAL_CALL GenericClipboard::getContents()
102     throw(RuntimeException)
103 {
104     MutexGuard aGuard(m_aMutex);
105     return m_aContents;
106 }
107 
108 // ------------------------------------------------------------------------
109 
setContents(const Reference<XTransferable> & xTrans,const Reference<XClipboardOwner> & xClipboardOwner)110 void SAL_CALL GenericClipboard::setContents(const Reference< XTransferable >& xTrans,
111                                       const Reference< XClipboardOwner >& xClipboardOwner )
112     throw(RuntimeException)
113 {
114     // remember old values for callbacks before setting the new ones.
115     ClearableMutexGuard aGuard(m_aMutex);
116 
117     Reference< XClipboardOwner > oldOwner(m_aOwner);
118     m_aOwner = xClipboardOwner;
119 
120     Reference< XTransferable > oldContents(m_aContents);
121     m_aContents = xTrans;
122 
123     aGuard.clear();
124 
125     // notify old owner on loss of ownership
126     if( oldOwner.is() )
127     	oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents);
128 
129     // notify all listeners on content changes
130     OInterfaceContainerHelper *pContainer =
131         rBHelper.aLC.getContainer(getCppuType( (Reference < XClipboardListener > *) 0));
132     if (pContainer)
133     {
134         ClipboardEvent aEvent(static_cast < XClipboard * > (this), m_aContents);
135         OInterfaceIteratorHelper aIterator(*pContainer);
136 
137         while (aIterator.hasMoreElements())
138         {
139             Reference < XClipboardListener > xListener(aIterator.next(), UNO_QUERY);
140             if (xListener.is())
141                 xListener->changedContents(aEvent);
142         }
143     }
144 }
145 
146 // ------------------------------------------------------------------------
147 
getName()148 OUString SAL_CALL GenericClipboard::getName()
149     throw(RuntimeException)
150 {
151     return m_aName;
152 }
153 
154 // ------------------------------------------------------------------------
155 
getRenderingCapabilities()156 sal_Int8 SAL_CALL GenericClipboard::getRenderingCapabilities()
157     throw(RuntimeException)
158 {
159     return RenderingCapabilities::Delayed;
160 }
161 
162 
163 // ------------------------------------------------------------------------
164 
addClipboardListener(const Reference<XClipboardListener> & listener)165 void SAL_CALL GenericClipboard::addClipboardListener( const Reference< XClipboardListener >& listener )
166     throw(RuntimeException)
167 {
168     MutexGuard aGuard( rBHelper.rMutex );
169     OSL_ENSURE( !rBHelper.bInDispose, "do not add listeners in the dispose call" );
170     OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
171     if (!rBHelper.bInDispose && !rBHelper.bDisposed)
172         rBHelper.aLC.addInterface( getCppuType( (const ::com::sun::star::uno::Reference< XClipboardListener > *) 0), listener );
173 }
174 
175 // ------------------------------------------------------------------------
176 
removeClipboardListener(const Reference<XClipboardListener> & listener)177 void SAL_CALL GenericClipboard::removeClipboardListener( const Reference< XClipboardListener >& listener )
178     throw(RuntimeException)
179 {
180     MutexGuard aGuard( rBHelper.rMutex );
181     OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
182     if (!rBHelper.bInDispose && !rBHelper.bDisposed)
183         rBHelper.aLC.removeInterface( getCppuType( (const Reference< XClipboardListener > *) 0 ), listener ); \
184 }
185 
186 // ------------------------------------------------------------------------
187 
GenericClipboard_getSupportedServiceNames()188 Sequence< OUString > SAL_CALL GenericClipboard_getSupportedServiceNames()
189 {
190 	Sequence< OUString > aRet(1);
191 	aRet[0] = OUString::createFromAscii("com.sun.star.datatransfer.clipboard.GenericClipboard");
192 	return aRet;
193 }
194 
195 // ------------------------------------------------------------------------
196 
GenericClipboard_createInstance(const Reference<XMultiServiceFactory> &)197 Reference< XInterface > SAL_CALL GenericClipboard_createInstance(
198 	const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/)
199 {
200 	return Reference < XInterface >( ( OWeakObject * ) new GenericClipboard());
201 }
202