74715517ba1c30504f8a84332dbea614648ff289
[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 #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     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path)));
134                              }
135
136 void BrowseMainWindow::onPlay() {
137     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path)));
138                              }
139
140 void BrowseMainWindow::browseDirectory(QString dir) {
141     mContents->clear();
142     ui->listWidget->clear();
143     mResponse.clear();
144     QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir)));
145     connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
146     connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
147 }
148 void BrowseMainWindow::readReady() {
149     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
150     // append to buffer
151     mResponse += reply->readAll();
152 }
153 void BrowseMainWindow::finished(QNetworkReply * reply) {
154     // now we can call parseXmlDirectory to process the full buffers
155     this->parseXmlDirectory();
156     // only interested in finished signals
157     disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
158 }
159 void BrowseMainWindow::parseXmlDirectory() {
160     QDomDocument doc;
161     doc.setContent(this->mResponse);
162     QDomElement docElem = doc.documentElement();
163     QDomNodeList elements = docElem.elementsByTagName("element");
164     // we can sort by folders then files alphabetically by running to lists and appending them at the end
165     // vlc alpha sorts everything in the incoming stream, we just need to seperate files from folders.
166     QList<VlcBrowseElement>* files = new QList<VlcBrowseElement>();
167     if (0 < elements.count()) {
168         int idx = 0;
169         do {
170             QDomNode node = elements.at(idx);
171             VlcBrowseElement* dir = new VlcBrowseElement();
172             dir->type = node.attributes().namedItem("type").nodeValue();
173             dir->size = node.attributes().namedItem("size").nodeValue().toInt();
174             dir->date = QDate::fromString(node.attributes().namedItem("date").nodeValue());
175             dir->path = node.attributes().namedItem("path").nodeValue();
176             dir->name = node.attributes().namedItem("name").nodeValue();
177             dir->extension = node.attributes().namedItem("extension").nodeValue();
178             ++idx;
179             if (0 != QString::compare("directory", dir->type)) {
180                 files->append(*dir);
181             }
182             else {
183                 this->mContents->append(*dir);
184             }
185             delete dir;
186         } while (idx < elements.count());
187         if (0 < files->count()) {
188             mContents->append(*files);
189         }
190     }
191     delete files;
192     mResponse.clear();
193
194     // Update UI
195     this->updateList();
196 }
197
198 void BrowseMainWindow::writeFile(QString path, QByteArray text) {
199     QFile file(path);
200     if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
201         return;
202
203     QTextStream out(&file);
204     out << text;
205 }
206
207 void BrowseMainWindow::updateList() {
208     int ct = this->mContents->count();
209     if (0 < ct) {
210         QIcon icon_up     = QIcon::fromTheme("filemanager_folder_up");
211         QIcon icon_folder = QIcon::fromTheme("general_folder");
212         QIcon icon_audio  = QIcon::fromTheme("general_audio_file");
213         QIcon icon_video  = QIcon::fromTheme("general_video_file");
214         QIcon icon_image  = QIcon::fromTheme("general_image");
215         QIcon icon_flash  = QIcon::fromTheme("filemanager_flash_file");
216         for (int idx = 0; idx < ct; ++idx) {
217             VlcBrowseElement dir = mContents->at(idx);
218             QListWidgetItem* item;
219             bool item_good = false;
220             if (0 == QString::compare("directory", dir.type)) {
221                 if (0 == QString::compare("..", dir.name)) {
222                     item = new QListWidgetItem(icon_up, dir.name, ui->listWidget, 0);
223                     item_good = true;
224                 }
225                 else {
226                     item = new QListWidgetItem(icon_folder, dir.name, ui->listWidget, 0);
227                     item_good = true;
228                 }
229             }
230             else if (0 == QString::compare("file", dir.type)) {
231                 if ( 0 == QString::compare(dir.extension, "jpg")  ||
232                      0 == QString::compare(dir.extension, "jpeg") ||
233                      0 == QString::compare(dir.extension, "gif")  ||
234                      0 == QString::compare(dir.extension, "png")  ||
235                      0 == QString::compare(dir.extension, "bmp")  ) {
236                     item_good = true;
237                     item = new QListWidgetItem(icon_image, dir.name, ui->listWidget, 0); // .jpg, .jpeg, .gif, .png, .bmp
238                 }
239                 else if ( 0 == QString::compare(dir.extension, "mp3")  ||
240                           0 == QString::compare(dir.extension, "m4a")  ||
241                           0 == QString::compare(dir.extension, "ogg")  ||
242                           0 == QString::compare(dir.extension, "oga")  ||
243                           0 == QString::compare(dir.extension, "wav")  ||
244                           0 == QString::compare(dir.extension, "flac")  ) {
245                     item_good = true;
246                     item = new QListWidgetItem(icon_audio, dir.name, ui->listWidget, 0); // .mp3, .m4a, .ogg, .oga, .wav, .flac
247                 }
248                 else if ( 0 == QString::compare(dir.extension, "avi")  ||
249                           0 == QString::compare(dir.extension, "mpeg") ||
250                           0 == QString::compare(dir.extension, "mpg")  ||
251                           0 == QString::compare(dir.extension, "mov")  ||
252                           0 == QString::compare(dir.extension, "mp4")  ||
253                           0 == QString::compare(dir.extension, "wmv")  ||
254                           0 == QString::compare(dir.extension, "mkv")  ||
255                           0 == QString::compare(dir.extension, "ogv")  ) {
256                     item_good = true;
257                     item = new QListWidgetItem(icon_video, dir.name, ui->listWidget, 0); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
258                 }
259                 else if ( 0 == QString::compare(dir.extension, "flv")  ) {
260                     item_good = true;
261                     item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0); // .flv
262                 }
263                 else {
264                     if (dir.name.startsWith("Flash")) {
265                         item_good = true;
266                         item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0);
267                     }
268                     else {
269                         item_good = false;
270                     }
271                 }
272             }
273             if (item_good) {
274                 ui->listWidget->addItem(item);
275             }
276             // other types ignored
277         }
278     }
279 }
280
281