Qt 4.8
qmimedata.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qmimedata.h"
43 
44 #include "private/qobject_p.h"
45 #include "qurl.h"
46 #include "qstringlist.h"
47 #include "qtextcodec.h"
48 
50 
52 {
55 };
56 
58 {
60 public:
61  void removeData(const QString &format);
62  void setData(const QString &format, const QVariant &data);
63  QVariant getData(const QString &format) const;
64 
65  QVariant retrieveTypedData(const QString &format, QVariant::Type type) const;
66 
68 };
69 
71 {
72  for (int i=0; i<dataList.size(); i++) {
73  if (dataList.at(i).format == format) {
74  dataList.removeAt(i);
75  return;
76  }
77  }
78 }
79 
81 {
82  // remove it first if the format is already here.
83  removeData(format);
84  QMimeDataStruct mimeData;
85  mimeData.format = format;
86  mimeData.data = data;
87  dataList += mimeData;
88 }
89 
90 
92 {
93  QVariant data;
94  for (int i=0; i<dataList.size(); i++) {
95  if (dataList.at(i).format == format) {
96  data = dataList.at(i).data;
97  break;
98  }
99  }
100  return data;
101 }
102 
104 {
105  Q_Q(const QMimeData);
106 
107  QVariant data = q->retrieveData(format, type);
108  if (data.type() == type || !data.isValid())
109  return data;
110 
111  // provide more conversion possiblities than just what QVariant provides
112 
113  // URLs can be lists as well...
114  if ((type == QVariant::Url && data.type() == QVariant::List)
115  || (type == QVariant::List && data.type() == QVariant::Url))
116  return data;
117 
118  // images and pixmaps are interchangeable
119  if ((type == QVariant::Pixmap && data.type() == QVariant::Image)
120  || (type == QVariant::Image && data.type() == QVariant::Pixmap))
121  return data;
122 
123  if (data.type() == QVariant::ByteArray) {
124  // see if we can convert to the requested type
125  switch(type) {
126 #ifndef QT_NO_TEXTCODEC
127  case QVariant::String: {
128  const QByteArray ba = data.toByteArray();
130  if (format == QLatin1String("text/html"))
131  codec = QTextCodec::codecForHtml(ba, codec);
132  return codec->toUnicode(ba);
133  }
134 #endif // QT_NO_TEXTCODEC
135  case QVariant::Color: {
136  QVariant newData = data;
137  newData.convert(QVariant::Color);
138  return newData;
139  }
140  case QVariant::List: {
141  if (format != QLatin1String("text/uri-list"))
142  break;
143  // fall through
144  }
145  case QVariant::Url: {
146  QByteArray ba = data.toByteArray();
147  // Qt 3.x will send text/uri-list with a trailing
148  // null-terminator (that is *not* sent for any other
149  // text/* mime-type), so chop it off
150  if (ba.endsWith('\0'))
151  ba.chop(1);
152 
153  QList<QByteArray> urls = ba.split('\n');
154  QList<QVariant> list;
155  for (int i = 0; i < urls.size(); ++i) {
156  QByteArray ba = urls.at(i).trimmed();
157  if (!ba.isEmpty())
158  list.append(QUrl::fromEncoded(ba));
159  }
160  return list;
161  }
162  default:
163  break;
164  }
165 
166  } else if (type == QVariant::ByteArray) {
167 
168  // try to convert to bytearray
169  switch(data.type()) {
170  case QVariant::ByteArray:
171  case QVariant::Color:
172  return data.toByteArray();
173  break;
174  case QVariant::String:
175  return data.toString().toUtf8();
176  break;
177  case QVariant::Url:
178  return data.toUrl().toEncoded();
179  break;
180  case QVariant::List: {
181  // has to be list of URLs
182  QByteArray result;
183  QList<QVariant> list = data.toList();
184  for (int i = 0; i < list.size(); ++i) {
185  if (list.at(i).type() == QVariant::Url) {
186  result += list.at(i).toUrl().toEncoded();
187  result += "\r\n";
188  }
189  }
190  if (!result.isEmpty())
191  return result;
192  break;
193  }
194  default:
195  break;
196  }
197  }
198  return data;
199 }
200 
292  : QObject(*new QMimeDataPrivate, 0)
293 {
294 }
295 
300 {
301 }
302 
311 {
312  Q_D(const QMimeData);
313  QVariant data = d->retrieveTypedData(QLatin1String("text/uri-list"), QVariant::List);
315  if (data.type() == QVariant::Url)
316  urls.append(data.toUrl());
317  else if (data.type() == QVariant::List) {
318  QList<QVariant> list = data.toList();
319  for (int i = 0; i < list.size(); ++i) {
320  if (list.at(i).type() == QVariant::Url)
321  urls.append(list.at(i).toUrl());
322  }
323  }
324  return urls;
325 }
326 
335 {
336  Q_D(QMimeData);
337  QList<QVariant> list;
338  for (int i = 0; i < urls.size(); ++i)
339  list.append(urls.at(i));
340 
341  d->setData(QLatin1String("text/uri-list"), list);
342 }
343 
352 bool QMimeData::hasUrls() const
353 {
354  return hasFormat(QLatin1String("text/uri-list"));
355 }
356 
357 
365 {
366  Q_D(const QMimeData);
367  QVariant data = d->retrieveTypedData(QLatin1String("text/plain"), QVariant::String);
368  return data.toString();
369 }
370 
378 {
379  Q_D(QMimeData);
380  d->setData(QLatin1String("text/plain"), text);
381 }
382 
389 bool QMimeData::hasText() const
390 {
391  return hasFormat(QLatin1String("text/plain"));
392 }
393 
401 {
402  Q_D(const QMimeData);
403  QVariant data = d->retrieveTypedData(QLatin1String("text/html"), QVariant::String);
404  return data.toString();
405 }
406 
414 {
415  Q_D(QMimeData);
416  d->setData(QLatin1String("text/html"), html);
417 }
418 
425 bool QMimeData::hasHtml() const
426 {
427  return hasFormat(QLatin1String("text/html"));
428 }
429 
443 {
444  Q_D(const QMimeData);
445  return d->retrieveTypedData(QLatin1String("application/x-qt-image"), QVariant::Image);
446 }
447 
460 {
461  Q_D(QMimeData);
462  d->setData(QLatin1String("application/x-qt-image"), image);
463 }
464 
472 {
473  return hasFormat(QLatin1String("application/x-qt-image"));
474 }
475 
490 {
491  Q_D(const QMimeData);
492  return d->retrieveTypedData(QLatin1String("application/x-color"), QVariant::Color);
493 }
494 
503 {
504  Q_D(QMimeData);
505  d->setData(QLatin1String("application/x-color"), color);
506 }
507 
508 
516 {
517  return hasFormat(QLatin1String("application/x-color"));
518 }
519 
524 QByteArray QMimeData::data(const QString &mimeType) const
525 {
526  Q_D(const QMimeData);
527  QVariant data = d->retrieveTypedData(mimeType, QVariant::ByteArray);
528  return data.toByteArray();
529 }
530 
547 void QMimeData::setData(const QString &mimeType, const QByteArray &data)
548 {
549  Q_D(QMimeData);
550  d->setData(mimeType, QVariant(data));
551 }
552 
563 bool QMimeData::hasFormat(const QString &mimeType) const
564 {
565  return formats().contains(mimeType);
566 }
567 
580 {
581  Q_D(const QMimeData);
582  QStringList list;
583  for (int i=0; i<d->dataList.size(); i++)
584  list += d->dataList.at(i).format;
585  return list;
586 }
587 
604 {
605  Q_UNUSED(type);
606  Q_D(const QMimeData);
607  return d->getData(mimeType);
608 }
609 
614 {
615  Q_D(QMimeData);
616  d->dataList.clear();
617 }
618 
627 void QMimeData::removeFormat(const QString &mimeType)
628 {
629  Q_D(QMimeData);
630  d->removeData(mimeType);
631 }
632 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
bool hasUrls() const
Returns true if the object can return a list of urls; otherwise returns false.
Definition: qmimedata.cpp:352
void chop(int n)
Removes n bytes from the end of the byte array.
void setData(const QString &mimetype, const QByteArray &data)
Sets the data associated with the MIME type given by mimeType to the specified data.
Definition: qmimedata.cpp:547
int type
Definition: qmetatype.cpp:239
void clear()
Removes all the MIME type and data entries in the object.
Definition: qmimedata.cpp:613
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual QStringList formats() const
Returns a list of formats supported by the object.
Definition: qmimedata.cpp:579
void removeData(const QString &format)
Definition: qmimedata.cpp:70
void setImageData(const QVariant &image)
Sets the data in the object to the given image.
Definition: qmimedata.cpp:459
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
QVariant colorData() const
Returns a color if the data stored in the object represents a color (MIME type application/x-color); ...
Definition: qmimedata.cpp:489
bool hasHtml() const
Returns true if the object can return HTML (MIME type text/html); otherwise returns false...
Definition: qmimedata.cpp:425
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void setColorData(const QVariant &color)
Sets the color data in the object to the given color.
Definition: qmimedata.cpp:502
void removeFormat(const QString &mimetype)
Removes the data entry for mimeType in the object.
Definition: qmimedata.cpp:627
static QTextCodec * codecForHtml(const QByteArray &ba)
Tries to detect the encoding of the provided snippet of HTML in the given byte array, ba, by checking the BOM (Byte Order Mark) and the content-type meta header and returns a QTextCodec instance that is capable of decoding the html to unicode.
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has type() List or StringList ; otherwise return...
Definition: qvariant.cpp:2751
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QByteArray data(const QString &mimetype) const
Returns the data stored in the object in the format described by the MIME type specified by mimeType...
Definition: qmimedata.cpp:524
QVariant data
Definition: qmimedata.cpp:54
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString format
Definition: qmimedata.cpp:53
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
QList< QMimeDataStruct > dataList
Definition: qmimedata.cpp:67
QVariant getData(const QString &format) const
Definition: qmimedata.cpp:91
#define Q_Q(Class)
Definition: qglobal.h:2483
QMimeData()
Constructs a new MIME data object with no data in it.
Definition: qmimedata.cpp:291
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
void * data()
Definition: qvariant.cpp:3077
QVariant retrieveTypedData(const QString &format, QVariant::Type type) const
Definition: qmimedata.cpp:103
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QString text() const
Returns a plain text (MIME type text/plain) representation of the data.
Definition: qmimedata.cpp:364
static bool setData(const QByteArray &data, STGMEDIUM *pmedium)
Definition: qmime_win.cpp:141
QByteArray trimmed() const
Returns a byte array that has whitespace removed from the start and the end.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QString html() const
Returns a string if the data stored in the object is HTML (MIME type text/html); otherwise returns an...
Definition: qmimedata.cpp:400
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setUrls(const QList< QUrl > &urls)
Sets the URLs stored in the MIME data object to those specified by urls.
Definition: qmimedata.cpp:334
static QTextCodec * codec(MYSQL *mysql)
Definition: qsql_mysql.cpp:220
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
QList< QUrl > urls() const
Returns a list of URLs contained within the MIME data object.
Definition: qmimedata.cpp:310
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
QUrl toUrl() const
Returns the variant as a QUrl if the variant has type() Url ; otherwise returns an invalid QUrl...
Definition: qvariant.cpp:2528
~QMimeData()
Destroys the MIME data object.
Definition: qmimedata.cpp:299
bool convert(Type t)
Casts the variant to the requested type, t.
Definition: qvariant.cpp:2959
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
QVariant imageData() const
Returns a QVariant storing a QImage if the object can return an image; otherwise returns a null varia...
Definition: qmimedata.cpp:442
bool hasImage() const
Returns true if the object can return an image; otherwise returns false.
Definition: qmimedata.cpp:471
QString toUnicode(const QByteArray &) const
Converts a from the encoding of this codec to Unicode, and returns the result in a QString...
virtual bool hasFormat(const QString &mimetype) const
Returns true if the object can return data for the MIME type specified by mimeType; otherwise returns...
Definition: qmimedata.cpp:563
QList< QByteArray > split(char sep) const
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays...
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
virtual QVariant retrieveData(const QString &mimetype, QVariant::Type preferredType) const
Returns a variant with the given type containing data for the MIME type specified by mimeType...
Definition: qmimedata.cpp:603
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
void setHtml(const QString &html)
Sets html as the HTML (MIME type text/html) used to represent the data.
Definition: qmimedata.cpp:413
bool hasText() const
Returns true if the object can return plain text (MIME type text/plain); otherwise returns false...
Definition: qmimedata.cpp:389
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
bool hasColor() const
Returns true if the object can return a color (MIME type application/x-color); otherwise returns fals...
Definition: qmimedata.cpp:515
void setText(const QString &text)
Sets text as the plain text (MIME type text/plain) used to represent the data.
Definition: qmimedata.cpp:377
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
static QUrl fromEncoded(const QByteArray &url)
Parses input and returns the corresponding QUrl.
Definition: qurl.cpp:5964
void setData(const QString &format, const QVariant &data)
Definition: qmimedata.cpp:80
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static QByteArray getData(int cf, IDataObject *pDataObj)
Definition: qmime_win.cpp:156
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...