add Yannux version with account manager
[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
40
41     ui->playlistButton->setIcon(QIcon::fromTheme("notes_bullets"));
42     ui->previousButton->setIcon(QIcon::fromTheme("pdf_viewer_first_page"));
43     ui->nextButton->setIcon(QIcon::fromTheme("pdf_viewer_last_page"));
44     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
45     ui->stopButton->setIcon(QIcon::fromTheme("camera_video_stop"));
46     ui->pauseButton->setIcon(QIcon::fromTheme("camera_video_pause"));
47     ui->fullscreenButton->setIcon(QIcon::fromTheme("general_fullsize"));
48     ui->volDown->setIcon(QIcon::fromTheme("statusarea_volumelevel1"));
49     ui->volUp->setIcon(QIcon::fromTheme("statusarea_volumelevel4"));
50
51 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
52     mPlayListMainWindow->setParent(this);
53     mPlayListMainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
54     mPlayListMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
55     mPlayListMainWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,true);
56     setAttribute(Qt::WA_Maemo5StackedWindow);
57     mPlayListMainWindow->setWindowFlags(mPlayListMainWindow->windowFlags() | Qt::Window);
58 #endif
59
60     connect(mTimer,SIGNAL(timeout()),this,SLOT(askStatus()));
61     connect(ui->actionConfiguration,SIGNAL(triggered()),this,SLOT(showConfig()));
62     connect(ui->actionAbout,SIGNAL(triggered()),this,SLOT(showAbout()));
63     connect(ui->playlistButton,SIGNAL(clicked()),mPlayListMainWindow,SLOT(show()));
64     connect(ui->playButton,SIGNAL(clicked()),this,SLOT(play()));
65     connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(stop()));
66     connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(pause()));
67     connect(ui->previousButton,SIGNAL(clicked()),this,SLOT(previous()));
68     connect(ui->nextButton,SIGNAL(clicked()),this,SLOT(next()));
69     connect(ui->fullscreenButton,SIGNAL(clicked()),this,SLOT(fullscreen()));
70     connect(ui->volUp,SIGNAL(clicked()),this,SLOT(volUp()));
71     connect(ui->volDown,SIGNAL(clicked()),this,SLOT(volDown()));
72     connect(ui->slider,SIGNAL(sliderMoved(int)),this,SLOT(slide(int)));
73
74     init();
75
76 }
77 void PlayerMainWindow::init()
78 {
79     QSettings settings;
80     QString currentKey = settings.value("config/currentKey").toString();
81     mIp = settings.value("account/"+currentKey).toString()+":8080";
82
83     if ( mIp.isEmpty())
84         showConfig();
85     else
86         mTimer->start(5000);
87 }
88
89 PlayerMainWindow::~PlayerMainWindow()
90 {
91     delete ui;
92 }
93
94 void PlayerMainWindow::changeEvent(QEvent *e)
95 {
96     QMainWindow::changeEvent(e);
97     switch (e->type()) {
98     case QEvent::LanguageChange:
99         ui->retranslateUi(this);
100         break;
101     default:
102         break;
103     }
104 }
105
106 void PlayerMainWindow::play()
107 {
108
109     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_play")));
110
111 }
112 void PlayerMainWindow::stop()
113 {
114     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_stop")));
115
116 }
117 void PlayerMainWindow::pause()
118 {
119     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_pause")));
120
121 }
122 void PlayerMainWindow::previous()
123 {
124     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_previous")));
125
126 }
127 void PlayerMainWindow::next()
128 {
129     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_next")));
130
131 }
132 void PlayerMainWindow::fullscreen()
133 {
134     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=fullscreen")));
135
136 }
137 void PlayerMainWindow::volUp()
138 {
139     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=500")));
140
141 }
142 void PlayerMainWindow::volDown()
143 {
144
145     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=volume&val=-20")));
146
147 }
148 void PlayerMainWindow::slide(int value)
149 {
150     mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=seek&val="+QString::number(value)+"%25")));
151
152 }
153
154 void PlayerMainWindow::showConfig()
155 {
156     mTimer->stop();
157     AccountDialog * dialog = new AccountDialog;
158     dialog->exec();
159     init();
160
161
162 }
163 void PlayerMainWindow::showAbout()
164 {
165
166     AboutDialog * dialog = new AboutDialog;
167     dialog->exec();
168
169 }
170
171 void PlayerMainWindow::askStatus()
172 {
173
174     QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml")));
175     connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlStatus()));
176 }
177
178 void PlayerMainWindow::parseXmlStatus()
179 {
180     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
181     QDomDocument doc;
182     doc.setContent(reply->readAll());
183     QDomElement docElem = doc.documentElement();
184
185     int volume = docElem.namedItem("volume").toElement().text().toInt();
186     int length = docElem.namedItem("length").toElement().text().toInt();
187     int time = docElem.namedItem("time").toElement().text().toInt();
188     int position = docElem.namedItem("position").toElement().text().toInt();
189     QString state  =docElem.namedItem("state").toElement().text();
190
191     QTime timeLength(0,0,0) ;
192     timeLength =  timeLength.addSecs(time);
193
194     ui->timeLabel->setText(timeLength.toString("mm:ss"));
195
196
197     QDomNode infoNode =  docElem.namedItem("information");
198     QDomNode metaInfoNode =  infoNode.namedItem("meta-information");
199     QString title = metaInfoNode.namedItem("title").toElement().text();
200
201     if ( position >= 0 && position <=100)
202         ui->slider->setValue(position);
203
204     ui->label->setText(title);
205     delete reply;
206
207 }
208