Enabled orientation selection (Potrait Mode)
[vlc-remote] / playermainwindow.cpp
1   /*   VLC-REMOTE for MAEMO 5
2   *   Copyright (C) 2010 Schutz Sacha <istdasklar@gmail.com>, Dru Moore <usr@dru-id.co.uk>, Yann Nave <yannux@onbebop.net>
3   *   This program is free software; you can redistribute it and/or modify
4   *   it under the terms of the GNU General Public License version 2,
5   *   or (at your option) any later version, as published by the Free
6   *   Software Foundation
7   *
8   *   This program is distributed in the hope that it will be useful,
9   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   *   GNU General Public License for more details
12   *
13   *   You should have received a copy of the GNU General Public
14   *   License along with this program; if not, write to the
15   *   Free Software Foundation, Inc.,
16   *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17   */
18   #include <QDebug>
19   #include <QTime>
20   #include <QtGui>
21   #include "playermainwindow.h"
22   #include "ui_playermainwindow.h"
23   #include "configdialog.h"
24   #include "aboutdialog.h"
25   #include "accountdialog.h"
26   //#include "vlcstatus.h"
27
28   PlayerMainWindow::PlayerMainWindow(QWidget *parent) :
29           QMainWindow(parent),
30           ui(new Ui::PlayerMainWindow)
31   {
32       ui->setupUi(this);
33       setWindowTitle("Vlc remote");
34
35
36
37       mTimer = new QTimer(this);
38       mNetManager = new QNetworkAccessManager(this);
39       mPlayListMainWindow = new PlayListMainWindow;
40       mBrowserMainWindow = new BrowseMainWindow;
41
42       mVolume = 100;
43       mMuted = false;
44
45       mIsLandscape = true;
46
47       ui->playlistButton->setIcon(QIcon::fromTheme("notes_bullets"));
48       ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
49
50       ui->previousButton->setIcon(QIcon::fromTheme("pdf_viewer_first_page"));
51       ui->nextButton->setIcon(QIcon::fromTheme("pdf_viewer_last_page"));
52       ui->playpauseButton->setIcon(QIcon::fromTheme("camera_playback"));
53       ui->stopButton->setIcon(QIcon::fromTheme("camera_video_stop"));
54       //ui->pauseButton->setIcon(QIcon::fromTheme("camera_video_pause"));
55       ui->fullscreenButton->setIcon(QIcon::fromTheme("general_fullsize"));
56       ui->volDown->setIcon(QIcon::fromTheme("statusarea_volumelevel1"));
57       ui->volUp->setIcon(QIcon::fromTheme("statusarea_volumelevel4"));
58       ui->volMute->setIcon(QIcon::fromTheme("statusarea_volume_mute"));
59
60       ui->labelArtPortrait->setVisible(false);
61       ui->labelArtLandscape->setVisible(false);
62
63       ui->labelTitle->setTextFormat(Qt::RichText);
64       ui->labelArtist->setTextFormat(Qt::RichText);
65       ui->labelAlbum->setTextFormat(Qt::RichText);
66
67
68   #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
69
70       mPlayListMainWindow->setParent(this);
71       mPlayListMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
72       setAttribute(Qt::WA_Maemo5StackedWindow);
73       mPlayListMainWindow->setWindowFlags(mPlayListMainWindow->windowFlags() | Qt::Window);
74
75       mBrowserMainWindow->setParent(this);
76       mBrowserMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
77       setAttribute(Qt::WA_Maemo5StackedWindow);
78       mBrowserMainWindow->setWindowFlags(mBrowserMainWindow->windowFlags() | Qt::Window);
79
80       connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
81
82   #endif
83
84       connect(mTimer,SIGNAL(timeout()),this,SLOT(askStatus()));
85       connect(ui->actionConfiguration,SIGNAL(triggered()),this,SLOT(showConfig()));
86       connect(ui->actionAbout,SIGNAL(triggered()),this,SLOT(showAbout()));
87       connect(ui->actionPortrait,SIGNAL(triggered()),this,SLOT(setPortrait()));
88       connect(ui->actionLandscape,SIGNAL(triggered()),this,SLOT(setLandscape()));
89       connect(ui->playlistButton,SIGNAL(clicked()),mPlayListMainWindow,SLOT(show()));
90       connect(ui->playlistButton,SIGNAL(clicked()),mPlayListMainWindow,SLOT(showPlayList()));
91       connect(ui->browseButton,SIGNAL(clicked()),mBrowserMainWindow,SLOT(show()));
92       connect(ui->browseButton,SIGNAL(clicked()),mBrowserMainWindow,SLOT(showCurrentDirectory()));
93
94       connect(ui->playpauseButton,SIGNAL(clicked()),this,SLOT(playpause()));
95       connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(stop()));
96       //connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(playpause()));
97       connect(ui->previousButton,SIGNAL(clicked()),this,SLOT(previous()));
98       connect(ui->nextButton,SIGNAL(clicked()),this,SLOT(next()));
99       connect(ui->fullscreenButton,SIGNAL(clicked()),this,SLOT(fullscreen()));
100       connect(ui->volUp,SIGNAL(clicked()),this,SLOT(volUp()));
101       connect(ui->volDown,SIGNAL(clicked()),this,SLOT(volDown()));
102       connect(ui->volMute,SIGNAL(clicked()),this,SLOT(volMute()));
103       connect(ui->slider,SIGNAL(sliderMoved(int)),this,SLOT(slide(int)));
104
105       connect(mPlayListMainWindow, SIGNAL(idUpdated(int,bool,QString)), this, SLOT(playlistIdUpdated(int, bool, QString)));
106
107
108       // check if last used connection is still valid or showConfig
109       QSettings settings;
110       QString last_ip = AccountDialog::currentIp();
111       if (!last_ip.isNull() && !last_ip.isEmpty()) {
112           QTcpSocket * socket = new QTcpSocket;
113           if(last_ip.contains(":"))
114           {
115               QStringList hostSplit = last_ip.split(":");
116               QString ip   = hostSplit.at(0);
117               QString port = hostSplit.at(1);
118               socket->connectToHost(ip,port.toInt());
119           }
120           else {
121               socket->connectToHost(last_ip,8080);
122           }
123           if (!socket->waitForConnected(1000)) {
124                  showConfig();
125              }
126           else {
127               mIp= last_ip;
128
129              mPlayListMainWindow->init();
130              mBrowserMainWindow->init();
131              mTimer->start(5000);
132              askStatus();
133           }
134           delete socket;
135       }
136       else {
137         showConfig();
138       }
139
140
141   }
142   
143
144   PlayerMainWindow::~PlayerMainWindow()
145   {
146       delete ui;
147   }
148
149   void PlayerMainWindow::changeEvent(QEvent *e)
150   {
151       QMainWindow::changeEvent(e);
152       switch (e->type()) {
153       case QEvent::LanguageChange:
154           ui->retranslateUi(this);
155           break;
156       default:
157           break;
158       }
159   }
160
161   void PlayerMainWindow::setPortrait()
162   {
163       this->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
164   }
165
166   void PlayerMainWindow::setLandscape()
167   {
168       this->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
169   }
170
171   void PlayerMainWindow::setAutoRotate()
172   {
173       this->setAttribute(Qt::WA_Maemo5AutoOrientation, true);
174   }
175
176   void PlayerMainWindow::orientationChanged() {
177       QRect screenGeometry = QApplication::desktop()->screenGeometry();
178       mIsLandscape = (screenGeometry.width() > screenGeometry.height());
179       if (mHasImage) {
180           if (mIsLandscape) {
181               ui->labelArtPortrait->setVisible(false);
182               ui->labelArtLandscape->setVisible(true);
183           }
184           else {
185               ui->labelArtLandscape->setVisible(false);
186               ui->labelArtPortrait->setVisible(true);
187           }
188       }
189       else {
190           ui->labelArtLandscape->setVisible(false);
191           ui->labelArtPortrait->setVisible(false);
192       }
193   }
194
195   void PlayerMainWindow::playpause()
196   {
197       // NB. There is no guarentee that our current state is the real current state.
198       // This is due to the polling frequency and possibility of user interaction directly on the server.
199       // Still this is probably better than nothing and our next real poll will set us straight again.
200       if (PAUSED == mCurrentStatus.state) {
201         mCurrentStatus.state = PLAYING;
202         pause();
203         updateUiWithCurrentStatus();
204       }
205       else if (PLAYING == mCurrentStatus.state) {
206         mCurrentStatus.state = PAUSED;
207         pause();
208         updateUiWithCurrentStatus();
209       }
210       else {
211         // could be STOP or UNKNOWN, either way there is no guarentee we will enter a playing state next.
212         // So don't update the current state or UI
213         // Ideally we would try to find a way to check the current state again but this could lead to an infinite loop!
214         play();
215       }
216   }
217   void PlayerMainWindow::play()
218   {
219       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play")));
220   }
221   void PlayerMainWindow::stop()
222   {
223       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_stop")));
224   }
225   void PlayerMainWindow::pause()
226   {
227       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_pause")));
228   }
229   void PlayerMainWindow::previous()
230   {
231       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_previous")));
232   }
233   void PlayerMainWindow::next()
234   {
235       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_next")));
236   }
237   void PlayerMainWindow::fullscreen()
238   {
239       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=fullscreen")));
240   }
241   void PlayerMainWindow::volUp()
242   {
243       QUrl url = QUrl("http://"+mIp+"/requests/status.xml?command=volume");
244       url.addEncodedQueryItem(QByteArray("val"), QByteArray("%2B20"));
245       mNetManager->get(QNetworkRequest(url));
246   }
247   void PlayerMainWindow::volDown()
248   {
249       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=-20")));
250   }
251   void PlayerMainWindow::volMute()
252   {
253       this->mMuted = !this->mMuted;
254       if (this->mMuted) {
255           mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=0")));
256       }
257       else {
258           mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val="+QString::number(this->mVolume))));
259       }
260   }
261   void PlayerMainWindow::slide(int value)
262   {
263       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=seek&val="+QString::number(value)+"%25")));
264   }
265
266   void PlayerMainWindow::showConfig()
267   {
268       mTimer->stop();
269       AccountDialog * dialog = new AccountDialog;
270       dialog->exec();
271      
272        mIp= AccountDialog::currentIp();
273
274       mPlayListMainWindow->init();
275       mBrowserMainWindow->init();
276       mTimer->start(5000);
277       askStatus();
278   }
279   void PlayerMainWindow::showAbout()
280   {
281
282       AboutDialog * dialog = new AboutDialog;
283       dialog->exec();
284
285   }
286
287   void PlayerMainWindow::askStatus()
288   {
289
290       QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml")));
291       connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlStatus()));
292   }
293
294   void PlayerMainWindow::parseXmlStatus()
295   {
296       QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
297       QDomDocument doc;
298       doc.setContent(reply->readAll());
299       delete reply;
300       QDomElement docElem = doc.documentElement();
301       // Get the raw values
302       int volume = docElem.namedItem("volume").toElement().text().toInt();
303       int length = docElem.namedItem("length").toElement().text().toInt();
304       int time = docElem.namedItem("time").toElement().text().toInt();
305       int position = docElem.namedItem("position").toElement().text().toInt();
306       int random = docElem.namedItem("random").toElement().text().toInt();
307       int loop = docElem.namedItem("loop").toElement().text().toInt();
308       int repeat = docElem.namedItem("repeat").toElement().text().toInt();
309       QString state = docElem.namedItem("state").toElement().text();
310       QDomNode infoNode =  docElem.namedItem("information");
311       QDomNode metaInfoNode =  infoNode.namedItem("meta-information");
312       QString title = metaInfoNode.namedItem("title").toElement().text().replace("\\\\", "\\");
313       // if it's a file style title fix it up
314       if (40 < title.length()) {
315           if (0 < title.lastIndexOf("\\")) {
316               title = title.right(title.length() - (title.lastIndexOf("\\") + 1));
317           }
318           else if (0 < title.lastIndexOf("/")) {
319               title = title.right(title.length() - (title.lastIndexOf("/") + 1));
320           }
321       }
322       QString artist = metaInfoNode.namedItem("artist").toElement().text();
323       QString album = metaInfoNode.namedItem("album").toElement().text();
324       QString now_playing = metaInfoNode.namedItem("now_playing").toElement().text();
325       QString art_url = metaInfoNode.namedItem("art_url").toElement().text();
326       // Populate the current status structure
327       // now would be a good time to work out if we are a new track / file or not.
328       // key if we are going to look for album art later
329       // for now we check length and title this will require further examination later
330       mCurrentStatus.newtrack = true;
331       if (mCurrentStatus.length == length && !mCurrentStatus.title.isNull() && 0 == QString::compare(mCurrentStatus.title, title)) {
332         mCurrentStatus.newtrack = false;
333       }
334       mCurrentStatus.volume = volume;
335       mCurrentStatus.length = length;
336       mCurrentStatus.time = time;
337       mCurrentStatus.position = position;
338       mCurrentStatus.random = (1 == random);
339       mCurrentStatus.loop = (1 == loop);
340       mCurrentStatus.repeat = (1 == repeat);
341       mCurrentStatus.title = title;
342       mCurrentStatus.artist = artist;
343       mCurrentStatus.album = album;
344       mCurrentStatus.nowplaying = now_playing;
345       mCurrentStatus.hasart = (!art_url.isNull() && !art_url.isEmpty());
346       if (!state.isNull() && !state.isEmpty()) {
347           if (0 == QString::compare("playing", state, Qt::CaseInsensitive)) {
348             mCurrentStatus.state = PLAYING;
349           }
350           else if (0 == QString::compare("paused", state, Qt::CaseInsensitive)) {
351             mCurrentStatus.state = PAUSED;
352           }
353           else if (0 == QString::compare("stop", state, Qt::CaseInsensitive)) {
354             mCurrentStatus.state = STOP;
355           }
356           else {
357             mCurrentStatus.state = UNKNOWN;
358           }
359       }
360       else {
361           mCurrentStatus.state = UNKNOWN;
362       }
363       // What's our mute status?
364       if (0 < mCurrentStatus.volume) {
365           this->mVolume = mCurrentStatus.volume;
366           this->mMuted = false;
367       }
368       else {
369           this->mMuted = true;
370       }
371       // Update the UI
372       updateUiWithCurrentStatus();
373
374   }
375
376   void PlayerMainWindow::updateUiWithCurrentStatus() {
377       // position
378       QTime timePosition(0,0,0) ;
379       timePosition =  timePosition.addSecs(mCurrentStatus.time);
380
381       ui->timeLabel->setText(timePosition.toString("h:mm:ss"));
382
383       // duration
384       if (0 < mCurrentStatus.length) {
385           QTime timeDuration(0,0,0) ;
386           timeDuration =  timeDuration.addSecs(mCurrentStatus.length);
387
388           ui->durationLabel->setText(timeDuration.toString("h:mm:ss"));
389       }
390       else {
391           ui->durationLabel->setText("0:00:00");
392       }
393
394
395       if (mCurrentStatus.position >= 0 && mCurrentStatus.position <= 100)
396           ui->slider->setValue(mCurrentStatus.position);
397
398       ui->labelTitle->setText(mCurrentStatus.title);
399       ui->labelArtist->setText(mCurrentStatus.artist);
400       ui->labelAlbum->setText(mCurrentStatus.album);
401
402       if (PLAYING == mCurrentStatus.state) {
403           ui->playpauseButton->setIcon(QIcon::fromTheme("camera_video_pause"));
404       }
405       else {
406           ui->playpauseButton->setIcon(QIcon::fromTheme("camera_playback"));
407       }
408
409       if (mCurrentStatus.newtrack) {
410           // potential actions:
411           //   rebuild display layout
412           //   retrieve album art
413           mHasImage = false;
414           mPlayListMainWindow->requestPlayList();
415       }
416       // Update the buttons on the playlist window
417       if (NULL != this->mPlayListMainWindow) {
418         this->mPlayListMainWindow->updateUiWithCurrentStatus(& mCurrentStatus);
419       }
420
421   }
422   void PlayerMainWindow::playlistIdUpdated(int id, bool hasart, QString extension) {
423       if (hasart) {
424           getCoverArt(id);
425       }
426       else {
427           ui->labelArtLandscape->setVisible(false);
428           ui->labelArtPortrait->setVisible(false);
429           // could use a default graphic here!
430           // setCoverArtFromPixmap();
431       }
432   }
433   void PlayerMainWindow::readReady() {
434     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
435     // append to buffer
436     mResponse += reply->readAll();
437   }
438   void PlayerMainWindow::finished(QNetworkReply * reply) {
439     // now we can call setCoverArt to process the full buffers
440     this->setCoverArt(mResponse);
441     // only interested in finished signals
442     disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
443   }
444   void PlayerMainWindow::getCoverArt(int id) {
445     mResponse.clear();
446     QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/art?id=" + QString::number(id))));
447     connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
448     connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
449
450   }
451   void PlayerMainWindow::setCoverArt(const QByteArray data) {
452     QPixmap* image = new QPixmap();
453     if (image->loadFromData(data)) {
454         mHasImage = true;
455         ui->labelArtLandscape->setPixmap(image->scaledToHeight(120, Qt::SmoothTransformation));
456         ui->labelArtPortrait->setPixmap(image->scaledToHeight(310, Qt::SmoothTransformation));
457         if (mIsLandscape) {
458             ui->labelArtPortrait->setVisible(false);
459             ui->labelArtLandscape->setVisible(true);
460         }
461         else {
462             ui->labelArtLandscape->setVisible(false);
463             ui->labelArtPortrait->setVisible(true);
464         }
465     }
466     else {
467         ui->labelArtPortrait->setVisible(false);
468         ui->labelArtLandscape->setVisible(false);
469     }
470   }
471   void PlayerMainWindow::setCoverArtFromPixmap(QPixmap image) {
472     mHasImage = true;
473     ui->labelArtLandscape->setPixmap(image.scaledToHeight(120, Qt::SmoothTransformation));
474     ui->labelArtPortrait->setPixmap(image.scaledToHeight(310, Qt::SmoothTransformation));
475     if (mIsLandscape) {
476         ui->labelArtPortrait->setVisible(false);
477         ui->labelArtLandscape->setVisible(true);
478     }
479     else {
480         ui->labelArtLandscape->setVisible(false);
481         ui->labelArtPortrait->setVisible(true);
482     }
483   }
484