Improved encoding support for browsing and adding.
[vlc-remote] / playermainwindow.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 <QDebug>
19   #include <QTime>
20   #include "playermainwindow.h"
21   #include "ui_playermainwindow.h"
22   #include "configdialog.h"
23   #include "aboutdialog.h"
24   #include "accountdialog.h"
25
26
27   PlayerMainWindow::PlayerMainWindow(QWidget *parent) :
28           QMainWindow(parent),
29           ui(new Ui::PlayerMainWindow)
30   {
31       ui->setupUi(this);
32       setWindowTitle("Vlc remote");
33
34
35
36       mTimer = new QTimer(this);
37       mNetManager = new QNetworkAccessManager(this);
38       mPlayListMainWindow = new PlayListMainWindow;
39       mBrowserMainWindow = new BrowseMainWindow;
40
41       mVolume = 100;
42       mMuted = false;
43
44       ui->playlistButton->setIcon(QIcon::fromTheme("notes_bullets"));
45       ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
46
47       ui->previousButton->setIcon(QIcon::fromTheme("pdf_viewer_first_page"));
48       ui->nextButton->setIcon(QIcon::fromTheme("pdf_viewer_last_page"));
49       ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
50       ui->stopButton->setIcon(QIcon::fromTheme("camera_video_stop"));
51       ui->pauseButton->setIcon(QIcon::fromTheme("camera_video_pause"));
52       ui->fullscreenButton->setIcon(QIcon::fromTheme("general_fullsize"));
53       ui->volDown->setIcon(QIcon::fromTheme("statusarea_volumelevel1"));
54       ui->volUp->setIcon(QIcon::fromTheme("statusarea_volumelevel4"));
55       ui->volMute->setIcon(QIcon::fromTheme("statusarea_volume_mute"));
56
57
58   #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
59       mPlayListMainWindow->setParent(this);
60       mPlayListMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
61       mPlayListMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
62       mPlayListMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
63       setAttribute(Qt::WA_Maemo5StackedWindow);
64       mPlayListMainWindow->setWindowFlags(mPlayListMainWindow->windowFlags() | Qt::Window);
65
66       mBrowserMainWindow->setParent(this);
67       mBrowserMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
68       mBrowserMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
69       mBrowserMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
70       setAttribute(Qt::WA_Maemo5StackedWindow);
71       mBrowserMainWindow->setWindowFlags(mBrowserMainWindow->windowFlags() | Qt::Window);
72
73   #endif
74
75       connect(mTimer,SIGNAL(timeout()),this,SLOT(askStatus()));
76       connect(ui->actionConfiguration,SIGNAL(triggered()),this,SLOT(showConfig()));
77       connect(ui->actionAbout,SIGNAL(triggered()),this,SLOT(showAbout()));
78       connect(ui->playlistButton,SIGNAL(clicked()),mPlayListMainWindow,SLOT(show()));
79       connect(ui->playlistButton,SIGNAL(clicked()),mPlayListMainWindow,SLOT(showPlayList()));
80       connect(ui->browseButton,SIGNAL(clicked()),mBrowserMainWindow,SLOT(show()));
81       connect(ui->browseButton,SIGNAL(clicked()),mBrowserMainWindow,SLOT(showCurrentDirectory()));
82
83       connect(ui->playButton,SIGNAL(clicked()),this,SLOT(play()));
84       connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(stop()));
85       connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(pause()));
86       connect(ui->previousButton,SIGNAL(clicked()),this,SLOT(previous()));
87       connect(ui->nextButton,SIGNAL(clicked()),this,SLOT(next()));
88       connect(ui->fullscreenButton,SIGNAL(clicked()),this,SLOT(fullscreen()));
89       connect(ui->volUp,SIGNAL(clicked()),this,SLOT(volUp()));
90       connect(ui->volDown,SIGNAL(clicked()),this,SLOT(volDown()));
91       connect(ui->volMute,SIGNAL(clicked()),this,SLOT(volMute()));
92       connect(ui->slider,SIGNAL(sliderMoved(int)),this,SLOT(slide(int)));
93
94              showConfig();
95
96
97   }
98   
99
100   PlayerMainWindow::~PlayerMainWindow()
101   {
102       delete ui;
103   }
104
105   void PlayerMainWindow::changeEvent(QEvent *e)
106   {
107       QMainWindow::changeEvent(e);
108       switch (e->type()) {
109       case QEvent::LanguageChange:
110           ui->retranslateUi(this);
111           break;
112       default:
113           break;
114       }
115   }
116
117   void PlayerMainWindow::play()
118   {
119       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play")));
120   }
121   void PlayerMainWindow::stop()
122   {
123       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_stop")));
124   }
125   void PlayerMainWindow::pause()
126   {
127       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_pause")));
128   }
129   void PlayerMainWindow::previous()
130   {
131       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_previous")));
132   }
133   void PlayerMainWindow::next()
134   {
135       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_next")));
136   }
137   void PlayerMainWindow::fullscreen()
138   {
139       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=fullscreen")));
140   }
141   void PlayerMainWindow::volUp()
142   {
143       QUrl url = QUrl("http://"+mIp+"/requests/status.xml?command=volume");
144       url.addEncodedQueryItem(QByteArray("val"), QByteArray("%2B20"));
145       mNetManager->get(QNetworkRequest(url));
146   }
147   void PlayerMainWindow::volDown()
148   {
149       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=-20")));
150   }
151   void PlayerMainWindow::volMute()
152   {
153       this->mMuted = !this->mMuted;
154       if (this->mMuted) {
155           mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=0")));
156       }
157       else {
158           mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val="+QString::number(this->mVolume))));
159       }
160   }
161   void PlayerMainWindow::slide(int value)
162   {
163       mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=seek&val="+QString::number(value)+"%25")));
164   }
165
166   void PlayerMainWindow::showConfig()
167   {
168       mTimer->stop();
169       AccountDialog * dialog = new AccountDialog;
170       dialog->exec();
171      
172        mIp= AccountDialog::currentIp();
173
174       mPlayListMainWindow->init();
175       mBrowserMainWindow->init();
176       mTimer->start(5000);
177       askStatus();
178   }
179   void PlayerMainWindow::showAbout()
180   {
181
182       AboutDialog * dialog = new AboutDialog;
183       dialog->exec();
184
185   }
186
187   void PlayerMainWindow::askStatus()
188   {
189
190       QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml")));
191       connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlStatus()));
192   }
193
194   void PlayerMainWindow::parseXmlStatus()
195   {
196       QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
197       QDomDocument doc;
198       doc.setContent(reply->readAll());
199       QDomElement docElem = doc.documentElement();
200
201       int volume = docElem.namedItem("volume").toElement().text().toInt();
202       int length = docElem.namedItem("length").toElement().text().toInt();
203       int time = docElem.namedItem("time").toElement().text().toInt();
204       int position = docElem.namedItem("position").toElement().text().toInt();
205       QString state  =docElem.namedItem("state").toElement().text();
206
207
208       if (0 < volume) {
209           this->mVolume = volume;
210           this->mMuted = false;
211       }
212       else {
213           this->mMuted = true;
214       }
215
216
217       QTime timeLength(0,0,0) ;
218       timeLength =  timeLength.addSecs(time);
219
220       ui->timeLabel->setText(timeLength.toString("h:mm:ss"));
221
222
223       QDomNode infoNode =  docElem.namedItem("information");
224       QDomNode metaInfoNode =  infoNode.namedItem("meta-information");
225       QString title = metaInfoNode.namedItem("title").toElement().text().replace("\\\\", "\\");
226
227       if ( position >= 0 && position <=100)
228           ui->slider->setValue(position);
229
230       ui->label->setText(title);
231       delete reply;
232
233   }
234