1 /* VLC-REMOTE for MAEMO 5
2 * Copyright (C) 2010 Schutz Sacha <istdasklar@gmail.com>
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 #include "configdialog.h"
22 #include "aboutdialog.h"
23 #include "vlcbrowseelement.h"
24 #include "accountdialog.h"
26 BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
28 ui(new Ui::BrowseMainWindow)
32 mCurrentDir = "~/"; // This works on win as well as linux, would guess mac too.
33 setWindowTitle("Vlc remote");
36 mNetManager = new QNetworkAccessManager(this);
38 mContents = new QList<VlcBrowseElement>();
40 ui->listWidget->setTextElideMode(Qt::ElideMiddle);
41 ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
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);
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()));
59 void BrowseMainWindow::init() // THIS METHOD IS CALLED WHEN CONFIG CHANGED...
61 mIp = AccountDialog::currentIp();
63 void BrowseMainWindow::showCurrentDirectory() // THIS METHOD IS CALLED WHEN WINDOW IS OPENED...
65 browseDirectory(mCurrentDir);
68 BrowseMainWindow::~BrowseMainWindow()
73 void BrowseMainWindow::changeEvent(QEvent *e)
75 QMainWindow::changeEvent(e);
77 case QEvent::LanguageChange:
78 ui->retranslateUi(this);
85 void BrowseMainWindow::onListSelectionChanged() {
86 QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
87 if (0 < items.count()) {
88 mCurrentElement = getElementFromText(items.at(0)->text());
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);
98 if (0 == QString::compare("directory", mCurrentElement.type)) {
99 ui->browseButton->setDisabled(false);
102 ui->browseButton->setDisabled(true);
105 ui->playButton->setDisabled(false);
107 ui->addButton->setDisabled(false);
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);
118 return *(new VlcBrowseElement());
121 void BrowseMainWindow::onBrowse() {
122 // check for directory
123 if (0 == QString::compare("directory", mCurrentElement.type)) {
124 // call browseDirectory
125 this->browseDirectory(mCurrentElement.path);
128 ui->browseButton->setDisabled(true);
132 void BrowseMainWindow::onAddToPlaylist() {
133 qDebug() << QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path.replace("\\", "\\\\")).toString();
134 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path.replace("\\", "\\\\"))));
137 void BrowseMainWindow::onPlay() {
138 qDebug() << QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path.replace("\\", "\\\\")).toString();
139 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path.replace("\\", "\\\\"))));
142 void BrowseMainWindow::browseDirectory(QString dir) {
144 ui->listWidget->clear();
146 qDebug() << QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir.replace("\\", "\\\\")).toString();
147 QNetworkReply * reply = mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir.replace("\\", "\\\\"))));
148 connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
149 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
151 void BrowseMainWindow::readReady() {
152 QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
154 mResponse += reply->readAll();
156 void BrowseMainWindow::finished(QNetworkReply * reply) {
157 // now we can call parseXmlDirectory to process the full buffers
158 this->parseXmlDirectory();
159 // only interested in finished signals
160 disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
162 void BrowseMainWindow::parseXmlDirectory() {
164 doc.setContent(this->mResponse);
165 QDomElement docElem = doc.documentElement();
166 QDomNodeList elements = docElem.elementsByTagName("element");
167 // we can sort by folders then files alphabetically by running to lists and appending them at the end
168 // vlc alpha sorts everything in the incoming stream, we just need to seperate files from folders.
169 QList<VlcBrowseElement>* files = new QList<VlcBrowseElement>();
170 if (0 < elements.count()) {
173 QDomNode node = elements.at(idx);
174 VlcBrowseElement* dir = new VlcBrowseElement();
175 dir->type = node.attributes().namedItem("type").nodeValue();
176 dir->size = node.attributes().namedItem("size").nodeValue().toInt();
177 dir->date = QDate::fromString(node.attributes().namedItem("date").nodeValue());
178 dir->path = node.attributes().namedItem("path").nodeValue();
179 dir->name = node.attributes().namedItem("name").nodeValue();
180 dir->extension = getExtension(dir->path, node.attributes().namedItem("extension").nodeValue());
182 if (0 != QString::compare("directory", dir->type)) {
185 else if (0 == QString::compare("..", dir->name)) {
186 this->mContents->prepend(*dir);
189 this->mContents->append(*dir);
192 } while (idx < elements.count());
193 if (0 < files->count()) {
194 mContents->append(*files);
204 QString BrowseMainWindow::getExtension(QString path, QString extension) {
205 // return extension if exists
206 if (!extension.isNull() && !extension.isEmpty()) return extension;
207 // return blank if no path
208 if (path.isNull() || path.isEmpty()) return "";
209 // otherwise extract the extension
210 int dot_pos = path.lastIndexOf('.');
212 return path.right(path.length() - (dot_pos + 1));
219 void BrowseMainWindow::writeFile(QString path, QByteArray text) {
221 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
224 QTextStream out(&file);
228 void BrowseMainWindow::updateList() {
229 int ct = this->mContents->count();
231 QIcon icon_up = QIcon::fromTheme("filemanager_folder_up");
232 QIcon icon_folder = QIcon::fromTheme("general_folder");
233 QIcon icon_audio = QIcon::fromTheme("general_audio_file");
234 QIcon icon_video = QIcon::fromTheme("general_video_file");
235 QIcon icon_image = QIcon::fromTheme("general_image");
236 QIcon icon_flash = QIcon::fromTheme("filemanager_flash_file");
237 for (int idx = 0; idx < ct; ++idx) {
238 VlcBrowseElement dir = mContents->at(idx);
239 QListWidgetItem* item;
240 bool item_good = false;
241 if (0 == QString::compare("directory", dir.type)) {
242 if (0 == QString::compare("..", dir.name)) {
243 item = new QListWidgetItem(icon_up, dir.name, ui->listWidget, 0);
247 item = new QListWidgetItem(icon_folder, dir.name, ui->listWidget, 0);
251 else if (0 == QString::compare("file", dir.type)) {
252 if ( 0 == QString::compare(dir.extension, "jpg") ||
253 0 == QString::compare(dir.extension, "jpeg") ||
254 0 == QString::compare(dir.extension, "gif") ||
255 0 == QString::compare(dir.extension, "png") ||
256 0 == QString::compare(dir.extension, "bmp") ) {
258 item = new QListWidgetItem(icon_image, dir.name, ui->listWidget, 0); // .jpg, .jpeg, .gif, .png, .bmp
260 else if ( 0 == QString::compare(dir.extension, "mp3") ||
261 0 == QString::compare(dir.extension, "m4a") ||
262 0 == QString::compare(dir.extension, "ogg") ||
263 0 == QString::compare(dir.extension, "oga") ||
264 0 == QString::compare(dir.extension, "wav") ||
265 0 == QString::compare(dir.extension, "flac") ) {
267 item = new QListWidgetItem(icon_audio, dir.name, ui->listWidget, 0); // .mp3, .m4a, .ogg, .oga, .wav, .flac
269 else if ( 0 == QString::compare(dir.extension, "avi") ||
270 0 == QString::compare(dir.extension, "mpeg") ||
271 0 == QString::compare(dir.extension, "mpg") ||
272 0 == QString::compare(dir.extension, "mov") ||
273 0 == QString::compare(dir.extension, "mp4") ||
274 0 == QString::compare(dir.extension, "wmv") ||
275 0 == QString::compare(dir.extension, "mkv") ||
276 0 == QString::compare(dir.extension, "ogv") ) {
278 item = new QListWidgetItem(icon_video, dir.name, ui->listWidget, 0); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
280 else if ( 0 == QString::compare(dir.extension, "flv") ) {
282 item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0); // .flv
285 if (dir.name.startsWith("Flash")) {
287 item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0);
295 ui->listWidget->addItem(item);
297 // other types ignored