#include "mainwindow.h" #include "ui_mainwindow.h" #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QIcon* icon = new QIcon("folder.png"); this->qsystemtrayicon = new QSystemTrayIcon(*icon, this); this->setWindowIcon(*icon); 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->installEventFilter(this); //this->qsystemtrayicon->setIcon(this->windowIcon()); this->qsystemtrayicon->setToolTip(this->windowTitle()); QUrl url("http://hackerspace-bielefeld.de/spacestatus/status.json"); QByteArray rawData = this->download(url); QJsonDocument doc = QJsonDocument::fromJson(rawData); //qDebug() << rawData; //qDebug() << doc.toJson(QJsonDocument::Compact); 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!"); } qDebug() << "downloading: " << strUrl; QByteArray 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"]; } MainWindow::~MainWindow() { delete ui; } QByteArray MainWindow::download(QUrl url) { QNetworkAccessManager manager; QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url))); QEventLoop event; connect(response,SIGNAL(finished()),&event,SLOT(quit())); event.exec(); return response->readAll(); } void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) { if(reason == QSystemTrayIcon::Trigger) { qDebug() << "trigger"; this->setWindowState(Qt::WindowNoState); this->show(); this->activateWindow(); } else { qDebug() << "activate(reason): " << reason; } } void MainWindow::exitApplication() { qDebug() << "exit now!"; exit(0); }