From a1049ff899fa621ef41266e5af419bddbf0f8cdb Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Thu, 4 Aug 2011 13:01:04 +0300 Subject: Support QImage and others in Wayland clipboard. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The wayland plugin's clipboard implementation lacked support for data set via QMimeData::setImageData, QClipboard::setPixmap, etc. because everything was treated as a QByteArray. A variant having an image cannot however be converted to a byte array. Therefore, to make these convenience functions to work, the image is now encoded via QImageWriter before sending it. On the receiving side QMimeData is capable of decoding images from byte arrays, so no problem there. The patch also renames an internal QWaylandMimeData class to a more suitable clipboard-specific name as the functionality is clipboard specific. Change-Id: I77e7df903b8dbe9731613fdbb9693b2a37b05d18 Reviewed-on: http://codereview.qt.nokia.com/2616 Reviewed-by: Qt Sanity Bot Reviewed-by: Jørgen Lind --- .../platforms/wayland/qwaylandclipboard.cpp | 17 ++--- src/plugins/platforms/wayland/qwaylandclipboard.h | 4 +- src/plugins/platforms/wayland/qwaylandmime.cpp | 83 ++++++++++++++++++++++ src/plugins/platforms/wayland/qwaylandmime.h | 55 ++++++++++++++ src/plugins/platforms/wayland/wayland.pro | 6 +- 5 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 src/plugins/platforms/wayland/qwaylandmime.cpp create mode 100644 src/plugins/platforms/wayland/qwaylandmime.h diff --git a/src/plugins/platforms/wayland/qwaylandclipboard.cpp b/src/plugins/platforms/wayland/qwaylandclipboard.cpp index 1ef063f..d20f183 100644 --- a/src/plugins/platforms/wayland/qwaylandclipboard.cpp +++ b/src/plugins/platforms/wayland/qwaylandclipboard.cpp @@ -42,6 +42,7 @@ #include "qwaylandclipboard.h" #include "qwaylanddisplay.h" #include "qwaylandinputdevice.h" +#include "qwaylandmime.h" #include #include #include @@ -52,7 +53,7 @@ static QWaylandClipboard *clipboard = 0; -class QWaylandMimeData : public QInternalMimeData +class QWaylandClipboardMimeData : public QInternalMimeData { public: void clearAll(); @@ -64,28 +65,28 @@ private: QStringList mFormatList; }; -void QWaylandMimeData::clearAll() +void QWaylandClipboardMimeData::clearAll() { clear(); mFormatList.clear(); } -void QWaylandMimeData::setFormats(const QStringList &formatList) +void QWaylandClipboardMimeData::setFormats(const QStringList &formatList) { mFormatList = formatList; } -bool QWaylandMimeData::hasFormat_sys(const QString &mimeType) const +bool QWaylandClipboardMimeData::hasFormat_sys(const QString &mimeType) const { return formats().contains(mimeType); } -QStringList QWaylandMimeData::formats_sys() const +QStringList QWaylandClipboardMimeData::formats_sys() const { return mFormatList; } -QVariant QWaylandMimeData::retrieveData_sys(const QString &mimeType, QVariant::Type type) const +QVariant QWaylandClipboardMimeData::retrieveData_sys(const QString &mimeType, QVariant::Type type) const { return clipboard->retrieveData(mimeType, type); } @@ -147,7 +148,7 @@ void QWaylandSelection::send(void *data, Q_UNUSED(selection); QWaylandSelection *self = static_cast(data); QString mimeType = QString::fromLatin1(mime_type); - QByteArray content = self->mMimeData->data(mimeType); + QByteArray content = QWaylandMimeHelper::getByteArray(self->mMimeData, mimeType); if (!content.isEmpty()) { QFile f; if (f.open(fd, QIODevice::WriteOnly)) @@ -230,7 +231,7 @@ QMimeData *QWaylandClipboard::mimeData(QClipboard::Mode mode) if (!mSelections.isEmpty()) return mSelections.last()->mMimeData; if (!mMimeDataIn) - mMimeDataIn = new QWaylandMimeData; + mMimeDataIn = new QWaylandClipboardMimeData; mMimeDataIn->clearAll(); if (!mOfferedMimeTypes.isEmpty() && mOffer) mMimeDataIn->setFormats(mOfferedMimeTypes); diff --git a/src/plugins/platforms/wayland/qwaylandclipboard.h b/src/plugins/platforms/wayland/qwaylandclipboard.h index 8729434..6b113ae 100644 --- a/src/plugins/platforms/wayland/qwaylandclipboard.h +++ b/src/plugins/platforms/wayland/qwaylandclipboard.h @@ -48,7 +48,7 @@ class QWaylandDisplay; class QWaylandSelection; -class QWaylandMimeData; +class QWaylandClipboardMimeData; struct wl_selection_offer; class QWaylandClipboardSignalEmitter : public QObject @@ -90,7 +90,7 @@ private: static void forceRoundtrip(struct wl_display *display); QWaylandDisplay *mDisplay; - QWaylandMimeData *mMimeDataIn; + QWaylandClipboardMimeData *mMimeDataIn; QList mSelections; QStringList mOfferedMimeTypes; struct wl_selection_offer *mOffer; diff --git a/src/plugins/platforms/wayland/qwaylandmime.cpp b/src/plugins/platforms/wayland/qwaylandmime.cpp new file mode 100644 index 0000000..c218d7d --- /dev/null +++ b/src/plugins/platforms/wayland/qwaylandmime.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qwaylandmime.h" +#include +#include +#include +#include +#include + +QByteArray QWaylandMimeHelper::getByteArray(QMimeData *mimeData, const QString &mimeType) +{ + QByteArray content; + if (mimeType == QLatin1String("text/plain")) { + content = mimeData->text().toUtf8(); + } else if (mimeData->hasImage() + && (mimeType == QLatin1String("application/x-qt-image") + || mimeType.startsWith("image/"))) { + QImage image = qvariant_cast(mimeData->imageData()); + if (!image.isNull()) { + QBuffer buf; + buf.open(QIODevice::ReadWrite); + QByteArray fmt = "BMP"; + if (mimeType.startsWith("image/")) { + QByteArray imgFmt = mimeType.mid(6).toUpper().toAscii(); + if (QImageWriter::supportedImageFormats().contains(imgFmt)) + fmt = imgFmt; + } + QImageWriter wr(&buf, fmt); + wr.write(image); + content = buf.buffer(); + } + } else if (mimeType == QLatin1String("application/x-color")) { + content = qvariant_cast(mimeData->colorData()).name().toAscii(); + } else if (mimeType == QLatin1String("text/uri-list")) { + QList urls = mimeData->urls(); + for (int i = 0; i < urls.count(); ++i) { + content.append(urls.at(i).toEncoded()); + content.append('\n'); + } + } else { + content = mimeData->data(mimeType); + } + return content; +} diff --git a/src/plugins/platforms/wayland/qwaylandmime.h b/src/plugins/platforms/wayland/qwaylandmime.h new file mode 100644 index 0000000..5f6ed95 --- /dev/null +++ b/src/plugins/platforms/wayland/qwaylandmime.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWAYLANDMIME_H +#define QWAYLANDMIME_H + +#include +#include +#include + +class QWaylandMimeHelper +{ +public: + static QByteArray getByteArray(QMimeData *mimeData, const QString &mimeType); +}; + +#endif diff --git a/src/plugins/platforms/wayland/wayland.pro b/src/plugins/platforms/wayland/wayland.pro index 857a291..1e0bbe6 100644 --- a/src/plugins/platforms/wayland/wayland.pro +++ b/src/plugins/platforms/wayland/wayland.pro @@ -20,7 +20,8 @@ SOURCES = main.cpp \ qwaylandwindow.cpp \ qwaylandscreen.cpp \ qwaylandshmwindow.cpp \ - qwaylandclipboard.cpp + qwaylandclipboard.cpp \ + qwaylandmime.cpp HEADERS = qwaylandintegration.h \ qwaylandnativeinterface.h \ @@ -31,7 +32,8 @@ HEADERS = qwaylandintegration.h \ qwaylandshmsurface.h \ qwaylandbuffer.h \ qwaylandshmwindow.h \ - qwaylandclipboard.h + qwaylandclipboard.h \ + qwaylandmime.h INCLUDEPATH += $$QMAKE_INCDIR_WAYLAND LIBS += $$QMAKE_LIBS_WAYLAND -- cgit v1.2.3 From bafeda59c7707f67e5bbf13f3a4772e21faeb09a Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 10 Aug 2011 10:05:03 +0300 Subject: Handle EINTR gracefully in Wayland clipboard. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Idd5082f02eb6708685421492afb8ad5b8546a5d6 Reviewed-on: http://codereview.qt.nokia.com/2810 Reviewed-by: Qt Sanity Bot Reviewed-by: Jørgen Lind --- src/plugins/platforms/wayland/qwaylandclipboard.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/plugins/platforms/wayland/qwaylandclipboard.cpp b/src/plugins/platforms/wayland/qwaylandclipboard.cpp index d20f183..45f0abc 100644 --- a/src/plugins/platforms/wayland/qwaylandclipboard.cpp +++ b/src/plugins/platforms/wayland/qwaylandclipboard.cpp @@ -50,6 +50,7 @@ #include #include #include +#include // for QT_READ static QWaylandClipboard *clipboard = 0; @@ -219,7 +220,7 @@ QVariant QWaylandClipboard::retrieveData(const QString &mimeType, QVariant::Type char buf[256]; int n; close(pipefd[1]); - while ((n = read(pipefd[0], &buf, sizeof buf)) > 0) + while ((n = QT_READ(pipefd[0], &buf, sizeof buf)) > 0) content.append(buf, n); close(pipefd[0]); return content; -- cgit v1.2.3