mirror of
https://github.com/status-im/snorenotify.git
synced 2025-01-09 16:15:45 +00:00
make pushover frontend 10% more clean
This commit is contained in:
parent
8a30deef6b
commit
7fb29fe49e
@ -224,6 +224,7 @@ void SnoreCore::setDefaultValue(const QString &key, const QVariant &value, Setti
|
||||
Q_D(SnoreCore);
|
||||
QString nk = d->normalizeKey(key, type);
|
||||
if (!d->m_settings->contains(nk)) {
|
||||
snoreDebug(SNORE_DEBUG) << "Set default value" << nk << value;
|
||||
d->m_settings->setValue(nk, value);
|
||||
}
|
||||
}
|
||||
|
@ -80,19 +80,19 @@ bool GrowlBackend::deinitialize()
|
||||
|
||||
void GrowlBackend::slotRegisterApplication(const Application &application)
|
||||
{
|
||||
// snoreDebug( SNORE_DEBUG ) << application.name().toUtf8().constData();
|
||||
snoreDebug( SNORE_DEBUG ) << application.name();
|
||||
std::vector<std::string> alerts;
|
||||
foreach(const Alert & a, application.alerts()) {
|
||||
snoreDebug(SNORE_DEBUG) << a.name().toUtf8().constData();
|
||||
snoreDebug(SNORE_DEBUG) << a.name();
|
||||
alerts.push_back(a.name().toUtf8().constData());
|
||||
}
|
||||
|
||||
Growl *growl = new Growl(GROWL_TCP, value(QLatin1String("Host")).toString().toUtf8().constData(),
|
||||
value(QLatin1String("Password")).toString().toUtf8().constData(),
|
||||
application.name().toUtf8().constData());
|
||||
m_applications.insert(application.name(), growl);
|
||||
growl->Register(alerts, application.icon().localUrl().toUtf8().constData());
|
||||
|
||||
m_applications.insert(application.name(), growl);
|
||||
}
|
||||
|
||||
void GrowlBackend::slotDeregisterApplication(const Application &application)
|
||||
|
@ -38,20 +38,26 @@ using namespace Snore;
|
||||
// TODO: use qtkeychain to encrypt credentials?
|
||||
// TODO: massive refactoring ...
|
||||
|
||||
PushoverFrontend::PushoverFrontend()
|
||||
{
|
||||
connect(this, &PushoverFrontend::loggedInChanged, [this](bool state){
|
||||
m_loggedIn = state;
|
||||
});
|
||||
connect(this, &PushoverFrontend::error, [this](QString error){
|
||||
m_errorMessage = error;
|
||||
});
|
||||
}
|
||||
|
||||
bool PushoverFrontend::initialize()
|
||||
{
|
||||
setDefaultValue(QLatin1String("Secret"), QString(), LOCAL_SETTING);
|
||||
setDefaultValue(QLatin1String("DeviceID"), QString(), LOCAL_SETTING);
|
||||
setDefaultValue(QLatin1String("Registered"), false, LOCAL_SETTING);
|
||||
|
||||
if(!SnoreFrontend::initialize()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(value(QLatin1String("Registered"), LOCAL_SETTING).toBool())
|
||||
{
|
||||
connectToService();
|
||||
}
|
||||
connectToService();
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -73,8 +79,9 @@ PluginSettingsWidget *PushoverFrontend::settingsWidget()
|
||||
return new PushoverSettings(this);
|
||||
}
|
||||
|
||||
void PushoverFrontend::registerDevice(const QString &email, const QString &password, const QString &deviceName)
|
||||
void PushoverFrontend::login(const QString &email, const QString &password, const QString &deviceName)
|
||||
{
|
||||
setValue(QLatin1String("DeviceName"), deviceName, Snore::LOCAL_SETTING);
|
||||
|
||||
QNetworkRequest request(QUrl(QLatin1String("https://api.pushover.net/1/users/login.json")));
|
||||
|
||||
@ -93,39 +100,33 @@ void PushoverFrontend::registerDevice(const QString &email, const QString &passw
|
||||
|
||||
if(message.value(QLatin1String("status")).toInt() == 1)
|
||||
{
|
||||
QString secret = message.value(QLatin1String("secret")).toString();
|
||||
QNetworkRequest request(QUrl(QLatin1String("https://api.pushover.net/1/devices.json")));
|
||||
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(QLatin1String("application/x-www-form-urlencoded")));
|
||||
QNetworkReply *reply = m_manager.post(request, QString(QLatin1String("secret=%1&name=%2&os=O")).arg(secret, deviceName).toUtf8().constData());
|
||||
|
||||
|
||||
connect(reply, &QNetworkReply::finished, [reply, secret, this]() {
|
||||
snoreDebug(SNORE_DEBUG) << reply->error();
|
||||
QByteArray input = reply->readAll();
|
||||
reply->close();
|
||||
reply->deleteLater();
|
||||
|
||||
|
||||
QJsonObject message = QJsonDocument::fromJson(input).object();
|
||||
|
||||
|
||||
if(message.value(QLatin1String("status")).toInt() == 1) {
|
||||
setValue(QLatin1String("Secret"), secret, LOCAL_SETTING);
|
||||
setValue(QLatin1String("DeviceID"), message.value(QLatin1String("id")).toString(), LOCAL_SETTING);
|
||||
setValue(QLatin1String("Registered"), true, LOCAL_SETTING);
|
||||
connectToService();
|
||||
} else {
|
||||
snoreDebug(SNORE_WARNING) << "An error occure" << input;
|
||||
}
|
||||
|
||||
});
|
||||
registerDevice(message.value(QLatin1String("secret")).toString(), deviceName);
|
||||
}else {
|
||||
snoreDebug(SNORE_WARNING) << "An error occure" << input;
|
||||
emit loggedInChanged(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void PushoverFrontend::logOut()
|
||||
{
|
||||
setValue(QLatin1String("Secret"), QString(), LOCAL_SETTING);
|
||||
setValue(QLatin1String("DeviceID"), QString(), LOCAL_SETTING);
|
||||
m_socket->close();
|
||||
m_socket->deleteLater();
|
||||
emit loggedInChanged(false);
|
||||
}
|
||||
|
||||
bool PushoverFrontend::isLoggedIn() const
|
||||
{
|
||||
return m_loggedIn;
|
||||
}
|
||||
|
||||
QString PushoverFrontend::errorMessage()
|
||||
{
|
||||
return m_errorMessage;
|
||||
}
|
||||
|
||||
void PushoverFrontend::slotActionInvoked(Notification notification)
|
||||
{
|
||||
if(notification.priority() == Notification::EMERGENCY){
|
||||
@ -146,7 +147,7 @@ QString PushoverFrontend::device()
|
||||
|
||||
void PushoverFrontend::connectToService()
|
||||
{
|
||||
if(!value(QLatin1String("Registered"), LOCAL_SETTING).toBool())
|
||||
if(secret().isEmpty() || device().isEmpty())
|
||||
{
|
||||
snoreDebug(SNORE_WARNING) << "not logged in";
|
||||
return;
|
||||
@ -169,8 +170,9 @@ void PushoverFrontend::connectToService()
|
||||
connectToService();
|
||||
break;
|
||||
case 'E':
|
||||
snoreDebug(SNORE_DEBUG) << "Connection Error";
|
||||
setValue(QLatin1String("Registered"), false, LOCAL_SETTING);
|
||||
snoreDebug(SNORE_WARNING) << "Connection Error";
|
||||
emit error(QLatin1String("Please Loggin to https://pushover.net and reanble your device."));
|
||||
emit loggedInChanged(false);
|
||||
m_socket->close();
|
||||
m_socket->deleteLater();
|
||||
break;
|
||||
@ -182,16 +184,47 @@ void PushoverFrontend::connectToService()
|
||||
snoreDebug(SNORE_DEBUG) << "disconnected";
|
||||
});
|
||||
connect(m_socket, static_cast<void (QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), [&](QAbstractSocket::SocketError error){
|
||||
snoreDebug(SNORE_DEBUG) << error << m_socket->errorString();
|
||||
snoreDebug(SNORE_WARNING) << error << m_socket->errorString();
|
||||
emit loggedInChanged(false);
|
||||
});
|
||||
connect(m_socket, &QWebSocket::connected, [&](){
|
||||
snoreDebug(SNORE_DEBUG) << "connecting";
|
||||
m_socket->sendBinaryMessage((QLatin1String("login:=") + device() + QLatin1Char(':') + secret() + QLatin1Char('\n')).toUtf8().constData());
|
||||
m_socket->sendBinaryMessage((QLatin1String("login:") + device() + QLatin1Char(':') + secret() + QLatin1Char('\n')).toUtf8().constData());
|
||||
emit loggedInChanged(true);
|
||||
getMessages();
|
||||
});
|
||||
m_socket->open(QUrl::fromEncoded("wss://client.pushover.net/push"));
|
||||
}
|
||||
|
||||
void PushoverFrontend::registerDevice(const QString &secret, const QString &deviceName)
|
||||
{
|
||||
QNetworkRequest request(QUrl(QLatin1String("https://api.pushover.net/1/devices.json")));
|
||||
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(QLatin1String("application/x-www-form-urlencoded")));
|
||||
QNetworkReply *reply = m_manager.post(request, (QLatin1String("os=O&secret=") + secret + QLatin1String("&name=") + deviceName).toUtf8().constData());
|
||||
|
||||
|
||||
connect(reply, &QNetworkReply::finished, [reply, secret, this]() {
|
||||
snoreDebug(SNORE_DEBUG) << reply->error();
|
||||
QByteArray input = reply->readAll();
|
||||
reply->close();
|
||||
reply->deleteLater();
|
||||
QJsonObject message = QJsonDocument::fromJson(input).object();
|
||||
if(message.value(QLatin1String("status")).toInt() == 1) {
|
||||
setValue(QLatin1String("Secret"), secret, LOCAL_SETTING);
|
||||
setValue(QLatin1String("DeviceID"), message.value(QLatin1String("id")).toString(), LOCAL_SETTING);;
|
||||
connectToService();
|
||||
} else {
|
||||
snoreDebug(SNORE_WARNING) << "An error occure" << input;
|
||||
emit loggedInChanged(false);
|
||||
emit error(message.value(QLatin1String("error")).toString());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
void PushoverFrontend::getMessages()
|
||||
{
|
||||
QNetworkRequest request(QUrl::fromEncoded((QLatin1String("https://api.pushover.net/1/messages.json?"
|
||||
|
@ -32,35 +32,45 @@ class PushoverFrontend : public Snore::SnoreFrontend
|
||||
Q_INTERFACES(Snore::SnoreFrontend)
|
||||
Q_PLUGIN_METADATA(IID "org.Snore.NotificationFrontend/1.0" FILE "plugin.json")
|
||||
public:
|
||||
PushoverFrontend() = default;
|
||||
PushoverFrontend();
|
||||
~PushoverFrontend() = default;
|
||||
bool initialize() override;
|
||||
bool deinitialize() override;
|
||||
|
||||
Snore::PluginSettingsWidget *settingsWidget() override;
|
||||
|
||||
void registerDevice(const QString &email, const QString &password, const QString& deviceName);
|
||||
void login(const QString &email, const QString &password, const QString& deviceName);
|
||||
void logOut();
|
||||
|
||||
bool isLoggedIn() const;
|
||||
QString errorMessage();
|
||||
|
||||
public Q_SLOTS:
|
||||
void slotActionInvoked(Snore::Notification notification);
|
||||
|
||||
|
||||
Q_SIGNALS:
|
||||
void loggedInChanged(bool isLoggedIn);
|
||||
void error(QString error);
|
||||
|
||||
private:
|
||||
QNetworkAccessManager m_manager;
|
||||
QPointer<QWebSocket> m_socket;
|
||||
bool m_loggedIn = false;
|
||||
QString m_errorMessage;
|
||||
|
||||
QString secret();
|
||||
QString device();
|
||||
|
||||
void connectToService();
|
||||
|
||||
void registerDevice(const QString &secret, const QString &deviceName);
|
||||
void getMessages();
|
||||
void deleteMessages(int latestMessageId);
|
||||
void acknowledgeNotification(Snore::Notification notification);
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif//PUSHOVER_FRONTEND_H
|
||||
|
@ -22,41 +22,46 @@
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QTimer>
|
||||
#include <QLabel>
|
||||
|
||||
PushoverSettings::PushoverSettings(Snore::SnorePlugin *plugin, QWidget *parent) :
|
||||
Snore::PluginSettingsWidget(plugin, parent),
|
||||
m_emailLineEdit(new QLineEdit(this)),
|
||||
m_passwordLineEdit(new QLineEdit(this)),
|
||||
m_deviceLineEdit(new QLineEdit(this)),
|
||||
m_registerButton(new QPushButton(this))
|
||||
m_registerButton(new QPushButton(this)),
|
||||
m_errorMessageLabel(new QLabel(this))
|
||||
{
|
||||
m_passwordLineEdit->setEchoMode(QLineEdit::Password);
|
||||
addRow(tr("Email Address:"), m_emailLineEdit);
|
||||
addRow(tr("Password:"), m_passwordLineEdit);
|
||||
addRow(tr("Device Name:"), m_deviceLineEdit);
|
||||
updateLoginState();
|
||||
addRow(QString(), m_registerButton);
|
||||
addRow(tr("Status"), m_errorMessageLabel);
|
||||
addRow(QString(), new QLabel(this));
|
||||
addRow(QString(), new QLabel(tr("If you don't have an accout yet please register at <a href=\"https://pushover.net\">Pushover.net</a>"),this));
|
||||
|
||||
m_emailLineEdit->setEnabled(false);
|
||||
m_passwordLineEdit->setEnabled(false);
|
||||
m_deviceLineEdit->setEnabled(false);
|
||||
m_registerButton->setEnabled(false);
|
||||
|
||||
PushoverFrontend *pushover = dynamic_cast<PushoverFrontend*>(plugin);
|
||||
connect(m_registerButton, &QPushButton::clicked, [pushover, this] () {
|
||||
if(!value(QLatin1String("Registered"), Snore::LOCAL_SETTING).toBool()) {
|
||||
pushover->registerDevice(m_emailLineEdit->text(), m_passwordLineEdit->text(), m_deviceLineEdit->text());
|
||||
setValue(QLatin1String("DeviceName"), m_deviceLineEdit->text(), Snore::LOCAL_SETTING);
|
||||
QTimer *updateTimer = new QTimer(this);
|
||||
updateTimer->setInterval(500);
|
||||
connect(updateTimer, &QTimer::timeout, [updateTimer, this](){
|
||||
qDebug() << value(QLatin1String("Registered")).toBool();
|
||||
if (value(QLatin1String("Registered"), Snore::LOCAL_SETTING).toBool()) {
|
||||
updateLoginState();
|
||||
updateTimer->deleteLater();
|
||||
}
|
||||
});
|
||||
m_errorMessageLabel->setText(pushover->errorMessage());
|
||||
|
||||
updateTimer->start();
|
||||
connect(pushover, &PushoverFrontend::loggedInChanged, this, &PushoverSettings::slotUpdateLoginState);
|
||||
connect(pushover, &PushoverFrontend::error, [this](QString message){
|
||||
m_errorMessageLabel->setText(message);
|
||||
});
|
||||
|
||||
slotUpdateLoginState(pushover->isLoggedIn());
|
||||
|
||||
connect(m_registerButton, &QPushButton::clicked, [pushover, this] () {
|
||||
m_registerButton->setEnabled(false);
|
||||
if(!pushover->isLoggedIn()) {
|
||||
pushover->login(m_emailLineEdit->text(), m_passwordLineEdit->text(), m_deviceLineEdit->text());
|
||||
}else{
|
||||
setValue(QLatin1String("Registered"), false, Snore::LOCAL_SETTING);
|
||||
updateLoginState();
|
||||
pushover->logOut();
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -74,17 +79,22 @@ void PushoverSettings::save()
|
||||
{
|
||||
}
|
||||
|
||||
void PushoverSettings::updateLoginState()
|
||||
void PushoverSettings::slotUpdateLoginState(bool state)
|
||||
{
|
||||
if (value(QLatin1String("Registered"), Snore::LOCAL_SETTING).toBool()) {
|
||||
if (state) {
|
||||
m_emailLineEdit->setEnabled(false);
|
||||
m_passwordLineEdit->setEnabled(false);
|
||||
m_deviceLineEdit->setEnabled(false);
|
||||
m_registerButton->setText(tr("Log out"));
|
||||
m_errorMessageLabel->setText(tr("Logged in."));
|
||||
|
||||
} else {
|
||||
m_emailLineEdit->setEnabled(true);
|
||||
m_passwordLineEdit->setEnabled(true);
|
||||
m_deviceLineEdit->setEnabled(true);
|
||||
m_registerButton->setText(tr("Log in"));
|
||||
|
||||
m_errorMessageLabel->setText(tr("Logged out."));
|
||||
}
|
||||
m_registerButton->setEnabled(true);
|
||||
}
|
||||
|
@ -22,7 +22,7 @@
|
||||
|
||||
class QLineEdit;
|
||||
class QPushButton;
|
||||
|
||||
class QLabel;
|
||||
|
||||
class PushoverSettings : public Snore::PluginSettingsWidget
|
||||
{
|
||||
@ -39,8 +39,10 @@ private:
|
||||
QLineEdit *m_passwordLineEdit;
|
||||
QLineEdit *m_deviceLineEdit;
|
||||
QPushButton *m_registerButton;
|
||||
QLabel *m_errorMessageLabel;
|
||||
|
||||
void updateLoginState();
|
||||
private Q_SLOTS:
|
||||
void slotUpdateLoginState(bool state);
|
||||
|
||||
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user