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>
31 @implementation EventListener
;
33 -(EventListener
*)initWithAquaClipboard
: (AquaClipboard
*) pcb
43 -(void)pasteboard
:(NSPasteboard
*)sender provideDataForType
:(const NSString
*)type
46 pAquaClipboard
->provideDataForType(sender
, type
);
49 -(void)applicationDidBecomeActive
:(NSNotification
*)aNotification
52 pAquaClipboard
->applicationDidBecomeActive(aNotification
);
57 pAquaClipboard
= nullptr;
62 static OUString
clipboard_getImplementationName()
64 return "com.sun.star.datatransfer.clipboard.AquaClipboard";
67 static uno::Sequence
<OUString
> clipboard_getSupportedServiceNames()
69 return { OUString("com.sun.star.datatransfer.clipboard.SystemClipboard") };
72 AquaClipboard::AquaClipboard(NSPasteboard
* pasteboard
, bool bUseSystemPasteboard
)
73 : WeakComponentImplHelper
<XSystemClipboard
, XFlushableClipboard
, XServiceInfo
>(m_aMutex
)
74 , mIsSystemPasteboard(bUseSystemPasteboard
)
76 uno::Reference
<uno::XComponentContext
> xContext
= comphelper::getProcessComponentContext();
78 mrXMimeCntFactory
= datatransfer::MimeContentTypeFactory::create(xContext
);
80 mpDataFlavorMapper
= std::make_shared
<DataFlavorMapper
>();
82 if (pasteboard
!= nullptr)
84 mPasteboard
= pasteboard
;
85 mIsSystemPasteboard
= false;
89 SAL_WNODEPRECATED_DECLARATIONS_PUSH
//TODO: 10.13 NSDragPboard
90 mPasteboard
= bUseSystemPasteboard
? [NSPasteboard generalPasteboard
] :
91 [NSPasteboard pasteboardWithName
: NSDragPboard
];
92 SAL_WNODEPRECATED_DECLARATIONS_POP
94 if (mPasteboard
== nil
)
96 throw uno::RuntimeException("AquaClipboard: Cannot create Cocoa pasteboard",
97 static_cast<XClipboardEx
*>(this));
101 [mPasteboard retain
];
103 mEventListener
= [[EventListener alloc
] initWithAquaClipboard
: this];
105 if (mEventListener
== nil
)
107 [mPasteboard release
];
109 throw uno::RuntimeException(
110 "AquaClipboard: Cannot create pasteboard change listener",
111 static_cast<XClipboardEx
*>(this));
114 if (mIsSystemPasteboard
)
116 NSNotificationCenter
* notificationCenter
= [NSNotificationCenter defaultCenter
];
118 [notificationCenter addObserver
: mEventListener
119 selector
: @
selector(applicationDidBecomeActive
:)
120 name
: @
"NSApplicationDidBecomeActiveNotification"
121 object
: [NSApplication sharedApplication
]];
124 mPasteboardChangeCount
= [mPasteboard changeCount
];
127 AquaClipboard::~AquaClipboard()
129 if (mIsSystemPasteboard
)
131 [[NSNotificationCenter defaultCenter
] removeObserver
: mEventListener
];
134 [mEventListener disposing
];
135 [mEventListener release
];
136 [mPasteboard release
];
139 uno::Reference
<datatransfer::XTransferable
> SAL_CALL
AquaClipboard::getContents()
141 osl::MutexGuard
aGuard(m_aMutex
);
143 // Shortcut: If we are clipboard owner already we don't need
144 // to drag the data through the system clipboard
145 if (mXClipboardContent
.is())
147 return mXClipboardContent
;
150 return uno::Reference
<datatransfer::XTransferable
>(
151 new OSXTransferable(mrXMimeCntFactory
,
156 void SAL_CALL
AquaClipboard::setContents(
157 uno::Reference
<datatransfer::XTransferable
> const & xTransferable
,
158 uno::Reference
<datatransfer::clipboard::XClipboardOwner
> const & xClipboardOwner
)
160 NSArray
* types
= xTransferable
.is() ?
161 mpDataFlavorMapper
->flavorSequenceToTypesArray(xTransferable
->getTransferDataFlavors()) :
164 osl::ClearableMutexGuard
aGuard(m_aMutex
);
166 uno::Reference
<datatransfer::clipboard::XClipboardOwner
> oldOwner(mXClipboardOwner
);
167 mXClipboardOwner
= xClipboardOwner
;
169 uno::Reference
<datatransfer::XTransferable
> oldContent(mXClipboardContent
);
170 mXClipboardContent
= xTransferable
;
172 mPasteboardChangeCount
= [mPasteboard declareTypes
: types owner
: mEventListener
];
176 // if we are already the owner of the clipboard
177 // then fire lost ownership event
180 fireLostClipboardOwnershipEvent(oldOwner
, oldContent
);
183 fireClipboardChangedEvent();
186 OUString SAL_CALL
AquaClipboard::getName()
191 sal_Int8 SAL_CALL
AquaClipboard::getRenderingCapabilities()
196 void SAL_CALL
AquaClipboard::addClipboardListener(uno::Reference
<datatransfer::clipboard::XClipboardListener
> const & listener
)
198 osl::MutexGuard
aGuard(m_aMutex
);
201 throw lang::IllegalArgumentException("empty reference",
202 static_cast<XClipboardEx
*>(this), 1);
204 mClipboardListeners
.push_back(listener
);
207 void SAL_CALL
AquaClipboard::removeClipboardListener(uno::Reference
<datatransfer::clipboard::XClipboardListener
> const & listener
)
209 osl::MutexGuard
aGuard(m_aMutex
);
212 throw lang::IllegalArgumentException("empty reference",
213 static_cast<XClipboardEx
*>(this), 1);
215 mClipboardListeners
.remove(listener
);
218 void AquaClipboard::applicationDidBecomeActive(NSNotification
*)
220 osl::ClearableMutexGuard
aGuard(m_aMutex
);
222 int currentPboardChgCount
= [mPasteboard changeCount
];
224 if (currentPboardChgCount
!= mPasteboardChangeCount
)
226 mPasteboardChangeCount
= currentPboardChgCount
;
228 // Clear clipboard content and owner and send lostOwnership
229 // notification to the old clipboard owner as well as
230 // ClipboardChanged notification to any clipboard listener
231 uno::Reference
<datatransfer::clipboard::XClipboardOwner
> oldOwner(mXClipboardOwner
);
232 mXClipboardOwner
.clear();
234 uno::Reference
<datatransfer::XTransferable
> oldContent(mXClipboardContent
);
235 mXClipboardContent
.clear();
241 fireLostClipboardOwnershipEvent(oldOwner
, oldContent
);
244 fireClipboardChangedEvent();
248 void AquaClipboard::fireClipboardChangedEvent()
250 osl::ClearableMutexGuard
aGuard(m_aMutex
);
252 datatransfer::clipboard::ClipboardEvent aEvent
;
254 if (!mClipboardListeners
.empty())
256 aEvent
= datatransfer::clipboard::ClipboardEvent(static_cast<OWeakObject
*>(this), getContents());
261 for (auto const& rListener
: mClipboardListeners
)
267 rListener
->changedContents(aEvent
);
269 catch (uno::RuntimeException
& )
275 void AquaClipboard::fireLostClipboardOwnershipEvent(
276 uno::Reference
<datatransfer::clipboard::XClipboardOwner
> const & rOldOwner
,
277 uno::Reference
<datatransfer::XTransferable
> const & rOldContent
)
279 assert(rOldOwner
.is());
283 rOldOwner
->lostOwnership(static_cast<XClipboardEx
*>(this), rOldContent
);
285 catch(uno::RuntimeException
&)
289 void AquaClipboard::provideDataForType(NSPasteboard
* sender
, const NSString
* type
)
291 if( mXClipboardContent
.is() )
293 DataProviderPtr_t dp
= mpDataFlavorMapper
->getDataProvider(type
, mXClipboardContent
);
294 NSData
* pBoardData
= nullptr;
298 pBoardData
= dp
->getSystemData();
299 [sender setData
: pBoardData forType
:const_cast<NSString
*>(type
)];
304 void SAL_CALL
AquaClipboard::flushClipboard()
306 if (mXClipboardContent
.is())
308 uno::Sequence
<datatransfer::DataFlavor
> flavorList
= mXClipboardContent
->getTransferDataFlavors();
309 sal_uInt32 nFlavors
= flavorList
.getLength();
310 bool bInternal(false);
312 for (sal_uInt32 i
= 0; i
< nFlavors
; i
++)
314 const NSString
* sysType
= mpDataFlavorMapper
->openOfficeToSystemFlavor(flavorList
[i
], bInternal
);
316 if (sysType
!= nullptr)
318 provideDataForType(mPasteboard
, sysType
);
321 mXClipboardContent
.clear();
325 NSPasteboard
* AquaClipboard::getPasteboard() const
330 OUString SAL_CALL
AquaClipboard::getImplementationName()
332 return clipboard_getImplementationName();
335 sal_Bool SAL_CALL
AquaClipboard::supportsService(OUString
const & rServiceName
)
337 return cppu::supportsService(this, rServiceName
);
340 uno::Sequence
<OUString
> SAL_CALL
AquaClipboard::getSupportedServiceNames()
342 return clipboard_getSupportedServiceNames();
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */