Skip to content

Commit 28c8981

Browse files
committed
Update for API changes, Also added keepalive heartbeat and a settings
window.
1 parent a010cdd commit 28c8981

9 files changed

+214
-44
lines changed

groove.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,17 @@ groove::groove(QWidget *parent) :
116116
connect(bBar,SIGNAL(settings()),settingsWin,SLOT(show()));
117117

118118
connect(bBar,SIGNAL(back()),player,SLOT(back()));
119-
connect(bBar,SIGNAL(settings()),settingsWin,SLOT(show()));
119+
stack->addWidget(settingsWin);
120+
connect(bBar,SIGNAL(settings()),this,SLOT(toggleSettings()));
121+
connect(settingsWin,SIGNAL(closed()),this,SLOT(toggleSettings()));
120122
bBar->setPlaybackProgress(100,100);
121123
pwindow = new pWin();
122124
stack->addWidget(pwindow);
123125
stack->setCurrentWidget(resultView);
124126
connect(bBar,SIGNAL(list()),this,SLOT(togglePlaylist()));
125127
pwindow->setModel(pl);
128+
connect(settingsWin,SIGNAL(settingsChange(QString,QVariant)),pl,SLOT(setSettings(QString,QVariant)));
129+
settingsWin->populate();
126130

127131
}
128132
void groove::showSettings(){
@@ -131,6 +135,14 @@ void groove::showSettings(){
131135
#endif
132136
}
133137

138+
void groove::toggleSettings()
139+
{
140+
if(stack->currentWidget()==settingsWin)
141+
stack->setCurrentWidget(resultView);
142+
else
143+
stack->setCurrentWidget(settingsWin);
144+
}
145+
134146
void groove::togglePlaylist()
135147
{
136148
if(stack->currentWidget()==pwindow)

groove.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ private slots:
4040
void addSongPlaylist();
4141
void showOthers();
4242
void togglePlaylist();
43+
void toggleSettings();
4344
void showSettings();
4445
private:
4546
void resizeEvent(QResizeEvent *);

gscom.cpp

+51-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
#include <parser.h>
33
#include <serializer.h>
44
#include <QCryptographicHash>
5+
#include <QTimer>
6+
57
//#include <QApplication>
6-
#define CVERSION "20100831"
8+
//#define CVERSION "20100831"
9+
#define CVERSION "20101222"
710
#define CLIENT "htmlshark"
811

912
gscom::gscom()
1013
{
1114
manager = new QNetworkAccessManager(this);
15+
QTimer *timer = new QTimer(this);
1216
connect(manager, SIGNAL(finished(QNetworkReply*)),
1317
this, SLOT(replyFinished(QNetworkReply*)));
1418
cookies = manager->cookieJar();
@@ -21,6 +25,8 @@ gscom::gscom()
2125
model->setHorizontalHeaderLabels(
2226
QStringList() << "Online");
2327
addDebugMsg("You may now search for a song");
28+
connect(timer,SIGNAL(timeout()),this,SLOT(refreshsession()));
29+
timer->start(60000); //default to one min
2430
//addProgressbar("test");
2531
//getSessionKey();
2632
}
@@ -42,7 +48,7 @@ QStandardItemModel* gscom::getSongModel(QString song)
4248
QString *token = getToken(getSearchResults);
4349
qDebug() << token->toAscii();
4450
QNetworkRequest request;
45-
request.setUrl(QUrl("http://listen.grooveshark.com/more.php?getSearchResults"));
51+
request.setUrl(QUrl("http://grooveshark.com/more.php?getSearchResults"));
4652
request.setHeader(request.ContentTypeHeader,QVariant("application/json"));
4753
QVariantMap jlist;
4854
QVariantMap header;
@@ -84,7 +90,7 @@ QStandardItemModel* gscom::getArtistModel(QString song)
8490
QString *token = getToken(getSearchResults);
8591
qDebug() << token->toAscii();
8692
QNetworkRequest request;
87-
request.setUrl(QUrl("http://listen.grooveshark.com/more.php?getSearchResults"));
93+
request.setUrl(QUrl("http://grooveshark.com/more.php?getSearchResults"));
8894
request.setHeader(request.ContentTypeHeader,QVariant("application/json"));
8995
QVariantMap jlist;
9096
QVariantMap header;
@@ -147,6 +153,15 @@ QStandardItemModel* gscom::getAlbumModel(QString song)
147153

148154
return model;
149155
}
156+
void gscom::refreshsession()
157+
{
158+
if(this->currentaction==none)
159+
{
160+
currentaction=refresh;
161+
manager->get(QNetworkRequest(QUrl(GS_KEEPALIVE)));
162+
}
163+
}
164+
150165
void gscom::replyFinished(QNetworkReply *reply)
151166
{
152167
switch (currentaction)
@@ -237,7 +252,9 @@ void gscom::replyFinished(QNetworkReply *reply)
237252
{
238253
QJson::Parser parser;
239254
bool ok;
240-
QVariantMap result = parser.parse (reply->readAll(), &ok).toMap();
255+
QByteArray array = reply->readAll();
256+
//qDebug(array);
257+
QVariantMap result = parser.parse (array, &ok).toMap();
241258
if (!ok) {
242259
qFatal("An error occurred during parsing");
243260
return;
@@ -271,6 +288,33 @@ void gscom::replyFinished(QNetworkReply *reply)
271288
emit finishedSearch();
272289
}
273290
break;
291+
case refresh:
292+
{
293+
qDebug() << "Got refresh reply";
294+
reply->readAll();
295+
reply->close();
296+
currentaction = none;
297+
/*
298+
QList<QNetworkCookie> cookieList = cookies->cookiesForUrl(QUrl(QString("http://") + GS_LISTEN));
299+
foreach(QNetworkCookie cookie, cookieList)
300+
{
301+
if(cookie.name() == "PHPSESSID")
302+
{
303+
if(phpSession!=cookie.value())
304+
{
305+
delete phpSession;
306+
phpSession = new QString(cookie.value());
307+
qDebug() << QDateTime::currentDateTime();
308+
qDebug() << cookie.expirationDate();
309+
currentaction = getCommunicationToken;
310+
getSessionKey();
311+
continue;
312+
}
313+
}
314+
}
315+
*/
316+
}
317+
break;
274318
default:
275319
qDebug() << reply->readAll();
276320
reply->close();
@@ -364,7 +408,7 @@ void gscom::getSong(QString songid)
364408
QString *token = getToken(getStreamKeyFromSongIDEx);
365409
qDebug() << token->toAscii();
366410
QNetworkRequest request;
367-
request.setUrl(QUrl("http://listen.grooveshark.com/more.php?getStreamKeyFromSongIDEx"));
411+
request.setUrl(QUrl("http://grooveshark.com/more.php?getStreamKeyFromSongIDEx"));
368412
request.setHeader(request.ContentTypeHeader,QVariant("application/json"));
369413
QVariantMap jlist;
370414
QVariantMap header;
@@ -399,7 +443,8 @@ void gscom::getSong(QString songid)
399443
void gscom::getSessionKey()
400444
{
401445
QNetworkRequest request; // = new QNetworkRequest(QUrl("https://listen.grooveshark.com/service.php"));
402-
request.setUrl(QUrl("https://listen.grooveshark.com/more.php"));
446+
//request.setUrl(QUrl("https://listen.grooveshark.com/more.php"));
447+
request.setUrl(QUrl("https://grooveshark.com/more.php?getCommunicationToken"));
403448
request.setHeader(request.ContentTypeHeader,QVariant("application/json"));
404449
QVariantMap jlist;
405450
QVariantMap header;

gscom.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
//#include <parser.h>
77
#define GSPASS "**********"
88
#define GSIP "grooveshark.com"
9-
#define GS_LISTEN "listen.grooveshark.com"
9+
#define GS_LISTEN "grooveshark.com"
10+
#define GS_KEEPALIVE "http://grooveshark.com/sidebar.php?ThemeID=166&0=1"
1011
#define GS_GUTS "guts.grroveshark.com"
1112
class gscom : public QObject
1213
{
@@ -39,6 +40,7 @@ public slots:
3940
getStreamKeyFromSongIDEx,
4041
getPHP,
4142
getTokenForForSong,
43+
refresh,
4244
none
4345
};
4446
gMETHOD currentaction;
@@ -54,6 +56,8 @@ public slots:
5456
void addProgressbar(QString debug);
5557
bool firstR;
5658
QByteArray *postdata;
59+
private slots:
60+
void refreshsession();
5761
};
5862

5963
#endif // GSCOM_H

playlist.cpp

+43-5
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ playlist::playlist(QObject *parent) :
1111
this->reply = NULL;
1212
invalid = new QVariant();
1313
icon = new QVariant(QIcon(":/groove/icons/general_forward.png"));
14+
this->bufferpcnt = 0.05;
15+
this->prefetch = 3;
1416
}
1517

1618
//Implemented model class information
1719
QVariant playlist::data(const QModelIndex &index, int role) const
1820
{
1921

2022
playlist* play = (playlist *)index.model();
21-
/*QVariant dat = *play->invalid;
23+
QVariant dat = *play->invalid;
2224
if(play->existAt(index.row()))
2325
{
2426
if (!index.isValid())
@@ -82,8 +84,8 @@ QVariant playlist::data(const QModelIndex &index, int role) const
8284
}
8385
else
8486
dat = *play->invalid;
85-
return dat;*/
86-
return *play->invalid;
87+
return dat;
88+
//return *play->invalid;
8789
}
8890
int playlist::rowCount(const QModelIndex &) const
8991
{
@@ -143,7 +145,20 @@ void playlist::setCurrentPlaying(int position)
143145
{
144146
this->currentplayingitem = position;
145147
if(!pList->at(position)->downloaded && this->currentdownloaditem != this->currentplayingitem)
148+
{
146149
this->beginDownload(position);
150+
}
151+
else
152+
{
153+
for(int i = 0; i < prefetch;i++)
154+
{
155+
if(this->existAt(position+1+i) && !pList->at(position+1+i)->downloaded)
156+
{
157+
this->beginDownload(position+1+i);
158+
continue;
159+
}
160+
}
161+
}
147162
/*if(pList->at(position)->bufferready == false &&)
148163
{
149164
if(!pList->at(position)->downloaded)
@@ -168,6 +183,24 @@ QIODevice * playlist::getBuffer(int position)
168183
{
169184
return pList->at(position)->buffer;
170185
}
186+
void playlist::setSettings(QString key, QVariant Val)
187+
{
188+
if(key=="buffer-pcnt")
189+
this->bufferpcnt = Val.toInt()/100.0;
190+
if(key=="playlist-precache")
191+
{
192+
this->prefetch = Val.toInt();
193+
//when we change precahce never delete and make sure the setting takes effect
194+
if(this->currentdownloaditem!=this->currentplayingitem)
195+
for(int i = 0; i < this->prefetch;i++)
196+
if(this->existAt(this->currentplayingitem+1+i) && !pList->at(this->currentplayingitem+1+i)->downloaded)
197+
{
198+
this->beginDownload(this->currentplayingitem+1+i);
199+
continue;
200+
}
201+
}
202+
203+
}
171204

172205
void playlist::beginDownload(int position)
173206
{
@@ -239,10 +272,15 @@ int playlist::addSong(QStandardItem *item, QString name)
239272
void playlist::downloadDone(int position)
240273
{
241274
if(this->existAt(position+1) && this->currentSkeyItem == -1 && !pList->at(position+1)->downloaded && this->currentdownloaditem != position+1)
242-
beginDownload(position+1);
275+
{
276+
if(((position+1) - this->currentplaying())<=this->prefetch)
277+
beginDownload(position+1);
278+
}
243279
else
244280
this->currentdownloaditem = -1;
245281
pList->at(position)->downloaded = true;
282+
QModelIndex changed = this->createIndex(position,this->sName,0);
283+
emit this->dataChanged(changed,changed);
246284
}
247285
void playlist::networkReplyFinish()
248286
{
@@ -277,7 +315,7 @@ void playlist::downloadSlot(qint64 b, qint64 t)
277315
{
278316
pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
279317
//qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
280-
if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
318+
if ( b >= t*bufferpcnt && !pList->at(this->currentdownloaditem)->bufferready)
281319
//if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
282320
{
283321
this->setBufferRdy(this->currentdownloaditem);

playlist.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class playlist : public QAbstractTableModel
5252
bool played;
5353
bool bufferready;
5454
};
55+
int prefetch;
5556
QList<songElement *>* getList();
5657
explicit playlist(QObject *parent = 0);
5758
int addSong(QStandardItem *item,QString name);
@@ -85,6 +86,7 @@ class playlist : public QAbstractTableModel
8586
void rowsInserted ( const QModelIndex & parent, int start, int end );
8687

8788
public slots:
89+
void setSettings(QString key, QVariant Val);
8890
private slots:
8991
void downloadSlot(qint64 d, qint64 t);
9092
void networkReplyFinish();
@@ -93,11 +95,6 @@ private slots:
9395
void setBufferRdy(int b);
9496
void getNError(QNetworkReply::NetworkError);
9597
private:
96-
97-
98-
99-
100-
10198
int currentplayingitem;
10299
int currentSkeyItem;
103100
QList<songElement *> *pList;
@@ -109,6 +106,8 @@ private slots:
109106
QTime startStreamT;
110107
QVariant *icon;
111108
QVariant *invalid;
109+
double bufferpcnt;
110+
112111
};
113112

114113
#endif // PLAYLIST_H

0 commit comments

Comments
 (0)