diff --git a/frame/controller/dockpluginscontroller.cpp b/frame/controller/dockpluginscontroller.cpp index 9d300abdc..6c3e6103f 100644 --- a/frame/controller/dockpluginscontroller.cpp +++ b/frame/controller/dockpluginscontroller.cpp @@ -33,9 +33,13 @@ DockPluginsController::DockPluginsController( : QObject(itemControllerInter) , m_dbusDaemonInterface(QDBusConnection::sessionBus().interface()) , m_itemControllerInter(itemControllerInter) - , m_pluginsSetting("deepin", "dde-dock") + , m_dockDaemonInter(new DockDaemonInter("com.deepin.dde.daemon.Dock", "/com/deepin/dde/daemon/Dock", QDBusConnection::sessionBus(), this)) { qApp->installEventFilter(this); + + refreshPluginSettings(QDateTime::currentMSecsSinceEpoch() / 1000 / 1000); + + connect(m_dockDaemonInter, &DockDaemonInter::PluginSettingsUpdated, this, &DockPluginsController::refreshPluginSettings); } void DockPluginsController::itemAdded(PluginsItemInterface * const itemInter, const QString &itemKey) @@ -193,6 +197,32 @@ void DockPluginsController::initPlugin(PluginsItemInterface *interface) { qDebug() << "init plugin finished: " << interface->pluginName(); } +void DockPluginsController::refreshPluginSettings(qlonglong ts) +{ + // TODO: handle nano seconds + + const QString &pluginSettings = m_dockDaemonInter->GetPluginSettings().value(); + if (pluginSettings.isEmpty()) { + qDebug() << "Error! get plugin settings from dbus failed!"; + return; + } + + const QJsonObject &settingsObject = QJsonDocument::fromJson(pluginSettings.toLocal8Bit()).object(); + if (settingsObject.isEmpty()) { + qDebug() << "Error! parse plugin settings from json failed!"; + return; + } + + m_pluginSettingsObject = settingsObject; + + // not notify plugins to refresh settings if this update is not emit by dock daemon + if (sender() != m_dockDaemonInter) { + return; + } + + // TODO: notify all plugins to reload plugin settings +} + bool DockPluginsController::eventFilter(QObject *o, QEvent *e) { if (o != qApp) @@ -219,15 +249,20 @@ PluginsItem *DockPluginsController::pluginItemAt(PluginsItemInterface * const it return m_pluginList[itemInter][itemKey]; } -void DockPluginsController::saveValue(PluginsItemInterface *const itemInter, const QString &itemKey, const QVariant &value) { - m_pluginsSetting.beginGroup(itemInter->pluginName()); - m_pluginsSetting.setValue(itemKey, value); - m_pluginsSetting.endGroup(); +void DockPluginsController::saveValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant &value) { + QJsonObject valueObject = m_pluginSettingsObject.value(itemInter->pluginName()).toObject(); + valueObject.insert(key, value.toJsonValue()); + m_pluginSettingsObject.insert(itemInter->pluginName(), valueObject); + + m_dockDaemonInter->SetPluginSettings( + QDateTime::currentMSecsSinceEpoch() / 1000 / 1000, + QJsonDocument(m_pluginSettingsObject).toJson()); } -const QVariant DockPluginsController::getValue(PluginsItemInterface *const itemInter, const QString &itemKey, const QVariant& failback) { - m_pluginsSetting.beginGroup(itemInter->pluginName()); - QVariant value(m_pluginsSetting.value(itemKey, failback)); - m_pluginsSetting.endGroup(); - return value; +const QVariant DockPluginsController::getValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant& fallback) { + QVariant v = m_pluginSettingsObject.value(itemInter->pluginName()).toObject().value(key).toVariant(); + if (v.isNull() || !v.isValid()) { + v = fallback; + } + return v; } diff --git a/frame/controller/dockpluginscontroller.h b/frame/controller/dockpluginscontroller.h index aea88afc6..03d90f5b9 100644 --- a/frame/controller/dockpluginscontroller.h +++ b/frame/controller/dockpluginscontroller.h @@ -25,11 +25,15 @@ #include "item/pluginsitem.h" #include "pluginproxyinterface.h" +#include + #include #include #include #include +using DockDaemonInter = com::deepin::dde::daemon::Dock; + class DockItemController; class PluginsItemInterface; class DockPluginsController : public QObject, PluginProxyInterface @@ -48,8 +52,8 @@ class DockPluginsController : public QObject, PluginProxyInterface void requestWindowAutoHide(PluginsItemInterface * const itemInter, const QString &itemKey, const bool autoHide) Q_DECL_OVERRIDE; void requestRefreshWindowVisible(PluginsItemInterface * const itemInter, const QString &itemKey) Q_DECL_OVERRIDE; void requestSetAppletVisible(PluginsItemInterface * const itemInter, const QString &itemKey, const bool visible) Q_DECL_OVERRIDE; - void saveValue(PluginsItemInterface *const itemInter, const QString &itemKey, const QVariant &value) Q_DECL_OVERRIDE; - const QVariant getValue(PluginsItemInterface *const itemInter, const QString &itemKey, const QVariant& failback = QVariant()) Q_DECL_OVERRIDE; + void saveValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant &value) Q_DECL_OVERRIDE; + const QVariant getValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant& fallback = QVariant()) Q_DECL_OVERRIDE; signals: void pluginItemInserted(PluginsItem *pluginItem) const; @@ -63,6 +67,7 @@ private slots: void positionChanged(); void loadPlugin(const QString &pluginFile); void initPlugin(PluginsItemInterface *interface); + void refreshPluginSettings(qlonglong ts); private: bool eventFilter(QObject *o, QEvent *e) Q_DECL_OVERRIDE; @@ -70,9 +75,11 @@ private slots: private: QDBusConnectionInterface *m_dbusDaemonInterface; - QMap> m_pluginList; DockItemController *m_itemControllerInter; - QSettings m_pluginsSetting; + DockDaemonInter *m_dockDaemonInter; + + QMap> m_pluginList; + QJsonObject m_pluginSettingsObject; }; #endif // DOCKPLUGINSCONTROLLER_H diff --git a/plugins/tray/fashiontray/containers/holdcontainer.cpp b/plugins/tray/fashiontray/containers/holdcontainer.cpp index b6d05dafc..8d685f6b3 100644 --- a/plugins/tray/fashiontray/containers/holdcontainer.cpp +++ b/plugins/tray/fashiontray/containers/holdcontainer.cpp @@ -29,7 +29,7 @@ HoldContainer::HoldContainer(TrayPlugin *trayPlugin, QWidget *parent) bool HoldContainer::acceptWrapper(FashionTrayWidgetWrapper *wrapper) { - const QString &key = wrapper->itemKey() + HoldKeySuffix; + const QString &key = HoldKeyPrefix + wrapper->itemKey(); return trayPlugin()->getValue(wrapper->itemKey(), key, false).toBool(); } @@ -38,7 +38,7 @@ void HoldContainer::addWrapper(FashionTrayWidgetWrapper *wrapper) AbstractContainer::addWrapper(wrapper); if (containsWrapper(wrapper)) { - const QString &key = wrapper->itemKey() + HoldKeySuffix; + const QString &key = HoldKeyPrefix + wrapper->itemKey(); trayPlugin()->saveValue(wrapper->itemKey(), key, true); } } diff --git a/plugins/tray/fashiontray/containers/normalcontainer.cpp b/plugins/tray/fashiontray/containers/normalcontainer.cpp index d42c43428..903fa48cd 100644 --- a/plugins/tray/fashiontray/containers/normalcontainer.cpp +++ b/plugins/tray/fashiontray/containers/normalcontainer.cpp @@ -18,7 +18,7 @@ void NormalContainer::addWrapper(FashionTrayWidgetWrapper *wrapper) AbstractContainer::addWrapper(wrapper); if (containsWrapper(wrapper)) { - const QString &key = wrapper->itemKey() + HoldKeySuffix; + const QString &key = HoldKeyPrefix + wrapper->itemKey(); trayPlugin()->saveValue(wrapper->itemKey(), key, false); } } diff --git a/plugins/tray/fashiontray/fashiontrayconstants.h b/plugins/tray/fashiontray/fashiontrayconstants.h index 3c2926f17..02bbb266b 100644 --- a/plugins/tray/fashiontray/fashiontrayconstants.h +++ b/plugins/tray/fashiontray/fashiontrayconstants.h @@ -9,7 +9,7 @@ namespace Dock { #define TrayWidgetHeightMin 24 -#define HoldKeySuffix "-holded" +#define HoldKeyPrefix "holded_" } diff --git a/plugins/tray/system-trays/systemtrayscontroller.cpp b/plugins/tray/system-trays/systemtrayscontroller.cpp index 006e92fcf..7c9dfae9c 100644 --- a/plugins/tray/system-trays/systemtrayscontroller.cpp +++ b/plugins/tray/system-trays/systemtrayscontroller.cpp @@ -29,9 +29,13 @@ SystemTraysController::SystemTraysController(QObject *parent) : QObject(parent) , m_dbusDaemonInterface(QDBusConnection::sessionBus().interface()) - , m_pluginsSetting("deepin", "dde-dock") + , m_dockDaemonInter(new DockDaemonInter("com.deepin.dde.daemon.Dock", "/com/deepin/dde/daemon/Dock", QDBusConnection::sessionBus(), this)) { qApp->installEventFilter(this); + + refreshPluginSettings(QDateTime::currentMSecsSinceEpoch() / 1000 / 1000); + + connect(m_dockDaemonInter, &DockDaemonInter::PluginSettingsUpdated, this, &SystemTraysController::refreshPluginSettings); } void SystemTraysController::itemAdded(PluginsItemInterface * const itemInter, const QString &itemKey) @@ -182,6 +186,33 @@ void SystemTraysController::initPlugin(PluginsItemInterface *interface) { qDebug() << "SystemTray:" << "init plugin finished: " << interface->pluginName(); } +void SystemTraysController::refreshPluginSettings(qlonglong ts) +{ + // TODO: handle nano seconds + + const QString &pluginSettings = m_dockDaemonInter->GetPluginSettings().value(); + if (pluginSettings.isEmpty()) { + qDebug() << "Error! get plugin settings from dbus failed!"; + return; + } + + const QJsonObject &settingsObject = QJsonDocument::fromJson(pluginSettings.toLocal8Bit()).object(); + if (settingsObject.isEmpty()) { + qDebug() << "Error! parse plugin settings from json failed!"; + return; + } + + m_pluginSettingsObject = settingsObject; + + // not notify plugins to refresh settings if this update is not emit by dock daemon + if (sender() != m_dockDaemonInter) { + return; + } + +// static_cast(sender()) + // TODO: notify all plugins to reload plugin settings +} + bool SystemTraysController::eventFilter(QObject *o, QEvent *e) { if (o != qApp) @@ -235,16 +266,21 @@ PluginsItemInterface *SystemTraysController::pluginInterAt(SystemTrayItem *syste } void SystemTraysController::saveValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant &value) { - m_pluginsSetting.beginGroup(itemInter->pluginName()); - m_pluginsSetting.setValue(key, value); - m_pluginsSetting.endGroup(); + QJsonObject valueObject = m_pluginSettingsObject.value(itemInter->pluginName()).toObject(); + valueObject.insert(key, value.toJsonValue()); + m_pluginSettingsObject.insert(itemInter->pluginName(), valueObject); + + m_dockDaemonInter->SetPluginSettings( + QDateTime::currentMSecsSinceEpoch() / 1000 / 1000, + QJsonDocument(m_pluginSettingsObject).toJson()); } const QVariant SystemTraysController::getValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant& fallback) { - m_pluginsSetting.beginGroup(itemInter->pluginName()); - QVariant value(m_pluginsSetting.value(key, fallback)); - m_pluginsSetting.endGroup(); - return value; + QVariant v = m_pluginSettingsObject.value(itemInter->pluginName()).toObject().value(key).toVariant(); + if (v.isNull() || !v.isValid()) { + v = fallback; + } + return v; } int SystemTraysController::systemTrayItemSortKey(const QString &itemKey) diff --git a/plugins/tray/system-trays/systemtrayscontroller.h b/plugins/tray/system-trays/systemtrayscontroller.h index dc916c9b2..3a92c3b4e 100644 --- a/plugins/tray/system-trays/systemtrayscontroller.h +++ b/plugins/tray/system-trays/systemtrayscontroller.h @@ -25,11 +25,15 @@ #include "systemtrayitem.h" #include "pluginproxyinterface.h" +#include + #include #include #include #include +using DockDaemonInter = com::deepin::dde::daemon::Dock; + class PluginsItemInterface; class SystemTraysController : public QObject, PluginProxyInterface { @@ -67,6 +71,7 @@ private slots: void positionChanged(); void loadPlugin(const QString &pluginFile); void initPlugin(PluginsItemInterface *interface); + void refreshPluginSettings(qlonglong ts); private: bool eventFilter(QObject *o, QEvent *e) Q_DECL_OVERRIDE; @@ -77,8 +82,9 @@ private slots: private: QDBusConnectionInterface *m_dbusDaemonInterface; QMap> m_pluginsMap; + DockDaemonInter *m_dockDaemonInter; - QSettings m_pluginsSetting; + QJsonObject m_pluginSettingsObject; }; #endif // SYSTEMTRAYSCONTROLLER_H