1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
37 using namespace comphelper
;
40 @implementation EventListener
;
42 -(EventListener
*)initWithAquaClipboard
: (AquaClipboard
*) pcb
52 -(void)pasteboard
:(NSPasteboard
*)sender provideDataForType
:(NSString
*)type
55 pAquaClipboard
->provideDataForType(sender
, type
);
58 -(void)applicationDidBecomeActive
:(NSNotification
*)aNotification
61 pAquaClipboard
->applicationDidBecomeActive(aNotification
);
66 pAquaClipboard
= NULL
;
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;
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
,
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()) :
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
];
187 // if we are already the owner of the clipboard
188 // then fire lost ownership event
191 fireLostClipboardOwnershipEvent(oldOwner
, oldContent
);
194 fireClipboardChangedEvent();
198 OUString SAL_CALL
AquaClipboard::getName() throw( RuntimeException
)
204 sal_Int8 SAL_CALL
AquaClipboard::getRenderingCapabilities() throw( RuntimeException
)
210 void SAL_CALL
AquaClipboard::addClipboardListener(const Reference
< XClipboardListener
>& listener
)
211 throw( RuntimeException
)
213 MutexGuard
aGuard(m_aMutex
);
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
);
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();
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());
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
);
337 provideDataForType(mPasteboard
, sysType
);
340 mXClipboardContent
.clear();
345 NSPasteboard
* AquaClipboard::getPasteboard() const
351 //-------------------------------------------------
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
)
367 Sequence
< OUString
> SAL_CALL
AquaClipboard::getSupportedServiceNames() throw( RuntimeException
)
369 return clipboard_getSupportedServiceNames();
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */