aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.cpp9
-rw-r--r--mainwindow.cpp148
-rw-r--r--mainwindow.h12
-rw-r--r--mainwindow.ui139
-rw-r--r--qspaceapi.pro (renamed from qtapp.pro)3
5 files changed, 261 insertions, 50 deletions
diff --git a/main.cpp b/main.cpp
index 74e1a60..9b39d7c 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,10 +1,19 @@
1#include "mainwindow.h" 1#include "mainwindow.h"
2
2#include <QApplication> 3#include <QApplication>
4#include <QMetaObject>
5
3 6
4int main(int argc, char *argv[]) 7int main(int argc, char *argv[])
5{ 8{
6 QApplication a(argc, argv); 9 QApplication a(argc, argv);
10 a.setOrganizationName("CoderOnline");
7 a.setQuitOnLastWindowClosed(false); 11 a.setQuitOnLastWindowClosed(false);
8 MainWindow w; 12 MainWindow w;
13
14
15
9 return a.exec(); 16 return a.exec();
10} 17}
18
19
diff --git a/mainwindow.cpp b/mainwindow.cpp
index 0dd8a3e..2590d4f 100644
--- a/mainwindow.cpp
+++ b/mainwindow.cpp
@@ -5,81 +5,152 @@
5MainWindow::MainWindow(QWidget *parent) : 5MainWindow::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
31MainWindow::~MainWindow()
32{
33 delete ui;
34}
35
36void 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.
53void 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
59MainWindow::~MainWindow() 92void MainWindow::saveSettings()
60{ 93{
61 delete ui; 94 this->qsettings.setValue("hackerspace", ui->comboBox->currentText());
62} 95}
63 96
97
98
64QByteArray MainWindow::download(QUrl url) 99QByteArray 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
75void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) 141void 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
168void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
169{
170 qDebug() << arg1;
171 this->updateStatus();
172 this->saveSettings();
173}
diff --git a/mainwindow.h b/mainwindow.h
index 11c1616..9656734 100644
--- a/mainwindow.h
+++ b/mainwindow.h
@@ -6,6 +6,7 @@
6 6
7#include <QJsonDocument> 7#include <QJsonDocument>
8#include <QJsonObject> 8#include <QJsonObject>
9#include <QJsonArray>
9#include <QByteArray> 10#include <QByteArray>
10 11
11 12
@@ -16,6 +17,8 @@
16#include <QtNetwork/QNetworkRequest> 17#include <QtNetwork/QNetworkRequest>
17#include <QtNetwork/QNetworkReply> 18#include <QtNetwork/QNetworkReply>
18 19
20#include <QSettings>
21#include <QTimer>
19 22
20namespace Ui { 23namespace Ui {
21class MainWindow; 24class MainWindow;
@@ -28,15 +31,22 @@ class MainWindow : public QMainWindow
28public: 31public:
29 explicit MainWindow(QWidget *parent = 0); 32 explicit MainWindow(QWidget *parent = 0);
30 ~MainWindow(); 33 ~MainWindow();
34 void updateSpaceList(QString defaultSpace);
31 35
32private: 36private:
33 Ui::MainWindow *ui; 37 Ui::MainWindow *ui;
34 QSystemTrayIcon* qsystemtrayicon; 38 QSystemTrayIcon* qsystemtrayicon;
35 QByteArray download(QUrl url); 39 QByteArray download(QUrl url);
36 QIcon* currentIcon; 40 QIcon* currentIcon;
41 QByteArray imageArray;
42 void saveSettings();
43 QSettings qsettings;\
44 QTimer* timer;
37 45
38private slots: 46private slots:
39 void slotActivated(QSystemTrayIcon::ActivationReason); 47 void slotActivated(QSystemTrayIcon::ActivationReason);
48 void on_comboBox_currentIndexChanged(const QString &arg1);
49 void updateStatus();
40 void exitApplication(); 50 void exitApplication();
41 51
42}; 52};
diff --git a/mainwindow.ui b/mainwindow.ui
index 812bddc..27a41ad 100644
--- a/mainwindow.ui
+++ b/mainwindow.ui
@@ -6,10 +6,22 @@
6 <rect> 6 <rect>
7 <x>0</x> 7 <x>0</x>
8 <y>0</y> 8 <y>0</y>
9 <width>646</width> 9 <width>670</width>
10 <height>486</height> 10 <height>442</height>
11 </rect> 11 </rect>
12 </property> 12 </property>
13 <property name="sizePolicy">
14 <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
15 <horstretch>0</horstretch>
16 <verstretch>0</verstretch>
17 </sizepolicy>
18 </property>
19 <property name="maximumSize">
20 <size>
21 <width>670</width>
22 <height>442</height>
23 </size>
24 </property>
13 <property name="windowTitle"> 25 <property name="windowTitle">
14 <string>MainWindow</string> 26 <string>MainWindow</string>
15 </property> 27 </property>
@@ -17,36 +29,118 @@
17 <iconset> 29 <iconset>
18 <normaloff>folder.png</normaloff>folder.png</iconset> 30 <normaloff>folder.png</normaloff>folder.png</iconset>
19 </property> 31 </property>
20 <widget class="QWidget" name="centralWidget"/> 32 <widget class="QWidget" name="centralWidget">
33 <layout class="QVBoxLayout" name="verticalLayout">
34 <item>
35 <widget class="QComboBox" name="comboBox">
36 <property name="insertPolicy">
37 <enum>QComboBox::InsertAlphabetically</enum>
38 </property>
39 <property name="sizeAdjustPolicy">
40 <enum>QComboBox::AdjustToMinimumContentsLength</enum>
41 </property>
42 </widget>
43 </item>
44 <item>
45 <widget class="QLabel" name="label_url_json">
46 <property name="text">
47 <string>TextLabel</string>
48 </property>
49 </widget>
50 </item>
51 <item>
52 <widget class="QScrollArea" name="scrollArea">
53 <property name="widgetResizable">
54 <bool>true</bool>
55 </property>
56 <widget class="QWidget" name="scrollAreaWidgetContents">
57 <property name="geometry">
58 <rect>
59 <x>0</x>
60 <y>0</y>
61 <width>648</width>
62 <height>305</height>
63 </rect>
64 </property>
65 <widget class="QLabel" name="label_icon">
66 <property name="geometry">
67 <rect>
68 <x>10</x>
69 <y>10</y>
70 <width>120</width>
71 <height>120</height>
72 </rect>
73 </property>
74 <property name="text">
75 <string>TextLabel</string>
76 </property>
77 <property name="alignment">
78 <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
79 </property>
80 </widget>
81 <widget class="QPlainTextEdit" name="plainTextEdit">
82 <property name="geometry">
83 <rect>
84 <x>140</x>
85 <y>10</y>
86 <width>501</width>
87 <height>311</height>
88 </rect>
89 </property>
90 <property name="acceptDrops">
91 <bool>false</bool>
92 </property>
93 <property name="readOnly">
94 <bool>true</bool>
95 </property>
96 </widget>
97 </widget>
98 </widget>
99 </item>
100 </layout>
101 </widget>
21 <widget class="QMenuBar" name="menuBar"> 102 <widget class="QMenuBar" name="menuBar">
103 <property name="enabled">
104 <bool>true</bool>
105 </property>
22 <property name="geometry"> 106 <property name="geometry">
23 <rect> 107 <rect>
24 <x>0</x> 108 <x>0</x>
25 <y>0</y> 109 <y>0</y>
26 <width>646</width> 110 <width>670</width>
27 <height>29</height> 111 <height>29</height>
28 </rect> 112 </rect>
29 </property> 113 </property>
114 <property name="defaultUp">
115 <bool>false</bool>
116 </property>
117 <property name="nativeMenuBar">
118 <bool>false</bool>
119 </property>
30 <widget class="QMenu" name="menuMenu1"> 120 <widget class="QMenu" name="menuMenu1">
31 <property name="title"> 121 <property name="title">
32 <string>menu1</string> 122 <string>File</string>
33 </property> 123 </property>
124 <addaction name="actionUpdate_Status"/>
125 <addaction name="separator"/>
34 <addaction name="actionExit"/> 126 <addaction name="actionExit"/>
35 </widget> 127 </widget>
36 <addaction name="menuMenu1"/> 128 <addaction name="menuMenu1"/>
37 </widget> 129 </widget>
38 <widget class="QToolBar" name="mainToolBar">
39 <attribute name="toolBarArea">
40 <enum>TopToolBarArea</enum>
41 </attribute>
42 <attribute name="toolBarBreak">
43 <bool>false</bool>
44 </attribute>
45 </widget>
46 <widget class="QStatusBar" name="statusBar"/> 130 <widget class="QStatusBar" name="statusBar"/>
47 <action name="actionExit"> 131 <action name="actionExit">
48 <property name="text"> 132 <property name="text">
49 <string>exit</string> 133 <string>Quit</string>
134 </property>
135 </action>
136 <action name="actionSpaces">
137 <property name="text">
138 <string>Spaces</string>
139 </property>
140 </action>
141 <action name="actionUpdate_Status">
142 <property name="text">
143 <string>Update Status</string>
50 </property> 144 </property>
51 </action> 145 </action>
52 </widget> 146 </widget>
@@ -69,8 +163,25 @@
69 </hint> 163 </hint>
70 </hints> 164 </hints>
71 </connection> 165 </connection>
166 <connection>
167 <sender>actionUpdate_Status</sender>
168 <signal>triggered()</signal>
169 <receiver>MainWindow</receiver>
170 <slot>updateStatus()</slot>
171 <hints>
172 <hint type="sourcelabel">
173 <x>-1</x>
174 <y>-1</y>
175 </hint>
176 <hint type="destinationlabel">
177 <x>334</x>
178 <y>220</y>
179 </hint>
180 </hints>
181 </connection>
72 </connections> 182 </connections>
73 <slots> 183 <slots>
74 <slot>exitApplication()</slot> 184 <slot>exitApplication()</slot>
185 <slot>updateStatus()</slot>
75 </slots> 186 </slots>
76</ui> 187</ui>
diff --git a/qtapp.pro b/qspaceapi.pro
index db84b23..4384e98 100644
--- a/qtapp.pro
+++ b/qspaceapi.pro
@@ -3,6 +3,7 @@
3# Project created by QtCreator 2015-11-17T21:16:22 3# Project created by QtCreator 2015-11-17T21:16:22
4# 4#
5#------------------------------------------------- 5#-------------------------------------------------
6CONFIG += c++11
6 7
7QT += core gui network 8QT += core gui network
8 9
@@ -21,3 +22,5 @@ FORMS += mainwindow.ui
21 22
22DISTFILES += \ 23DISTFILES += \
23 folder.png 24 folder.png
25
26RESOURCES +=
..