Qt 4.8
qbbclipboard.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 - 2012 Research In Motion <blackberry-qt@qnx.com>
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 // #define QBBCLIPBOARD_DEBUG
43 
44 #ifndef QT_NO_CLIPBOARD
45 
46 #include "qbbclipboard.h"
47 
48 #include <QDebug>
49 #include <QMimeData>
50 #include <QStringList>
51 
52 #include <clipboard/clipboard.h>
53 #include <errno.h>
54 
56 
57 // null terminated array
58 static const char *typeList[] = {"text/html", "text/plain", "image/png", "image/jpeg", "application/x-color", 0};
59 
60 static QByteArray readClipboardBuff(const char *type)
61 {
62  char *pbuffer;
63  if (is_clipboard_format_present(type) == 0) {
64  int size = get_clipboard_data(type, &pbuffer);
65  if (size != -1 && pbuffer) {
66  const QByteArray result = QByteArray(pbuffer, size);
67  free(pbuffer);
68  return result;
69  }
70  }
71 
72  return QByteArray();
73 }
74 
76 {
77  Q_OBJECT
78 public:
80  : QMimeData(),
81  m_clipboard(clipboard),
83  {
84  Q_ASSERT(clipboard);
85 
86  for (int i = 0; typeList[i] != 0; ++i) {
88  }
89  }
90 
92  {
93  delete m_userMimeData;
94  }
95 
98 
99 #if defined(QBBCLIPBOARD_DEBUG)
100  qDebug() << Q_FUNC_INFO << "formats=" << m_formatsToCheck;
101 #endif
102  }
103 
104  bool hasFormat(const QString &mimetype) const
105  {
106  const bool result = is_clipboard_format_present(mimetype.toUtf8().constData()) == 0;
107 #if defined(QBBCLIPBOARD_DEBUG)
108  qDebug() << Q_FUNC_INFO << "mimetype=" << mimetype << "result=" << result;
109 #endif
110  return result;
111  }
112 
114  {
115  QStringList result;
116 
118  if (is_clipboard_format_present(format.toUtf8().constData()) == 0)
119  result << format;
120  }
121 
122 #if defined(QBBCLIPBOARD_DEBUG)
123  qDebug() << Q_FUNC_INFO << "result=" << result;
124 #endif
125  return result;
126  }
127 
129  {
130  delete m_userMimeData;
132 
133  // system clipboard API doesn't allow detection of changes by other applications
134  // simulate an owner change through delayed invocation
135  // basically transfer ownership of data to the system clipboard once event processing resumes
136  if (m_userMimeData)
137  QMetaObject::invokeMethod(this, "releaseOwnership", Qt::QueuedConnection);
138  }
139 
141  {
142  return m_userMimeData;
143  }
144 
145 protected:
146  QVariant retrieveData(const QString &mimetype, QVariant::Type preferredType) const
147  {
148 #if defined(QBBCLIPBOARD_DEBUG)
149  qDebug() << Q_FUNC_INFO << "mimetype=" << mimetype << "preferredType=" << preferredType;
150 #endif
151  if (is_clipboard_format_present(mimetype.toUtf8().constData()) != 0)
152  return QMimeData::retrieveData(mimetype, preferredType);
153 
154  const QByteArray data = readClipboardBuff(mimetype.toUtf8().constData());
155  return qVariantFromValue(data);
156  }
157 
158 private Q_SLOTS:
160  {
161  if (m_userMimeData) {
162 #if defined(QBBCLIPBOARD_DEBUG)
163  qDebug() << Q_FUNC_INFO << "user data formats=" << m_userMimeData->formats() << "system formats=" << formats();
164 #endif
165  delete m_userMimeData;
166  m_userMimeData = 0;
168  }
169  }
170 
171 private:
173 
176 };
177 
179  : m_mimeData(new MimeData(this))
180 {
181 }
182 
184 {
185  delete m_mimeData;
186 }
187 
189 {
190  if (mode != QClipboard::Clipboard)
191  return;
192 
193  if (m_mimeData == data)
194  return;
195 
197  return;
198 
199  empty_clipboard();
200 
201  m_mimeData->clear();
203 
204  if (data == 0)
205  return;
206 
207  const QStringList formats = data->formats();
208 #if defined(QBBCLIPBOARD_DEBUG)
209  qDebug() << Q_FUNC_INFO << "formats=" << formats;
210 #endif
211 
212  Q_FOREACH (const QString &format, formats) {
213  const QByteArray buf = data->data(format);
214 
215  if (buf.isEmpty())
216  continue;
217 
218  int ret = set_clipboard_data(format.toUtf8().data(), buf.size(), buf.data());
219 #if defined(QBBCLIPBOARD_DEBUG)
220  qDebug() << "QBB: set " << format << "to clipboard, size=" << buf.size() << ";ret=" << ret;
221 #endif
222  if (ret)
223  m_mimeData->addFormatToCheck(format);
224  }
225 
226 }
227 
229 {
230  if (mode != QClipboard::Clipboard)
231  return 0;
232 
233  if (m_mimeData->userMimeData())
234  return m_mimeData->userMimeData();
235 
236  m_mimeData->clear();
237 
238  return m_mimeData;
239 }
240 
242 
243 #include "qbbclipboard.moc"
244 
245 #endif //QT_NO_CLIPBOARD
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
Mode
This enum type is used to control which part of the system clipboard is used by QClipboard::mimeData(...
Definition: qclipboard.h:71
void setUserMimeData(QMimeData *userMimeData)
static QWaylandClipboard * clipboard
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
QVariant qVariantFromValue(const T &)
Definition: qvariant.h:451
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
MimeData(QBBClipboard *clipboard)
virtual QMimeData * mimeData(QClipboard::Mode mode=QClipboard::Clipboard)
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
#define Q_SLOTS
Definition: qobjectdefs.h:71
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...
The QString class provides a Unicode character string.
Definition: qstring.h:83
QBBClipboard *const m_clipboard
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool hasFormat(const QString &mimetype) const
Returns true if the object can return data for the MIME type specified by mimeType; otherwise returns...
QStringList formats() const
Returns a list of formats supported by the object.
Q_CORE_EXPORT void qDebug(const char *,...)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QSet< QString > m_formatsToCheck
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
static const char * data(const QByteArray &arr)
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
static const char * typeList[]
QMimeData * userMimeData()
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
MimeData * m_mimeData
Definition: qbbclipboard.h:59
#define Q_OBJECT
Definition: qobjectdefs.h:157
void emitChanged(QClipboard::Mode mode)
virtual ~QBBClipboard()
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
virtual void setMimeData(QMimeData *data, QClipboard::Mode mode=QClipboard::Clipboard)
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
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
#define Q_FOREACH(variable, container)
Same as foreach(variable, container).
Definition: qglobal.h:2435
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
static QByteArray readClipboardBuff(const char *type)
void addFormatToCheck(const QString &format)
#define Q_FUNC_INFO
Definition: qglobal.h:1871