Update translation,
[vlc-remote] / playermainwindow.cpp
index b4b2ed4..22e6218 100644 (file)
@@ -1,5 +1,5 @@
   /*   VLC-REMOTE for MAEMO 5
-  *   Copyright (C) 2010 Schutz Sacha <istdasklar@gmail.com>
+  *   Copyright (C) 2010 Schutz Sacha <istdasklar@gmail.com>, Dru Moore <usr@dru-id.co.uk>, Yann Nave <yannux@onbebop.net>
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License version 2,
   *   or (at your option) any later version, as published by the Free
@@ -22,7 +22,7 @@
   #include "configdialog.h"
   #include "aboutdialog.h"
   #include "accountdialog.h"
-
+  //#include "vlcstatus.h"
 
   PlayerMainWindow::PlayerMainWindow(QWidget *parent) :
          QMainWindow(parent),
@@ -46,9 +46,9 @@
 
       ui->previousButton->setIcon(QIcon::fromTheme("pdf_viewer_first_page"));
       ui->nextButton->setIcon(QIcon::fromTheme("pdf_viewer_last_page"));
-      ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
+      ui->playpauseButton->setIcon(QIcon::fromTheme("camera_playback"));
       ui->stopButton->setIcon(QIcon::fromTheme("camera_video_stop"));
-      ui->pauseButton->setIcon(QIcon::fromTheme("camera_video_pause"));
+      //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"));
@@ -80,9 +80,9 @@
       connect(ui->browseButton,SIGNAL(clicked()),mBrowserMainWindow,SLOT(show()));
       connect(ui->browseButton,SIGNAL(clicked()),mBrowserMainWindow,SLOT(showCurrentDirectory()));
 
-      connect(ui->playButton,SIGNAL(clicked()),this,SLOT(play()));
+      connect(ui->playpauseButton,SIGNAL(clicked()),this,SLOT(playpause()));
       connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(stop()));
-      connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(pause()));
+      //connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(playpause()));
       connect(ui->previousButton,SIGNAL(clicked()),this,SLOT(previous()));
       connect(ui->nextButton,SIGNAL(clicked()),this,SLOT(next()));
       connect(ui->fullscreenButton,SIGNAL(clicked()),this,SLOT(fullscreen()));
       }
   }
 
+  void PlayerMainWindow::playpause()
+  {
+      // NB. There is no guarentee that our current state is the real current state.
+      // This is due to the polling frequency and possibility of user interaction directly on the server.
+      // Still this is probably better than nothing and our next real poll will set us straight again.
+      if (PAUSED == mCurrentStatus.state) {
+        mCurrentStatus.state = PLAYING;
+        qDebug() << "pause() from PAUSED";
+        pause();
+        updateUiWithCurrentStatus();
+      }
+      else if (PLAYING == mCurrentStatus.state) {
+        mCurrentStatus.state = PAUSED;
+        qDebug() << "pause() from PLAYING";
+        pause();
+        updateUiWithCurrentStatus();
+      }
+      else {
+        // could be STOP or UNKNOWN, either way there is no guarentee we will enter a playing state next.
+        // So don't update the current state or UI
+        // Ideally we would try to find a way to check the current state again but this could lead to an infinite loop!
+        qDebug() << "play() from " << ((STOP == mCurrentStatus.state) ? "STOP" : "UNKNOWN");
+        play();
+      }
+  }
   void PlayerMainWindow::play()
   {
-
       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play")));
-
   }
   void PlayerMainWindow::stop()
   {
       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_stop")));
-
   }
   void PlayerMainWindow::pause()
   {
       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_pause")));
-
   }
   void PlayerMainWindow::previous()
   {
       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_previous")));
-
   }
   void PlayerMainWindow::next()
   {
       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_next")));
-
   }
   void PlayerMainWindow::fullscreen()
   {
       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=fullscreen")));
-
   }
   void PlayerMainWindow::volUp()
   {
   }
   void PlayerMainWindow::volDown()
   {
-
       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=-20")));
-
   }
   void PlayerMainWindow::volMute()
   {
       else {
          mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val="+QString::number(this->mVolume))));
       }
-
   }
   void PlayerMainWindow::slide(int value)
   {
       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=seek&val="+QString::number(value)+"%25")));
-
   }
 
   void PlayerMainWindow::showConfig()
       mPlayListMainWindow->init();
       mBrowserMainWindow->init();
       mTimer->start(5000);
+      askStatus();
   }
   void PlayerMainWindow::showAbout()
   {
       QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
       QDomDocument doc;
       doc.setContent(reply->readAll());
+      delete reply;
       QDomElement docElem = doc.documentElement();
-
+      // Get the raw values
       int volume = docElem.namedItem("volume").toElement().text().toInt();
       int length = docElem.namedItem("length").toElement().text().toInt();
       int time = docElem.namedItem("time").toElement().text().toInt();
       int position = docElem.namedItem("position").toElement().text().toInt();
-      QString state  =docElem.namedItem("state").toElement().text();
-
-
-      if (0 < volume) {
-         this->mVolume = volume;
+      int random = docElem.namedItem("random").toElement().text().toInt();
+      int loop = docElem.namedItem("loop").toElement().text().toInt();
+      int repeat = docElem.namedItem("repeat").toElement().text().toInt();
+      QString state = docElem.namedItem("state").toElement().text();
+      QDomNode infoNode =  docElem.namedItem("information");
+      QDomNode metaInfoNode =  infoNode.namedItem("meta-information");
+      QString title = metaInfoNode.namedItem("title").toElement().text().replace("\\\\", "\\");
+      QString artist = metaInfoNode.namedItem("artist").toElement().text();
+      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();
+      // 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)) {
+        mCurrentStatus.newtrack = false;
+      }
+      mCurrentStatus.volume = volume;
+      mCurrentStatus.length = length;
+      mCurrentStatus.time = time;
+      mCurrentStatus.position = position;
+      mCurrentStatus.random = (1 == random);
+      mCurrentStatus.loop = (1 == loop);
+      mCurrentStatus.repeat = (1 == repeat);
+      mCurrentStatus.title = title;
+      mCurrentStatus.artist = artist;
+      mCurrentStatus.album = album;
+      mCurrentStatus.nowplaying = now_playing;
+      mCurrentStatus.hasart = (!art_url.isNull() && !art_url.isEmpty());
+      if (!state.isNull() && !state.isEmpty()) {
+          if (0 == QString::compare("playing", state, Qt::CaseInsensitive)) {
+            mCurrentStatus.state = PLAYING;
+          }
+          else if (0 == QString::compare("paused", state, Qt::CaseInsensitive)) {
+            mCurrentStatus.state = PAUSED;
+          }
+          else if (0 == QString::compare("stop", state, Qt::CaseInsensitive)) {
+            mCurrentStatus.state = STOP;
+          }
+          else {
+            mCurrentStatus.state = UNKNOWN;
+          }
+      }
+      else {
+          mCurrentStatus.state = UNKNOWN;
+      }
+      // What's our mute status?
+      if (0 < mCurrentStatus.volume) {
+          this->mVolume = mCurrentStatus.volume;
          this->mMuted = false;
       }
       else {
          this->mMuted = true;
       }
+      // Update the UI
+      updateUiWithCurrentStatus();
 
+  }
 
-      QTime timeLength(0,0,0) ;
-      timeLength =  timeLength.addSecs(time);
+  void PlayerMainWindow::updateUiWithCurrentStatus() {
+      // position
+      QTime timePosition(0,0,0) ;
+      timePosition =  timePosition.addSecs(mCurrentStatus.time);
 
-      ui->timeLabel->setText(timeLength.toString("mm:ss"));
+      ui->timeLabel->setText(timePosition.toString("h:mm:ss"));
 
+      // duration
+      if (0 < mCurrentStatus.length) {
+          QTime timeDuration(0,0,0) ;
+          timeDuration =  timeDuration.addSecs(mCurrentStatus.length);
 
-      QDomNode infoNode =  docElem.namedItem("information");
-      QDomNode metaInfoNode =  infoNode.namedItem("meta-information");
-      QString title = metaInfoNode.namedItem("title").toElement().text();
+          ui->durationLabel->setText(timeDuration.toString("h:mm:ss"));
+      }
+      else {
+          ui->durationLabel->setText("0:00:00");
+      }
 
-      if ( position >= 0 && position <=100)
-         ui->slider->setValue(position);
 
-      ui->label->setText(title);
-      delete reply;
+      if (mCurrentStatus.position >= 0 && mCurrentStatus.position <= 100)
+          ui->slider->setValue(mCurrentStatus.position);
+
+      ui->label->setText(mCurrentStatus.title);
+
+      if (PLAYING == mCurrentStatus.state) {
+          ui->playpauseButton->setIcon(QIcon::fromTheme("camera_video_pause"));
+      }
+      else {
+          ui->playpauseButton->setIcon(QIcon::fromTheme("camera_playback"));
+      }
 
+      if (mCurrentStatus.newtrack) {
+          // potential actions:
+          //   rebuild display layout
+          //   retrieve album art
+      }
+      // Update the buttons on the playlist window
+      if (NULL != this->mPlayListMainWindow) {
+        this->mPlayListMainWindow->updateUiWithCurrentStatus(& mCurrentStatus);
+      }
   }