cleanup Hints, make noitification inherit Hints from application, fix marokup..

This commit is contained in:
Patrick von Reth 2015-06-29 21:09:05 +02:00
parent 64a680f729
commit 4f96a52aa8
10 changed files with 51 additions and 69 deletions

View File

@ -35,8 +35,8 @@ using namespace Snore;
TrayIcon::TrayIcon(): TrayIcon::TrayIcon():
m_trayIcon(new QSystemTrayIcon(QIcon(":/root/snore.png"))) m_trayIcon(new QSystemTrayIcon(QIcon(":/root/snore.png")))
{ {
SnoreCorePrivate::instance()->defaultApplication().hints().setValue("use-markup", QVariant::fromValue(true)); SnoreCorePrivate::instance()->defaultApplication().hints().setValue("use-markup", true);
SnoreCorePrivate::instance()->defaultApplication().hints().setValue("tray-icon", m_trayIcon); SnoreCorePrivate::instance()->defaultApplication().hints().setValue("tray-icon", QVariant::fromValue(QPointer<QSystemTrayIcon>(m_trayIcon)));
} }
void TrayIcon::initConextMenu() void TrayIcon::initConextMenu()

View File

@ -106,7 +106,7 @@ public:
* use-markup | Enable markup support for title and message, strings must be html escaped.| Many Backends. * use-markup | Enable markup support for title and message, strings must be html escaped.| Many Backends.
* desktop-entry | The name of the desktop enty associated with the application. | Used for The freedesktop backend. * desktop-entry | The name of the desktop enty associated with the application. | Used for The freedesktop backend.
* windows-app-id | The app id associated with the application. | Needed for the Windows 8 backend [See MSDN Documentation](http://msdn.microsoft.com/en-us/library/windows/apps/dd378459.aspx). * windows-app-id | The app id associated with the application. | Needed for the Windows 8 backend [See MSDN Documentation](http://msdn.microsoft.com/en-us/library/windows/apps/dd378459.aspx).
* tray-icon | A pointer to a QSystemTray item. | Needed for the System Tray Backend. * tray-icon | A QPointer<QSystemTray> item. | Needed for the System Tray Backend.
* pushover-token | The token associated with your application. | Needed to associate pushover notification with your application, to register your application visit [Pushover](https://pushover.net/apps/build). * pushover-token | The token associated with your application. | Needed to associate pushover notification with your application, to register your application visit [Pushover](https://pushover.net/apps/build).
*/ */
Hint &hints(); Hint &hints();

View File

@ -29,18 +29,16 @@ void Hint::setValue(const QString &key, const QVariant &value)
m_data.insert(key.toLower(), value); m_data.insert(key.toLower(), value);
} }
void Hint::setValue(const QString &key, QObject *value)
{
m_data.insert(key.toLower(), qVariantFromValue(value));
value->setProperty("hint_key", key.toLower());
connect(value, SIGNAL(destroyed()), this, SLOT(slotValueDestroyed()), Qt::DirectConnection);
}
QVariant Hint::value(const QString &k) const QVariant Hint::value(const QString &k) const
{ {
return m_data.value(k.toLower()); return m_data.value(k.toLower());
} }
QVariant Hint::take(const QString &key)
{
return m_data.take(key.toLower());
}
bool Hint::contains(const QString &key) const bool Hint::contains(const QString &key) const
{ {
return m_data.contains(key.toLower()); return m_data.contains(key.toLower());
@ -52,23 +50,10 @@ void Hint::setPrivateValue(const void *owner, const QString &key, const QVariant
m_privateData.insert(pk, value); m_privateData.insert(pk, value);
} }
void Hint::setPrivateValue(const void *owner, const QString &key, QObject *value) const QVariant Hint::privateValue(const void *owner, const QString &k) const
{
QPair<quintptr, QString> pk((quintptr)owner, key.toLower());
m_privateData.insert(pk, qVariantFromValue(value));
value->setProperty("hint_key", key.toLower());
value->setProperty("hint_owner", (quintptr)owner);
connect(value, SIGNAL(destroyed()), this, SLOT(slotValueDestroyed()), Qt::DirectConnection);
}
QVariant Hint::privateValue(const void *owner, const QString &k, const QVariant &defaultValue) const
{ {
QPair<quintptr, QString> key((quintptr)owner, k.toLower()); QPair<quintptr, QString> key((quintptr)owner, k.toLower());
if (m_privateData.contains(key)) { return m_privateData.value(key);
return m_privateData.value(key);
} else {
return defaultValue;
}
} }
bool Hint::containsPrivateValue(const void *owner, const QString &key) const bool Hint::containsPrivateValue(const void *owner, const QString &key) const
@ -77,16 +62,10 @@ bool Hint::containsPrivateValue(const void *owner, const QString &key) const
return m_privateData.contains(pk); return m_privateData.contains(pk);
} }
void Hint::slotValueDestroyed() QVariant Hint::takePrivateValue(const void *owner, const QString &key)
{ {
QObject *o = sender(); QPair<quintptr, QString> pk((quintptr)owner, key.toLower());
QString key = o->property("hint_key").toString(); return m_privateData.take(pk);
if (!o->property("hint_owner").isNull()) {
QPair<quintptr, QString> pk(o->property("hint_owner").value<quintptr>(), key);
m_privateData.remove(pk);
} else {
m_data.remove(key);
}
} }
QDebug operator<<(QDebug debug, const Snore::Hint &hint) QDebug operator<<(QDebug debug, const Snore::Hint &hint)

