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();
183 for (int jdx = 0; jdx < leafct; ++jdx) {
184 QDomNode leaf = leafs.at(jdx);
185 VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
186 el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
187 el->path = leaf.attributes().namedItem("uri").nodeValue();
188 el->name = leaf.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
189 current = leaf.attributes().namedItem("current").nodeValue();
190 el->playing = (0 < current.length());
192 if (0 == QString::compare(leaf.nodeName(), "node")) {
194 el->extension = getExtension(el->path, NULL);
195 this->mContents->append(*el);
196 // now parse the child nodes as leafs.
197 if (leaf.hasChildNodes()) {
198 QDomNodeList items = leaf.childNodes();
199 int itemct = items.count();
201 for (int kdx = 0; kdx < itemct; ++kdx) {
202 QDomNode item = items.at(kdx);
203 VlcPlayListElementSimple* it = new VlcPlayListElementSimple();
204 it->id = item.attributes().namedItem("id").nodeValue().toInt();
205 it->path = item.attributes().namedItem("uri").nodeValue();
206 it->name = item.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
207 it->extension = getExtension(it->path, NULL);
210 current = item.attributes().namedItem("current").nodeValue();
211 it->playing = (0 < current.length());
213 currentLeafId = it->id;
214 QString art = item.toElement().namedItem("art_url").toElement().text();
215 hasArt = (!art.isNull() && !art.isEmpty());
216 extension = getExtension(it->path, NULL);
218 this->mContents->append(*it);
226 el->extension = getExtension(el->path, NULL);
227 this->mContents->append(*el);
239 emit this->idUpdated(currentLeafId, hasArt, extension);
245 QString PlayListMainWindow::getExtension(QString path, QString extension) {
246 // return extension if exists
247 if (!extension.isNull() && !extension.isEmpty()) return extension;
248 // return blank if no path
249 if (path.isNull() || path.isEmpty()) return "";
250 // otherwise extract the extension
251 int dot_pos = path.lastIndexOf('.');
253 return path.right(path.length() - (dot_pos + 1));
260 VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
261 //if (0 != QString::compare("", text)) {
262 for (int idx = 0; idx < mContents->count(); ++idx) {
263 if (0 == QString::compare(text, mContents->at(idx).name)) {
264 return mContents->at(idx);
268 return *(new VlcPlayListElementSimple());
271 void PlayListMainWindow::updateList() {
272 int ct = this->mContents->count();
274 QIcon icon_audio = QIcon::fromTheme("general_audio_file");
275 QIcon icon_video = QIcon::fromTheme("general_video_file");
276 QIcon icon_image = QIcon::fromTheme("general_image");
277 QIcon icon_flash = QIcon::fromTheme("filemanager_flash_file");
278 QIcon icon_media = QIcon::fromTheme("filemanager_media_folder");
279 for (int idx = 0; idx < ct; ++idx) {
280 VlcPlayListElementSimple el = mContents->at(idx);
281 QListWidgetItem* item;
282 if (0 == QString::compare("node", el.type)) {
283 item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
286 if ( 0 == QString::compare(el.extension, "jpg") ||
287 0 == QString::compare(el.extension, "jpeg") ||
288 0 == QString::compare(el.extension, "gif") ||
289 0 == QString::compare(el.extension, "png") ||
290 0 == QString::compare(el.extension, "bmp") ) {
291 item = new QListWidgetItem(icon_image, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .jpg, .jpeg, .gif, .png, .bmp
293 else if ( 0 == QString::compare(el.extension, "mp3") ||
294 0 == QString::compare(el.extension, "m4a") ||
295 0 == QString::compare(el.extension, "ogg") ||
296 0 == QString::compare(el.extension, "oga") ||
297 0 == QString::compare(el.extension, "wav") ||
298 0 == QString::compare(el.extension, "flac") ) {
299 item = new QListWidgetItem(icon_audio, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .mp3, .m4a, .ogg, .oga, .wav, .flac
301 else if ( 0 == QString::compare(el.extension, "avi") ||
302 0 == QString::compare(el.extension, "mpeg") ||
303 0 == QString::compare(el.extension, "mpg") ||
304 0 == QString::compare(el.extension, "mov") ||
305 0 == QString::compare(el.extension, "mp4") ||
306 0 == QString::compare(el.extension, "wmv") ||
307 0 == QString::compare(el.extension, "mkv") ||
308 0 == QString::compare(el.extension, "ogv") ) {
309 item = new QListWidgetItem(icon_video, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
311 else if ( 0 == QString::compare(el.extension, "flv") ) {
312 item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .flv
315 if (el.name.contains("Flash")) {
316 item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
319 item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
323 item->setSelected(el.playing);
324 ui->listWidget->addItem(item);
326 ui->listWidget->scrollToItem(item, QAbstractItemView::PositionAtCenter);
331 void PlayListMainWindow::updateUiWithCurrentStatus(VlcStatus * status) {
332 ui->loopButton->setChecked(status->loop);
333 ui->repeatButton->setChecked(status->repeat);
334 ui->shuffleButton->setChecked(status->random);