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 pList->at(position)->bufferready = false;
22 delete pList->at(position)->buffer;
23 pList->at(position)->buffer = new QBuffer();
25 bool playlist::existAt(int position)
29 return (pList->size() > position);
32 int playlist::currentplaying()
34 return this->currentplayingitem;
36 bool playlist::bReady(int b)
39 return pList->at(b)->bufferready;
43 void playlist::setBufferRdy(int b)
45 pList->at(b)->bufferready = true;
47 bool playlist::setCurrentPlaying(int position)
49 if(this->existAt(position))
51 this->currentplayingitem = position;
52 if(!pList->at(position)->downloaded && this->currentdownloaditem != this->currentplayingitem)
53 this->beginDownload(position);
54 /*if(pList->at(position)->bufferready == false &&)
56 if(!pList->at(position)->downloaded)
57 this->beginDownload(position);
60 emit this->bufferReady(position);
68 this->currentplayingitem = -1;
74 QIODevice * playlist::getBuffer(int position)
76 return pList->at(position)->buffer;
79 void playlist::beginDownload(int position)
81 this->currentdownloaditem = position;
82 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
84 req.setUrl(*pList->at(currentdownloaditem)->server);
85 qDebug() << pList->at(currentdownloaditem)->server;
86 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
92 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
93 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
94 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
95 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
96 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
97 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
98 startStreamT = QTime::currentTime();
100 void playlist::getNError(QNetworkReply::NetworkError error)
102 qDebug() << "Network Error (if this is 99 then it will retry" << error;
103 if(error == QNetworkReply::UnknownNetworkError && this->currentdownloaditem != -1)
104 beginDownload(this->currentdownloaditem);
108 void playlist::setGscom(gscom *comm)
111 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
113 void playlist::skeyFound()
115 emit this->freeze(false);
116 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
117 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
118 if(this->currentdownloaditem == -1)
119 this->beginDownload(this->currentSkeyItem);
121 if(this->currentplaying() == this->currentSkeyItem)
122 this->beginDownload(this->currentSkeyItem);
123 this->currentSkeyItem = -1;
126 int playlist::addSong(QStandardItem *item)
128 playlist::songElement *newelement = new playlist::songElement;
129 newelement->buffer = new QBuffer();
130 newelement->downloaded =false;
131 newelement->songId = new QString(item->text());
132 newelement->played = false;
133 newelement->server = new QUrl();
134 newelement->streamkey = new QString("noneatm");
135 newelement->bufferready = false;
136 newelement->type = playlist::EStream;
137 pList->append(newelement);
138 gs->getSong(item->text());
140 this->currentSkeyItem = pList->size()-1;
141 emit this->freeze(true);
142 return pList->size()-1;
145 void playlist::downloadDone(int position)
147 if(this->existAt(position+1) && this->currentSkeyItem == -1 && !pList->at(position+1)->downloaded && this->currentdownloaditem != position+1)
148 beginDownload(position+1);
150 this->currentdownloaditem = -1;
151 pList->at(position)->downloaded = true;
153 void playlist::networkReplyFinish()
155 qDebug() << "finish";
156 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
157 if(url.toUrl().isValid())
160 req.setUrl(url.toUrl());
165 reply->deleteLater();
167 reply = manager->get(req);
168 startStreamT = QTime::currentTime();
169 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
170 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
171 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
172 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
176 void playlist::downloadSlot(qint64 b, qint64 t)
178 //qDebug() << "Download: " << b << "Total: " << t;
181 emit this->downloadProgress(this->currentdownloaditem,b,t);
182 if(existAt(this->currentdownloaditem))
184 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
185 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
186 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
187 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
189 this->setBufferRdy(this->currentdownloaditem);
190 emit this->bufferReady(this->currentdownloaditem);
192 qDebug() << "Buffer Ready";
196 emit this->downloadComplete(this->currentdownloaditem);
197 //emit this->bufferReady(this->currentdownloaditem);