some refactoring

This commit is contained in:
Patrick von Reth 2013-07-24 14:55:18 +02:00
parent 3baca0e14f
commit 552400b46c
4 changed files with 96 additions and 93 deletions

View File

@ -22,12 +22,25 @@
#include "plugins.h"
#include "snorebackend.h"
#include "snorefrontend.h"
#include "../version.h"
#include <QDir>
#include <QDebug>
using namespace Snore;
QHash<QString,PluginContainer*> PluginContainer::s_pluginCache = QHash<QString,PluginContainer*>() ;
QSettings &PluginContainer::cacheFile(){
#ifdef Q_OS_LINUX
static QSettings cache("TheOneRing","libsnore");
#else
static QSettings cache(SnoreCore::pluginDir().absoluteFilePath("plugin.cache"),QSettings::IniFormat);
#endif
return cache;
}
PluginContainer::PluginContainer(QString fileName, QString pluginName, PluginContainer::PluginType type):
m_pluginFile(fileName),
m_pluginName(pluginName),
@ -103,3 +116,73 @@ const QStringList &PluginContainer::types()
}
return list;
}
void PluginContainer::updatePluginCache(){
QSettings &cache = cacheFile();
qDebug() << "Updating plugin cache" << cache.fileName();
s_pluginCache.clear();
cache.clear();
foreach(const QString &type,PluginContainer::types()){
QDir plPath(SnoreCore::pluginDir().absoluteFilePath(type));
qDebug() << "Searching for plugins in" << plPath.path();
foreach (QString fileName, plPath.entryList(QDir::Files)) {
QString filepath(plPath.absoluteFilePath(fileName));
qDebug() << "adding" << filepath;
QPluginLoader loader(filepath);
QObject *plugin = loader.instance();
if (plugin == NULL) {
qDebug() << "Failed loading plugin: " << filepath << loader.errorString();
continue;
}
SnorePlugin *sp = dynamic_cast<SnorePlugin*>(plugin);
if(sp == NULL){
qDebug() << "Error:" << fileName << " is not a Snore plugin" ;
plugin->deleteLater();
continue;
}
PluginContainer *info = new PluginContainer( SnoreCore::pluginDir().relativeFilePath(filepath),sp->name(),PluginContainer::typeFromString(type));
s_pluginCache.insert(info->name(),info);
delete sp;
qDebug() << "added" << info->name() << "to cache";
}
}
qDebug()<<s_pluginCache.keys();
cache.setValue("version",Version::revision());
cache.setValue("pluginPath",SnoreCore::pluginDir().path());
QList<PluginContainer*> plugins = s_pluginCache.values();
cache.beginWriteArray("plugins");
for(int i=0;i< plugins.size();++i) {
cache.setArrayIndex(i);
cache.setValue("fileName",plugins[i]->file());
cache.setValue("name", plugins[i]->name());
cache.setValue("type",(int)plugins[i]->type());
}
cache.endArray();
}
QHash<QString, PluginContainer *> PluginContainer::pluginCache(){
if(!s_pluginCache.isEmpty())
return s_pluginCache;
QSettings &cache = cacheFile();
QString version = cache.value("version").toString();
QString path = cache.value("pluginPath").toString();
int size = cache.beginReadArray("plugins");
if(size == 0 || version != Version::revision() || path != SnoreCore::pluginDir().path()){
cache.endArray();
updatePluginCache();
}else{
for(int i=0;i<size;++i) {
cache.setArrayIndex(i);
PluginContainer::PluginType type = (PluginContainer::PluginType)cache.value("type").toInt();
PluginContainer *info = new PluginContainer(cache.value("fileName").toString(),cache.value("name").toString(),type);
s_pluginCache.insert(info->name(),info);
}
cache.endArray();
}
return s_pluginCache;
}

View File

@ -22,6 +22,7 @@
#include "../snore_exports.h"
#include <QPointer>
#include <QSettings>
#include <QFlag>
namespace Snore{
@ -41,6 +42,9 @@ public:
PLUGIN = 0x8
};
Q_DECLARE_FLAGS(PluginTypes, PluginType)
static QHash<QString,PluginContainer*> pluginCache();
PluginContainer(QString fileName,QString pluginName,PluginType type);
~PluginContainer();
SnorePlugin *load();
@ -53,6 +57,11 @@ public:
static const QStringList &types();
private:
static void updatePluginCache();
static QSettings &cacheFile();
static QHash<QString,PluginContainer*> s_pluginCache;
QPointer<SnorePlugin> m_instance;
QString m_pluginFile;
QString m_pluginName;

View File

