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