7bb6cbb7ec0c43cfa23445f4eab7f672b34e363d
[vlc-remote] / browsemainwindow.cpp
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
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
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     QSettings settings;
36     mIp = settings.value("ip").toString();
37
38     mNetManager = new QNetworkAccessManager(this);
39
40     mContents = new QList<VlcBrowseElement>();
41
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);
48
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()));
53
54     this->browseDirectory(mCurrentDir);
55 }
56
57 BrowseMainWindow::~BrowseMainWindow()
58 {
59     delete ui;
60 }
61
62 void BrowseMainWindow::changeEvent(QEvent *e)
63 {
64     QMainWindow::changeEvent(e);
65     switch (e->type()) {
66     case QEvent::LanguageChange:
67         ui->retranslateUi(this);
68         break;
69     default:
70         break;
71     }
72 }
73
74 void BrowseMainWindow::onListSelectionChanged() {
75     QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
76     if (0 < items.count()) {
77         mCurrentElement = getElementFromText(items.at(0)->text());
78         // are we up dir?
79         if (0 == QString::compare("..", mCurrentElement.name)) {
80             ui->browseButton->setDisabled(true);
81             ui->playButton->setDisabled(true);
82             ui->addButton->setDisabled(true);
83             browseDirectory(mCurrentElement.path);
84         }
85         else {
86             // can we browse?
87             if (0 == QString::compare("directory", mCurrentElement.type)) {
88                 ui->browseButton->setDisabled(false);
89             }
90             else {
91                 ui->browseButton->setDisabled(true);
92             }
93             // can we play?
94             ui->playButton->setDisabled(false);
95             // can we playlist?
96             ui->addButton->setDisabled(false);
97         }
98     }
99 }
100
101 VlcBrowseElement BrowseMainWindow::getElementFromText(QString text) {
102     //if (0 != QString::compare("", text)) {
103         for (int idx = 0; idx < mContents->count(); ++idx) {
104             if (0 == QString::compare(text, mContents->at(idx).name)) {
105                 return mContents->at(idx);
106             }
107         }
108     //}
109     return *(new VlcBrowseElement());
110 }
111
112 void BrowseMainWindow::onBrowse() {
113     // check for directory
114     if (0 == QString::compare("directory", mCurrentElement.type)) {
115         // call browseDirectory
116         this->browseDirectory(mCurrentElement.path);
117     }
118     else {
119         ui->browseButton->setDisabled(true);
120     }
121 }
122
123 void BrowseMainWindow::onAddToPlaylist() {
124     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path)));
125 }
126
127 void BrowseMainWindow::onPlay() {
128     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path)));
129 }
130
131 void BrowseMainWindow::browseDirectory(QString dir) {
132     ui->listWidget->clear();
133     QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir)));
134     connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlDirectory()));
135 }
136 void BrowseMainWindow::parseXmlDirectory() {
137     qDebug() << "got called!";
138     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
139     QDomDocument doc;
140     doc.setContent(reply->readAll());
141     QDomElement docElem = doc.documentElement();
142     QDomNodeList elements = docElem.elementsByTagName("element");
143     mContents->clear();
144     //mContents = new QList<VlcBrowseElement>();
145     // we can sort by filders then files alphabetically by running to lists and appending them at the end
146     // vlc alpha sorts everything in the incoming stream, we just need to seperate files from folders.
147     QList<VlcBrowseElement>* files = new QList<VlcBrowseElement>();
148     if (0 < elements.count()) {
149         int idx = 0;
150         do {
151             QDomNode node = elements.at(idx);
152             VlcBrowseElement* dir = new VlcBrowseElement();
153             dir->type = node.attributes().namedItem("type").nodeValue();
154             dir->size = node.attributes().namedItem("size").nodeValue().toInt();
155             dir->date = QDate::fromString(node.attributes().namedItem("date").nodeValue());
156             dir->path = node.attributes().namedItem("path").nodeValue();
157             dir->name = node.attributes().namedItem("name").nodeValue();
158             dir->extension = node.attributes().namedItem("extension").nodeValue();
159             ++idx;
160             if (0 != QString::compare("directory", dir->type)) {
161                 files->append(*dir);
162             }
163             else {
164                 this->mContents->append(*dir);
165             }
166             delete dir;
167         } while (idx < elements.count());
168         if (0 < files->count()) {
169             mContents->append(*files);
170         }
171     }
172     delete files;
173     //reply->deleteLater();
174     delete reply;
175
176     // Update UI
177     this->updateList();
178 }
179
180 void BrowseMainWindow::writeFile(QString path, QByteArray text) {
181     QFile file(path);
182          if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
183              return;
184
185          QTextStream out(&file);
186          out << text;
187 }
188
189 void BrowseMainWindow::updateList() {
190     int ct = this->mContents->count();
191     if (0 < ct) {
192         QIcon icon_up     = QIcon::fromTheme("filemanager_folder_up");
193         QIcon icon_folder = QIcon::fromTheme("general_folder");
194         QIcon icon_audio  = QIcon::fromTheme("general_audio_file");
195         QIcon icon_video  = QIcon::fromTheme("general_video_file");
196         QIcon icon_image  = QIcon::fromTheme("general_image");
197         QIcon icon_flash  = QIcon::fromTheme("filemanager_flash_file");
198         for (int idx = 0; idx < ct; ++idx) {
199             VlcBrowseElement dir = mContents->at(idx);
200             QListWidgetItem* item;
201             bool item_good = false;
202             if (0 == QString::compare("directory", dir.type)) {
203                 if (0 == QString::compare("..", dir.name)) {
204                     item = new QListWidgetItem(icon_up, dir.name, ui->listWidget, 0);
205                     item_good = true;
206                 }
207                 else {
208                     item = new QListWidgetItem(icon_folder, dir.name, ui->listWidget, 0);
209                     item_good = true;
210                 }
211             }
212             else if (0 == QString::compare("file", dir.type)) {
213                 if ( 0 == QString::compare(dir.extension, "jpg")  ||
214                      0 == QString::compare(dir.extension, "jpeg") ||
215                      0 == QString::compare(dir.extension, "gif")  ||
216                      0 == QString::compare(dir.extension, "png")  ||
217                      0 == QString::compare(dir.extension, "bmp")  ) {
218                     item_good = true;
219                     item = new QListWidgetItem(icon_image, dir.name, ui->listWidget, 0); // .jpg, .jpeg, .gif, .png, .bmp
220                 }
221                 else if ( 0 == QString::compare(dir.extension, "mp3")  ||
222                           0 == QString::compare(dir.extension, "m4a")  ||
223                           0 == QString::compare(dir.extension, "ogg")  ||
224                           0 == QString::compare(dir.extension, "oga")  ||
225                           0 == QString::compare(dir.extension, "wav")  ||
226                           0 == QString::compare(dir.extension, "flac")  ) {
227                     item_good = true;
228                     item = new QListWidgetItem(icon_audio, dir.name, ui->listWidget, 0); // .mp3, .m4a, .ogg, .oga, .wav, .flac
229                 }
230                 else if ( 0 == QString::compare(dir.extension, "avi")  ||
231                           0 == QString::compare(dir.extension, "mpeg") ||
232                           0 == QString::compare(dir.extension, "mpg")  ||
233                           0 == QString::compare(dir.extension, "mov")  ||
234                           0 == QString::compare(dir.extension, "mp4")  ||
235                           0 == QString::compare(dir.extension, "wmv")  ||
236                           0 == QString::compare(dir.extension, "mkv")  ||
237                           0 == QString::compare(dir.extension, "ogv")  ) {
238                     item_good = true;
239                     item = new QListWidgetItem(icon_video, dir.name, ui->listWidget, 0); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
240                 }
241                 else if ( 0 == QString::compare(dir.extension, "flv")  ) {
242                     item_good = true;
243                     item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0); // .flv
244                 }
245                 else {
246                     if (dir.name.startsWith("Flash")) {
247                         item_good = true;
248                         item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0);
249                     }
250                     else {
251                         item_good = false;
252                     }
253                 }
254             }
255             if (item_good) {
256                 ui->listWidget->addItem(item);
257             }
258             // other types ignored
259             //if (item) delete item;
260         }
261     }
262 }
263
264