fixed plugins unloaded when cache is updated

This commit is contained in:
Patrick von Reth 2014-01-11 18:41:28 +01:00
parent a43234f115
commit 072736d832
5 changed files with 25 additions and 25 deletions

View File

@ -45,37 +45,29 @@ QSettings &PluginContainer::cacheFile(){
PluginContainer::PluginContainer(QString fileName, QString pluginName, PluginContainer::PluginType type): PluginContainer::PluginContainer(QString fileName, QString pluginName, PluginContainer::PluginType type):
m_pluginFile(fileName), m_pluginFile(fileName),
m_pluginName(pluginName), m_pluginName(pluginName),
m_pluginType(type) m_pluginType(type),
m_loader(SnoreCorePrivate::pluginDir().absoluteFilePath(file()))
{ {
} }
PluginContainer::~PluginContainer() PluginContainer::~PluginContainer()
{ {
if(m_instance) m_loader.unload();
{
m_instance->deleteLater();
}
} }
SnorePlugin *PluginContainer::load() SnorePlugin *PluginContainer::load()
{ {
if(m_instance != NULL) if ( !m_loader.load())
return m_instance;
QPluginLoader loader ( SnoreCorePrivate::pluginDir().absoluteFilePath(file()));
qDebug() << "Trying to load" << file();
if ( !loader.load())
{ {
qDebug() << "Failed loading plugin: " << loader.errorString(); qDebug() << "Failed loading plugin: " << m_loader.errorString();
return NULL; return NULL;
} }
return qobject_cast<SnorePlugin*> ( m_loader.instance());
m_instance = qobject_cast<SnorePlugin*> ( loader.instance());
return m_instance;
} }
void PluginContainer::unload() void PluginContainer::unload()
{ {
m_instance->deleteLater(); m_loader.unload();
} }
const QString & PluginContainer::file() const QString & PluginContainer::file()
@ -151,13 +143,13 @@ void PluginContainer::updatePluginCache(){
} }
PluginContainer *info = new PluginContainer( SnoreCorePrivate::pluginDir().relativeFilePath(filepath),sp->name(),PluginContainer::typeFromString(type)); PluginContainer *info = new PluginContainer( SnoreCorePrivate::pluginDir().relativeFilePath(filepath),sp->name(),PluginContainer::typeFromString(type));
s_pluginCache.insert(info->name(),info); s_pluginCache.insert(info->name(),info);
sp->deleteLater();
qDebug() << "added" << info->name() << "to cache"; qDebug() << "added" << info->name() << "to cache";
} }
} }
qDebug()<<s_pluginCache.keys(); qDebug()<<s_pluginCache.keys();
cache.setValue("version",Version::revision()); cache.setValue("version",Version::revision());
cache.setValue("buildtime",Version::buildTime());
cache.setValue("pluginPath",SnoreCorePrivate::pluginDir().path()); cache.setValue("pluginPath",SnoreCorePrivate::pluginDir().path());
QList<PluginContainer*> plugins = s_pluginCache.values(); QList<PluginContainer*> plugins = s_pluginCache.values();
cache.beginWriteArray("plugins"); cache.beginWriteArray("plugins");
@ -176,8 +168,12 @@ QHash<QString, PluginContainer *> PluginContainer::pluginCache(){
QSettings &cache = cacheFile(); QSettings &cache = cacheFile();
QString version = cache.value("version").toString(); QString version = cache.value("version").toString();
QString path = cache.value("pluginPath").toString(); QString path = cache.value("pluginPath").toString();
QString buildTime = cache.value("buildtime").toString();
int size = cache.beginReadArray("plugins"); int size = cache.beginReadArray("plugins");
if(size == 0 || version != Version::revision() || path != SnoreCorePrivate::pluginDir().path()){ if(size == 0 ||
version != Version::revision() ||
buildTime != Version::buildTime() ||
path != SnoreCorePrivate::pluginDir().path()){
cache.endArray(); cache.endArray();
updatePluginCache(); updatePluginCache();
}else{ }else{

View File

@ -24,6 +24,8 @@
#include <QPointer> #include <QPointer>
#include <QSettings> #include <QSettings>
#include <QFlag> #include <QFlag>
#include <QPluginLoader>
namespace Snore{ namespace Snore{
class SnoreCore; class SnoreCore;
@ -63,10 +65,10 @@ private:
static QHash<QString,PluginContainer*> s_pluginCache; static QHash<QString,PluginContainer*> s_pluginCache;
QPointer<SnorePlugin> m_instance;
QString m_pluginFile; QString m_pluginFile;
QString m_pluginName; QString m_pluginName;
PluginContainer::PluginType m_pluginType; PluginContainer::PluginType m_pluginType;
QPluginLoader m_loader;
}; };
} }

View File

@ -63,7 +63,7 @@ void SnoreCore::loadPlugins( PluginContainer::PluginTypes types )
{ {
case PluginContainer::BACKEND: case PluginContainer::BACKEND:
{ {
qDebug() <<info->name()<<"is a Notification_Backend"; qDebug() << info->name() << "is a Notification_Backend";
d->m_notificationBackends.append( info->name()); d->m_notificationBackends.append( info->name());
break; break;
} }
@ -78,7 +78,7 @@ void SnoreCore::loadPlugins( PluginContainer::PluginTypes types )
} }
case PluginContainer::FRONTEND: case PluginContainer::FRONTEND:
{ {
qDebug() <<info->name()<<"is a Notification_Frontend"; qDebug() << info->name() << "is a Notification_Frontend";
if(!info->load()->init( this )){ if(!info->load()->init( this )){
info->unload(); info->unload();
break; break;
@ -170,14 +170,17 @@ const QStringList &SnoreCore::secondaryNotificationBackends() const
bool SnoreCore::setPrimaryNotificationBackend ( const QString &backend ) bool SnoreCore::setPrimaryNotificationBackend ( const QString &backend )
{ {
Q_D(SnoreCore); Q_D(SnoreCore);
if(!PluginContainer::pluginCache().contains(backend)){ if(!PluginContainer::pluginCache().contains(backend))
{
qDebug()<<"Unknown Backend:"<<backend; qDebug()<<"Unknown Backend:"<<backend;
return setPrimaryNotificationBackend(); return setPrimaryNotificationBackend();
} }
qDebug() << "Setting Notification Backend to:" << backend; qDebug() << "Setting Notification Backend to:" << backend;
SnoreBackend* b = qobject_cast<SnoreBackend*>(PluginContainer::pluginCache()[backend]->load()); SnoreBackend* b = qobject_cast<SnoreBackend*>(PluginContainer::pluginCache()[backend]->load());
if(!b->isInitialized()){ if(!b->isInitialized())
if(!b->init(this)){ {
if(!b->init(this))
{
qDebug() << "Failed to initialize" << b->name(); qDebug() << "Failed to initialize" << b->name();
return false; return false;
} }

View File

@ -40,7 +40,6 @@ const QDir &SnoreCorePrivate::pluginDir(){
{ {
path = QDir(LIBSNORE_PLUGIN_PATH); path = QDir(LIBSNORE_PLUGIN_PATH);
} }
qDebug() << "PluginDir" << path.absolutePath();
return path; return path;
} }

View File

@ -15,7 +15,7 @@ int main ( int argc, char *argv[] )
#ifdef HAVE_KDE #ifdef HAVE_KDE
KAboutData about("SnoreNotify",0,ki18n("SnoreNotify"),Snore::Version::version().toLatin1(), KAboutData about("SnoreNotify",0,ki18n("SnoreNotify"),Snore::Version::version().toLatin1(),
ki18n("A notification deamon."),KAboutData::License_LGPL_V3, ki18n("Copyright (c) 2010-2013-2014 Patrick von Reth <vonreth@kde.org>")); ki18n("A notification deamon."),KAboutData::License_LGPL_V3, ki18n("Copyright (c) 2010-2014 Patrick von Reth <vonreth@kde.org>"));
KCmdLineArgs::init(argc, argv, &about); KCmdLineArgs::init(argc, argv, &about);
KUniqueApplication app; KUniqueApplication app;
#else #else