3 playlist::playlist(QObject *parent) :
4 QAbstractTableModel(parent)
6 manager = new QNetworkAccessManager();
7 this->currentdownloaditem = -1;
8 pList = new QList<songElement *>;
9 this->currentplayingitem = -1;
10 this->currentSkeyItem = -1;
14 //Implemented model class information
15 QVariant playlist::data(const QModelIndex &index, int role) const
18 playlist* play = (playlist *)index.model();
19 if(play->existAt(index.row()))
23 if (role == Qt::TextAlignmentRole) {
24 return int(Qt::AlignLeft | Qt::AlignVCenter);
25 } else if (role == Qt::DisplayRole) {
26 switch(index.column())
29 dat = QVariant(*play->pList->at(index.row())->name);
32 dat = QVariant(*play->pList->at(index.row())->songId);
35 dat = QVariant(*play->pList->at(index.row())->streamkey);
38 dat = QVariant(play->pList->at(index.row())->downloaded);
41 dat = QVariant(play->pList->at(index.row())->bufferready);
44 dat = QVariant(play->pList->at(index.row())->server->toString());
47 dat = QVariant(play->pList->at(index.row())->played);
59 int playlist::rowCount(const QModelIndex &) const
63 int playlist::columnCount(const QModelIndex &) const
69 QList<playlist::songElement *>* playlist::getList()
74 void playlist::markPlayed(int position)
76 if(0 <= position && position < pList->size())
78 pList->at(position)->played = true;
79 this->freeMemory(position);
82 void playlist::freeMemory(int position)
84 pList->at(position)->downloaded = false;
85 pList->at(position)->bufferready = false;
86 delete pList->at(position)->buffer;
87 pList->at(position)->buffer = new QBuffer();
89 bool playlist::existAt(int position)
93 return (pList->size() > position);
96 int playlist::currentplaying()
98 return this->currentplayingitem;
100 bool playlist::bReady(int b)
102 if(pList->size() > b)
103 return pList->at(b)->bufferready;
107 void playlist::setBufferRdy(int b)
109 pList->at(b)->bufferready = true;
111 void playlist::setCurrentPlaying(int position)
113 if(this->existAt(position))
115 this->currentplayingitem = position;
116 if(!pList->at(position)->downloaded && this->currentdownloaditem != this->currentplayingitem)
117 this->beginDownload(position);
118 /*if(pList->at(position)->bufferready == false &&)
120 if(!pList->at(position)->downloaded)
121 this->beginDownload(position);
124 emit this->bufferReady(position);
132 this->currentplayingitem = -1;
138 QIODevice * playlist::getBuffer(int position)
140 return pList->at(position)->buffer;
143 void playlist::beginDownload(int position)
145 this->currentdownloaditem = position;
146 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
148 req.setUrl(*pList->at(currentdownloaditem)->server);
149 qDebug() << pList->at(currentdownloaditem)->server;
150 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
154 reply->deleteLater();
156 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
157 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
158 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
159 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
160 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
161 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
162 startStreamT = QTime::currentTime();
164 void playlist::getNError(QNetworkReply::NetworkError error)
166 qDebug() << "Network Error (if this is 99 then it will retry" << error;
167 if(error == QNetworkReply::UnknownNetworkError && this->currentdownloaditem != -1)
168 beginDownload(this->currentdownloaditem);
172 void playlist::setGscom(gscom *comm)
175 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
177 void playlist::skeyFound()
179 emit this->freeze(false);
180 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
181 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
182 if(this->currentdownloaditem == -1)
183 this->beginDownload(this->currentSkeyItem);
185 if(this->currentplaying() == this->currentSkeyItem)
186 this->beginDownload(this->currentSkeyItem);
187 this->currentSkeyItem = -1;
190 int playlist::addSong(QStandardItem *item, QString name)
192 playlist::songElement *newelement = new playlist::songElement;
193 newelement->name = new QString(name);
194 newelement->buffer = new QBuffer();
195 newelement->downloaded =false;
196 newelement->songId = new QString(item->text());
197 newelement->played = false;
198 newelement->server = new QUrl();
199 newelement->streamkey = new QString("noneatm");
200 newelement->bufferready = false;
201 newelement->type = playlist::EStream;
202 pList->append(newelement);
203 gs->getSong(item->text());
204 emit this->rowsInserted(QModelIndex(),pList->size(),pList->size());
205 this->currentSkeyItem = pList->size()-1;
206 emit this->freeze(true);
207 return pList->size()-1;
210 void playlist::downloadDone(int position)
212 if(this->existAt(position+1) && this->currentSkeyItem == -1 && !pList->at(position+1)->downloaded && this->currentdownloaditem != position+1)
213 beginDownload(position+1);
215 this->currentdownloaditem = -1;
216 pList->at(position)->downloaded = true;
218 void playlist::networkReplyFinish()
220 qDebug() << "finish";
221 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
222 if(url.toUrl().isValid())
225 req.setUrl(url.toUrl());
230 reply->deleteLater();
232 reply = manager->get(req);
233 startStreamT = QTime::currentTime();
234 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
235 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
236 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
237 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
241 void playlist::downloadSlot(qint64 b, qint64 t)
243 //qDebug() << "Download: " << b << "Total: " << t;
246 emit this->downloadProgress(this->currentdownloaditem,b,t);
247 if(existAt(this->currentdownloaditem))
249 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
250 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
251 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
252 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
254 this->setBufferRdy(this->currentdownloaditem);
255 emit this->bufferReady(this->currentdownloaditem);
257 qDebug() << "Buffer Ready";
261 emit this->downloadComplete(this->currentdownloaditem);
262 //emit this->bufferReady(this->currentdownloaditem);