| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dfiledragserver.h" |
| 6 |
#include "private/dfiledragcommon_p.h" |
| 7 |
|
| 8 |
#include <DObjectPrivate> |
| 9 |
|
| 10 |
#include <QMimeData> |
| 11 |
#include <QUuid> |
| 12 |
#include <QDBusServer> |
| 13 |
#include <QDBusConnection> |
| 14 |
#include <QDBusConnectionInterface> |
| 15 |
#include <QDBusVariant> |
| 16 |
#include <QDBusError> |
| 17 |
#include <QCoreApplication> |
| 18 |
#include <QSharedPointer> |
| 19 |
|
| 20 |
DGUI_BEGIN_NAMESPACE |
| 21 |
|
| 22 |
class DDndSourceInterface; |
| 23 |
|
| 24 |
/*! |
| 25 |
@private |
| 26 |
*/ |
| 27 |
class DFileDragServerPrivate : public DCORE_NAMESPACE::DObjectPrivate |
| 28 |
{ |
| 29 |
QMap<QString, QVariant> data; |
| 30 |
QUuid uuid; |
| 31 |
|
| 32 |
explicit DFileDragServerPrivate(DFileDragServer *q); |
| 33 |
~DFileDragServerPrivate(); |
| 34 |
|
| 35 |
void writeMimeData(QMimeData *dest); |
| 36 |
|
| 37 |
QSharedPointer<DDndSourceInterface> dbusif; |
| 38 |
static QHash<QString, DFileDragServer*> servermap; |
| 39 |
static QWeakPointer<DDndSourceInterface> dbusifinst; |
| 40 |
|
| 41 |
D_DECLARE_PUBLIC(DFileDragServer) |
| 42 |
friend class DDndSourceInterface; |
| 43 |
}; |
| 44 |
|
| 45 |
QWeakPointer<DDndSourceInterface> DFileDragServerPrivate::dbusifinst; |
| 46 |
QHash<QString, DFileDragServer*> DFileDragServerPrivate::servermap; |
| 47 |
|
| 48 |
class DDndSourceInterface : public QObject |
| 49 |
{ |
| 50 |
Q_OBJECT |
| 51 |
Q_CLASSINFO("D-Bus Interface", DND_INTERFACE) |
| 52 |
public: |
| 53 |
explicit DDndSourceInterface(QObject *parent = nullptr) : QObject(parent) {} |
| 54 |
|
| 55 |
Q_SIGNALS: |
| 56 |
void serverDestroyed(QString uuid); |
| 57 |
void stateChanged(QString uuid, int state); |
| 58 |
void progressChanged(QString uuid, int progress); |
| 59 |
|
| 60 |
public Q_SLOTS: |
| 61 |
void setData(QString uuid, QString key, QString value) { |
| 62 |
DFileDragServer *srv = DFileDragServerPrivate::servermap.value(uuid); |
| 63 |
Q_ASSERT(srv); |
| 64 |
|
| 65 |
DFileDragServerPrivate *d = srv->d_func(); |
| 66 |
|
| 67 |
if (d->data.value(key) != value) { |
| 68 |
d->data[key] = value; |
| 69 |
Q_EMIT srv->targetDataChanged(key); |
| 70 |
} |
| 71 |
} |
| 72 |
int state(QString uuid) const {return m_statemap.value(uuid);} |
| 73 |
int progress(QString uuid) const {return m_progressmap.value(uuid);} |
| 74 |
|
| 75 |
private: |
| 76 |
QHash<QString, int> m_statemap; |
| 77 |
QHash<QString, int> m_progressmap; |
| 78 |
|
| 79 |
friend class DFileDragServer; |
| 80 |
}; |
| 81 |
|
| 82 |
/*! |
| 83 |
\class Dtk::Gui::DFileDragServer |
| 84 |
\inmodule dtkgui |
| 85 |
\brief 提供拖拽文件时与文件接收方交互的接口. |
| 86 |
*/ |
| 87 |
|
| 88 |
/*! |
| 89 |
\fn void DFileDragServer::targetDataChanged(const QString &key) |
| 90 |
|
| 91 |
\brief 信号会在接收方调用 setData 变化时被发送, \a key 为改变的键值. |
| 92 |
*/ |
| 93 |
|
| 94 |
DFileDragServer::DFileDragServer(QObject *parent) |
| 95 |
: QObject(parent) |
| 96 |
, DCORE_NAMESPACE::DObject(*new DFileDragServerPrivate(this)) |
| 97 |
{ |
| 98 |
D_D(DFileDragServer); |
| 99 |
DFileDragServerPrivate::servermap[d->uuid.toString()] = this; |
| 100 |
} |
| 101 |
|
| 102 |
DFileDragServer::~DFileDragServer() |
| 103 |
{ |
| 104 |
D_D(DFileDragServer); |
| 105 |
Q_EMIT d->dbusif->serverDestroyed(d->uuid.toString()); |
| 106 |
DFileDragServerPrivate::servermap.remove(d->uuid.toString()); |
| 107 |
} |
| 108 |
|
| 109 |
/*! |
| 110 |
\brief DFileDragServer::targetData. |
| 111 |
\a key |
| 112 |
\return 返回文件接收方设置数据 \a key 对应的 value |
| 113 |
*/ |
| 114 |
QVariant DFileDragServer::targetData(const QString &key) const |
| 115 |
{ |
| 116 |
D_D(const DFileDragServer); |
| 117 |
|
| 118 |
return d->data.value(key); |
| 119 |
} |
| 120 |
|
| 121 |
/*! |
| 122 |
\brief DFileDragServer::setProgress. |
| 123 |
\a progress 当前进度 |
| 124 |
\brief 拖拽进度更新,接收方会受到 progressChanged 信号. |
| 125 |
*/ |
| 126 |
void DFileDragServer::setProgress(int progress) |
| 127 |
{ |
| 128 |
D_D(DFileDragServer); |
| 129 |
|
| 130 |
if (d->dbusif && progress != d->dbusif->m_progressmap.value(d->uuid.toString())) { |
| 131 |
d->dbusif->m_progressmap[d->uuid.toString()] = progress; |
| 132 |
Q_EMIT d->dbusif->progressChanged(d->uuid.toString(), progress); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/*! |
| 137 |
\brief DFileDragServer::setState. |
| 138 |
\a state |
| 139 |
\brief 改变状态,接收方会受到 stateChanged 信号. |
| 140 |
*/ |
| 141 |
void DFileDragServer::setState(DFileDragState state) |
| 142 |
{ |
| 143 |
D_D(DFileDragServer); |
| 144 |
|
| 145 |
if (d->dbusif && state != d->dbusif->m_statemap.value(d->uuid.toString())) { |
| 146 |
d->dbusif->m_statemap[d->uuid.toString()] = state; |
| 147 |
Q_EMIT d->dbusif->stateChanged(d->uuid.toString(), state); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
DFileDragServerPrivate::DFileDragServerPrivate(DFileDragServer *q) |
| 152 |
: DCORE_NAMESPACE::DObjectPrivate(q) |
| 153 |
, uuid(QUuid::createUuid()) |
| 154 |
{ |
| 155 |
if (dbusifinst.isNull()) { |
| 156 |
dbusif = QSharedPointer<DDndSourceInterface>(new DDndSourceInterface(), [](DDndSourceInterface *intf){ |
| 157 |
QDBusConnection::sessionBus().unregisterObject(DND_OBJPATH); |
| 158 |
intf->deleteLater(); |
| 159 |
}); |
| 160 |
dbusifinst = dbusif.toWeakRef(); |
| 161 |
QDBusConnection::sessionBus().registerObject(DND_OBJPATH, DND_INTERFACE, dbusif.data(), QDBusConnection::RegisterOption::ExportAllContents); |
| 162 |
} else { |
| 163 |
dbusif = dbusifinst.toStrongRef(); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
DFileDragServerPrivate::~DFileDragServerPrivate() |
| 168 |
{ |
| 169 |
} |
| 170 |
|
| 171 |
void DFileDragServerPrivate::writeMimeData(QMimeData *dest) |
| 172 |
{ |
| 173 |
dest->setData(DND_MIME_SERVICE, QDBusConnection::sessionBus().baseService().toUtf8()); |
| 174 |
// qApp->applicationPid() not right in ll-box |
| 175 |
auto pid = QDBusConnection::sessionBus().interface()->servicePid(QDBusConnection::sessionBus().baseService()); |
| 176 |
dest->setData(DND_MIME_PID, QString::number(pid).toUtf8()); |
| 177 |
dest->setData(DND_MIME_UUID, uuid.toString().toUtf8()); |
| 178 |
} |
| 179 |
|
| 180 |
DGUI_END_NAMESPACE |
| 181 |
|
| 182 |
#include "dfiledragserver.moc" |