Skip to content

Commit

Permalink
system tray notification
Browse files Browse the repository at this point in the history
  • Loading branch information
bigeagle committed May 27, 2015
1 parent 4b6bca6 commit 4b96d56
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/danmaQ_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ void DMApp::toggle_subscription() {
this->subscriber, SIGNAL(finished()),
this, SLOT(on_subscription_stopped())
);
connect(
this->subscriber, SIGNAL(new_alert(QString)),
this, SLOT(on_new_alert(QString))
);
this->subscriber->start();

} else {
Expand Down Expand Up @@ -141,6 +145,7 @@ void DMApp::on_subscription_started() {
this->hide();
this->trayIcon->set_icon_running();
this->mainBtn->setText("&Unsubscribe");
this->trayIcon->showMessage("Subscription Started", "Let's Go");
}

void DMApp::on_subscription_stopped() {
Expand All @@ -151,6 +156,12 @@ void DMApp::on_subscription_stopped() {

void DMApp::on_new_alert(QString msg) {
myDebug << "Alert:" << msg;
this->trayIcon->showMessage("Ooops!", msg, QSystemTrayIcon::Critical);
this->subscriber->mark_stop = true;
emit stop_subscription();
if (this->subscriber->wait(1000) == false) {
this->subscriber->terminate();
}
}

void DMApp::show_about_dialog() {
Expand Down
29 changes: 25 additions & 4 deletions src/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <QUuid>
#include <QHttp>
#include <QHttpHeader>
#include <QUrl>

#include <QByteArray>
Expand Down Expand Up @@ -47,7 +48,7 @@ Subscriber::Subscriber(QString server, QString channel, QString passwd, QObject*
header.setValue("X-GDANMAKU-SUBSCRIBER-ID", this->_uuid);
header.setValue("X-GDANMAKU-AUTH-KEY", this->passwd);

connect(http, SIGNAL(done(bool)), this, SLOT(parse_response(bool)));
// connect(http, SIGNAL(done(bool)), this, SLOT(parse_response(bool)));
connect(this, SIGNAL(terminated()), this, SLOT(deleteLater()));
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
}
Expand Down Expand Up @@ -79,14 +80,34 @@ void Subscriber::run()
if(http->error()){
myDebug << http->errorString() << "Wait 2 secs";
this->msleep(2000);
} else {
parse_response();
}
}
}


void Subscriber::parse_response(bool error) {
if (error) {
return;
void Subscriber::parse_response() {
QHttpResponseHeader resp = http->lastResponse();
if(resp.isValid()) {
bool fatal = false;
int statusCode = resp.statusCode();
if (statusCode >= 400) {
fatal = true;
QString errMsg;
if (statusCode == 403 ) {
errMsg = "Wrong Password";
} else if (statusCode == 404) {
errMsg = "No Such Channel";
} else if (statusCode >= 500) {
errMsg = "Server Error";
}
myDebug << errMsg;
emit new_alert(errMsg);
}
if (fatal) {
return;
}
}

bool ok;
Expand Down
2 changes: 1 addition & 1 deletion src/subscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Subscriber : public QThread
bool mark_stop;

public slots:
void parse_response(bool);
void parse_response();

signals:
void new_danmaku(QString text, QString color, QString position);
Expand Down

0 comments on commit 4b96d56

Please sign in to comment.