Add a wrapper to be able to insert a lambda into the hints.

This commit is contained in:
Patrick von Reth 2015-09-11 15:51:27 +02:00
parent d337082930
commit faba87868b
7 changed files with 117 additions and 7 deletions

View File

@ -28,6 +28,7 @@ list(APPEND SnoreNotify_SRCS
alert.cpp
alert_p.cpp
hint.cpp
lambdahint.cpp
log.cpp
settingsdialog.cpp
utils.cpp
@ -42,6 +43,7 @@ list(APPEND SnoreNotify_HDR
application.h
alert.h
hint.h
lambdahint.h
log.h
settingsdialog.h
snoreglobals.h

View File

@ -17,6 +17,7 @@
*/
#include "application_p.h"
#include "lambdahint.h"
#include "snore_p.h"
#include <QApplication>
@ -34,7 +35,9 @@ ApplicationData::ApplicationData(const QString &key, const QString &name, const
m_hint.setValue("pushover-token", QLatin1String("aFB1TPCyZkkr7mubCGEKy5vJEWak9t"));
m_hint.setValue("use-markup", false);
m_hint.setValue("silent", SnoreCore::instance().settingsValue(QLatin1String("Silent"), LOCAL_SETTING));
m_hint.setValue("silent", QVariant::fromValue(LambdaHint([]() {
return SnoreCore::instance().settingsValue(QLatin1String("Silent"), LOCAL_SETTING);
})));
}

View File

@ -16,6 +16,7 @@
along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hint.h"
#include "lambdahint.h"
#include "log.h"
using namespace Snore;
@ -31,12 +32,20 @@ void Hint::setValue(const QByteArray &key, const QVariant &value)
QVariant Hint::value(const QByteArray &key) const
{
return m_data.value(key);
QVariant out = m_data.value(key);
if (out.canConvert<LambdaHint>()) {
return out.value<LambdaHint>()();
}
return out;
}
QVariant Hint::take(const QByteArray &key)
{
return m_data.take(key);
QVariant out = m_data.take(key);
if (out.canConvert<LambdaHint>()) {
return out.value<LambdaHint>()();
}
return out;
}
bool Hint::contains(const QByteArray &key) const
@ -51,7 +60,11 @@ void Hint::setPrivateValue(const void *owner, const QByteArray &key, const QVari
QVariant Hint::privateValue(const void *owner, const QByteArray &key) const
{
return m_privateData.value(qMakePair<const quintptr, const QByteArray>((quintptr)owner, key));
QVariant out = m_privateData.value(qMakePair<const quintptr, const QByteArray>((quintptr)owner, key));
if (out.canConvert<LambdaHint>()) {
return out.value<LambdaHint>()();
}
return out;
}
bool Hint::containsPrivateValue(const void *owner, const QByteArray &key) const
@ -61,7 +74,11 @@ bool Hint::containsPrivateValue(const void *owner, const QByteArray &key) const
QVariant Hint::takePrivateValue(const void *owner, const QByteArray &key)
{
return m_privateData.take(qMakePair<const quintptr, const QByteArray>((quintptr)owner, key));
QVariant out = m_privateData.take(qMakePair<const quintptr, const QByteArray>((quintptr)owner, key));
if (out.canConvert<LambdaHint>()) {
return out.value<LambdaHint>()();
}
return out;
}
QDebug operator<<(QDebug debug, const Snore::Hint &hint)

View File

@ -0,0 +1,41 @@
/*
SnoreNotify is a Notification Framework based on Qt
Copyright (C) 2015 Patrick von Reth <vonreth@kde.org>
SnoreNotify is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SnoreNotify is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lambdahint.h"
using namespace Snore;
LambdaHint::LambdaHint():
m_func([]()
{
return QVariant();
})
{
}
LambdaHint::LambdaHint(std::function<QVariant()> function):
m_func(function)
{
}
QVariant LambdaHint::operator()()
{
return m_func();
}

45
src/libsnore/lambdahint.h Normal file
View File

@ -0,0 +1,45 @@
#ifndef LAMBDAHINT_H
#define LAMBDAHINT_H
#include "snore_exports.h"
/*
SnoreNotify is a Notification Framework based on Qt
Copyright (C) 201 Patrick von Reth <vonreth@kde.org>
SnoreNotify is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SnoreNotify is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QVariant>
#include <functional>
namespace Snore
{
class SNORE_EXPORT LambdaHint
{
public:
LambdaHint();
LambdaHint(std::function<QVariant()> function);
QVariant operator()();
private:
std::function<QVariant()> m_func;
};
}
Q_DECLARE_METATYPE(Snore::LambdaHint)
#endif // LAMBDAHINT_H

View File

@ -18,6 +18,7 @@
#include "snore.h"
#include "snore_p.h"
#include "lambdahint.h"
#include "notification/notification.h"
#include "notification/notification_p.h"
#include "plugins/plugincontainer.h"
@ -48,8 +49,9 @@ SnoreCore &SnoreCore::instance()
{
static SnoreCore *instance = nullptr;
if (!instance) {
qRegisterMetaType<Notification>();
qRegisterMetaType<Application>();
qRegisterMetaType<LambdaHint>();
qRegisterMetaType<Notification>();
qRegisterMetaType<SnorePlugin::PluginTypes>();
qRegisterMetaTypeStreamOperators<SnorePlugin::PluginTypes>();
instance = new SnoreCore(qApp);

View File

@ -17,7 +17,7 @@ FreedesktopBackend::FreedesktopBackend()
QDBusConnection::sessionBus(), this);
QDBusPendingReply<QStringList> reply = m_interface->GetCapabilities();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
connect(watcher, &QDBusPendingCallWatcher::finished, [reply, watcher, this](){
connect(watcher, &QDBusPendingCallWatcher::finished, [reply, watcher, this]() {
m_supportsRichtext = reply.value().contains(QLatin1String("body-markup"));
watcher->deleteLater();
});