aboutsummaryrefslogtreecommitdiff
path: root/mainwindow.cpp
blob: 0dd8a3e6b8ac5426a11a1a4c5800a13f99520216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

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);
}
..