Don't poll for album art when track is stopped.
[vlc-remote] / src / playermainwindow.cpp
index 33d001c..ff734cb 100644 (file)
@@ -24,7 +24,9 @@
   #include "aboutdialog.h"
   #include "accountdialog.h"
   #include "appsettings.h"
-  //#include "vlcstatus.h"
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+#include <QMaemo5InformationBox>
+#endif
 
   PlayerMainWindow::PlayerMainWindow(QWidget *parent) :
          QMainWindow(parent),
@@ -39,6 +41,7 @@
       mNetManager = new QNetworkAccessManager(this);
       mPlayListMainWindow = new PlayListMainWindow;
       mBrowserMainWindow = new BrowseMainWindow;
+      mFavouritesMainWindow = new FavouritesMainWindow;
 
       mVolume = 100;
       mMuted = false;
@@ -52,7 +55,6 @@
       ui->nextButton->setIcon(QIcon::fromTheme("pdf_viewer_last_page"));
       ui->playpauseButton->setIcon(QIcon::fromTheme("camera_playback"));
       ui->stopButton->setIcon(QIcon::fromTheme("camera_video_stop"));
-      //ui->pauseButton->setIcon(QIcon::fromTheme("camera_video_pause"));
       ui->fullscreenButton->setIcon(QIcon::fromTheme("general_fullsize"));
       ui->volDown->setIcon(QIcon::fromTheme("statusarea_volumelevel1"));
       ui->volUp->setIcon(QIcon::fromTheme("statusarea_volumelevel4"));
       setAttribute(Qt::WA_Maemo5StackedWindow, true);
       mBrowserMainWindow->setWindowFlags(mBrowserMainWindow->windowFlags() | Qt::Window);
 
+      mFavouritesMainWindow->setParent(this);
+      mFavouritesMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow, true);
+      setAttribute(Qt::WA_Maemo5StackedWindow, true);
+      mFavouritesMainWindow->setWindowFlags(mFavouritesMainWindow->windowFlags() | Qt::Window);
+
       connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
 
   #endif
       connect(ui->slider,SIGNAL(sliderMoved(int)),this,SLOT(slide(int)));
 
       connect(mPlayListMainWindow, SIGNAL(idUpdated(int,bool,QString)), this, SLOT(playlistIdUpdated(int, bool, QString)));
+      connect(mBrowserMainWindow, SIGNAL(showFavouritesWindow()), this, SLOT(showFavourites()));
+
+      connect(mFavouritesMainWindow, SIGNAL(browseDirectory(QString)), mFavouritesMainWindow, SLOT(close()));
+      connect(mFavouritesMainWindow, SIGNAL(browseDirectory(QString)), mBrowserMainWindow, SLOT(browseDirectory(QString)));
+      connect(mFavouritesMainWindow, SIGNAL(closeSignal()), mBrowserMainWindow, SLOT(show()));
 
 
       // 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::showFavourites() {
+      mFavouritesMainWindow->show();
+      mFavouritesMainWindow->init();
+  }
+
   void PlayerMainWindow::setPortrait()
   {
     #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
   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();
+
+           mIp= AccountDialog::currentIp();
 
-      mPlayListMainWindow->init();
-      mBrowserMainWindow->init();
-      mTimer->start(5000);
-      askStatus();
+          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()
       QString album = metaInfoNode.namedItem("album").toElement().text();
       QString now_playing = metaInfoNode.namedItem("now_playing").toElement().text();
       QString art_url = metaInfoNode.namedItem("art_url").toElement().text();
-      //metaInfoNode.clear();
-      //infoNode.clear();
-      //docElem.clear();
-      //doc.clear();
       // Populate the current status structure
       // now would be a good time to work out if we are a new track / file or not.
       // key if we are going to look for album art later
       // for now we check length and title this will require further examination later
       mCurrentStatus.newtrack = true;
-      if (mCurrentStatus.length == length && !mCurrentStatus.title.isNull() && 0 == QString::compare(mCurrentStatus.title, title)) {
+      if ( (0 == mCurrentStatus.length || STOP == mCurrentStatus.state) // stopped or null
+              || // same track as current playing
+           (mCurrentStatus.length == length && !mCurrentStatus.title.isNull() && 0 == QString::compare(mCurrentStatus.title, title)) ){
         mCurrentStatus.newtrack = false;
       }
       mCurrentStatus.volume = volume;