diff options
author | Max Christian Pohle | 2015-11-18 04:12:48 +0100 |
---|---|---|
committer | Max Christian Pohle | 2015-11-18 04:12:48 +0100 |
commit | de6cb045fd2b237e58320f0da7f3a365ca4d2ace (patch) | |
tree | 2bc3a13c3308aadaa0e92c556d587c60f8d718de /mainwindow.cpp | |
download | qspacestatus-de6cb045fd2b237e58320f0da7f3a365ca4d2ace.tar.bz2 qspacestatus-de6cb045fd2b237e58320f0da7f3a365ca4d2ace.zip |
downloads+parses json and displays systemtray icon
can be activated by a single click and shows the corresponding window.
Diffstat (limited to 'mainwindow.cpp')
-rw-r--r-- | mainwindow.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..0dd8a3e --- /dev/null +++ b/mainwindow.cpp | |||
@@ -0,0 +1,95 @@ | |||
1 | #include "mainwindow.h" | ||
2 | #include "ui_mainwindow.h" | ||
3 | #include <QDebug> | ||
4 | |||
5 | MainWindow::MainWindow(QWidget *parent) : | ||
6 | QMainWindow(parent), | ||
7 | ui(new Ui::MainWindow) | ||
8 | |||
9 | { | ||
10 | ui->setupUi(this); | ||
11 | |||
12 | QIcon* icon = new QIcon("folder.png"); | ||
13 | this->qsystemtrayicon = new QSystemTrayIcon(*icon, this); | ||
14 | this->setWindowIcon(*icon); | ||
15 | |||
16 | this->qsystemtrayicon->show(); | ||
17 | this->qsystemtrayicon->setContextMenu(ui->menuMenu1); | ||
18 | //this->connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systray_clicked(QSystemTrayIcon::ActivationReason))); | ||
19 | this->connect(qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); | ||
20 | |||
21 | //this->qsystemtrayicon->installEventFilter(this); | ||
22 | |||
23 | //this->qsystemtrayicon->setIcon(this->windowIcon()); | ||
24 | this->qsystemtrayicon->setToolTip(this->windowTitle()); | ||
25 | |||
26 | QUrl url("http://hackerspace-bielefeld.de/spacestatus/status.json"); | ||
27 | QByteArray rawData = this->download(url); | ||
28 | QJsonDocument doc = QJsonDocument::fromJson(rawData); | ||
29 | //qDebug() << rawData; | ||
30 | //qDebug() << doc.toJson(QJsonDocument::Compact); | ||
31 | |||
32 | QJsonObject json = doc.object(); | ||
33 | |||
34 | QString strUrl; | ||
35 | if(json["state/open"].toBool()) | ||
36 | { | ||
37 | strUrl = json["state"].toObject()["icon"].toObject()["open"].toString(); | ||
38 | this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geƶffnet!"); | ||
39 | } | ||
40 | else | ||
41 | { | ||
42 | strUrl = json["state"].toObject()["icon"].toObject()["closed"].toString(); | ||
43 | this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geschlossen!"); | ||
44 | } | ||
45 | |||
46 | qDebug() << "downloading: " << strUrl; | ||
47 | QByteArray imageArray = this->download(QUrl(strUrl)); | ||
48 | //qDebug("test: " + imageArray.size()); | ||
49 | QImage image = QImage::fromData(imageArray); | ||
50 | |||
51 | this->currentIcon = new QIcon(QPixmap::fromImage(image)); | ||
52 | this->setWindowIcon(*currentIcon); | ||
53 | this->qsystemtrayicon->setIcon(*currentIcon); | ||
54 | this->setWindowTitle("closed"); | ||
55 | |||
56 | qDebug() << json["state", "open"]; | ||
57 | } | ||
58 | |||
59 | MainWindow::~MainWindow() | ||
60 | { | ||
61 | delete ui; | ||
62 | } | ||
63 | |||
64 | QByteArray MainWindow::download(QUrl url) | ||
65 | { | ||
66 | QNetworkAccessManager manager; | ||
67 | QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url))); | ||
68 | QEventLoop event; | ||
69 | connect(response,SIGNAL(finished()),&event,SLOT(quit())); | ||
70 | event.exec(); | ||
71 | |||
72 | return response->readAll(); | ||
73 | } | ||
74 | |||
75 | void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) | ||
76 | { | ||
77 | if(reason == QSystemTrayIcon::Trigger) | ||
78 | { | ||
79 | qDebug() << "trigger"; | ||
80 | this->setWindowState(Qt::WindowNoState); | ||
81 | this->show(); | ||
82 | this->activateWindow(); | ||
83 | |||
84 | } | ||
85 | else | ||
86 | { | ||
87 | qDebug() << "activate(reason): " << reason; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | void MainWindow::exitApplication() | ||
92 | { | ||
93 | qDebug() << "exit now!"; | ||
94 | exit(0); | ||
95 | } | ||