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