@ -36,17 +36,6 @@
using namespace Snore;
QHash<QString,PluginContainer*> SnoreCore::s_pluginCache = QHash<QString,PluginContainer*>() ;
QSettings &SnoreCore::cacheFile(){
#ifdef Q_OS_LINUX
static QSettings cache("TheOneRing","libsnore");
#else
static QSettings cache(SnoreCore::pluginDir().absoluteFilePath("plugin.cache"),QSettings::IniFormat);
#endif
return cache;
}
void SnoreCore::slotNotificationClosed(Notification n)
{
emit notificationClosed(n);
@ -69,77 +58,6 @@ SnoreCore::SnoreCore ( QSystemTrayIcon *trayIcon ) :
}
QHash<QString, PluginContainer *> SnoreCore::pluginCache(){
if(!s_pluginCache.isEmpty())
return s_pluginCache;
QSettings &cache = cacheFile();
QString version = cache.value("version").toString();
QString path = cache.value("pluginPath").toString();
int size = cache.beginReadArray("plugins");
if(size == 0 || version != Version::revision() || path != pluginDir().path()){
qDebug() << version << "!=" << Version::revision();
qDebug() << path << "!=" << pluginDir().path();
cache.endArray();
updatePluginCache();
}else{
for(int i=0;i<size;++i) {
cache.setArrayIndex(i);
PluginContainer::PluginType type = (PluginContainer::PluginType)cache.value("type").toInt();
PluginContainer *info = new PluginContainer(cache.value("fileName").toString(),cache.value("name").toString(),type);
s_pluginCache.insert(info->name(),info);
}
cache.endArray();
}
return s_pluginCache;
}
void SnoreCore::updatePluginCache(){
QSettings &cache = cacheFile();
qDebug() << "Updating plugin cache" << cache.fileName();
s_pluginCache.clear();
cache.clear();
foreach(const QString &type,PluginContainer::types()){
QDir plPath(SnoreCore::pluginDir().absoluteFilePath(type));
qDebug() << "Searching for plugins in" << plPath.path();
foreach (QString fileName, plPath.entryList(QDir::Files)) {
QString filepath(plPath.absoluteFilePath(fileName));
qDebug() << "adding" << filepath;
QPluginLoader loader(filepath);
QObject *plugin = loader.instance();
if (plugin == NULL) {
qDebug() << "Failed loading plugin: " << filepath << loader.errorString();
continue;
}
SnorePlugin *sp = dynamic_cast<SnorePlugin*>(plugin);
if(sp == NULL){
qDebug() << "Error:" << fileName << " is not a Snore plugin" ;
plugin->deleteLater();
continue;
}
PluginContainer *info = new PluginContainer( SnoreCore::pluginDir().relativeFilePath(filepath),sp->name(),PluginContainer::typeFromString(type));
s_pluginCache.insert(info->name(),info);
delete sp;
qDebug() << "added" << info->name() << "to cache";
}
}
qDebug()<<s_pluginCache.keys();
cache.setValue("version",Version::revision());
cache.setValue("pluginPath",pluginDir().path());
QList<PluginContainer*> plugins = s_pluginCache.values();
cache.beginWriteArray("plugins");
for(int i=0;i< plugins.size();++i) {
cache.setArrayIndex(i);
cache.setValue("fileName",plugins[i]->file());
cache.setValue("name", plugins[i]->name());
cache.setValue("type",(int)plugins[i]->type());
}
cache.endArray();
}
const QDir &SnoreCore::pluginDir(){
static QDir path(QString("%1/snoreplugins").arg(qApp->applicationDirPath()));
if(!path.exists())
@ -153,8 +71,8 @@ const QDir &SnoreCore::pluginDir(){
void SnoreCore::loadPlugins ( PluginContainer::PluginTypes types )
{
qDebug() << "PluginInfo" << SnoreCore::pluginCache().keys();
foreach ( PluginContainer *info, SnoreCore::pluginCache().values())
qDebug() << "PluginInfo" << PluginContainer::pluginCache().keys();
foreach ( PluginContainer *info, PluginContainer::pluginCache().values())
{
if(types == PluginContainer::ALL or types.testFlag(info->type()))
{
@ -276,12 +194,12 @@ const QStringList &SnoreCore::secondaryNotificationBackends() const
bool SnoreCore::setPrimaryNotificationBackend ( const QString &backend )
{
if(!pluginCache().contains(backend)){
if(!PluginContainer::pluginCache().contains(backend)){
qDebug()<<"Unknown Backend:"<<backend;
return false;
}
qDebug()<<"Setting Notification Backend to:"<<backend;
SnoreBackend* b = qobject_cast<SnoreBackend*>(pluginCache()[backend]->load());
SnoreBackend* b = qobject_cast<SnoreBackend*>(PluginContainer::pluginCache()[backend]->load());
if(!b->isInitialized()){
if(!b->init(this)){
qDebug()<<"Failed to initialize"<<b->name();

View File

@ -27,7 +27,6 @@
#include "hint.h"
#include <QStringList>
#include <QSettings>
#include <QTextDocument>
#include <QTextDocumentFragment>
@ -41,7 +40,6 @@ class SNORE_EXPORT SnoreCore : public QObject
Q_OBJECT
public:
static const QString snoreTMP();
static void updatePluginCache();
static const QDir &pluginDir();
public:
@ -86,11 +84,6 @@ private slots:
private:
static QHash<QString,PluginContainer*> pluginCache();
static QSettings &cacheFile();
static QHash<QString,PluginContainer*> s_pluginCache;
Hint m_hints;
ApplicationsList m_applications;