Revert "Suppress QWindowSystemInterface inclusion warnings."
[profile/ivi/qtwayland.git] / src / plugins / platforms / wayland / qwaylanddataoffer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwaylanddataoffer.h"
43 #include "qwaylanddatadevicemanager.h"
44
45 #include <QtGui/private/qguiapplication_p.h>
46 #include <qpa/qplatformclipboard.h>
47
48 #include <QtCore/QDebug>
49
50
51 void QWaylandDataOffer::offer_sync_callback(void *data,
52              struct wl_callback *callback,
53              uint32_t time)
54 {
55     Q_UNUSED(time);
56     QWaylandDataOffer *mime = static_cast<QWaylandDataOffer *>(data);
57     if (mime->m_receiveSyncCallback == callback) {
58         mime->m_receiveSyncCallback = 0;
59         wl_callback_destroy(callback);
60     }
61 }
62
63 const struct wl_callback_listener QWaylandDataOffer::offer_sync_callback_listener = {
64     QWaylandDataOffer::offer_sync_callback
65 };
66
67 void QWaylandDataOffer::offer(void *data,
68               struct wl_data_offer *wl_data_offer,
69               const char *type)
70 {
71     Q_UNUSED(wl_data_offer);
72
73     QWaylandDataOffer *data_offer = static_cast<QWaylandDataOffer *>(data);
74
75     if (!data_offer->m_receiveSyncCallback) {
76         data_offer->m_receiveSyncCallback = wl_display_sync(data_offer->m_display->wl_display());
77         wl_callback_add_listener(data_offer->m_receiveSyncCallback, &offer_sync_callback_listener, data_offer);
78     }
79
80     data_offer->m_offered_mime_types.append(QString::fromLocal8Bit(type));
81 //    qDebug() << data_offer->m_offered_mime_types;
82 }
83
84 const struct wl_data_offer_listener QWaylandDataOffer::data_offer_listener = {
85     QWaylandDataOffer::offer
86 };
87
88 QWaylandDataOffer::QWaylandDataOffer(QWaylandDisplay *display, struct wl_data_offer *data_offer)
89     : m_display(display)
90     , m_receiveSyncCallback(0)
91 {
92     m_data_offer = data_offer;
93     wl_data_offer_set_user_data(m_data_offer,this);
94     wl_data_offer_add_listener(m_data_offer,&data_offer_listener,this);
95 }
96
97 QWaylandDataOffer::~QWaylandDataOffer()
98 {
99     wl_data_offer_destroy(m_data_offer);
100 }
101
102 bool QWaylandDataOffer::hasFormat_sys(const QString &mimeType) const
103 {
104     return m_offered_mime_types.contains(mimeType);
105 }
106
107 QStringList QWaylandDataOffer::formats_sys() const
108 {
109     return m_offered_mime_types;
110 }
111
112 QVariant QWaylandDataOffer::retrieveData_sys(const QString &mimeType, QVariant::Type type) const
113 {
114     Q_UNUSED(type);
115     if (m_offered_mime_types.isEmpty())
116         return QVariant();
117
118     int pipefd[2];
119     if (pipe(pipefd) == -1) {
120         qWarning("QWaylandMimeData: pipe() failed");
121         return QVariant();
122     }
123
124     QByteArray mimeTypeBa = mimeType.toLatin1();
125     wl_data_offer_receive(m_data_offer,mimeTypeBa.constData(),pipefd[1]);
126
127     m_display->forceRoundTrip();
128     close(pipefd[1]);
129
130     QByteArray content;
131     char buf[256];
132     int n;
133     while ((n = QT_READ(pipefd[0], &buf, sizeof buf)) > 0) {
134         content.append(buf, n);
135     }
136
137     close(pipefd[0]);
138     return content;
139 }
140
141 struct wl_data_offer *QWaylandDataOffer::handle() const
142 {
143     return m_data_offer;
144 }