d77844d6acf1352e6b4dcd098669b907d52992bc
[vlc-remote] / browsemainwindow.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 "browsemainwindow.h"
19 #include "ui_browsemainwindow.h"
20 #include <QSettings>
21 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
22 #include <QMaemo5InformationBox>
23 #endif
24 #include "configdialog.h"
25 #include "aboutdialog.h"
26 #include "vlcbrowseelement.h"
27 #include "appsettings.h"
28 #include "favouritesmainwindow.h"
29
30 BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
31         QMainWindow(parent),
32         ui(new Ui::BrowseMainWindow)
33 {
34
35     ui->setupUi(this);
36     mFavouritesMainWindow = new FavouritesMainWindow;
37     mCurrentDir = "~/"; //AppSettings::getHomeDirectory().path; // This works on win as well as linux, would guess mac too.
38     setWindowTitle("Vlc remote");
39
40
41     mNetManager = new QNetworkAccessManager(this);
42
43     mContents = new QList<VlcBrowseElement>();
44
45     ui->listWidget->setTextElideMode(Qt::ElideMiddle);
46     ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47
48     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
49     ui->addButton->setIcon(QIcon::fromTheme("general_add"));
50     ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
51     ui->browseButton->setDisabled(true);
52     ui->playButton->setDisabled(true);
53     ui->addButton->setDisabled(true);
54
55
56 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
57
58     mFavouritesMainWindow->setParent(this);
59     mFavouritesMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
60     setAttribute(Qt::WA_Maemo5StackedWindow);
61     mFavouritesMainWindow->setWindowFlags(mFavouritesMainWindow->windowFlags() | Qt::Window);
62
63 #endif
64
65     connect(ui->browseButton,SIGNAL(clicked()),this,SLOT(onBrowse()));
66     connect(ui->addButton,SIGNAL(clicked()),this,SLOT(onAddToPlaylist()));
67     connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
68     connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
69
70     connect(ui->actionGoUserHome, SIGNAL(triggered()), this, SLOT(showUserHomeFolder()));
71     connect(ui->actionGoHome, SIGNAL(triggered()), this, SLOT(showHomeFolder()));
72     connect(ui->actionSetHome, SIGNAL(triggered()), this, SLOT(setHomeFolder()));
73     connect(ui->actionViewFavourites, SIGNAL(triggered()), this, SLOT(showFavourites()));
74     connect(ui->actionSetFavourite, SIGNAL(triggered()), this, SLOT(setFavourite()));
75
76     init();
77
78
79 }
80 void BrowseMainWindow::init()  // THIS METHOD IS CALLED WHEN CONFIG CHANGED...
81 {
82     mIp = AppSettings::getCurrentIp(); // AccountDialog::currentIp();
83     setHomeDirectory();
84 }
85 void BrowseMainWindow::setHomeDirectory()
86 {
87     mCurrentDir = AppSettings::getHomeDirectory().path;
88 }
89 void BrowseMainWindow::showCurrentDirectory()  // THIS METHOD IS CALLED WHEN WINDOW IS OPENED...
90 {
91     browseDirectory(mCurrentDir);
92 }
93
94 BrowseMainWindow::~BrowseMainWindow()
95 {
96     delete ui;
97 }
98
99 void BrowseMainWindow::changeEvent(QEvent *e)
100 {
101     QMainWindow::changeEvent(e);
102     switch (e->type()) {
103     case QEvent::LanguageChange:
104         ui->retranslateUi(this);
105         break;
106     default:
107         break;
108     }
109 }
110
111 void BrowseMainWindow::showHomeFolder() {
112     browseDirectory(AppSettings::getHomeDirectory().path);
113 }
114 void BrowseMainWindow::showUserHomeFolder() {
115     browseDirectory("~/");
116 }
117 void BrowseMainWindow::setHomeFolder() {
118     if (0 < mCurrentElement.name.length() && (0 == QString::compare("directory", mCurrentElement.type) || 0 == QString::compare("dir", mCurrentElement.type))) {
119         VlcDirectory dir;
120         dir.name = mCurrentElement.name;
121         dir.path = mCurrentElement.path;
122         AppSettings::setHomeDirectory(dir);
123     }
124     else if (0 < mCurrentDir.length()) {
125         VlcDirectory dir;
126         QString name = mCurrentDir;
127         int idx = mCurrentDir.lastIndexOf('/');
128         if (0 > idx) idx = mCurrentDir.lastIndexOf('\\');
129         if (0 < idx) {
130             name = mCurrentDir.right(mCurrentDir.length() - (idx + 1));
131         }
132         dir.name = name;
133         dir.path = mCurrentDir;
134         AppSettings::setHomeDirectory(dir);
135     }
136 }
137 void BrowseMainWindow::showFavourites() {
138     mFavouritesMainWindow->show();
139     mFavouritesMainWindow->init();
140 }
141 void BrowseMainWindow::setFavourite() {
142     if (0 < mCurrentElement.name.length() && (0 == QString::compare("directory", mCurrentElement.type) || 0 == QString::compare("dir", mCurrentElement.type))) {
143         VlcDirectory dir;
144         dir.name = mCurrentElement.name;
145         dir.path = mCurrentElement.path;
146         AppSettings::addFavourite(dir);
147     }
148     else if (0 < mCurrentDir.length()) {
149         VlcDirectory dir;
150         QString name = mCurrentDir;
151         int idx = mCurrentDir.lastIndexOf('/');
152         if (0 > idx) idx = mCurrentDir.lastIndexOf('\\');
153         if (0 < idx) {
154             name = mCurrentDir.right(mCurrentDir.length() - (idx + 1));
155         }
156         dir.name = name;
157         dir.path = mCurrentDir;
158         AppSettings::addFavourite(dir);
159     }
160 }
161
162 void BrowseMainWindow::onListSelectionChanged() {
163     QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
164     if (0 < items.count()) {
165         mCurrentElement = getElementFromText(items.at(0)->text());
166         // are we up dir?
167         if (0 == QString::compare("..", mCurrentElement.name)) {
168             ui->browseButton->setDisabled(true);
169             ui->playButton->setDisabled(true);
170             ui->addButton->setDisabled(true);
171             mCurrentDir = mCurrentElement.path;
172             browseDirectory(mCurrentDir);
173         }
174         else {
175             // can we browse?
176             if (0 == QString::compare("directory", mCurrentElement.type) || 0 == QString::compare("dir", mCurrentElement.type)) {
177                 ui->browseButton->setDisabled(false);
178             }
179             else {
180                 ui->browseButton->setDisabled(true);
181             }
182             // can we play?
183             ui->playButton->setDisabled(false);
184             // can we playlist?
185             ui->addButton->setDisabled(false);
186         }
187     }
188 }
189
190 VlcBrowseElement BrowseMainWindow::getElementFromText(QString text) {
191     for (int idx = 0; idx < mContents->count(); ++idx) {
192         if (0 == QString::compare(text, mContents->at(idx).name)) {
193             return mContents->at(idx);
194         }
195     }
196     return *(new VlcBrowseElement());
197 }
198
199 void BrowseMainWindow::onBrowse() {
200     // check for directory
201     if (0 == QString::compare("directory", mCurrentElement.type) || 0 == QString::compare("dir", mCurrentElement.type)) {
202         // call browseDirectory
203         mCurrentDir = mCurrentElement.path;
204         browseDirectory(mCurrentDir);
205     }
206     else {
207         ui->browseButton->setDisabled(true);
208     }
209 }
210
211 void BrowseMainWindow::onAddToPlaylist() {
212     QUrl url = QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue");
213     url.addEncodedQueryItem(QByteArray("input"), QUrl::toPercentEncoding(mCurrentElement.path.replace("\\", "\\\\").replace("'", "\\'")));
214     mNetManager->get(QNetworkRequest(url));
215     //mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path.replace("\\", "\\\\"))));
216 }
217
218 void BrowseMainWindow::onPlay() {
219     QUrl url = QUrl("http://"+mIp+"/requests/status.xml?command=in_play");
220     url.addEncodedQueryItem(QByteArray("input"), QUrl::toPercentEncoding(mCurrentElement.path.replace("\\", "\\\\").replace("'", "\\'")));
221     mNetManager->get(QNetworkRequest(url));
222     //mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path.replace("\\", "\\\\"))));
223 }
224
225 void BrowseMainWindow::browseDirectory(QString dir) {
226     mResponse.clear();
227     QUrl url = QUrl("http://"+mIp+"/requests/browse.xml");
228     url.addEncodedQueryItem(QByteArray("dir"), QUrl::toPercentEncoding(dir));
229     QNetworkReply * reply = mNetManager->get(QNetworkRequest(url));
230     //QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir.replace("&", "%26").replace("\\", "\\\\"))));
231 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
232     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
233 #endif
234     connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
235     connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
236     connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
237 }
238 void BrowseMainWindow::error(QNetworkReply::NetworkError code) {
239 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
240     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
241 #endif
242     qDebug() << code;
243 }
244 void BrowseMainWindow::readReady() {
245     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
246     // append to buffer
247     mResponse += reply->readAll();
248 }
249 void BrowseMainWindow::finished(QNetworkReply * reply) {
250     // now we can call parseXmlDirectory to process the full buffers
251     this->parseXmlDirectory();
252     // only interested in finished signals
253     disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
254 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
255     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
256 #endif
257     delete reply;
258 }
259 void BrowseMainWindow::parseXmlDirectory() {
260     QDomDocument doc;
261     doc.setContent(this->mResponse);
262     QDomElement docElem = doc.documentElement();
263     QDomNodeList elements = docElem.elementsByTagName("element");
264     // we can sort by folders then files alphabetically by running to lists and appending them at the end
265     // vlc alpha sorts everything in the incoming stream, we just need to seperate files from folders.
266     if (0 < elements.count()) {
267         QList<VlcBrowseElement>* files = new QList<VlcBrowseElement>();
268         int idx = 0;
269         mContents->clear();
270         do {
271             QDomNode node = elements.at(idx);
272             VlcBrowseElement* dir = new VlcBrowseElement();
273             dir->type = node.attributes().namedItem("type").nodeValue();
274             dir->size = node.attributes().namedItem("size").nodeValue().toInt();
275             dir->date = QDate::fromString(node.attributes().namedItem("date").nodeValue());
276             dir->path = node.attributes().namedItem("path").nodeValue();
277             dir->name = node.attributes().namedItem("name").nodeValue();
278             dir->extension = getExtension(dir->path, node.attributes().namedItem("extension").nodeValue());
279             ++idx;
280             if (0 != QString::compare("directory", dir->type) && 0 != QString::compare("dir", dir->type)) {
281                 files->append(*dir);
282             }
283             else if (0 == QString::compare("..", dir->name)) {
284                 this->mContents->prepend(*dir);
285             }
286             else {
287                 this->mContents->append(*dir);
288             }
289             delete dir;
290         } while (idx < elements.count());
291         if (0 < files->count()) {
292             mContents->append(*files);
293         }
294         delete files;
295         // Update UI
296         this->updateList();
297     }
298     else {
299         // alert user of error / empty directory
300         qDebug() << "can't browse dir: " << mCurrentDir;
301 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
302         QMaemo5InformationBox::information(this, tr("Directory could not be browsed!"), QMaemo5InformationBox::DefaultTimeout);
303 #endif
304     }
305     mResponse.clear();
306
307 }
308
309 QString BrowseMainWindow::getExtension(QString path, QString extension) {
310     // return extension if exists
311     if (!extension.isNull() && !extension.isEmpty()) return extension;
312     // return blank if no path
313     if (path.isNull() || path.isEmpty()) return "";
314     // otherwise extract the extension
315     int dot_pos = path.lastIndexOf('.');
316     if (0 < dot_pos) {
317         return path.right(path.length() - (dot_pos + 1));
318     }
319     else { // no dot
320         return "";
321     }
322 }
323
324 void BrowseMainWindow::updateList() {
325     ui->listWidget->clear();
326     int ct = this->mContents->count();
327     if (0 < ct) {
328         QIcon icon_up     = QIcon::fromTheme("filemanager_folder_up");
329         QIcon icon_folder = QIcon::fromTheme("general_folder");
330         QIcon icon_audio  = QIcon::fromTheme("general_audio_file");
331         QIcon icon_video  = QIcon::fromTheme("general_video_file");
332         QIcon icon_image  = QIcon::fromTheme("general_image");
333         QIcon icon_flash  = QIcon::fromTheme("filemanager_flash_file");
334         QIcon icon_real   = QIcon::fromTheme("filemanager_real_music");
335         QIcon icon_playl  = QIcon::fromTheme("filemanager_playlist");
336         QIcon icon_unknown= QIcon::fromTheme("filemanager_unknown_file");
337         for (int idx = 0; idx < ct; ++idx) {
338             VlcBrowseElement dir = mContents->at(idx);
339             QListWidgetItem* item;
340             bool item_good = false;
341             if (0 == QString::compare("directory", dir.type) || 0 == QString::compare("dir", dir.type)) {
342                 if (0 == QString::compare("..", dir.name)) {
343                     item = new QListWidgetItem(icon_up, dir.name, ui->listWidget, 0);
344                     item_good = true;
345                 }
346                 else {
347                     item = new QListWidgetItem(icon_folder, dir.name, ui->listWidget, 0);
348                     item_good = true;
349                 }
350             }
351             else if (0 == QString::compare("file", dir.type)) {
352                 if ( 0 == QString::compare(dir.extension, "jpg")  ||
353                      0 == QString::compare(dir.extension, "jpeg") ||
354                      0 == QString::compare(dir.extension, "gif")  ||
355                      0 == QString::compare(dir.extension, "png")  ||
356                      0 == QString::compare(dir.extension, "bmp")  ) {
357                     item_good = true;
358                     item = new QListWidgetItem(icon_image, dir.name, ui->listWidget, 0); // .jpg, .jpeg, .gif, .png, .bmp
359                 }
360                 else if ( 0 == QString::compare(dir.extension, "mp3")  ||
361                           0 == QString::compare(dir.extension, "m4a")  ||
362                           0 == QString::compare(dir.extension, "ogg")  ||
363                           0 == QString::compare(dir.extension, "oga")  ||
364                           0 == QString::compare(dir.extension, "wav")  ||
365                           0 == QString::compare(dir.extension, "flac")  ) {
366                     item_good = true;
367                     item = new QListWidgetItem(icon_audio, dir.name, ui->listWidget, 0); // .mp3, .m4a, .ogg, .oga, .wav, .flac
368                 }
369                 else if ( 0 == QString::compare(dir.extension, "m3u")  ||
370                           0 == QString::compare(dir.extension, "wpl")  ||
371                           0 == QString::compare(dir.extension, "pls")  ||
372                           0 == QString::compare(dir.extension, "asx")  ||
373                           0 == QString::compare(dir.extension, "xspf") ||
374                           0 == QString::compare(dir.extension, "cmml")  ) {
375                     item_good = true;
376                     item = new QListWidgetItem(icon_playl, dir.name, ui->listWidget, 0); // .m3u, .wpl, .pls, .asx, .xspf, .cmml
377                 }
378                 else if ( 0 == QString::compare(dir.extension, "avi")  ||
379                           0 == QString::compare(dir.extension, "mpeg") ||
380                           0 == QString::compare(dir.extension, "mpg")  ||
381                           0 == QString::compare(dir.extension, "mov")  ||
382                           0 == QString::compare(dir.extension, "mp4")  ||
383                           0 == QString::compare(dir.extension, "m4v")  ||
384                           0 == QString::compare(dir.extension, "wmv")  ||
385                           0 == QString::compare(dir.extension, "mkv")  ||
386                           0 == QString::compare(dir.extension, "ogv")  ) {
387                     item_good = true;
388                     item = new QListWidgetItem(icon_video, dir.name, ui->listWidget, 0); // .avi, .mpg, .mpeg, .mov, .mp4, .m4v, .wmv, .mkv, .ogv
389                 }
390                 else if ( 0 == QString::compare(dir.extension, "rm")  ||
391                           0 == QString::compare(dir.extension, "ra")  ||
392                           0 == QString::compare(dir.extension, "ram")  ) {
393                     item = new QListWidgetItem(icon_real, dir.name, ui->listWidget, 0); // .ram, 'rm, 'ra
394                 }
395                 else if ( 0 == QString::compare(dir.extension, "flv")  ) {
396                     item_good = true;
397                     item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0); // .flv
398                 }
399                 else {
400                     if (dir.name.startsWith("Flash")) {
401                         item_good = true;
402                         item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0);
403                     }
404                     else {
405                         item_good = false;
406                         //item = new QListWidgetItem(icon_unknown, dir.name, ui->listWidget, 0);
407                     }
408                 }
409             }
410             if (item_good) {
411                 ui->listWidget->addItem(item);
412             }
413             // other types ignored
414         }
415     }
416 }
417
418