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
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
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.
18 #include "browsemainwindow.h"
19 #include "ui_browsemainwindow.h"
21 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
22 #include <QMaemo5InformationBox>
24 #include "configdialog.h"
25 #include "aboutdialog.h"
26 #include "vlcbrowseelement.h"
27 #include "appsettings.h"
29 BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
31 ui(new Ui::BrowseMainWindow)
35 mCurrentDir = "~/"; //AppSettings::getHomeDirectory().path; // This works on win as well as linux, would guess mac too.
36 setWindowTitle("Vlc remote");
39 mNetManager = new QNetworkAccessManager(this);
41 mContents = new QList<VlcBrowseElement>();
43 ui->listWidget->setTextElideMode(Qt::ElideMiddle);
44 ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
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);
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()));
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()));
68 void BrowseMainWindow::init() // THIS METHOD IS CALLED WHEN CONFIG CHANGED...
70 mIp = AppSettings::getCurrentIp(); // AccountDialog::currentIp();
73 void BrowseMainWindow::setHomeDirectory()
75 mCurrentDir = AppSettings::getHomeDirectory().path;
77 void BrowseMainWindow::showCurrentDirectory() // THIS METHOD IS CALLED WHEN WINDOW IS OPENED...
79 browseDirectory(mCurrentDir);
82 BrowseMainWindow::~BrowseMainWindow()
87 void BrowseMainWindow::changeEvent(QEvent *e)
89 QMainWindow::changeEvent(e);
91 case QEvent::LanguageChange:
92 ui->retranslateUi(this);
99 void BrowseMainWindow::showHomeFolder() {
100 browseDirectory(AppSettings::getHomeDirectory().path);
102 void BrowseMainWindow::showUserHomeFolder() {
103 browseDirectory("~/");
105 void BrowseMainWindow::setHomeFolder() {
106 if (0 < mCurrentDir.length()) {
109 dir.path = mCurrentDir;
110 AppSettings::setHomeDirectory(dir);
113 void BrowseMainWindow::showFavourites() {}
114 void BrowseMainWindow::setFavourite() {}
116 void BrowseMainWindow::onListSelectionChanged() {
117 QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
118 if (0 < items.count()) {
119 mCurrentElement = getElementFromText(items.at(0)->text());
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);
130 if (0 == QString::compare("directory", mCurrentElement.type) || 0 == QString::compare("dir", mCurrentElement.type)) {
131 ui->browseButton->setDisabled(false);
134 ui->browseButton->setDisabled(true);
137 ui->playButton->setDisabled(false);
139 ui->addButton->setDisabled(false);
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);
150 return *(new VlcBrowseElement());
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);
161 ui->browseButton->setDisabled(true);
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("\\", "\\\\"))));
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("\\", "\\\\"))));
179 void BrowseMainWindow::browseDirectory(QString dir) {
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);
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 *)));
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);
198 void BrowseMainWindow::readReady() {
199 QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
201 mResponse += reply->readAll();
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);
213 void BrowseMainWindow::parseXmlDirectory() {
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>();
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());
234 if (0 != QString::compare("directory", dir->type) && 0 != QString::compare("dir", dir->type)) {
237 else if (0 == QString::compare("..", dir->name)) {
238 this->mContents->prepend(*dir);
241 this->mContents->append(*dir);
244 } while (idx < elements.count());
245 if (0 < files->count()) {
246 mContents->append(*files);
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);
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('.');
271 return path.right(path.length() - (dot_pos + 1));
278 void BrowseMainWindow::updateList() {
279 ui->listWidget->clear();
280 int ct = this->mContents->count();
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);
300 item = new QListWidgetItem(icon_folder, dir.name, ui->listWidget, 0);
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") ) {
311 item = new QListWidgetItem(icon_image, dir.name, ui->listWidget, 0); // .jpg, .jpeg, .gif, .png, .bmp
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") ) {
320 item = new QListWidgetItem(icon_audio, dir.name, ui->listWidget, 0); // .mp3, .m4a, .ogg, .oga, .wav, .flac
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") ) {
332 item = new QListWidgetItem(icon_video, dir.name, ui->listWidget, 0); // .avi, .mpg, .mpeg, .mov, .mp4, .m4v, .wmv, .mkv, .ogv
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
339 else if ( 0 == QString::compare(dir.extension, "flv") ) {
341 item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0); // .flv
344 if (dir.name.startsWith("Flash")) {
346 item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0);
350 //item = new QListWidgetItem(icon_unknown, dir.name, ui->listWidget, 0);
355 ui->listWidget->addItem(item);
357 // other types ignored