Update data when device unlocked.
[yandex-traffic] / mainwidget.cpp
index 76fc3ed..2a97dbc 100644 (file)
@@ -2,6 +2,9 @@
 
 #include "mainwidget.hpp"
 #include "settingsDialog.hpp"
+#include "connection.hpp"
+#include "devstate.hpp"
+#include "settings.hpp"
 
 
 // --------------------------------------------------
 MainWidget::MainWidget ()
     : QWidget ()
 {
-    setMinimumSize (300, 80);
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_TranslucentBackground);
 #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;
@@ -26,12 +31,11 @@ MainWidget::MainWidget ()
     layout->addWidget (_label);
     setLayout (layout);
 
-    _light->setVisible (_settings->check (Settings::C_Light));
+    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 ();
 }
@@ -60,51 +64,56 @@ void MainWidget::paintEvent(QPaintEvent *event)
 }
 
 
-void MainWidget::timerEvent (QTimerEvent *)
-{
-    updateData ();
-}
-
-
 
 void MainWidget::trafficUpdated ()
 {
-    ExtendedTrafficInfo info = _traffic->lookup_ext ("1");
+    ExtendedTrafficInfo info = _traffic->lookup_ext (_settings->regionID ());
 
     if (info.valid ()) {
         QString data;
         bool first = true;
         _light->setColor (info.color ());
 
-        if (_settings->check (Settings::C_Rank)) {
-            data.append (QString::number (info.level ()));
-            data.append (info.level () > 1 ? tr (" points") : tr (" point"));
+        if (_settings->check (Settings::C_ShowRank)) {
+            data.append (tr ("%n point(s)", "", info.level ()));
             first = false;
         }
 
-        if (_settings->check (Settings::C_Time)) {
+        if (_settings->check (Settings::C_ShowTime)) {
             if (!first)
                 data.append (", ");
             data.append (info.localtime ());
             first = false;
         }
 
-        if (_settings->check (Settings::C_Hint)) {
-            data.append ("\n");
+        if (_settings->check (Settings::C_ShowHint)) {
+            if (!first)
+                data.append ("\n");
             data.append (info.hint ());
         }
 
         _label->setText (data);
     }
-    else
+    else {
         _light->setColor (ExtendedTrafficInfo::Unknown);
+        _label->setText (tr ("No data"));
+    }
 }
 
 
 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));
+    if (!_settings->check (Settings::C_UpdateWhenLocked))
+        update &= !DeviceState::instance ()->locked ();
+#endif
+
+    if (update)
+        _traffic->update ();
 }
 
 
@@ -114,8 +123,66 @@ void MainWidget::settingsDialog ()
 
     dlg.exec ();
 
-    // Handle settings
-    _light->setVisible (_settings->check (Settings::C_Light));
-
+    applySettings ();
     trafficUpdated ();
 }
+
+
+void MainWidget::updateSize ()
+{
+    QSize minSize (0, 80);
+
+    if (_settings->check (Settings::C_ShowLight))
+        minSize += QSize (80, 0);
+    if (_settings->check (Settings::C_ShowHint))
+        minSize += QSize (270, 0);
+    else {
+        if (_settings->check (Settings::C_ShowTime))
+            minSize += QSize (75, 0);
+        if (_settings->check (Settings::C_ShowRank))
+            minSize += QSize (75, 0);
+    }
+
+    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 ());
+}
+
+
+void MainWidget::mousePressEvent (QMouseEvent *event)
+{
+    QMenu menu;
+    QAction *settingsAction, *updateAction, *todo;
+
+    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 ();
+}