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