3 playlist::playlist(QObject *parent) :
6 manager = new QNetworkAccessManager();
7 this->currentdownloaditem = -1;
8 pList = new QList<songElement *>;
9 this->currentplayingitem = -1;
10 this->currentSkeyItem = -1;
13 void playlist::markPlayed(int position)
15 pList->at(position)->played = true;
16 this->freeMemory(position);
18 void playlist::freeMemory(int position)
20 pList->at(position)->downloaded = false;
21 delete pList->at(position)->buffer;
22 pList->at(position)->buffer = new QBuffer();
24 bool playlist::existAt(int position)
26 return (pList->size() > position);
29 int playlist::currentplaying()
31 return this->currentplayingitem;
33 bool playlist::bReady(int b)
36 return pList->at(b)->bufferready;
40 void playlist::setBufferRdy(int b)
42 pList->at(b)->bufferready = true;
44 bool playlist::setCurrentPlaying(int position)
46 if(pList->size() > position)
48 this->currentplayingitem = position;
49 if(!pList->at(position)->downloaded && this->currentdownloaditem != this->currentplayingitem)
50 this->beginDownload(position);
51 /*if(pList->at(position)->bufferready == false &&)
53 if(!pList->at(position)->downloaded)
54 this->beginDownload(position);
57 emit this->bufferReady(position);
64 QIODevice * playlist::getBuffer(int position)
66 return pList->at(position)->buffer;
69 void playlist::beginDownload(int position)
71 this->currentdownloaditem = position;
72 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
74 req.setUrl(*pList->at(currentdownloaditem)->server);
75 qDebug() << pList->at(currentdownloaditem)->server;
76 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
82 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
83 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
84 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
85 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
86 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
87 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
88 startStreamT = QTime::currentTime();
90 void playlist::getNError(QNetworkReply::NetworkError error)
92 qDebug() << "Network Error (if this is 99 then it will retry" << error;
93 if(error == QNetworkReply::UnknownNetworkError)
94 beginDownload(this->currentdownloaditem);
98 void playlist::setGscom(gscom *comm)
101 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
103 void playlist::skeyFound()
105 emit this->freeze(false);
106 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
107 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
108 if(this->currentdownloaditem == -1)
109 this->beginDownload(this->currentSkeyItem);
111 if(this->currentplaying() == this->currentSkeyItem)
112 this->beginDownload(this->currentSkeyItem);
113 this->currentSkeyItem = -1;
116 int playlist::addSong(QStandardItem *item)
118 playlist::songElement *newelement = new playlist::songElement;
119 newelement->buffer = new QBuffer();
120 newelement->downloaded =false;
121 newelement->songId = new QString(item->text());
122 newelement->played = false;
123 newelement->server = new QUrl();
124 newelement->streamkey = new QString("noneatm");
125 newelement->bufferready = false;
126 newelement->type = playlist::EStream;
127 pList->append(newelement);
128 gs->getSong(item->text());
130 this->currentSkeyItem = pList->size()-1;
131 emit this->freeze(true);
132 return pList->size()-1;
135 void playlist::downloadDone(int position)
137 if(this->existAt(position+1) && this->currentSkeyItem == -1)
138 beginDownload(position+1);
140 this->currentdownloaditem = -1;
141 pList->at(position)->downloaded = true;
143 void playlist::networkReplyFinish()
145 qDebug() << "finish";
146 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
147 if(url.toUrl().isValid())
150 req.setUrl(url.toUrl());
155 reply->deleteLater();
157 reply = manager->get(req);
158 startStreamT = QTime::currentTime();
159 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
160 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
161 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
162 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
166 void playlist::downloadSlot(qint64 b, qint64 t)
168 //qDebug() << "Download: " << b << "Total: " << t;
171 emit this->downloadProgress(this->currentdownloaditem,b,t);
172 if(existAt(this->currentdownloaditem))
174 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
175 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
176 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
177 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
179 this->setBufferRdy(this->currentdownloaditem);
180 emit this->bufferReady(this->currentdownloaditem);
182 qDebug() << "Buffer Ready";
186 emit this->downloadComplete(this->currentdownloaditem);
187 //emit this->bufferReady(this->currentdownloaditem);