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 "clipboard.hxx"
22 #include "DataFlavorMapping.hxx"
23 #include "OSXTransferable.hxx"
24 #include <com/sun/star/datatransfer/MimeContentTypeFactory.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include "comphelper/processfactory.hxx"
27 #include <cppuhelper/supportsservice.hxx>
29 using namespace com::sun::star::datatransfer
;
30 using namespace com::sun::star::datatransfer::clipboard
;
31 using namespace com::sun::star::lang
;
32 using namespace com::sun::star::uno
;
36 using namespace comphelper
;
38 @implementation EventListener
;
40 -(EventListener
*)initWithAquaClipboard
: (AquaClipboard
*) pcb
50 -(void)pasteboard
:(NSPasteboard
*)sender provideDataForType
:(const NSString
*)type
53 pAquaClipboard
->provideDataForType(sender
, type
);
56 -(void)applicationDidBecomeActive
:(NSNotification
*)aNotification
59 pAquaClipboard
->applicationDidBecomeActive(aNotification
);
64 pAquaClipboard
= nullptr;
69 OUString
clipboard_getImplementationName()
71 return OUString("com.sun.star.datatransfer.clipboard.AquaClipboard");
74 Sequence
<OUString
> clipboard_getSupportedServiceNames()
76 return { OUString("com.sun.star.datatransfer.clipboard.SystemClipboard") };
79 AquaClipboard::AquaClipboard(NSPasteboard
* pasteboard
, bool bUseSystemPasteboard
) :
80 WeakComponentImplHelper
<XSystemClipboard
, XFlushableClipboard
, XServiceInfo
>(m_aMutex
),
81 mIsSystemPasteboard(bUseSystemPasteboard
)
83 Reference
<XComponentContext
> xContext
= comphelper::getProcessComponentContext();
85 mrXMimeCntFactory
= MimeContentTypeFactory::create(xContext
);
87 mpDataFlavorMapper
= DataFlavorMapperPtr_t(new DataFlavorMapper());
89 if (pasteboard
!= nullptr)
91 mPasteboard
= pasteboard
;
92 mIsSystemPasteboard
= false;
96 mPasteboard
= bUseSystemPasteboard
? [NSPasteboard generalPasteboard
] :
97 [NSPasteboard pasteboardWithName
: NSDragPboard
];
99 if (mPasteboard
== nil
)
101 throw RuntimeException("AquaClipboard: Cannot create Cocoa pasteboard",
102 static_cast<XClipboardEx
*>(this));
106 [mPasteboard retain
];
108 mEventListener
= [[EventListener alloc
] initWithAquaClipboard
: this];
110 if (mEventListener
== nil
)
112 [mPasteboard release
];
114 throw RuntimeException(
115 "AquaClipboard: Cannot create pasteboard change listener",
116 static_cast<XClipboardEx
*>(this));
119 if (mIsSystemPasteboard
)
121 NSNotificationCenter
* notificationCenter
= [NSNotificationCenter defaultCenter
];
123 [notificationCenter addObserver
: mEventListener
124 selector
: @
selector(applicationDidBecomeActive
:)
125 name
: @
"NSApplicationDidBecomeActiveNotification"
126 object
: [NSApplication sharedApplication
]];
129 mPasteboardChangeCount
= [mPasteboard changeCount
];
132 AquaClipboard::~AquaClipboard()
134 if (mIsSystemPasteboard
)
136 [[NSNotificationCenter defaultCenter
] removeObserver
: mEventListener
];
139 [mEventListener disposing
];
140 [mEventListener release
];
141 [mPasteboard release
];
144 Reference
<XTransferable
> SAL_CALL
AquaClipboard::getContents()
146 MutexGuard
aGuard(m_aMutex
);
148 // Shortcut: If we are clipboard owner already we don't need
149 // to drag the data through the system clipboard
150 if (mXClipboardContent
.is())
152 return mXClipboardContent
;
155 return Reference
<XTransferable
>(new OSXTransferable(mrXMimeCntFactory
,
160 void SAL_CALL
AquaClipboard::setContents(const Reference
<XTransferable
>& xTransferable
,
161 const Reference
<XClipboardOwner
>& xClipboardOwner
)
163 NSArray
* types
= xTransferable
.is() ?
164 mpDataFlavorMapper
->flavorSequenceToTypesArray(xTransferable
->getTransferDataFlavors()) :
167 ClearableMutexGuard
aGuard(m_aMutex
);
169 Reference
<XClipboardOwner
> oldOwner(mXClipboardOwner
);
170 mXClipboardOwner
= xClipboardOwner
;
172 Reference
<XTransferable
> oldContent(mXClipboardContent
);
173 mXClipboardContent
= xTransferable
;
175 mPasteboardChangeCount
= [mPasteboard declareTypes
: types owner
: mEventListener
];
179 // if we are already the owner of the clipboard
180 // then fire lost ownership event
183 fireLostClipboardOwnershipEvent(oldOwner
, oldContent
);
186 fireClipboardChangedEvent();
189 OUString SAL_CALL
AquaClipboard::getName()
194 sal_Int8 SAL_CALL
AquaClipboard::getRenderingCapabilities()
199 void SAL_CALL
AquaClipboard::addClipboardListener(const Reference
< XClipboardListener
>& listener
)
201 MutexGuard
aGuard(m_aMutex
);
204 throw IllegalArgumentException("empty reference",
205 static_cast<XClipboardEx
*>(this), 1);
207 mClipboardListeners
.push_back(listener
);
210 void SAL_CALL
AquaClipboard::removeClipboardListener(const Reference
< XClipboardListener
>& listener
)
212 MutexGuard
aGuard(m_aMutex
);
215 throw IllegalArgumentException("empty reference",
216 static_cast<XClipboardEx
*>(this), 1);
218 mClipboardListeners
.remove(listener
);
221 void AquaClipboard::applicationDidBecomeActive(NSNotification
*)
223 ClearableMutexGuard
aGuard(m_aMutex
);
225 int currentPboardChgCount
= [mPasteboard changeCount
];
227 if (currentPboardChgCount
!= mPasteboardChangeCount
)
229 mPasteboardChangeCount
= currentPboardChgCount
;
231 // Clear clipboard content and owner and send lostOwnership
232 // notification to the old clipboard owner as well as
233 // ClipboardChanged notification to any clipboard listener
234 Reference
<XClipboardOwner
> oldOwner(mXClipboardOwner
);
235 mXClipboardOwner
.clear();
237 Reference
<XTransferable
> oldContent(mXClipboardContent
);
238 mXClipboardContent
.clear();
244 fireLostClipboardOwnershipEvent(oldOwner
, oldContent
);
247 fireClipboardChangedEvent();
251 void AquaClipboard::fireClipboardChangedEvent()
253 ClearableMutexGuard
aGuard(m_aMutex
);
255 list
<Reference
< XClipboardListener
> > listeners(mClipboardListeners
);
256 ClipboardEvent aEvent
;
258 if (!listeners
.empty())
260 aEvent
= ClipboardEvent(static_cast<OWeakObject
*>(this), getContents());
265 while (!listeners
.empty())
267 if (listeners
.front().is())
269 try { listeners
.front()->changedContents(aEvent
); }
270 catch (RuntimeException
&) { }
272 listeners
.pop_front();
276 void AquaClipboard::fireLostClipboardOwnershipEvent(Reference
<XClipboardOwner
> const & oldOwner
, Reference
<XTransferable
> const & oldContent
)
278 assert(oldOwner
.is());
280 try { oldOwner
->lostOwnership(static_cast<XClipboardEx
*>(this), oldContent
); }
281 catch(RuntimeException
&) { }
284 void AquaClipboard::provideDataForType(NSPasteboard
* sender
, const NSString
* type
)
286 if( mXClipboardContent
.is() )
288 DataProviderPtr_t dp
= mpDataFlavorMapper
->getDataProvider(type
, mXClipboardContent
);
289 NSData
* pBoardData
= nullptr;
291 if (dp
.get() != nullptr)
293 pBoardData
= (NSData
*)dp
->getSystemData();
294 [sender setData
: pBoardData forType
:const_cast<NSString
*>(type
)];
299 void SAL_CALL
AquaClipboard::flushClipboard()
301 if (mXClipboardContent
.is())
303 Sequence
<DataFlavor
> flavorList
= mXClipboardContent
->getTransferDataFlavors();
304 sal_uInt32 nFlavors
= flavorList
.getLength();
305 bool bInternal(false);
307 for (sal_uInt32 i
= 0; i
< nFlavors
; i
++)
309 const NSString
* sysType
= mpDataFlavorMapper
->openOfficeToSystemFlavor(flavorList
[i
], bInternal
);
311 if (sysType
!= nullptr)
313 provideDataForType(mPasteboard
, sysType
);
316 mXClipboardContent
.clear();
320 NSPasteboard
* AquaClipboard::getPasteboard() const
325 OUString SAL_CALL
AquaClipboard::getImplementationName()
327 return clipboard_getImplementationName();
330 sal_Bool SAL_CALL
AquaClipboard::supportsService( const OUString
& ServiceName
)
332 return cppu::supportsService(this, ServiceName
);
335 Sequence
< OUString
> SAL_CALL
AquaClipboard::getSupportedServiceNames()
337 return clipboard_getSupportedServiceNames();
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */