Fixed bug with updates. Finished logging.
[yandex-traffic] / mainwidget.cpp
index 1477f9c..7e9a455 100644 (file)
@@ -2,6 +2,10 @@
 
 #include "mainwidget.hpp"
 #include "settingsDialog.hpp"
+#include "connection.hpp"
+#include "devstate.hpp"
+#include "settings.hpp"
+#include "log.hpp"
 
 
 // --------------------------------------------------
@@ -15,26 +19,24 @@ MainWidget::MainWidget ()
 #endif
     _light = new TrafficLight (this);
     _label = new QLabel (this);
+    _timer = new QTimer (this);
 
     _label->setAlignment (Qt::AlignHCenter | Qt::AlignVCenter);
 
     _traffic = new Traffic;
     _regions = new RegionsTable;
-    _settings = new Settings;
+    _settings = Settings::instance ();
 
     QHBoxLayout *layout = new QHBoxLayout;
     layout->addWidget (_light);
     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 (updateData ()));
+    connect (DeviceState::instance (), SIGNAL (lockChanged (bool)), SLOT (deviceLockChanged (bool)));
 
     updateData ();
 }
@@ -63,13 +65,6 @@ void MainWidget::paintEvent(QPaintEvent *event)
 }
 
 
-void MainWidget::timerEvent (QTimerEvent *)
-{
-    updateData ();
-}
-
-
-
 void MainWidget::trafficUpdated ()
 {
     ExtendedTrafficInfo info = _traffic->lookup_ext (_settings->regionID ());
@@ -80,8 +75,7 @@ void MainWidget::trafficUpdated ()
         _light->setColor (info.color ());
 
         if (_settings->check (Settings::C_ShowRank)) {
-            data.append (QString::number (info.level ()));
-            data.append (info.level () > 1 ? tr (" points") : tr (" point"));
+            data.append (tr ("%n point(s)", "", info.level ()));
             first = false;
         }
 
@@ -109,8 +103,26 @@ void MainWidget::trafficUpdated ()
 
 void MainWidget::updateData ()
 {
-    // Here we need to check for internet connection
-    _traffic->update ();
+    bool update = true;
+
+    Log::instance ()->add ("updateData called");
+
+#if CHECK_FOR_CONNECTION
+    update = ConnectionChecker::instance ()->checkConnection (_settings->check (Settings::C_UpdateOnGSM),
+                                                              _settings->check (Settings::C_UpdateOnWiFi));
+    Log::instance ()->add (QString ("checkConnection returned %1").arg (update ? "true" : "false"));
+    if (!_settings->check (Settings::C_UpdateWhenLocked)) {
+        Log::instance ()->add ("Check for device state");
+        update &= !DeviceState::instance ()->locked ();
+    }
+#endif
+
+    if (update) {
+        Log::instance ()->add ("Perform update");
+        _traffic->update ();
+    }
+    else
+        Log::instance ()->add ("Update not performed");
 }
 
 
@@ -120,10 +132,7 @@ void MainWidget::settingsDialog ()
 
     dlg.exec ();
 
-    // Handle settings
-    _light->setVisible (_settings->check (Settings::C_ShowLight));
-
-    updateSize ();
+    applySettings ();
     trafficUpdated ();
 }
 
@@ -145,3 +154,53 @@ void MainWidget::updateSize ()
 
     setFixedSize (minSize);
 }
+
+
+
+void MainWidget::applySettings ()
+{
+    _light->setVisible (_settings->check (Settings::C_ShowLight));
+
+    updateSize ();
+
+    Log::instance ()->add (QString ("applySettings: updateInterval is %1").arg (_settings->updateInterval ()));
+
+    if (_settings->updateInterval () < 0) {
+        _timer->stop ();
+        Log::instance ()->add ("Timer disabled");
+    }
+    else {
+        _timer->setInterval (1000 * 60 * _settings->updateInterval ());
+        _timer->start ();
+        Log::instance ()->add (QString ("Timer interval set to %1 ms").arg (1000 * 60 * _settings->updateInterval ()));
+    }
+}
+
+
+void MainWidget::mousePressEvent (QMouseEvent *event)
+{
+    QMenu menu;
+    QAction *settingsAction, *updateAction, *todo;
+
+    Log::instance ()->add (QString ("mousePressEvent at %1,%2").arg (event->pos ().x ()).arg (event->pos ().y ()));
+
+    settingsAction = menu.addAction (tr ("Settings"));
+    updateAction = menu.addAction (tr ("Update"));
+
+    todo = menu.exec (event->pos ());
+    if (!todo)
+        return;
+
+    if (todo == settingsAction)
+        settingsDialog ();
+    if (todo == updateAction)
+        _traffic->update ();
+}
+
+
+void MainWidget::deviceLockChanged (bool locked)
+{
+    if (!_settings->check (Settings::C_UpdateWhenLocked))
+        if (!locked)
+            updateData ();
+}