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 //mResponse = new QByteArray();
42 ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
43 ui->addButton->setIcon(QIcon::fromTheme("general_add"));
44 ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
45 ui->browseButton->setDisabled(true);
46 ui->playButton->setDisabled(true);
47 ui->addButton->setDisabled(true);
49 connect(ui->browseButton,SIGNAL(clicked()),this,SLOT(onBrowse()));
50 connect(ui->addButton,SIGNAL(clicked()),this,SLOT(onAddToPlaylist()));
51 connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
52 connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
58 void BrowseMainWindow::init() // THIS METHOD IS CALLED WHEN CONFIG CHANGED...
60 mIp = AccountDialog::currentIp();
61 browseDirectory(mCurrentDir);
64 BrowseMainWindow::~BrowseMainWindow()
69 void BrowseMainWindow::changeEvent(QEvent *e)
71 QMainWindow::changeEvent(e);
73 case QEvent::LanguageChange:
74 ui->retranslateUi(this);
81 void BrowseMainWindow::onListSelectionChanged() {
82 QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
83 if (0 < items.count()) {
84 mCurrentElement = getElementFromText(items.at(0)->text());
86 if (0 == QString::compare("..", mCurrentElement.name)) {
87 ui->browseButton->setDisabled(true);
88 ui->playButton->setDisabled(true);
89 ui->addButton->setDisabled(true);
90 browseDirectory(mCurrentElement.path);
94 if (0 == QString::compare("directory", mCurrentElement.type)) {
95 ui->browseButton->setDisabled(false);
98 ui->browseButton->setDisabled(true);
101 ui->playButton->setDisabled(false);
103 ui->addButton->setDisabled(false);
108 VlcBrowseElement BrowseMainWindow::getElementFromText(QString text) {
109 for (int idx = 0; idx < mContents->count(); ++idx) {
110 if (0 == QString::compare(text, mContents->at(idx).name)) {
111 return mContents->at(idx);
114 return *(new VlcBrowseElement());
117 void BrowseMainWindow::onBrowse() {
118 // check for directory
119 if (0 == QString::compare("directory", mCurrentElement.type)) {
120 // call browseDirectory
121 this->browseDirectory(mCurrentElement.path);
124 ui->browseButton->setDisabled(true);
128 void BrowseMainWindow::onAddToPlaylist() {
129 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path)));
132 void BrowseMainWindow::onPlay() {
133 /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path)));
136 void BrowseMainWindow::browseDirectory(QString dir) {
138 ui->listWidget->clear();
140 QNetworkReply * reply = mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir)));
141 //reply->setReadBufferSize(1024 * 500);
142 connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
143 connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
145 void BrowseMainWindow::readReady() {
146 QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
148 mResponse += reply->readAll();
150 void BrowseMainWindow::finished(QNetworkReply * reply) {
151 // now we can call parseXmlDirectory to process the full buffers
152 this->parseXmlDirectory();
153 // only interested in finished signals
154 disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
156 void BrowseMainWindow::parseXmlDirectory() {
158 doc.setContent(this->mResponse);
159 QDomElement docElem = doc.documentElement();
160 QDomNodeList elements = docElem.elementsByTagName("element");
161 // we can sort by folders then files alphabetically by running to lists and appending them at the end
162 // vlc alpha sorts everything in the incoming stream, we just need to seperate files from folders.
163 QList<VlcBrowseElement>* files = new QList<VlcBrowseElement>();
164 if (0 < elements.count()) {
167 QDomNode node = elements.at(idx);
168 VlcBrowseElement* dir = new VlcBrowseElement();
169 dir->type = node.attributes().namedItem("type").nodeValue();
170 dir->size = node.attributes().namedItem("size").nodeValue().toInt();
171 dir->date = QDate::fromString(node.attributes().namedItem("date").nodeValue());
172 dir->path = node.attributes().namedItem("path").nodeValue();
173 dir->name = node.attributes().namedItem("name").nodeValue();
174 dir->extension = node.attributes().namedItem("extension").nodeValue();
176 if (0 != QString::compare("directory", dir->type)) {
180 this->mContents->append(*dir);
183 } while (idx < elements.count());
184 if (0 < files->count()) {
185 mContents->append(*files);
195 void BrowseMainWindow::writeFile(QString path, QByteArray text) {
197 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
200 QTextStream out(&file);
204 void BrowseMainWindow::updateList() {
205 int ct = this->mContents->count();
207 QIcon icon_up = QIcon::fromTheme("filemanager_folder_up");
208 QIcon icon_folder = QIcon::fromTheme("general_folder");
209 QIcon icon_audio = QIcon::fromTheme("general_audio_file");
210 QIcon icon_video = QIcon::fromTheme("general_video_file");
211 QIcon icon_image = QIcon::fromTheme("general_image");
212 QIcon icon_flash = QIcon::fromTheme("filemanager_flash_file");
213 for (int idx = 0; idx < ct; ++idx) {
214 VlcBrowseElement dir = mContents->at(idx);
215 QListWidgetItem* item;
216 bool item_good = false;
217 if (0 == QString::compare("directory", dir.type)) {
218 if (0 == QString::compare("..", dir.name)) {
219 item = new QListWidgetItem(icon_up, dir.name, ui->listWidget, 0);
223 item = new QListWidgetItem(icon_folder, dir.name, ui->listWidget, 0);
227 else if (0 == QString::compare("file", dir.type)) {
228 if ( 0 == QString::compare(dir.extension, "jpg") ||
229 0 == QString::compare(dir.extension, "jpeg") ||
230 0 == QString::compare(dir.extension, "gif") ||
231 0 == QString::compare(dir.extension, "png") ||
232 0 == QString::compare(dir.extension, "bmp") ) {
234 item = new QListWidgetItem(icon_image, dir.name, ui->listWidget, 0); // .jpg, .jpeg, .gif, .png, .bmp
236 else if ( 0 == QString::compare(dir.extension, "mp3") ||
237 0 == QString::compare(dir.extension, "m4a") ||
238 0 == QString::compare(dir.extension, "ogg") ||
239 0 == QString::compare(dir.extension, "oga") ||
240 0 == QString::compare(dir.extension, "wav") ||
241 0 == QString::compare(dir.extension, "flac") ) {
243 item = new QListWidgetItem(icon_audio, dir.name, ui->listWidget, 0); // .mp3, .m4a, .ogg, .oga, .wav, .flac
245 else if ( 0 == QString::compare(dir.extension, "avi") ||
246 0 == QString::compare(dir.extension, "mpeg") ||
247 0 == QString::compare(dir.extension, "mpg") ||
248 0 == QString::compare(dir.extension, "mov") ||
249 0 == QString::compare(dir.extension, "mp4") ||
250 0 == QString::compare(dir.extension, "wmv") ||
251 0 == QString::compare(dir.extension, "mkv") ||
252 0 == QString::compare(dir.extension, "ogv") ) {
254 item = new QListWidgetItem(icon_video, dir.name, ui->listWidget, 0); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
256 else if ( 0 == QString::compare(dir.extension, "flv") ) {
258 item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0); // .flv
261 if (dir.name.startsWith("Flash")) {
263 item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0);
271 ui->listWidget->addItem(item);
273 // other types ignored