some cleaning up

This commit is contained in:
Patrick von Reth 2013-07-23 01:10:04 +02:00
parent 17d899f151
commit bfe963cb64
7 changed files with 99 additions and 104 deletions

View File

@ -18,9 +18,10 @@
*/ */
#include "application.h" #include "application.h"
namespace Snore{
using namespace Snore;
Application::Application (const QString &name, const Icon &icon) : Application::Application (const QString &name, const Icon &icon) :
m_name ( name ), m_name ( name ),
m_icon(icon), m_icon(icon),
@ -34,14 +35,11 @@ Application::Application() :
Application::~Application() Application::~Application()
{ {
foreach ( Alert *a,m_alerts )
{
a->deleteLater();
}
} }
void Application::addAlert ( Alert *alert ) void Application::addAlert ( Alert *alert )
{ {
alert->setParent(this);
m_alerts.insert ( alert->name(),alert ); m_alerts.insert ( alert->name(),alert );
} }
@ -102,4 +100,3 @@ bool Alert::isActive() const
return m_active; return m_active;
} }
}

View File

@ -70,6 +70,9 @@ public:
QString m_hash; QString m_hash;
bool m_isLocalFile; bool m_isLocalFile;
private:
Q_DISABLE_COPY(IconData)
}; };
} }

View File

@ -23,34 +23,36 @@
#include "snorebackend.h" #include "snorebackend.h"
#include "snorefrontend.h" #include "snorefrontend.h"
#include <QTimer>
#include <QPluginLoader> #include <QPluginLoader>
#include <QDir> #include <QDir>
#include <QDebug> #include <QDebug>
namespace Snore{ using namespace Snore;
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)
{ {
} }
PluginContainer::~PluginContainer(){ PluginContainer::~PluginContainer()
{
if(m_instance) if(m_instance)
{
m_instance->deleteLater(); m_instance->deleteLater();
}
} }
SnorePlugin *PluginContainer::load(){ SnorePlugin *PluginContainer::load()
{
if(m_instance != NULL) if(m_instance != NULL)
return m_instance; return m_instance;
QPluginLoader loader ( SnoreCore::pluginDir().absoluteFilePath(file())); QPluginLoader loader ( SnoreCore::pluginDir().absoluteFilePath(file()));
qDebug()<<"Trying to load"<<file(); qDebug() << "Trying to load" << file();
if ( !loader.load()) if ( !loader.load())
{ {
qDebug() <<"Failed loading plugin: "<<loader.errorString(); qDebug() << "Failed loading plugin: " << loader.errorString();
return NULL; return NULL;
} }
@ -73,23 +75,32 @@ PluginContainer::PluginType PluginContainer::type()
return m_pluginType; return m_pluginType;
} }
PluginContainer::PluginType PluginContainer::typeFromString(const QString &t){ PluginContainer::PluginType PluginContainer::typeFromString(const QString &t)
{
PluginContainer::PluginType type = PLUGIN;
if(t == QLatin1String("backend")) if(t == QLatin1String("backend"))
return BACKEND; {
if(t == QLatin1String("secondary_backend")) type = BACKEND;
return SECONDARY_BACKEND;
if(t == QLatin1String("frontend"))
return FRONTEND;
return PLUGIN;
}
const QStringList &PluginContainer::types(){
static QStringList *list = NULL;
if(list == NULL){
list = new QStringList();
*list<<"backend"<<"secondary_backend"<<"frontend"<<"plugin";
} }
return *list; else if(t == QLatin1String("secondary_backend"))
{
type = SECONDARY_BACKEND;
}
else if(t == QLatin1String("frontend"))
{
type = FRONTEND;
}
return type;
} }
const QStringList &PluginContainer::types()
{
static QStringList list;
if(list.isEmpty()){
list << "backend"
<< "secondary_backend"
<< "frontend"
<< "plugin";
}
return list;
} }

View File

