Update new account dialog to have Port field.
[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     mNetManager = new QNetworkAccessManager(this);
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     //ui->listWidget->setHorizontalScrollMode(QListWidget::ScrollMode::ScrollPerItem);
50     //ui->listWidget->setHorizontalScrollHint(QListWidget::ScrollHint::PositionAtTop);
51
52     connect(ui->browseButton,SIGNAL(clicked()),this,SLOT(onBrowse()));
53     connect(ui->addButton,SIGNAL(clicked()),this,SLOT(onAddToPlaylist()));
54     connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
55     connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
56
57     this->browseDirectory(mCurrentDir);
58 }
59
60 BrowseMainWindow::~BrowseMainWindow()
61 {
62     delete ui;
63 }
64
65 void BrowseMainWindow::changeEvent(QEvent *e)
66 {
67     QMainWindow::changeEvent(e);
68     switch (e->type()) {
69     case QEvent::LanguageChange:
70         ui->retranslateUi(this);
71         break;
72     default:
73         break;
74     }
75 }
76
77 void BrowseMainWindow::onListSelectionChanged() {
78     QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
79     if (0 < items.count()) {
80         mCurrentElement = getElementFromText(items.at(0)->text());
81         // are we up dir?
82         if (0 == QString::compare("..", mCurrentElement.name)) {
83             ui->browseButton->setDisabled(true);
84             ui->playButton->setDisabled(true);
85             ui->addButton->setDisabled(true);
86             browseDirectory(mCurrentElement.path);
87         }
88         else {
89             // can we browse?
90             if (0 == QString::compare("directory", mCurrentElement.type)) {
91                 ui->browseButton->setDisabled(false);
92             }
93             else {
94                 ui->browseButton->setDisabled(true);
95             }
96             // can we play?
97             ui->playButton->setDisabled(false);
98             // can we playlist?
99             ui->addButton->setDisabled(false);
100         }
101     }
102 }
103
104 VlcBrowseElement BrowseMainWindow::getElementFromText(QString text) {
105     //if (0 != QString::compare("", text)) {
106     for (int idx = 0; idx < mContents->count(); ++idx) {
107         if (0 == QString::compare(text, mContents->at(idx).name)) {
108             return mContents->at(idx);
109         }
110     }
111     //}
112     return *(new VlcBrowseElement());
113 }
114
115 void BrowseMainWindow::onBrowse() {
116     // check for directory
117     if (0 == QString::compare("directory", mCurrentElement.type)) {
118         // call browseDirectory
119         this->browseDirectory(mCurrentElement.path);
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     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
138     QDomDocument doc;
139     doc.setContent(reply->readAll());
140     QDomElement docElem = doc.documentElement();
141     QDomNodeList elements = docElem.elementsByTagName("element");
142     mContents = new QList<VlcBrowseElement>();
143     if (0 < elements.count()) {
144         int idx = 0;
145         do {
146             QDomNode node = elements.at(idx);
147             VlcBrowseElement* dir = new VlcBrowseElement();
148             dir->type = node.attributes().namedItem("type").nodeValue();
149             dir->size = node.attributes().namedItem("size").nodeValue().toInt();
150             dir->date = QDate::fromString(node.attributes().namedItem("date").nodeValue());
151             dir->path = node.attributes().namedItem("path").nodeValue();
152             dir->name = node.attributes().namedItem("name").nodeValue();
153             dir->extension = node.attributes().namedItem("extension").nodeValue();
154             ++idx;
155             this->mContents->append(*dir);
156         } while (idx < elements.count());
157     }
158     delete reply;
159
160     // Update UI
161     this->updateList();
162 }
163
164 void BrowseMainWindow::updateList() {
165     int ct = this->mContents->count();
166     if (0 < ct) {
167         for (int idx = 0; idx < ct; ++idx) {
168             VlcBrowseElement dir = mContents->at(idx);
169             QListWidgetItem* item;
170             if (0 == QString::compare("directory", dir.type)) {
171                 if (0 == QString::compare("..", dir.name)) {
172                     item = new QListWidgetItem(QIcon::fromTheme("filemanager_folder_up"), dir.name, ui->listWidget, 0);
173                 }
174                 else {
175                     item = new QListWidgetItem(QIcon::fromTheme("general_folder"), dir.name, ui->listWidget, 0);
176                 }
177                 ui->listWidget->addItem(item);
178             }
179             else if (0 == QString::compare("file", dir.type)) {
180                 if ( 0 == QString::compare(dir.extension, "jpg")  ||
181                      0 == QString::compare(dir.extension, "jpeg") ||
182                      0 == QString::compare(dir.extension, "gif")  ||
183                      0 == QString::compare(dir.extension, "png")  ||
184                      0 == QString::compare(dir.extension, "bmp")  ) {
185                     item = new QListWidgetItem(QIcon::fromTheme("general_image"), dir.name, ui->listWidget, 0); // .jpg, .jpeg, .gif, .png, .bmp
186                 }
187                 else if ( 0 == QString::compare(dir.extension, "mp3")  ||
188                           0 == QString::compare(dir.extension, "m4a") ||
189                           0 == QString::compare(dir.extension, "ogg")  ||
190                           0 == QString::compare(dir.extension, "oga")  ||
191                           0 == QString::compare(dir.extension, "wav")  ||
192                           0 == QString::compare(dir.extension, "flac")  ) {
193                     item = new QListWidgetItem(QIcon::fromTheme("general_audio_file"), dir.name, ui->listWidget, 0); // .mp3, .m4a, .ogg, .oga, .wav, .flac
194                 }
195                 else if ( 0 == QString::compare(dir.extension, "flv")   ||
196                           0 == QString::compare(dir.extension, "avi")  ||
197                           0 == QString::compare(dir.extension, "mpeg") ||
198                           0 == QString::compare(dir.extension, "mov")  ||
199                           0 == QString::compare(dir.extension, "mp4")  ||
200                           0 == QString::compare(dir.extension, "wmv")  ||
201                           0 == QString::compare(dir.extension, "mkv")  ||
202                           0 == QString::compare(dir.extension, "ogv")  ) {
203                     item = new QListWidgetItem(QIcon::fromTheme("general_video_file"), dir.name, ui->listWidget, 0); // .flv, .avi, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
204                 }
205                 else {
206                     if (dir.name.startsWith("Flash")) {
207                         item = new QListWidgetItem(QIcon::fromTheme("general_video_file"), dir.name, ui->listWidget, 0);
208                     }
209                     else {
210                         item = new QListWidgetItem(QIcon::fromTheme("filemanager_unknown_file"), dir.name, ui->listWidget, 0);
211                     }
212                 }
213                 ui->listWidget->addItem(item);
214             }
215             // other types ignored
216             //if (item) delete item;
217         }
218     }
219 }
220
221