From 252b7588cfd6fc7a42fa7f38bc225a4b9bed2e19 Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Mon, 22 Jun 2015 20:19:23 +0200 Subject: [PATCH] added pushover support --- src/libsnore/application.h | 7 +- src/libsnore/plugins/pluginsettingswidget.cpp | 8 +- src/libsnore/plugins/pluginsettingswidget.h | 5 +- src/libsnore/snore_p.cpp | 1 + src/plugins/secondary_backends/CMakeLists.txt | 1 + .../pushover/CMakeLists.txt | 10 ++ .../secondary_backends/pushover/plugin.json | 1 + .../secondary_backends/pushover/pushover.cpp | 111 ++++++++++++++++++ .../secondary_backends/pushover/pushover.h | 44 +++++++ .../pushover/pushoversettings.cpp | 51 ++++++++ .../pushover/pushoversettings.h | 42 +++++++ src/settings/main.cpp | 4 +- 12 files changed, 274 insertions(+), 11 deletions(-) create mode 100644 src/plugins/secondary_backends/pushover/CMakeLists.txt create mode 100644 src/plugins/secondary_backends/pushover/plugin.json create mode 100644 src/plugins/secondary_backends/pushover/pushover.cpp create mode 100644 src/plugins/secondary_backends/pushover/pushover.h create mode 100644 src/plugins/secondary_backends/pushover/pushoversettings.cpp create mode 100644 src/plugins/secondary_backends/pushover/pushoversettings.h diff --git a/src/libsnore/application.h b/src/libsnore/application.h index c994fbe..a1a0240 100644 --- a/src/libsnore/application.h +++ b/src/libsnore/application.h @@ -103,9 +103,10 @@ public: * Returns application specific hints: * Key | Value | Required * ------------- | ----------- | ----------- - * 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) - * tray-icon | A instance of QSystemTray | Needed for the System Tray 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). + * tray-icon | A pointer to a 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). */ Hint &hints(); diff --git a/src/libsnore/plugins/pluginsettingswidget.cpp b/src/libsnore/plugins/pluginsettingswidget.cpp index 9d4f580..e5dbbe4 100644 --- a/src/libsnore/plugins/pluginsettingswidget.cpp +++ b/src/libsnore/plugins/pluginsettingswidget.cpp @@ -74,15 +74,15 @@ bool PluginSettingsWidget::isDirty() return m_dirty; } -QVariant PluginSettingsWidget::value(const QString &key) const +QVariant PluginSettingsWidget::value(const QString &key, SettingsType type) const { - return m_snorePlugin->value(key); + return m_snorePlugin->value(key, type); } -void PluginSettingsWidget::setValue(const QString &key, const QVariant &value) +void PluginSettingsWidget::setValue(const QString &key, const QVariant &value, SettingsType type) { if (this->value(key) != value) { - m_snorePlugin->setValue(key, value); + m_snorePlugin->setValue(key, value, type); m_dirty = true; } } diff --git a/src/libsnore/plugins/pluginsettingswidget.h b/src/libsnore/plugins/pluginsettingswidget.h index e83a1ee..b947aa3 100644 --- a/src/libsnore/plugins/pluginsettingswidget.h +++ b/src/libsnore/plugins/pluginsettingswidget.h @@ -19,6 +19,7 @@ #define PLUGINSETTINGSWIDGET_H #include "libsnore/snore_exports.h" +#include "libsnore/snoreglobals.h" #include #include @@ -45,8 +46,8 @@ public: bool isDirty(); protected: - QVariant value(const QString &key) const; - void setValue(const QString &key, const QVariant &value); + QVariant value(const QString &key, Snore::SettingsType type = Snore::GLOBAL_SETTING) const; + void setValue(const QString &key, const QVariant &value, Snore::SettingsType type = Snore::GLOBAL_SETTING); virtual void load(); virtual void save(); diff --git a/src/libsnore/snore_p.cpp b/src/libsnore/snore_p.cpp index 78928fc..b94e521 100644 --- a/src/libsnore/snore_p.cpp +++ b/src/libsnore/snore_p.cpp @@ -196,6 +196,7 @@ QString SnoreCorePrivate::tempPath() return dir.path(); } +// TODO: this is somehow horrible code SnoreCorePrivate *SnoreCorePrivate::instance() { return SnoreCore::instance().d_ptr; diff --git a/src/plugins/secondary_backends/CMakeLists.txt b/src/plugins/secondary_backends/CMakeLists.txt index 513510e..09dfc91 100644 --- a/src/plugins/secondary_backends/CMakeLists.txt +++ b/src/plugins/secondary_backends/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(toasty) add_subdirectory(nma) add_subdirectory(sound) +add_subdirectory(pushover) diff --git a/src/plugins/secondary_backends/pushover/CMakeLists.txt b/src/plugins/secondary_backends/pushover/CMakeLists.txt new file mode 100644 index 0000000..d6995b6 --- /dev/null +++ b/src/plugins/secondary_backends/pushover/CMakeLists.txt @@ -0,0 +1,10 @@ +set( PUSHOVER_SRC + pushover.cpp + pushoversettings.cpp + ) + +add_library(libsnore_secondary_backend_pushover MODULE ${PUSHOVER_SRC} ) +target_link_libraries(libsnore_secondary_backend_pushover Snore::Libsnore) + +install(TARGETS libsnore_secondary_backend_pushover ${SNORE_PLUGIN_INSTALL_PATH}) + diff --git a/src/plugins/secondary_backends/pushover/plugin.json b/src/plugins/secondary_backends/pushover/plugin.json new file mode 100644 index 0000000..5d3d36d --- /dev/null +++ b/src/plugins/secondary_backends/pushover/plugin.json @@ -0,0 +1 @@ +{ "type" : "secondary_backend", "name" : "Pushover" } \ No newline at end of file diff --git a/src/plugins/secondary_backends/pushover/pushover.cpp b/src/plugins/secondary_backends/pushover/pushover.cpp new file mode 100644 index 0000000..ad03f77 --- /dev/null +++ b/src/plugins/secondary_backends/pushover/pushover.cpp @@ -0,0 +1,111 @@ +/* + SnoreNotify is a Notification Framework based on Qt + Copyright (C) 2015 Patrick von Reth + + 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 . +*/ +#include "pushover.h" +#include "pushoversettings.h" + +#include"libsnore/utils.h" + +#include +#include +#include +#include + +using namespace Snore; + +Pushover::Pushover(): + SnoreSecondaryBackend("Pushover", false) +{ + setDefaultValue("UserKey", ""); + setDefaultValue("Sound", "pushover", LOCAL_SETTING); + setDefaultValue("Devices", "", LOCAL_SETTING); +} + +Pushover::~Pushover() +{ + +} + +void Pushover::slotNotify(Notification notification) +{ + QString key = value("ApiKey").toString(); + if (key.isEmpty()) { + return; + } + + QNetworkRequest request(QUrl("https://api.pushover.net/1/messages.json")); + QHttpMultiPart *mp = new QHttpMultiPart(QHttpMultiPart::FormDataType); + + QHttpPart title; + title.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"title\"")); + title.setBody(Utils::toPlainText(notification.title()).toUtf8().constData()); + mp->append(title); + + QHttpPart text; + text.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"message\"")); + text.setBody(Utils::toPlainText(notification.text()).toUtf8().constData()); + mp->append(text); + + + QHttpPart priority; + priority.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"priority\"")); + priority.setBody(QString::number(notification.priority()).toUtf8().constData()); + mp->append(priority); + + QHttpPart sound; + sound.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"sound\"")); + if (notification.hints().value("silent", false).toBool()) { + sound.setBody("none"); + } else { + sound.setBody(value("Sound", LOCAL_SETTING).toString().toUtf8().constData()); + } + mp->append(sound); + + if (!value("Devices", LOCAL_SETTING).toString().isEmpty()) { + QHttpPart devices; + devices.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"device\"")); + devices.setBody(value("Devices", LOCAL_SETTING).toString().toUtf8().constData()); + mp->append(devices); + } + + QHttpPart token; + token.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"token\"")); + token.setBody(notification.application().constHints().value("pushover-token","aFB1TPCyZkkr7mubCGEKy5vJEWak9t").toString().toUtf8().constData()); + mp->append(token); + + QHttpPart user; + user.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"user\"")); + user.setBody(key.toUtf8().constData()); + mp->append(user); + + + QNetworkReply *reply = m_manager.post(request, mp); + mp->setParent(reply); + + connect(reply, &QNetworkReply::finished, [reply]() { + snoreDebug(SNORE_DEBUG) << reply->error(); + snoreDebug(SNORE_DEBUG) << reply->readAll(); + reply->close(); + reply->deleteLater(); + }); + +} + +PluginSettingsWidget *Pushover::settingsWidget() +{ + return new PushoverSettings(this); +} diff --git a/src/plugins/secondary_backends/pushover/pushover.h b/src/plugins/secondary_backends/pushover/pushover.h new file mode 100644 index 0000000..a08f335 --- /dev/null +++ b/src/plugins/secondary_backends/pushover/pushover.h @@ -0,0 +1,44 @@ +/* + SnoreNotify is a Notification Framework based on Qt + Copyright (C) 2015 Patrick von Reth + + 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 . +*/ +#ifndef PUSHOVER_H +#define PUSHOVER_H + +#include "libsnore/plugins/snorebackend.h" + +#include + +class Pushover : public Snore::SnoreSecondaryBackend +{ + Q_OBJECT + Q_INTERFACES(Snore::SnoreSecondaryBackend) + Q_PLUGIN_METADATA(IID "org.Snore.SecondaryNotificationBackend/1.0" FILE "plugin.json") +public: + Pushover(); + ~Pushover(); + + Snore::PluginSettingsWidget *settingsWidget() override; + +public slots: + void slotNotify(Snore::Notification notification) override; + +private: + QNetworkAccessManager m_manager; + +}; + +#endif // PUSHOVER_H diff --git a/src/plugins/secondary_backends/pushover/pushoversettings.cpp b/src/plugins/secondary_backends/pushover/pushoversettings.cpp new file mode 100644 index 0000000..004f2a5 --- /dev/null +++ b/src/plugins/secondary_backends/pushover/pushoversettings.cpp @@ -0,0 +1,51 @@ +/* + SnoreNotify is a Notification Framework based on Qt + Copyright (C) 2015 Patrick von Reth + + 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 . +*/ +#include "pushoversettings.h" + +#include "plugins/plugins.h" + +#include + +PushoverSettings::PushoverSettings(Snore::SnorePlugin *plugin, QWidget *parent) : + Snore::PluginSettingsWidget(plugin, parent), + m_keyLineEdit(new QLineEdit(this)), + m_soundLineEdit(new QLineEdit(this)), + m_deviceLineEdit(new QLineEdit(this)) +{ + addRow(tr("User Key:"), m_keyLineEdit); + addRow(tr("Sound:"), m_soundLineEdit); + addRow(tr("Devices:"), m_deviceLineEdit); +} + +PushoverSettings::~PushoverSettings() +{ +} + +void PushoverSettings::load() +{ + m_keyLineEdit->setText(value("UserKey").toString()); + m_soundLineEdit->setText(value("Sound", Snore::LOCAL_SETTING).toString()); + m_deviceLineEdit->setText(value("Devices", Snore::LOCAL_SETTING).toString()); +} + +void PushoverSettings::save() +{ + setValue("UserKey", m_keyLineEdit->text()); + setValue("Sound", m_soundLineEdit->text(), Snore::LOCAL_SETTING); + setValue("Devices", m_deviceLineEdit->text(), Snore::LOCAL_SETTING); +} diff --git a/src/plugins/secondary_backends/pushover/pushoversettings.h b/src/plugins/secondary_backends/pushover/pushoversettings.h new file mode 100644 index 0000000..61a3001 --- /dev/null +++ b/src/plugins/secondary_backends/pushover/pushoversettings.h @@ -0,0 +1,42 @@ +/* + SnoreNotify is a Notification Framework based on Qt + Copyright (C) 2015 Patrick von Reth + + 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 . +*/ +#ifndef PUSHOVERSETTINGS_H +#define PUSHOVERSETTINGS_H + +#include "plugins/pluginsettingswidget.h" + +class QLineEdit; + +class PushoverSettings : public Snore::PluginSettingsWidget +{ + Q_OBJECT +public: + explicit PushoverSettings(Snore::SnorePlugin *plugin, QWidget *parent = 0); + ~PushoverSettings(); + + void load() override; + void save() override; + +private: + QLineEdit *m_keyLineEdit; + QLineEdit *m_soundLineEdit; + QLineEdit *m_deviceLineEdit; + +}; + +#endif // PUSHOVERSETTINGS_H diff --git a/src/settings/main.cpp b/src/settings/main.cpp index 6654670..e471de3 100644 --- a/src/settings/main.cpp +++ b/src/settings/main.cpp @@ -53,7 +53,7 @@ void listSettings(SettingsType type, const QString &application) int main(int argc, char *argv[]) { - QScopedPointer window; + SettingsWindow *window; QApplication app(argc, argv); app.setApplicationName("SnoreSettings"); @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) } else if (parser.isSet(listSettingsCommand)) { listSettings(type, parser.value(appNameCommand)); } else if (parser.optionNames().empty() && parser.positionalArguments().empty()) { - window.reset(new SettingsWindow()); + window = new SettingsWindow(); window->show(); return app.exec(); } else {