#include "mainwindow.h" #include "ui_mainwindow.h" #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->menuBar->setVisible(false); qDebug() << "Using: " << this->qsettings.fileName(); this->qsystemtrayicon = new QSystemTrayIcon(this->windowIcon(), 0); this->qsystemtrayicon->show(); this->qsystemtrayicon->setContextMenu(ui->menuMenu1); 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->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); //qDebug() << rawData; QJsonDocument doc = QJsonDocument::fromJson(rawData); ui->plainTextEdit->setPlainText(doc.toJson(QJsonDocument::Indented)); //qDebug() << doc.toJson(QJsonDocument::Indented); QJsonObject json = doc.object(); QString strUrl; 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; this->imageArray = this->download(QUrl(strUrl)); //qDebug("test: " + imageArray.size()); QImage image = QImage::fromData(imageArray); this->currentIcon = new QIcon(QPixmap::fromImage(image)); this->qsystemtrayicon->setIcon(*currentIcon); //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)); } void MainWindow::saveSettings() { 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, &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) { if(this->isVisible()) this->hide(); else { this->setWindowState(Qt::WindowNoState); this->show(); this->activateWindow(); } } else { qDebug() << "activate(reason): " << reason; } } void MainWindow::exitApplication() { qDebug() << "exit now!"; exit(0); } void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) { qDebug() << arg1; this->updateStatus(); this->saveSettings(); }