Bugfix to settingsdlg progress indicator
[irwi] / src / settingsdlg.cpp
index 4b7d5b3..f7b4cfa 100644 (file)
 #include <QPushButton>
 #include <QLabel>
 #include <QDebug>
+#include <QNetworkConfiguration>
+#include <QShowEvent>
 
 SettingsDlg::SettingsDlg(QWidget *parent)
     : QDialog(parent)
+    , m_busy(false)
+    , m_netConfMan(NULL)
 {
     QSettings settings(this);
     m_layout = new QVBoxLayout(this);
-    m_btnLayout = new QHBoxLayout(this);
-    m_remoteNameLayout = new QHBoxLayout(this);
+    m_btnLayout = new QHBoxLayout();
+    m_remoteNameLayout = new QHBoxLayout();
     
     m_advSettingsBtn = new QPushButton(tr("Advanced"), this);
-    m_selectRemoteBtn = new QPushButton(tr("Select m_remote"), this);
+    m_selectRemoteBtn = new QPushButton(tr("Select remote"), this);
     m_aboutBtn = new QPushButton(tr("About"), this);
     m_rateUpBtn = new QPushButton(
             QIcon(settings.value("rateUpIcon",
@@ -61,7 +65,24 @@ SettingsDlg::SettingsDlg(QWidget *parent)
     m_layout->addLayout(m_btnLayout);
     this->setLayout(m_layout);
 
-    initRemote();
+    QString selectedRemote = settings.value("remoteName", "").toString();
+    if (selectedRemote == "") {
+        m_remoteNameLabel->setText(tr("No remote selected"));
+        enableRateBtns(false);
+    } else {
+        // Create remote by name and update it's info if online
+        m_busy = true;
+        m_remote = Remote(selectedRemote);
+        connect(&m_remote, SIGNAL(infoUpdated()),
+                this, SLOT(updateRemoteInfo()));
+        m_remoteNameLabel->setText(selectedRemote);
+        m_netConfMan = new QTM_PREPEND_NAMESPACE(
+                QNetworkConfigurationManager)(this);
+        connect(m_netConfMan, SIGNAL(updateCompleted()),
+                this, SLOT(onNetworkStatusUpdate()));
+        m_netConfMan->updateConfigurations();
+        enableRateBtns(false);
+    }
 }
 
 SettingsDlg::~SettingsDlg()
@@ -76,52 +97,60 @@ SettingsDlg::~SettingsDlg()
     delete m_btnLayout;
     delete m_remoteNameLayout;
     delete m_layout;
+    delete m_netConfMan;
+}
+
+void SettingsDlg::setBusy(bool busy)
+{
+    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, busy);
+    m_busy = busy;
 }
 
 void SettingsDlg::showAdvSettingsDlg()
 {
-    AdvSettingsDlg dlg(this);
+    AdvSettingsDlg dlg;
     dlg.exec();
 }
 
 void SettingsDlg::showSelectRemoteDlg()
 {
-    SelectRemoteDlg dlg(this);
-    connect(&dlg, SIGNAL(m_remoteChanged(Remote)),
+    SelectRemoteDlg dlg;
+    connect(&dlg, SIGNAL(remoteChanged(Remote)),
             this, SLOT(setRemote(Remote)));
     dlg.exec();
+    onNetworkStatusUpdate();
 }
 
 void SettingsDlg::showAboutDlg()
 {
-    AboutDlg dlg(this);
+    AboutDlg dlg;
     dlg.exec();
 }
 
-void SettingsDlg::initRemote()
-{
-    QString selectedRemote = QSettings(this).value("remoteName", "").toString();
-    if (selectedRemote == "") {
-        m_remoteNameLabel->setText("No remote selected");
-        enableRateBtns(false);
-    } else {
-        m_remote = Remote(selectedRemote);
-        connect(&m_remote, SIGNAL(infoUpdated()),
-                this, SLOT(updateRemoteInfo()));
-        m_remote.updateInfo();
-        enableRateBtns();
-    }
-}
-
 void SettingsDlg::setRemote(Remote r)
 {
     m_remote = r;
+    connect(&m_remote, SIGNAL(infoUpdated()),
+            this, SLOT(updateRemoteInfo()));
     updateRemoteInfo();
     enableRateBtns();
 }
 
+void SettingsDlg::onNetworkStatusUpdate()
+{
+    if (m_netConfMan->isOnline()) {
+        setBusy();
+        m_remote.updateInfo();
+        enableRateBtns();
+    } else {
+        m_ratingLabel->setText(tr("Offline"));
+        setBusy(false);
+    }
+}
+
 void SettingsDlg::updateRemoteInfo()
 {
+    setBusy(false);
     m_remoteNameLabel->setText(m_remote.mfg() + " " + m_remote.name());
     m_ratingLabel->setText(tr("Rating") + ": "
             + QString::number(m_remote.rating()));
@@ -129,13 +158,19 @@ void SettingsDlg::updateRemoteInfo()
 
 void SettingsDlg::rateUpClicked()
 {
+    processRatingSent();
     m_remote.sendRating(Rating::Up);
-    enableRateBtns(false);
 }
 
 void SettingsDlg::rateDownClicked()
 {
+    processRatingSent();
     m_remote.sendRating(Rating::Down);
+}
+
+void SettingsDlg::processRatingSent()
+{
+    setBusy();
     enableRateBtns(false);
 }
 
@@ -145,4 +180,11 @@ void SettingsDlg::enableRateBtns(bool enable)
     m_rateDownBtn->setEnabled(enable);
 }
 
+void SettingsDlg::showEvent(QShowEvent *event)
+{
+    setBusy(m_busy);
+    QDialog::showEvent(event);
+}
+
+