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;
12 invalid = new QVariant();
13 icon = new QVariant(QIcon(":/groove/icons/general_forward.png"));
16 //Implemented model class information
17 QVariant playlist::data(const QModelIndex &index, int role) const
20 playlist* play = (playlist *)index.model();
21 QVariant dat = *play->invalid;
22 if(play->existAt(index.row()))
25 return *play->invalid;
26 if (role == Qt::TextAlignmentRole) {
27 return int(Qt::AlignLeft | Qt::AlignVCenter);
28 } else if (role == Qt::DecorationRole) {
29 switch(index.column())
32 if(play->currentplaying()==index.row())
40 } else if (role == Qt::ForegroundRole) {
41 switch(index.column())
44 if(!play->pList->at(index.row())->downloaded)
45 dat = QVariant(Qt::gray);
52 } else if (role == Qt::DisplayRole) {
53 switch(index.column())
56 dat = QVariant(*play->pList->at(index.row())->name);
59 dat = QVariant(*play->pList->at(index.row())->songId);
62 dat = QVariant(*play->pList->at(index.row())->streamkey);
65 dat = QVariant(play->pList->at(index.row())->downloaded);
68 dat = QVariant(play->pList->at(index.row())->bufferready);
71 dat = QVariant(play->pList->at(index.row())->server->toString());
74 dat = QVariant(play->pList->at(index.row())->played);
86 int playlist::rowCount(const QModelIndex &) const
90 int playlist::columnCount(const QModelIndex &) const
96 QList<playlist::songElement *>* playlist::getList()
101 void playlist::markPlayed(int position)
103 if(0 <= position && position < pList->size())
105 pList->at(position)->played = true;
106 this->freeMemory(position);
109 void playlist::freeMemory(int position)
111 pList->at(position)->downloaded = false;
112 pList->at(position)->bufferready = false;
113 delete pList->at(position)->buffer;
114 pList->at(position)->buffer = new QBuffer();
116 bool playlist::existAt(int position)
120 return (pList->size() > position);
123 int playlist::currentplaying()
125 return this->currentplayingitem;
127 bool playlist::bReady(int b)
129 if(pList->size() > b)
130 return pList->at(b)->bufferready;
134 void playlist::setBufferRdy(int b)
136 pList->at(b)->bufferready = true;
138 void playlist::setCurrentPlaying(int position)
140 if(this->existAt(position))
142 this->currentplayingitem = position;
143 if(!pList->at(position)->downloaded && this->currentdownloaditem != this->currentplayingitem)
144 this->beginDownload(position);
145 /*if(pList->at(position)->bufferready == false &&)
147 if(!pList->at(position)->downloaded)
148 this->beginDownload(position);
151 emit this->bufferReady(position);
159 this->currentplayingitem = -1;
165 QIODevice * playlist::getBuffer(int position)
167 return pList->at(position)->buffer;
170 void playlist::beginDownload(int position)
172 this->currentdownloaditem = position;
173 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
175 req.setUrl(*pList->at(currentdownloaditem)->server);
176 qDebug() << pList->at(currentdownloaditem)->server;
177 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
181 reply->deleteLater();
183 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
184 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
185 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
186 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
187 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
188 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
189 startStreamT = QTime::currentTime();
191 void playlist::getNError(QNetworkReply::NetworkError error)
193 qDebug() << "Network Error (if this is 99 then it will retry" << error;
194 if(error == QNetworkReply::UnknownNetworkError && this->currentdownloaditem != -1)
195 beginDownload(this->currentdownloaditem);
199 void playlist::setGscom(gscom *comm)
202 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
204 void playlist::skeyFound()
206 emit this->freeze(false);
207 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
208 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
209 if(this->currentdownloaditem == -1)
210 this->beginDownload(this->currentSkeyItem);
212 if(this->currentplaying() == this->currentSkeyItem)
213 this->beginDownload(this->currentSkeyItem);
214 this->currentSkeyItem = -1;
217 int playlist::addSong(QStandardItem *item, QString name)
219 playlist::songElement *newelement = new playlist::songElement;
220 newelement->name = new QString(name);
221 newelement->buffer = new QBuffer();
222 newelement->downloaded =false;
223 newelement->songId = new QString(item->text());
224 newelement->played = false;
225 newelement->server = new QUrl();
226 newelement->streamkey = new QString("noneatm");
227 newelement->bufferready = false;
228 newelement->type = playlist::EStream;
229 pList->append(newelement);
230 gs->getSong(item->text());
231 emit this->rowsInserted(QModelIndex(),pList->size(),pList->size());
232 this->currentSkeyItem = pList->size()-1;
233 emit this->freeze(true);
234 return pList->size()-1;
237 void playlist::downloadDone(int position)
239 if(this->existAt(position+1) && this->currentSkeyItem == -1 && !pList->at(position+1)->downloaded && this->currentdownloaditem != position+1)
240 beginDownload(position+1);
242 this->currentdownloaditem = -1;
243 pList->at(position)->downloaded = true;
245 void playlist::networkReplyFinish()
247 qDebug() << "finish";
248 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
249 if(url.toUrl().isValid())
252 req.setUrl(url.toUrl());
257 reply->deleteLater();
259 reply = manager->get(req);
260 startStreamT = QTime::currentTime();
261 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
262 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
263 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
264 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
268 void playlist::downloadSlot(qint64 b, qint64 t)
270 //qDebug() << "Download: " << b << "Total: " << t;
273 emit this->downloadProgress(this->currentdownloaditem,b,t);
274 if(existAt(this->currentdownloaditem))
276 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
277 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
278 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
279 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
281 this->setBufferRdy(this->currentdownloaditem);
282 emit this->bufferReady(this->currentdownloaditem);
284 qDebug() << "Buffer Ready";
288 emit this->downloadComplete(this->currentdownloaditem);
289 //emit this->bufferReady(this->currentdownloaditem);