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