Update new account dialog to have Port field.
[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->browseButton,SIGNAL(clicked()),mBrowserMainWindow,SLOT(show()));
80
81     connect(ui->playButton,SIGNAL(clicked()),this,SLOT(play()));
82     connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(stop()));
83     connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(pause()));
84     connect(ui->previousButton,SIGNAL(clicked()),this,SLOT(previous()));
85     connect(ui->nextButton,SIGNAL(clicked()),this,SLOT(next()));
86     connect(ui->fullscreenButton,SIGNAL(clicked()),this,SLOT(fullscreen()));
87     connect(ui->volUp,SIGNAL(clicked()),this,SLOT(volUp()));
88     connect(ui->volDown,SIGNAL(clicked()),this,SLOT(volDown()));
89     connect(ui->volMute,SIGNAL(clicked()),this,SLOT(volMute()));
90     connect(ui->slider,SIGNAL(sliderMoved(int)),this,SLOT(slide(int)));
91
92     init();
93
94 }
95 void PlayerMainWindow::init()
96 {
97     QSettings settings;
98     QString currentKey = settings.value("config/currentKey").toString();
99     mIp = settings.value("account/"+currentKey).toString();
100
101     if ( mIp.isEmpty())
102         showConfig();
103     else
104         mTimer->start(5000);
105 }
106
107 PlayerMainWindow::~PlayerMainWindow()
108 {
109     delete ui;
110 }
111
112 void PlayerMainWindow::changeEvent(QEvent *e)
113 {
114     QMainWindow::changeEvent(e);
115     switch (e->type()) {
116     case QEvent::LanguageChange:
117         ui->retranslateUi(this);
118         break;
119     default:
120         break;
121     }
122 }
123
124 void PlayerMainWindow::play()
125 {
126
127     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play")));
128
129 }
130 void PlayerMainWindow::stop()
131 {
132     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_stop")));
133
134 }
135 void PlayerMainWindow::pause()
136 {
137     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_pause")));
138
139 }
140 void PlayerMainWindow::previous()
141 {
142     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_previous")));
143
144 }
145 void PlayerMainWindow::next()
146 {
147     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_next")));
148
149 }
150 void PlayerMainWindow::fullscreen()
151 {
152     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=fullscreen")));
153
154 }
155 void PlayerMainWindow::volUp()
156 {
157     QUrl url = QUrl("http://"+mIp+"/requests/status.xml?command=volume");
158     url.addEncodedQueryItem(QByteArray("val"), QByteArray("%2B20"));
159     mNetManager->get(QNetworkRequest(url));
160 }
161 void PlayerMainWindow::volDown()
162 {
163
164     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=-20")));
165
166 }
167 void PlayerMainWindow::volMute()
168 {
169     this->mMuted = !this->mMuted;
170     if (this->mMuted) {
171         mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=0")));
172     }
173     else {
174         mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val="+QString::number(this->mVolume))));
175     }
176
177 }
178 void PlayerMainWindow::slide(int value)
179 {
180     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=seek&val="+QString::number(value)+"%25")));
181
182 }
183
184 void PlayerMainWindow::showConfig()
185 {
186     mTimer->stop();
187     AccountDialog * dialog = new AccountDialog;
188     dialog->exec();
189     init();
190
191
192 }
193 void PlayerMainWindow::showAbout()
194 {
195
196     AboutDialog * dialog = new AboutDialog;
197     dialog->exec();
198
199 }
200
201 void PlayerMainWindow::askStatus()
202 {
203
204     QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml")));
205     connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlStatus()));
206 }
207
208 void PlayerMainWindow::parseXmlStatus()
209 {
210     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
211     QDomDocument doc;
212     doc.setContent(reply->readAll());
213     QDomElement docElem = doc.documentElement();
214
215     int volume = docElem.namedItem("volume").toElement().text().toInt();
216     int length = docElem.namedItem("length").toElement().text().toInt();
217     int time = docElem.namedItem("time").toElement().text().toInt();
218     int position = docElem.namedItem("position").toElement().text().toInt();
219     QString state  =docElem.namedItem("state").toElement().text();
220
221
222     if (0 < volume) {
223         this->mVolume = volume;
224         this->mMuted = false;
225     }
226     else {
227         this->mMuted = true;
228     }
229
230
231     QTime timeLength(0,0,0) ;
232     timeLength =  timeLength.addSecs(time);
233
234     ui->timeLabel->setText(timeLength.toString("mm:ss"));
235
236
237     QDomNode infoNode =  docElem.namedItem("information");
238     QDomNode metaInfoNode =  infoNode.namedItem("meta-information");
239     QString title = metaInfoNode.namedItem("title").toElement().text();
240
241     if ( position >= 0 && position <=100)
242         ui->slider->setValue(position);
243
244     ui->label->setText(title);
245     delete reply;
246
247 }
248