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
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
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.
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"
28 PlayerMainWindow::PlayerMainWindow(QWidget *parent) :
30 ui(new Ui::PlayerMainWindow)
33 setWindowTitle("Vlc remote");
37 mTimer = new QTimer(this);
38 mNetManager = new QNetworkAccessManager(this);
39 mPlayListMainWindow = new PlayListMainWindow;
40 mBrowserMainWindow = new BrowseMainWindow;
47 ui->playlistButton->setIcon(QIcon::fromTheme("notes_bullets"));
48 ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
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"));
60 ui->labelArtPortrait->setVisible(false);
61 ui->labelArtLandscape->setVisible(false);
63 ui->labelTitle->setTextFormat(Qt::RichText);
64 ui->labelArtist->setTextFormat(Qt::RichText);
65 ui->labelAlbum->setTextFormat(Qt::RichText);
68 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
70 mPlayListMainWindow->setParent(this);
71 mPlayListMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
72 setAttribute(Qt::WA_Maemo5StackedWindow);
73 mPlayListMainWindow->setWindowFlags(mPlayListMainWindow->windowFlags() | Qt::Window);
75 mBrowserMainWindow->setParent(this);
76 mBrowserMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
77 setAttribute(Qt::WA_Maemo5StackedWindow);
78 mBrowserMainWindow->setWindowFlags(mBrowserMainWindow->windowFlags() | Qt::Window);
80 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
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()));
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)));
105 connect(mPlayListMainWindow, SIGNAL(idUpdated(int,bool,QString)), this, SLOT(playlistIdUpdated(int, bool, QString)));
108 // check if last used connection is still valid or showConfig
110 QString last_ip = AccountDialog::currentIp();
111 if (!last_ip.isNull() && !last_ip.isEmpty()) {
112 QTcpSocket * socket = new QTcpSocket;
113 if(last_ip.contains(":"))
115 QStringList hostSplit = last_ip.split(":");
116 QString ip = hostSplit.at(0);
117 QString port = hostSplit.at(1);
118 socket->connectToHost(ip,port.toInt());
121 socket->connectToHost(last_ip,8080);
123 if (!socket->waitForConnected(1000)) {
129 mPlayListMainWindow->init();
130 mBrowserMainWindow->init();
144 PlayerMainWindow::~PlayerMainWindow()
149 void PlayerMainWindow::changeEvent(QEvent *e)
151 QMainWindow::changeEvent(e);
153 case QEvent::LanguageChange:
154 ui->retranslateUi(this);
161 void PlayerMainWindow::setPortrait()
163 this->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
166 void PlayerMainWindow::setLandscape()
168 this->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
171 void PlayerMainWindow::setAutoRotate()
173 this->setAttribute(Qt::WA_Maemo5AutoOrientation, true);
176 void PlayerMainWindow::orientationChanged() {
177 QRect screenGeometry = QApplication::desktop()->screenGeometry();
178 mIsLandscape = (screenGeometry.width() > screenGeometry.height());
181 ui->labelArtPortrait->setVisible(false);
182 ui->labelArtLandscape->setVisible(true);
185 ui->labelArtLandscape->setVisible(false);
186 ui->labelArtPortrait->setVisible(true);
190 ui->labelArtLandscape->setVisible(false);
191 ui->labelArtPortrait->setVisible(false);
195 void PlayerMainWindow::playpause()
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;
203 updateUiWithCurrentStatus();
205 else if (PLAYING == mCurrentStatus.state) {
206 mCurrentStatus.state = PAUSED;
208 updateUiWithCurrentStatus();
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!
217 void PlayerMainWindow::play()
219 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play")));
221 void PlayerMainWindow::stop()
223 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_stop")));
225 void PlayerMainWindow::pause()
227 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_pause")));
229 void PlayerMainWindow::previous()
231 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_previous")));
233 void PlayerMainWindow::next()
235 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_next")));
237 void PlayerMainWindow::fullscreen()
239 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=fullscreen")));
241 void PlayerMainWindow::volUp()
243 QUrl url = QUrl("http://"+mIp+"/requests/status.xml?command=volume");
244 url.addEncodedQueryItem(QByteArray("val"), QByteArray("%2B20"));
245 mNetManager->get(QNetworkRequest(url));
247 void PlayerMainWindow::volDown()
249 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=-20")));
251 void PlayerMainWindow::volMute()
253 this->mMuted = !this->mMuted;
255 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=0")));
258 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val="+QString::number(this->mVolume))));
261 void PlayerMainWindow::slide(int value)
263 mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=seek&val="+QString::number(value)+"%25")));
266 void PlayerMainWindow::showConfig()
269 AccountDialog * dialog = new AccountDialog;
272 mIp= AccountDialog::currentIp();
274 mPlayListMainWindow->init();
275 mBrowserMainWindow->init();
279 void PlayerMainWindow::showAbout()
282 AboutDialog * dialog = new AboutDialog;
287 void PlayerMainWindow::askStatus()
290 QNetworkReply * reply = mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml")));
291 connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlStatus()));
294 void PlayerMainWindow::parseXmlStatus()
296 QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
298 doc.setContent(reply->readAll());
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));
318 else if (0 < title.lastIndexOf("/")) {
319 title = title.right(title.length() - (title.lastIndexOf("/") + 1));
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;
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;
350 else if (0 == QString::compare("paused", state, Qt::CaseInsensitive)) {
351 mCurrentStatus.state = PAUSED;
353 else if (0 == QString::compare("stop", state, Qt::CaseInsensitive)) {
354 mCurrentStatus.state = STOP;
357 mCurrentStatus.state = UNKNOWN;
361 mCurrentStatus.state = UNKNOWN;
363 // What's our mute status?
364 if (0 < mCurrentStatus.volume) {
365 this->mVolume = mCurrentStatus.volume;
366 this->mMuted = false;
372 updateUiWithCurrentStatus();
376 void PlayerMainWindow::updateUiWithCurrentStatus() {
378 QTime timePosition(0,0,0) ;
379 timePosition = timePosition.addSecs(mCurrentStatus.time);
381 ui->timeLabel->setText(timePosition.toString("h:mm:ss"));
384 if (0 < mCurrentStatus.length) {
385 QTime timeDuration(0,0,0) ;
386 timeDuration = timeDuration.addSecs(mCurrentStatus.length);
388 ui->durationLabel->setText(timeDuration.toString("h:mm:ss"));
391 ui->durationLabel->setText("0:00:00");
395 if (mCurrentStatus.position >= 0 && mCurrentStatus.position <= 100)
396 ui->slider->setValue(mCurrentStatus.position);
398 ui->labelTitle->setText(mCurrentStatus.title);
399 ui->labelArtist->setText(mCurrentStatus.artist);
400 ui->labelAlbum->setText(mCurrentStatus.album);
402 if (PLAYING == mCurrentStatus.state) {
403 ui->playpauseButton->setIcon(QIcon::fromTheme("camera_video_pause"));
406 ui->playpauseButton->setIcon(QIcon::fromTheme("camera_playback"));
409 if (mCurrentStatus.newtrack) {
410 // potential actions:
411 // rebuild display layout
412 // retrieve album art
414 mPlayListMainWindow->requestPlayList();
416 // Update the buttons on the playlist window
417 if (NULL != this->mPlayListMainWindow) {
418 this->mPlayListMainWindow->updateUiWithCurrentStatus(& mCurrentStatus);
422 void PlayerMainWindow::playlistIdUpdated(int id, bool hasart, QString extension) {
427 ui->labelArtLandscape->setVisible(false);
428 ui->labelArtPortrait->setVisible(false);
429 // could use a default graphic here!
430 // setCoverArtFromPixmap();
433 void PlayerMainWindow::readReady() {
434 QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
436 mResponse += reply->readAll();
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 *)));
444 void PlayerMainWindow::getCoverArt(int id) {
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 *)));
451 void PlayerMainWindow::setCoverArt(const QByteArray data) {
452 QPixmap* image = new QPixmap();
453 if (image->loadFromData(data)) {
455 ui->labelArtLandscape->setPixmap(image->scaledToHeight(120, Qt::SmoothTransformation));
456 ui->labelArtPortrait->setPixmap(image->scaledToHeight(310, Qt::SmoothTransformation));
458 ui->labelArtPortrait->setVisible(false);
459 ui->labelArtLandscape->setVisible(true);
462 ui->labelArtLandscape->setVisible(false);
463 ui->labelArtPortrait->setVisible(true);
467 ui->labelArtPortrait->setVisible(false);
468 ui->labelArtLandscape->setVisible(false);
471 void PlayerMainWindow::setCoverArtFromPixmap(QPixmap image) {
473 ui->labelArtLandscape->setPixmap(image.scaledToHeight(120, Qt::SmoothTransformation));
474 ui->labelArtPortrait->setPixmap(image.scaledToHeight(310, Qt::SmoothTransformation));
476 ui->labelArtPortrait->setVisible(false);
477 ui->labelArtLandscape->setVisible(true);
480 ui->labelArtLandscape->setVisible(false);
481 ui->labelArtPortrait->setVisible(true);