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 QList<playlist::songElement *>* playlist::getList()
18 void playlist::markPlayed(int position)
20 if(0 <= position && position < pList->size())
22 pList->at(position)->played = true;
23 this->freeMemory(position);
26 void playlist::freeMemory(int position)
28 pList->at(position)->downloaded = false;
29 pList->at(position)->bufferready = false;
30 delete pList->at(position)->buffer;
31 pList->at(position)->buffer = new QBuffer();
33 bool playlist::existAt(int position)
37 return (pList->size() > position);
40 int playlist::currentplaying()
42 return this->currentplayingitem;
44 bool playlist::bReady(int b)
47 return pList->at(b)->bufferready;
51 void playlist::setBufferRdy(int b)
53 pList->at(b)->bufferready = true;
55 void playlist::setCurrentPlaying(int position)
57 if(this->existAt(position))
59 this->currentplayingitem = position;
60 if(!pList->at(position)->downloaded && this->currentdownloaditem != this->currentplayingitem)
61 this->beginDownload(position);
62 /*if(pList->at(position)->bufferready == false &&)
64 if(!pList->at(position)->downloaded)
65 this->beginDownload(position);
68 emit this->bufferReady(position);
76 this->currentplayingitem = -1;
82 QIODevice * playlist::getBuffer(int position)
84 return pList->at(position)->buffer;
87 void playlist::beginDownload(int position)
89 this->currentdownloaditem = position;
90 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
92 req.setUrl(*pList->at(currentdownloaditem)->server);
93 qDebug() << pList->at(currentdownloaditem)->server;
94 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
100 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
101 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
102 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
103 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
104 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
105 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
106 startStreamT = QTime::currentTime();
108 void playlist::getNError(QNetworkReply::NetworkError error)
110 qDebug() << "Network Error (if this is 99 then it will retry" << error;
111 if(error == QNetworkReply::UnknownNetworkError && this->currentdownloaditem != -1)
112 beginDownload(this->currentdownloaditem);
116 void playlist::setGscom(gscom *comm)
119 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
121 void playlist::skeyFound()
123 emit this->freeze(false);
124 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
125 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
126 if(this->currentdownloaditem == -1)
127 this->beginDownload(this->currentSkeyItem);
129 if(this->currentplaying() == this->currentSkeyItem)
130 this->beginDownload(this->currentSkeyItem);
131 this->currentSkeyItem = -1;
134 int playlist::addSong(QStandardItem *item)
136 playlist::songElement *newelement = new playlist::songElement;
137 newelement->buffer = new QBuffer();
138 newelement->downloaded =false;
139 newelement->songId = new QString(item->text());
140 newelement->played = false;
141 newelement->server = new QUrl();
142 newelement->streamkey = new QString("noneatm");
143 newelement->bufferready = false;
144 newelement->type = playlist::EStream;
145 pList->append(newelement);
146 gs->getSong(item->text());
148 this->currentSkeyItem = pList->size()-1;
149 emit this->freeze(true);
150 return pList->size()-1;
153 void playlist::downloadDone(int position)
155 if(this->existAt(position+1) && this->currentSkeyItem == -1 && !pList->at(position+1)->downloaded && this->currentdownloaditem != position+1)
156 beginDownload(position+1);
158 this->currentdownloaditem = -1;
159 pList->at(position)->downloaded = true;
161 void playlist::networkReplyFinish()
163 qDebug() << "finish";
164 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
165 if(url.toUrl().isValid())
168 req.setUrl(url.toUrl());
173 reply->deleteLater();
175 reply = manager->get(req);
176 startStreamT = QTime::currentTime();
177 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
178 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
179 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
180 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
184 void playlist::downloadSlot(qint64 b, qint64 t)
186 //qDebug() << "Download: " << b << "Total: " << t;
189 emit this->downloadProgress(this->currentdownloaditem,b,t);
190 if(existAt(this->currentdownloaditem))
192 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
193 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
194 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
195 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
197 this->setBufferRdy(this->currentdownloaditem);
198 emit this->bufferReady(this->currentdownloaditem);
200 qDebug() << "Buffer Ready";
204 emit this->downloadComplete(this->currentdownloaditem);
205 //emit this->bufferReady(this->currentdownloaditem);