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