From 64ae6f0db1e8af1bc287c52b010fe87cc5380191 Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Fri, 20 Nov 2015 16:09:06 +0100 Subject: displays space status in system tray successfully tested under linux and windows --- main.cpp | 9 ++++ mainwindow.cpp | 148 +++++++++++++++++++++++++++++++++++++++++++-------------- mainwindow.h | 12 ++++- mainwindow.ui | 139 +++++++++++++++++++++++++++++++++++++++++++++++------ qspaceapi.pro | 26 ++++++++++ qtapp.pro | 23 --------- 6 files changed, 284 insertions(+), 73 deletions(-) create mode 100644 qspaceapi.pro delete mode 100644 qtapp.pro diff --git a/main.cpp b/main.cpp index 74e1a60..9b39d7c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,19 @@ #include "mainwindow.h" + #include +#include + int main(int argc, char *argv[]) { QApplication a(argc, argv); + a.setOrganizationName("CoderOnline"); a.setQuitOnLastWindowClosed(false); MainWindow w; + + + return a.exec(); } + + diff --git a/mainwindow.cpp b/mainwindow.cpp index 0dd8a3e..2590d4f 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -5,81 +5,152 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) - { ui->setupUi(this); + ui->menuBar->setVisible(false); - QIcon* icon = new QIcon("folder.png"); - this->qsystemtrayicon = new QSystemTrayIcon(*icon, this); - this->setWindowIcon(*icon); + qDebug() << "Using: " << this->qsettings.fileName(); + this->qsystemtrayicon = new QSystemTrayIcon(this->windowIcon(), 0); this->qsystemtrayicon->show(); this->qsystemtrayicon->setContextMenu(ui->menuMenu1); - //this->connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systray_clicked(QSystemTrayIcon::ActivationReason))); - this->connect(qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); + this->qsystemtrayicon->setToolTip(this->windowTitle()); + this->connect(this->qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); + this->updateStatus(); + this->updateSpaceList(this->qsettings.value("hackerspace").toString()); - //this->qsystemtrayicon->installEventFilter(this); - //this->qsystemtrayicon->setIcon(this->windowIcon()); - this->qsystemtrayicon->setToolTip(this->windowTitle()); - QUrl url("http://hackerspace-bielefeld.de/spacestatus/status.json"); + this->timer = new QTimer(); + this->connect(this->timer, SIGNAL(timeout()), this, SLOT(updateStatus())); + // update every five minutes... + this->timer->start(60000 * 5); + +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::updateSpaceList(QString defaultSpace) +{ + QUrl url("http://spaceapi.net/directory.json"); + QJsonObject json = QJsonDocument::fromJson(this->download(url)).object(); + + QJsonObject::Iterator it; + for(it=json.begin(); it!=json.end(); it++) + { + //qDebug() << it.key() << "\t" << it.value().toString(); + ui->comboBox->addItem(it.key(), it.value().toString()); + if(it.key() == defaultSpace) + ui->comboBox->setCurrentIndex(ui->comboBox->count()-1); + } + +} + +// lets not talk about memory leaks. yes, they are there. but it works somehow. +void MainWindow::updateStatus() +{ + //QUrl url("http://hackerspace-bielefeld.de/spacestatus/status.json"); + qDebug() << ui->comboBox->currentData().toString(); + ui->label_url_json->setText(ui->comboBox->currentData().toString()); + + QUrl url(ui->comboBox->currentData().toString()); QByteArray rawData = this->download(url); - QJsonDocument doc = QJsonDocument::fromJson(rawData); //qDebug() << rawData; - //qDebug() << doc.toJson(QJsonDocument::Compact); + QJsonDocument doc = QJsonDocument::fromJson(rawData); + ui->plainTextEdit->setPlainText(doc.toJson(QJsonDocument::Indented)); + //qDebug() << doc.toJson(QJsonDocument::Indented); QJsonObject json = doc.object(); - QString strUrl; - if(json["state/open"].toBool()) - { - strUrl = json["state"].toObject()["icon"].toObject()["open"].toString(); - this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geƶffnet!"); - } - else - { - strUrl = json["state"].toObject()["icon"].toObject()["closed"].toString(); - this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geschlossen!"); - } + strUrl = json["state"].toObject() + ["icon"].toObject() + [json["state"].toObject()["open"].toBool() ? "open" : "closed"].toString(); + if(strUrl == "") + strUrl = json["icon"].toObject() + [json["state"].toObject()["open"].toBool() ? "open" : "closed"].toString(); + + qDebug() << "downloading: " << strUrl; - QByteArray imageArray = this->download(QUrl(strUrl)); + this->imageArray = this->download(QUrl(strUrl)); //qDebug("test: " + imageArray.size()); QImage image = QImage::fromData(imageArray); this->currentIcon = new QIcon(QPixmap::fromImage(image)); - this->setWindowIcon(*currentIcon); this->qsystemtrayicon->setIcon(*currentIcon); - this->setWindowTitle("closed"); - qDebug() << json["state", "open"]; + //QPixmap* pixmap = new QPixmap("/home/max/folder.png"); + QUrl logoURL(json["logo"].toString()); + //qDebug() << "logo: " << logoURL.toString(); + QPixmap pixmap = QPixmap::fromImage(QImage::fromData(this->download(logoURL))); + ui->label_icon->setPixmap(pixmap.scaled(120, 120, Qt::KeepAspectRatio)); } -MainWindow::~MainWindow() +void MainWindow::saveSettings() { - delete ui; + this->qsettings.setValue("hackerspace", ui->comboBox->currentText()); } + + QByteArray MainWindow::download(QUrl url) { + qDebug() << "downloading: " << url.toString(); QNetworkAccessManager manager; QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url))); QEventLoop event; - connect(response,SIGNAL(finished()),&event,SLOT(quit())); + connect(response, SIGNAL(finished()), &event, SLOT(quit())); + connect(response, &QNetworkReply::sslErrors, + [=](const QList& errors) { + qDebug() << errors[0].errorString(); + // many of us use self-signed certificates or they have expired. + // this should not matter here, therefore ignore... + response->ignoreSslErrors(); + } + ); + connect(response, static_cast(&QNetworkReply::error), + [=](QNetworkReply::NetworkError) + { + qDebug() << "NetworkError!"; // code.errorString(); + //qDebug() << ; + qDebug() << response->errorString(); + + } + ); event.exec(); - + if (response->error() == QNetworkReply::NoError) + { + int statusCode = response->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + qDebug() << "Status: " << statusCode; + switch(statusCode) + { + case 301: + case 302: + case 303: + return this->download(response->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl()); + break; + } + } return response->readAll(); } void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) { + qDebug() << "trigger"; if(reason == QSystemTrayIcon::Trigger) { - qDebug() << "trigger"; - this->setWindowState(Qt::WindowNoState); - this->show(); - this->activateWindow(); + if(this->isVisible()) + this->hide(); + else + { + this->setWindowState(Qt::WindowNoState); + this->show(); + this->activateWindow(); + } } else @@ -93,3 +164,10 @@ void MainWindow::exitApplication() qDebug() << "exit now!"; exit(0); } + +void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) +{ + qDebug() << arg1; + this->updateStatus(); + this->saveSettings(); +} diff --git a/mainwindow.h b/mainwindow.h index 11c1616..9656734 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -6,6 +6,7 @@ #include #include +#include #include @@ -16,6 +17,8 @@ #include #include +#include +#include namespace Ui { class MainWindow; @@ -28,15 +31,22 @@ class MainWindow : public QMainWindow public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); + void updateSpaceList(QString defaultSpace); private: Ui::MainWindow *ui; QSystemTrayIcon* qsystemtrayicon; QByteArray download(QUrl url); - QIcon* currentIcon; + QIcon* currentIcon; + QByteArray imageArray; + void saveSettings(); + QSettings qsettings;\ + QTimer* timer; private slots: void slotActivated(QSystemTrayIcon::ActivationReason); + void on_comboBox_currentIndexChanged(const QString &arg1); + void updateStatus(); void exitApplication(); }; diff --git a/mainwindow.ui b/mainwindow.ui index 812bddc..27a41ad 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,10 +6,22 @@ 0 0 - 646 - 486 + 670 + 442 + + + 0 + 0 + + + + + 670 + 442 + + MainWindow @@ -17,36 +29,118 @@ folder.pngfolder.png - + + + + + + QComboBox::InsertAlphabetically + + + QComboBox::AdjustToMinimumContentsLength + + + + + + + TextLabel + + + + + + + true + + + + + 0 + 0 + 648 + 305 + + + + + + 10 + 10 + 120 + 120 + + + + TextLabel + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + 140 + 10 + 501 + 311 + + + + false + + + true + + + + + + + + + true + 0 0 - 646 + 670 29 + + false + + + false + - menu1 + File + + - - - TopToolBarArea - - - false - - - exit + Quit + + + + + Spaces + + + + + Update Status @@ -69,8 +163,25 @@ + + actionUpdate_Status + triggered() + MainWindow + updateStatus() + + + -1 + -1 + + + 334 + 220 + + + exitApplication() + updateStatus() diff --git a/qspaceapi.pro b/qspaceapi.pro new file mode 100644 index 0000000..4384e98 --- /dev/null +++ b/qspaceapi.pro @@ -0,0 +1,26 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-11-17T21:16:22 +# +#------------------------------------------------- +CONFIG += c++11 + +QT += core gui network + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = qtapp +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp + +HEADERS += mainwindow.h + +FORMS += mainwindow.ui + +DISTFILES += \ + folder.png + +RESOURCES += diff --git a/qtapp.pro b/qtapp.pro deleted file mode 100644 index db84b23..0000000 --- a/qtapp.pro +++ /dev/null @@ -1,23 +0,0 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2015-11-17T21:16:22 -# -#------------------------------------------------- - -QT += core gui network - -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets - -TARGET = qtapp -TEMPLATE = app - - -SOURCES += main.cpp\ - mainwindow.cpp - -HEADERS += mainwindow.h - -FORMS += mainwindow.ui - -DISTFILES += \ - folder.png -- cgit v1.2.3