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