2017-09-18 14:33:44 +08:00
|
|
|
/*
|
2018-02-07 11:52:47 +08:00
|
|
|
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
2017-09-18 14:33:44 +08:00
|
|
|
*
|
|
|
|
* Author: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* Maintainer: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-06-16 16:56:21 +08:00
|
|
|
#include "dockpluginscontroller.h"
|
|
|
|
#include "pluginsiteminterface.h"
|
2016-06-23 16:43:22 +08:00
|
|
|
#include "dockitemcontroller.h"
|
2016-08-02 14:41:42 +08:00
|
|
|
#include "dockpluginloader.h"
|
2018-11-20 14:04:16 +08:00
|
|
|
#include "item/traypluginitem.h"
|
2016-06-16 16:56:21 +08:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
|
|
|
|
2018-12-04 16:13:17 +08:00
|
|
|
#define API_VERSION "1.1"
|
2017-10-23 11:53:44 +08:00
|
|
|
|
2018-11-08 11:39:26 +08:00
|
|
|
DockPluginsController::DockPluginsController(
|
|
|
|
DockItemController *itemControllerInter)
|
|
|
|
: QObject(itemControllerInter)
|
|
|
|
, m_dbusDaemonInterface(QDBusConnection::sessionBus().interface())
|
|
|
|
, m_itemControllerInter(itemControllerInter)
|
|
|
|
, m_pluginsSetting("deepin", "dde-dock")
|
2016-06-16 16:56:21 +08:00
|
|
|
{
|
2016-06-28 10:06:04 +08:00
|
|
|
qApp->installEventFilter(this);
|
2016-06-16 16:56:21 +08:00
|
|
|
}
|
|
|
|
|
2016-06-24 11:32:25 +08:00
|
|
|
void DockPluginsController::itemAdded(PluginsItemInterface * const itemInter, const QString &itemKey)
|
|
|
|
{
|
2016-06-28 19:35:19 +08:00
|
|
|
// check if same item added
|
|
|
|
if (m_pluginList.contains(itemInter))
|
2016-07-19 16:10:58 +08:00
|
|
|
if (m_pluginList[itemInter].contains(itemKey))
|
|
|
|
return;
|
2016-06-28 19:35:19 +08:00
|
|
|
|
2018-10-16 14:33:06 +08:00
|
|
|
PluginsItem *item = nullptr;
|
2018-11-20 14:04:16 +08:00
|
|
|
if (itemInter->pluginName() == "tray") {
|
|
|
|
item = new TrayPluginItem(itemInter, itemKey);
|
|
|
|
connect(static_cast<TrayPluginItem *>(item), &TrayPluginItem::fashionTraySizeChanged,
|
|
|
|
this, &DockPluginsController::fashionTraySizeChanged, Qt::UniqueConnection);
|
2018-10-16 14:33:06 +08:00
|
|
|
} else {
|
|
|
|
item = new PluginsItem(itemInter, itemKey);
|
|
|
|
}
|
|
|
|
|
2017-04-24 15:41:06 +08:00
|
|
|
item->setVisible(false);
|
|
|
|
|
2016-06-28 10:06:04 +08:00
|
|
|
m_pluginList[itemInter][itemKey] = item;
|
|
|
|
|
2016-06-24 11:32:25 +08:00
|
|
|
emit pluginItemInserted(item);
|
|
|
|
}
|
|
|
|
|
2016-06-28 10:06:04 +08:00
|
|
|
void DockPluginsController::itemUpdate(PluginsItemInterface * const itemInter, const QString &itemKey)
|
2016-06-27 14:33:21 +08:00
|
|
|
{
|
2016-06-28 10:06:04 +08:00
|
|
|
PluginsItem *item = pluginItemAt(itemInter, itemKey);
|
|
|
|
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
|
|
|
item->update();
|
2017-02-09 09:27:03 +08:00
|
|
|
|
|
|
|
emit pluginItemUpdated(item);
|
2016-06-27 14:33:21 +08:00
|
|
|
}
|
|
|
|
|
2016-06-28 17:48:02 +08:00
|
|
|
void DockPluginsController::itemRemoved(PluginsItemInterface * const itemInter, const QString &itemKey)
|
|
|
|
{
|
|
|
|
PluginsItem *item = pluginItemAt(itemInter, itemKey);
|
|
|
|
|
2016-06-28 19:35:19 +08:00
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
|
|
|
|
item->detachPluginWidget();
|
2016-06-28 17:48:02 +08:00
|
|
|
|
|
|
|
emit pluginItemRemoved(item);
|
|
|
|
|
|
|
|
m_pluginList[itemInter].remove(itemKey);
|
2017-11-10 16:47:58 +08:00
|
|
|
|
2018-10-31 15:46:22 +08:00
|
|
|
// do not delete the itemWidget object(specified in the plugin interface)
|
|
|
|
item->centralWidget()->setParent(nullptr);
|
|
|
|
|
|
|
|
// just delete our wrapper object(PluginsItem)
|
|
|
|
item->deleteLater();
|
2016-06-28 17:48:02 +08:00
|
|
|
}
|
|
|
|
|
2016-09-19 14:11:18 +08:00
|
|
|
void DockPluginsController::requestContextMenu(PluginsItemInterface * const itemInter, const QString &itemKey)
|
2016-08-08 19:19:11 +08:00
|
|
|
{
|
2016-09-19 14:11:18 +08:00
|
|
|
PluginsItem *item = pluginItemAt(itemInter, itemKey);
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
|
|
|
item->showContextMenu();
|
2016-08-08 19:19:11 +08:00
|
|
|
}
|
|
|
|
|
2018-12-03 18:16:39 +08:00
|
|
|
void DockPluginsController::requestWindowAutoHide(PluginsItemInterface * const itemInter, const QString &itemKey, const bool autoHide)
|
|
|
|
{
|
|
|
|
PluginsItem *item = pluginItemAt(itemInter, itemKey);
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
|
|
|
Q_EMIT item->requestWindowAutoHide(autoHide);
|
|
|
|
}
|
2016-07-14 16:12:28 +08:00
|
|
|
|
2018-12-04 09:50:56 +08:00
|
|
|
void DockPluginsController::requestRefreshWindowVisible(PluginsItemInterface * const itemInter, const QString &itemKey)
|
2018-12-03 18:16:39 +08:00
|
|
|
{
|
|
|
|
PluginsItem *item = pluginItemAt(itemInter, itemKey);
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
2018-12-04 09:50:56 +08:00
|
|
|
Q_EMIT item->requestRefreshWindowVisible();
|
2018-12-03 18:16:39 +08:00
|
|
|
}
|
2016-07-14 16:12:28 +08:00
|
|
|
|
2016-08-02 14:41:42 +08:00
|
|
|
void DockPluginsController::startLoader()
|
2016-06-16 16:56:21 +08:00
|
|
|
{
|
2016-08-02 14:41:42 +08:00
|
|
|
DockPluginLoader *loader = new DockPluginLoader(this);
|
|
|
|
|
|
|
|
connect(loader, &DockPluginLoader::finished, loader, &DockPluginLoader::deleteLater, Qt::QueuedConnection);
|
|
|
|
connect(loader, &DockPluginLoader::pluginFounded, this, &DockPluginsController::loadPlugin, Qt::QueuedConnection);
|
|
|
|
|
2017-04-20 17:22:19 +08:00
|
|
|
QTimer::singleShot(1, loader, [=] { loader->start(QThread::LowestPriority); });
|
2016-06-16 16:56:21 +08:00
|
|
|
}
|
2016-06-28 10:06:04 +08:00
|
|
|
|
|
|
|
void DockPluginsController::displayModeChanged()
|
|
|
|
{
|
|
|
|
const DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
|
2018-05-16 17:22:08 +08:00
|
|
|
const auto inters = m_pluginList.keys();
|
|
|
|
|
|
|
|
for (auto inter : inters)
|
2016-06-28 10:06:04 +08:00
|
|
|
inter->displayModeChanged(displayMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DockPluginsController::positionChanged()
|
|
|
|
{
|
|
|
|
const Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
|
2018-05-16 17:22:08 +08:00
|
|
|
const auto inters = m_pluginList.keys();
|
|
|
|
|
|
|
|
for (auto inter : inters)
|
2016-06-28 10:06:04 +08:00
|
|
|
inter->positionChanged(position);
|
|
|
|
}
|
|
|
|
|
2016-08-02 14:41:42 +08:00
|
|
|
void DockPluginsController::loadPlugin(const QString &pluginFile)
|
|
|
|
{
|
2017-11-08 14:51:11 +08:00
|
|
|
QPluginLoader *pluginLoader = new QPluginLoader(pluginFile);
|
2017-10-23 11:53:44 +08:00
|
|
|
const auto meta = pluginLoader->metaData().value("MetaData").toObject();
|
|
|
|
if (!meta.contains("api") || meta["api"].toString() != API_VERSION)
|
|
|
|
{
|
|
|
|
qWarning() << "plugin api version not matched!" << pluginFile;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-02 14:41:42 +08:00
|
|
|
PluginsItemInterface *interface = qobject_cast<PluginsItemInterface *>(pluginLoader->instance());
|
|
|
|
if (!interface)
|
|
|
|
{
|
2017-04-25 20:42:31 +08:00
|
|
|
qWarning() << "load plugin failed!!!" << pluginLoader->errorString() << pluginFile;
|
2016-08-02 14:41:42 +08:00
|
|
|
pluginLoader->unload();
|
|
|
|
pluginLoader->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-09 17:01:56 +08:00
|
|
|
m_pluginList.insert(interface, QMap<QString, PluginsItem *>());
|
2018-07-31 23:03:37 +08:00
|
|
|
|
|
|
|
QString dbusService = meta.value("depends-daemon-dbus-service").toString();
|
|
|
|
if (!dbusService.isEmpty() && !m_dbusDaemonInterface->isServiceRegistered(dbusService).value()) {
|
|
|
|
qDebug() << dbusService << "daemon has not started, waiting for signal";
|
|
|
|
connect(m_dbusDaemonInterface, &QDBusConnectionInterface::serviceOwnerChanged, this,
|
|
|
|
[=](const QString &name, const QString &oldOwner, const QString &newOwner) {
|
|
|
|
if (name == dbusService && !newOwner.isEmpty()) {
|
|
|
|
qDebug() << dbusService << "daemon started, init plugin and disconnect";
|
|
|
|
initPlugin(interface);
|
|
|
|
disconnect(m_dbusDaemonInterface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initPlugin(interface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DockPluginsController::initPlugin(PluginsItemInterface *interface) {
|
2017-03-27 14:44:31 +08:00
|
|
|
qDebug() << "init plugin: " << interface->pluginName();
|
2016-08-02 14:41:42 +08:00
|
|
|
interface->init(this);
|
2017-03-27 14:44:31 +08:00
|
|
|
qDebug() << "init plugin finished: " << interface->pluginName();
|
2016-08-02 14:41:42 +08:00
|
|
|
}
|
|
|
|
|
2016-06-28 10:06:04 +08:00
|
|
|
bool DockPluginsController::eventFilter(QObject *o, QEvent *e)
|
|
|
|
{
|
|
|
|
if (o != qApp)
|
|
|
|
return false;
|
|
|
|
if (e->type() != QEvent::DynamicPropertyChange)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QDynamicPropertyChangeEvent * const dpce = static_cast<QDynamicPropertyChangeEvent *>(e);
|
|
|
|
const QString propertyName = dpce->propertyName();
|
|
|
|
|
|
|
|
if (propertyName == PROP_POSITION)
|
|
|
|
positionChanged();
|
|
|
|
else if (propertyName == PROP_DISPLAY_MODE)
|
|
|
|
displayModeChanged();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginsItem *DockPluginsController::pluginItemAt(PluginsItemInterface * const itemInter, const QString &itemKey) const
|
|
|
|
{
|
|
|
|
if (!m_pluginList.contains(itemInter))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return m_pluginList[itemInter][itemKey];
|
|
|
|
}
|
2018-11-08 11:39:26 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
const QVariant DockPluginsController::getValue(PluginsItemInterface *const itemInter, const QString &itemKey, const QVariant& failback) {
|
|
|
|
m_pluginsSetting.beginGroup(itemInter->pluginName());
|
2018-12-05 14:45:44 +08:00
|
|
|
QVariant value(m_pluginsSetting.value(itemKey, failback));
|
2018-11-08 11:39:26 +08:00
|
|
|
m_pluginsSetting.endGroup();
|
2018-12-05 14:45:44 +08:00
|
|
|
return value;
|
2018-11-08 11:39:26 +08:00
|
|
|
}
|