KWayland

datasource.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Martin Gräßlin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "datasource.h"
7 #include "wayland_pointer_p.h"
8 // Qt
9 #include <QMimeType>
10 // Wayland
11 #include <wayland-client-protocol.h>
12 
13 namespace KWayland
14 {
15 namespace Client
16 {
17 
18 class Q_DECL_HIDDEN DataSource::Private
19 {
20 public:
21  explicit Private(DataSource *q);
22  void setup(wl_data_source *s);
23 
24  WaylandPointer<wl_data_source, wl_data_source_destroy> source;
25  DataDeviceManager::DnDAction selectedAction = DataDeviceManager::DnDAction::None;
26 
27 private:
28  void setAction(DataDeviceManager::DnDAction action);
29  static void targetCallback(void *data, wl_data_source *dataSource, const char *mimeType);
30  static void sendCallback(void *data, wl_data_source *dataSource, const char *mimeType, int32_t fd);
31  static void cancelledCallback(void *data, wl_data_source *dataSource);
32  static void dndDropPerformedCallback(void *data, wl_data_source *wl_data_source);
33  static void dndFinishedCallback(void *data, wl_data_source *wl_data_source);
34  static void actionCallback(void *data, wl_data_source *wl_data_source, uint32_t dnd_action);
35 
36  static const struct wl_data_source_listener s_listener;
37 
38  DataSource *q;
39 };
40 
41 const wl_data_source_listener DataSource::Private::s_listener = {
42  targetCallback,
43  sendCallback,
44  cancelledCallback,
45  dndDropPerformedCallback,
46  dndFinishedCallback,
47  actionCallback
48 };
49 
50 DataSource::Private::Private(DataSource *q)
51  : q(q)
52 {
53 }
54 
55 void DataSource::Private::targetCallback(void *data, wl_data_source *dataSource, const char *mimeType)
56 {
57  auto d = reinterpret_cast<DataSource::Private*>(data);
58  Q_ASSERT(d->source == dataSource);
59  emit d->q->targetAccepts(QString::fromUtf8(mimeType));
60 }
61 
62 void DataSource::Private::sendCallback(void *data, wl_data_source *dataSource, const char *mimeType, int32_t fd)
63 {
64  auto d = reinterpret_cast<DataSource::Private*>(data);
65  Q_ASSERT(d->source == dataSource);
66  emit d->q->sendDataRequested(QString::fromUtf8(mimeType), fd);
67 }
68 
69 void DataSource::Private::cancelledCallback(void *data, wl_data_source *dataSource)
70 {
71  auto d = reinterpret_cast<DataSource::Private*>(data);
72  Q_ASSERT(d->source == dataSource);
73  emit d->q->cancelled();
74 }
75 
76 void DataSource::Private::dndDropPerformedCallback(void *data, wl_data_source *wl_data_source)
77 {
78  Q_UNUSED(wl_data_source)
79  auto d = reinterpret_cast<DataSource::Private*>(data);
80  emit d->q->dragAndDropPerformed();
81 }
82 
83 void DataSource::Private::dndFinishedCallback(void *data, wl_data_source *wl_data_source)
84 {
85  Q_UNUSED(wl_data_source)
86  auto d = reinterpret_cast<DataSource::Private*>(data);
87  emit d->q->dragAndDropFinished();
88 }
89 
90 void DataSource::Private::actionCallback(void *data, wl_data_source *wl_data_source, uint32_t dnd_action)
91 {
92  Q_UNUSED(wl_data_source)
93  auto d = reinterpret_cast<Private*>(data);
94  switch(dnd_action) {
95  case WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY:
96  d->setAction(DataDeviceManager::DnDAction::Copy);
97  break;
98  case WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE:
99  d->setAction(DataDeviceManager::DnDAction::Move);
100  break;
101  case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK:
102  d->setAction(DataDeviceManager::DnDAction::Ask);
103  break;
104  case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE:
105  d->setAction(DataDeviceManager::DnDAction::None);
106  break;
107  default:
108  Q_UNREACHABLE();
109  }
110 }
111 
112 void DataSource::Private::setAction(DataDeviceManager::DnDAction action)
113 {
114  if (action == selectedAction) {
115  return;
116  }
117  selectedAction = action;
118  emit q->selectedDragAndDropActionChanged();
119 }
120 
121 void DataSource::Private::setup(wl_data_source *s)
122 {
123  Q_ASSERT(!source.isValid());
124  Q_ASSERT(s);
125  source.setup(s);
126  wl_data_source_add_listener(s, &s_listener, this);
127 }
128 
129 DataSource::DataSource(QObject *parent)
130  : QObject(parent)
131  , d(new Private(this))
132 {
133 }
134 
135 DataSource::~DataSource()
136 {
137  release();
138 }
139 
140 void DataSource::release()
141 {
142  d->source.release();
143 }
144 
145 void DataSource::destroy()
146 {
147  d->source.destroy();
148 }
149 
150 bool DataSource::isValid() const
151 {
152  return d->source.isValid();
153 }
154 
155 void DataSource::setup(wl_data_source *dataSource)
156 {
157  d->setup(dataSource);
158 }
159 
160 void DataSource::offer(const QString &mimeType)
161 {
162  wl_data_source_offer(d->source, mimeType.toUtf8().constData());
163 }
164 
165 void DataSource::offer(const QMimeType &mimeType)
166 {
167  if (!mimeType.isValid()) {
168  return;
169  }
170  offer(mimeType.name());
171 }
172 
173 DataSource::operator wl_data_source*() const
174 {
175  return d->source;
176 }
177 
178 DataSource::operator wl_data_source*()
179 {
180  return d->source;
181 }
182 
183 void DataSource::setDragAndDropActions(DataDeviceManager::DnDActions actions)
184 {
185  uint32_t wlActions = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
186  if (actions.testFlag(DataDeviceManager::DnDAction::Copy)) {
187  wlActions |= WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
188  }
189  if (actions.testFlag(DataDeviceManager::DnDAction::Move)) {
190  wlActions |= WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
191  }
192  if (actions.testFlag(DataDeviceManager::DnDAction::Ask)) {
193  wlActions |= WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
194  }
195  wl_data_source_set_actions(d->source, wlActions);
196 }
197 
198 DataDeviceManager::DnDAction DataSource::selectedDragAndDropAction() const
199 {
200  return d->selectedAction;
201 }
202 
203 }
204 }
QString fromUtf8(const char *str, int size)
const char * constData() const const
bool isValid() const const
DnDAction
Drag and Drop actions supported by DataSource and DataOffer.
QObject * parent() const const
bool testFlag(Enum flag) const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:48:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.