View File

@ -38,9 +38,8 @@ namespace Snore
* The keys are case insensitive. * The keys are case insensitive.
*/ */
class SNORE_EXPORT Hint : public QObject class SNORE_EXPORT Hint
{ {
Q_OBJECT
public: public:
Hint(); Hint();
@ -51,20 +50,20 @@ 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.
* @param key the key * @param key the key
* @param defaultValue the fallback value
*/ */
QVariant value(const QString &key) const; QVariant value(const QString &key) const;
/**
* The associated value of the key.
* @param key the key
*/
QVariant take(const QString &key);
/** /**
* *
* @param key the key * @param key the key
@ -81,20 +80,11 @@ public:
void setPrivateValue(const void *owner, const QString &key, const QVariant &value) const; void setPrivateValue(const void *owner, const QString &key, const QVariant &value) const;
/** /**
* Sets the value for the key depending on the owner * The associated value of the key if present.
* @param owner the owner * @param owner the owner
* @param key the key * @param key the key
* @param value the value
*/ */
void setPrivateValue(const void *owner, const QString &key, QObject *value) const; QVariant privateValue(const void *owner, const QString &key) const;
/**
* The associated value of the key if present, returns the default value otherwise.
* @param owner the owner
* @param key the key
* @param defaultValue the fallback value
*/
QVariant privateValue(const void *owner, const QString &key, const QVariant &defaultValue = QVariant()) const;
/** /**
* *
@ -104,8 +94,15 @@ public:
*/ */
bool containsPrivateValue(const void *owner, const QString &key) const; bool containsPrivateValue(const void *owner, const QString &key) const;
private slots:
void slotValueDestroyed();
/**
* The associated value of the key if present.
* @param owner the owner
* @param key the key
* @return whether the key is set
*/
QVariant takePrivateValue(const void *owner, const QString &key);
private: private:
QVariantHash m_data; QVariantHash m_data;

View File

@ -41,7 +41,8 @@ NotificationData::NotificationData(const Snore::Application &application, const
m_text(text), m_text(text),
m_icon(icon), m_icon(icon),
m_priority(priority), m_priority(priority),
m_closeReason(Notification::NONE) m_closeReason(Notification::NONE),
m_hints(m_application.constHints())
{ {
notificationCount++; notificationCount++;
snoreDebug(SNORE_INFO) << "Creating Notification: ActiveNotifications" << notificationCount << "id" << m_id; snoreDebug(SNORE_INFO) << "Creating Notification: ActiveNotifications" << notificationCount << "id" << m_id;
@ -59,6 +60,7 @@ Snore::NotificationData::NotificationData(const Notification &old, const QString
m_icon(icon), m_icon(icon),
m_priority(priority), m_priority(priority),
m_closeReason(Notification::NONE), m_closeReason(Notification::NONE),
m_hints(m_application.constHints()),
m_toReplace(old) m_toReplace(old)
{ {
notificationCount++; notificationCount++;
@ -94,11 +96,11 @@ void NotificationData::setTimeoutTimer(QTimer *timer)
QString NotificationData::resolveMarkup(const QString &string, Utils::MARKUP_FLAGS flags) QString NotificationData::resolveMarkup(const QString &string, Utils::MARKUP_FLAGS flags)
{ {
if(!m_application.constHints().value("use-markup").toBool()) { if(!m_hints.value("use-markup").toBool()) {
if(flags == Utils::NO_MARKUP){ if(flags == Utils::NO_MARKUP){
return string; return string;
} else { } else {
return string.toHtmlEscaped(); return Utils::normalizeMarkup(string.toHtmlEscaped(), flags);
} }
} else { } else {
return Utils::normalizeMarkup(string, flags); return Utils::normalizeMarkup(string, flags);

View File

@ -88,7 +88,6 @@ QString Utils::normalizeMarkup(QString string, MARKUP_FLAGS tags)
} }
QMutexLocker lock(&mutex); QMutexLocker lock(&mutex);
if (~tags & Utils::BREAK) { if (~tags & Utils::BREAK) {
static QRegExp br("<br>"); static QRegExp br("<br>");
string = string.replace(br, "\n"); string = string.replace(br, "\n");

View File

@ -59,7 +59,7 @@ void TrayIconNotifer::slotDeregisterApplication(const Application &application)
QSystemTrayIcon *TrayIconNotifer::trayIcon(const Application &app) QSystemTrayIcon *TrayIconNotifer::trayIcon(const Application &app)
{ {
if (app.constHints().contains("tray-icon")) { if (app.constHints().contains("tray-icon")) {
return app.constHints().value("tray-icon").value<QSystemTrayIcon *>(); return app.constHints().value("tray-icon").value<QPointer<QSystemTrayIcon>>();
} }
return nullptr; return nullptr;
} }

View File

@ -228,10 +228,6 @@ void PushoverFrontend::getMessages()
SnoreCore::instance().registerApplication(app); SnoreCore::instance().registerApplication(app);
} }
if(notification.value("html").toInt() == 1) {
app.hints().setValue("use-markup", QVariant::fromValue(true)) ;
}
Notification n(app, *app.alerts().begin(), notification.value("title").toString(), notification.value("message").toString(), Notification n(app, *app.alerts().begin(), notification.value("title").toString(), notification.value("message").toString(),
app.icon(), Notification::defaultTimeout(), static_cast<Notification::Prioritys>(notification.value("priority").toInt())); app.icon(), Notification::defaultTimeout(), static_cast<Notification::Prioritys>(notification.value("priority").toInt()));
@ -239,6 +235,9 @@ void PushoverFrontend::getMessages()
n.hints().setValue("receipt", notification.value("receipt").toString()); n.hints().setValue("receipt", notification.value("receipt").toString());
n.hints().setValue("acked", notification.value("acked").toInt()); n.hints().setValue("acked", notification.value("acked").toInt());
} }
if(notification.value("html").toInt() == 1) {
n.hints().setValue("use-markup", true) ;
}
SnoreCore::instance().broadcastNotification(n); SnoreCore::instance().broadcastNotification(n);
} }
if(latestID != -1){ if(latestID != -1){

View File

@ -25,6 +25,7 @@
#include <QObject> #include <QObject>
#include <QTcpSocket> #include <QTcpSocket>
#include <QPointer>
using namespace Snore; using namespace Snore;
@ -116,7 +117,7 @@ void Parser::parse(Notification &sNotification, const QString &msg, QTcpSocket *
break; break;
} }
sNotification = Notification(app, alert, title, text, icon, timeout); sNotification = Notification(app, alert, title, text, icon, timeout);
sNotification.hints().setPrivateValue(snarl, "clientSocket", client); sNotification.hints().setPrivateValue(snarl, "clientSocket", QVariant::fromValue(QPointer<QTcpSocket>(client)));
break; break;
} }
case ADD_CLASS: case ADD_CLASS:

View File

@ -22,6 +22,7 @@
#include <QTcpServer> #include <QTcpServer>
#include <QTcpSocket> #include <QTcpSocket>
#include <QPointer>
#include <iostream> #include <iostream>
using namespace Snore; using namespace Snore;
@ -117,8 +118,12 @@ void SnarlNetworkFrontend::handleMessages()
void SnarlNetworkFrontend::callback(Notification &sn, const QString msg) void SnarlNetworkFrontend::callback(Notification &sn, const QString msg)
{ {
if (sn.hints().containsPrivateValue(this, "clientSocket")) { if (sn.hints().containsPrivateValue(this, "clientSocket")) {
QTcpSocket *client = qobject_cast<QTcpSocket *>(sn.hints().privateValue(this, "clientSocket").value<QObject *>()); QTcpSocket *client = sn.hints().privateValue(this, "clientSocket").value<QPointer<QTcpSocket>>();
write(client, QString("%1%2\r\n").arg(msg, QString::number(sn.id()))); if(client){
write(client, QString("%1%2\r\n").arg(msg, QString::number(sn.id())));
}else{
sn.hints().takePrivateValue(this, "clientSocket");
}
} }
} }