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 #include "configdialog.h"
23 #include "aboutdialog.h"
24 #include "appsettings.h"
25 #include "vlcstatus.h"
27 PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
29 ui(new Ui::PlayListMainWindow)
33 mTimer = new QTimer(this);
34 setWindowTitle("Vlc remote");
40 mNetManager = new QNetworkAccessManager(this);
42 mContents = new QList<VlcPlayListElementSimple>();
44 ui->listWidget->setTextElideMode(Qt::ElideLeft);
45 ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
48 ui->clearButton->setIcon(QIcon::fromTheme("general_delete"));
49 ui->shuffleButton->setIcon(QIcon::fromTheme("mediaplayer_default_shuffle"));
50 ui->loopButton->setIcon(QIcon::fromTheme("general_refresh"));
51 ui->repeatButton->setIcon(QIcon::fromTheme("general_redo"));
52 ui->removeButton->setIcon(QIcon::fromTheme("general_close"));
54 ui->clearButton->setDisabled(false);
55 ui->shuffleButton->setDisabled(false);
56 ui->loopButton->setDisabled(false);
57 ui->repeatButton->setDisabled(false);
58 ui->removeButton->setDisabled(true);
59 ui->playButton->setDisabled(true);
61 connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
62 connect(ui->removeButton,SIGNAL(clicked()),this,SLOT(onRemove()));
63 connect(ui->repeatButton,SIGNAL(clicked()),this,SLOT(onRepeat()));
64 connect(ui->loopButton,SIGNAL(clicked()),this,SLOT(onLoop()));
65 connect(ui->shuffleButton,SIGNAL(clicked()),this,SLOT(onShuffle()));
66 connect(ui->clearButton,SIGNAL(clicked()),this,SLOT(onClear()));
67 connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
72 void PlayListMainWindow::init() // CALL WHEN CONFIG CHANGES
74 mIp = AppSettings::getCurrentIp(); // AccountDialog::currentIp();
76 void PlayListMainWindow::showPlayList() // CALL WHEN SHOWN
81 PlayListMainWindow::~PlayListMainWindow()
86 void PlayListMainWindow::changeEvent(QEvent *e)
88 QMainWindow::changeEvent(e);
90 case QEvent::LanguageChange:
91 ui->retranslateUi(this);
98 void PlayListMainWindow::onListSelectionChanged() {
99 QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
100 if (0 < items.count()) {
101 mCurrentElement = getElementFromText(items.at(0)->text());
102 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!
103 ui->removeButton->setDisabled(false);
104 ui->playButton->setDisabled(false);
107 mCurrentVlcIndex = 0;
108 ui->removeButton->setDisabled(true);
109 ui->playButton->setDisabled(true);
113 void PlayListMainWindow::onRemove() {
114 if (0 < this->mCurrentVlcIndex) {
115 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_delete&id=" + QString::number(this->mCurrentVlcIndex))));
116 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
119 void PlayListMainWindow::onPlay() {
120 if (0 < this->mCurrentVlcIndex) {
121 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play&id=" + QString::number(this->mCurrentVlcIndex))));
124 void PlayListMainWindow::onRepeat() {
125 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_repeat")));
127 void PlayListMainWindow::onLoop() {
128 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_loop")));
130 void PlayListMainWindow::onShuffle() {
131 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_random")));
133 void PlayListMainWindow::onClear() {
134 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_empty")));
135 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
137 void PlayListMainWindow::requestPlayList() {
139 ui->listWidget->clear();
141 ui->removeButton->setDisabled(true);
142 ui->playButton->setDisabled(true);
143 QNetworkReply * reply = mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/playlist.xml")));
144 disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
145 connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
146 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
148 void PlayListMainWindow::readReady() {
149 QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
151 mResponse += reply->readAll();
153 void PlayListMainWindow::finished(QNetworkReply * reply) {
154 // now we can call parseXmlList to process the full buffers
155 this->parseXmlPlayList();
156 // only interested in finished signals
157 disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
160 void PlayListMainWindow::parseXmlPlayList() {
162 doc.setContent(this->mResponse);
163 QDomElement docElem = doc.documentElement();
164 QDomNodeList nodes = docElem.elementsByTagName("node");
167 int currentLeafId = 0;
169 QString extension = "";
171 int ct = nodes.count();
172 for (int idx = 0; idx < ct; ++idx) {
173 QDomNode node = nodes.at(idx);
174 QString current = "";
175 //QString name = node.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
176 int id = node.attributes().namedItem("id").nodeValue().toInt();
177 if (4 > id && 0 == QString::compare(node.attributes().namedItem("ro").nodeValue(), "ro")) {
178 // got the main playlist, let's build it up
179 if (node.hasChildNodes()) {
180 QDomNodeList leafs = node.childNodes();
181 int leafct = leafs.count();
184 for (int jdx = 0; jdx < leafct; ++jdx) {
185 QDomNode leaf = leafs.at(jdx);
186 parsePlayListItem(&leaf, &extension, &hasArt, ¤tLeafId, 1);
188 VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
189 el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
190 el->path = leaf.attributes().namedItem("uri").nodeValue();
191 el->name = leaf.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
192 current = leaf.attributes().namedItem("current").nodeValue();
193 el->playing = (0 < current.length());
195 if (0 == QString::compare(leaf.nodeName(), "node")) {
197 el->extension = getExtension(el->path, NULL);
198 this->mContents->append(*el);
199 // now parse the child nodes as leafs.
200 if (leaf.hasChildNodes()) {
201 QDomNodeList items = leaf.childNodes();
202 int itemct = items.count();
205 for (int kdx = 0; kdx < itemct; ++kdx) {
206 QDomNode item = items.at(kdx);
207 VlcPlayListElementSimple* it = new VlcPlayListElementSimple();
208 it->id = item.attributes().namedItem("id").nodeValue().toInt();
209 it->path = item.attributes().namedItem("uri").nodeValue();
210 it->name = item.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
211 it->extension = getExtension(it->path, NULL);
214 current = item.attributes().namedItem("current").nodeValue();
215 it->playing = (0 < current.length());
217 currentLeafId = it->id;
218 QString art = item.toElement().namedItem("art_url").toElement().text();
219 hasArt = (!art.isNull() && !art.isEmpty());
220 extension = getExtension(it->path, NULL);
222 this->mContents->append(*it);
230 el->extension = getExtension(el->path, NULL);
232 currentLeafId = el->id;
233 QString art = leaf.toElement().namedItem("art_url").toElement().text();
234 hasArt = (!art.isNull() && !art.isEmpty());
235 extension = getExtension(el->path, NULL);
237 this->mContents->append(*el);
250 emit this->idUpdated(currentLeafId, hasArt, extension);
257 void PlayListMainWindow::parsePlayListItem(QDomNode *node, QString *extension, bool *hasArt, int *currentLeafId, int depth) {
260 VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
261 el->id = node->attributes().namedItem("id").nodeValue().toInt();
262 el->path = node->attributes().namedItem("uri").nodeValue();
263 el->name = node->attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
264 current = node->attributes().namedItem("current").nodeValue();
265 el->playing = (0 < current.length());
267 if (0 != QString::compare(node->nodeName(), "node")) {
269 el->extension = getExtension(el->path, NULL);
271 *currentLeafId = el->id;
272 QString art = node->toElement().namedItem("art_url").toElement().text();
273 *hasArt = (!art.isNull() && !art.isEmpty());
274 *extension = getExtension(el->path, NULL);
276 this->mContents->append(*el);
281 el->extension = getExtension(el->path, NULL);
282 this->mContents->append(*el);
284 // now parse the child nodes as leaf.
285 if (node->hasChildNodes()) {
286 QDomNodeList items = node->childNodes();
287 int itemct = items.count();
290 for (int kdx = 0; kdx < itemct; ++kdx) {
291 QDomNode item = items.at(kdx);
292 parsePlayListItem(&item, extension, hasArt, currentLeafId, depth);
300 QString PlayListMainWindow::getExtension(QString path, QString extension) {
301 // return extension if exists
302 if (!extension.isNull() && !extension.isEmpty()) return extension;
303 // return blank if no path
304 if (path.isNull() || path.isEmpty()) return "";
305 // otherwise extract the extension
306 int dot_pos = path.lastIndexOf('.');
308 return path.right(path.length() - (dot_pos + 1));
315 VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
316 //if (0 != QString::compare("", text)) {
317 for (int idx = 0; idx < mContents->count(); ++idx) {
318 if (0 == QString::compare(text, mContents->at(idx).name)) {
319 return mContents->at(idx);
323 return *(new VlcPlayListElementSimple());
326 void PlayListMainWindow::updateList() {
327 int ct = this->mContents->count();
329 QIcon icon_audio = QIcon::fromTheme("general_audio_file");
330 QIcon icon_video = QIcon::fromTheme("general_video_file");
331 QIcon icon_image = QIcon::fromTheme("general_image");
332 QIcon icon_flash = QIcon::fromTheme("filemanager_flash_file");
333 QIcon icon_media = QIcon::fromTheme("filemanager_media_folder");
334 QIcon icon_real = QIcon::fromTheme("filemanager_real_music");
335 QIcon icon_unknown= QIcon::fromTheme("filemanager_unknown_file");
336 for (int idx = 0; idx < ct; ++idx) {
337 VlcPlayListElementSimple el = mContents->at(idx);
338 QListWidgetItem* item;
339 if (0 == QString::compare("node", el.type)) {
340 item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
343 if ( 0 == QString::compare(el.extension, "jpg") ||
344 0 == QString::compare(el.extension, "jpeg") ||
345 0 == QString::compare(el.extension, "gif") ||
346 0 == QString::compare(el.extension, "png") ||
347 0 == QString::compare(el.extension, "bmp") ) {
348 item = new QListWidgetItem(icon_image, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .jpg, .jpeg, .gif, .png, .bmp
350 else if ( 0 == QString::compare(el.extension, "mp3") ||
351 0 == QString::compare(el.extension, "m4a") ||
352 0 == QString::compare(el.extension, "ogg") ||
353 0 == QString::compare(el.extension, "oga") ||
354 0 == QString::compare(el.extension, "wav") ||
355 0 == QString::compare(el.extension, "flac") ) {
356 item = new QListWidgetItem(icon_audio, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .mp3, .m4a, .ogg, .oga, .wav, .flac
358 else if ( 0 == QString::compare(el.extension, "avi") ||
359 0 == QString::compare(el.extension, "mpeg") ||
360 0 == QString::compare(el.extension, "mpg") ||
361 0 == QString::compare(el.extension, "mov") ||
362 0 == QString::compare(el.extension, "mp4") ||
363 0 == QString::compare(el.extension, "m4v") ||
364 0 == QString::compare(el.extension, "wmv") ||
365 0 == QString::compare(el.extension, "mkv") ||
366 0 == QString::compare(el.extension, "ogv") ) {
367 item = new QListWidgetItem(icon_video, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .avi, .mpg, .mpeg, .mov, .m4v, .mp4, .wmv, .mkv, .ogv
369 else if ( 0 == QString::compare(el.extension, "rm") ||
370 0 == QString::compare(el.extension, "ra") ||
371 0 == QString::compare(el.extension, "ram") ) {
372 item = new QListWidgetItem(icon_real, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .ram, 'rm, 'ra
374 else if ( 0 == QString::compare(el.extension, "flv") ) {
375 item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .flv
377 else if ( 0 == QString::compare(el.extension, "") ) {
378 item = new QListWidgetItem(icon_unknown, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .flv
381 if (el.name.contains("Flash")) {
382 item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
385 item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
389 item->setSelected(el.playing);
390 ui->listWidget->addItem(item);
392 ui->listWidget->scrollToItem(item, QAbstractItemView::PositionAtCenter);
397 void PlayListMainWindow::updateUiWithCurrentStatus(VlcStatus * status) {
398 ui->loopButton->setChecked(status->loop);
399 ui->repeatButton->setChecked(status->repeat);
400 ui->shuffleButton->setChecked(status->random);