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