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