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