Implement selection offers from compositor to clients.
[profile/ivi/qtwayland.git] / src / plugins / platforms / wayland / qwaylanddataoffer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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 <QtGui/QPlatformClipboard>
47
48 #include <QtCore/QDebug>
49
50
51 void QWaylandDataOffer::offer_sync_callback(void *data,
52              struct wl_callback *wl_callback,
53              uint32_t time)
54 {
55     Q_UNUSED(time);
56
57     QWaylandDataOffer *mime = static_cast<QWaylandDataOffer *>(data);
58     mime->m_receiving_offers = false;
59     wl_callback_destroy(wl_callback);
60 }
61
62 const struct wl_callback_listener QWaylandDataOffer::offer_sync_callback_listener = {
63     QWaylandDataOffer::offer_sync_callback
64 };
65
66 void QWaylandDataOffer::offer(void *data,
67               struct wl_data_offer *wl_data_offer,
68               const char *type)
69 {
70     Q_UNUSED(wl_data_offer);
71
72     QWaylandDataOffer *data_offer = static_cast<QWaylandDataOffer *>(data);
73
74     if (!data_offer->m_receiving_offers) {
75         struct wl_callback *callback = wl_display_sync(data_offer->m_display->wl_display());
76         wl_callback_add_listener(callback,&offer_sync_callback_listener,data_offer);
77         data_offer->m_receiving_offers = true;
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_receiving_offers(false)
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 }