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())
41 } else if (role == Qt::ForegroundRole) {
42 switch(index.column())
45 if(!play->pList->at(index.row())->downloaded)
46 dat = QVariant(Qt::gray);
53 } else if (role == Qt::DisplayRole) {
54 switch(index.column())
57 dat = QVariant(*play->pList->at(index.row())->name);
60 dat = QVariant(*play->pList->at(index.row())->songId);
63 dat = QVariant(*play->pList->at(index.row())->streamkey);
66 dat = QVariant(play->pList->at(index.row())->downloaded);
69 dat = QVariant(play->pList->at(index.row())->bufferready);
72 dat = QVariant(play->pList->at(index.row())->server->toString());
75 dat = QVariant(play->pList->at(index.row())->played);
86 return *play->invalid;
88 int playlist::rowCount(const QModelIndex &) const
92 int playlist::columnCount(const QModelIndex &) const
98 QList<playlist::songElement *>* playlist::getList()
103 void playlist::markPlayed(int position)
105 if(0 <= position && position < pList->size())
107 pList->at(position)->played = true;
108 this->freeMemory(position);
111 void playlist::freeMemory(int position)
113 pList->at(position)->downloaded = false;
114 pList->at(position)->bufferready = false;
115 delete pList->at(position)->buffer;
116 pList->at(position)->buffer = new QBuffer();
118 bool playlist::existAt(int position)
122 return (pList->size() > position);
125 int playlist::currentplaying()
127 return this->currentplayingitem;
129 bool playlist::bReady(int b)
131 if(pList->size() > b)
132 return pList->at(b)->bufferready;
136 void playlist::setBufferRdy(int b)
138 pList->at(b)->bufferready = true;
140 void playlist::setCurrentPlaying(int position)
142 if(this->existAt(position))
144 this->currentplayingitem = position;
145 if(!pList->at(position)->downloaded && this->currentdownloaditem != this->currentplayingitem)
146 this->beginDownload(position);
147 /*if(pList->at(position)->bufferready == false &&)
149 if(!pList->at(position)->downloaded)
150 this->beginDownload(position);
153 emit this->bufferReady(position);
161 this->currentplayingitem = -1;
167 QIODevice * playlist::getBuffer(int position)
169 return pList->at(position)->buffer;
172 void playlist::beginDownload(int position)
174 this->currentdownloaditem = position;
175 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
177 req.setUrl(*pList->at(currentdownloaditem)->server);
178 qDebug() << pList->at(currentdownloaditem)->server;
179 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
183 reply->deleteLater();
185 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
186 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
187 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
188 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
189 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
190 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
191 startStreamT = QTime::currentTime();
193 void playlist::getNError(QNetworkReply::NetworkError error)
195 qDebug() << "Network Error (if this is 99 then it will retry" << error;
196 if(error == QNetworkReply::UnknownNetworkError && this->currentdownloaditem != -1)
197 beginDownload(this->currentdownloaditem);
201 void playlist::setGscom(gscom *comm)
204 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
206 void playlist::skeyFound()
208 emit this->freeze(false);
209 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
210 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
211 if(this->currentdownloaditem == -1)
212 this->beginDownload(this->currentSkeyItem);
214 if(this->currentplaying() == this->currentSkeyItem)
215 this->beginDownload(this->currentSkeyItem);
216 this->currentSkeyItem = -1;
219 int playlist::addSong(QStandardItem *item, QString name)
221 playlist::songElement *newelement = new playlist::songElement;
222 newelement->name = new QString(name);
223 newelement->buffer = new QBuffer();
224 newelement->downloaded =false;
225 newelement->songId = new QString(item->text());
226 newelement->played = false;
227 newelement->server = new QUrl();
228 newelement->streamkey = new QString("noneatm");
229 newelement->bufferready = false;
230 newelement->type = playlist::EStream;
231 pList->append(newelement);
232 gs->getSong(item->text());
233 emit this->rowsInserted(QModelIndex(),pList->size(),pList->size());
234 this->currentSkeyItem = pList->size()-1;
235 emit this->freeze(true);
236 return pList->size()-1;
239 void playlist::downloadDone(int position)
241 if(this->existAt(position+1) && this->currentSkeyItem == -1 && !pList->at(position+1)->downloaded && this->currentdownloaditem != position+1)
242 beginDownload(position+1);
244 this->currentdownloaditem = -1;
245 pList->at(position)->downloaded = true;
247 void playlist::networkReplyFinish()
249 qDebug() << "finish";
250 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
251 if(url.toUrl().isValid())
254 req.setUrl(url.toUrl());
259 reply->deleteLater();
261 reply = manager->get(req);
262 startStreamT = QTime::currentTime();
263 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
264 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
265 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
266 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
270 void playlist::downloadSlot(qint64 b, qint64 t)
272 //qDebug() << "Download: " << b << "Total: " << t;
275 emit this->downloadProgress(this->currentdownloaditem,b,t);
276 if(existAt(this->currentdownloaditem))
278 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
279 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
280 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
281 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
283 this->setBufferRdy(this->currentdownloaditem);
284 emit this->bufferReady(this->currentdownloaditem);
286 qDebug() << "Buffer Ready";
290 emit this->downloadComplete(this->currentdownloaditem);
291 //emit this->bufferReady(this->currentdownloaditem);