19f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
39f62ea84SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist * or more contributor license agreements. See the NOTICE file
59f62ea84SAndrew Rist * distributed with this work for additional information
69f62ea84SAndrew Rist * regarding copyright ownership. The ASF licenses this file
79f62ea84SAndrew Rist * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist * with the License. You may obtain a copy of the License at
109f62ea84SAndrew Rist *
119f62ea84SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
129f62ea84SAndrew Rist *
139f62ea84SAndrew Rist * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist * software distributed under the License is distributed on an
159f62ea84SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist * KIND, either express or implied. See the License for the
179f62ea84SAndrew Rist * specific language governing permissions and limitations
189f62ea84SAndrew Rist * under the License.
199f62ea84SAndrew Rist *
209f62ea84SAndrew Rist *************************************************************/
219f62ea84SAndrew Rist
229f62ea84SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir #include "aqua_clipboard.hxx"
27cdf0e10cSrcweir
28cdf0e10cSrcweir #include "DataFlavorMapping.hxx"
29cdf0e10cSrcweir #include "OSXTransferable.hxx"
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include "vcl/unohelp.hxx"
32cdf0e10cSrcweir
33cdf0e10cSrcweir #include "comphelper/makesequence.hxx"
34cdf0e10cSrcweir
35cdf0e10cSrcweir #include <boost/assert.hpp>
36cdf0e10cSrcweir
37cdf0e10cSrcweir using namespace com::sun::star::datatransfer;
38cdf0e10cSrcweir using namespace com::sun::star::datatransfer::clipboard;
39cdf0e10cSrcweir using namespace com::sun::star::lang;
40cdf0e10cSrcweir using namespace com::sun::star::uno;
41cdf0e10cSrcweir using namespace cppu;
42cdf0e10cSrcweir using namespace osl;
43cdf0e10cSrcweir using namespace rtl;
44cdf0e10cSrcweir using namespace std;
45cdf0e10cSrcweir using namespace comphelper;
46cdf0e10cSrcweir
47cdf0e10cSrcweir
48cdf0e10cSrcweir @implementation EventListener;
49cdf0e10cSrcweir
50cdf0e10cSrcweir -(EventListener*)initWithAquaClipboard: (AquaClipboard*) pcb
51cdf0e10cSrcweir {
52cdf0e10cSrcweir self = [super init];
53cdf0e10cSrcweir
54cdf0e10cSrcweir if (self)
55cdf0e10cSrcweir pAquaClipboard = pcb;
56cdf0e10cSrcweir
57cdf0e10cSrcweir return self;
58cdf0e10cSrcweir }
59cdf0e10cSrcweir
60*1cc6f0e2SHerbert Dürr -(void)pasteboard:(NSPasteboard*)sender provideDataForType:(const NSString*)type
61cdf0e10cSrcweir {
62cdf0e10cSrcweir if( pAquaClipboard )
63cdf0e10cSrcweir pAquaClipboard->provideDataForType(sender, type);
64cdf0e10cSrcweir }
65cdf0e10cSrcweir
66cdf0e10cSrcweir -(void)applicationDidBecomeActive:(NSNotification*)aNotification
67cdf0e10cSrcweir {
68cdf0e10cSrcweir if( pAquaClipboard )
69cdf0e10cSrcweir pAquaClipboard->applicationDidBecomeActive(aNotification);
70cdf0e10cSrcweir }
71cdf0e10cSrcweir
72cdf0e10cSrcweir -(void)disposing
73cdf0e10cSrcweir {
74cdf0e10cSrcweir pAquaClipboard = NULL;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir
77cdf0e10cSrcweir @end
78cdf0e10cSrcweir
79cdf0e10cSrcweir
clipboard_getImplementationName()80cdf0e10cSrcweir OUString clipboard_getImplementationName()
81cdf0e10cSrcweir {
82cdf0e10cSrcweir return OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.datatransfer.clipboard.AquaClipboard"));
83cdf0e10cSrcweir }
84cdf0e10cSrcweir
clipboard_getSupportedServiceNames()85cdf0e10cSrcweir Sequence<OUString> clipboard_getSupportedServiceNames()
86cdf0e10cSrcweir {
87cdf0e10cSrcweir return makeSequence(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.datatransfer.clipboard.SystemClipboard")));
88cdf0e10cSrcweir }
89cdf0e10cSrcweir
90cdf0e10cSrcweir
AquaClipboard(NSPasteboard * pasteboard,bool bUseSystemPasteboard)91cdf0e10cSrcweir AquaClipboard::AquaClipboard(NSPasteboard* pasteboard, bool bUseSystemPasteboard) :
92cdf0e10cSrcweir WeakComponentImplHelper4<XClipboardEx, XClipboardNotifier, XFlushableClipboard, XServiceInfo>(m_aMutex),
93cdf0e10cSrcweir mIsSystemPasteboard(bUseSystemPasteboard)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir Reference<XMultiServiceFactory> mrServiceMgr = vcl::unohelper::GetMultiServiceFactory();
96cdf0e10cSrcweir
97cdf0e10cSrcweir mrXMimeCntFactory = Reference<XMimeContentTypeFactory>(mrServiceMgr->createInstance(
98cdf0e10cSrcweir OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.datatransfer.MimeContentTypeFactory"))), UNO_QUERY);
99cdf0e10cSrcweir
100cdf0e10cSrcweir if (!mrXMimeCntFactory.is())
101cdf0e10cSrcweir {
102cdf0e10cSrcweir throw RuntimeException(OUString(
103cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("AquaClipboard: Cannot create com.sun.star.datatransfer.MimeContentTypeFactory")),
104cdf0e10cSrcweir static_cast<XClipboardEx*>(this));
105cdf0e10cSrcweir }
106cdf0e10cSrcweir
107cdf0e10cSrcweir mpDataFlavorMapper = DataFlavorMapperPtr_t(new DataFlavorMapper());
108cdf0e10cSrcweir
109cdf0e10cSrcweir if (pasteboard != NULL)
110cdf0e10cSrcweir {
111cdf0e10cSrcweir mPasteboard = pasteboard;
112cdf0e10cSrcweir mIsSystemPasteboard = false;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir else
115cdf0e10cSrcweir {
116cdf0e10cSrcweir mPasteboard = bUseSystemPasteboard ? [NSPasteboard generalPasteboard] :
117cdf0e10cSrcweir [NSPasteboard pasteboardWithName: NSDragPboard];
118cdf0e10cSrcweir
119cdf0e10cSrcweir if (mPasteboard == nil)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir throw RuntimeException(OUString(
122cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("AquaClipboard: Cannot create Cocoa pasteboard")),
123cdf0e10cSrcweir static_cast<XClipboardEx*>(this));
124cdf0e10cSrcweir }
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
127cdf0e10cSrcweir [mPasteboard retain];
128cdf0e10cSrcweir
129cdf0e10cSrcweir mEventListener = [[EventListener alloc] initWithAquaClipboard: this];
130cdf0e10cSrcweir
131cdf0e10cSrcweir if (mEventListener == nil)
132cdf0e10cSrcweir {
133cdf0e10cSrcweir [mPasteboard release];
134cdf0e10cSrcweir
135cdf0e10cSrcweir throw RuntimeException(
136cdf0e10cSrcweir OUString(RTL_CONSTASCII_USTRINGPARAM("AquaClipboard: Cannot create pasteboard change listener")),
137cdf0e10cSrcweir static_cast<XClipboardEx*>(this));
138cdf0e10cSrcweir }
139cdf0e10cSrcweir
140cdf0e10cSrcweir if (mIsSystemPasteboard)
141cdf0e10cSrcweir {
142cdf0e10cSrcweir NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
143cdf0e10cSrcweir
144cdf0e10cSrcweir [notificationCenter addObserver: mEventListener
145cdf0e10cSrcweir selector: @selector(applicationDidBecomeActive:)
146cdf0e10cSrcweir name: @"NSApplicationDidBecomeActiveNotification"
147cdf0e10cSrcweir object: [NSApplication sharedApplication]];
148cdf0e10cSrcweir }
149cdf0e10cSrcweir
150cdf0e10cSrcweir mPasteboardChangeCount = [mPasteboard changeCount];
151cdf0e10cSrcweir }
152cdf0e10cSrcweir
153cdf0e10cSrcweir
~AquaClipboard()154cdf0e10cSrcweir AquaClipboard::~AquaClipboard()
155cdf0e10cSrcweir {
156cdf0e10cSrcweir if (mIsSystemPasteboard)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir [[NSNotificationCenter defaultCenter] removeObserver: mEventListener];
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
161cdf0e10cSrcweir [mEventListener disposing];
162cdf0e10cSrcweir [mEventListener release];
163cdf0e10cSrcweir [mPasteboard release];
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir
getContents()167cdf0e10cSrcweir Reference<XTransferable> SAL_CALL AquaClipboard::getContents() throw(RuntimeException)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir MutexGuard aGuard(m_aMutex);
170cdf0e10cSrcweir
171cdf0e10cSrcweir // Shortcut: If we are clipboard owner already we don't need
172cdf0e10cSrcweir // to drag the data through the system clipboard
173cdf0e10cSrcweir if (mXClipboardContent.is())
174cdf0e10cSrcweir {
175cdf0e10cSrcweir return mXClipboardContent;
176cdf0e10cSrcweir }
177cdf0e10cSrcweir
178cdf0e10cSrcweir return Reference<XTransferable>(new OSXTransferable(mrXMimeCntFactory,
179cdf0e10cSrcweir mpDataFlavorMapper,
180cdf0e10cSrcweir mPasteboard));
181cdf0e10cSrcweir }
182cdf0e10cSrcweir
183cdf0e10cSrcweir
setContents(const Reference<XTransferable> & xTransferable,const Reference<XClipboardOwner> & xClipboardOwner)184cdf0e10cSrcweir void SAL_CALL AquaClipboard::setContents(const Reference<XTransferable>& xTransferable,
185cdf0e10cSrcweir const Reference<XClipboardOwner>& xClipboardOwner)
186cdf0e10cSrcweir throw( RuntimeException )
187cdf0e10cSrcweir {
188cdf0e10cSrcweir NSArray* types = xTransferable.is() ?
189cdf0e10cSrcweir mpDataFlavorMapper->flavorSequenceToTypesArray(xTransferable->getTransferDataFlavors()) :
190cdf0e10cSrcweir [NSArray array];
191cdf0e10cSrcweir
192cdf0e10cSrcweir ClearableMutexGuard aGuard(m_aMutex);
193cdf0e10cSrcweir
194cdf0e10cSrcweir Reference<XClipboardOwner> oldOwner(mXClipboardOwner);
195cdf0e10cSrcweir mXClipboardOwner = xClipboardOwner;
196cdf0e10cSrcweir
197cdf0e10cSrcweir Reference<XTransferable> oldContent(mXClipboardContent);
198cdf0e10cSrcweir mXClipboardContent = xTransferable;
199cdf0e10cSrcweir
200cdf0e10cSrcweir mPasteboardChangeCount = [mPasteboard declareTypes: types owner: mEventListener];
201cdf0e10cSrcweir
202cdf0e10cSrcweir aGuard.clear();
203cdf0e10cSrcweir
204cdf0e10cSrcweir // if we are already the owner of the clipboard
205cdf0e10cSrcweir // then fire lost ownership event
206cdf0e10cSrcweir if (oldOwner.is())
207cdf0e10cSrcweir {
208cdf0e10cSrcweir fireLostClipboardOwnershipEvent(oldOwner, oldContent);
209cdf0e10cSrcweir }
210cdf0e10cSrcweir
211cdf0e10cSrcweir fireClipboardChangedEvent();
212cdf0e10cSrcweir }
213cdf0e10cSrcweir
214cdf0e10cSrcweir
getName()215cdf0e10cSrcweir OUString SAL_CALL AquaClipboard::getName() throw( RuntimeException )
216cdf0e10cSrcweir {
217cdf0e10cSrcweir return OUString();
218cdf0e10cSrcweir }
219cdf0e10cSrcweir
220cdf0e10cSrcweir
getRenderingCapabilities()221cdf0e10cSrcweir sal_Int8 SAL_CALL AquaClipboard::getRenderingCapabilities() throw( RuntimeException )
222cdf0e10cSrcweir {
223cdf0e10cSrcweir return 0;
224cdf0e10cSrcweir }
225cdf0e10cSrcweir
226cdf0e10cSrcweir
addClipboardListener(const Reference<XClipboardListener> & listener)227cdf0e10cSrcweir void SAL_CALL AquaClipboard::addClipboardListener(const Reference< XClipboardListener >& listener)
228cdf0e10cSrcweir throw( RuntimeException )
229cdf0e10cSrcweir {
230cdf0e10cSrcweir MutexGuard aGuard(m_aMutex);
231cdf0e10cSrcweir
232cdf0e10cSrcweir if (!listener.is())
233cdf0e10cSrcweir throw IllegalArgumentException(OUString(RTL_CONSTASCII_USTRINGPARAM("empty reference")),
234cdf0e10cSrcweir static_cast<XClipboardEx*>(this), 1);
235cdf0e10cSrcweir
236cdf0e10cSrcweir mClipboardListeners.push_back(listener);
237cdf0e10cSrcweir }
238cdf0e10cSrcweir
239cdf0e10cSrcweir
removeClipboardListener(const Reference<XClipboardListener> & listener)240cdf0e10cSrcweir void SAL_CALL AquaClipboard::removeClipboardListener(const Reference< XClipboardListener >& listener)
241cdf0e10cSrcweir throw( RuntimeException )
242cdf0e10cSrcweir {
243cdf0e10cSrcweir MutexGuard aGuard(m_aMutex);
244cdf0e10cSrcweir
245cdf0e10cSrcweir if (!listener.is())
246cdf0e10cSrcweir throw IllegalArgumentException(OUString(RTL_CONSTASCII_USTRINGPARAM("empty reference")),
247cdf0e10cSrcweir static_cast<XClipboardEx*>(this), 1);
248cdf0e10cSrcweir
249cdf0e10cSrcweir mClipboardListeners.remove(listener);
250cdf0e10cSrcweir }
251cdf0e10cSrcweir
252cdf0e10cSrcweir
applicationDidBecomeActive(NSNotification *)253cdf0e10cSrcweir void AquaClipboard::applicationDidBecomeActive(NSNotification*)
254cdf0e10cSrcweir {
255cdf0e10cSrcweir ClearableMutexGuard aGuard(m_aMutex);
256cdf0e10cSrcweir
257cdf0e10cSrcweir int currentPboardChgCount = [mPasteboard changeCount];
258cdf0e10cSrcweir
259cdf0e10cSrcweir if (currentPboardChgCount != mPasteboardChangeCount)
260cdf0e10cSrcweir {
261cdf0e10cSrcweir mPasteboardChangeCount = currentPboardChgCount;
262cdf0e10cSrcweir
263cdf0e10cSrcweir // Clear clipboard content and owner and send lostOwnership
264cdf0e10cSrcweir // notification to the old clipboard owner as well as
265cdf0e10cSrcweir // ClipboardChanged notification to any clipboard listener
266cdf0e10cSrcweir Reference<XClipboardOwner> oldOwner(mXClipboardOwner);
267cdf0e10cSrcweir mXClipboardOwner = Reference<XClipboardOwner>();
268cdf0e10cSrcweir
269cdf0e10cSrcweir Reference<XTransferable> oldContent(mXClipboardContent);
270cdf0e10cSrcweir mXClipboardContent = Reference<XTransferable>();
271cdf0e10cSrcweir
272cdf0e10cSrcweir aGuard.clear();
273cdf0e10cSrcweir
274cdf0e10cSrcweir if (oldOwner.is())
275cdf0e10cSrcweir {
276cdf0e10cSrcweir fireLostClipboardOwnershipEvent(oldOwner, oldContent);
277cdf0e10cSrcweir }
278cdf0e10cSrcweir
279cdf0e10cSrcweir fireClipboardChangedEvent();
280cdf0e10cSrcweir }
281cdf0e10cSrcweir }
282cdf0e10cSrcweir
283cdf0e10cSrcweir
fireClipboardChangedEvent()284cdf0e10cSrcweir void AquaClipboard::fireClipboardChangedEvent()
285cdf0e10cSrcweir {
286cdf0e10cSrcweir ClearableMutexGuard aGuard(m_aMutex);
287cdf0e10cSrcweir
288cdf0e10cSrcweir list<Reference< XClipboardListener > > listeners(mClipboardListeners);
289cdf0e10cSrcweir ClipboardEvent aEvent;
290cdf0e10cSrcweir
291cdf0e10cSrcweir if (listeners.begin() != listeners.end())
292cdf0e10cSrcweir {
293cdf0e10cSrcweir aEvent = ClipboardEvent(static_cast<OWeakObject*>(this), getContents());
294cdf0e10cSrcweir }
295cdf0e10cSrcweir
296cdf0e10cSrcweir aGuard.clear();
297cdf0e10cSrcweir
298cdf0e10cSrcweir while (listeners.begin() != listeners.end())
299cdf0e10cSrcweir {
300cdf0e10cSrcweir if (listeners.front().is())
301cdf0e10cSrcweir {
302cdf0e10cSrcweir try { listeners.front()->changedContents(aEvent); }
303cdf0e10cSrcweir catch (RuntimeException&) { }
304cdf0e10cSrcweir }
305cdf0e10cSrcweir listeners.pop_front();
306cdf0e10cSrcweir }
307cdf0e10cSrcweir }
308cdf0e10cSrcweir
309cdf0e10cSrcweir
fireLostClipboardOwnershipEvent(Reference<XClipboardOwner> oldOwner,Reference<XTransferable> oldContent)310cdf0e10cSrcweir void AquaClipboard::fireLostClipboardOwnershipEvent(Reference<XClipboardOwner> oldOwner, Reference<XTransferable> oldContent)
311cdf0e10cSrcweir {
312cdf0e10cSrcweir BOOST_ASSERT(oldOwner.is());
313cdf0e10cSrcweir
314cdf0e10cSrcweir try { oldOwner->lostOwnership(static_cast<XClipboardEx*>(this), oldContent); }
315cdf0e10cSrcweir catch(RuntimeException&) { }
316cdf0e10cSrcweir }
317cdf0e10cSrcweir
318cdf0e10cSrcweir
provideDataForType(NSPasteboard * sender,const NSString * type)319*1cc6f0e2SHerbert Dürr void AquaClipboard::provideDataForType(NSPasteboard* sender, const NSString* type)
320cdf0e10cSrcweir {
321cdf0e10cSrcweir if( mXClipboardContent.is() )
322cdf0e10cSrcweir {
323cdf0e10cSrcweir DataProviderPtr_t dp = mpDataFlavorMapper->getDataProvider(type, mXClipboardContent);
324cdf0e10cSrcweir NSData* pBoardData = NULL;
325cdf0e10cSrcweir
326cdf0e10cSrcweir if (dp.get() != NULL)
327cdf0e10cSrcweir {
328cdf0e10cSrcweir pBoardData = (NSData*)dp->getSystemData();
329*1cc6f0e2SHerbert Dürr [sender setData: pBoardData forType:const_cast<NSString*>(type)];
330cdf0e10cSrcweir }
331cdf0e10cSrcweir }
332cdf0e10cSrcweir }
333cdf0e10cSrcweir
334cdf0e10cSrcweir
335cdf0e10cSrcweir //------------------------------------------------
336cdf0e10cSrcweir // XFlushableClipboard
337cdf0e10cSrcweir //------------------------------------------------
338cdf0e10cSrcweir
flushClipboard()339cdf0e10cSrcweir void SAL_CALL AquaClipboard::flushClipboard()
340cdf0e10cSrcweir throw(RuntimeException)
341cdf0e10cSrcweir {
342cdf0e10cSrcweir if (mXClipboardContent.is())
343cdf0e10cSrcweir {
344cdf0e10cSrcweir Sequence<DataFlavor> flavorList = mXClipboardContent->getTransferDataFlavors();
345cdf0e10cSrcweir sal_uInt32 nFlavors = flavorList.getLength();
3463ee7bea8SArmin Le Grand bool bInternal(false);
3473ee7bea8SArmin Le Grand
348cdf0e10cSrcweir for (sal_uInt32 i = 0; i < nFlavors; i++)
349cdf0e10cSrcweir {
350*1cc6f0e2SHerbert Dürr const NSString* sysType = mpDataFlavorMapper->openOfficeToSystemFlavor(flavorList[i], bInternal);
351*1cc6f0e2SHerbert Dürr
352cdf0e10cSrcweir if (sysType != NULL)
353cdf0e10cSrcweir {
354cdf0e10cSrcweir provideDataForType(mPasteboard, sysType);
355cdf0e10cSrcweir }
356cdf0e10cSrcweir }
357cdf0e10cSrcweir mXClipboardContent.clear();
358cdf0e10cSrcweir }
359cdf0e10cSrcweir }
360cdf0e10cSrcweir
361cdf0e10cSrcweir
getPasteboard() const362cdf0e10cSrcweir NSPasteboard* AquaClipboard::getPasteboard() const
363cdf0e10cSrcweir {
364cdf0e10cSrcweir return mPasteboard;
365cdf0e10cSrcweir }
366cdf0e10cSrcweir
367cdf0e10cSrcweir
368cdf0e10cSrcweir //-------------------------------------------------
369cdf0e10cSrcweir // XServiceInfo
370cdf0e10cSrcweir //-------------------------------------------------
371cdf0e10cSrcweir
getImplementationName()372cdf0e10cSrcweir OUString SAL_CALL AquaClipboard::getImplementationName() throw( RuntimeException )
373cdf0e10cSrcweir {
374cdf0e10cSrcweir return clipboard_getImplementationName();
375cdf0e10cSrcweir }
376cdf0e10cSrcweir
377cdf0e10cSrcweir
supportsService(const OUString &)378cdf0e10cSrcweir sal_Bool SAL_CALL AquaClipboard::supportsService( const OUString& /*ServiceName*/ ) throw( RuntimeException )
379cdf0e10cSrcweir {
380cdf0e10cSrcweir return sal_False;
381cdf0e10cSrcweir }
382cdf0e10cSrcweir
383cdf0e10cSrcweir
getSupportedServiceNames()384cdf0e10cSrcweir Sequence< OUString > SAL_CALL AquaClipboard::getSupportedServiceNames() throw( RuntimeException )
385cdf0e10cSrcweir {
386cdf0e10cSrcweir return clipboard_getSupportedServiceNames();
387cdf0e10cSrcweir }
388cdf0e10cSrcweir
389