bump product version to 4.2.0.1
[LibreOffice.git] / vcl / aqua / source / dtrans / aqua_clipboard.cxx
blobac3bcaee1d60ef88b9ff5c4eaaca38815fa301f9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "aqua_clipboard.hxx"
22 #include "DataFlavorMapping.hxx"
23 #include "OSXTransferable.hxx"
24 #include <com/sun/star/datatransfer/MimeContentTypeFactory.hpp>
25 #include "comphelper/makesequence.hxx"
26 #include "comphelper/processfactory.hxx"
28 #include <boost/assert.hpp>
30 using namespace com::sun::star::datatransfer;
31 using namespace com::sun::star::datatransfer::clipboard;
32 using namespace com::sun::star::lang;
33 using namespace com::sun::star::uno;
34 using namespace cppu;
35 using namespace osl;
36 using namespace std;
37 using namespace comphelper;
40 @implementation EventListener;
42 -(EventListener*)initWithAquaClipboard: (AquaClipboard*) pcb
44 self = [super init];
46 if (self)
47 pAquaClipboard = pcb;
49 return self;
52 -(void)pasteboard:(NSPasteboard*)sender provideDataForType:(NSString*)type
54 if( pAquaClipboard )
55 pAquaClipboard->provideDataForType(sender, type);
58 -(void)applicationDidBecomeActive:(NSNotification*)aNotification
60 if( pAquaClipboard )
61 pAquaClipboard->applicationDidBecomeActive(aNotification);
64 -(void)disposing
66 pAquaClipboard = NULL;
69 @end
72 OUString clipboard_getImplementationName()
74 return OUString("com.sun.star.datatransfer.clipboard.AquaClipboard");
77 Sequence<OUString> clipboard_getSupportedServiceNames()
79 return makeSequence(OUString("com.sun.star.datatransfer.clipboard.SystemClipboard"));
83 AquaClipboard::AquaClipboard(NSPasteboard* pasteboard, bool bUseSystemPasteboard) :
84 WeakComponentImplHelper3<XSystemClipboard, XFlushableClipboard, XServiceInfo>(m_aMutex),
85 mIsSystemPasteboard(bUseSystemPasteboard)
87 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
89 mrXMimeCntFactory = MimeContentTypeFactory::create(xContext);
91 mpDataFlavorMapper = DataFlavorMapperPtr_t(new DataFlavorMapper());
93 if (pasteboard != NULL)
95 mPasteboard = pasteboard;
96 mIsSystemPasteboard = false;
98 else
100 mPasteboard = bUseSystemPasteboard ? [NSPasteboard generalPasteboard] :
101 [NSPasteboard pasteboardWithName: NSDragPboard];
103 if (mPasteboard == nil)
105 throw RuntimeException("AquaClipboard: Cannot create Cocoa pasteboard",
106 static_cast<XClipboardEx*>(this));
110 [mPasteboard retain];
112 mEventListener = [[EventListener alloc] initWithAquaClipboard: this];
114 if (mEventListener == nil)
116 [mPasteboard release];
118 throw RuntimeException(
119 OUString("AquaClipboard: Cannot create pasteboard change listener"),
120 static_cast<XClipboardEx*>(this));
123 if (mIsSystemPasteboard)
125 NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
127 [notificationCenter addObserver: mEventListener
128 selector: @selector(applicationDidBecomeActive:)
129 name: @"NSApplicationDidBecomeActiveNotification"
130 object: [NSApplication sharedApplication]];
133 mPasteboardChangeCount = [mPasteboard changeCount];
137 AquaClipboard::~AquaClipboard()
139 if (mIsSystemPasteboard)
141 [[NSNotificationCenter defaultCenter] removeObserver: mEventListener];
144 [mEventListener disposing];
145 [mEventListener release];
146 [mPasteboard release];
150 Reference<XTransferable> SAL_CALL AquaClipboard::getContents() throw(RuntimeException)
152 MutexGuard aGuard(m_aMutex);
154 // Shortcut: If we are clipboard owner already we don't need
155 // to drag the data through the system clipboard
156 if (mXClipboardContent.is())
158 return mXClipboardContent;
161 return Reference<XTransferable>(new OSXTransferable(mrXMimeCntFactory,
162 mpDataFlavorMapper,
163 mPasteboard));
167 void SAL_CALL AquaClipboard::setContents(const Reference<XTransferable>& xTransferable,
168 const Reference<XClipboardOwner>& xClipboardOwner)
169 throw( RuntimeException )
171 NSArray* types = xTransferable.is() ?
172 mpDataFlavorMapper->flavorSequenceToTypesArray(xTransferable->getTransferDataFlavors()) :
173 [NSArray array];
175 ClearableMutexGuard aGuard(m_aMutex);
177 Reference<XClipboardOwner> oldOwner(mXClipboardOwner);
178 mXClipboardOwner = xClipboardOwner;
180 Reference<XTransferable> oldContent(mXClipboardContent);
181 mXClipboardContent = xTransferable;
183 mPasteboardChangeCount = [mPasteboard declareTypes: types owner: mEventListener];
185 aGuard.clear();
187 // if we are already the owner of the clipboard
188 // then fire lost ownership event
189 if (oldOwner.is())
191 fireLostClipboardOwnershipEvent(oldOwner, oldContent);
194 fireClipboardChangedEvent();
198 OUString SAL_CALL AquaClipboard::getName() throw( RuntimeException )
200 return OUString();
204 sal_Int8 SAL_CALL AquaClipboard::getRenderingCapabilities() throw( RuntimeException )
206 return 0;
210 void SAL_CALL AquaClipboard::addClipboardListener(const Reference< XClipboardListener >& listener)
211 throw( RuntimeException )
213 MutexGuard aGuard(m_aMutex);
215 if (!listener.is())
216 throw IllegalArgumentException("empty reference",
217 static_cast<XClipboardEx*>(this), 1);
219 mClipboardListeners.push_back(listener);
223 void SAL_CALL AquaClipboard::removeClipboardListener(const Reference< XClipboardListener >& listener)
224 throw( RuntimeException )
226 MutexGuard aGuard(m_aMutex);
228 if (!listener.is())
229 throw IllegalArgumentException("empty reference",
230 static_cast<XClipboardEx*>(this), 1);
232 mClipboardListeners.remove(listener);
236 void AquaClipboard::applicationDidBecomeActive(NSNotification*)
238 ClearableMutexGuard aGuard(m_aMutex);
240 int currentPboardChgCount = [mPasteboard changeCount];
242 if (currentPboardChgCount != mPasteboardChangeCount)
244 mPasteboardChangeCount = currentPboardChgCount;
246 // Clear clipboard content and owner and send lostOwnership
247 // notification to the old clipboard owner as well as
248 // ClipboardChanged notification to any clipboard listener
249 Reference<XClipboardOwner> oldOwner(mXClipboardOwner);
250 mXClipboardOwner.clear();
252 Reference<XTransferable> oldContent(mXClipboardContent);
253 mXClipboardContent.clear();
255 aGuard.clear();
257 if (oldOwner.is())
259 fireLostClipboardOwnershipEvent(oldOwner, oldContent);
262 fireClipboardChangedEvent();
267 void AquaClipboard::fireClipboardChangedEvent()
269 ClearableMutexGuard aGuard(m_aMutex);
271 list<Reference< XClipboardListener > > listeners(mClipboardListeners);
272 ClipboardEvent aEvent;
274 if (listeners.begin() != listeners.end())
276 aEvent = ClipboardEvent(static_cast<OWeakObject*>(this), getContents());
279 aGuard.clear();
281 while (listeners.begin() != listeners.end())
283 if (listeners.front().is())
285 try { listeners.front()->changedContents(aEvent); }
286 catch (RuntimeException&) { }
288 listeners.pop_front();
293 void AquaClipboard::fireLostClipboardOwnershipEvent(Reference<XClipboardOwner> oldOwner, Reference<XTransferable> oldContent)
295 BOOST_ASSERT(oldOwner.is());
297 try { oldOwner->lostOwnership(static_cast<XClipboardEx*>(this), oldContent); }
298 catch(RuntimeException&) { }
302 void AquaClipboard::provideDataForType(NSPasteboard* sender, NSString* type)
304 if( mXClipboardContent.is() )
306 DataProviderPtr_t dp = mpDataFlavorMapper->getDataProvider(type, mXClipboardContent);
307 NSData* pBoardData = NULL;
309 if (dp.get() != NULL)
311 pBoardData = (NSData*)dp->getSystemData();
312 [sender setData: pBoardData forType: type];
318 //------------------------------------------------
319 // XFlushableClipboard
320 //------------------------------------------------
322 void SAL_CALL AquaClipboard::flushClipboard()
323 throw(RuntimeException)
325 if (mXClipboardContent.is())
327 Sequence<DataFlavor> flavorList = mXClipboardContent->getTransferDataFlavors();
328 sal_uInt32 nFlavors = flavorList.getLength();
329 bool bInternal(false);
331 for (sal_uInt32 i = 0; i < nFlavors; i++)
333 NSString* sysType = mpDataFlavorMapper->openOfficeToSystemFlavor(flavorList[i], bInternal);
335 if (sysType != NULL)
337 provideDataForType(mPasteboard, sysType);
340 mXClipboardContent.clear();
345 NSPasteboard* AquaClipboard::getPasteboard() const
347 return mPasteboard;
351 //-------------------------------------------------
352 // XServiceInfo
353 //-------------------------------------------------
355 OUString SAL_CALL AquaClipboard::getImplementationName() throw( RuntimeException )
357 return clipboard_getImplementationName();
361 sal_Bool SAL_CALL AquaClipboard::supportsService( const OUString& /*ServiceName*/ ) throw( RuntimeException )
363 return sal_False;
367 Sequence< OUString > SAL_CALL AquaClipboard::getSupportedServiceNames() throw( RuntimeException )
369 return clipboard_getSupportedServiceNames();
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */