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 "comphelper/makesequence.hxx"
26 #include "comphelper/processfactory.hxx"
27 #include <cppuhelper/supportsservice.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
;
39 @implementation EventListener
;
41 -(EventListener
*)initWithAquaClipboard
: (AquaClipboard
*) pcb
51 -(void)pasteboard
:(NSPasteboard
*)sender provideDataForType
:(const NSString
*)type
54 pAquaClipboard
->provideDataForType(sender
, type
);
57 -(void)applicationDidBecomeActive
:(NSNotification
*)aNotification
60 pAquaClipboard
->applicationDidBecomeActive(aNotification
);
65 pAquaClipboard
= NULL
;
70 OUString
clipboard_getImplementationName()
72 return OUString("com.sun.star.datatransfer.clipboard.AquaClipboard");
75 Sequence
<OUString
> clipboard_getSupportedServiceNames()
77 return makeSequence(OUString("com.sun.star.datatransfer.clipboard.SystemClipboard"));
80 AquaClipboard::AquaClipboard(NSPasteboard
* pasteboard
, bool bUseSystemPasteboard
) :
81 WeakComponentImplHelper3
<XSystemClipboard
, XFlushableClipboard
, XServiceInfo
>(m_aMutex
),
82 mIsSystemPasteboard(bUseSystemPasteboard
)
84 Reference
<XComponentContext
> xContext
= comphelper::getProcessComponentContext();
86 mrXMimeCntFactory
= MimeContentTypeFactory::create(xContext
);
88 mpDataFlavorMapper
= DataFlavorMapperPtr_t(new DataFlavorMapper());
90 if (pasteboard
!= NULL
)
92 mPasteboard
= pasteboard
;
93 mIsSystemPasteboard
= false;
97 mPasteboard
= bUseSystemPasteboard
? [NSPasteboard generalPasteboard
] :
98 [NSPasteboard pasteboardWithName
: NSDragPboard
];
100 if (mPasteboard
== nil
)
102 throw RuntimeException("AquaClipboard: Cannot create Cocoa pasteboard",
103 static_cast<XClipboardEx
*>(this));
107 [mPasteboard retain
];
109 mEventListener
= [[EventListener alloc
] initWithAquaClipboard
: this];
111 if (mEventListener
== nil
)
113 [mPasteboard release
];
115 throw RuntimeException(
116 "AquaClipboard: Cannot create pasteboard change listener",
117 static_cast<XClipboardEx
*>(this));
120 if (mIsSystemPasteboard
)
122 NSNotificationCenter
* notificationCenter
= [NSNotificationCenter defaultCenter
];
124 [notificationCenter addObserver
: mEventListener
125 selector
: @
selector(applicationDidBecomeActive
:)
126 name
: @
"NSApplicationDidBecomeActiveNotification"
127 object
: [NSApplication sharedApplication
]];
130 mPasteboardChangeCount
= [mPasteboard changeCount
];
133 AquaClipboard::~AquaClipboard()
135 if (mIsSystemPasteboard
)
137 [[NSNotificationCenter defaultCenter
] removeObserver
: mEventListener
];
140 [mEventListener disposing
];
141 [mEventListener release
];
142 [mPasteboard release
];
145 Reference
<XTransferable
> SAL_CALL
AquaClipboard::getContents() throw(RuntimeException
, std::exception
)
147 MutexGuard
aGuard(m_aMutex
);
149 // Shortcut: If we are clipboard owner already we don't need
150 // to drag the data through the system clipboard
151 if (mXClipboardContent
.is())
153 return mXClipboardContent
;
156 return Reference
<XTransferable
>(new OSXTransferable(mrXMimeCntFactory
,
161 void SAL_CALL
AquaClipboard::setContents(const Reference
<XTransferable
>& xTransferable
,
162 const Reference
<XClipboardOwner
>& xClipboardOwner
)
163 throw( RuntimeException
, std::exception
)
165 NSArray
* types
= xTransferable
.is() ?
166 mpDataFlavorMapper
->flavorSequenceToTypesArray(xTransferable
->getTransferDataFlavors()) :
169 ClearableMutexGuard
aGuard(m_aMutex
);
171 Reference
<XClipboardOwner
> oldOwner(mXClipboardOwner
);
172 mXClipboardOwner
= xClipboardOwner
;
174 Reference
<XTransferable
> oldContent(mXClipboardContent
);
175 mXClipboardContent
= xTransferable
;
177 mPasteboardChangeCount
= [mPasteboard declareTypes
: types owner
: mEventListener
];
181 // if we are already the owner of the clipboard
182 // then fire lost ownership event
185 fireLostClipboardOwnershipEvent(oldOwner
, oldContent
);
188 fireClipboardChangedEvent();
191 OUString SAL_CALL
AquaClipboard::getName() throw( RuntimeException
, std::exception
)
196 sal_Int8 SAL_CALL
AquaClipboard::getRenderingCapabilities() throw( RuntimeException
, std::exception
)
201 void SAL_CALL
AquaClipboard::addClipboardListener(const Reference
< XClipboardListener
>& listener
)
202 throw( RuntimeException
, std::exception
)
204 MutexGuard
aGuard(m_aMutex
);
207 throw IllegalArgumentException("empty reference",
208 static_cast<XClipboardEx
*>(this), 1);
210 mClipboardListeners
.push_back(listener
);
213 void SAL_CALL
AquaClipboard::removeClipboardListener(const Reference
< XClipboardListener
>& listener
)
214 throw( RuntimeException
, std::exception
)
216 MutexGuard
aGuard(m_aMutex
);
219 throw IllegalArgumentException("empty reference",
220 static_cast<XClipboardEx
*>(this), 1);
222 mClipboardListeners
.remove(listener
);
225 void AquaClipboard::applicationDidBecomeActive(NSNotification
*)
227 ClearableMutexGuard
aGuard(m_aMutex
);
229 int currentPboardChgCount
= [mPasteboard changeCount
];
231 if (currentPboardChgCount
!= mPasteboardChangeCount
)
233 mPasteboardChangeCount
= currentPboardChgCount
;
235 // Clear clipboard content and owner and send lostOwnership
236 // notification to the old clipboard owner as well as
237 // ClipboardChanged notification to any clipboard listener
238 Reference
<XClipboardOwner
> oldOwner(mXClipboardOwner
);
239 mXClipboardOwner
.clear();
241 Reference
<XTransferable
> oldContent(mXClipboardContent
);
242 mXClipboardContent
.clear();
248 fireLostClipboardOwnershipEvent(oldOwner
, oldContent
);
251 fireClipboardChangedEvent();
255 void AquaClipboard::fireClipboardChangedEvent()
257 ClearableMutexGuard
aGuard(m_aMutex
);
259 list
<Reference
< XClipboardListener
> > listeners(mClipboardListeners
);
260 ClipboardEvent aEvent
;
262 if (listeners
.begin() != listeners
.end())
264 aEvent
= ClipboardEvent(static_cast<OWeakObject
*>(this), getContents());
269 while (listeners
.begin() != listeners
.end())
271 if (listeners
.front().is())
273 try { listeners
.front()->changedContents(aEvent
); }
274 catch (RuntimeException
&) { }
276 listeners
.pop_front();
280 void AquaClipboard::fireLostClipboardOwnershipEvent(Reference
<XClipboardOwner
> oldOwner
, Reference
<XTransferable
> oldContent
)
282 BOOST_ASSERT(oldOwner
.is());
284 try { oldOwner
->lostOwnership(static_cast<XClipboardEx
*>(this), oldContent
); }
285 catch(RuntimeException
&) { }
288 void AquaClipboard::provideDataForType(NSPasteboard
* sender
, const NSString
* type
)
290 if( mXClipboardContent
.is() )
292 DataProviderPtr_t dp
= mpDataFlavorMapper
->getDataProvider(type
, mXClipboardContent
);
293 NSData
* pBoardData
= NULL
;
295 if (dp
.get() != NULL
)
297 pBoardData
= (NSData
*)dp
->getSystemData();
298 [sender setData
: pBoardData forType
:const_cast<NSString
*>(type
)];
303 void SAL_CALL
AquaClipboard::flushClipboard()
304 throw(RuntimeException
, std::exception
)
306 if (mXClipboardContent
.is())
308 Sequence
<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
);
318 provideDataForType(mPasteboard
, sysType
);
321 mXClipboardContent
.clear();
325 NSPasteboard
* AquaClipboard::getPasteboard() const
330 OUString SAL_CALL
AquaClipboard::getImplementationName() throw( RuntimeException
, std::exception
)
332 return clipboard_getImplementationName();
335 sal_Bool SAL_CALL
AquaClipboard::supportsService( const OUString
& ServiceName
) throw( RuntimeException
, std::exception
)
337 return cppu::supportsService(this, ServiceName
);
340 Sequence
< OUString
> SAL_CALL
AquaClipboard::getSupportedServiceNames() throw( RuntimeException
, std::exception
)
342 return clipboard_getSupportedServiceNames();
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */