Skip to content

Commit

Permalink
feat: access plugin settings from dock dbus daemon
Browse files Browse the repository at this point in the history
Change-Id: I97f3f3e0b3bfa65cac2abda4e3b59494ea1c13d1
  • Loading branch information
listenerri committed Jan 7, 2019
1 parent 945d58a commit 57f8e6e
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 27 deletions.
55 changes: 45 additions & 10 deletions frame/controller/dockpluginscontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
15 changes: 11 additions & 4 deletions frame/controller/dockpluginscontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
#include "item/pluginsitem.h"
#include "pluginproxyinterface.h"

#include <com_deepin_dde_daemon_dock.h>

#include <QPluginLoader>
#include <QList>
#include <QMap>
#include <QDBusConnectionInterface>

using DockDaemonInter = com::deepin::dde::daemon::Dock;

class DockItemController;
class PluginsItemInterface;
class DockPluginsController : public QObject, PluginProxyInterface
Expand All @@ -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;
Expand All @@ -63,16 +67,19 @@ 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;
PluginsItem *pluginItemAt(PluginsItemInterface * const itemInter, const QString &itemKey) const;

private:
QDBusConnectionInterface *m_dbusDaemonInterface;
QMap<PluginsItemInterface *, QMap<QString, PluginsItem *>> m_pluginList;
DockItemController *m_itemControllerInter;
QSettings m_pluginsSetting;
DockDaemonInter *m_dockDaemonInter;

QMap<PluginsItemInterface *, QMap<QString, PluginsItem *>> m_pluginList;
QJsonObject m_pluginSettingsObject;
};

#endif // DOCKPLUGINSCONTROLLER_H
4 changes: 2 additions & 2 deletions plugins/tray/fashiontray/containers/holdcontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/tray/fashiontray/containers/normalcontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/tray/fashiontray/fashiontrayconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Dock {
#define TrayWidgetHeightMin 24


#define HoldKeySuffix "-holded"
#define HoldKeyPrefix "holded_"

}

Expand Down
52 changes: 44 additions & 8 deletions plugins/tray/system-trays/systemtrayscontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<DockDaemonInter *>(sender())
// TODO: notify all plugins to reload plugin settings
}

bool SystemTraysController::eventFilter(QObject *o, QEvent *e)
{
if (o != qApp)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion plugins/tray/system-trays/systemtrayscontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
#include "systemtrayitem.h"
#include "pluginproxyinterface.h"

#include <com_deepin_dde_daemon_dock.h>

#include <QPluginLoader>
#include <QList>
#include <QMap>
#include <QDBusConnectionInterface>

using DockDaemonInter = com::deepin::dde::daemon::Dock;

class PluginsItemInterface;
class SystemTraysController : public QObject, PluginProxyInterface
{
Expand Down Expand Up @@ -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;
Expand All @@ -77,8 +82,9 @@ private slots:
private:
QDBusConnectionInterface *m_dbusDaemonInterface;
QMap<PluginsItemInterface *, QMap<QString, SystemTrayItem *>> m_pluginsMap;
DockDaemonInter *m_dockDaemonInter;

QSettings m_pluginsSetting;
QJsonObject m_pluginSettingsObject;
};

#endif // SYSTEMTRAYSCONTROLLER_H

0 comments on commit 57f8e6e

Please sign in to comment.