Added Network status checking to fix Bug [#6294] Becoming disconnected from network...
authordruid23 <usr@dru-id.co.uk>
Sun, 5 Sep 2010 10:49:12 +0000 (11:49 +0100)
committerdruid23 <usr@dru-id.co.uk>
Sun, 5 Sep 2010 10:49:12 +0000 (11:49 +0100)
vlc-remote will now not let you use the interface without an active connection (wlan or gprs).
This will break the use case of using vlc-remote to control a local (on device) vlc instance!
modified:   src/accountdialog.cpp
modified:   src/appsettings.cpp
modified:   src/appsettings.h
modified:   src/playermainwindow.cpp

src/accountdialog.cpp
src/appsettings.cpp
src/appsettings.h
src/playermainwindow.cpp

index ecdb02f..a5fe39e 100644 (file)
 #include <QTcpSocket>
 #include <QFuture>
 #include <QtConcurrentMap>
+#include "appsettings.h"
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+#include <QMaemo5InformationBox>
+#endif
 
 
 
@@ -111,7 +115,9 @@ void AccountDialog::load()
             item->setFont(font);
         }
         ui->listWidget->addItem(item);
-        asycItems.append(*item);
+        if (AppSettings::isConnected()) {
+            asycItems.append(*item);
+        }
     }
     settings.endGroup();    
 
@@ -121,7 +127,14 @@ void AccountDialog::load()
     //  QFuture<QListWidgetItem> itemFutur = QtConcurrent::mapped(asycItems, asyncTestItem);
 
 
-    mFuturWatcher->setFuture(QtConcurrent::mapped(asycItems, asyncTestItem));
+    if (AppSettings::isConnected()) {
+        mFuturWatcher->setFuture(QtConcurrent::mapped(asycItems, asyncTestItem));
+    }
+    else {
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+        QMaemo5InformationBox::information(this, tr("No network connection available!"), QMaemo5InformationBox::DefaultTimeout);
+#endif
+    }
 }
 
 QListWidgetItem AccountDialog::asyncTestItem(const QListWidgetItem& item)
index babef8c..e910978 100644 (file)
@@ -16,6 +16,7 @@
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 #include <QStringList>
+#include <QNetworkInterface>
 #include "appsettings.h"
 
 AppSettings::AppSettings() {
@@ -24,7 +25,19 @@ AppSettings::AppSettings() {
 AppSettings::~AppSettings() {
     ;
 }
+bool AppSettings::isConnected() {
+    QNetworkInterface wlan = QNetworkInterface::interfaceFromName("wlan0");
+    QNetworkInterface gprs = QNetworkInterface::interfaceFromName("gprs0");
 
+    if( (wlan.isValid() && wlan.flags().testFlag(QNetworkInterface::IsUp)) || (gprs.isValid() && gprs.flags().testFlag(QNetworkInterface::IsUp)) )
+    {
+        return true;
+    }
+    else
+    {
+        return false;
+    }
+}
 QString AppSettings::getCurrentKey() {
     QSettings sets;
     return sets.value("config/currentKey", "").toString();
index 407b5a2..98b287b 100644 (file)
@@ -42,6 +42,7 @@ public:
     static bool setHomeDirectory(VlcDirectory dir);
     static Orientation setOrientation(Orientation orientation);
     static Orientation getOrientation();
+    static bool isConnected();
 //private:
     //static QSettings settings;
 };
index 26ea30e..f34a9c8 100644 (file)
@@ -24,6 +24,9 @@
   #include "aboutdialog.h"
   #include "accountdialog.h"
   #include "appsettings.h"
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+#include <QMaemo5InformationBox>
+#endif
 
   PlayerMainWindow::PlayerMainWindow(QWidget *parent) :
          QMainWindow(parent),
 
 
       // check if last used connection is still valid or showConfig
-      QSettings settings;
-      QString last_ip = AccountDialog::currentIp();
-      if (!last_ip.isNull() && !last_ip.isEmpty()) {
-          QTcpSocket * socket = new QTcpSocket;
-          if(last_ip.contains(":"))
-          {
-              QStringList hostSplit = last_ip.split(":");
-              QString ip   = hostSplit.at(0);
-              QString port = hostSplit.at(1);
-              socket->connectToHost(ip,port.toInt());
-          }
-          else {
-              socket->connectToHost(last_ip,8080);
+
+      // check for network
+      if (AppSettings::isConnected()) {
+          QSettings settings;
+          QString last_ip = AccountDialog::currentIp();
+          if (!last_ip.isNull() && !last_ip.isEmpty()) {
+              QTcpSocket * socket = new QTcpSocket;
+              if(last_ip.contains(":"))
+              {
+                  QStringList hostSplit = last_ip.split(":");
+                  QString ip   = hostSplit.at(0);
+                  QString port = hostSplit.at(1);
+                  socket->connectToHost(ip,port.toInt());
+              }
+              else {
+                  socket->connectToHost(last_ip,8080);
+              }
+              if (!socket->waitForConnected(1000)) {
+                     showConfig();
+                 }
+              else {
+                  mIp= last_ip;
+
+                 mPlayListMainWindow->init();
+                 mBrowserMainWindow->init();
+                 mTimer->start(5000);
+                 askStatus();
+              }
+              delete socket;
           }
-          if (!socket->waitForConnected(1000)) {
-                 showConfig();
-             }
           else {
-              mIp= last_ip;
-
-             mPlayListMainWindow->init();
-             mBrowserMainWindow->init();
-             mTimer->start(5000);
-             askStatus();
+            showConfig();
           }
-          delete socket;
       }
       else {
-        showConfig();
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+        QMaemo5InformationBox::information(this, tr("Network unavailable!"), QMaemo5InformationBox::DefaultTimeout);
+#endif
+          showConfig();
       }
-
-
   }
   
 
   void PlayerMainWindow::showConfig()
   {
       mTimer->stop();
-      AccountDialog * dialog = new AccountDialog;
-      dialog->exec();
-     
-       mIp= AccountDialog::currentIp();
+      // check for network
+      if (AppSettings::isConnected()) {
+          AccountDialog * dialog = new AccountDialog(this);
+          dialog->exec();
 
-      mPlayListMainWindow->init();
-      mBrowserMainWindow->init();
-      mTimer->start(5000);
-      askStatus();
+           mIp= AccountDialog::currentIp();
+
+          mPlayListMainWindow->init();
+          mBrowserMainWindow->init();
+          mTimer->start(5000);
+          askStatus();
+      }
+      else {
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+        QMaemo5InformationBox::information(this, tr("Network unavailable!"), QMaemo5InformationBox::DefaultTimeout);
+#endif
+        QTimer::singleShot(20000, this, SLOT(showConfig()));
+      }
   }
   void PlayerMainWindow::showAbout()
   {
   void PlayerMainWindow::askStatus()
   {
       //qDebug() << "Status requested. at:" << QTime::currentTime().toString("hh::mm:ss");
-      QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml")));
-      connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlStatus()));
+      if (AppSettings::isConnected()) {
+          QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml")));
+          connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlStatus()));
+          connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
+      }
+      else {
+        showConfig(); // this will handle stopping and restarting the timer.
+      }
   }
 
   void PlayerMainWindow::parseXmlStatus()