Began refactoring settings handling.
[vlc-remote] / playermainwindow.cpp
index c8ee2e5..c77bd56 100644 (file)
   */
   #include <QDebug>
   #include <QTime>
+  #include <QtGui>
   #include "playermainwindow.h"
   #include "ui_playermainwindow.h"
   #include "configdialog.h"
   #include "aboutdialog.h"
   #include "accountdialog.h"
-
+  //#include "vlcstatus.h"
 
   PlayerMainWindow::PlayerMainWindow(QWidget *parent) :
          QMainWindow(parent),
       mVolume = 100;
       mMuted = false;
 
+      mIsLandscape = true;
+
       ui->playlistButton->setIcon(QIcon::fromTheme("notes_bullets"));
       ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
 
       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"));
       ui->volMute->setIcon(QIcon::fromTheme("statusarea_volume_mute"));
 
+      ui->labelArtPortrait->setVisible(false);
+      ui->labelArtLandscape->setVisible(false);
+
+      ui->labelTitle->setTextFormat(Qt::RichText);
+      ui->labelArtist->setTextFormat(Qt::RichText);
+      ui->labelAlbum->setTextFormat(Qt::RichText);
+
 
   #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+
       mPlayListMainWindow->setParent(this);
       mPlayListMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
-      mPlayListMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
-      mPlayListMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
       setAttribute(Qt::WA_Maemo5StackedWindow);
       mPlayListMainWindow->setWindowFlags(mPlayListMainWindow->windowFlags() | Qt::Window);
 
       mBrowserMainWindow->setParent(this);
       mBrowserMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
-      mBrowserMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
-      mBrowserMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
       setAttribute(Qt::WA_Maemo5StackedWindow);
       mBrowserMainWindow->setWindowFlags(mBrowserMainWindow->windowFlags() | Qt::Window);
 
+      connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
+
   #endif
 
       connect(mTimer,SIGNAL(timeout()),this,SLOT(askStatus()));
       connect(ui->actionConfiguration,SIGNAL(triggered()),this,SLOT(showConfig()));
       connect(ui->actionAbout,SIGNAL(triggered()),this,SLOT(showAbout()));
+      connect(ui->actionPortrait,SIGNAL(triggered()),this,SLOT(setPortrait()));
+      connect(ui->actionLandscape,SIGNAL(triggered()),this,SLOT(setLandscape()));
       connect(ui->playlistButton,SIGNAL(clicked()),mPlayListMainWindow,SLOT(show()));
       connect(ui->playlistButton,SIGNAL(clicked()),mPlayListMainWindow,SLOT(showPlayList()));
       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()));
       connect(ui->volMute,SIGNAL(clicked()),this,SLOT(volMute()));
       connect(ui->slider,SIGNAL(sliderMoved(int)),this,SLOT(slide(int)));
 
-             showConfig();
+      connect(mPlayListMainWindow, SIGNAL(idUpdated(int,bool,QString)), this, SLOT(playlistIdUpdated(int, bool, QString)));
+
+
+      // 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);
+          }
+          if (!socket->waitForConnected(1000)) {
+                 showConfig();
+             }
+          else {
+              mIp= last_ip;
+
+             mPlayListMainWindow->init();
+             mBrowserMainWindow->init();
+             mTimer->start(5000);
+             askStatus();
+          }
+          delete socket;
+      }
+      else {
+        showConfig();
+      }
 
 
   }
       }
   }
 
