xref: /trunk/main/dtrans/source/generic/generic_clipboard.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
57     if (!m_bInitialized)
58     {
59         for (sal_Int32 n = 0, nmax = aArguments.getLength(); n < nmax; n++)
60             if (aArguments[n].getValueType() == getCppuType((OUString *) 0))
61             {
62                 aArguments[0] >>= m_aName;
63                 break;
64             }
65     }
66 }
67 
68 // ------------------------------------------------------------------------
69 
getImplementationName()70 OUString SAL_CALL GenericClipboard::getImplementationName(  )
71 {
72     return OUString::createFromAscii(GENERIC_CLIPBOARD_IMPLEMENTATION_NAME);
73 }
74 
75 // ------------------------------------------------------------------------
76 
supportsService(const OUString & ServiceName)77 sal_Bool SAL_CALL GenericClipboard::supportsService( const OUString& ServiceName )
78 {
79     Sequence < OUString > SupportedServicesNames = GenericClipboard_getSupportedServiceNames();
80 
81     for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
82         if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
83             return sal_True;
84 
85     return sal_False;
86 }
87 
88 // ------------------------------------------------------------------------
89 
getSupportedServiceNames()90 Sequence< OUString > SAL_CALL GenericClipboard::getSupportedServiceNames(    )
91 {
92     return GenericClipboard_getSupportedServiceNames();
93 }
94 
95 // ------------------------------------------------------------------------
96 
getContents()97 Reference< XTransferable > SAL_CALL GenericClipboard::getContents()
98 {
99     MutexGuard aGuard(m_aMutex);
100     return m_aContents;
101 }
102 
103 // ------------------------------------------------------------------------
104 
setContents(const Reference<XTransferable> & xTrans,const Reference<XClipboardOwner> & xClipboardOwner)105 void SAL_CALL GenericClipboard::setContents(const Reference< XTransferable >& xTrans,
106                                       const Reference< XClipboardOwner >& xClipboardOwner )
107 {
108     // remember old values for callbacks before setting the new ones.
109     ClearableMutexGuard aGuard(m_aMutex);
110 
111     Reference< XClipboardOwner > oldOwner(m_aOwner);
112     m_aOwner = xClipboardOwner;
113 
114     Reference< XTransferable > oldContents(m_aContents);
115     m_aContents = xTrans;
116 
117     aGuard.clear();
118 
119     // notify old owner on loss of ownership
120     if( oldOwner.is() )
121         oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents);
122 
123     // notify all listeners on content changes
124     OInterfaceContainerHelper *pContainer =
125         rBHelper.aLC.getContainer(getCppuType( (Reference < XClipboardListener > *) 0));
126     if (pContainer)
127     {
128         ClipboardEvent aEvent(static_cast < XClipboard * > (this), m_aContents);
129         OInterfaceIteratorHelper aIterator(*pContainer);
130 
131         while (aIterator.hasMoreElements())
132         {
133             Reference < XClipboardListener > xListener(aIterator.next(), UNO_QUERY);
134             if (xListener.is())
135                 xListener->changedContents(aEvent);
136         }
137     }
138 }
139 
140 // ------------------------------------------------------------------------
141 
getName()142 OUString SAL_CALL GenericClipboard::getName()
143 {
144     return m_aName;
145 }
146 
147 // ------------------------------------------------------------------------
148 
getRenderingCapabilities()149 sal_Int8 SAL_CALL GenericClipboard::getRenderingCapabilities()
150 {
151     return RenderingCapabilities::Delayed;
152 }
153 
154 
155 // ------------------------------------------------------------------------
156 
addClipboardListener(const Reference<XClipboardListener> & listener)157 void SAL_CALL GenericClipboard::addClipboardListener( const Reference< XClipboardListener >& listener )
158 {
159     MutexGuard aGuard( rBHelper.rMutex );
160     OSL_ENSURE( !rBHelper.bInDispose, "do not add listeners in the dispose call" );
161     OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
162     if (!rBHelper.bInDispose && !rBHelper.bDisposed)
163         rBHelper.aLC.addInterface( getCppuType( (const ::com::sun::star::uno::Reference< XClipboardListener > *) 0), listener );
164 }
165 
166 // ------------------------------------------------------------------------
167 
removeClipboardListener(const Reference<XClipboardListener> & listener)168 void SAL_CALL GenericClipboard::removeClipboardListener( const Reference< XClipboardListener >& listener )
169 {
170     MutexGuard aGuard( rBHelper.rMutex );
171     OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
172     if (!rBHelper.bInDispose && !rBHelper.bDisposed)
173         rBHelper.aLC.removeInterface( getCppuType( (const Reference< XClipboardListener > *) 0 ), listener ); \
174 }
175 
176 // ------------------------------------------------------------------------
177 
GenericClipboard_getSupportedServiceNames()178 Sequence< OUString > SAL_CALL GenericClipboard_getSupportedServiceNames()
179 {
180     Sequence< OUString > aRet(1);
181     aRet[0] = OUString::createFromAscii("com.sun.star.datatransfer.clipboard.GenericClipboard");
182     return aRet;
183 }
184 
185 // ------------------------------------------------------------------------
186 
GenericClipboard_createInstance(const Reference<XMultiServiceFactory> &)187 Reference< XInterface > SAL_CALL GenericClipboard_createInstance(
188     const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/)
189 {
190     return Reference < XInterface >( ( OWeakObject * ) new GenericClipboard());
191 }
192