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