From 950bf7952bd31c64fb0e5536daec4c6881276d74 Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Mon, 30 Nov 2015 02:35:31 +0100 Subject: added network monitoring shows network errors and updates status on reconnect --- main.cpp | 1 + mainwindow.cpp | 39 ++++++++++++++++++++++++++++++++++----- mainwindow.h | 8 ++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 0e12450..68af217 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include + int main(int argc, char *argv[]) { QApplication a(argc, argv); diff --git a/mainwindow.cpp b/mainwindow.cpp index 6149e0c..b27876c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -2,6 +2,21 @@ #include "ui_mainwindow.h" #include +void MainWindow::onNetworkConfigurationChanged() +{ + qDebug() << "configuration changed\n"; + if(this->mgr->isOnline()) + { + if(this->ui->comboBox->count() == 0) + { + this->updateSpaceList(); + this->updateStatus(); + } + else + this->updateStatus(); + } +} + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) @@ -21,12 +36,30 @@ MainWindow::MainWindow(QWidget *parent) : this->timer = new QTimer(); - this->connect(this->timer, SIGNAL(timeout()), this, SLOT(updateStatus())); + this->connect(this->timer, SIGNAL(timeout()), this, SLOT(updateSpaceList())); // update every five minutes... this->timer->start(60000 * 5); //this->pixmap = new QPixmap(120, 120); //this->currentIcon = new QIcon(this->pixmap); + + this->mgr = new QNetworkConfigurationManager(); + connect(this->mgr, + SIGNAL(configurationChanged(const QNetworkConfiguration &)), + this, + SLOT(onNetworkConfigurationChanged())); + + connect(this->mgr, &QNetworkConfigurationManager::onlineStateChanged, + [=] (bool b) + { + if(!b) + { + this->qsystemtrayicon->setIcon(QIcon::fromTheme("network-error")); + this->qsystemtrayicon->showMessage("Offline", "The network connection was lost :("); + } + + }); + } MainWindow::~MainWindow() @@ -56,10 +89,6 @@ void MainWindow::updateSpaceList() } ui->comboBox->setDisabled(false); - // in case of a bad network connection the combobox might show an element - // but does not download data. clicking refresh does not solve that, because - // setCurrentIndex does not trigger a refresh in that (rare) case... - this->updateStatus(); // fixes case where setCurrentIndex does not call on_comboBox_currentIndexChanged } QJsonObject json_find(QJsonObject json, QStringList list) diff --git a/mainwindow.h b/mainwindow.h index 305b79b..916f95b 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -26,6 +26,10 @@ #include #include +#include +#include + + namespace Ui { class MainWindow; @@ -47,6 +51,7 @@ private: QTimer* timer; void saveSettings(); QByteArray download(QUrl url); + QNetworkConfigurationManager* mgr; private slots: @@ -55,6 +60,9 @@ private slots: void updateStatus(); void updateSpaceList(); void exitApplication(); + //void stateChanged(QNetworkSession::State); + //void onlineStateChanged(bool); + void onNetworkConfigurationChanged(); }; #endif // MAINWINDOW_H -- cgit v1.2.3