make pushover frontend 10% more clean

This commit is contained in:
Patrick von Reth 2015-07-03 11:32:37 +02:00
parent 8a30deef6b
commit 7fb29fe49e
6 changed files with 123 additions and 67 deletions

View File

@ -224,6 +224,7 @@ void SnoreCore::setDefaultValue(const QString &key, const QVariant &value, Setti
Q_D(SnoreCore); Q_D(SnoreCore);
QString nk = d->normalizeKey(key, type); QString nk = d->normalizeKey(key, type);
if (!d->m_settings->contains(nk)) { if (!d->m_settings->contains(nk)) {
snoreDebug(SNORE_DEBUG) << "Set default value" << nk << value;
d->m_settings->setValue(nk, value); d->m_settings->setValue(nk, value);
} }
} }

View File

@ -80,19 +80,19 @@ bool GrowlBackend::deinitialize()
void GrowlBackend::slotRegisterApplication(const Application &application) void GrowlBackend::slotRegisterApplication(const Application &application)
{ {
// snoreDebug( SNORE_DEBUG ) << application.name().toUtf8().constData(); snoreDebug( SNORE_DEBUG ) << application.name();
std::vector<std::string> alerts; std::vector<std::string> alerts;
foreach(const Alert & a, application.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()); alerts.push_back(a.name().toUtf8().constData());
} }
Growl *growl = new Growl(GROWL_TCP, value(QLatin1String("Host")).toString().toUtf8().constData(), Growl *growl = new Growl(GROWL_TCP, value(QLatin1String("Host")).toString().toUtf8().constData(),
value(QLatin1String("Password")).toString().toUtf8().constData(), value(QLatin1String("Password")).toString().toUtf8().constData(),
application.name().toUtf8().constData()); application.name().toUtf8().constData());
m_applications.insert(application.name(), growl);
growl->Register(alerts, application.icon().localUrl().toUtf8().constData()); growl->Register(alerts, application.icon().localUrl().toUtf8().constData());
m_applications.insert(application.name(), growl);
} }
void GrowlBackend::slotDeregisterApplication(const Application &application) void GrowlBackend::slotDeregisterApplication(const Application &application)

View File

