Update async methods! Now valid ip is detected
[vlc-remote] / browsemainwindow.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 "browsemainwindow.h"
19 #include "ui_browsemainwindow.h"
20 #include <QSettings>
21 #include "configdialog.h"
22 #include "aboutdialog.h"
23 #include "vlcbrowseelement.h"
24 #include "accountdialog.h"
25
26 BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
27         QMainWindow(parent),
28         ui(new Ui::BrowseMainWindow)
29 {
30
31     ui->setupUi(this);
32     mCurrentDir = "~/"; // This works on win as well as linux, would guess mac too.
33     setWindowTitle("Vlc remote");
34
35
36     mNetManager = new QNetworkAccessManager(this);
37
38     mContents = new QList<VlcBrowseElement>();
39
40     //mResponse = new QByteArray();
41
42     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
43     ui->addButton->setIcon(QIcon::fromTheme("general_add"));
44     ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
45     ui->browseButton->setDisabled(true);
46     ui->playButton->setDisabled(true);
47     ui->addButton->setDisabled(true);
48
49     connect(ui->browseButton,SIGNAL(clicked()),this,SLOT(onBrowse()));
50     connect(ui->addButton,SIGNAL(clicked()),this,SLOT(onAddToPlaylist()));
51     connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
52     connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
53
54     init();
55
56
57 }
58 void BrowseMainWindow::init()  // THIS METHOD IS CALLED WHEN CONFIG CHANGED...
59 {
60     mIp = AccountDialog::currentIp();
61 }
62 void BrowseMainWindow::showCurrentDirectory()  // THIS METHOD IS CALLED WHEN WINDOW IS OPENED...
63 {
64     browseDirectory(mCurrentDir);
65 }
66
67 BrowseMainWindow::~BrowseMainWindow()
68 {
69     delete ui;
70 }
71
72 void BrowseMainWindow::changeEvent(QEvent *e)
73 {
74     QMainWindow::changeEvent(e);
75     switch (e->type()) {
76     case QEvent::LanguageChange:
77         ui->retranslateUi(this);
78         break;
79     default:
80         break;
81     }
82 }
83
84 void BrowseMainWindow::onListSelectionChanged() {
85     QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
86     if (0 < items.count()) {
87         mCurrentElement = getElementFromText(items.at(0)->text());
88         // are we up dir?
89         if (0 == QString::compare("..", mCurrentElement.name)) {
90             ui->browseButton->setDisabled(true);
91             ui->playButton->setDisabled(true);
92             ui->addButton->setDisabled(true);
93             browseDirectory(mCurrentElement.path);
94         }
95         else {
96             // can we browse?
97             if (0 == QString::compare("directory", mCurrentElement.type)) {
98                 ui->browseButton->setDisabled(false);
99             }
100             else {
101                 ui->browseButton->setDisabled(true);
102             }
103             // can we play?
104             ui->playButton->setDisabled(false);
105             // can we playlist?
106             ui->addButton->setDisabled(false);
107         }
108     }
109 }
110
111 VlcBrowseElement BrowseMainWindow::getElementFromText(QString text) {
112     for (int idx = 0; idx < mContents->count(); ++idx) {
113         if (0 == QString::compare(text, mContents->at(idx).name)) {
114             return mContents->at(idx);
115         }
116     }
117     return *(new VlcBrowseElement());
118 }
119
120 void BrowseMainWindow::onBrowse() {
121     // check for directory
122     if (0 == QString::compare("directory", mCurrentElement.type)) {
123         // call browseDirectory
124         this->browseDirectory(mCurrentElement.path);
125     }
126     else {
127         ui->browseButton->setDisabled(true);
128     }
129 }
130
131 void BrowseMainWindow::onAddToPlaylist() {
132     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path)));
133                              }
134
135 void BrowseMainWindow::onPlay() {
136     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path)));
137                              }
138
139 void BrowseMainWindow::browseDirectory(QString dir) {
140     mContents->clear();
141     ui->listWidget->clear();
142     mResponse.clear();
143     QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir)));
144     connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
145     connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
146 }
147 void BrowseMainWindow::readReady() {
148     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
149     // append to buffer
150     mResponse += reply->readAll();
151 }
152 void BrowseMainWindow::finished(QNetworkReply * reply) {
153     // now we can call parseXmlDirectory to process the full buffers
154     this->parseXmlDirectory();
155     // only interested in finished signals
156     disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
157 }
158 void BrowseMainWindow::parseXmlDirectory() {
159     QDomDocument doc;
160     doc.setContent(this->mResponse);
161     QDomElement docElem = doc.documentElement();
162     QDomNodeList elements = docElem.elementsByTagName("element");
163     // we can sort by folders then files alphabetically by running to lists and appending them at the end
164     // vlc alpha sorts everything in the incoming stream, we just need to seperate files from folders.
165     QList<VlcBrowseElement>* files = new QList<VlcBrowseElement>();
166     if (0 < elements.count()) {
167         int idx = 0;
168         do {
169             QDomNode node = elements.at(idx);
170             VlcBrowseElement* dir = new VlcBrowseElement();
171             dir->type = node.attributes().namedItem("type").nodeValue();
172             dir->size = node.attributes().namedItem("size").nodeValue().toInt();
173             dir->date = QDate::fromString(node.attributes().namedItem("date").nodeValue());
174             dir->path = node.attributes().namedItem("path").nodeValue();
175             dir->name = node.attributes().namedItem("name").nodeValue();
176             dir->extension = node.attributes().namedItem("extension").nodeValue();
177             ++idx;
178             if (0 != QString::compare("directory", dir->type)) {
179                 files->append(*dir);
180             }
181             else {
182                 this->mContents->append(*dir);
183             }
184             delete dir;
185         } while (idx < elements.count());
186         if (0 < files->count()) {
187             mContents->append(*files);
188         }
189     }
190     delete files;
191     mResponse.clear();
192
193     // Update UI
194     this->updateList();
195 }
196
197 void BrowseMainWindow::writeFile(QString path, QByteArray text) {
198     QFile file(path);
199     if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
200         return;
201
202     QTextStream out(&file);
203     out << text;
204 }
205
206 void BrowseMainWindow::updateList() {
207     int ct = this->mContents->count();
208     if (0 < ct) {
209         QIcon icon_up     = QIcon::fromTheme("filemanager_folder_up");
210         QIcon icon_folder = QIcon::fromTheme("general_folder");
211         QIcon icon_audio  = QIcon::fromTheme("general_audio_file");
212         QIcon icon_video  = QIcon::fromTheme("general_video_file");
213         QIcon icon_image  = QIcon::fromTheme("general_image");
214         QIcon icon_flash  = QIcon::fromTheme("filemanager_flash_file");
215         for (int idx = 0; idx < ct; ++idx) {
216             VlcBrowseElement dir = mContents->at(idx);
217             QListWidgetItem* item;
218             bool item_good = false;
219             if (0 == QString::compare("directory", dir.type)) {
220                 if (0 == QString::compare("..", dir.name)) {
221                     item = new QListWidgetItem(icon_up, dir.name, ui->listWidget, 0);
222                     item_good = true;
223                 }
224                 else {
225                     item = new QListWidgetItem(icon_folder, dir.name, ui->listWidget, 0);
226                     item_good = true;
227                 }
228             }
229             else if (0 == QString::compare("file", dir.type)) {
230                 if ( 0 == QString::compare(dir.extension, "jpg")  ||
231                      0 == QString::compare(dir.extension, "jpeg") ||
232                      0 == QString::compare(dir.extension, "gif")  ||
233                      0 == QString::compare(dir.extension, "png")  ||
234                      0 == QString::compare(dir.extension, "bmp")  ) {
235                     item_good = true;
236                     item = new QListWidgetItem(icon_image, dir.name, ui->listWidget, 0); // .jpg, .jpeg, .gif, .png, .bmp
237                 }
238                 else if ( 0 == QString::compare(dir.extension, "mp3")  ||
239                           0 == QString::compare(dir.extension, "m4a")  ||
240                           0 == QString::compare(dir.extension, "ogg")  ||
241                           0 == QString::compare(dir.extension, "oga")  ||
242                           0 == QString::compare(dir.extension, "wav")  ||
243                           0 == QString::compare(dir.extension, "flac")  ) {
244                     item_good = true;
245                     item = new QListWidgetItem(icon_audio, dir.name, ui->listWidget, 0); // .mp3, .m4a, .ogg, .oga, .wav, .flac
246                 }
247                 else if ( 0 == QString::compare(dir.extension, "avi")  ||
248                           0 == QString::compare(dir.extension, "mpeg") ||
249                           0 == QString::compare(dir.extension, "mpg")  ||
250                           0 == QString::compare(dir.extension, "mov")  ||
251                           0 == QString::compare(dir.extension, "mp4")  ||
252                           0 == QString::compare(dir.extension, "wmv")  ||
253                           0 == QString::compare(dir.extension, "mkv")  ||
254                           0 == QString::compare(dir.extension, "ogv")  ) {
255                     item_good = true;
256                     item = new QListWidgetItem(icon_video, dir.name, ui->listWidget, 0); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
257                 }
258                 else if ( 0 == QString::compare(dir.extension, "flv")  ) {
259                     item_good = true;
260                     item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0); // .flv
261                 }
262                 else {
263                     if (dir.name.startsWith("Flash")) {
264                         item_good = true;
265                         item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0);
266                     }
267                     else {
268                         item_good = false;
269                     }
270                 }
271             }
272             if (item_good) {
273                 ui->listWidget->addItem(item);
274             }
275             // other types ignored
276         }
277     }
278 }
279
280