mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-04 17:33:05 +00:00
load and unload plugin at anytime
This commit is contained in:
parent
aad6089479
commit
53afe99a3c
@ -28,26 +28,22 @@ Panel::Panel(QWidget *parent)
|
||||
this, SLOT(slotDockModeChanged(Dock::DockMode,Dock::DockMode)));
|
||||
|
||||
DockPluginManager *pluginManager = new DockPluginManager(this);
|
||||
|
||||
connect(DockModeData::instance(), &DockModeData::dockModeChanged,
|
||||
pluginManager, &DockPluginManager::onDockModeChanged);
|
||||
connect(pluginManager, &DockPluginManager::itemAdded, [=](AbstractDockItem* item) {
|
||||
rightLayout->addItem(item);
|
||||
});
|
||||
connect(pluginManager, &DockPluginManager::itemRemoved, [=](AbstractDockItem* item) {
|
||||
int index = rightLayout->indexOf(item);
|
||||
if (index != -1) {
|
||||
rightLayout->removeItem(index);
|
||||
}
|
||||
});
|
||||
|
||||
QList<DockPluginProxy*> proxies = pluginManager->getAll();
|
||||
foreach (DockPluginProxy* proxy, proxies) {
|
||||
connect(proxy, &DockPluginProxy::itemAdded, [=](AbstractDockItem* item) {
|
||||
rightLayout->addItem(item);
|
||||
});
|
||||
connect(proxy, &DockPluginProxy::itemRemoved, [=](AbstractDockItem* item) {
|
||||
int index = rightLayout->indexOf(item);
|
||||
if (index != -1) {
|
||||
rightLayout->removeItem(index);
|
||||
}
|
||||
});
|
||||
|
||||
proxy->plugin()->init(proxy);
|
||||
}
|
||||
pluginManager->initAll();
|
||||
|
||||
initAppManager();
|
||||
|
||||
initHSManager();
|
||||
initState();
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include <QDir>
|
||||
#include <QLibrary>
|
||||
#include <QPluginLoader>
|
||||
#include <QFileSystemWatcher>
|
||||
|
||||
#include "dockpluginmanager.h"
|
||||
#include "dockpluginproxy.h"
|
||||
@ -10,20 +12,28 @@ DockPluginManager::DockPluginManager(QObject *parent) :
|
||||
{
|
||||
m_searchPaths << "/usr/share/dde-dock/plugins/";
|
||||
|
||||
m_watcher = new QFileSystemWatcher(this);
|
||||
m_watcher->addPaths(m_searchPaths);
|
||||
|
||||
foreach (QString path, m_searchPaths) {
|
||||
QDir pluginsDir(path);
|
||||
|
||||
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
|
||||
QString pluginPath = pluginsDir.absoluteFilePath(fileName);
|
||||
DockPluginProxy * proxy = loadPlugin(pluginPath);
|
||||
m_proxies[pluginPath] = proxy;
|
||||
|
||||
this->loadPlugin(pluginPath);
|
||||
}
|
||||
}
|
||||
|
||||
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &DockPluginManager::watchedFileChanged);
|
||||
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &DockPluginManager::watchedDirectoryChanged);
|
||||
}
|
||||
|
||||
QList<DockPluginProxy*> DockPluginManager::getAll()
|
||||
void DockPluginManager::initAll()
|
||||
{
|
||||
return m_proxies.values();
|
||||
foreach (DockPluginProxy * proxy, m_proxies.values()) {
|
||||
proxy->plugin()->init(proxy);
|
||||
}
|
||||
}
|
||||
|
||||
// public slots
|
||||
@ -39,25 +49,77 @@ void DockPluginManager::onDockModeChanged(Dock::DockMode newMode,
|
||||
}
|
||||
|
||||
// private methods
|
||||
DockPluginProxy* DockPluginManager::loadPlugin(QString &path)
|
||||
DockPluginProxy * DockPluginManager::loadPlugin(const QString &path)
|
||||
{
|
||||
QPluginLoader pluginLoader(path);
|
||||
QObject *plugin = pluginLoader.instance();
|
||||
if (!QLibrary::isLibrary(path)) return NULL;
|
||||
|
||||
QPluginLoader * pluginLoader = new QPluginLoader(path);
|
||||
|
||||
// TODO: API version check;
|
||||
|
||||
QObject *plugin = pluginLoader->instance();
|
||||
|
||||
if (plugin) {
|
||||
DockPluginInterface * interface = qobject_cast<DockPluginInterface*>(plugin);
|
||||
|
||||
if (interface) {
|
||||
qDebug() << "Plugin loaded: " << path;
|
||||
|
||||
DockPluginProxy *proxy = new DockPluginProxy(interface);
|
||||
DockPluginProxy * proxy = new DockPluginProxy(pluginLoader, interface);
|
||||
if (proxy) {
|
||||
m_proxies[path] = proxy;
|
||||
m_watcher->addPath(path);
|
||||
|
||||
return proxy;
|
||||
connect(proxy, &DockPluginProxy::itemAdded, this, &DockPluginManager::itemAdded);
|
||||
connect(proxy, &DockPluginProxy::itemRemoved, this, &DockPluginManager::itemRemoved);
|
||||
|
||||
return proxy;
|
||||
}
|
||||
} else {
|
||||
qWarning() << "Load plugin failed(failed to convert) " << path;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
qWarning() << "Load plugin failed" << pluginLoader.errorString();
|
||||
qWarning() << "Load plugin failed" << pluginLoader->errorString();
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void DockPluginManager::unloadPlugin(const QString &path)
|
||||
{
|
||||
if (m_proxies.contains(path)) {
|
||||
DockPluginProxy * proxy = m_proxies.take(path);
|
||||
delete proxy;
|
||||
}
|
||||
}
|
||||
|
||||
// private slots
|
||||
void DockPluginManager::watchedFileChanged(const QString & file)
|
||||
{
|
||||
qDebug() << "DockPluginManager::watchedFileChanged" << file;
|
||||
this->unloadPlugin(file);
|
||||
|
||||
if (QFile::exists(file)) {
|
||||
DockPluginProxy * proxy = loadPlugin(file);
|
||||
|
||||
if (proxy) proxy->plugin()->init(proxy);
|
||||
}
|
||||
}
|
||||
|
||||
void DockPluginManager::watchedDirectoryChanged(const QString & directory)
|
||||
{
|
||||
qDebug() << "DockPluginManager::watchedDirectoryChanged" << directory;
|
||||
// we just need to take care of the situation that new files pop up in
|
||||
// our watched directory.
|
||||
QDir targetDir(directory);
|
||||
foreach (QString fileName, targetDir.entryList(QDir::Files)) {
|
||||
QString absPath = targetDir.absoluteFilePath(fileName);
|
||||
if (!m_proxies.contains(absPath)) {
|
||||
DockPluginProxy * proxy = loadPlugin(absPath);
|
||||
|
||||
if (proxy) proxy->plugin()->init(proxy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,9 @@
|
||||
#include <QStringList>
|
||||
|
||||
#include "dockconstants.h"
|
||||
#include "abstractdockitem.h"
|
||||
|
||||
class QFileSystemWatcher;
|
||||
class DockPluginProxy;
|
||||
class DockPluginManager : public QObject
|
||||
{
|
||||
@ -14,7 +16,11 @@ class DockPluginManager : public QObject
|
||||
public:
|
||||
explicit DockPluginManager(QObject *parent = 0);
|
||||
|
||||
QList<DockPluginProxy*> getAll();
|
||||
void initAll();
|
||||
|
||||
signals:
|
||||
void itemAdded(AbstractDockItem * item);
|
||||
void itemRemoved(AbstractDockItem * item);
|
||||
|
||||
public slots:
|
||||
void onDockModeChanged(Dock::DockMode newMode,
|
||||
@ -23,8 +29,14 @@ public slots:
|
||||
private:
|
||||
QStringList m_searchPaths;
|
||||
QMap<QString, DockPluginProxy*> m_proxies;
|
||||
QFileSystemWatcher * m_watcher;
|
||||
|
||||
DockPluginProxy* loadPlugin(QString &path);
|
||||
DockPluginProxy * loadPlugin(const QString & path);
|
||||
void unloadPlugin(const QString & path);
|
||||
|
||||
private slots:
|
||||
void watchedFileChanged(const QString & file);
|
||||
void watchedDirectoryChanged(const QString & directory);
|
||||
};
|
||||
|
||||
#endif // DOCKPLUGINMANAGER_H
|
||||
|
@ -1,13 +1,29 @@
|
||||
#include <QPluginLoader>
|
||||
|
||||
#include "dockpluginproxy.h"
|
||||
#include "pluginitemwrapper.h"
|
||||
#include "Controller/dockmodedata.h"
|
||||
|
||||
DockPluginProxy::DockPluginProxy(DockPluginInterface * plugin, QObject * parent) :
|
||||
QObject(parent),
|
||||
DockPluginProxy::DockPluginProxy(QPluginLoader * loader, DockPluginInterface * plugin) :
|
||||
QObject(),
|
||||
m_loader(loader),
|
||||
m_plugin(plugin)
|
||||
{
|
||||
}
|
||||
|
||||
DockPluginProxy::~DockPluginProxy()
|
||||
{
|
||||
foreach (AbstractDockItem * item, m_items) {
|
||||
emit itemRemoved(item);
|
||||
}
|
||||
m_items.clear();
|
||||
|
||||
m_loader->unload();
|
||||
m_loader->deleteLater();
|
||||
|
||||
qDebug() << "Plugin unloaded: " << m_loader->fileName();
|
||||
}
|
||||
|
||||
DockPluginInterface * DockPluginProxy::plugin()
|
||||
{
|
||||
return m_plugin;
|
||||
|
@ -5,11 +5,13 @@
|
||||
#include "dockpluginproxyinterface.h"
|
||||
#include "abstractdockitem.h"
|
||||
|
||||
class QPluginLoader;
|
||||
class DockPluginProxy : public QObject, public DockPluginProxyInterface
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
DockPluginProxy(DockPluginInterface * plugin, QObject * parent = 0);
|
||||
DockPluginProxy(QPluginLoader * loader, DockPluginInterface * plugin);
|
||||
~DockPluginProxy();
|
||||
|
||||
DockPluginInterface * plugin();
|
||||
|
||||
@ -25,6 +27,8 @@ signals:
|
||||
|
||||
private:
|
||||
QList<AbstractDockItem*> m_items;
|
||||
|
||||
QPluginLoader * m_loader;
|
||||
DockPluginInterface * m_plugin;
|
||||
|
||||
AbstractDockItem * getItem(QString uuid);
|
||||
|
@ -10,8 +10,6 @@ PluginItemWrapper::PluginItemWrapper(DockPluginInterface *plugin,
|
||||
{
|
||||
qDebug() << "PluginItemWrapper created " << m_plugin->name() << m_uuid;
|
||||
|
||||
// setStyleSheet("PluginItemWrapper { background-color: red } ");
|
||||
|
||||
if (m_plugin) {
|
||||
QWidget * item = m_plugin->getItem(uuid);
|
||||
m_pluginItemContents = m_plugin->getApplet(uuid);
|
||||
|
Loading…
x
Reference in New Issue
Block a user