File indexing completed on 2024-05-19 16:35:17

0001 /*
0002     SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #include "datacontrolsource_v1_interface.h"
0007 #include "clientconnection.h"
0008 #include "datacontroldevicemanager_v1_interface.h"
0009 #include "utils.h"
0010 // Qt
0011 #include <QStringList>
0012 // Wayland
0013 #include <qwayland-server-wlr-data-control-unstable-v1.h>
0014 // system
0015 #include <unistd.h>
0016 
0017 namespace KWaylandServer
0018 {
0019 class DataControlSourceV1InterfacePrivate : public QtWaylandServer::zwlr_data_control_source_v1
0020 {
0021 public:
0022     DataControlSourceV1InterfacePrivate(DataControlSourceV1Interface *q, ::wl_resource *resource);
0023 
0024     QStringList mimeTypes;
0025     DataControlSourceV1Interface *q;
0026 
0027 protected:
0028     void zwlr_data_control_source_v1_destroy_resource(Resource *resource) override;
0029     void zwlr_data_control_source_v1_offer(Resource *resource, const QString &mime_type) override;
0030     void zwlr_data_control_source_v1_destroy(Resource *resource) override;
0031 };
0032 
0033 DataControlSourceV1InterfacePrivate::DataControlSourceV1InterfacePrivate(DataControlSourceV1Interface *_q, ::wl_resource *resource)
0034     : QtWaylandServer::zwlr_data_control_source_v1(resource)
0035     , q(_q)
0036 {
0037 }
0038 
0039 void DataControlSourceV1InterfacePrivate::zwlr_data_control_source_v1_destroy_resource(QtWaylandServer::zwlr_data_control_source_v1::Resource *resource)
0040 {
0041     Q_EMIT q->aboutToBeDestroyed();
0042     delete q;
0043 }
0044 
0045 void DataControlSourceV1InterfacePrivate::zwlr_data_control_source_v1_offer(Resource *, const QString &mimeType)
0046 {
0047     mimeTypes << mimeType;
0048     Q_EMIT q->mimeTypeOffered(mimeType);
0049 }
0050 
0051 void DataControlSourceV1InterfacePrivate::zwlr_data_control_source_v1_destroy(QtWaylandServer::zwlr_data_control_source_v1::Resource *resource)
0052 {
0053     wl_resource_destroy(resource->handle);
0054 }
0055 
0056 DataControlSourceV1Interface::DataControlSourceV1Interface(DataControlDeviceManagerV1Interface *parent, ::wl_resource *resource)
0057     : AbstractDataSource(parent)
0058     , d(new DataControlSourceV1InterfacePrivate(this, resource))
0059 {
0060 }
0061 
0062 DataControlSourceV1Interface::~DataControlSourceV1Interface() = default;
0063 
0064 void DataControlSourceV1Interface::requestData(const QString &mimeType, qint32 fd)
0065 {
0066     d->send_send(mimeType, fd);
0067     close(fd);
0068 }
0069 
0070 void DataControlSourceV1Interface::cancel()
0071 {
0072     d->send_cancelled();
0073 }
0074 
0075 QStringList DataControlSourceV1Interface::mimeTypes() const
0076 {
0077     return d->mimeTypes;
0078 }
0079 
0080 wl_client *DataControlSourceV1Interface::client() const
0081 {
0082     return d->resource()->client();
0083 }
0084 
0085 DataControlSourceV1Interface *DataControlSourceV1Interface::get(wl_resource *native)
0086 {
0087     if (auto sourcePrivate = resource_cast<DataControlSourceV1InterfacePrivate *>(native)) {
0088         return sourcePrivate->q;
0089     }
0090     return nullptr;
0091 }
0092 
0093 }