Updated the licenses to include my details
[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 "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->listWidget->setTextElideMode(Qt::ElideLeft);
44     ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45
46     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
47     ui->clearButton->setIcon(QIcon::fromTheme("general_delete"));
48     ui->shuffleButton->setIcon(QIcon::fromTheme("mediaplayer_default_shuffle"));
49     ui->loopButton->setIcon(QIcon::fromTheme("general_refresh"));
50     ui->repeatButton->setIcon(QIcon::fromTheme("general_redo"));
51     ui->removeButton->setIcon(QIcon::fromTheme("general_close"));
52
53     ui->clearButton->setDisabled(false);
54     ui->shuffleButton->setDisabled(false);
55     ui->loopButton->setDisabled(false);
56     ui->repeatButton->setDisabled(false);
57     ui->removeButton->setDisabled(true);
58     ui->playButton->setDisabled(true);
59
60     connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
61     connect(ui->removeButton,SIGNAL(clicked()),this,SLOT(onRemove()));
62     connect(ui->repeatButton,SIGNAL(clicked()),this,SLOT(onRepeat()));
63     connect(ui->loopButton,SIGNAL(clicked()),this,SLOT(onLoop()));
64     connect(ui->shuffleButton,SIGNAL(clicked()),this,SLOT(onShuffle()));
65     connect(ui->clearButton,SIGNAL(clicked()),this,SLOT(onClear()));
66     connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
67
68     init();
69
70 }
71 void PlayListMainWindow::init()  // CALL WHEN CONFIG CHANGES
72 {
73     mIp = AccountDialog::currentIp();
74 }
75 void PlayListMainWindow::showPlayList()  // CALL WHEN SHOWN
76 {
77     requestPlayList();
78 }
79
80 PlayListMainWindow::~PlayListMainWindow()
81 {
82     delete ui;
83 }
84
85 void PlayListMainWindow::changeEvent(QEvent *e)
86 {
87     QMainWindow::changeEvent(e);
88     switch (e->type()) {
89     case QEvent::LanguageChange:
90         ui->retranslateUi(this);
91         break;
92     default:
93         break;
94     }
95 }
96
97 void PlayListMainWindow::onListSelectionChanged() {
98     QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
99     if (0 < items.count()) {
100         mCurrentElement = getElementFromText(items.at(0)->text());
101         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!
102         ui->removeButton->setDisabled(false);
103         ui->playButton->setDisabled(false);
104     }
105     else {
106         mCurrentVlcIndex = 0;
107         ui->removeButton->setDisabled(true);
108         ui->playButton->setDisabled(true);
109     }
110 }
111
112 void PlayListMainWindow::onRemove() {
113     if (0 < this->mCurrentVlcIndex) {
114         /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_delete&id=" + QString::number(this->mCurrentVlcIndex))));
115         connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
116     }
117 }
118 void PlayListMainWindow::onPlay() {
119     if (0 < this->mCurrentVlcIndex) {
120         /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play&id=" + QString::number(this->mCurrentVlcIndex))));
121     }
122 }
123 void PlayListMainWindow::onRepeat() {
124     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_repeat")));
125 }
126 void PlayListMainWindow::onLoop() {
127     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_loop")));
128 }
129 void PlayListMainWindow::onShuffle() {
130     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_random")));
131 }
132 void PlayListMainWindow::onClear() {
133     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_empty")));
134     connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
135 }
136 void PlayListMainWindow::requestPlayList() {
137   mContents->clear();
138   ui->listWidget->clear();
139   mResponse.clear();
140   ui->removeButton->setDisabled(true);
141   ui->playButton->setDisabled(true);
142   QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/playlist.xml")));
143   disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
144   connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
145   connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
146 }
147 void PlayListMainWindow::readReady() {
148   QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
149   // append to buffer
150   mResponse += reply->readAll();
151 }
152 void PlayListMainWindow::finished(QNetworkReply * reply) {
153   // now we can call parseXmlList to process the full buffers
154   this->parseXmlPlayList();
155   // only interested in finished signals
156   disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
157 }
158
159 void PlayListMainWindow::parseXmlPlayList() {
160   QDomDocument doc;
161   doc.setContent(this->mResponse);
162   QDomElement docElem = doc.documentElement();
163   QDomNodeList nodes = docElem.elementsByTagName("node");
164   int depth = 0;
165
166   int ct = nodes.count();
167   for (int idx = 0; idx < ct; ++idx) {
168     QDomNode node = nodes.at(idx);
169     QString current = "";
170     //QString name = node.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
171     int id = node.attributes().namedItem("id").nodeValue().toInt();
172     if (4 > id && 0 == QString::compare(node.attributes().namedItem("ro").nodeValue(), "ro")) {
173       // got the main playlist, let's build it up
174       if (node.hasChildNodes()) {
175         QDomNodeList leafs = node.childNodes();
176         int leafct = leafs.count();
177         if (0 < leafct) {
178           for (int jdx = 0; jdx < leafct; ++jdx) {
179             QDomNode leaf = leafs.at(jdx);
180             VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
181             el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
182             el->path = leaf.attributes().namedItem("uri").nodeValue();
183             el->name = leaf.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
184             current = leaf.attributes().namedItem("current").nodeValue();
185             el->playing = (0 < current.length());
186             el->depth = 1;
187             if (0 == QString::compare(leaf.nodeName(), "node")) {
188               el->type = "node";
189               el->extension = getExtension(el->path, NULL);
190               this->mContents->append(*el);
191               // now parse the child nodes as leafs.
192               if (leaf.hasChildNodes()) {
193                 QDomNodeList items = leaf.childNodes();
194                 int itemct = items.count();
195                 if (0 < itemct) {
196                   for (int kdx = 0; kdx < itemct; ++kdx) {
197                     QDomNode item = items.at(kdx);
198                     VlcPlayListElementSimple* it = new VlcPlayListElementSimple();
199                     it->id = item.attributes().namedItem("id").nodeValue().toInt();
200                     it->path = item.attributes().namedItem("uri").nodeValue();
201                     it->name = item.attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
202                     it->extension = getExtension(it->path, NULL);
203                     it->depth = 2;
204                     it->type = "leaf";
205                     current = item.attributes().namedItem("current").nodeValue();
206                     it->playing = (0 < current.length());
207                     this->mContents->append(*it);
208                     delete it;
209                   }
210                 }
211               }
212             }
213             else {
214               el->type = "leaf";
215               el->extension = getExtension(el->path, NULL);
216               this->mContents->append(*el);
217             }
218             delete el;
219           }
220         }
221       }
222
223     }
224   }
225
226   mResponse.clear();
227
228   this->updateList();
229
230 }
231
232 QString PlayListMainWindow::getExtension(QString path, QString extension) {
233     // return extension if exists
234     if (!extension.isNull() && !extension.isEmpty()) return extension;
235     // return blank if no path
236     if (path.isNull() || path.isEmpty()) return "";
237     // otherwise extract the extension
238     int dot_pos = path.lastIndexOf('.');
239     if (0 < dot_pos) {
240         return path.right(path.length() - (dot_pos + 1));
241     }
242     else { // no dot
243         return "";
244     }
245 }
246
247 VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
248   //if (0 != QString::compare("", text)) {
249     for (int idx = 0; idx < mContents->count(); ++idx) {
250       if (0 == QString::compare(text, mContents->at(idx).name)) {
251         return mContents->at(idx);
252       }
253     }
254     //}
255     return *(new VlcPlayListElementSimple());
256 }
257
258 void PlayListMainWindow::updateList() {
259   int ct = this->mContents->count();
260   if (0 < ct) {
261     QIcon icon_audio  = QIcon::fromTheme("general_audio_file");
262     QIcon icon_video  = QIcon::fromTheme("general_video_file");
263     QIcon icon_image  = QIcon::fromTheme("general_image");
264     QIcon icon_flash  = QIcon::fromTheme("filemanager_flash_file");
265     QIcon icon_media  = QIcon::fromTheme("filemanager_media_folder");
266     for (int idx = 0; idx < ct; ++idx) {
267       VlcPlayListElementSimple el = mContents->at(idx);
268       QListWidgetItem* item;
269       if (0 == QString::compare("node", el.type)) {
270         item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
271       }
272       else {
273           if ( 0 == QString::compare(el.extension, "jpg")  ||
274                0 == QString::compare(el.extension, "jpeg") ||
275                0 == QString::compare(el.extension, "gif")  ||
276                0 == QString::compare(el.extension, "png")  ||
277                0 == QString::compare(el.extension, "bmp")  ) {
278               item = new QListWidgetItem(icon_image, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .jpg, .jpeg, .gif, .png, .bmp
279           }
280           else if ( 0 == QString::compare(el.extension, "mp3")  ||
281                     0 == QString::compare(el.extension, "m4a")  ||
282                     0 == QString::compare(el.extension, "ogg")  ||
283                     0 == QString::compare(el.extension, "oga")  ||
284                     0 == QString::compare(el.extension, "wav")  ||
285                     0 == QString::compare(el.extension, "flac")  ) {
286               item = new QListWidgetItem(icon_audio, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .mp3, .m4a, .ogg, .oga, .wav, .flac
287           }
288           else if ( 0 == QString::compare(el.extension, "avi")  ||
289                     0 == QString::compare(el.extension, "mpeg") ||
290                     0 == QString::compare(el.extension, "mpg")  ||
291                     0 == QString::compare(el.extension, "mov")  ||
292                     0 == QString::compare(el.extension, "mp4")  ||
293                     0 == QString::compare(el.extension, "wmv")  ||
294                     0 == QString::compare(el.extension, "mkv")  ||
295                     0 == QString::compare(el.extension, "ogv")  ) {
296               item = new QListWidgetItem(icon_video, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
297           }
298           else if ( 0 == QString::compare(el.extension, "flv")  ) {
299               item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .flv
300           }
301           else {
302               if (el.name.contains("Flash")) {
303                   item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
304               }
305               else {
306                   item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
307               }
308           }
309       }
310       item->setSelected(el.playing);
311       ui->listWidget->addItem(item);
312       if (el.playing) {
313           ui->listWidget->scrollToItem(item, QAbstractItemView::PositionAtCenter);
314       }
315     }
316   }
317 }