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 "accountdialog.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 = 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 ct = nodes.count();
168 for (int idx = 0; idx < ct; ++idx) {
169 QDomNode node = nodes.at(idx);
170 QString current = "";
171 //QString name = node.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
172 int id = node.attributes().namedItem("id").nodeValue().toInt();
173 if (4 > id && 0 == QString::compare(node.attributes().namedItem("ro").nodeValue(), "ro")) {
174 // got the main playlist, let's build it up
175 if (node.hasChildNodes()) {
176 QDomNodeList leafs = node.childNodes();
177 int leafct = leafs.count();
179 for (int jdx = 0; jdx < leafct; ++jdx) {
180 QDomNode leaf = leafs.at(jdx);
181 VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
182 el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
183 el->path = leaf.attributes().namedItem("uri").nodeValue();
184 el->name = leaf.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
185 current = leaf.attributes().namedItem("current").nodeValue();
186 el->playing = (0 < current.length());
188 if (0 == QString::compare(leaf.nodeName(), "node")) {
190 el->extension = getExtension(el->path, NULL);
191 this->mContents->append(*el);
192 // now parse the child nodes as leafs.
193 if (leaf.hasChildNodes()) {
194 QDomNodeList items = leaf.childNodes();
195 int itemct = items.count();
197 for (int kdx = 0; kdx < itemct; ++kdx) {
198 QDomNode item = items.at(kdx);
199 VlcPlayListElementSimple* it = new VlcPlayListElementSimple();
200 it->id = item.attributes().namedItem("id").nodeValue().toInt();
201 it->path = item.attributes().namedItem("uri").nodeValue();
202 it->name = item.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
203 it->extension = getExtension(it->path, NULL);
206 current = item.attributes().namedItem("current").nodeValue();
207 it->playing = (0 < current.length());
208 this->mContents->append(*it);
216 el->extension = getExtension(el->path, NULL);
217 this->mContents->append(*el);
233 QString PlayListMainWindow::getExtension(QString path, QString extension) {
234 // return extension if exists
235 if (!extension.isNull() && !extension.isEmpty()) return extension;
236 // return blank if no path
237 if (path.isNull() || path.isEmpty()) return "";
238 // otherwise extract the extension
239 int dot_pos = path.lastIndexOf('.');
241 return path.right(path.length() - (dot_pos + 1));
248 VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
249 //if (0 != QString::compare("", text)) {
250 for (int idx = 0; idx < mContents->count(); ++idx) {
251 if (0 == QString::compare(text, mContents->at(idx).name)) {
252 return mContents->at(idx);
256 return *(new VlcPlayListElementSimple());
259 void PlayListMainWindow::updateList() {
260 int ct = this->mContents->count();
262 QIcon icon_audio = QIcon::fromTheme("general_audio_file");
263 QIcon icon_video = QIcon::fromTheme("general_video_file");
264 QIcon icon_image = QIcon::fromTheme("general_image");
265 QIcon icon_flash = QIcon::fromTheme("filemanager_flash_file");
266 QIcon icon_media = QIcon::fromTheme("filemanager_media_folder");
267 for (int idx = 0; idx < ct; ++idx) {
268 VlcPlayListElementSimple el = mContents->at(idx);
269 QListWidgetItem* item;
270 if (0 == QString::compare("node", el.type)) {
271 item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
274 if ( 0 == QString::compare(el.extension, "jpg") ||
275 0 == QString::compare(el.extension, "jpeg") ||
276 0 == QString::compare(el.extension, "gif") ||
277 0 == QString::compare(el.extension, "png") ||
278 0 == QString::compare(el.extension, "bmp") ) {
279 item = new QListWidgetItem(icon_image, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .jpg, .jpeg, .gif, .png, .bmp
281 else if ( 0 == QString::compare(el.extension, "mp3") ||
282 0 == QString::compare(el.extension, "m4a") ||
283 0 == QString::compare(el.extension, "ogg") ||
284 0 == QString::compare(el.extension, "oga") ||
285 0 == QString::compare(el.extension, "wav") ||
286 0 == QString::compare(el.extension, "flac") ) {
287 item = new QListWidgetItem(icon_audio, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .mp3, .m4a, .ogg, .oga, .wav, .flac
289 else if ( 0 == QString::compare(el.extension, "avi") ||
290 0 == QString::compare(el.extension, "mpeg") ||
291 0 == QString::compare(el.extension, "mpg") ||
292 0 == QString::compare(el.extension, "mov") ||
293 0 == QString::compare(el.extension, "mp4") ||
294 0 == QString::compare(el.extension, "wmv") ||
295 0 == QString::compare(el.extension, "mkv") ||
296 0 == QString::compare(el.extension, "ogv") ) {
297 item = new QListWidgetItem(icon_video, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
299 else if ( 0 == QString::compare(el.extension, "flv") ) {
300 item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .flv
303 if (el.name.contains("Flash")) {
304 item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
307 item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
311 item->setSelected(el.playing);
312 ui->listWidget->addItem(item);
314 ui->listWidget->scrollToItem(item, QAbstractItemView::PositionAtCenter);
319 void PlayListMainWindow::updateUiWithCurrentStatus(VlcStatus * status) {
320 ui->loopButton->setChecked(status->loop);
321 ui->repeatButton->setChecked(status->repeat);
322 ui->shuffleButton->setChecked(status->random);