mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-02 15:45:21 +00:00

完成插件区域的引用图标加载、快捷设置的展示、老插件的加载等功能 Log: 完成特效模式下右侧插件区域的功能 Influence: v23任务栏右侧插件区域 Task: https://pms.uniontech.com/task-view-110311.html Change-Id: I1599907d4529c57070ee2e21b70cc0c17f132e5e
275 lines
8.9 KiB
C++
275 lines
8.9 KiB
C++
#include "indicatorplugin.h"
|
|
|
|
#include <QLabel>
|
|
#include <QDBusConnection>
|
|
#include <QJsonObject>
|
|
#include <QDBusInterface>
|
|
#include <QDBusReply>
|
|
#include <QDebug>
|
|
#include <QApplication>
|
|
#include <QJsonDocument>
|
|
#include <QFile>
|
|
#include <QTimer>
|
|
#include <QDBusMessage>
|
|
#include <thread>
|
|
|
|
class IndicatorPluginPrivate
|
|
{
|
|
public:
|
|
explicit IndicatorPluginPrivate(IndicatorPlugin *parent) : q_ptr(parent) {}
|
|
|
|
void init();
|
|
|
|
void updateContent();
|
|
|
|
void initDBus(const QString &indicatorName);
|
|
|
|
template<typename Func>
|
|
void featData(const QString &key,
|
|
const QJsonObject &data,
|
|
const char *propertyChangedSlot,
|
|
Func const &callback)
|
|
{
|
|
Q_Q(IndicatorPlugin);
|
|
auto dataConfig = data.value(key).toObject();
|
|
auto dbusService = dataConfig.value("dbus_service").toString();
|
|
auto dbusPath = dataConfig.value("dbus_path").toString();
|
|
auto dbusInterface = dataConfig.value("dbus_interface").toString();
|
|
auto isSystemBus = dataConfig.value("system_dbus").toBool(false);
|
|
auto bus = isSystemBus ? QDBusConnection::systemBus() : QDBusConnection::sessionBus();
|
|
|
|
QDBusInterface interface(dbusService, dbusPath, dbusInterface, bus, q);
|
|
|
|
if (dataConfig.contains("dbus_method")) {
|
|
QString methodName = dataConfig.value("dbus_method").toString();
|
|
auto ratio = qApp->devicePixelRatio();
|
|
QDBusReply<QByteArray> reply = interface.call(methodName.toStdString().c_str(), ratio);
|
|
callback(reply.value());
|
|
}
|
|
|
|
if (dataConfig.contains("dbus_properties")) {
|
|
auto propertyName = dataConfig.value("dbus_properties").toString();
|
|
auto propertyNameCStr = propertyName.toStdString();
|
|
propertyInterfaceNames.insert(key, dbusInterface);
|
|
propertyNames.insert(key, QString::fromStdString(propertyNameCStr));
|
|
QDBusConnection::sessionBus().connect(dbusService,
|
|
dbusPath,
|
|
"org.freedesktop.DBus.Properties",
|
|
"PropertiesChanged",
|
|
"sa{sv}as",
|
|
q,
|
|
propertyChangedSlot);
|
|
|
|
// FIXME(sbw): hack for qt dbus property changed signal.
|
|
// see: https://bugreports.qt.io/browse/QTBUG-48008
|
|
QDBusConnection::sessionBus().connect(dbusService,
|
|
dbusPath,
|
|
dbusInterface,
|
|
QString("%1Changed").arg(propertyName),
|
|
"s",
|
|
q,
|
|
propertyChangedSlot);
|
|
|
|
callback(interface.property(propertyNameCStr.c_str()));
|
|
}
|
|
}
|
|
|
|
template<typename Func>
|
|
void propertyChanged(const QString &key, const QDBusMessage &msg, Func const &callback)
|
|
{
|
|
QList<QVariant> arguments = msg.arguments();
|
|
if (1 == arguments.count())
|
|
{
|
|
const QString &v = msg.arguments().at(0).toString();
|
|
callback(v);
|
|
return;
|
|
} else if (3 != arguments.count()) {
|
|
qDebug() << "arguments count must be 3";
|
|
return;
|
|
}
|
|
|
|
QString interfaceName = msg.arguments().at(0).toString();
|
|
if (interfaceName != propertyInterfaceNames.value(key)) {
|
|
qDebug() << "interfaceName mismatch" << interfaceName << propertyInterfaceNames.value(key) << key;
|
|
return;
|
|
}
|
|
QVariantMap changedProps = qdbus_cast<QVariantMap>(arguments.at(1).value<QDBusArgument>());
|
|
if (changedProps.contains(propertyNames.value(key))) {
|
|
callback(changedProps.value(propertyNames.value(key)));
|
|
}
|
|
}
|
|
|
|
IndicatorTrayItem* indicatorTrayWidget = Q_NULLPTR;
|
|
QString indicatorName;
|
|
QMap<QString, QString> propertyNames;
|
|
QMap<QString, QString> propertyInterfaceNames;
|
|
|
|
IndicatorPlugin *q_ptr;
|
|
Q_DECLARE_PUBLIC(IndicatorPlugin)
|
|
};
|
|
|
|
void IndicatorPluginPrivate::init()
|
|
{
|
|
//Q_Q(IndicatorTray);
|
|
|
|
indicatorTrayWidget = new IndicatorTrayItem(indicatorName);
|
|
|
|
initDBus(indicatorName);
|
|
updateContent();
|
|
}
|
|
|
|
void IndicatorPluginPrivate::updateContent()
|
|
{
|
|
indicatorTrayWidget->update();
|
|
|
|
Q_EMIT indicatorTrayWidget->iconChanged();
|
|
}
|
|
|
|
void IndicatorPluginPrivate::initDBus(const QString &indicatorName)
|
|
{
|
|
Q_Q(IndicatorPlugin);
|
|
|
|
QString filepath = QString("/etc/dde-dock/indicator/%1.json").arg(indicatorName);
|
|
QFile confFile(filepath);
|
|
if (!confFile.open(QIODevice::ReadOnly)) {
|
|
qCritical() << "read indicator config Error";
|
|
}
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(confFile.readAll());
|
|
confFile.close();
|
|
auto config = doc.object();
|
|
|
|
auto delay = config.value("delay").toInt(0);
|
|
|
|
qDebug() << "delay load" << delay << indicatorName << q;
|
|
|
|
QTimer::singleShot(delay, [ = ]() {
|
|
auto data = config.value("data").toObject();
|
|
|
|
if (data.contains("text")) {
|
|
featData("text", data, SLOT(textPropertyChanged(QDBusMessage)), [ = ](QVariant v) {
|
|
#ifdef QT_DEBUG // TODO
|
|
if (!v.toString().isEmpty()) {
|
|
#else
|
|
if (v.toString().isEmpty()) {
|
|
#endif
|
|
Q_EMIT q->removed();
|
|
return;
|
|
}
|
|
else {
|
|
Q_EMIT q->delayLoaded();
|
|
}
|
|
indicatorTrayWidget->setText(v.toString());
|
|
updateContent();
|
|
});
|
|
}
|
|
|
|
if (data.contains("icon")) {
|
|
featData("icon", data, SLOT(iconPropertyChanged(QDBusMessage)), [ = ](QVariant v) {
|
|
if (v.toByteArray().isEmpty()) {
|
|
Q_EMIT q->removed();
|
|
return;
|
|
}
|
|
else {
|
|
Q_EMIT q->delayLoaded();
|
|
}
|
|
indicatorTrayWidget->setPixmapData(v.toByteArray());
|
|
updateContent();
|
|
});
|
|
}
|
|
|
|
const QJsonObject action = config.value("action").toObject();
|
|
if (!action.isEmpty() && indicatorTrayWidget)
|
|
q->connect(indicatorTrayWidget, &IndicatorTrayItem::clicked, q, [ = ](uint8_t button_index, int x, int y) {
|
|
/*std::thread t([=]() -> void {
|
|
auto triggerConfig = action.value("trigger").toObject();
|
|
auto dbusService = triggerConfig.value("dbus_service").toString();
|
|
auto dbusPath = triggerConfig.value("dbus_path").toString();
|
|
auto dbusInterface = triggerConfig.value("dbus_interface").toString();
|
|
auto methodName = triggerConfig.value("dbus_method").toString();
|
|
auto isSystemBus = triggerConfig.value("system_dbus").toBool(false);
|
|
auto bus = isSystemBus ? QDBusConnection::systemBus() : QDBusConnection::sessionBus();
|
|
|
|
QDBusInterface interface(dbusService, dbusPath, dbusInterface, bus);
|
|
QDBusReply<void> reply = interface.call(methodName, button_index, x, y);
|
|
if (!reply.isValid()) {
|
|
qDebug() << interface.call(methodName);
|
|
}
|
|
else {
|
|
qDebug() << reply.error();
|
|
}
|
|
});
|
|
t.detach();*/
|
|
});
|
|
});
|
|
}
|
|
|
|
IndicatorPlugin::IndicatorPlugin(const QString &indicatorName, QObject *parent)
|
|
: QObject(parent)
|
|
, d_ptr(new IndicatorPluginPrivate(this))
|
|
{
|
|
Q_D(IndicatorPlugin);
|
|
|
|
d->indicatorName = indicatorName;
|
|
d->init();
|
|
}
|
|
|
|
IndicatorPlugin::~IndicatorPlugin()
|
|
{
|
|
|
|
}
|
|
|
|
IndicatorTrayItem *IndicatorPlugin::widget()
|
|
{
|
|
Q_D(IndicatorPlugin);
|
|
|
|
if (!d->indicatorTrayWidget) {
|
|
d->init();
|
|
}
|
|
|
|
return d->indicatorTrayWidget;
|
|
}
|
|
|
|
void IndicatorPlugin::removeWidget()
|
|
{
|
|
Q_D(IndicatorPlugin);
|
|
|
|
d->indicatorTrayWidget = nullptr;
|
|
}
|
|
|
|
void IndicatorPlugin::textPropertyChanged(const QDBusMessage &message)
|
|
{
|
|
Q_D(IndicatorPlugin);
|
|
|
|
d->propertyChanged("text", message, [=] (const QVariant &value) {
|
|
if (value.toString().isEmpty()) {
|
|
Q_EMIT removed();
|
|
return;
|
|
}
|
|
|
|
if (!d->indicatorTrayWidget) {
|
|
d->init();
|
|
}
|
|
|
|
d->indicatorTrayWidget->setText(value.toByteArray());
|
|
});
|
|
}
|
|
|
|
void IndicatorPlugin::iconPropertyChanged(const QDBusMessage &message)
|
|
{
|
|
Q_D(IndicatorPlugin);
|
|
|
|
d->propertyChanged("icon", message, [=] (const QVariant &value) {
|
|
if (value.toByteArray().isEmpty()) {
|
|
Q_EMIT removed();
|
|
return;
|
|
}
|
|
|
|
if (!d->indicatorTrayWidget) {
|
|
d->init();
|
|
}
|
|
|
|
d->indicatorTrayWidget->setPixmapData(value.toByteArray());
|
|
});
|
|
}
|