KWayland

datasource_interface.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_interface.h"
7 #include "datadevicemanager_interface.h"
8 #include "clientconnection.h"
9 #include "resource_p.h"
10 // Qt
11 #include <QStringList>
12 // Wayland
13 #include <wayland-server.h>
14 // system
15 #include <unistd.h>
16 
17 namespace KWayland
18 {
19 namespace Server
20 {
21 
22 class DataSourceInterface::Private : public Resource::Private
23 {
24 public:
25  Private(DataSourceInterface *q, DataDeviceManagerInterface *parent, wl_resource *parentResource);
26  ~Private();
27 
29  DataDeviceManagerInterface::DnDActions supportedDnDActions = DataDeviceManagerInterface::DnDAction::None;
30 
31 private:
32  DataSourceInterface *q_func() {
33  return reinterpret_cast<DataSourceInterface *>(q);
34  }
35  void offer(const QString &mimeType);
36 
37  static void offerCallback(wl_client *client, wl_resource *resource, const char *mimeType);
38  static void setActionsCallback(wl_client *client, wl_resource *resource, uint32_t dnd_actions);
39 
40  const static struct wl_data_source_interface s_interface;
41 };
42 
43 #ifndef K_DOXYGEN
44 const struct wl_data_source_interface DataSourceInterface::Private::s_interface = {
45  offerCallback,
46  resourceDestroyedCallback,
47  setActionsCallback
48 };
49 #endif
50 
51 DataSourceInterface::Private::Private(DataSourceInterface *q, DataDeviceManagerInterface *parent, wl_resource *parentResource)
52  : Resource::Private(q, parent, parentResource, &wl_data_source_interface, &s_interface)
53 {
54 }
55 
56 DataSourceInterface::Private::~Private() = default;
57 
58 void DataSourceInterface::Private::offerCallback(wl_client *client, wl_resource *resource, const char *mimeType)
59 {
60  Q_UNUSED(client)
61  cast<Private>(resource)->offer(QString::fromUtf8(mimeType));
62 }
63 
64 void DataSourceInterface::Private::offer(const QString &mimeType)
65 {
66  mimeTypes << mimeType;
67  Q_Q(DataSourceInterface);
68  emit q->mimeTypeOffered(mimeType);
69 }
70 
71 void DataSourceInterface::Private::setActionsCallback(wl_client *client, wl_resource *resource, uint32_t dnd_actions)
72 {
73  Q_UNUSED(client)
74  DataDeviceManagerInterface::DnDActions supportedActions;
75  if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY) {
76  supportedActions |= DataDeviceManagerInterface::DnDAction::Copy;
77  }
78  if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE) {
79  supportedActions |= DataDeviceManagerInterface::DnDAction::Move;
80  }
81  if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK) {
82  supportedActions |= DataDeviceManagerInterface::DnDAction::Ask;
83  }
84  // verify that the no other actions are sent
85  if (dnd_actions & ~(WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)) {
86  wl_resource_post_error(resource, WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK, "Invalid action mask");
87  return;
88  }
89  auto p = cast<Private>(resource);
90  if (p->supportedDnDActions!= supportedActions) {
91  p->supportedDnDActions = supportedActions;
92  emit p->q_func()->supportedDragAndDropActionsChanged();
93  }
94 }
95 
96 DataSourceInterface::DataSourceInterface(DataDeviceManagerInterface *parent, wl_resource *parentResource)
97  : Resource(new Private(this, parent, parentResource))
98 {
99  if (wl_resource_get_version(parentResource) < WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
100  Q_D();
101  d->supportedDnDActions = DataDeviceManagerInterface::DnDAction::Copy;
102  }
103 }
104 
105 DataSourceInterface::~DataSourceInterface() = default;
106 
107 void DataSourceInterface::accept(const QString &mimeType)
108 {
109  Q_D();
110  // TODO: does this require a sanity check on the possible mimeType?
111  wl_data_source_send_target(d->resource, mimeType.isEmpty() ? nullptr : mimeType.toUtf8().constData());
112 }
113 
114 void DataSourceInterface::requestData(const QString &mimeType, qint32 fd)
115 {
116  Q_D();
117  // TODO: does this require a sanity check on the possible mimeType?
118  if (d->resource) {
119  wl_data_source_send_send(d->resource, mimeType.toUtf8().constData(), int32_t(fd));
120  }
121  close(fd);
122 }
123 
124 void DataSourceInterface::cancel()
125 {
126  Q_D();
127  if (!d->resource) {
128  return;
129  }
130  wl_data_source_send_cancelled(d->resource);
131  client()->flush();
132 }
133 
134 QStringList DataSourceInterface::mimeTypes() const
135 {
136  Q_D();
137  return d->mimeTypes;
138 }
139 
140 DataSourceInterface *DataSourceInterface::get(wl_resource *native)
141 {
142  return Private::get<DataSourceInterface>(native);
143 }
144 
145 DataSourceInterface::Private *DataSourceInterface::d_func() const
146 {
147  return reinterpret_cast<DataSourceInterface::Private*>(d.data());
148 }
149 
150 DataDeviceManagerInterface::DnDActions DataSourceInterface::supportedDragAndDropActions() const
151 {
152  Q_D();
153  return d->supportedDnDActions;
154 }
155 
156 void DataSourceInterface::dropPerformed()
157 {
158  Q_D();
159  if (wl_resource_get_version(d->resource) < WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) {
160  return;
161  }
162  wl_data_source_send_dnd_drop_performed(d->resource);
163 }
164 
165 void DataSourceInterface::dndFinished()
166 {
167  Q_D();
168  if (wl_resource_get_version(d->resource) < WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
169  return;
170  }
171  wl_data_source_send_dnd_finished(d->resource);
172 }
173 
174 void DataSourceInterface::dndAction(DataDeviceManagerInterface::DnDAction action)
175 {
176  Q_D();
177  if (wl_resource_get_version(d->resource) < WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
178  return;
179  }
180  uint32_t wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
181  if (action == DataDeviceManagerInterface::DnDAction::Copy) {
182  wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
183  } else if (action == DataDeviceManagerInterface::DnDAction::Move ) {
184  wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
185  } else if (action == DataDeviceManagerInterface::DnDAction::Ask) {
186  wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
187  }
188  wl_data_source_send_action(d->resource, wlAction);
189 }
190 
191 }
192 }
wl_resource * parentResource() const
Definition: resource.cpp:93
const QList< QKeySequence > & close()
wl_resource * resource()
Definition: resource.cpp:88
bool isEmpty() const const
const char * constData() const const
QStringList mimeTypes(Types)
T * data() const const
DnDAction
Drag and Drop actions supported by the DataSourceInterface.
ClientConnection * client()
Definition: resource.cpp:78
QObject * parent() 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.