c561533a3c9582cec6780dffbae015b5ca988a17
[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 <QDebug>
22 #include "configdialog.h"
23 #include "aboutdialog.h"
24 #include "vlcbrowseelement.h"
25
26
27 BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
28         QMainWindow(parent),
29         ui(new Ui::BrowseMainWindow)
30 {
31
32     ui->setupUi(this);
33     mCurrentDir = "~/"; // This works on win as well as linux, would guess mac too.
34     setWindowTitle("Vlc remote");
35
36     QSettings settings;
37
38     QString currentKey = settings.value("config/currentKey").toString();
39     mIp = settings.value("account/"+currentKey).toString();
40
41
42     mNetManager = new QNetworkAccessManager(this);
43
44     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
45     ui->addButton->setIcon(QIcon::fromTheme("general_add"));
46     ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
47     ui->browseButton->setDisabled(true);
48     ui->playButton->setDisabled(true);
49     ui->addButton->setDisabled(true);
50
51     connect(ui->browseButton,SIGNAL(clicked()),this,SLOT(onBrowse()));
52     connect(ui->addButton,SIGNAL(clicked()),this,SLOT(onAddToPlaylist()));
53     connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
54     connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
55
56     this->browseDirectory(mCurrentDir);
57 }
58
59 BrowseMainWindow::~BrowseMainWindow()
60 {
61     delete ui;
62 }
63
64 void BrowseMainWindow::changeEvent(QEvent *e)
65 {
66     QMainWindow::changeEvent(e);
67     switch (e->type()) {
68     case QEvent::LanguageChange:
69         ui->retranslateUi(this);
70         break;
71     default:
72         break;
73     }
74 }
75
76 void BrowseMainWindow::onListSelectionChanged() {
77     QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
78     if (0 < items.count()) {
79         mCurrentElement = getElementFromText(items.at(0)->text());
80         // are we up dir?
81         if (0 == QString::compare("..", mCurrentElement.name)) {
82             ui->browseButton->setDisabled(true);
83             ui->playButton->setDisabled(true);
84             ui->addButton->setDisabled(true);
85             browseDirectory(mCurrentElement.path);
86         }
87         else {
88             // can we browse?
89             if (0 == QString::compare("directory", mCurrentElement.type)) {
90                 ui->browseButton->setDisabled(false);
91             }
92             else {
93                 ui->browseButton->setDisabled(true);
94             }
95             // can we play?
96             ui->playButton->setDisabled(false);
97             // can we playlist?
98             ui->addButton->setDisabled(false);
99         }
100     }
101 }
102
103 VlcBrowseElement BrowseMainWindow::getElementFromText(QString text) {
104     //if (0 != QString::compare("", text)) {
105     for (int idx = 0; idx < mContents->count(); ++idx) {
106         if (0 == QString::compare(text, mContents->at(idx).name)) {
107             return mContents->at(idx);
108         }
109     }
110     //}
111     return *(new VlcBrowseElement());
112 }
113
114 void BrowseMainWindow::onBrowse() {
115     // check for directory
116     if (0 == QString::compare("directory", mCurrentElement.type)) {
117         // call browseDirectory
118         this->browseDirectory(mCurrentElement.path);
119     }
120 }
121
122 void BrowseMainWindow::onAddToPlaylist() {
123     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path)));
124                              }
125
126 void BrowseMainWindow::onPlay() {
127     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path)));
128                              }
129
130 void BrowseMainWindow::browseDirectory(QString dir) {
131     ui->listWidget->clear();
132     QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir)));
133     connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlDirectory()));
134 }
135 void BrowseMainWindow::parseXmlDirectory() {
136     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
137     QDomDocument doc;
138     doc.setContent(reply->readAll());
139     QDomElement docElem = doc.documentElement();
140     QDomNodeList elements = docElem.elementsByTagName("element");
141     mContents = new QList<VlcBrowseElement>();
142     if (0 < elements.count()) {
143         int idx = 0;
144         do {
145             QDomNode node = elements.at(idx);
146             VlcBrowseElement* dir = new VlcBrowseElement();
147             dir->type = node.attributes().namedItem("type").nodeValue();
148             dir->size = node.attributes().namedItem("size").nodeValue().toInt();
149             dir->date = QDate::fromString(node.attributes().namedItem("date").nodeValue());
150             dir->path = node.attributes().namedItem("path").nodeValue();
151             dir->name = node.attributes().namedItem("name").nodeValue();
152             dir->extension = node.attributes().namedItem("extension").nodeValue();
153             ++idx;
154             this->mContents->append(*dir);
155         } while (idx < elements.count());
156     }
157     delete reply;
158
159     // Update UI
160     this->updateList();
161 }
162
163 void BrowseMainWindow::writeFile(QString path, QByteArray text) {
164     QFile file(path);
165          if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
166              return;
167
168          QTextStream out(&file);
169          out << text;
170 }
171
172 void BrowseMainWindow::updateList() {
173     int ct = this->mContents->count();
174     if (0 < ct) {
175         QIcon icon_up     = QIcon::fromTheme("filemanager_folder_up");
176         QIcon icon_folder = QIcon::fromTheme("general_folder");
177         QIcon icon_audio  = QIcon::fromTheme("general_audio_file");
178         QIcon icon_video  = QIcon::fromTheme("general_video_file");
179         QIcon icon_image  = QIcon::fromTheme("general_image");
180         QIcon icon_flash  = QIcon::fromTheme("filemanager_flash_file");
181         for (int idx = 0; idx < ct; ++idx) {
182             VlcBrowseElement dir = mContents->at(idx);
183             QListWidgetItem* item;
184             bool item_good = false;
185             if (0 == QString::compare("directory", dir.type)) {
186                 if (0 == QString::compare("..", dir.name)) {
187                     item = new QListWidgetItem(icon_up, dir.name, ui->listWidget, 0);
188                     item_good = true;
189                 }
190                 else {
191                     item = new QListWidgetItem(icon_folder, dir.name, ui->listWidget, 0);
192                     item_good = true;
193                 }
194             }
195             else if (0 == QString::compare("file", dir.type)) {
196                 if ( 0 == QString::compare(dir.extension, "jpg")  ||
197                      0 == QString::compare(dir.extension, "jpeg") ||
198                      0 == QString::compare(dir.extension, "gif")  ||
199                      0 == QString::compare(dir.extension, "png")  ||
200                      0 == QString::compare(dir.extension, "bmp")  ) {
201                     item_good = true;
202                     item = new QListWidgetItem(icon_image, dir.name, ui->listWidget, 0); // .jpg, .jpeg, .gif, .png, .bmp
203                 }
204                 else if ( 0 == QString::compare(dir.extension, "mp3")  ||
205                           0 == QString::compare(dir.extension, "m4a")  ||
206                           0 == QString::compare(dir.extension, "ogg")  ||
207                           0 == QString::compare(dir.extension, "oga")  ||
208                           0 == QString::compare(dir.extension, "wav")  ||
209                           0 == QString::compare(dir.extension, "flac")  ) {
210                     item_good = true;
211                     item = new QListWidgetItem(icon_audio, dir.name, ui->listWidget, 0); // .mp3, .m4a, .ogg, .oga, .wav, .flac
212                 }
213                 else if ( 0 == QString::compare(dir.extension, "avi")  ||
214                           0 == QString::compare(dir.extension, "mpeg") ||
215                           0 == QString::compare(dir.extension, "mpg")  ||
216                           0 == QString::compare(dir.extension, "mov")  ||
217                           0 == QString::compare(dir.extension, "mp4")  ||
218                           0 == QString::compare(dir.extension, "wmv")  ||
219                           0 == QString::compare(dir.extension, "mkv")  ||
220                           0 == QString::compare(dir.extension, "ogv")  ) {
221                     item_good = true;
222                     item = new QListWidgetItem(icon_video, dir.name, ui->listWidget, 0); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
223                 }
224                 else if ( 0 == QString::compare(dir.extension, "flv")  ) {
225                     item_good = true;
226                     item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0); // .flv
227                 }
228                 else {
229                     if (dir.name.startsWith("Flash")) {
230                         item_good = true;
231                         item = new QListWidgetItem(icon_flash, dir.name, ui->listWidget, 0);
232                     }
233                     else {
234                         item_good = false;
235                     }
236                 }
237             }
238             if (item_good) {
239                 ui->listWidget->addItem(item);
240             }
241             // other types ignored
242             //if (item) delete item;
243         }
244     }
245 }
246
247