diff options
| author | Max Christian Pohle | 2015-11-20 16:09:06 +0100 |
|---|---|---|
| committer | Max Christian Pohle | 2015-11-20 16:09:06 +0100 |
| commit | 64ae6f0db1e8af1bc287c52b010fe87cc5380191 (patch) | |
| tree | ec897be3df20df8a23458713def59cfb71d03e37 /mainwindow.cpp | |
| parent | de6cb045fd2b237e58320f0da7f3a365ca4d2ace (diff) | |
| download | qspacestatus-64ae6f0db1e8af1bc287c52b010fe87cc5380191.tar.bz2 qspacestatus-64ae6f0db1e8af1bc287c52b010fe87cc5380191.zip | |
displays space status in system tray
successfully tested under linux and windows
Diffstat (limited to 'mainwindow.cpp')
| -rw-r--r-- | mainwindow.cpp | 148 |
1 files changed, 113 insertions, 35 deletions
diff --git a/mainwindow.cpp b/mainwindow.cpp index 0dd8a3e..2590d4f 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp | |||
| @@ -5,81 +5,152 @@ | |||
| 5 | MainWindow::MainWindow(QWidget *parent) : | 5 | MainWindow::MainWindow(QWidget *parent) : |
| 6 | QMainWindow(parent), | 6 | QMainWindow(parent), |
| 7 | ui(new Ui::MainWindow) | 7 | ui(new Ui::MainWindow) |
| 8 | |||
| 9 | { | 8 | { |
| 10 | ui->setupUi(this); | 9 | ui->setupUi(this); |
| 10 | ui->menuBar->setVisible(false); | ||
| 11 | 11 | ||
| 12 | QIcon* icon = new QIcon("folder.png"); | 12 | qDebug() << "Using: " << this->qsettings.fileName(); |
| 13 | this->qsystemtrayicon = new QSystemTrayIcon(*icon, this); | ||
| 14 | this->setWindowIcon(*icon); | ||
| 15 | 13 | ||
| 14 | this->qsystemtrayicon = new QSystemTrayIcon(this->windowIcon(), 0); | ||
| 16 | this->qsystemtrayicon->show(); | 15 | this->qsystemtrayicon->show(); |
| 17 | this->qsystemtrayicon->setContextMenu(ui->menuMenu1); | 16 | this->qsystemtrayicon->setContextMenu(ui->menuMenu1); |
| 18 | //this->connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systray_clicked(QSystemTrayIcon::ActivationReason))); | 17 | this->qsystemtrayicon->setToolTip(this->windowTitle()); |
| 19 | this->connect(qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); | 18 | this->connect(this->qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); |
| 19 | this->updateStatus(); | ||
| 20 | this->updateSpaceList(this->qsettings.value("hackerspace").toString()); | ||
| 20 | 21 | ||
| 21 | //this->qsystemtrayicon->installEventFilter(this); | ||
| 22 | 22 | ||
| 23 | //this->qsystemtrayicon->setIcon(this->windowIcon()); | ||
| 24 | this->qsystemtrayicon->setToolTip(this->windowTitle()); | ||
| 25 | 23 | ||
| 26 | QUrl url("http://hackerspace-bielefeld.de/spacestatus/status.json"); | 24 | this->timer = new QTimer(); |
| 25 | this->connect(this->timer, SIGNAL(timeout()), this, SLOT(updateStatus())); | ||
| 26 | // update every five minutes... | ||
| 27 | this->timer->start(60000 * 5); | ||
| 28 | |||
| 29 | } | ||
| 30 | |||
| 31 | MainWindow::~MainWindow() | ||
| 32 | { | ||
| 33 | delete ui; | ||
| 34 | } | ||
| 35 | |||
| 36 | void MainWindow::updateSpaceList(QString defaultSpace) | ||
| 37 | { | ||
| 38 | QUrl url("http://spaceapi.net/directory.json"); | ||
| 39 | QJsonObject json = QJsonDocument::fromJson(this->download(url)).object(); | ||
| 40 | |||
| 41 | QJsonObject::Iterator it; | ||
| 42 | for(it=json.begin(); it!=json.end(); it++) | ||
| 43 | { | ||
| 44 | //qDebug() << it.key() << "\t" << it.value().toString(); | ||
| 45 | ui->comboBox->addItem(it.key(), it.value().toString()); | ||
| 46 | if(it.key() == defaultSpace) | ||
| 47 | ui->comboBox->setCurrentIndex(ui->comboBox->count()-1); | ||
| 48 | } | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | // lets not talk about memory leaks. yes, they are there. but it works somehow. | ||
| 53 | void MainWindow::updateStatus() | ||
| 54 | { | ||
| 55 | //QUrl url("http://hackerspace-bielefeld.de/spacestatus/status.json"); | ||
| 56 | qDebug() << ui->comboBox->currentData().toString(); | ||
| 57 | ui->label_url_json->setText(ui->comboBox->currentData().toString()); | ||
| 58 | |||
| 59 | QUrl url(ui->comboBox->currentData().toString()); | ||
| 27 | QByteArray rawData = this->download(url); | 60 | QByteArray rawData = this->download(url); |
| 28 | QJsonDocument doc = QJsonDocument::fromJson(rawData); | ||
| 29 | //qDebug() << rawData; | 61 | //qDebug() << rawData; |
| 30 | //qDebug() << doc.toJson(QJsonDocument::Compact); | 62 | QJsonDocument doc = QJsonDocument::fromJson(rawData); |
| 63 | ui->plainTextEdit->setPlainText(doc.toJson(QJsonDocument::Indented)); | ||
| 31 | 64 | ||
| 65 | //qDebug() << doc.toJson(QJsonDocument::Indented); | ||
| 32 | QJsonObject json = doc.object(); | 66 | QJsonObject json = doc.object(); |
| 33 | |||
| 34 | QString strUrl; | 67 | QString strUrl; |
| 35 | if(json["state/open"].toBool()) | 68 | strUrl = json["state"].toObject() |
| 36 | { | 69 | ["icon"].toObject() |
| 37 | strUrl = json["state"].toObject()["icon"].toObject()["open"].toString(); | 70 | [json["state"].toObject()["open"].toBool() ? "open" : "closed"].toString(); |
| 38 | this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geƶffnet!"); | 71 | if(strUrl == "") |
| 39 | } | 72 | strUrl = json["icon"].toObject() |
| 40 | else | 73 | [json["state"].toObject()["open"].toBool() ? "open" : "closed"].toString(); |
| 41 | { | 74 | |
| 42 | strUrl = json["state"].toObject()["icon"].toObject()["closed"].toString(); | 75 | |
| 43 | this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geschlossen!"); | ||
| 44 | } | ||
| 45 | 76 | ||
| 46 | qDebug() << "downloading: " << strUrl; | 77 | qDebug() << "downloading: " << strUrl; |
| 47 | QByteArray imageArray = this->download(QUrl(strUrl)); | 78 | this->imageArray = this->download(QUrl(strUrl)); |
| 48 | //qDebug("test: " + imageArray.size()); | 79 | //qDebug("test: " + imageArray.size()); |
| 49 | QImage image = QImage::fromData(imageArray); | 80 | QImage image = QImage::fromData(imageArray); |
| 50 | 81 | ||
| 51 | this->currentIcon = new QIcon(QPixmap::fromImage(image)); | 82 | this->currentIcon = new QIcon(QPixmap::fromImage(image)); |
| 52 | this->setWindowIcon(*currentIcon); | ||
| 53 | this->qsystemtrayicon->setIcon(*currentIcon); | 83 | this->qsystemtrayicon->setIcon(*currentIcon); |
| 54 | this->setWindowTitle("closed"); | ||
| 55 | 84 | ||
| 56 | qDebug() << json["state", "open"]; | 85 | //QPixmap* pixmap = new QPixmap("/home/max/folder.png"); |
| 86 | QUrl logoURL(json["logo"].toString()); | ||
| 87 | //qDebug() << "logo: " << logoURL.toString(); | ||
| 88 | QPixmap pixmap = QPixmap::fromImage(QImage::fromData(this->download(logoURL))); | ||
| 89 | ui->label_icon->setPixmap(pixmap.scaled(120, 120, Qt::KeepAspectRatio)); | ||
| 57 | } | 90 | } |
| 58 | 91 | ||
| 59 | MainWindow::~MainWindow() | 92 | void MainWindow::saveSettings() |
| 60 | { | 93 | { |
| 61 | delete ui; | 94 | this->qsettings.setValue("hackerspace", ui->comboBox->currentText()); |
| 62 | } | 95 | } |
| 63 | 96 | ||
| 97 | |||
| 98 | |||
| 64 | QByteArray MainWindow::download(QUrl url) | 99 | QByteArray MainWindow::download(QUrl url) |
| 65 | { | 100 | { |
| 101 | qDebug() << "downloading: " << url.toString(); | ||
| 66 | QNetworkAccessManager manager; | 102 | QNetworkAccessManager manager; |
| 67 | QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url))); | 103 | QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url))); |
| 68 | QEventLoop event; | 104 | QEventLoop event; |
| 69 | connect(response,SIGNAL(finished()),&event,SLOT(quit())); | 105 | connect(response, SIGNAL(finished()), &event, SLOT(quit())); |
| 106 | connect(response, &QNetworkReply::sslErrors, | ||
| 107 | [=](const QList<QSslError>& errors) { | ||
| 108 | qDebug() << errors[0].errorString(); | ||
| 109 | // many of us use self-signed certificates or they have expired. | ||
| 110 | // this should not matter here, therefore ignore... | ||
| 111 | response->ignoreSslErrors(); | ||
| 112 | } | ||
| 113 | ); | ||
| 114 | connect(response, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), | ||
| 115 | [=](QNetworkReply::NetworkError) | ||
| 116 | { | ||
| 117 | qDebug() << "NetworkError!"; // code.errorString(); | ||
| 118 | //qDebug() << ; | ||
| 119 | qDebug() << response->errorString(); | ||
| 120 | |||
| 121 | } | ||
| 122 | ); | ||
| 70 | event.exec(); | 123 | event.exec(); |
| 71 | 124 | if (response->error() == QNetworkReply::NoError) | |
| 125 | { | ||
| 126 | int statusCode = response->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); | ||
| 127 | |||
| 128 | qDebug() << "Status: " << statusCode; | ||
| 129 | switch(statusCode) | ||
| 130 | { | ||
| 131 | case 301: | ||
| 132 | case 302: | ||
| 133 | case 303: | ||
| 134 | return this->download(response->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl()); | ||
| 135 | break; | ||
| 136 | } | ||
| 137 | } | ||
| 72 | return response->readAll(); | 138 | return response->readAll(); |
| 73 | } | 139 | } |
| 74 | 140 | ||
| 75 | void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) | 141 | void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) |
| 76 | { | 142 | { |
| 143 | qDebug() << "trigger"; | ||
| 77 | if(reason == QSystemTrayIcon::Trigger) | 144 | if(reason == QSystemTrayIcon::Trigger) |
| 78 | { | 145 | { |
| 79 | qDebug() << "trigger"; | 146 | if(this->isVisible()) |
| 80 | this->setWindowState(Qt::WindowNoState); | 147 | this->hide(); |
| 81 | this->show(); | 148 | else |
| 82 | this->activateWindow(); | 149 | { |
| 150 | this->setWindowState(Qt::WindowNoState); | ||
| 151 | this->show(); | ||
| 152 | this->activateWindow(); | ||
| 153 | } | ||
| 83 | 154 | ||
| 84 | } | 155 | } |
| 85 | else | 156 | else |
| @@ -93,3 +164,10 @@ void MainWindow::exitApplication() | |||
| 93 | qDebug() << "exit now!"; | 164 | qDebug() << "exit now!"; |
| 94 | exit(0); | 165 | exit(0); |
| 95 | } | 166 | } |
| 167 | |||
| 168 | void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) | ||
| 169 | { | ||
| 170 | qDebug() << arg1; | ||
| 171 | this->updateStatus(); | ||
| 172 | this->saveSettings(); | ||
| 173 | } | ||
