| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qclipboard.h"
|
|---|
| 43 | #include "qapplication.h"
|
|---|
| 44 | #include "qbitmap.h"
|
|---|
| 45 | #include "qdatetime.h"
|
|---|
| 46 | #include "qdebug.h"
|
|---|
| 47 | #include "qapplication_p.h"
|
|---|
| 48 | #include <private/qt_mac_p.h>
|
|---|
| 49 | #include "qevent.h"
|
|---|
| 50 | #include "qurl.h"
|
|---|
| 51 | #include <stdlib.h>
|
|---|
| 52 | #include <string.h>
|
|---|
| 53 |
|
|---|
| 54 | QT_BEGIN_NAMESPACE
|
|---|
| 55 |
|
|---|
| 56 | QT_USE_NAMESPACE
|
|---|
| 57 |
|
|---|
| 58 | /*****************************************************************************
|
|---|
| 59 | QClipboard debug facilities
|
|---|
| 60 | *****************************************************************************/
|
|---|
| 61 | //#define DEBUG_PASTEBOARD
|
|---|
| 62 |
|
|---|
| 63 | #ifndef QT_NO_CLIPBOARD
|
|---|
| 64 |
|
|---|
| 65 | /*****************************************************************************
|
|---|
| 66 | QClipboard member functions for mac.
|
|---|
| 67 | *****************************************************************************/
|
|---|
| 68 |
|
|---|
| 69 | static QMacPasteboard *qt_mac_pasteboards[2] = {0, 0};
|
|---|
| 70 |
|
|---|
| 71 | static inline QMacPasteboard *qt_mac_pasteboard(QClipboard::Mode mode)
|
|---|
| 72 | {
|
|---|
| 73 | Q_ASSERT(mode == QClipboard::Clipboard || mode == QClipboard::FindBuffer);
|
|---|
| 74 | if (mode == QClipboard::Clipboard)
|
|---|
| 75 | return qt_mac_pasteboards[0];
|
|---|
| 76 | else
|
|---|
| 77 | return qt_mac_pasteboards[1];
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | static void qt_mac_cleanupPasteboard() {
|
|---|
| 81 | delete qt_mac_pasteboards[0];
|
|---|
| 82 | delete qt_mac_pasteboards[1];
|
|---|
| 83 | qt_mac_pasteboards[0] = 0;
|
|---|
| 84 | qt_mac_pasteboards[1] = 0;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | static bool qt_mac_updateScrap(QClipboard::Mode mode)
|
|---|
| 88 | {
|
|---|
| 89 | if(!qt_mac_pasteboards[0]) {
|
|---|
| 90 | qt_mac_pasteboards[0] = new QMacPasteboard(kPasteboardClipboard, QMacPasteboardMime::MIME_CLIP);
|
|---|
| 91 | qt_mac_pasteboards[1] = new QMacPasteboard(kPasteboardFind, QMacPasteboardMime::MIME_CLIP);
|
|---|
| 92 | qAddPostRoutine(qt_mac_cleanupPasteboard);
|
|---|
| 93 | return true;
|
|---|
| 94 | }
|
|---|
| 95 | return qt_mac_pasteboard(mode)->sync();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | void QClipboard::clear(Mode mode)
|
|---|
| 99 | {
|
|---|
| 100 | if (!supportsMode(mode))
|
|---|
| 101 | return;
|
|---|
| 102 | qt_mac_updateScrap(mode);
|
|---|
| 103 | qt_mac_pasteboard(mode)->clear();
|
|---|
| 104 | setMimeData(0, mode);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | void QClipboard::ownerDestroyed()
|
|---|
| 108 | {
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | void QClipboard::connectNotify(const char *signal)
|
|---|
| 113 | {
|
|---|
| 114 | Q_UNUSED(signal);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | bool QClipboard::event(QEvent *e)
|
|---|
| 118 | {
|
|---|
| 119 | if(e->type() != QEvent::Clipboard)
|
|---|
| 120 | return QObject::event(e);
|
|---|
| 121 |
|
|---|
| 122 | if (qt_mac_updateScrap(QClipboard::Clipboard)) {
|
|---|
| 123 | emitChanged(QClipboard::Clipboard);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | if (qt_mac_updateScrap(QClipboard::FindBuffer)) {
|
|---|
| 127 | emitChanged(QClipboard::FindBuffer);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | return QObject::event(e);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | const QMimeData *QClipboard::mimeData(Mode mode) const
|
|---|
| 134 | {
|
|---|
| 135 | if (!supportsMode(mode))
|
|---|
| 136 | return 0;
|
|---|
| 137 | qt_mac_updateScrap(mode);
|
|---|
| 138 | return qt_mac_pasteboard(mode)->mimeData();
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | void QClipboard::setMimeData(QMimeData *src, Mode mode)
|
|---|
| 142 | {
|
|---|
| 143 | if (!supportsMode(mode))
|
|---|
| 144 | return;
|
|---|
| 145 | qt_mac_updateScrap(mode);
|
|---|
| 146 | qt_mac_pasteboard(mode)->setMimeData(src);
|
|---|
| 147 | emitChanged(mode);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | bool QClipboard::supportsMode(Mode mode) const
|
|---|
| 151 | {
|
|---|
| 152 | return (mode == Clipboard || mode == FindBuffer);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | bool QClipboard::ownsMode(Mode mode) const
|
|---|
| 156 | {
|
|---|
| 157 | Q_UNUSED(mode);
|
|---|
| 158 | return false;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | #endif // QT_NO_CLIPBOARD
|
|---|
| 162 |
|
|---|
| 163 | /*****************************************************************************
|
|---|
| 164 | QMacPasteboard code
|
|---|
| 165 | *****************************************************************************/
|
|---|
| 166 |
|
|---|
| 167 | QMacPasteboard::QMacPasteboard(PasteboardRef p, uchar mt)
|
|---|
| 168 | {
|
|---|
| 169 | mac_mime_source = false;
|
|---|
| 170 | mime_type = mt ? mt : uchar(QMacPasteboardMime::MIME_ALL);
|
|---|
| 171 | paste = p;
|
|---|
| 172 | CFRetain(paste);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | QMacPasteboard::QMacPasteboard(uchar mt)
|
|---|
| 176 | {
|
|---|
| 177 | mac_mime_source = false;
|
|---|
| 178 | mime_type = mt ? mt : uchar(QMacPasteboardMime::MIME_ALL);
|
|---|
| 179 | paste = 0;
|
|---|
| 180 | OSStatus err = PasteboardCreate(0, &paste);
|
|---|
| 181 | if(err == noErr) {
|
|---|
| 182 | PasteboardSetPromiseKeeper(paste, promiseKeeper, this);
|
|---|
| 183 | } else {
|
|---|
| 184 | qDebug("PasteBoard: Error creating pasteboard: [%d]", (int)err);
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | QMacPasteboard::QMacPasteboard(CFStringRef name, uchar mt)
|
|---|
| 189 | {
|
|---|
| 190 | mac_mime_source = false;
|
|---|
| 191 | mime_type = mt ? mt : uchar(QMacPasteboardMime::MIME_ALL);
|
|---|
| 192 | paste = 0;
|
|---|
| 193 | OSStatus err = PasteboardCreate(name, &paste);
|
|---|
| 194 | if(err == noErr) {
|
|---|
| 195 | PasteboardSetPromiseKeeper(paste, promiseKeeper, this);
|
|---|
| 196 | } else {
|
|---|
| 197 | qDebug("PasteBoard: Error creating pasteboard: %s [%d]", QCFString::toQString(name).toLatin1().constData(), (int)err);
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | QMacPasteboard::~QMacPasteboard()
|
|---|
| 202 | {
|
|---|
| 203 | // commit all promises for paste after exit close
|
|---|
| 204 | for (int i = 0; i < promises.count(); ++i) {
|
|---|
| 205 | const Promise &promise = promises.at(i);
|
|---|
| 206 | QCFString flavor = QCFString(promise.convertor->flavorFor(promise.mime));
|
|---|
| 207 | promiseKeeper(paste, (PasteboardItemID)promise.itemId, flavor, this);
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | if(paste)
|
|---|
| 211 | CFRelease(paste);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | PasteboardRef
|
|---|
| 215 | QMacPasteboard::pasteBoard() const
|
|---|
| 216 | {
|
|---|
| 217 | return paste;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | OSStatus QMacPasteboard::promiseKeeper(PasteboardRef paste, PasteboardItemID id, CFStringRef flavor, void *_qpaste)
|
|---|
| 221 | {
|
|---|
| 222 | QMacPasteboard *qpaste = (QMacPasteboard*)_qpaste;
|
|---|
| 223 | const long promise_id = (long)id;
|
|---|
| 224 |
|
|---|
| 225 | // Find the kept promise
|
|---|
| 226 | const QString flavorAsQString = QCFString::toQString(flavor);
|
|---|
| 227 | QMacPasteboard::Promise promise;
|
|---|
| 228 | for (int i = 0; i < qpaste->promises.size(); i++){
|
|---|
| 229 | QMacPasteboard::Promise tmp = qpaste->promises[i];
|
|---|
| 230 | if (tmp.itemId == promise_id && tmp.convertor->canConvert(tmp.mime, flavorAsQString)){
|
|---|
| 231 | promise = tmp;
|
|---|
| 232 | break;
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | if (!promise.itemId && flavorAsQString == QLatin1String("com.trolltech.qt.MimeTypeName")) {
|
|---|
| 237 | // we have promised this data, but wont be able to convert, so return null data.
|
|---|
| 238 | // This helps in making the application/x-qt-mime-type-name hidden from normal use.
|
|---|
| 239 | QByteArray ba;
|
|---|
| 240 | QCFType<CFDataRef> data = CFDataCreate(0, (UInt8*)ba.constData(), ba.size());
|
|---|
| 241 | PasteboardPutItemFlavor(paste, id, flavor, data, kPasteboardFlavorNoFlags);
|
|---|
| 242 | return noErr;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | if (!promise.itemId) {
|
|---|
| 246 | // There was no promise that could deliver data for the
|
|---|
| 247 | // given id and flavor. This should not happend.
|
|---|
| 248 | qDebug("Pasteboard: %d: Request for %ld, %s, but no promise found!", __LINE__, promise_id, qPrintable(flavorAsQString));
|
|---|
| 249 | return cantGetFlavorErr;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 253 | qDebug("PasteBoard: Calling in promise for %s[%ld] [%s] (%s) [%d]", qPrintable(promise.mime), promise_id,
|
|---|
| 254 | qPrintable(flavorAsQString), qPrintable(promise.convertor->convertorName()), promise.offset);
|
|---|
| 255 | #endif
|
|---|
| 256 |
|
|---|
| 257 | QList<QByteArray> md = promise.convertor->convertFromMime(promise.mime, promise.data, flavorAsQString);
|
|---|
| 258 | if (md.size() <= promise.offset)
|
|---|
| 259 | return cantGetFlavorErr;
|
|---|
| 260 | const QByteArray &ba = md[promise.offset];
|
|---|
| 261 | QCFType<CFDataRef> data = CFDataCreate(0, (UInt8*)ba.constData(), ba.size());
|
|---|
| 262 | PasteboardPutItemFlavor(paste, id, flavor, data, kPasteboardFlavorNoFlags);
|
|---|
| 263 | return noErr;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | bool
|
|---|
| 267 | QMacPasteboard::hasOSType(int c_flavor) const
|
|---|
| 268 | {
|
|---|
| 269 | if (!paste)
|
|---|
| 270 | return false;
|
|---|
| 271 |
|
|---|
| 272 | sync();
|
|---|
| 273 |
|
|---|
| 274 | ItemCount cnt = 0;
|
|---|
| 275 | if(PasteboardGetItemCount(paste, &cnt) || !cnt)
|
|---|
| 276 | return false;
|
|---|
| 277 |
|
|---|
| 278 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 279 | qDebug("PasteBoard: hasOSType [%c%c%c%c]", (c_flavor>>24)&0xFF, (c_flavor>>16)&0xFF,
|
|---|
| 280 | (c_flavor>>8)&0xFF, (c_flavor>>0)&0xFF);
|
|---|
| 281 | #endif
|
|---|
| 282 | for(uint index = 1; index <= cnt; ++index) {
|
|---|
| 283 |
|
|---|
| 284 | PasteboardItemID id;
|
|---|
| 285 | if(PasteboardGetItemIdentifier(paste, index, &id) != noErr)
|
|---|
| 286 | return false;
|
|---|
| 287 |
|
|---|
| 288 | QCFType<CFArrayRef> types;
|
|---|
| 289 | if(PasteboardCopyItemFlavors(paste, id, &types ) != noErr)
|
|---|
| 290 | return false;
|
|---|
| 291 |
|
|---|
| 292 | const int type_count = CFArrayGetCount(types);
|
|---|
| 293 | for(int i = 0; i < type_count; ++i) {
|
|---|
| 294 | CFStringRef flavor = (CFStringRef)CFArrayGetValueAtIndex(types, i);
|
|---|
| 295 | const int os_flavor = UTGetOSTypeFromString(UTTypeCopyPreferredTagWithClass(flavor, kUTTagClassOSType));
|
|---|
| 296 | if(os_flavor == c_flavor) {
|
|---|
| 297 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 298 | qDebug(" - Found!");
|
|---|
| 299 | #endif
|
|---|
| 300 | return true;
|
|---|
| 301 | }
|
|---|
| 302 | }
|
|---|
| 303 | }
|
|---|
| 304 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 305 | qDebug(" - NotFound!");
|
|---|
| 306 | #endif
|
|---|
| 307 | return false;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | bool
|
|---|
| 311 | QMacPasteboard::hasFlavor(QString c_flavor) const
|
|---|
| 312 | {
|
|---|
| 313 | if (!paste)
|
|---|
| 314 | return false;
|
|---|
| 315 |
|
|---|
| 316 | sync();
|
|---|
| 317 |
|
|---|
| 318 | ItemCount cnt = 0;
|
|---|
| 319 | if(PasteboardGetItemCount(paste, &cnt) || !cnt)
|
|---|
| 320 | return false;
|
|---|
| 321 |
|
|---|
| 322 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 323 | qDebug("PasteBoard: hasFlavor [%s]", qPrintable(c_flavor));
|
|---|
| 324 | #endif
|
|---|
| 325 | for(uint index = 1; index <= cnt; ++index) {
|
|---|
| 326 |
|
|---|
| 327 | PasteboardItemID id;
|
|---|
| 328 | if(PasteboardGetItemIdentifier(paste, index, &id) != noErr)
|
|---|
| 329 | return false;
|
|---|
| 330 |
|
|---|
| 331 | PasteboardFlavorFlags flags;
|
|---|
| 332 | if(PasteboardGetItemFlavorFlags(paste, id, QCFString(c_flavor), &flags) == noErr) {
|
|---|
| 333 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 334 | qDebug(" - Found!");
|
|---|
| 335 | #endif
|
|---|
| 336 | return true;
|
|---|
| 337 | }
|
|---|
| 338 | }
|
|---|
| 339 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 340 | qDebug(" - NotFound!");
|
|---|
| 341 | #endif
|
|---|
| 342 | return false;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | class QMacPasteboardMimeSource : public QMimeData {
|
|---|
| 346 | const QMacPasteboard *paste;
|
|---|
| 347 | public:
|
|---|
| 348 | QMacPasteboardMimeSource(const QMacPasteboard *p) : QMimeData(), paste(p) { }
|
|---|
| 349 | ~QMacPasteboardMimeSource() { }
|
|---|
| 350 | virtual QStringList formats() const { return paste->formats(); }
|
|---|
| 351 | virtual QVariant retrieveData(const QString &format, QVariant::Type type) const { return paste->retrieveData(format, type); }
|
|---|
| 352 | };
|
|---|
| 353 |
|
|---|
| 354 | QMimeData
|
|---|
| 355 | *QMacPasteboard::mimeData() const
|
|---|
| 356 | {
|
|---|
| 357 | if(!mime) {
|
|---|
| 358 | mac_mime_source = true;
|
|---|
| 359 | mime = new QMacPasteboardMimeSource(this);
|
|---|
| 360 |
|
|---|
| 361 | }
|
|---|
| 362 | return mime;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | class QMacMimeData : public QMimeData
|
|---|
| 366 | {
|
|---|
| 367 | public:
|
|---|
| 368 | QVariant variantData(const QString &mime) { return retrieveData(mime, QVariant::Invalid); }
|
|---|
| 369 | private:
|
|---|
| 370 | QMacMimeData();
|
|---|
| 371 | };
|
|---|
| 372 |
|
|---|
| 373 | void
|
|---|
| 374 | QMacPasteboard::setMimeData(QMimeData *mime_src)
|
|---|
| 375 | {
|
|---|
| 376 | if (!paste)
|
|---|
| 377 | return;
|
|---|
| 378 |
|
|---|
| 379 | if (mime == mime_src || (!mime_src && mime && mac_mime_source))
|
|---|
| 380 | return;
|
|---|
| 381 | mac_mime_source = false;
|
|---|
| 382 | delete mime;
|
|---|
| 383 | mime = mime_src;
|
|---|
| 384 |
|
|---|
| 385 | QList<QMacPasteboardMime*> availableConverters = QMacPasteboardMime::all(mime_type);
|
|---|
| 386 | if (mime != 0) {
|
|---|
| 387 | clear_helper();
|
|---|
| 388 | QStringList formats = mime_src->formats();
|
|---|
| 389 |
|
|---|
| 390 | for(int f = 0; f < formats.size(); ++f) {
|
|---|
| 391 | QString mimeType = formats.at(f);
|
|---|
| 392 | for (QList<QMacPasteboardMime *>::Iterator it = availableConverters.begin(); it != availableConverters.end(); ++it) {
|
|---|
| 393 | QMacPasteboardMime *c = (*it);
|
|---|
| 394 | QString flavor(c->flavorFor(mimeType));
|
|---|
| 395 | if(!flavor.isEmpty()) {
|
|---|
| 396 | QVariant mimeData = static_cast<QMacMimeData*>(mime_src)->variantData(mimeType);
|
|---|
| 397 | #if 0
|
|---|
| 398 | //### Grrr, why didn't I put in a virtual int QMacPasteboardMime::count()? --Sam
|
|---|
| 399 | const int numItems = c->convertFromMime(mimeType, mimeData, flavor).size();
|
|---|
| 400 | #else
|
|---|
| 401 | int numItems = 1; //this is a hack but it is much faster than allowing conversion above
|
|---|
| 402 | if(c->convertorName() == QLatin1String("FileURL"))
|
|---|
| 403 | numItems = mime_src->urls().count();
|
|---|
| 404 | #endif
|
|---|
| 405 | for(int item = 0; item < numItems; ++item) {
|
|---|
| 406 | const int itemID = item+1; //id starts at 1
|
|---|
| 407 | promises.append(QMacPasteboard::Promise(itemID, c, mimeType, mimeData, item));
|
|---|
| 408 | PasteboardPutItemFlavor(paste, (PasteboardItemID)itemID, QCFString(flavor), 0, kPasteboardFlavorNoFlags);
|
|---|
| 409 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 410 | qDebug(" - adding %d %s [%s] <%s> [%d]",
|
|---|
| 411 | itemID, qPrintable(mimeType), qPrintable(flavor), qPrintable(c->convertorName()), item);
|
|---|
| 412 | #endif
|
|---|
| 413 | }
|
|---|
| 414 | }
|
|---|
| 415 | }
|
|---|
| 416 | }
|
|---|
| 417 | }
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | QStringList
|
|---|
| 421 | QMacPasteboard::formats() const
|
|---|
| 422 | {
|
|---|
| 423 | if (!paste)
|
|---|
| 424 | return QStringList();
|
|---|
| 425 |
|
|---|
| 426 | sync();
|
|---|
| 427 |
|
|---|
| 428 | QStringList ret;
|
|---|
| 429 | ItemCount cnt = 0;
|
|---|
| 430 | if(PasteboardGetItemCount(paste, &cnt) || !cnt)
|
|---|
| 431 | return ret;
|
|---|
| 432 |
|
|---|
| 433 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 434 | qDebug("PasteBoard: Formats [%d]", (int)cnt);
|
|---|
| 435 | #endif
|
|---|
| 436 | for(uint index = 1; index <= cnt; ++index) {
|
|---|
| 437 |
|
|---|
| 438 | PasteboardItemID id;
|
|---|
| 439 | if(PasteboardGetItemIdentifier(paste, index, &id) != noErr)
|
|---|
| 440 | continue;
|
|---|
| 441 |
|
|---|
| 442 | QCFType<CFArrayRef> types;
|
|---|
| 443 | if(PasteboardCopyItemFlavors(paste, id, &types ) != noErr)
|
|---|
| 444 | continue;
|
|---|
| 445 |
|
|---|
| 446 | const int type_count = CFArrayGetCount(types);
|
|---|
| 447 | for(int i = 0; i < type_count; ++i) {
|
|---|
| 448 | const QString flavor = QCFString::toQString((CFStringRef)CFArrayGetValueAtIndex(types, i));
|
|---|
| 449 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 450 | qDebug(" -%s", qPrintable(QString(flavor)));
|
|---|
| 451 | #endif
|
|---|
| 452 | QString mimeType = QMacPasteboardMime::flavorToMime(mime_type, flavor);
|
|---|
| 453 | if(!mimeType.isEmpty() && !ret.contains(mimeType)) {
|
|---|
| 454 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 455 | qDebug(" -<%d> %s [%s]", ret.size(), qPrintable(mimeType), qPrintable(QString(flavor)));
|
|---|
| 456 | #endif
|
|---|
| 457 | ret << mimeType;
|
|---|
| 458 | }
|
|---|
| 459 | }
|
|---|
| 460 | }
|
|---|
| 461 | return ret;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | bool
|
|---|
| 465 | QMacPasteboard::hasFormat(const QString &format) const
|
|---|
| 466 | {
|
|---|
| 467 | if (!paste)
|
|---|
| 468 | return false;
|
|---|
| 469 |
|
|---|
| 470 | sync();
|
|---|
| 471 |
|
|---|
| 472 | ItemCount cnt = 0;
|
|---|
| 473 | if(PasteboardGetItemCount(paste, &cnt) || !cnt)
|
|---|
| 474 | return false;
|
|---|
| 475 |
|
|---|
| 476 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 477 | qDebug("PasteBoard: hasFormat [%s]", qPrintable(format));
|
|---|
| 478 | #endif
|
|---|
| 479 | for(uint index = 1; index <= cnt; ++index) {
|
|---|
| 480 |
|
|---|
| 481 | PasteboardItemID id;
|
|---|
| 482 | if(PasteboardGetItemIdentifier(paste, index, &id) != noErr)
|
|---|
| 483 | continue;
|
|---|
| 484 |
|
|---|
| 485 | QCFType<CFArrayRef> types;
|
|---|
| 486 | if(PasteboardCopyItemFlavors(paste, id, &types ) != noErr)
|
|---|
| 487 | continue;
|
|---|
| 488 |
|
|---|
| 489 | const int type_count = CFArrayGetCount(types);
|
|---|
| 490 | for(int i = 0; i < type_count; ++i) {
|
|---|
| 491 | const QString flavor = QCFString::toQString((CFStringRef)CFArrayGetValueAtIndex(types, i));
|
|---|
| 492 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 493 | qDebug(" -%s [0x%x]", qPrintable(QString(flavor)), mime_type);
|
|---|
| 494 | #endif
|
|---|
| 495 | QString mimeType = QMacPasteboardMime::flavorToMime(mime_type, flavor);
|
|---|
| 496 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 497 | if(!mimeType.isEmpty())
|
|---|
| 498 | qDebug(" - %s", qPrintable(mimeType));
|
|---|
| 499 | #endif
|
|---|
| 500 | if(mimeType == format)
|
|---|
| 501 | return true;
|
|---|
| 502 | }
|
|---|
| 503 | }
|
|---|
| 504 | return false;
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | QVariant
|
|---|
| 508 | QMacPasteboard::retrieveData(const QString &format, QVariant::Type) const
|
|---|
| 509 | {
|
|---|
| 510 | if (!paste)
|
|---|
| 511 | return QVariant();
|
|---|
| 512 |
|
|---|
| 513 | sync();
|
|---|
| 514 |
|
|---|
| 515 | ItemCount cnt = 0;
|
|---|
| 516 | if(PasteboardGetItemCount(paste, &cnt) || !cnt)
|
|---|
| 517 | return QByteArray();
|
|---|
| 518 |
|
|---|
| 519 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 520 | qDebug("Pasteboard: retrieveData [%s]", qPrintable(format));
|
|---|
| 521 | #endif
|
|---|
| 522 | const QList<QMacPasteboardMime *> mimes = QMacPasteboardMime::all(mime_type);
|
|---|
| 523 | for(int mime = 0; mime < mimes.size(); ++mime) {
|
|---|
| 524 | QMacPasteboardMime *c = mimes.at(mime);
|
|---|
| 525 | QString c_flavor = c->flavorFor(format);
|
|---|
| 526 | if(!c_flavor.isEmpty()) {
|
|---|
| 527 | // Handle text/plain a little differently. Try handling Unicode first.
|
|---|
| 528 | if((c_flavor == QLatin1String("com.apple.traditional-mac-plain-text") || c_flavor == QLatin1String("public.utf8-plain-text")) &&
|
|---|
| 529 | hasFlavor(QLatin1String("public.utf16-plain-text")))
|
|---|
| 530 | c_flavor = QLatin1String("public.utf16-plain-text");
|
|---|
| 531 |
|
|---|
| 532 | QVariant ret;
|
|---|
| 533 | QList<QByteArray> retList;
|
|---|
| 534 | for(uint index = 1; index <= cnt; ++index) {
|
|---|
| 535 | PasteboardItemID id;
|
|---|
| 536 | if(PasteboardGetItemIdentifier(paste, index, &id) != noErr)
|
|---|
| 537 | continue;
|
|---|
| 538 |
|
|---|
| 539 | QCFType<CFArrayRef> types;
|
|---|
| 540 | if(PasteboardCopyItemFlavors(paste, id, &types ) != noErr)
|
|---|
| 541 | continue;
|
|---|
| 542 |
|
|---|
| 543 | const int type_count = CFArrayGetCount(types);
|
|---|
| 544 | for(int i = 0; i < type_count; ++i) {
|
|---|
| 545 | CFStringRef flavor = static_cast<CFStringRef>(CFArrayGetValueAtIndex(types, i));
|
|---|
| 546 | if(c_flavor == QCFString::toQString(flavor)) {
|
|---|
| 547 | QCFType<CFDataRef> macBuffer;
|
|---|
| 548 | if(PasteboardCopyItemFlavorData(paste, id, flavor, &macBuffer) == noErr) {
|
|---|
| 549 | QByteArray buffer((const char *)CFDataGetBytePtr(macBuffer), CFDataGetLength(macBuffer));
|
|---|
| 550 | if(!buffer.isEmpty()) {
|
|---|
| 551 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 552 | qDebug(" - %s [%s] (%s)", qPrintable(format), qPrintable(QCFString::toQString(flavor)), qPrintable(c->convertorName()));
|
|---|
| 553 | #endif
|
|---|
| 554 | buffer.detach(); //detach since we release the macBuffer
|
|---|
| 555 | retList.append(buffer);
|
|---|
| 556 | break; //skip to next element
|
|---|
| 557 | }
|
|---|
| 558 | }
|
|---|
| 559 | } else {
|
|---|
| 560 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 561 | qDebug(" - NoMatch %s [%s] (%s)", qPrintable(c_flavor), qPrintable(QCFString::toQString(flavor)), qPrintable(c->convertorName()));
|
|---|
| 562 | #endif
|
|---|
| 563 | }
|
|---|
| 564 | }
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | if (!retList.isEmpty()) {
|
|---|
| 568 | ret = c->convertToMime(format, retList, c_flavor);
|
|---|
| 569 | return ret;
|
|---|
| 570 | }
|
|---|
| 571 | }
|
|---|
| 572 | }
|
|---|
| 573 | return QVariant();
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | void QMacPasteboard::clear_helper()
|
|---|
| 577 | {
|
|---|
| 578 | if (paste)
|
|---|
| 579 | PasteboardClear(paste);
|
|---|
| 580 | promises.clear();
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | void
|
|---|
| 584 | QMacPasteboard::clear()
|
|---|
| 585 | {
|
|---|
| 586 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 587 | qDebug("PasteBoard: clear!");
|
|---|
| 588 | #endif
|
|---|
| 589 | clear_helper();
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | bool
|
|---|
| 593 | QMacPasteboard::sync() const
|
|---|
| 594 | {
|
|---|
| 595 | if (!paste)
|
|---|
| 596 | return false;
|
|---|
| 597 | const bool fromGlobal = PasteboardSynchronize(paste) & kPasteboardModified;
|
|---|
| 598 |
|
|---|
| 599 | if (fromGlobal)
|
|---|
| 600 | const_cast<QMacPasteboard *>(this)->setMimeData(0);
|
|---|
| 601 |
|
|---|
| 602 | #ifdef DEBUG_PASTEBOARD
|
|---|
| 603 | if(fromGlobal)
|
|---|
| 604 | qDebug("Pasteboard: Syncronize!");
|
|---|
| 605 | #endif
|
|---|
| 606 | return fromGlobal;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 | QT_END_NAMESPACE
|
|---|