@ -38,20 +38,26 @@ using namespace Snore;
// TODO: use qtkeychain to encrypt credentials? // TODO: use qtkeychain to encrypt credentials?
// TODO: massive refactoring ... // 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() bool PushoverFrontend::initialize()
{ {
setDefaultValue(QLatin1String("Secret"), QString(), LOCAL_SETTING); setDefaultValue(QLatin1String("Secret"), QString(), LOCAL_SETTING);
setDefaultValue(QLatin1String("DeviceID"), QString(), LOCAL_SETTING); setDefaultValue(QLatin1String("DeviceID"), QString(), LOCAL_SETTING);
setDefaultValue(QLatin1String("Registered"), false, LOCAL_SETTING);
if(!SnoreFrontend::initialize()) { if(!SnoreFrontend::initialize()) {
return false; return false;
} }
if(value(QLatin1String("Registered"), LOCAL_SETTING).toBool())
{
connectToService(); connectToService();
}
return true; return true;
} }
@ -73,8 +79,9 @@ PluginSettingsWidget *PushoverFrontend::settingsWidget()
return new PushoverSettings(this); 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"))); QNetworkRequest request(QUrl(QLatin1String("https://api.pushover.net/1/users/login.json")));
@ -93,37 +100,31 @@ void PushoverFrontend::registerDevice(const QString &email, const QString &passw
if(message.value(QLatin1String("status")).toInt() == 1) if(message.value(QLatin1String("status")).toInt() == 1)
{ {
QString secret = message.value(QLatin1String("secret")).toString(); registerDevice(message.value(QLatin1String("secret")).toString(), 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, 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 { }else {
snoreDebug(SNORE_WARNING) << "An error occure" << input; snoreDebug(SNORE_WARNING) << "An error occure" << input;
emit loggedInChanged(false);
}
});
} }
}); void PushoverFrontend::logOut()
}else { {
snoreDebug(SNORE_WARNING) << "An error occure" << input; 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) void PushoverFrontend::slotActionInvoked(Notification notification)
@ -146,7 +147,7 @@ QString PushoverFrontend::device()
void PushoverFrontend::connectToService() void PushoverFrontend::connectToService()
{ {
if(!value(QLatin1String("Registered"), LOCAL_SETTING).toBool()) if(secret().isEmpty() || device().isEmpty())
{ {
snoreDebug(SNORE_WARNING) << "not logged in"; snoreDebug(SNORE_WARNING) << "not logged in";
return; return;
@ -169,8 +170,9 @@ void PushoverFrontend::connectToService()
connectToService(); connectToService();
break; break;
case 'E': case 'E':
snoreDebug(SNORE_DEBUG) << "Connection Error"; snoreDebug(SNORE_WARNING) << "Connection Error";
setValue(QLatin1String("Registered"), false, LOCAL_SETTING); emit error(QLatin1String("Please Loggin to https://pushover.net and reanble your device."));
emit loggedInChanged(false);
m_socket->close(); m_socket->close();
m_socket->deleteLater(); m_socket->deleteLater();
break; break;
@ -182,16 +184,47 @@ void PushoverFrontend::connectToService()
snoreDebug(SNORE_DEBUG) << "disconnected"; snoreDebug(SNORE_DEBUG) << "disconnected";
}); });
connect(m_socket, static_cast<void (QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), [&](QAbstractSocket::SocketError error){ 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, [&](){ connect(m_socket, &QWebSocket::connected, [&](){
snoreDebug(SNORE_DEBUG) << "connecting"; 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(); getMessages();
}); });
m_socket->open(QUrl::fromEncoded("wss://client.pushover.net/push")); 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() void PushoverFrontend::getMessages()
{ {
QNetworkRequest request(QUrl::fromEncoded((QLatin1String("https://api.pushover.net/1/messages.json?" QNetworkRequest request(QUrl::fromEncoded((QLatin1String("https://api.pushover.net/1/messages.json?"

View File

@ -32,35 +32,45 @@ class PushoverFrontend : public Snore::SnoreFrontend
Q_INTERFACES(Snore::SnoreFrontend) Q_INTERFACES(Snore::SnoreFrontend)
Q_PLUGIN_METADATA(IID "org.Snore.NotificationFrontend/1.0" FILE "plugin.json") Q_PLUGIN_METADATA(IID "org.Snore.NotificationFrontend/1.0" FILE "plugin.json")
public: public:
PushoverFrontend() = default; PushoverFrontend();
~PushoverFrontend() = default; ~PushoverFrontend() = default;
bool initialize() override; bool initialize() override;
bool deinitialize() override; bool deinitialize() override;
Snore::PluginSettingsWidget *settingsWidget() 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: public Q_SLOTS:
void slotActionInvoked(Snore::Notification notification); void slotActionInvoked(Snore::Notification notification);
Q_SIGNALS:
void loggedInChanged(bool isLoggedIn);
void error(QString error);
private: private:
QNetworkAccessManager m_manager; QNetworkAccessManager m_manager;
QPointer<QWebSocket> m_socket; QPointer<QWebSocket> m_socket;
bool m_loggedIn = false;
QString m_errorMessage;
QString secret(); QString secret();
QString device(); QString device();
void connectToService(); void connectToService();
void registerDevice(const QString &secret, const QString &deviceName);
void getMessages(); void getMessages();
void deleteMessages(int latestMessageId); void deleteMessages(int latestMessageId);
void acknowledgeNotification(Snore::Notification notification); void acknowledgeNotification(Snore::Notification notification);
}; };
#endif//PUSHOVER_FRONTEND_H #endif//PUSHOVER_FRONTEND_H

View File

@ -22,41 +22,46 @@
#include <QLineEdit> #include <QLineEdit>
#include <QPushButton> #include <QPushButton>
#include <QTimer> #include <QLabel>
PushoverSettings::PushoverSettings(Snore::SnorePlugin *plugin, QWidget *parent) : PushoverSettings::PushoverSettings(Snore::SnorePlugin *plugin, QWidget *parent) :
Snore::PluginSettingsWidget(plugin, parent), Snore::PluginSettingsWidget(plugin, parent),
m_emailLineEdit(new QLineEdit(this)), m_emailLineEdit(new QLineEdit(this)),
m_passwordLineEdit(new QLineEdit(this)), m_passwordLineEdit(new QLineEdit(this)),
m_deviceLineEdit(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); m_passwordLineEdit->setEchoMode(QLineEdit::Password);
addRow(tr("Email Address:"), m_emailLineEdit); addRow(tr("Email Address:"), m_emailLineEdit);
addRow(tr("Password:"), m_passwordLineEdit); addRow(tr("Password:"), m_passwordLineEdit);
addRow(tr("Device Name:"), m_deviceLineEdit); addRow(tr("Device Name:"), m_deviceLineEdit);
updateLoginState();
addRow(QString(), m_registerButton); 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); PushoverFrontend *pushover = dynamic_cast<PushoverFrontend*>(plugin);
connect(m_registerButton, &QPushButton::clicked, [pushover, this] () { m_errorMessageLabel->setText(pushover->errorMessage());
if(!value(QLatin1String("Registered"), Snore::LOCAL_SETTING).toBool()) {
pushover->registerDevice(m_emailLineEdit->text(), m_passwordLineEdit->text(), m_deviceLineEdit->text()); connect(pushover, &PushoverFrontend::loggedInChanged, this, &PushoverSettings::slotUpdateLoginState);
setValue(QLatin1String("DeviceName"), m_deviceLineEdit->text(), Snore::LOCAL_SETTING); connect(pushover, &PushoverFrontend::error, [this](QString message){
QTimer *updateTimer = new QTimer(this); m_errorMessageLabel->setText(message);
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();
}
}); });
updateTimer->start(); 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{ }else{
setValue(QLatin1String("Registered"), false, Snore::LOCAL_SETTING); pushover->logOut();
updateLoginState();
} }
}); });
} }
@ -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_emailLineEdit->setEnabled(false);
m_passwordLineEdit->setEnabled(false); m_passwordLineEdit->setEnabled(false);
m_deviceLineEdit->setEnabled(false); m_deviceLineEdit->setEnabled(false);
m_registerButton->setText(tr("Log out")); m_registerButton->setText(tr("Log out"));
m_errorMessageLabel->setText(tr("Logged in."));
} else { } else {
m_emailLineEdit->setEnabled(true); m_emailLineEdit->setEnabled(true);
m_passwordLineEdit->setEnabled(true); m_passwordLineEdit->setEnabled(true);
m_deviceLineEdit->setEnabled(true); m_deviceLineEdit->setEnabled(true);
m_registerButton->setText(tr("Log in")); m_registerButton->setText(tr("Log in"));
m_errorMessageLabel->setText(tr("Logged out."));
} }
m_registerButton->setEnabled(true);
} }

View File

@ -22,7 +22,7 @@
class QLineEdit; class QLineEdit;
class QPushButton; class QPushButton;
class QLabel;
class PushoverSettings : public Snore::PluginSettingsWidget class PushoverSettings : public Snore::PluginSettingsWidget
{ {
@ -39,8 +39,10 @@ private:
QLineEdit *m_passwordLineEdit; QLineEdit *m_passwordLineEdit;
QLineEdit *m_deviceLineEdit; QLineEdit *m_deviceLineEdit;
QPushButton *m_registerButton; QPushButton *m_registerButton;
QLabel *m_errorMessageLabel;
void updateLoginState(); private Q_SLOTS:
void slotUpdateLoginState(bool state);
}; };