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