+  void PlayerMainWindow::setPortrait()
+  {
+     #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+     this->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
+    #endif
+    
+      
+  }
+
+  void PlayerMainWindow::setLandscape()
+  {
+      #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+      this->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
+      #endif
+  }
+
+  void PlayerMainWindow::setAutoRotate()
+  {
+    #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+   this->setAttribute(Qt::WA_Maemo5AutoOrientation, true);
+   #endif
+  }
+
+  void PlayerMainWindow::orientationChanged() {
+      QRect screenGeometry = QApplication::desktop()->screenGeometry();
+      mIsLandscape = (screenGeometry.width() > screenGeometry.height());
+      if (mHasImage) {
+          if (mIsLandscape) {
+              ui->labelArtPortrait->setVisible(false);
+              ui->labelArtLandscape->setVisible(true);
+          }
+          else {
+              ui->labelArtLandscape->setVisible(false);
+              ui->labelArtPortrait->setVisible(true);
+          }
+      }
+      else {
+          ui->labelArtLandscape->setVisible(false);
+          ui->labelArtPortrait->setVisible(false);
+      }
+  }
+
+  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;
+        pause();
+        updateUiWithCurrentStatus();
+      }
+      else if (PLAYING == mCurrentStatus.state) {
+        mCurrentStatus.state = PAUSED;
+        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!
+        play();
+      }
+  }
   void PlayerMainWindow::play()
   {
       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play")));
       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("\\\\", "\\");
+      // if it's a file style title fix it up
+      if (40 < title.length()) {
+          if (0 < title.lastIndexOf("\\")) {
+              title = title.right(title.length() - (title.lastIndexOf("\\") + 1));
+          }
+          else if (0 < title.lastIndexOf("/")) {
+              title = title.right(title.length() - (title.lastIndexOf("/") + 1));
+          }
+      }
+      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("h: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().replace("\\\\", "\\");
+          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->labelTitle->setText(mCurrentStatus.title);
+      ui->labelArtist->setText(mCurrentStatus.artist);
+      ui->labelAlbum->setText(mCurrentStatus.album);
+
+      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
+          mHasImage = false;
+          QTimer::singleShot(500, mPlayListMainWindow, SLOT(requestPlayList()));
+      }
+      // Update the buttons on the playlist window
+      if (NULL != this->mPlayListMainWindow) {
+        this->mPlayListMainWindow->updateUiWithCurrentStatus(& mCurrentStatus);
+      }
+
+  }
+  void PlayerMainWindow::playlistIdUpdated(int id, bool hasart, QString extension) {
+      if (hasart) {
+          getCoverArt(id);
+      }
+      else {
+          ui->labelArtLandscape->setVisible(false);
+          ui->labelArtPortrait->setVisible(false);
+          // could use a default graphic here!
+          // setCoverArtFromPixmap();
+      }
+  }
+  void PlayerMainWindow::readReady() {
+    QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
+    // append to buffer
+    mResponse += reply->readAll();
+  }
+  void PlayerMainWindow::finished(QNetworkReply * reply) {
+    // now we can call setCoverArt to process the full buffers
+    this->setCoverArt(mResponse);
+    // only interested in finished signals
+    disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
+  }
+  void PlayerMainWindow::getCoverArt(int id) {
+    mResponse.clear();
+    QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/art?id=" + QString::number(id))));
+    connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
+    connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
+
+  }
+  void PlayerMainWindow::setCoverArt(const QByteArray data) {
+    QPixmap* image = new QPixmap();
+    if (image->loadFromData(data)) {
+        mHasImage = true;
+        ui->labelArtLandscape->setPixmap(image->scaledToHeight(120, Qt::SmoothTransformation));
+        ui->labelArtPortrait->setPixmap(image->scaledToHeight(310, Qt::SmoothTransformation));
+        if (mIsLandscape) {
+            ui->labelArtPortrait->setVisible(false);
+            ui->labelArtLandscape->setVisible(true);
+        }
+        else {
+            ui->labelArtLandscape->setVisible(false);
+            ui->labelArtPortrait->setVisible(true);
+        }
+    }
+    else {
+        ui->labelArtPortrait->setVisible(false);
+        ui->labelArtLandscape->setVisible(false);
+    }
+  }
+  void PlayerMainWindow::setCoverArtFromPixmap(QPixmap image) {
+    mHasImage = true;
+    ui->labelArtLandscape->setPixmap(image.scaledToHeight(120, Qt::SmoothTransformation));
+    ui->labelArtPortrait->setPixmap(image.scaledToHeight(320, Qt::SmoothTransformation));
+    if (mIsLandscape) {
+        ui->labelArtPortrait->setVisible(false);
+        ui->labelArtLandscape->setVisible(true);
+    }
+    else {
+        ui->labelArtLandscape->setVisible(false);
+        ui->labelArtPortrait->setVisible(true);
+    }
   }