Log connection state transitions.
[yandex-traffic] / mainwidget.cpp
index 5f4771c..4e04909 100644 (file)
@@ -1,8 +1,13 @@
 #include <QtGui>
 
+#include "globals.hpp"
 #include "mainwidget.hpp"
 #include "settingsDialog.hpp"
 #include "connection.hpp"
+#include "devstate.hpp"
+#include "settings.hpp"
+#include "log.hpp"
+#include "menudialog.hpp"
 
 
 // --------------------------------------------------
@@ -22,7 +27,7 @@ MainWidget::MainWidget ()
 
     _traffic = new Traffic;
     _regions = new RegionsTable;
-    _settings = new Settings;
+    _settings = Settings::instance ();
 
     QHBoxLayout *layout = new QHBoxLayout;
     layout->addWidget (_light);
@@ -32,7 +37,8 @@ MainWidget::MainWidget ()
     applySettings ();
 
     connect (_traffic, SIGNAL (updated ()), SLOT (trafficUpdated ()));
-    connect (_timer, SIGNAL (timeout ()), SLOT (updateDate ()));
+    connect (_timer, SIGNAL (timeout ()), SLOT (updateData ()));
+    connect (DeviceState::instance (), SIGNAL (lockChanged (bool)), SLOT (deviceLockChanged (bool)));
 
     updateData ();
 }
@@ -61,7 +67,6 @@ void MainWidget::paintEvent(QPaintEvent *event)
 }
 
 
-
 void MainWidget::trafficUpdated ()
 {
     ExtendedTrafficInfo info = _traffic->lookup_ext (_settings->regionID ());
@@ -72,8 +77,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;
         }
 
@@ -103,13 +107,24 @@ void MainWidget::updateData ()
 {
     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)
+    if (update) {
+        Log::instance ()->add ("Perform update");
         _traffic->update ();
+    }
+    else
+        Log::instance ()->add ("Update not performed");
 }
 
 
@@ -150,8 +165,45 @@ void MainWidget::applySettings ()
 
     updateSize ();
 
-    if (_settings->updateInterval () < 0)
+    Log::instance ()->add (QString ("applySettings: updateInterval is %1").arg (_settings->updateInterval ()));
+
+    if (_settings->updateInterval () < 0) {
         _timer->stop ();
-    else
+        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 ()));
+    }
+}
+
+
+bool MainWidget::event (QEvent *event)
+{
+    if (event->type () != QEvent::WindowActivate)
+        return QWidget::event (event);
+
+    MenuDialog menu (tr ("Yandex.Traffic"));
+
+    menu.addEntry (tr ("Settings")).addEntry (tr ("Update"));
+
+    switch (menu.run ()) {
+        case 0:
+            settingsDialog ();
+            break;
+        case 1:
+            _traffic->update ();
+            break;
+    }
+
+    return QWidget::event (event);
+}
+
+
+void MainWidget::deviceLockChanged (bool locked)
+{
+    if (!_settings->check (Settings::C_UpdateWhenLocked))
+        if (!locked)
+            updateData ();
 }