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 delete pList->at(position)->buffer;
21 pList->at(position)->buffer = new QBuffer();
23 bool playlist::existAt(int position)
25 return (pList->size() > position);
28 int playlist::currentplaying()
30 return this->currentplayingitem;
32 bool playlist::bReady(int b)
35 return pList->at(b)->bufferready;
39 void playlist::setBufferRdy(int b)
41 pList->at(b)->bufferready = true;
43 bool playlist::setCurrentPlaying(int position)
45 if(pList->size() > position)
47 this->currentplayingitem = position;
48 /*if(pList->at(position)->bufferready == false &&)
50 if(!pList->at(position)->downloaded)
51 this->beginDownload(position);
54 emit this->bufferReady(position);
61 QIODevice * playlist::getBuffer(int position)
63 return pList->at(position)->buffer;
66 void playlist::beginDownload(int position)
68 this->currentdownloaditem = position;
69 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
71 req.setUrl(*pList->at(currentdownloaditem)->server);
72 qDebug() << pList->at(currentdownloaditem)->server;
73 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
79 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
80 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
81 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
82 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
83 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
84 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
85 startStreamT = QTime::currentTime();
87 void playlist::getNError(QNetworkReply::NetworkError error)
89 qDebug() << "Network Error (if this is 99 then it will retry" << error;
90 if(error == QNetworkReply::UnknownNetworkError)
91 beginDownload(this->currentdownloaditem);
95 void playlist::setGscom(gscom *comm)
98 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
100 void playlist::skeyFound()
102 emit this->freeze(false);
103 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
104 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
105 if(this->currentdownloaditem == -1)
106 this->beginDownload(this->currentSkeyItem);
108 if(this->currentplaying() == this->currentSkeyItem)
109 this->beginDownload(this->currentSkeyItem);
110 this->currentSkeyItem = -1;
113 int playlist::addSong(QStandardItem *item)
115 playlist::songElement *newelement = new playlist::songElement;
116 newelement->buffer = new QBuffer();
117 newelement->downloaded =false;
118 newelement->songId = new QString(item->text());
119 newelement->played = false;
120 newelement->server = new QUrl();
121 newelement->streamkey = new QString("noneatm");
122 newelement->bufferready = false;
123 newelement->type = playlist::EStream;
124 pList->append(newelement);
125 gs->getSong(item->text());
127 this->currentSkeyItem = pList->size()-1;
128 emit this->freeze(true);
129 return pList->size()-1;
132 void playlist::downloadDone(int position)
134 if(this->existAt(position+1) && this->currentSkeyItem == -1)
135 beginDownload(position+1);
137 this->currentdownloaditem = -1;
138 pList->at(position)->downloaded = true;
140 void playlist::networkReplyFinish()
142 qDebug() << "finish";
143 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
144 if(url.toUrl().isValid())
147 req.setUrl(url.toUrl());
152 reply->deleteLater();
154 reply = manager->get(req);
155 startStreamT = QTime::currentTime();
156 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
157 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
158 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
159 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
163 void playlist::downloadSlot(qint64 b, qint64 t)
165 //qDebug() << "Download: " << b << "Total: " << t;
168 emit this->downloadProgress(this->currentdownloaditem,b,t);
169 if(existAt(this->currentdownloaditem))
171 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
172 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
173 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
174 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
176 this->setBufferRdy(this->currentdownloaditem);
177 emit this->bufferReady(this->currentdownloaditem);
179 qDebug() << "Buffer Ready";
183 emit this->downloadComplete(this->currentdownloaditem);
184 //emit this->bufferReady(this->currentdownloaditem);