diff --git a/src/core/hint.cpp b/src/core/hint.cpp index 55cd5d9..f15c1b7 100644 --- a/src/core/hint.cpp +++ b/src/core/hint.cpp @@ -17,6 +17,7 @@ along with SnoreNotify. If not, see . */ #include "hint.h" +#include "log.h" using namespace Snore; @@ -49,13 +50,21 @@ bool Hint::contains(const QString &key) const void Hint::setPrivateValue(const void *owner, const QString &key, const QVariant &value) { - m_privateData.insert(QPair(owner,key.toLower()), value); + m_privateData.insert(QPair((quintptr)owner,key.toLower()), value); +} + +void Hint::setPrivateValue(const void *owner, const QString &key, QObject *value) +{ + m_privateData.insert(QPair((quintptr)owner,key.toLower()), qVariantFromValue(value)); + value->setProperty("hint_key",key); + value->setProperty("hint_owner",(quintptr)owner); + connect(value, SIGNAL(destroyed()), this, SLOT(slotValueDestroyed())); } QVariant Hint::privateValue(const void *owner, const QString &k, const QVariant &defaultValue) const { - QPair key(owner,k.toLower()); + QPair key((quintptr)owner, k.toLower()); if(m_privateData.contains(key)) { return m_privateData.value(key); @@ -69,7 +78,23 @@ QVariant Hint::privateValue(const void *owner, const QString &k, const QVariant bool Hint::containsPrivateValue(const void *owner, const QString &key) const { - return m_privateData.contains(QPair(owner,key.toLower())); + return m_privateData.contains(QPair((quintptr)owner,key.toLower())); +} + +void Hint::slotValueDestroyed() +{ + QObject * o = sender(); + snoreDebug( SNORE_DEBUG ) << o << o->property("hint_key"); + QString key = o->property("hint_key").toString(); + if(!o->property("hint_owner").isNull()) + { + m_privateData.take(QPair(o->property("hint_owner").value(),key)); + } + else + { + m_data.take(key); + } + } QDebug operator<<( QDebug debug, const Snore::Hint &hint ) @@ -83,7 +108,7 @@ QDebug operator<<( QDebug debug, const Snore::Hint &hint ) } debug << "(" << it.key() << ", " << it.value(); } - for(QHash< QPair, QVariant>::const_iterator it = hint.m_privateData.constBegin();it != hint.m_privateData.constEnd();++it) + for(QHash< QPair, QVariant>::const_iterator it = hint.m_privateData.constBegin();it != hint.m_privateData.constEnd();++it) { if(it != hint.m_privateData.constBegin()) { diff --git a/src/core/hint.h b/src/core/hint.h index bb738c8..d2f7894 100644 --- a/src/core/hint.h +++ b/src/core/hint.h @@ -27,7 +27,7 @@ namespace Snore { - class Hint; +class Hint; } SNORE_EXPORT QDebug operator<< ( QDebug, const Snore::Hint &); @@ -39,8 +39,9 @@ namespace Snore * The keys are case insensitive. */ -class SNORE_EXPORT Hint +class SNORE_EXPORT Hint : public QObject { + Q_OBJECT public: Hint(); @@ -51,6 +52,13 @@ public: */ void setValue(const QString &key, const QVariant &value); + /** + * Sets the value for the key + * @param key the key + * @param value the value + */ + void setValue(const QString &key, QObject *value); + /** * The associated value of the key if present, returns the default value otherwise. * @param key the key @@ -73,6 +81,14 @@ public: */ void setPrivateValue(const void *owner, const QString &key, const QVariant &value); + /** + * Sets the value for the key depending on the owner + * @param owner the owner + * @param key the key + * @param value the value + */ + void setPrivateValue(const void *owner, const QString &key, QObject *value); + /** * The associated value of the key if present, returns the default value otherwise. * @param owner the owner @@ -88,49 +104,18 @@ public: * @return whether the key is set */ bool containsPrivateValue(const void *owner, const QString & key ) const; +private slots: + void slotValueDestroyed(); private: QVariantHash m_data; - QHash, QVariant> m_privateData; + QHash, QVariant> m_privateData; friend SNORE_EXPORT QDebug (::operator<<) ( QDebug, const Snore::Hint &); }; - -#if QT_VERSION >= QT_VERSION_CHECK(5,0,0) -/** - * Helper function to easyly stor pointers to QObjects in a QVariant - */ -template -inline Type myQVariantCast(const QVariant &dat) -{ - return qvariant_cast(dat); -} - -/** - * Helper function to easyly stor pointers to QObjects in a QVariant - */ -template -inline QVariant myQVariantFromValue(const Type &dat) -{ - return qVariantFromValue(dat); -} -#else -template -inline Type myQVariantCast(const QVariant &dat) -{ - return qobject_cast(qvariant_cast(dat)); -} - -template -inline QVariant myQVariantFromValue(const Type &dat) -{ - return qVariantFromValue(qobject_cast(dat)); -} -#endif - } diff --git a/src/core/log.cpp b/src/core/log.cpp index 3ef5cd1..0ef1baa 100644 --- a/src/core/log.cpp +++ b/src/core/log.cpp @@ -42,6 +42,7 @@ SnoreLog::~SnoreLog() if(debugLvl() >= m_lvl) { std::cout << m_msg.toUtf8().constData() << std::endl; + std::cout.flush(); } m_logg << m_msg.toUtf8().constData() << std::endl; } diff --git a/src/plugins/frontends/snarlnetwork/parser.cpp b/src/plugins/frontends/snarlnetwork/parser.cpp index 6e404cd..9e5b17d 100644 --- a/src/plugins/frontends/snarlnetwork/parser.cpp +++ b/src/plugins/frontends/snarlnetwork/parser.cpp @@ -138,13 +138,13 @@ void Parser::parse(Notification &sNotification,const QString &msg,QTcpSocket* cl } sNotification = Notification(app,alert,title,text,icon,timeout); sNotification.data()->setSource(snarl); - sNotification.hints().setPrivateValue(snarl, "clientSocket", myQVariantFromValue(client)); + sNotification.hints().setPrivateValue(snarl, "clientSocket", client); break; } case ADD_CLASS: if(alertName.isEmpty()) { - snoreDebug( SNORE_DEBUG )<<"Error registering alert with empty name"; + snoreDebug( SNORE_DEBUG ) << "Error registering alert with empty name"; break; } alert = Alert(alertName, icon); diff --git a/src/plugins/frontends/snarlnetwork/snarlnetwork.cpp b/src/plugins/frontends/snarlnetwork/snarlnetwork.cpp index 75d6178..0dee1fa 100644 --- a/src/plugins/frontends/snarlnetwork/snarlnetwork.cpp +++ b/src/plugins/frontends/snarlnetwork/snarlnetwork.cpp @@ -50,7 +50,7 @@ bool SnarlNetworkFrontend::initialize(SnoreCore *snore){ else { connect(tcpServer, SIGNAL(newConnection()), this, SLOT(handleConnection())); - std::cout<<"The Snarl Network Protokoll is developed for Snarl "<" << std::endl; } return SnoreFrontend::initialize(snore); } @@ -129,7 +129,7 @@ void SnarlNetworkFrontend::handleMessages() void SnarlNetworkFrontend::callback(Notification &sn, const QString msg) { - QTcpSocket *client = myQVariantCast(sn.hints().privateValue(this, "clientSocket")); + QTcpSocket *client = (QTcpSocket*)qvariant_cast(sn.hints().privateValue(this, "clientSocket")); if(client && !msg.isEmpty()) { write(client, QString("%1%2\r\n").arg(msg, QString::number(sn.id()))); diff --git a/src/trayicon.cpp b/src/trayicon.cpp index 3bda480..8f74417 100644 --- a/src/trayicon.cpp +++ b/src/trayicon.cpp @@ -47,6 +47,8 @@ void TrayIcon::initConextMenu(SnoreCore *snore) m_trayMenu->addSeparator(); m_trayMenu->addAction("Test Notification", this, SLOT(slotTestNotification())); m_trayMenu->addSeparator(); + m_backendActions = new QActionGroup(m_trayMenu); + m_backendActions->setExclusive(true); foreach(const QString &back,m_snore->notificationBackends()) { QAction *b = m_trayMenu->addAction(back, this, SLOT(setPrimaryBackend())); @@ -55,7 +57,7 @@ void TrayIcon::initConextMenu(SnoreCore *snore) { b->setChecked(true); } - m_backendActions.append(b); + m_backendActions->addAction(b); } m_trayMenu->addSeparator(); m_trayMenu->addAction("Exit",qApp,SLOT(quit())); @@ -78,12 +80,14 @@ void TrayIcon::setPrimaryBackend(){ QAction *a = qobject_cast(sender()); m_snore->setPrimaryNotificationBackend(a->text()); - foreach(QAction *action,m_backendActions) + foreach (QAction *action, m_backendActions->actions()) { - action->setChecked(false); + if(action->text() == m_snore->primaryNotificationBackend()) + { + action->setChecked(true); + break; + } } - a->setChecked(true); - } void TrayIcon::slotTestNotification() diff --git a/src/trayicon.h b/src/trayicon.h index e0db423..044bbdb 100644 --- a/src/trayicon.h +++ b/src/trayicon.h @@ -21,6 +21,7 @@ #define TRAYICON_H #include +#include #include "core/snore.h" @@ -36,7 +37,7 @@ public: private: class QSystemTrayIcon *m_trayIcon; class QMenu *m_trayMenu; - class QList m_backendActions; + QActionGroup *m_backendActions; Snore::SnoreCore *m_snore; Snore::Application m_app; Snore::Alert m_alert;