File indexing completed on 2024-05-19 05:32:33

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 "datacontroloffer_v1.h"
0007 #include "datacontroldevice_v1.h"
0008 #include "datacontrolsource_v1.h"
0009 // Qt
0010 #include <QPointer>
0011 #include <QStringList>
0012 // Wayland
0013 #include <qwayland-server-wlr-data-control-unstable-v1.h>
0014 // system
0015 #include <unistd.h>
0016 
0017 namespace KWin
0018 {
0019 class DataControlOfferV1InterfacePrivate : public QtWaylandServer::zwlr_data_control_offer_v1
0020 {
0021 public:
0022     DataControlOfferV1InterfacePrivate(DataControlOfferV1Interface *q, AbstractDataSource *source, wl_resource *resource);
0023 
0024     DataControlOfferV1Interface *q;
0025     QPointer<AbstractDataSource> source;
0026 
0027 protected:
0028     void zwlr_data_control_offer_v1_receive(Resource *resource, const QString &mime_type, int32_t fd) override;
0029     void zwlr_data_control_offer_v1_destroy(Resource *resource) override;
0030     void zwlr_data_control_offer_v1_destroy_resource(Resource *resource) override;
0031 };
0032 
0033 DataControlOfferV1InterfacePrivate::DataControlOfferV1InterfacePrivate(DataControlOfferV1Interface *_q, AbstractDataSource *source, wl_resource *resource)
0034     : QtWaylandServer::zwlr_data_control_offer_v1(resource)
0035     , q(_q)
0036     , source(source)
0037 {
0038 }
0039 
0040 void DataControlOfferV1InterfacePrivate::zwlr_data_control_offer_v1_destroy(QtWaylandServer::zwlr_data_control_offer_v1::Resource *resource)
0041 {
0042     wl_resource_destroy(resource->handle);
0043 }
0044 
0045 void DataControlOfferV1InterfacePrivate::zwlr_data_control_offer_v1_destroy_resource(QtWaylandServer::zwlr_data_control_offer_v1::Resource *resource)
0046 {
0047     delete q;
0048 }
0049 
0050 void DataControlOfferV1InterfacePrivate::zwlr_data_control_offer_v1_receive(Resource *resource, const QString &mimeType, qint32 fd)
0051 {
0052     if (!source) {
0053         close(fd);
0054         return;
0055     }
0056     source->requestData(mimeType, fd);
0057 }
0058 
0059 DataControlOfferV1Interface::DataControlOfferV1Interface(AbstractDataSource *source, wl_resource *resource)
0060     : QObject()
0061     , d(new DataControlOfferV1InterfacePrivate(this, source, resource))
0062 {
0063     Q_ASSERT(source);
0064     connect(source, &AbstractDataSource::mimeTypeOffered, this, [this](const QString &mimeType) {
0065         d->send_offer(mimeType);
0066     });
0067 }
0068 
0069 DataControlOfferV1Interface::~DataControlOfferV1Interface() = default;
0070 
0071 void DataControlOfferV1Interface::sendAllOffers()
0072 {
0073     Q_ASSERT(d->source);
0074     for (const QString &mimeType : d->source->mimeTypes()) {
0075         d->send_offer(mimeType);
0076     }
0077 }
0078 
0079 wl_resource *DataControlOfferV1Interface::resource() const
0080 {
0081     return d->resource()->handle;
0082 }
0083 
0084 }
0085 
0086 #include "moc_datacontroloffer_v1.cpp"