1 /* VLC-REMOTE for MAEMO 5
2 * Copyright (C) 2010 Schutz Sacha <istdasklar@gmail.com>
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"
26 PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
28 ui(new Ui::PlayListMainWindow)
32 mTimer = new QTimer(this);
33 setWindowTitle("Vlc remote");
39 mNetManager = new QNetworkAccessManager(this);
41 mContents = new QList<VlcPlayListElementSimple>();
43 ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
44 ui->clearButton->setIcon(QIcon::fromTheme("general_delete"));
45 ui->shuffleButton->setIcon(QIcon::fromTheme("mediaplayer_default_shuffle"));
46 ui->loopButton->setIcon(QIcon::fromTheme("general_refresh"));
47 ui->repeatButton->setIcon(QIcon::fromTheme("general_redo"));
48 ui->removeButton->setIcon(QIcon::fromTheme("general_close"));
50 ui->clearButton->setDisabled(false);
51 ui->shuffleButton->setDisabled(false);
52 ui->loopButton->setDisabled(false);
53 ui->repeatButton->setDisabled(false);
54 ui->removeButton->setDisabled(true);
55 ui->playButton->setDisabled(true);
57 connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
58 connect(ui->removeButton,SIGNAL(clicked()),this,SLOT(onRemove()));
59 connect(ui->repeatButton,SIGNAL(clicked()),this,SLOT(onRepeat()));
60 connect(ui->loopButton,SIGNAL(clicked()),this,SLOT(onLoop()));
61 connect(ui->shuffleButton,SIGNAL(clicked()),this,SLOT(onShuffle()));
62 connect(ui->clearButton,SIGNAL(clicked()),this,SLOT(onClear()));
63 connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
68 void PlayListMainWindow::init() // CALL WHEN CONFIG CHANGES
70 mIp = AccountDialog::currentIp();
72 void PlayListMainWindow::showPlayList() // CALL WHEN SHOWN
77 PlayListMainWindow::~PlayListMainWindow()
82 void PlayListMainWindow::changeEvent(QEvent *e)
84 QMainWindow::changeEvent(e);
86 case QEvent::LanguageChange:
87 ui->retranslateUi(this);
94 void PlayListMainWindow::onListSelectionChanged() {
95 QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
96 if (0 < items.count()) {
97 mCurrentElement = getElementFromText(items.at(0)->text());
98 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!
99 ui->removeButton->setDisabled(false);
100 ui->playButton->setDisabled(false);
103 mCurrentVlcIndex = 0;
104 ui->removeButton->setDisabled(true);
105 ui->playButton->setDisabled(true);
109 void PlayListMainWindow::onRemove() {
110 if (0 < this->mCurrentVlcIndex) {
111 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_delete&id=" + QString::number(this->mCurrentVlcIndex))));
112 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
115 void PlayListMainWindow::onPlay() {
116 if (0 < this->mCurrentVlcIndex) {
117 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play&id=" + QString::number(this->mCurrentVlcIndex))));
120 void PlayListMainWindow::onRepeat() {
121 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_repeat")));
123 void PlayListMainWindow::onLoop() {
124 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_loop")));
126 void PlayListMainWindow::onShuffle() {
127 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_random")));
129 void PlayListMainWindow::onClear() {
130 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_empty")));
131 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
133 void PlayListMainWindow::requestPlayList() {
135 ui->listWidget->clear();
137 ui->removeButton->setDisabled(true);
138 ui->playButton->setDisabled(true);
139 QNetworkReply * reply = mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/playlist.xml")));
140 disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
141 connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
142 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
144 void PlayListMainWindow::readReady() {
145 QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
147 mResponse += reply->readAll();
149 void PlayListMainWindow::finished(QNetworkReply * reply) {
150 // now we can call parseXmlList to process the full buffers
151 this->parseXmlPlayList();
152 // only interested in finished signals
153 disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
156 void PlayListMainWindow::parseXmlPlayList() {
158 doc.setContent(this->mResponse);
159 QDomElement docElem = doc.documentElement();
160 QDomNodeList nodes = docElem.elementsByTagName("node");
164 int ct = nodes.count();
165 for (int idx = 0; idx < ct; ++idx) {
166 QDomNode node = nodes.at(idx);
167 QString name = node.attributes().namedItem("name").nodeValue();
168 int id = node.attributes().namedItem("id").nodeValue().toInt();
170 // got the main playlist, let's build it up
171 if (node.hasChildNodes()) {
172 QDomNodeList leafs = node.childNodes();
173 int leafct = leafs.count();
175 for (int jdx = 0; jdx < leafct; ++jdx) {
176 QDomNode leaf = leafs.at(jdx);
177 VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
178 el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
179 //el->path = leaf.attributes().namedItem("uri").nodeValue();
180 el->name = leaf.attributes().namedItem("name").nodeValue();
181 if (0 == QString::compare(leaf.nodeName(), "node")) {
184 this->mContents->append(*el);
185 // now parse the child nodes as leafs.
186 if (leaf.hasChildNodes()) {
187 QDomNodeList items = leaf.childNodes();
188 int itemct = items.count();
190 for (int kdx = 0; kdx < itemct; ++kdx) {
191 QDomNode item = items.at(kdx);
192 VlcPlayListElementSimple* it = new VlcPlayListElementSimple();
193 it->id = item.attributes().namedItem("id").nodeValue().toInt();
194 //it->path = item.attributes().namedItem("uri").nodeValue();
195 it->name = item.attributes().namedItem("name").nodeValue();
198 this->mContents->append(*it);
207 this->mContents->append(*el);
223 VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
224 //if (0 != QString::compare("", text)) {
225 for (int idx = 0; idx < mContents->count(); ++idx) {
226 if (0 == QString::compare(text, mContents->at(idx).name)) {
227 return mContents->at(idx);
231 return *(new VlcPlayListElementSimple());
234 void PlayListMainWindow::updateList() {
235 int ct = this->mContents->count();
237 for (int idx = 0; idx < ct; ++idx) {
238 VlcPlayListElementSimple el = mContents->at(idx);
239 QListWidgetItem* item;//
240 if (0 == QString::compare("node", el.type)) {
241 item = new QListWidgetItem(QIcon::fromTheme("filemanager_media_folder"), el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
244 item = new QListWidgetItem(QIcon::fromTheme("general_video_file"), el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
246 ui->listWidget->addItem(item);
247 /// TODO - Work out the file / media type and use an appropriate icon instead of the default.