Added Network status checking to fix Bug [#6294] Becoming disconnected from network...
[vlc-remote] / src / playermainwindow.cpp
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()