Began refactoring settings handling.
[vlc-remote] / playlistmainwindow.cpp
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
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 "appsettings.h"
25 #include "vlcstatus.h"
26
27 PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
28         QMainWindow(parent),
29         ui(new Ui::PlayListMainWindow)
30 {
31
32     ui->setupUi(this);
33     mTimer = new QTimer(this);
34     setWindowTitle("Vlc remote");
35
36     mCurrentDepth = 0;
37     mCurrentVlcIndex = 0;
38
39
40     mNetManager = new QNetworkAccessManager(this);
41
42     mContents = new QList<VlcPlayListElementSimple>();
43
44     ui->listWidget->setTextElideMode(Qt::ElideLeft);
45     ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46
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"));
53
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);
60
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()));
68
69     init();
70
71 }
72 void PlayListMainWindow::init()  // CALL WHEN CONFIG CHANGES
73 {
74     mIp = AppSettings::getCurrentIp(); // AccountDialog::currentIp();
75 }
76 void PlayListMainWindow::showPlayList()  // CALL WHEN SHOWN
77 {
78     requestPlayList();
79 }
80
81 PlayListMainWindow::~PlayListMainWindow()
82 {
83     delete ui;
84 }
85
86 void PlayListMainWindow::changeEvent(QEvent *e)
87 {
88     QMainWindow::changeEvent(e);
89     switch (e->type()) {
90     case QEvent::LanguageChange:
91         ui->retranslateUi(this);
92         break;
93     default:
94         break;
95     }
96 }
97
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);
105     }
106     else {
107         mCurrentVlcIndex = 0;
108         ui->removeButton->setDisabled(true);
109         ui->playButton->setDisabled(true);
110     }
111 }
112
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()));
117     }
118 }
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))));
122     }
123 }
124 void PlayListMainWindow::onRepeat() {
125     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_repeat")));
126 }
127 void PlayListMainWindow::onLoop() {
128     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_loop")));
129 }
130 void PlayListMainWindow::onShuffle() {
131     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_random")));
132 }
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()));
136 }
137 void PlayListMainWindow::requestPlayList() {
138   mContents->clear();
139   ui->listWidget->clear();
140   mResponse.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 *)));
147 }
148 void PlayListMainWindow::readReady() {
149   QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
150   // append to buffer
151   mResponse += reply->readAll();
152 }
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 *)));
158 }
159
160 void PlayListMainWindow::parseXmlPlayList() {
161   QDomDocument doc;
162   doc.setContent(this->mResponse);
163   QDomElement docElem = doc.documentElement();
164   QDomNodeList nodes = docElem.elementsByTagName("node");
165   int depth = 0;
166
167   int currentLeafId = 0;
168   bool hasArt = false;
169   QString extension = "";
170
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();
182         if (0 < leafct) {
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());
191             el->depth = 1;
192             if (0 == QString::compare(leaf.nodeName(), "node")) {
193               el->type = "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();
200                 if (0 < itemct) {
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);
208                     it->depth = 2;
209                     it->type = "leaf";
210                     current = item.attributes().namedItem("current").nodeValue();
211                     it->playing = (0 < current.length());
212                     if (it->playing) {
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);
217                     }
218                     this->mContents->append(*it);
219                     delete it;
220                   }
221                 }
222               }
223             }
224             else {
225               el->type = "leaf";
226               el->extension = getExtension(el->path, NULL);
227               this->mContents->append(*el);
228             }
229             delete el;
230           }
231         }
232       }
233
234     }
235   }
236
237   mResponse.clear();
238
239   emit this->idUpdated(currentLeafId, hasArt, extension);
240   this->updateList();
241
242
243 }
244
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('.');
252     if (0 < dot_pos) {
253         return path.right(path.length() - (dot_pos + 1));
254     }
255     else { // no dot
256         return "";
257     }
258 }
259
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);
265       }
266     }
267     //}
268     return *(new VlcPlayListElementSimple());
269 }
270
271 void PlayListMainWindow::updateList() {
272   int ct = this->mContents->count();
273   if (0 < ct) {
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);
284       }
285       else {
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
292           }
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
300           }
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
310           }
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
313           }
314           else {
315               if (el.name.contains("Flash")) {
316                   item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
317               }
318               else {
319                   item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
320               }
321           }
322       }
323       item->setSelected(el.playing);
324       ui->listWidget->addItem(item);
325       if (el.playing) {
326           ui->listWidget->scrollToItem(item, QAbstractItemView::PositionAtCenter);
327       }
328     }
329   }
330 }
331 void PlayListMainWindow::updateUiWithCurrentStatus(VlcStatus * status) {
332     ui->loopButton->setChecked(status->loop);
333     ui->repeatButton->setChecked(status->repeat);
334     ui->shuffleButton->setChecked(status->random);
335 }