refactord init, delete plugins which cant be initialized"

This commit is contained in:
Patrick von Reth 2012-01-26 01:12:39 +01:00
parent 1f66bb6dab
commit 57cb02094e
10 changed files with 68 additions and 43 deletions

View File

@ -23,52 +23,58 @@
namespace Snore{
SnorePlugin::SnorePlugin ( QString name ) :
_name ( name )
m_name ( name ),
m_initialized(false)
{}
SnorePlugin::~SnorePlugin()
{
qDebug()<<_name<<this<<"deleted";
_snore->deleteLater();
qDebug()<<m_name<<this<<"deleted";
}
void SnorePlugin::init( SnoreServer *snore )
bool SnorePlugin::init( SnoreServer *snore )
{
this->_snore = snore;
qDebug()<<"Initialize"<<_name<<this<<snore;
qDebug()<<"Initialize"<<m_name<<this<<snore;
this->m_snore = snore;
m_initialized = true;
return true;
}
bool SnorePlugin::isInitialized(){
return m_initialized;
}
SnoreServer* SnorePlugin::snore()
{
return _snore.data();
return m_snore.data();
}
const QString &SnorePlugin::name() const
{
return _name;
return m_name;
}
void SnorePlugin::startTimeout(uint id,int timeout){
if(timeout==-1)//sticky
return;
if(timeouts.contains(id)){
QTimer *t = timeouts.take(id);
if(m_timeouts.contains(id)){
QTimer *t = m_timeouts.take(id);
t->stop();
t->deleteLater();
timeout_order.removeOne(id);
m_timeout_order.removeOne(id);
}
QTimer *timer= new QTimer(this);
timer->setInterval(timeout*1000);
timer->setSingleShot(true);
timeouts.insert(id,timer);
timeout_order.append(id);
m_timeouts.insert(id,timer);
m_timeout_order.append(id);
connect(timer,SIGNAL(timeout()),this,SLOT(notificationTimedOut()));
timer->start();
}
void SnorePlugin::notificationTimedOut(){
uint id = timeout_order.takeFirst();
timeouts.take(id)->deleteLater();
uint id = m_timeout_order.takeFirst();
m_timeouts.take(id)->deleteLater();
if(activeNotifications.contains(id)){
Notification n = activeNotifications.take(id);
snore()->closeNotification(n,NotificationEnums::CloseReasons::TIMED_OUT);
@ -85,14 +91,17 @@ Notification_Backend::~Notification_Backend()
{
}
void Notification_Backend::init( SnoreServer *snore )
bool Notification_Backend::init( SnoreServer *snore )
{
SnorePlugin::init(snore);
if(!SnorePlugin::init(snore))
return false;
connect( snore,SIGNAL( closeNotify( Snore::Notification ) ),this,SLOT( closeNotification( Snore::Notification) ) );
connect( snore,SIGNAL( applicationInitialized( Snore::Application* ) ),this,SLOT( registerApplication( Snore::Application* ) ) );
connect( snore,SIGNAL( applicationRemoved( Snore::Application* ) ),this,SLOT( unregisterApplication( Snore::Application* ) ) );
if(!isPrimaryNotificationBackend())
connect( snore,SIGNAL( notify(Snore::Notification) ),this,SLOT( notify( Snore::Notification ) ) );
return true;
}
Notification_Frontend::Notification_Frontend ( QString name ) :
@ -104,10 +113,12 @@ Notification_Frontend::Notification_Frontend ( QString name ) :
Notification_Frontend::~Notification_Frontend()
{
}
void Notification_Frontend::init( SnoreServer *snore )
bool Notification_Frontend::init( SnoreServer *snore )
{
SnorePlugin::init(snore);
if(!SnorePlugin::init(snore))
return false;
connect( snore,SIGNAL ( closeNotify( Snore::Notification ) ),this,SLOT ( notificationClosed( Snore::Notification) ) );
return true;
}
}
#include "interface.moc"

View File

@ -31,7 +31,8 @@ class SNORE_EXPORT SnorePlugin:public QObject
public:
SnorePlugin ( QString name);
virtual ~SnorePlugin();
virtual void init( SnoreServer* snore );
virtual bool init( SnoreServer* snore );
bool isInitialized();
SnoreServer* snore();
const QString &name() const;
@ -43,10 +44,11 @@ private slots:
private:
SnorePlugin() {}
QString _name;
QPointer<SnoreServer> _snore;
QHash<uint,QTimer*> timeouts;
QList<uint> timeout_order;
QString m_name;
bool m_initialized;
QPointer<SnoreServer> m_snore;
QHash<uint,QTimer*> m_timeouts;
QList<uint> m_timeout_order;
};
@ -64,7 +66,7 @@ class SNORE_EXPORT Notification_Backend:public SnorePlugin
public:
Notification_Backend ( QString name );
virtual ~Notification_Backend();
virtual void init(SnoreServer *snore);
virtual bool init(SnoreServer *snore);
virtual bool isPrimaryNotificationBackend() =0;
@ -87,7 +89,7 @@ class SNORE_EXPORT Notification_Frontend:public SnorePlugin
public:
Notification_Frontend ( QString name);
virtual ~Notification_Frontend();
virtual void init(SnoreServer *snore);
virtual bool init(SnoreServer *snore);
public slots:
virtual void actionInvoked( Snore::Notification notification )=0;

View File

@ -38,6 +38,7 @@ public:
{}
SnoreIconData(const QString &url){
qDebug()<<"Creating SnoreIcon"<<url;
if(url.startsWith(":/")){
_img = QImage(url);
_isLocalFile = false;

View File

@ -25,6 +25,7 @@
#include <QDebug>
#include <QDir>
#include <QSystemTrayIcon>
#include <QApplication>
namespace Snore{
@ -95,7 +96,10 @@ void SnoreServer::publicatePlugin ( SnorePlugin *plugin )
m_notificationBackend = nb;
}
}else{
nb->init( this );
if(!nb->init( this )){
nb->deleteLater();
return;
}
}
m_notyfier.insert ( pluginName,nb );
@ -103,9 +107,10 @@ void SnoreServer::publicatePlugin ( SnorePlugin *plugin )
Notification_Frontend * nf = qobject_cast<Notification_Frontend*> ( plugin );
if(nf != NULL){
qDebug() <<pluginName<<"is a Notification_Frontend";
nf->init( this );
if(nf != NULL)
if(nf->init( this ))
m_frontends.insert(nf->name(),nf);
else
nf->deleteLater();
}
}
}
@ -116,6 +121,10 @@ uint SnoreServer::broadcastNotification ( Notification notification )
emit notify ( notification );
if ( m_notificationBackend != NULL )
{
if(!m_notificationBackend->isInitialized()){
qDebug()<<"Notification backend "<<m_notificationBackend<<" isnt initialized will snore will exit now";
qApp->quit();
}
notification.setId(m_notificationBackend->notify( notification ));
return notification.id();
}

View File

@ -40,17 +40,16 @@ FreedesktopNotification_Frontend::FreedesktopNotification_Frontend():
FreedesktopNotification_Frontend::~FreedesktopNotification_Frontend(){
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.unregisterService( "org.freedesktop.Notifications" );
qDebug()<<"FreedesktopNotification_Frontend"<<"bye";
}
void FreedesktopNotification_Frontend::init(SnoreServer *snore){
Notification_Frontend::init(snore);
qDebug()<<"arg"<<snore<<this;
bool FreedesktopNotification_Frontend::init(SnoreServer *snore){
if(!Notification_Frontend::init(snore))
return false;
new NotificationsAdaptor(this);
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.registerService( "org.freedesktop.Notifications" );
dbus.registerObject( "/org/freedesktop/Notifications", this );
return true;
}
void FreedesktopNotification_Frontend::actionInvoked(Notification notification) {

View File

@ -25,7 +25,7 @@ class FreedesktopNotification_Frontend:public Snore::Notification_Frontend{
public:
FreedesktopNotification_Frontend();
~FreedesktopNotification_Frontend();
virtual void init(Snore::SnoreServer *snore);
virtual bool init(Snore::SnoreServer *snore);
void actionInvoked(Snore::Notification notification);
void notificationClosed(Snore::Notification notification);

View File

@ -51,13 +51,15 @@ Snarl_Backend::~Snarl_Backend()
}
void Snarl_Backend::init(SnoreServer *snore){
Notification_Backend::init(snore);
bool Snarl_Backend::init(SnoreServer *snore){
if(!Notification_Backend::init(snore))
return false;
winIDWidget = new SnarlWidget(this);
SnarlInterface *snarlInterface = new SnarlInterface();
_applications.insert("SnoreNotify",snarlInterface);
qDebug()<<"Initiating Snarl Backend, Snarl version: "<<snarlInterface->GetVersion();
_defautSnarlinetrface = new SnarlInterface();
return true;
}
void Snarl_Backend::registerApplication(Application *application){

View File

@ -35,7 +35,7 @@ class Snarl_Backend:public Snore::Notification_Backend
public:
Snarl_Backend();
~Snarl_Backend();
virtual void init(Snore::SnoreServer *snore);
virtual bool init(Snore::SnoreServer *snore);
bool isPrimaryNotificationBackend();
private:

View File

@ -34,20 +34,21 @@ SnarlNetworkFrontend::SnarlNetworkFrontend():
}
SnarlNetworkFrontend::~SnarlNetworkFrontend(){
delete parser;
delete tcpServer;
}
void SnarlNetworkFrontend::init(SnoreServer *snore){
Notification_Frontend::init(snore);
bool SnarlNetworkFrontend::init(SnoreServer *snore){
if(!Notification_Frontend::init(snore))
return false;
parser=new Parser(this);
tcpServer=new QTcpServer(this);
if(!tcpServer->listen(QHostAddress::Any,port)){
qDebug()<<"The port is already used";
return false;
}else{
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(handleConnection()));
std::cout<<"The Snarl Network Protokoll is developed for Snarl <http://www.fullphat.net/>"<<std::endl;
}
return true;
}

View File

@ -45,7 +45,7 @@ public:
public:
SnarlNetworkFrontend();
~SnarlNetworkFrontend();
virtual void init(Snore::SnoreServer *snore);
virtual bool init(Snore::SnoreServer *snore);
void actionInvoked(Snore::Notification notification);
void notificationClosed(Snore::Notification notification);