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