| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dfiledragclient.h" |
| 6 |
#include "private/dfiledragcommon_p.h" |
| 7 |
|
| 8 |
#include <DObjectPrivate> |
| 9 |
|
| 10 |
#include <QMimeData> |
| 11 |
#include <QUrl> |
| 12 |
#include <QUuid> |
| 13 |
#include <QSharedPointer> |
| 14 |
#include <QDBusConnection> |
| 15 |
#include <QDBusConnectionInterface> |
| 16 |
#include <QDBusInterface> |
| 17 |
#include <QDBusReply> |
| 18 |
|
| 19 |
DGUI_BEGIN_NAMESPACE |
| 20 |
|
| 21 |
class DDndClientSignalRelay : public QObject |
| 22 |
{ |
| 23 |
Q_OBJECT |
| 24 |
public Q_SLOTS: |
| 25 |
void progressChanged(QString uuid, int progress); |
| 26 |
void stateChanged(QString uuid, int state); |
| 27 |
void serverDestroyed(QString uuid); |
| 28 |
|
| 29 |
private: |
| 30 |
static QWeakPointer<DDndClientSignalRelay> relayref; |
| 31 |
friend class DFileDragClient; |
| 32 |
}; |
| 33 |
|
| 34 |
class DFileDragClientPrivate : DCORE_NAMESPACE::DObjectPrivate |
| 35 |
{ |
| 36 |
explicit DFileDragClientPrivate(DFileDragClient *q) |
| 37 |
: DCORE_NAMESPACE::DObjectPrivate(q) {} |
| 38 |
QUuid uuid; |
| 39 |
QString service; |
| 40 |
QSharedPointer<QDBusInterface> iface; |
| 41 |
QSharedPointer<DDndClientSignalRelay> relay; |
| 42 |
|
| 43 |
static QHash<QString, DFileDragClient*> connectionmap; |
| 44 |
static QHash<QString, QWeakPointer<QDBusInterface>> ifacemap; |
| 45 |
|
| 46 |
D_DECLARE_PUBLIC(DFileDragClient) |
| 47 |
friend class DDndClientSignalRelay; |
| 48 |
}; |
| 49 |
|
| 50 |
QHash<QString, DFileDragClient*> DFileDragClientPrivate::connectionmap; |
| 51 |
QHash<QString, QWeakPointer<QDBusInterface>> DFileDragClientPrivate::ifacemap; |
| 52 |
QWeakPointer<DDndClientSignalRelay> DDndClientSignalRelay::relayref; |
| 53 |
|
| 54 |
void DDndClientSignalRelay::progressChanged(QString uuid, int progress) |
| 55 |
{ |
| 56 |
if (DFileDragClientPrivate::connectionmap.contains(uuid)) { |
| 57 |
Q_EMIT DFileDragClientPrivate::connectionmap[uuid]->progressChanged(progress); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void DDndClientSignalRelay::stateChanged(QString uuid, int state) |
| 62 |
{ |
| 63 |
if (DFileDragClientPrivate::connectionmap.contains(uuid)) { |
| 64 |
Q_EMIT DFileDragClientPrivate::connectionmap[uuid]->stateChanged(static_cast<DFileDragState>(state)); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
void DDndClientSignalRelay::serverDestroyed(QString uuid) |
| 69 |
{ |
| 70 |
if (DFileDragClientPrivate::connectionmap.contains(uuid)) { |
| 71 |
Q_EMIT DFileDragClientPrivate::connectionmap[uuid]->serverDestroyed(); |
| 72 |
DFileDragClientPrivate::connectionmap[uuid]->deleteLater(); |
| 73 |
DFileDragClientPrivate::connectionmap.remove(uuid); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/*! |
| 78 |
\class Dtk::Gui::DFileDragClient |
| 79 |
\inmodule dtkgui |
| 80 |
\brief 提供拖拽文件时与文件发送方交互的接口. |
| 81 |
*/ |
| 82 |
|
| 83 |
/*! |
| 84 |
\fn void DFileDragClient::progressChanged(int progress) |
| 85 |
\a progress 当前进度 |
| 86 |
\brief 信号会在当前进度变化时被发送. |
| 87 |
*/ |
| 88 |
|
| 89 |
/*! |
| 90 |
\fn void DFileDragClient::stateChanged(DFileDragState state) |
| 91 |
\a state 改变后的新状态 |
| 92 |
\brief 信号会在当前状态变化时被发送. |
| 93 |
*/ |
| 94 |
|
| 95 |
/*! |
| 96 |
\fn void DFileDragClient::serverDestroyed() |
| 97 |
\brief 信号会在发送方析构销毁前被发送. |
| 98 |
|
| 99 |
\note DFileDragClient 收到后会自删除(deletelater),因此不用去管理 new 出来的 DFileDragClient |
| 100 |
*/ |
| 101 |
|
| 102 |
DFileDragClient::DFileDragClient(const QMimeData *data, QObject *parent) |
| 103 |
: QObject(parent) |
| 104 |
, DCORE_NAMESPACE::DObject(*new DFileDragClientPrivate(this)) |
| 105 |
{ |
| 106 |
D_D(DFileDragClient); |
| 107 |
Q_ASSERT(checkMimeData(data)); |
| 108 |
|
| 109 |
d->service = data->data(DND_MIME_SERVICE); |
| 110 |
d->uuid = QUuid(data->data(DND_MIME_UUID)); |
| 111 |
d->connectionmap[d->uuid.toString()] = this; |
| 112 |
|
| 113 |
if (DDndClientSignalRelay::relayref.isNull()) { |
| 114 |
d->relay = QSharedPointer<DDndClientSignalRelay>(new DDndClientSignalRelay); |
| 115 |
DDndClientSignalRelay::relayref = d->relay.toWeakRef(); |
| 116 |
} else { |
| 117 |
d->relay = DDndClientSignalRelay::relayref.toStrongRef(); |
| 118 |
} |
| 119 |
|
| 120 |
if (!DFileDragClientPrivate::ifacemap.contains(d->service)) { |
| 121 |
QDBusConnection sessionBus(QDBusConnection::sessionBus()); |
| 122 |
d->iface = QSharedPointer<QDBusInterface>(new QDBusInterface(d->service, DND_OBJPATH, DND_INTERFACE, sessionBus), [d](QDBusInterface* intf){ |
| 123 |
QDBusConnection sessionBus(QDBusConnection::sessionBus()); |
| 124 |
sessionBus.disconnect(d->service, DND_OBJPATH, DND_INTERFACE, "progressChanged", "si", d->relay.data(), SLOT(progressChanged(QString, int))); |
| 125 |
sessionBus.disconnect(d->service, DND_OBJPATH, DND_INTERFACE, "stateChanged", "si", d->relay.data(), SLOT(stateChanged(QString, int))); |
| 126 |
sessionBus.disconnect(d->service, DND_OBJPATH, DND_INTERFACE, "serverDestroyed", "s", d->relay.data(), SLOT(serverDestroyed(QString))); |
| 127 |
intf->deleteLater(); |
| 128 |
DFileDragClientPrivate::ifacemap.remove(d->service); |
| 129 |
}); |
| 130 |
DFileDragClientPrivate::ifacemap[d->service] = d->iface.toWeakRef(); |
| 131 |
sessionBus.connect(d->service, DND_OBJPATH, DND_INTERFACE, "progressChanged", "si", d->relay.data(), SLOT(progressChanged(QString, int))); |
| 132 |
sessionBus.connect(d->service, DND_OBJPATH, DND_INTERFACE, "stateChanged", "si", d->relay.data(), SLOT(stateChanged(QString, int))); |
| 133 |
sessionBus.connect(d->service, DND_OBJPATH, DND_INTERFACE, "serverDestroyed", "s", d->relay.data(), SLOT(serverDestroyed(QString))); |
| 134 |
} else { |
| 135 |
d->iface = DFileDragClientPrivate::ifacemap[d->service].toStrongRef(); |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
/*! |
| 141 |
\brief DFileDragClient::progress |
| 142 |
\return 返回当前拖拽的进度 |
| 143 |
*/ |
| 144 |
int DFileDragClient::progress() const |
| 145 |
{ |
| 146 |
D_D(const DFileDragClient); |
| 147 |
|
| 148 |
return QDBusReply<int>(d->iface->call("progress", d->uuid.toString())).value(); |
| 149 |
} |
| 150 |
|
| 151 |
/*! |
| 152 |
\brief DFileDragClient::state |
| 153 |
\return 返回当前状态,见 DFileDragState |
| 154 |
*/ |
| 155 |
DFileDragState DFileDragClient::state() const |
| 156 |
{ |
| 157 |
D_D(const DFileDragClient); |
| 158 |
|
| 159 |
return static_cast<DFileDragState>(QDBusReply<int>(d->iface->call("state", d->uuid.toString())).value()); |
| 160 |
} |
| 161 |
|
| 162 |
/*! |
| 163 |
\brief DFileDragClient::checkMimeData |
| 164 |
\a data |
| 165 |
\return 包含 DND_MIME_PID 格式的数据时返回 true,否则返回 false |
| 166 |
\note 通常在接收拖放数据的应用的dropEvent(QDropEvent *event)函数中检测当前 event->mimeData() 是否是 DFileDrag |
| 167 |
*/ |
| 168 |
bool DFileDragClient::checkMimeData(const QMimeData *data) |
| 169 |
{ |
| 170 |
return data->hasFormat(DND_MIME_SERVICE) && data->hasFormat(DND_MIME_PID); |
| 171 |
} |
| 172 |
|
| 173 |
/*! |
| 174 |
\brief DFileDragClient::setTargetData |
| 175 |
\a data 拖放时传入的data,用于获取和发送数据的应用dbus通讯需要的一些信息 |
| 176 |
\a key |
| 177 |
\a value |
| 178 |
\note 向文件发送方设置自定义数据 |
| 179 |
*/ |
| 180 |
void DFileDragClient::setTargetData(const QMimeData *data, QString key, QVariant value) |
| 181 |
{ |
| 182 |
Q_ASSERT(checkMimeData(data)); |
| 183 |
QString service(data->data(DND_MIME_SERVICE)); |
| 184 |
QString uuid(data->data(DND_MIME_UUID)); |
| 185 |
QDBusInterface iface(service, DND_OBJPATH, DND_INTERFACE, QDBusConnection::sessionBus()); |
| 186 |
|
| 187 |
QDBusReply<uint> pid = QDBusConnection::sessionBus().interface()->servicePid(service); |
| 188 |
if (QString::number(pid).toUtf8() != data->data(DND_MIME_PID)) { |
| 189 |
return; |
| 190 |
} |
| 191 |
iface.call("setData", uuid, key, value.toString()); |
| 192 |
} |
| 193 |
|
| 194 |
/*! |
| 195 |
\brief DFileDragClient::setTargetUrl |
| 196 |
\a data |
| 197 |
\a url |
| 198 |
\note 告知文件发送方拖拽目标路径 |
| 199 |
*/ |
| 200 |
void DFileDragClient::setTargetUrl(const QMimeData *data, QUrl url) |
| 201 |
{ |
| 202 |
setTargetData(data, DND_TARGET_URL_KEY, QVariant::fromValue(url.toString())); |
| 203 |
} |
| 204 |
|
| 205 |
DGUI_END_NAMESPACE |
| 206 |
|
| 207 |
#include "dfiledragclient.moc" |