/
mainwindow.cpp
294 lines (247 loc) · 8.66 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDesktopServices>
#include <QTime>
#include <QWebFrame>
#include <QGridLayout>
#include <QPushButton>
#include <QInputDialog>
#include <QNetworkCookieJar>
#include <QNetworkCookie>
MainWindow::MainWindow(QWidget *parent)
{
/*QXmppLogger::getLogger()->setLoggingType(QXmppLogger::FILE);
bool ok;
QString server= QInputDialog::getText(this, "Enter jabber server address", "jid",
QLineEdit::Normal, "talk.google.com", &ok);
QString jid = QInputDialog::getText(this, "Enter jabber username (user@example.com)", "jid",
QLineEdit::Normal, "rj@metabrew.com", &ok);
QString pass = QInputDialog::getText(this, "Enter jabber password", "pass",
QLineEdit::Normal, "", &ok);
jabber = new Jabber();
jabber->connectToServer(server, jid, pass);
*/
setParent(parent);
source = 0;
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
mediaObject = new Phonon::MediaObject(this);
metaInformationResolver = new Phonon::MediaObject(this);
mediaObject->setTickInterval(1000);
connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
this, SLOT(stateChanged(Phonon::State, Phonon::State)));
connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
this, SLOT(metaStateChanged(Phonon::State, Phonon::State)));
connect(mediaObject, SIGNAL(currentSourceChanged(const Phonon::MediaSource &)),
this, SLOT(sourceChanged(const Phonon::MediaSource &)));
connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
connect(mediaObject, SIGNAL(bufferStatus(int)), this, SLOT(bufferPercent(int)));
Phonon::createPath(mediaObject, audioOutput);
setupUi();
webkitApi = new WebkitApi(this);
webkitApi->setWebView(webView);
connect(webkitApi, SIGNAL(pauseToggled()), this, SLOT(togglePause()));
connect(webkitApi, SIGNAL(volumeChanged(int)), this, SLOT(setVolume(int)));
connect(webkitApi, SIGNAL(playRequested(QString)), this, SLOT(play(QString)));
connect(webkitApi, SIGNAL(stopRequested()), this, SLOT(stop()));
connect(audioOutput, SIGNAL(volumeChanged(qreal)), this, SLOT(volumeChanged(qreal)));
QString url("./www/player/index.html");
webView->load(QUrl(url));
}
MainWindow::~MainWindow()
{
}
void MainWindow::setupUi()
{
QGridLayout *layout = new QGridLayout();
webView = new QWebView();
webView->setPage(new Webpage);
webView->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,true); // needs QT >= 4.6
// QNetworkCookieJar jar;
//QList<QNetworkCookie> cookies;
//cookies.append( QNetworkCookie("auth","XXX") );
//jar.setCookiesFromUrl(cookies, QUrl(""))
//->page->networkAccessManager()->setCookieJar( new QNetworkCookieJar() );
playurlBtn = new QPushButton("Play Stream");
playfileBtn = new QPushButton("Play Local File");
reloadBtn = new QPushButton("Reload");
layout->addWidget(webView, 0, 0, 1, 3);
/*
layout->addWidget(playurlBtn, 1, 0);
layout->addWidget(playfileBtn, 1, 1);
layout->addWidget(reloadBtn, 1, 2);
*/
//layout->setSpacing(0);
layout->setContentsMargins(QMargins(0,0,0,0));
setLayout(layout);
show();
connect(playurlBtn, SIGNAL(clicked()), this, SLOT(on_streambutton_clicked()));
connect(playfileBtn, SIGNAL(clicked()), this, SLOT(on_playbutton_clicked()));
connect(reloadBtn, SIGNAL(clicked()), this, SLOT(on_reloadbutton_clicked()));
}
void MainWindow::volumeChanged(qreal vr)
{
int v = (int) vr*100;
webkitApi->emitVolumeChanged(v);
}
void MainWindow::setVolume(int v)
{
if((int)audioOutput->volume() == v) return;
qDebug() << "Request to set volume to " << v;
audioOutput->setVolume((qreal)v/100);
}
void MainWindow::togglePause()
{
switch( mediaObject->state() )
{
case Phonon::PausedState:
mediaObject->play();
break;
case Phonon::PlayingState: // fallthrus - states that we are allowed to pause
case Phonon::LoadingState:
case Phonon::BufferingState:
mediaObject->pause();
break;
default: ;
}
}
void MainWindow::play(QString s)
{
play(QUrl(s));
}
void MainWindow::play(QUrl url)
{
mediaObject->stop();
mediaObject->clearQueue();
if(source) free(source);
if( (source = new Phonon::MediaSource(url)))
{
webkitApi->emitStateChange("loading");
metaInformationResolver->setCurrentSource(*source);
mediaObject->setCurrentSource(url);
mediaObject->play();
} else {
log("Error: Failed to create source");
webkitApi->emitStateChange("fatalerror");
}
}
void MainWindow::stop()
{
mediaObject->stop();
}
void MainWindow::bufferPercent(int pc)
{
qDebug() << "BufferPercent: " << pc;
// perhaps some logic that decides if we should abort due to slowness?
webkitApi->emitBufferPercent(pc);
}
void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
{
switch (newState) {
case Phonon::ErrorState:
if (mediaObject->errorType() == Phonon::FatalError) {
webkitApi->emitStateChange("fatalerror");
log("StateChanged: Fatal error: " + mediaObject->errorString());
} else {
webkitApi->emitStateChange("error");
log("StateChanged: error: " + mediaObject->errorString());
}
break;
case Phonon::PlayingState:
webkitApi->emitStateChange("playing");
break;
case Phonon::StoppedState:
webkitApi->emitStateChange("stopped");
break;
case Phonon::PausedState:
webkitApi->emitStateChange("paused");
break;
case Phonon::BufferingState:
webkitApi->emitStateChange("buffering");
break;
case Phonon::LoadingState:
webkitApi->emitStateChange("loading");
break;
default:
webkitApi->emitStateChange("unhandledstate");
;
}
}
void MainWindow::sourceChanged(const Phonon::MediaSource & source)
{
qDebug() << "Source changed: " + source.url().toString();
// musicTable->selectRow(sources.indexOf(source));
// timeLcd->display("00:00");
}
void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State /* oldState */)
{
qDebug() << "Metastate changed to " << newState;
if (newState == Phonon::ErrorState) {
log("ERROR opening files: " + metaInformationResolver->errorString());
//QMessageBox::warning(this, tr("Error opening files"),
// metaInformationResolver->errorString());
return;
}
if (newState != Phonon::StoppedState && newState != Phonon::PausedState)
return;
if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid)
return;
}
void MainWindow::tick(qint64 time)
{
//QTime displayTime(0, (int)((time / 60000) % 60), (int)((time / 1000) % 60), 0);
//QString prog = displayTime.toString("mm:ss");
//setWindowTitle(prog);
qDebug() << time << " /// " << mediaObject->remainingTime();
webkitApi->emitTick(time, mediaObject->remainingTime());
//int secs = time / 1000;
//jseval(QString("Musickit.tick(%1); null").arg(secs));
}
void MainWindow::aboutToFinish()
{
}
void MainWindow::log(QString str)
{
qDebug() << str;
//QString js("mk_log(\"" + str.replace("\"","\\\"") + "\"); null");
//jseval(js);
}
void MainWindow::jseval(QString str)
{
qDebug() << str ;
webView->page()->currentFrame()->evaluateJavaScript(str);
}
void MainWindow::changeEvent(QEvent * /* e */)
{
/*QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
//retranslateUi(this);
break;
default:
break;
}*/
}
void MainWindow::on_playbutton_clicked()
{
QString file = QFileDialog::getOpenFileName(this, "Select Audio File", QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
if(!file.isEmpty())
{
play( QUrl::fromLocalFile("file://"+file) );
}
}
void MainWindow::on_streambutton_clicked()
{
bool ok;
QString url = QInputDialog::getText(this, "Enter Stream URL", "URL:",
QLineEdit::Normal, "http://www.playdar.org/hiding.mp3", &ok);
if(ok)
{
play(QUrl(url));
}
}
void MainWindow::on_reloadbutton_clicked()
{
webView->reload();
}