make hint really work with qobjects

This commit is contained in:
Patrick von Reth 2014-02-04 16:35:16 +01:00
parent f2459a4ae8
commit 6cc6f08317
7 changed files with 66 additions and 50 deletions

View File

@ -17,6 +17,7 @@
along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>. along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "hint.h" #include "hint.h"
#include "log.h"
using namespace Snore; 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) void Hint::setPrivateValue(const void *owner, const QString &key, const QVariant &value)
{ {
m_privateData.insert(QPair<const void*,QString>(owner,key.toLower()), value); m_privateData.insert(QPair<quintptr,QString>((quintptr)owner,key.toLower()), value);
}
void Hint::setPrivateValue(const void *owner, const QString &key, QObject *value)
{
m_privateData.insert(QPair<quintptr,QString>((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 QVariant Hint::privateValue(const void *owner, const QString &k, const QVariant &defaultValue) const
{ {
QPair<const void*,QString> key(owner,k.toLower()); QPair<quintptr,QString> key((quintptr)owner, k.toLower());
if(m_privateData.contains(key)) if(m_privateData.contains(key))
{ {
return m_privateData.value(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 bool Hint::containsPrivateValue(const void *owner, const QString &key) const
{ {
return m_privateData.contains(QPair<const void*,QString>(owner,key.toLower())); return m_privateData.contains(QPair<quintptr,QString>((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<quintptr,QString>(o->property("hint_owner").value<quintptr>(),key));
}
else
{
m_data.take(key);
}
} }
QDebug operator<<( QDebug debug, const Snore::Hint &hint ) 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(); debug << "(" << it.key() << ", " << it.value();
} }
for(QHash< QPair<const void*, QString>, QVariant>::const_iterator it = hint.m_privateData.constBegin();it != hint.m_privateData.constEnd();++it) for(QHash< QPair<quintptr, QString>, QVariant>::const_iterator it = hint.m_privateData.constBegin();it != hint.m_privateData.constEnd();++it)
{ {
if(it != hint.m_privateData.constBegin()) if(it != hint.m_privateData.constBegin())
{ {

View File

@ -27,7 +27,7 @@
namespace Snore namespace Snore
{ {
class Hint; class Hint;
} }
SNORE_EXPORT QDebug operator<< ( QDebug, const Snore::Hint &); SNORE_EXPORT QDebug operator<< ( QDebug, const Snore::Hint &);
@ -39,8 +39,9 @@ namespace Snore
* The keys are case insensitive. * The keys are case insensitive.
*/ */
class SNORE_EXPORT Hint class SNORE_EXPORT Hint : public QObject
{ {
Q_OBJECT
public: public:
Hint(); Hint();
@ -51,6 +52,13 @@ public:
*/ */
void setValue(const QString &key, const QVariant &value); 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. * The associated value of the key if present, returns the default value otherwise.
* @param key the key * @param key the key
@ -73,6 +81,14 @@ public:
*/ */
void setPrivateValue(const void *owner, const QString &key, const QVariant &value); 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. * The associated value of the key if present, returns the default value otherwise.
* @param owner the owner * @param owner the owner
@ -88,49 +104,18 @@ public:
* @return whether the key is set * @return whether the key is set
*/ */
bool containsPrivateValue(const void *owner, const QString & key ) const; bool containsPrivateValue(const void *owner, const QString & key ) const;
private slots:
void slotValueDestroyed();
private: private:
QVariantHash m_data; QVariantHash m_data;
QHash<QPair<const void*,QString>, QVariant> m_privateData; QHash<QPair<quintptr,QString>, QVariant> m_privateData;
friend SNORE_EXPORT QDebug (::operator<<) ( QDebug, const Snore::Hint &); 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<typename Type>
inline Type myQVariantCast(const QVariant &dat)
{
return qvariant_cast<Type>(dat);
}
/**
* Helper function to easyly stor pointers to QObjects in a QVariant
*/
template<typename Type>
inline QVariant myQVariantFromValue(const Type &dat)
{
return qVariantFromValue(dat);
}
#else
template<typename Type>
inline Type myQVariantCast(const QVariant &dat)
{
return qobject_cast<Type>(qvariant_cast<QObject*>(dat));
}
template<typename Type>
inline QVariant myQVariantFromValue(const Type &dat)
{
return qVariantFromValue(qobject_cast<QObject*>(dat));
}
#endif
} }

View File

@ -42,6 +42,7 @@ SnoreLog::~SnoreLog()
if(debugLvl() >= m_lvl) if(debugLvl() >= m_lvl)
{ {
std::cout << m_msg.toUtf8().constData() << std::endl; std::cout << m_msg.toUtf8().constData() << std::endl;
std::cout.flush();
} }
m_logg << m_msg.toUtf8().constData() << std::endl; m_logg << m_msg.toUtf8().constData() << std::endl;
} }

View File

@ -138,13 +138,13 @@ void Parser::parse(Notification &sNotification,const QString &msg,QTcpSocket* cl
} }
sNotification = Notification(app,alert,title,text,icon,timeout); sNotification = Notification(app,alert,title,text,icon,timeout);
sNotification.data()->setSource(snarl); sNotification.data()->setSource(snarl);
sNotification.hints().setPrivateValue(snarl, "clientSocket", myQVariantFromValue(client)); sNotification.hints().setPrivateValue(snarl, "clientSocket", client);
break; break;
} }
case ADD_CLASS: case ADD_CLASS:
if(alertName.isEmpty()) if(alertName.isEmpty())
{ {
snoreDebug( SNORE_DEBUG )<<"Error registering alert with empty name"; snoreDebug( SNORE_DEBUG ) << "Error registering alert with empty name";
break; break;
} }
alert = Alert(alertName, icon); alert = Alert(alertName, icon);

View File

@ -50,7 +50,7 @@ bool SnarlNetworkFrontend::initialize(SnoreCore *snore){
else else
{ {
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(handleConnection())); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(handleConnection()));
std::cout<<"The Snarl Network Protokoll is developed for Snarl <http://www.fullphat.net/>"<<std::endl; std::cout << "The Snarl Network Protokoll is developed for Snarl <http://www.fullphat.net/>" << std::endl;
} }
return SnoreFrontend::initialize(snore); return SnoreFrontend::initialize(snore);
} }
@ -129,7 +129,7 @@ void SnarlNetworkFrontend::handleMessages()
void SnarlNetworkFrontend::callback(Notification &sn, const QString msg) void SnarlNetworkFrontend::callback(Notification &sn, const QString msg)
{ {
QTcpSocket *client = myQVariantCast<QTcpSocket*>(sn.hints().privateValue(this, "clientSocket")); QTcpSocket *client = (QTcpSocket*)qvariant_cast<void*>(sn.hints().privateValue(this, "clientSocket"));
if(client && !msg.isEmpty()) if(client && !msg.isEmpty())
{ {
write(client, QString("%1%2\r\n").arg(msg, QString::number(sn.id()))); write(client, QString("%1%2\r\n").arg(msg, QString::number(sn.id())));

View File

@ -47,6 +47,8 @@ void TrayIcon::initConextMenu(SnoreCore *snore)
m_trayMenu->addSeparator(); m_trayMenu->addSeparator();
m_trayMenu->addAction("Test Notification", this, SLOT(slotTestNotification())); m_trayMenu->addAction("Test Notification", this, SLOT(slotTestNotification()));
m_trayMenu->addSeparator(); m_trayMenu->addSeparator();
m_backendActions = new QActionGroup(m_trayMenu);
m_backendActions->setExclusive(true);
foreach(const QString &back,m_snore->notificationBackends()) foreach(const QString &back,m_snore->notificationBackends())
{ {
QAction *b = m_trayMenu->addAction(back, this, SLOT(setPrimaryBackend())); QAction *b = m_trayMenu->addAction(back, this, SLOT(setPrimaryBackend()));
@ -55,7 +57,7 @@ void TrayIcon::initConextMenu(SnoreCore *snore)
{ {
b->setChecked(true); b->setChecked(true);
} }
m_backendActions.append(b); m_backendActions->addAction(b);
} }
m_trayMenu->addSeparator(); m_trayMenu->addSeparator();
m_trayMenu->addAction("Exit",qApp,SLOT(quit())); m_trayMenu->addAction("Exit",qApp,SLOT(quit()));
@ -78,12 +80,14 @@ void TrayIcon::setPrimaryBackend(){
QAction *a = qobject_cast<QAction*>(sender()); QAction *a = qobject_cast<QAction*>(sender());
m_snore->setPrimaryNotificationBackend(a->text()); 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() void TrayIcon::slotTestNotification()

View File

@ -21,6 +21,7 @@
#define TRAYICON_H #define TRAYICON_H
#include <QtCore> #include <QtCore>
#include <QAction>
#include "core/snore.h" #include "core/snore.h"
@ -36,7 +37,7 @@ public:
private: private:
class QSystemTrayIcon *m_trayIcon; class QSystemTrayIcon *m_trayIcon;
class QMenu *m_trayMenu; class QMenu *m_trayMenu;
class QList<class QAction*> m_backendActions; QActionGroup *m_backendActions;
Snore::SnoreCore *m_snore; Snore::SnoreCore *m_snore;
Snore::Application m_app; Snore::Application m_app;
Snore::Alert m_alert; Snore::Alert m_alert;