1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the plugins of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qmacclipboard.h"
43 #include "qclipboard.h"
44 #include "qapplication.h"
46 #include "qdatetime.h"
48 #include "qguiapplication.h"
53 #include "qcocoahelpers.h"
55 #include "qcocoaautoreleasepool.h"
61 /*****************************************************************************
62 QClipboard debug facilities
63 *****************************************************************************/
64 //#define DEBUG_PASTEBOARD
66 /*****************************************************************************
68 *****************************************************************************/
70 QMacPasteboard::QMacPasteboard(PasteboardRef p, uchar mt)
72 mac_mime_source = false;
73 mime_type = mt ? mt : uchar(QMacPasteboardMime::MIME_ALL);
78 QMacPasteboard::QMacPasteboard(uchar mt)
80 mac_mime_source = false;
81 mime_type = mt ? mt : uchar(QMacPasteboardMime::MIME_ALL);
83 OSStatus err = PasteboardCreate(0, &paste);
85 PasteboardSetPromiseKeeper(paste, promiseKeeper, this);
87 qDebug("PasteBoard: Error creating pasteboard: [%d]", (int)err);
91 QMacPasteboard::QMacPasteboard(CFStringRef name, uchar mt)
93 mac_mime_source = false;
94 mime_type = mt ? mt : uchar(QMacPasteboardMime::MIME_ALL);
96 OSStatus err = PasteboardCreate(name, &paste);
98 PasteboardSetPromiseKeeper(paste, promiseKeeper, this);
100 qDebug("PasteBoard: Error creating pasteboard: %s [%d]", QCFString::toQString(name).toLatin1().constData(), (int)err);
104 QMacPasteboard::~QMacPasteboard()
106 // commit all promises for paste after exit close
107 for (int i = 0; i < promises.count(); ++i) {
108 const Promise &promise = promises.at(i);
109 QCFString flavor = QCFString(promise.convertor->flavorFor(promise.mime));
110 promiseKeeper(paste, (PasteboardItemID)promise.itemId, flavor, this);
118 QMacPasteboard::pasteBoard() const
123 OSStatus QMacPasteboard::promiseKeeper(PasteboardRef paste, PasteboardItemID id, CFStringRef flavor, void *_qpaste)
125 QMacPasteboard *qpaste = (QMacPasteboard*)_qpaste;
126 const long promise_id = (long)id;
128 // Find the kept promise
129 const QString flavorAsQString = QCFString::toQString(flavor);
130 QMacPasteboard::Promise promise;
131 for (int i = 0; i < qpaste->promises.size(); i++){
132 QMacPasteboard::Promise tmp = qpaste->promises[i];
133 if (tmp.itemId == promise_id && tmp.convertor->canConvert(tmp.mime, flavorAsQString)){
139 if (!promise.itemId && flavorAsQString == QLatin1String("com.trolltech.qt.MimeTypeName")) {
140 // we have promised this data, but wont be able to convert, so return null data.
141 // This helps in making the application/x-qt-mime-type-name hidden from normal use.
143 QCFType<CFDataRef> data = CFDataCreate(0, (UInt8*)ba.constData(), ba.size());
144 PasteboardPutItemFlavor(paste, id, flavor, data, kPasteboardFlavorNoFlags);
148 if (!promise.itemId) {
149 // There was no promise that could deliver data for the
150 // given id and flavor. This should not happend.
151 qDebug("Pasteboard: %d: Request for %ld, %s, but no promise found!", __LINE__, promise_id, qPrintable(flavorAsQString));
152 return cantGetFlavorErr;
155 #ifdef DEBUG_PASTEBOARD
156 qDebug("PasteBoard: Calling in promise for %s[%ld] [%s] (%s) [%d]", qPrintable(promise.mime), promise_id,
157 qPrintable(flavorAsQString), qPrintable(promise.convertor->convertorName()), promise.offset);
160 QList<QByteArray> md = promise.convertor->convertFromMime(promise.mime, promise.data, flavorAsQString);
161 if (md.size() <= promise.offset)
162 return cantGetFlavorErr;
163 const QByteArray &ba = md[promise.offset];
164 QCFType<CFDataRef> data = CFDataCreate(0, (UInt8*)ba.constData(), ba.size());
165 PasteboardPutItemFlavor(paste, id, flavor, data, kPasteboardFlavorNoFlags);
170 QMacPasteboard::hasOSType(int c_flavor) const
178 if (PasteboardGetItemCount(paste, &cnt) || !cnt)
181 #ifdef DEBUG_PASTEBOARD
182 qDebug("PasteBoard: hasOSType [%c%c%c%c]", (c_flavor>>24)&0xFF, (c_flavor>>16)&0xFF,
183 (c_flavor>>8)&0xFF, (c_flavor>>0)&0xFF);
185 for (uint index = 1; index <= cnt; ++index) {
188 if (PasteboardGetItemIdentifier(paste, index, &id) != noErr)
191 QCFType<CFArrayRef> types;
192 if (PasteboardCopyItemFlavors(paste, id, &types ) != noErr)
195 const int type_count = CFArrayGetCount(types);
196 for (int i = 0; i < type_count; ++i) {
197 CFStringRef flavor = (CFStringRef)CFArrayGetValueAtIndex(types, i);
198 const int os_flavor = UTGetOSTypeFromString(UTTypeCopyPreferredTagWithClass(flavor, kUTTagClassOSType));
199 if (os_flavor == c_flavor) {
200 #ifdef DEBUG_PASTEBOARD
207 #ifdef DEBUG_PASTEBOARD
208 qDebug(" - NotFound!");
214 QMacPasteboard::hasFlavor(QString c_flavor) const
222 if (PasteboardGetItemCount(paste, &cnt) || !cnt)
225 #ifdef DEBUG_PASTEBOARD
226 qDebug("PasteBoard: hasFlavor [%s]", qPrintable(c_flavor));
228 for (uint index = 1; index <= cnt; ++index) {
231 if (PasteboardGetItemIdentifier(paste, index, &id) != noErr)
234 PasteboardFlavorFlags flags;
235 if (PasteboardGetItemFlavorFlags(paste, id, QCFString(c_flavor), &flags) == noErr) {
236 #ifdef DEBUG_PASTEBOARD
242 #ifdef DEBUG_PASTEBOARD
243 qDebug(" - NotFound!");
248 class QMacPasteboardMimeSource : public QMimeData {
249 const QMacPasteboard *paste;
251 QMacPasteboardMimeSource(const QMacPasteboard *p) : QMimeData(), paste(p) { }
252 ~QMacPasteboardMimeSource() { }
253 virtual QStringList formats() const { return paste->formats(); }
254 virtual QVariant retrieveData(const QString &format, QVariant::Type type) const { return paste->retrieveData(format, type); }
258 *QMacPasteboard::mimeData() const
261 mac_mime_source = true;
262 mime = new QMacPasteboardMimeSource(this);
268 class QMacMimeData : public QMimeData
271 QVariant variantData(const QString &mime) { return retrieveData(mime, QVariant::Invalid); }
277 QMacPasteboard::setMimeData(QMimeData *mime_src)
282 if (mime == mime_src || (!mime_src && mime && mac_mime_source))
284 mac_mime_source = false;
288 QList<QMacPasteboardMime*> availableConverters = QMacPasteboardMime::all(mime_type);
291 QStringList formats = mime_src->formats();
293 // QMimeData sub classes reimplementing the formats() might not expose the
294 // temporary "application/x-qt-mime-type-name" mimetype. So check the existence
295 // of this mime type while doing drag and drop.
296 QString dummyMimeType(QLatin1String("application/x-qt-mime-type-name"));
297 if (!formats.contains(dummyMimeType)) {
298 QByteArray dummyType = mime_src->data(dummyMimeType);
299 if (!dummyType.isEmpty()) {
300 formats.append(dummyMimeType);
303 for (int f = 0; f < formats.size(); ++f) {
304 QString mimeType = formats.at(f);
305 for (QList<QMacPasteboardMime *>::Iterator it = availableConverters.begin(); it != availableConverters.end(); ++it) {
306 QMacPasteboardMime *c = (*it);
307 QString flavor(c->flavorFor(mimeType));
308 if (!flavor.isEmpty()) {
309 QVariant mimeData = static_cast<QMacMimeData*>(mime_src)->variantData(mimeType);
311 //### Grrr, why didn't I put in a virtual int QMacPasteboardMime::count()? --Sam
312 const int numItems = c->convertFromMime(mimeType, mimeData, flavor).size();
314 int numItems = 1; //this is a hack but it is much faster than allowing conversion above
315 if (c->convertorName() == QLatin1String("FileURL"))
316 numItems = mime_src->urls().count();
318 for (int item = 0; item < numItems; ++item) {
319 const int itemID = item+1; //id starts at 1
320 promises.append(QMacPasteboard::Promise(itemID, c, mimeType, mimeData, item));
321 PasteboardPutItemFlavor(paste, (PasteboardItemID)itemID, QCFString(flavor), 0, kPasteboardFlavorNoFlags);
322 #ifdef DEBUG_PASTEBOARD
323 qDebug(" - adding %d %s [%s] <%s> [%d]",
324 itemID, qPrintable(mimeType), qPrintable(flavor), qPrintable(c->convertorName()), item);
334 QMacPasteboard::formats() const
337 return QStringList();
343 if (PasteboardGetItemCount(paste, &cnt) || !cnt)
346 #ifdef DEBUG_PASTEBOARD
347 qDebug("PasteBoard: Formats [%d]", (int)cnt);
349 for (uint index = 1; index <= cnt; ++index) {
352 if (PasteboardGetItemIdentifier(paste, index, &id) != noErr)
355 QCFType<CFArrayRef> types;
356 if (PasteboardCopyItemFlavors(paste, id, &types ) != noErr)
359 const int type_count = CFArrayGetCount(types);
360 for (int i = 0; i < type_count; ++i) {
361 const QString flavor = QCFString::toQString((CFStringRef)CFArrayGetValueAtIndex(types, i));
362 #ifdef DEBUG_PASTEBOARD
363 qDebug(" -%s", qPrintable(QString(flavor)));
365 QString mimeType = QMacPasteboardMime::flavorToMime(mime_type, flavor);
366 if (!mimeType.isEmpty() && !ret.contains(mimeType)) {
367 #ifdef DEBUG_PASTEBOARD
368 qDebug(" -<%d> %s [%s]", ret.size(), qPrintable(mimeType), qPrintable(QString(flavor)));
378 QMacPasteboard::hasFormat(const QString &format) const
386 if (PasteboardGetItemCount(paste, &cnt) || !cnt)
389 #ifdef DEBUG_PASTEBOARD
390 qDebug("PasteBoard: hasFormat [%s]", qPrintable(format));
392 for (uint index = 1; index <= cnt; ++index) {
395 if (PasteboardGetItemIdentifier(paste, index, &id) != noErr)
398 QCFType<CFArrayRef> types;
399 if (PasteboardCopyItemFlavors(paste, id, &types ) != noErr)
402 const int type_count = CFArrayGetCount(types);
403 for (int i = 0; i < type_count; ++i) {
404 const QString flavor = QCFString::toQString((CFStringRef)CFArrayGetValueAtIndex(types, i));
405 #ifdef DEBUG_PASTEBOARD
406 qDebug(" -%s [0x%x]", qPrintable(QString(flavor)), mime_type);
408 QString mimeType = QMacPasteboardMime::flavorToMime(mime_type, flavor);
409 #ifdef DEBUG_PASTEBOARD
410 if (!mimeType.isEmpty())
411 qDebug(" - %s", qPrintable(mimeType));
413 if (mimeType == format)
421 QMacPasteboard::retrieveData(const QString &format, QVariant::Type) const
429 if (PasteboardGetItemCount(paste, &cnt) || !cnt)
432 #ifdef DEBUG_PASTEBOARD
433 qDebug("Pasteboard: retrieveData [%s]", qPrintable(format));
435 const QList<QMacPasteboardMime *> mimes = QMacPasteboardMime::all(mime_type);
436 for (int mime = 0; mime < mimes.size(); ++mime) {
437 QMacPasteboardMime *c = mimes.at(mime);
438 QString c_flavor = c->flavorFor(format);
439 if (!c_flavor.isEmpty()) {
440 // Handle text/plain a little differently. Try handling Unicode first.
441 bool checkForUtf16 = (c_flavor == QLatin1String("com.apple.traditional-mac-plain-text")
442 || c_flavor == QLatin1String("public.utf8-plain-text"));
443 if (checkForUtf16 || c_flavor == QLatin1String("public.utf16-plain-text")) {
444 // Try to get the NSStringPboardType from NSPasteboard, newlines are mapped
445 // correctly (as '\n') in this data. The 'public.utf16-plain-text' type
446 // usually maps newlines to '\r' instead.
447 QString str = qt_mac_get_pasteboardString(paste);
451 if (checkForUtf16 && hasFlavor(QLatin1String("public.utf16-plain-text")))
452 c_flavor = QLatin1String("public.utf16-plain-text");
455 QList<QByteArray> retList;
456 for (uint index = 1; index <= cnt; ++index) {
458 if (PasteboardGetItemIdentifier(paste, index, &id) != noErr)
461 QCFType<CFArrayRef> types;
462 if (PasteboardCopyItemFlavors(paste, id, &types ) != noErr)
465 const int type_count = CFArrayGetCount(types);
466 for (int i = 0; i < type_count; ++i) {
467 CFStringRef flavor = static_cast<CFStringRef>(CFArrayGetValueAtIndex(types, i));
468 if (c_flavor == QCFString::toQString(flavor)) {
469 QCFType<CFDataRef> macBuffer;
470 if (PasteboardCopyItemFlavorData(paste, id, flavor, &macBuffer) == noErr) {
471 QByteArray buffer((const char *)CFDataGetBytePtr(macBuffer), CFDataGetLength(macBuffer));
472 if (!buffer.isEmpty()) {
473 #ifdef DEBUG_PASTEBOARD
474 qDebug(" - %s [%s] (%s)", qPrintable(format), qPrintable(QCFString::toQString(flavor)), qPrintable(c->convertorName()));
476 buffer.detach(); //detach since we release the macBuffer
477 retList.append(buffer);
478 break; //skip to next element
482 #ifdef DEBUG_PASTEBOARD
483 qDebug(" - NoMatch %s [%s] (%s)", qPrintable(c_flavor), qPrintable(QCFString::toQString(flavor)), qPrintable(c->convertorName()));
489 if (!retList.isEmpty()) {
490 ret = c->convertToMime(format, retList, c_flavor);
498 void QMacPasteboard::clear_helper()
501 PasteboardClear(paste);
506 QMacPasteboard::clear()
508 #ifdef DEBUG_PASTEBOARD
509 qDebug("PasteBoard: clear!");
515 QMacPasteboard::sync() const
519 const bool fromGlobal = PasteboardSynchronize(paste) & kPasteboardModified;
522 const_cast<QMacPasteboard *>(this)->setMimeData(0);
524 #ifdef DEBUG_PASTEBOARD
526 qDebug("Pasteboard: Synchronize!");
532 QString qt_mac_get_pasteboardString(PasteboardRef paste)
534 QCocoaAutoReleasePool pool;
535 NSPasteboard *pb = nil;
537 if (PasteboardCopyName(paste, &pbname) == noErr) {
538 pb = [NSPasteboard pasteboardWithName:const_cast<NSString *>(reinterpret_cast<const NSString *>(pbname))];
541 pb = [NSPasteboard generalPasteboard];
544 NSString *text = [pb stringForType:NSStringPboardType];
546 return QCFString::toQString(text);