Consolidated play / pause to single button
[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 #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 = 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 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();
178         if (0 < leafct) {
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());
187             el->depth = 1;
188             if (0 == QString::compare(leaf.nodeName(), "node")) {
189               el->type = "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();
196                 if (0 < itemct) {
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);
204                     it->depth = 2;
205                     it->type = "leaf";
206                     current = item.attributes().namedItem("current").nodeValue();
207                     it->playing = (0 < current.length());
208                     this->mContents->append(*it);
209                     delete it;
210                   }
211                 }
212               }
213             }
214             else {
215               el->type = "leaf";
216               el->extension = getExtension(el->path, NULL);
217               this->mContents->append(*el);
218             }
219             delete el;
220           }
221         }
222       }
223
224     }
225   }
226
227   mResponse.clear();
228
229   this->updateList();
230
231 }
232
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('.');
240     if (0 < dot_pos) {
241         return path.right(path.length() - (dot_pos + 1));
242     }
243     else { // no dot
244         return "";
245     }
246 }
247
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);
253       }
254     }
255     //}
256     return *(new VlcPlayListElementSimple());
257 }
258
259 void PlayListMainWindow::updateList() {
260   int ct = this->mContents->count();
261   if (0 < ct) {
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);
272       }
273       else {
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
280           }
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
288           }
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
298           }
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
301           }
302           else {
303               if (el.name.contains("Flash")) {
304                   item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
305               }
306               else {
307                   item = new QListWidgetItem(icon_media, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
308               }
309           }
310       }
311       item->setSelected(el.playing);
312       ui->listWidget->addItem(item);
313       if (el.playing) {
314           ui->listWidget->scrollToItem(item, QAbstractItemView::PositionAtCenter);
315       }
316     }
317   }
318 }
319 void PlayListMainWindow::updateUiWithCurrentStatus(VlcStatus * status) {
320     ui->loopButton->setChecked(status->loop);
321     ui->repeatButton->setChecked(status->repeat);
322     ui->shuffleButton->setChecked(status->random);
323 }