@ -35,18 +35,17 @@
#include <QApplication> #include <QApplication>
#include <QSettings> #include <QSettings>
namespace Snore{ using namespace Snore;
QHash<QString,PluginContainer*> SnoreCore::s_pluginCache = QHash<QString,PluginContainer*>() ; QHash<QString,PluginContainer*> SnoreCore::s_pluginCache = QHash<QString,PluginContainer*>() ;
QDir *SnoreCore::s_pluginDir = NULL; QSettings &SnoreCore::cacheFile(){
QSettings *SnoreCore::cacheFile(){
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
return new QSettings("TheOneRing","libsnore"); static QSettings cache("TheOneRing","libsnore");
#else #else
return new QSettings(SnoreCore::pluginDir().absoluteFilePath("plugin.cache"),QSettings::IniFormat); static QSettings cache(SnoreCore::pluginDir().absoluteFilePath("plugin.cache"),QSettings::IniFormat);
#endif #endif
return cache;
} }
void SnoreCore::slotNotificationClosed(Notification n) void SnoreCore::slotNotificationClosed(Notification n)
@ -54,11 +53,6 @@ void SnoreCore::slotNotificationClosed(Notification n)
emit notificationClosed(n); emit notificationClosed(n);
} }
QString const SnoreCore::version(){
static QString ver(QString().append(Version::major()).append(".").append(Version::minor()).append(Version::suffix()));
return ver;
}
QString const SnoreCore::snoreTMP(){ QString const SnoreCore::snoreTMP(){
static const QString tmp = QString("%1/SnoreNotify/").arg(QDir::temp().path()); static const QString tmp = QString("%1/SnoreNotify/").arg(QDir::temp().path());
return tmp; return tmp;
@ -79,30 +73,30 @@ SnoreCore::SnoreCore ( QSystemTrayIcon *trayIcon ) :
QHash<QString, PluginContainer *> SnoreCore::pluginCache(){ QHash<QString, PluginContainer *> SnoreCore::pluginCache(){
if(!s_pluginCache.isEmpty()) if(!s_pluginCache.isEmpty())
return s_pluginCache; return s_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();
int size = cache->beginReadArray("plugins"); int size = cache.beginReadArray("plugins");
if(size == 0 || version != Version::revision() || path != pluginDir().path()){ if(size == 0 || version != Version::revision() || path != pluginDir().path()){
qDebug() << version << "!=" << Version::revision(); qDebug() << version << "!=" << Version::revision();
qDebug() << path << "!=" << pluginDir().path(); qDebug() << path << "!=" << pluginDir().path();
updatePluginCache(); updatePluginCache();
}else{ }else{
for(int i=0;i<size;++i) { for(int i=0;i<size;++i) {
cache->setArrayIndex(i); cache.setArrayIndex(i);
PluginContainer::PluginType type = (PluginContainer::PluginType)cache->value("type").toInt(); PluginContainer::PluginType type = (PluginContainer::PluginType)cache.value("type").toInt();
PluginContainer *info = new PluginContainer(cache->value("fileName").toString(),cache->value("name").toString(),type); PluginContainer *info = new PluginContainer(cache.value("fileName").toString(),cache.value("name").toString(),type);
s_pluginCache.insert(info->name(),info); s_pluginCache.insert(info->name(),info);
} }
cache->endArray(); cache.endArray();
} }
return s_pluginCache; return s_pluginCache;
} }
void SnoreCore::updatePluginCache(){ void SnoreCore::updatePluginCache(){
QSettings *cache = cacheFile(); QSettings &cache = cacheFile();
qDebug() << "Updating plugin cache" << cache->fileName(); qDebug() << "Updating plugin cache" << cache.fileName();
s_pluginCache.clear(); s_pluginCache.clear();
@ -132,57 +126,47 @@ void SnoreCore::updatePluginCache(){
} }
qDebug()<<s_pluginCache.keys(); qDebug()<<s_pluginCache.keys();
cache->setValue("version",Version::revision()); cache.setValue("version",Version::revision());
cache->setValue("pluginPath",pluginDir().path()); cache.setValue("pluginPath",pluginDir().path());
QList<PluginContainer*> plugins = s_pluginCache.values(); QList<PluginContainer*> plugins = s_pluginCache.values();
cache->beginWriteArray("plugins"); cache.beginWriteArray("plugins");
for(int i=0;i< plugins.size();++i) { for(int i=0;i< plugins.size();++i) {
cache->setArrayIndex(i); cache.setArrayIndex(i);
cache->setValue("fileName",plugins[i]->file()); cache.setValue("fileName",plugins[i]->file());
cache->setValue("name", plugins[i]->name()); cache.setValue("name", plugins[i]->name());
cache->setValue("type",(int)plugins[i]->type()); cache.setValue("type",(int)plugins[i]->type());
} }
cache->endArray(); cache.endArray();
} }
const QDir &SnoreCore::pluginDir(){ const QDir &SnoreCore::pluginDir(){
if(s_pluginDir == NULL) static QDir path(QString("%1/snoreplugins").arg(qApp->applicationDirPath()));
setPluginDir(); if(!path.exists())
qDebug()<<"SnorePluginDir:"<<s_pluginDir->path(); {
return *s_pluginDir; path = QDir(LIBSNORE_PLUGIN_PATH);
}
qDebug() << "PluginDir" << path.absolutePath();
return path;
} }
void SnoreCore::setPluginDir(const QString &path){
if(!path.isEmpty()){//path is not empty
QDir *tmp = new QDir(path);
if(tmp->exists()){//path exists
s_pluginDir = tmp;
}else{
delete tmp;
}
}
//if pluginpath is not initialized try snoreplugins/ in application dir
if(s_pluginDir == NULL){
s_pluginDir = new QDir(qApp->applicationDirPath()+"/snoreplugins");
qDebug()<<"PluginDir"<<s_pluginDir->absolutePath();
if(!s_pluginDir->exists())//still not existing? use the path defined on compile time
s_pluginDir = new QDir(LIBSNORE_PLUGIN_PATH);
}
}
void SnoreCore::loadPlugins ( PluginContainer::PluginTypes types ) void SnoreCore::loadPlugins ( PluginContainer::PluginTypes types )
{ {
qDebug()<<"PluginInfo"<<SnoreCore::pluginCache().keys(); qDebug() << "PluginInfo" << SnoreCore::pluginCache().keys();
foreach ( PluginContainer *info, SnoreCore::pluginCache().values()) foreach ( PluginContainer *info, SnoreCore::pluginCache().values())
{ {
if(types == PluginContainer::ALL or types.testFlag(info->type())){ if(types == PluginContainer::ALL or types.testFlag(info->type()))
switch(info->type()){ {
case PluginContainer::BACKEND:{ switch(info->type())
{
case PluginContainer::BACKEND:
{
qDebug() <<info->name()<<"is a Notification_Backend"; qDebug() <<info->name()<<"is a Notification_Backend";
m_notificationBackends.append( info->name()); m_notificationBackends.append( info->name());
break; break;
} }
case PluginContainer::SECONDARY_BACKEND:{ case PluginContainer::SECONDARY_BACKEND:
{
SnoreSecondaryBackend *nb = qobject_cast<SnoreSecondaryBackend *> ( info->load() ); SnoreSecondaryBackend *nb = qobject_cast<SnoreSecondaryBackend *> ( info->load() );
if(!nb->init( this )){ if(!nb->init( this )){
nb->deleteLater(); nb->deleteLater();
@ -191,7 +175,8 @@ void SnoreCore::loadPlugins ( PluginContainer::PluginTypes types )
m_secondaryNotificationBackends.append(info->name()); m_secondaryNotificationBackends.append(info->name());
break; break;
} }
case PluginContainer::FRONTEND:{ case PluginContainer::FRONTEND:
{
SnoreFrontend * nf = qobject_cast<SnoreFrontend*> (info->load()); SnoreFrontend * nf = qobject_cast<SnoreFrontend*> (info->load());
qDebug() <<info->name()<<"is a Notification_Frontend"; qDebug() <<info->name()<<"is a Notification_Frontend";
if(!nf->init( this )){ if(!nf->init( this )){
@ -201,7 +186,8 @@ void SnoreCore::loadPlugins ( PluginContainer::PluginTypes types )
m_Frontends.append(info->name()); m_Frontends.append(info->name());
break; break;
} }
case PluginContainer::PLUGIN:{ case PluginContainer::PLUGIN:
{
qDebug() <<info->name()<<"is a SnorePlugin"; qDebug() <<info->name()<<"is a SnorePlugin";
if(!info->load()->init(this)){ if(!info->load()->init(this)){
info->load()->deleteLater(); info->load()->deleteLater();
@ -210,9 +196,10 @@ void SnoreCore::loadPlugins ( PluginContainer::PluginTypes types )
m_plugins.append(info->name()); m_plugins.append(info->name());
break; break;
} }
default:{ default:
{
std::cerr<<"Plugin Cache corrupted"<<std::endl; std::cerr<<"Plugin Cache corrupted"<<std::endl;
std::cerr<<info->file().toLatin1().constData()<<QString::number((int)info->type()).toLatin1().constData()<<std::endl; std::cerr<<info->file().toLocal8Bit().constData()<<QString::number((int)info->type()).toLatin1().constData()<<std::endl;
} }
} }
}else{ }else{
@ -240,7 +227,7 @@ void SnoreCore::broadcastNotification ( Notification notification )
void SnoreCore::notificationActionInvoked ( Notification notification ) void SnoreCore::notificationActionInvoked ( Notification notification )
{ {
emit actionInvoked(notification); emit actionInvoked(notification);
SnoreFrontend *nf= notification.source(); SnoreFrontend *nf = notification.source();
if ( nf != NULL ) if ( nf != NULL )
{ {
nf->actionInvoked ( notification ); nf->actionInvoked ( notification );
@ -372,5 +359,3 @@ bool SnoreCore::primaryBackendSupportsRichtext()
{ {
return m_notificationBackend->supportsRichtext(); return m_notificationBackend->supportsRichtext();
} }
}

View File

@ -33,15 +33,13 @@ class QSettings;
namespace Snore{ namespace Snore{
class SNORE_EXPORT SnoreCore:public QObject class SNORE_EXPORT SnoreCore : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
static const QString version();
static const QString snoreTMP(); static const QString snoreTMP();
static void updatePluginCache(); static void updatePluginCache();
static const QDir &pluginDir(); static const QDir &pluginDir();
static void setPluginDir(const QString &path = "");
public: public:
SnoreCore (QSystemTrayIcon *trayIcon = NULL ); SnoreCore (QSystemTrayIcon *trayIcon = NULL );
@ -76,10 +74,9 @@ public:
private: private:
static QHash<QString,PluginContainer*> pluginCache(); static QHash<QString,PluginContainer*> pluginCache();
static QSettings *cacheFile(); static QSettings &cacheFile();
static QHash<QString,PluginContainer*> s_pluginCache; static QHash<QString,PluginContainer*> s_pluginCache;
static QDir *s_pluginDir;
ApplicationsList m_applications; ApplicationsList m_applications;

View File

@ -22,6 +22,7 @@
#include "plugins/backends/freedesktop/fredesktopnotification.h" #include "plugins/backends/freedesktop/fredesktopnotification.h"
#include "core/snore.h" #include "core/snore.h"
#include "core/version.h"
#include <QtCore> #include <QtCore>
#include <QtDBus> #include <QtDBus>
@ -126,16 +127,15 @@ QStringList FreedesktopFrontend::GetCapabilities()
return QStringList() return QStringList()
<< "body" << "body"
// << "body-hyperlinks" // << "body-hyperlinks"
// << "body-markup" << "body-markup"
<< "icon-static" << "icon-static"
<< "actions" << "actions";
;
} }
QString FreedesktopFrontend::GetServerInformation(QString& vendor, QString& version, QString& specVersion) QString FreedesktopFrontend::GetServerInformation(QString& vendor, QString& version, QString& specVersion)
{ {
vendor = "Snore"; vendor = "Snore";
version = snore()->version(); version = Version::version();
specVersion = "0"; specVersion = "0";
return "Snore"; return "Snore";
} }

View File

@ -24,6 +24,8 @@
#include <QMenu> #include <QMenu>
#include <QAction> #include <QAction>
#include "core/version.h"
using namespace Snore; using namespace Snore;
TrayIcon::TrayIcon() TrayIcon::TrayIcon()
@ -36,7 +38,7 @@ void TrayIcon::initConextMenu(SnoreCore *snore){
_trayIcon->setVisible(true); _trayIcon->setVisible(true);
_trayMenu = new QMenu("SnoreNotify"); _trayMenu = new QMenu("SnoreNotify");
_trayMenu->addAction(QString("SnoreNotify ").append(_snore->version())); _trayMenu->addAction(QString("SnoreNotify ").append(Version::version()));
_trayMenu->addSeparator(); _trayMenu->addSeparator();
foreach(const QString &back,_snore->notificationBackends()){ foreach(const QString &back,_snore->notificationBackends()){
QAction *b= new QAction(back,this); QAction *b= new QAction(back,this);