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.
18 #include "playlistmainwindow.h"
19 #include "ui_playlistmainwindow.h"
20 #include <QPushButton>
22 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
23 #include <QMaemo5InformationBox>
25 #include "configdialog.h"
26 #include "aboutdialog.h"
27 #include "appsettings.h"
28 #include "vlcstatus.h"
30 PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
32 ui(new Ui::PlayListMainWindow)
36 mTimer = new QTimer(this);
37 setWindowTitle("Vlc remote");
43 mNetManager = new QNetworkAccessManager(this);
45 mContents = new QList<VlcPlayListElementSimple>();
47 ui->listWidget->setTextElideMode(Qt::ElideLeft);
48 ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50 ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
51 ui->clearButton->setIcon(QIcon::fromTheme("general_delete"));
52 ui->shuffleButton->setIcon(QIcon::fromTheme("mediaplayer_default_shuffle"));
53 ui->loopButton->setIcon(QIcon::fromTheme("general_refresh"));
54 ui->repeatButton->setIcon(QIcon::fromTheme("general_redo"));
55 ui->removeButton->setIcon(QIcon::fromTheme("general_close"));
57 ui->clearButton->setDisabled(false);
58 ui->shuffleButton->setDisabled(false);
59 ui->loopButton->setDisabled(false);
60 ui->repeatButton->setDisabled(false);
61 ui->removeButton->setDisabled(true);
62 ui->playButton->setDisabled(true);
64 connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
65 connect(ui->removeButton,SIGNAL(clicked()),this,SLOT(onRemove()));
66 connect(ui->repeatButton,SIGNAL(clicked()),this,SLOT(onRepeat()));
67 connect(ui->loopButton,SIGNAL(clicked()),this,SLOT(onLoop()));
68 connect(ui->shuffleButton,SIGNAL(clicked()),this,SLOT(onShuffle()));
69 connect(ui->clearButton,SIGNAL(clicked()),this,SLOT(onClear()));
70 connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
75 void PlayListMainWindow::init() // CALL WHEN CONFIG CHANGES
77 mIp = AppSettings::getCurrentIp(); // AccountDialog::currentIp();
79 void PlayListMainWindow::showPlayList() // CALL WHEN SHOWN
84 PlayListMainWindow::~PlayListMainWindow()
89 void PlayListMainWindow::changeEvent(QEvent *e)
91 QMainWindow::changeEvent(e);
93 case QEvent::LanguageChange:
94 ui->retranslateUi(this);
101 void PlayListMainWindow::onListSelectionChanged() {
102 QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
103 if (0 < items.count()) {
104 mCurrentElement = getElementFromText(items.at(0)->text());
105 mCurrentVlcIndex = items.at(0)->type() - LIST_ITEM_TYPE_OFFSET; // Qt reserves types up to 1000, we use an offset beyond that for index tracking. May prove to be too hacky!
106 ui->removeButton->setDisabled(false);
107 ui->playButton->setDisabled(false);
110 mCurrentVlcIndex = 0;
111 ui->removeButton->setDisabled(true);
112 ui->playButton->setDisabled(true);
116 void PlayListMainWindow::onRemove() {
117 if (0 < this->mCurrentVlcIndex) {
118 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_delete&id=" + QString::number(this->mCurrentVlcIndex))));
119 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
122 void PlayListMainWindow::onPlay() {
123 if (0 < this->mCurrentVlcIndex) {
124 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play&id=" + QString::number(this->mCurrentVlcIndex))));
127 void PlayListMainWindow::onRepeat() {
128 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_repeat")));
130 void PlayListMainWindow::onLoop() {
131 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_loop")));
133 void PlayListMainWindow::onShuffle() {
134 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_random")));
136 void PlayListMainWindow::onClear() {
137 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_empty")));
138 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
140 void PlayListMainWindow::requestPlayList() {
142 ui->removeButton->setDisabled(true);
143 ui->playButton->setDisabled(true);
144 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
145 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
147 QNetworkReply * reply = mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/playlist.xml")));
148 disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
149 connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
150 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
151 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
153 void PlayListMainWindow::error(QNetworkReply::NetworkError code) {
154 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
155 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
158 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
159 QMaemo5InformationBox::information(this, tr("Playlist could not be retrieved."), QMaemo5InformationBox::DefaultTimeout);
162 void PlayListMainWindow::readReady() {
163 QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
165 mResponse += reply->readAll();
167 void PlayListMainWindow::finished(QNetworkReply * reply) {
168 // now we can call parseXmlList to process the full buffers
169 this->parseXmlPlayList();
170 // only interested in finished signals
171 disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
172 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
173 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
177 void PlayListMainWindow::parseXmlPlayList() {
180 doc.setContent(this->mResponse);
181 QDomElement docElem = doc.documentElement();
182 QDomNodeList nodes = docElem.elementsByTagName("node");
185 int currentLeafId = 0;
187 QString extension = "";
189 int ct = nodes.count();
190 for (int idx = 0; idx < ct; ++idx) {
191 QDomNode node = nodes.at(idx);
192 QString current = "";
193 //QString name = node.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
194 int id = node.attributes().namedItem("id").nodeValue().toInt();
195 if (4 > id && 0 == QString::compare(node.attributes().namedItem("ro").nodeValue(), "ro")) {
196 // got the main playlist, let's build it up
197 if (node.hasChildNodes()) {
198 QDomNodeList leafs = node.childNodes();
199 int leafct = leafs.count();
202 for (int jdx = 0; jdx < leafct; ++jdx) {
203 QDomNode leaf = leafs.at(jdx);
204 parsePlayListItem(&leaf, &extension, &hasArt, ¤tLeafId, 1);
206 VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
207 el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
208 el->path = leaf.attributes().namedItem("uri").nodeValue();
209 el->name = leaf.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
210 current = leaf.attributes().namedItem("current").nodeValue();
211 el->playing = (0 < current.length());
213 if (0 == QString::compare(leaf.nodeName(), "node")) {
215 el->extension = getExtension(el->path, NULL);
216 this->mContents->append(*el);
217 // now parse the child nodes as leafs.
218 if (leaf.hasChildNodes()) {
219 QDomNodeList items = leaf.childNodes();
220 int itemct = items.count();
223 for (int kdx = 0; kdx < itemct; ++kdx) {
224 QDomNode item = items.at(kdx);
225 VlcPlayListElementSimple* it = new VlcPlayListElementSimple();
226 it->id = item.attributes().namedItem("id").nodeValue().toInt();
227 it->path = item.attributes().namedItem("uri").nodeValue();
228 it->name = item.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
229 it->extension = getExtension(it->path, NULL);
232 current = item.attributes().namedItem("current").nodeValue();
233 it->playing = (0 < current.length());
235 currentLeafId = it->id;
236 QString art = item.toElement().namedItem("art_url").toElement().text();
237 hasArt = (!art.isNull() && !art.isEmpty());
238 extension = getExtension(it->path, NULL);
240 this->mContents->append(*it);
248 el->extension = getExtension(el->path, NULL);
250 currentLeafId = el->id;
251 QString art = leaf.toElement().namedItem("art_url").toElement().text();
252 hasArt = (!art.isNull() && !art.isEmpty());
253 extension = getExtension(el->path, NULL);
255 this->mContents->append(*el);
268 emit this->idUpdated(currentLeafId, hasArt, extension);
275 void PlayListMainWindow::parsePlayListItem(QDomNode *node, QString *extension, bool *hasArt, int *currentLeafId, int depth) {
278 VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
279 el->id = node->attributes().namedItem("id").nodeValue().toInt();
280 el->path = node->attributes().namedItem("uri").nodeValue();
281 el->name = node->attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
282 current = node->attributes().namedItem("current").nodeValue();
283 el->playing = (0 < current.length());
285 if (0 != QString::compare(node->nodeName(), "node")) {
287 el->extension = getExtension(el->path, NULL);
289 *currentLeafId = el->id;
290 QString art = node->toElement().namedItem("art_url").toElement().text();
291 *hasArt = (!art.isNull() && !art.isEmpty());
292 *extension = getExtension(el->path, NULL);
294 this->mContents->append(*el);
299 el->extension = getExtension(el->path, NULL);
300 // empty nodes appear in the playlist when they can't be played!
301 if (node->hasChildNodes()) {
302 this->mContents->append(*el);
305 // now parse the child nodes as leaf.
306 if (node->hasChildNodes()) {
307 QDomNodeList items = node->childNodes();
308 int itemct = items.count();
311 for (int kdx = 0; kdx < itemct; ++kdx) {
312 QDomNode item = items.at(kdx);
313 parsePlayListItem(&item, extension, hasArt, currentLeafId, depth);
321 QString PlayListMainWindow::getExtension(QString path, QString extension) {
322 // return extension if exists
323 if (!extension.isNull() && !extension.isEmpty()) return extension;
324 // return blank if no path
325 if (path.isNull() || path.isEmpty()) return "";
326 // otherwise extract the extension
327 int dot_pos = path.lastIndexOf('.');
329 return path.right(path.length() - (dot_pos + 1));
336 VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
337 //if (0 != QString::compare("", text)) {
338 for (int idx = 0; idx < mContents->count(); ++idx) {
339 if (0 == QString::compare(text, mContents->at(idx).name)) {
340 return mContents->at(idx);
344 return *(new VlcPlayListElementSimple());
347 void PlayListMainWindow::updateList() {
348 ui->listWidget->clear();
349 int ct = this->mContents->count();
351 QIcon icon_audio = QIcon::fromTheme("general_audio_file");
352 QIcon icon_video = QIcon::fromTheme("general_video_file");
353 QIcon icon_image = QIcon::fromTheme("general_image");
354 QIcon icon_flash = QIcon::fromTheme("filemanager_flash_file");
355 QIcon icon_media = QIcon::fromTheme("filemanager_media_folder");
356 QIcon icon_real = QIcon::fromTheme("filemanager_real_music");
357 QIcon icon_unknown= QIcon::fromTheme("filemanager_unknown_file");
358 for (int idx = 0; idx < ct; ++idx) {
359 VlcPlayListElementSimple el = mContents->at(idx);
360 QListWidgetItem* item;
361 if (0 == QString::compare("node", el.type)) {
362 item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
365 if ( 0 == QString::compare(el.extension, "jpg") ||
366 0 == QString::compare(el.extension, "jpeg") ||
367 0 == QString::compare(el.extension, "gif") ||
368 0 == QString::compare(el.extension, "png") ||
369 0 == QString::compare(el.extension, "bmp") ) {
370 item = new QListWidgetItem(icon_image, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .jpg, .jpeg, .gif, .png, .bmp
372 else if ( 0 == QString::compare(el.extension, "mp3") ||
373 0 == QString::compare(el.extension, "m4a") ||
374 0 == QString::compare(el.extension, "ogg") ||
375 0 == QString::compare(el.extension, "oga") ||
376 0 == QString::compare(el.extension, "wav") ||
377 0 == QString::compare(el.extension, "flac") ) {
378 item = new QListWidgetItem(icon_audio, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .mp3, .m4a, .ogg, .oga, .wav, .flac
380 else if ( 0 == QString::compare(el.extension, "avi") ||
381 0 == QString::compare(el.extension, "mpeg") ||
382 0 == QString::compare(el.extension, "mpg") ||
383 0 == QString::compare(el.extension, "mov") ||
384 0 == QString::compare(el.extension, "mp4") ||
385 0 == QString::compare(el.extension, "m4v") ||
386 0 == QString::compare(el.extension, "wmv") ||
387 0 == QString::compare(el.extension, "mkv") ||
388 0 == QString::compare(el.extension, "ogv") ) {
389 item = new QListWidgetItem(icon_video, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .avi, .mpg, .mpeg, .mov, .m4v, .mp4, .wmv, .mkv, .ogv
391 else if ( 0 == QString::compare(el.extension, "rm") ||
392 0 == QString::compare(el.extension, "ra") ||
393 0 == QString::compare(el.extension, "ram") ) {
394 item = new QListWidgetItem(icon_real, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .ram, 'rm, 'ra
396 else if ( 0 == QString::compare(el.extension, "flv") ) {
397 item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .flv
399 //else if ( 0 == QString::compare(el.extension, "") ) {
400 // item = new QListWidgetItem(icon_unknown, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .flv
403 if (el.name.contains("Flash")) {
404 item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
407 item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
411 item->setSelected(el.playing);
412 ui->listWidget->addItem(item);
414 ui->listWidget->scrollToItem(item, QAbstractItemView::PositionAtCenter);
419 void PlayListMainWindow::updateUiWithCurrentStatus(VlcStatus * status) {
420 ui->loopButton->setChecked(status->loop);
421 ui->repeatButton->setChecked(status->repeat);
422 ui->shuffleButton->setChecked(status->random);