modified: browsemainwindow.cpp
[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
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     QSettings settings;
39     QString currentKey = settings.value("config/currentKey").toString();
40     mIp = settings.value("account/"+currentKey).toString();
41
42     //mIp = settings.value("ip").toString();
43
44     mNetManager = new QNetworkAccessManager(this);
45
46     mContents = new QList<VlcPlayListElementSimple>();
47
48     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
49     ui->clearButton->setIcon(QIcon::fromTheme("general_delete"));
50     ui->shuffleButton->setIcon(QIcon::fromTheme("mediaplayer_default_shuffle"));
51     ui->loopButton->setIcon(QIcon::fromTheme("general_refresh"));
52     ui->repeatButton->setIcon(QIcon::fromTheme("general_redo"));
53     ui->removeButton->setIcon(QIcon::fromTheme("general_close"));
54
55     ui->clearButton->setDisabled(false);
56     ui->shuffleButton->setDisabled(false);
57     ui->loopButton->setDisabled(false);
58     ui->repeatButton->setDisabled(false);
59     ui->removeButton->setDisabled(true);
60     ui->playButton->setDisabled(true);
61
62     connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
63     connect(ui->removeButton,SIGNAL(clicked()),this,SLOT(onRemove()));
64     connect(ui->repeatButton,SIGNAL(clicked()),this,SLOT(onRepeat()));
65     connect(ui->loopButton,SIGNAL(clicked()),this,SLOT(onLoop()));
66     connect(ui->shuffleButton,SIGNAL(clicked()),this,SLOT(onShuffle()));
67     connect(ui->clearButton,SIGNAL(clicked()),this,SLOT(onClear()));
68     connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
69
70     this->requestPlayList();
71 }
72
73 PlayListMainWindow::~PlayListMainWindow()
74 {
75     delete ui;
76 }
77
78 void PlayListMainWindow::changeEvent(QEvent *e)
79 {
80     QMainWindow::changeEvent(e);
81     switch (e->type()) {
82     case QEvent::LanguageChange:
83         ui->retranslateUi(this);
84         break;
85     default:
86         break;
87     }
88 }
89
90 void PlayListMainWindow::onListSelectionChanged() {
91     QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
92     if (0 < items.count()) {
93         mCurrentElement = getElementFromText(items.at(0)->text());
94         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!
95         ui->removeButton->setDisabled(false);
96         ui->playButton->setDisabled(false);
97     }
98     else {
99         mCurrentVlcIndex = 0;
100         ui->removeButton->setDisabled(true);
101         ui->playButton->setDisabled(true);
102     }
103 }
104
105 void PlayListMainWindow::onRemove() {
106     if (0 < this->mCurrentVlcIndex) {
107         /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_delete&id=" + QString::number(this->mCurrentVlcIndex))));
108         this->requestPlayList();
109     }
110 }
111 void PlayListMainWindow::onPlay() {
112     if (0 < this->mCurrentVlcIndex) {
113         /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play&id=" + QString::number(this->mCurrentVlcIndex))));
114     }
115 }
116 void PlayListMainWindow::onRepeat() {
117     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_repeat")));
118 }
119 void PlayListMainWindow::onLoop() {
120     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_loop")));
121 }
122 void PlayListMainWindow::onShuffle() {
123     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_random")));
124 }
125 void PlayListMainWindow::onClear() {
126     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_empty")));
127     this->requestPlayList();
128 }
129 void PlayListMainWindow::requestPlayList() {
130     ui->listWidget->clear();
131     ui->removeButton->setDisabled(true);
132     ui->playButton->setDisabled(true);
133     QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/playlist.xml")));
134     connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlPlayList()));
135 }
136
137 void PlayListMainWindow::parseXmlPlayList() {
138     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
139     QDomDocument doc;
140     doc.setContent(reply->readAll());
141     QDomElement docElem = doc.documentElement();
142     QDomNodeList nodes = docElem.elementsByTagName("node");
143     mContents->clear();
144
145     int depth = 0;
146
147     int ct = nodes.count();
148     qDebug() << "elements " << ct;
149     for (int idx = 0; idx < ct; ++idx) {
150         QDomNode node = nodes.at(idx);
151         QString name = node.attributes().namedItem("name").nodeValue();
152         if (0 == QString::compare("Playlist", name)) {
153             // got the main playlist, let's build it up
154             if (node.hasChildNodes()) {
155                 QDomNodeList leafs = node.childNodes();
156                 int leafct = leafs.count();
157                 if (0 < leafct) {
158                     for (int jdx = 0; jdx < leafct; ++jdx) {
159                         QDomNode leaf = leafs.at(jdx);
160                         VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
161                         el->depth = 1;
162                         el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
163                         el->type = "leaf";
164                         el->path = leaf.attributes().namedItem("uri").nodeValue();
165                         el->name = leaf.attributes().namedItem("name").nodeValue();
166                         this->mContents->append(*el);
167                         delete el;
168                     }
169                 }
170             }
171
172         }
173     }
174
175
176
177     delete reply;
178
179     this->updateList();
180
181 }
182
183 VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
184     //if (0 != QString::compare("", text)) {
185         for (int idx = 0; idx < mContents->count(); ++idx) {
186             if (0 == QString::compare(text, mContents->at(idx).name)) {
187                 return mContents->at(idx);
188             }
189         }
190     //}
191     return *(new VlcPlayListElementSimple());
192 }
193
194 void PlayListMainWindow::updateList() {
195     int ct = this->mContents->count();
196     if (0 < ct) {
197         for (int idx = 0; idx < ct; ++idx) {
198             VlcPlayListElementSimple el = mContents->at(idx);
199             QListWidgetItem* item;
200             item = new QListWidgetItem(QIcon::fromTheme("general_video_file"), el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
201             ui->listWidget->addItem(item);
202             /// TODO - Work out the file / media type and use an appropriate icon instead of the default.
203         }
204     }
205 }
206