4af4b7aab9b8319d768ba040ca1854d1b8817619
[vlc-remote] / playlistmainwindow.cpp
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
6  *   Software Foundation
7  *
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
12  *
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.
17  */
18 #include "playlistmainwindow.h"
19 #include "ui_playlistmainwindow.h"
20 #include <QPushButton>
21 #include <QSettings>
22 #include "configdialog.h"
23 #include "aboutdialog.h"
24 #include "accountdialog.h"
25
26 PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
27         QMainWindow(parent),
28         ui(new Ui::PlayListMainWindow)
29 {
30
31     ui->setupUi(this);
32     mTimer = new QTimer(this);
33     setWindowTitle("Vlc remote");
34
35     mCurrentDepth = 0;
36     mCurrentVlcIndex = 0;
37
38
39     mNetManager = new QNetworkAccessManager(this);
40
41     mContents = new QList<VlcPlayListElementSimple>();
42
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"));
49
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);
56
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()));
64
65     init();
66
67 }
68 void PlayListMainWindow::init()  // CALL WHEN CONFIG CHANGES
69 {
70     mIp = AccountDialog::currentIp();
71 }
72 void PlayListMainWindow::showPlayList()  // CALL WHEN SHOWN
73 {
74     requestPlayList();
75 }
76
77 PlayListMainWindow::~PlayListMainWindow()
78 {
79     delete ui;
80 }
81
82 void PlayListMainWindow::changeEvent(QEvent *e)
83 {
84     QMainWindow::changeEvent(e);
85     switch (e->type()) {
86     case QEvent::LanguageChange:
87         ui->retranslateUi(this);
88         break;
89     default:
90         break;
91     }
92 }
93
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);
101     }
102     else {
103         mCurrentVlcIndex = 0;
104         ui->removeButton->setDisabled(true);
105         ui->playButton->setDisabled(true);
106     }
107 }
108
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         this->requestPlayList();
113     }
114 }
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))));
118     }
119 }
120 void PlayListMainWindow::onRepeat() {
121     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_repeat")));
122 }
123 void PlayListMainWindow::onLoop() {
124     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_loop")));
125 }
126 void PlayListMainWindow::onShuffle() {
127     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_random")));
128 }
129 void PlayListMainWindow::onClear() {
130     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_empty")));
131                                  this->requestPlayList();
132 }
133 void PlayListMainWindow::requestPlayList() {
134   mContents->clear();
135   ui->listWidget->clear();
136   mResponse.clear();
137   ui->removeButton->setDisabled(true);
138   ui->playButton->setDisabled(true);
139   QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/playlist.xml")));
140   connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
141   connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
142 }
143 void PlayListMainWindow::readReady() {
144   QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
145   // append to buffer
146   mResponse += reply->readAll();
147 }
148 void PlayListMainWindow::finished(QNetworkReply * reply) {
149   // now we can call parseXmlList to process the full buffers
150   this->parseXmlPlayList();
151   // only interested in finished signals
152   disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
153 }
154
155 void PlayListMainWindow::parseXmlPlayList() {
156   QDomDocument doc;
157   doc.setContent(this->mResponse);
158   QDomElement docElem = doc.documentElement();
159   QDomNodeList nodes = docElem.elementsByTagName("node");
160
161   int depth = 0;
162
163   int ct = nodes.count();
164   for (int idx = 0; idx < ct; ++idx) {
165     QDomNode node = nodes.at(idx);
166     QString name = node.attributes().namedItem("name").nodeValue();
167     if (0 == QString::compare("Playlist", name)) {
168       // got the main playlist, let's build it up
169       if (node.hasChildNodes()) {
170         QDomNodeList leafs = node.childNodes();
171         int leafct = leafs.count();
172         if (0 < leafct) {
173           for (int jdx = 0; jdx < leafct; ++jdx) {
174             QDomNode leaf = leafs.at(jdx);
175             VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
176             el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
177             //el->path = leaf.attributes().namedItem("uri").nodeValue();
178             el->name = leaf.attributes().namedItem("name").nodeValue();
179             if (0 == QString::compare(leaf.nodeName(), "node")) {
180               el->depth = 1;
181               el->type = "node";
182               this->mContents->append(*el);
183               // now parse the child nodes as leafs.
184               if (leaf.hasChildNodes()) {
185                 QDomNodeList items = leaf.childNodes();
186                 int itemct = items.count();
187                 if (0 < itemct) {
188                   for (int kdx = 0; kdx < itemct; ++kdx) {
189                     QDomNode item = items.at(kdx);
190                     VlcPlayListElementSimple* it = new VlcPlayListElementSimple();
191                     it->id = item.attributes().namedItem("id").nodeValue().toInt();
192                     //it->path = item.attributes().namedItem("uri").nodeValue();
193                     it->name = item.attributes().namedItem("name").nodeValue();
194                     it->depth = 2;
195                     it->type = "leaf";
196                     this->mContents->append(*it);
197                     delete it;
198                   }
199                 }
200               }
201             }
202             else {
203               el->depth = 1;
204               el->type = "leaf";
205               this->mContents->append(*el);
206             }
207             delete el;
208           }
209         }
210       }
211
212     }
213   }
214
215   mResponse.clear();
216
217   this->updateList();
218
219 }
220
221 VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
222   //if (0 != QString::compare("", text)) {
223     for (int idx = 0; idx < mContents->count(); ++idx) {
224       if (0 == QString::compare(text, mContents->at(idx).name)) {
225         return mContents->at(idx);
226       }
227     }
228     //}
229     return *(new VlcPlayListElementSimple());
230 }
231
232 void PlayListMainWindow::updateList() {
233   int ct = this->mContents->count();
234   if (0 < ct) {
235     for (int idx = 0; idx < ct; ++idx) {
236       VlcPlayListElementSimple el = mContents->at(idx);
237       QListWidgetItem* item;//
238       if (0 == QString::compare("node", el.type)) {
239         item = new QListWidgetItem(QIcon::fromTheme("filemanager_media_folder"), el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
240       }
241       else {
242         item = new QListWidgetItem(QIcon::fromTheme("general_video_file"), el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
243       }
244       ui->listWidget->addItem(item);
245       /// TODO - Work out the file / media type and use an appropriate icon instead of the default.
246     }
247   }
248 }