From: Max Lapan Date: Tue, 16 Mar 2010 12:50:07 +0000 (+0300) Subject: Change update interval according to settings. Perform extended check for connection. X-Git-Tag: v0.1~26 X-Git-Url: http://git.maemo.org/git/?a=commitdiff_plain;h=76c4d7987cc699a60491a786dd66d81c2b0ce31e;p=yandex-traffic Change update interval according to settings. Perform extended check for connection. --- diff --git a/connection.cpp b/connection.cpp index 9e990c3..07882b0 100644 --- a/connection.cpp +++ b/connection.cpp @@ -81,3 +81,21 @@ void ConnectionChecker::updateState (bool new_state, const QString& net_type) type_changed (_net_type); } } + + +bool ConnectionChecker::checkConnection (bool allow_gsm, bool allow_wifi) +{ + if (!_connected) + return false; + + switch (_net_type) { + case Net_None: + return true; + case Net_WLAN: + return allow_wifi; + case Net_GSM: + return allow_gsm; + default: + return true; + } +} diff --git a/connection.hpp b/connection.hpp index e89b267..e2d7567 100644 --- a/connection.hpp +++ b/connection.hpp @@ -39,6 +39,8 @@ public: bool isConnected () const { return _connected; }; + bool checkConnection (bool allow_gsm, bool allow_wifi); + network_type_t network_type () const { return _net_type; }; diff --git a/http_fetcher.cpp b/http_fetcher.cpp index a518e9f..2d1d030 100644 --- a/http_fetcher.cpp +++ b/http_fetcher.cpp @@ -2,8 +2,6 @@ #include #include "http_fetcher.hpp" -#include "connection.hpp" -#include "globals.hpp" // -------------------------------------------------- // HttpFetcher @@ -25,11 +23,10 @@ void HttpFetcher::fetch (const QString& url) { QUrl u (url); - if (!CHECK_FOR_CONNECTION || ConnectionChecker::instance ()->isConnected ()) - if (u.isValid ()) { - _http.setHost (u.host ()); - _http.get (u.encodedPath (), &_buffer); - } + if (u.isValid ()) { + _http.setHost (u.host ()); + _http.get (u.encodedPath (), &_buffer); + } } diff --git a/mainwidget.cpp b/mainwidget.cpp index 1477f9c..5f4771c 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -2,6 +2,7 @@ #include "mainwidget.hpp" #include "settingsDialog.hpp" +#include "connection.hpp" // -------------------------------------------------- @@ -15,6 +16,7 @@ MainWidget::MainWidget () #endif _light = new TrafficLight (this); _label = new QLabel (this); + _timer = new QTimer (this); _label->setAlignment (Qt::AlignHCenter | Qt::AlignVCenter); @@ -27,14 +29,10 @@ MainWidget::MainWidget () layout->addWidget (_label); setLayout (layout); - _light->setVisible (_settings->check (Settings::C_ShowLight)); - - updateSize (); + applySettings (); connect (_traffic, SIGNAL (updated ()), SLOT (trafficUpdated ())); - - // every 5 minutes (TODO, make option) - startTimer (5*60*1000); + connect (_timer, SIGNAL (timeout ()), SLOT (updateDate ())); updateData (); } @@ -63,12 +61,6 @@ void MainWidget::paintEvent(QPaintEvent *event) } -void MainWidget::timerEvent (QTimerEvent *) -{ - updateData (); -} - - void MainWidget::trafficUpdated () { @@ -109,8 +101,15 @@ void MainWidget::trafficUpdated () void MainWidget::updateData () { - // Here we need to check for internet connection - _traffic->update (); + bool update = true; + +#if CHECK_FOR_CONNECTION + update = ConnectionChecker::instance ()->checkConnection (_settings->check (Settings::C_UpdateOnGSM), + _settings->check (Settings::C_UpdateOnWiFi)); +#endif + + if (update) + _traffic->update (); } @@ -120,10 +119,7 @@ void MainWidget::settingsDialog () dlg.exec (); - // Handle settings - _light->setVisible (_settings->check (Settings::C_ShowLight)); - - updateSize (); + applySettings (); trafficUpdated (); } @@ -145,3 +141,17 @@ void MainWidget::updateSize () setFixedSize (minSize); } + + + +void MainWidget::applySettings () +{ + _light->setVisible (_settings->check (Settings::C_ShowLight)); + + updateSize (); + + if (_settings->updateInterval () < 0) + _timer->stop (); + else + _timer->setInterval (1000 * 60 * _settings->updateInterval ()); +} diff --git a/mainwidget.hpp b/mainwidget.hpp index ce894c6..9fb10cd 100644 --- a/mainwidget.hpp +++ b/mainwidget.hpp @@ -21,19 +21,22 @@ private: TrafficLight* _light; QLabel* _label; + // Other stuff + QTimer *_timer; + public: MainWidget (); virtual ~MainWidget (); public slots: void settingsDialog (); + void updateData (); protected: void paintEvent (QPaintEvent *event); - void timerEvent (QTimerEvent *event); - void updateData (); void updateSize (); + void applySettings (); protected slots: void trafficUpdated (); diff --git a/settings.hpp b/settings.hpp index 1fd19ef..28fb78f 100644 --- a/settings.hpp +++ b/settings.hpp @@ -59,6 +59,9 @@ public: void setUpdateIntervalIndex (int index) { _updateIntervalIndex = index; }; + + int updateInterval () const + { return intervalIndex2Minutes (_updateIntervalIndex); }; };