Change update interval according to settings. Perform extended check for connection.
authorMax Lapan <max.lapan@gmail.com>
Tue, 16 Mar 2010 12:50:07 +0000 (15:50 +0300)
committerMax Lapan <max.lapan@gmail.com>
Tue, 16 Mar 2010 12:50:07 +0000 (15:50 +0300)
connection.cpp
connection.hpp
http_fetcher.cpp
mainwidget.cpp
mainwidget.hpp
settings.hpp

index 9e990c3..07882b0 100644 (file)
@@ -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;
+    }
+}
index e89b267..e2d7567 100644 (file)
@@ -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; };
 
index a518e9f..2d1d030 100644 (file)
@@ -2,8 +2,6 @@
 #include <QtNetwork>
 
 #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);
+    }
 }
 
 
index 1477f9c..5f4771c 100644 (file)
@@ -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 ());
+}
index ce894c6..9fb10cd 100644 (file)
@@ -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 ();
index 1fd19ef..28fb78f 100644 (file)
@@ -59,6 +59,9 @@ public:
 
     void setUpdateIntervalIndex (int index)
     { _updateIntervalIndex = index; };
+
+    int updateInterval () const
+    { return intervalIndex2Minutes (_updateIntervalIndex); };
 };