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;
12 void playlist::markPlayed(int position)
14 pList->at(position)->played = true;
15 this->freeMemory(position);
17 void playlist::freeMemory(int position)
19 pList->at(position)->buffer->~QBuffer();
20 pList->at(position)->buffer = new QBuffer();
23 int playlist::currentplaying()
25 return this->currentplayingitem;
27 bool playlist::bReady(int b)
29 return pList->at(b)->bufferready;
31 void playlist::setBufferRdy(int b)
33 pList->at(b)->bufferready = true;
35 bool playlist::setCurrentPlaying(int position)
37 if(pList->size() > position)
39 this->currentplayingitem = position;
40 /*if(pList->at(position)->bufferready == false &&)
42 if(!pList->at(position)->downloaded)
43 this->beginDownload(position);
46 emit this->bufferReady(position);
53 QIODevice * playlist::getBuffer(int position)
55 return pList->at(position)->buffer;
58 void playlist::beginDownload(int position)
60 this->currentdownloaditem = position;
61 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
63 req.setUrl(*pList->at(currentdownloaditem)->server);
64 qDebug() << pList->at(currentdownloaditem)->server;
65 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
67 reply->~QNetworkReply();
68 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
69 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
70 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
71 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
72 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
73 startStreamT = QTime::currentTime();
76 void playlist::setGscom(gscom *comm)
79 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
81 void playlist::skeyFound()
83 emit this->unfreeze();
84 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
85 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
86 if(this->currentdownloaditem == -1)
87 this->beginDownload(this->currentSkeyItem);
89 if(this->currentplaying() == this->currentSkeyItem)
90 this->beginDownload(this->currentSkeyItem);
93 int playlist::addSong(QStandardItem *item)
95 playlist::songElement *newelement = new playlist::songElement;
96 newelement->buffer = new QBuffer();
97 newelement->downloaded =false;
98 newelement->songId = new QString(item->text());
99 newelement->played = false;
100 newelement->server = new QUrl();
101 newelement->streamkey = new QString("noneatm");
102 newelement->bufferready = false;
103 newelement->type = playlist::EStream;
104 pList->append(newelement);
105 gs->getSong(item->text());
107 this->currentSkeyItem = pList->size()-1;
108 return pList->size()-1;
111 void playlist::downloadDone(int position)
113 if(pList->size() < position+1)
114 beginDownload(position+1);
116 this->currentdownloaditem = -1;
117 pList->at(position)->downloaded = true;
119 void playlist::networkReplyFinish()
121 qDebug() << "finish";
122 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
123 if(url.toUrl().isValid())
126 req.setUrl(url.toUrl());
128 reply = manager->get(req);
129 startStreamT = QTime::currentTime();
130 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
131 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
135 void playlist::downloadSlot(qint64 b, qint64 t)
137 //qDebug() << "Download: " << b << "Total: " << t;
140 emit this->downloadProgress(this->currentdownloaditem,b,t);
141 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
142 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
143 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
144 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
146 this->setBufferRdy(this->currentdownloaditem);
147 emit this->bufferReady(this->currentdownloaditem);
149 qDebug() << "Buffer Ready";
153 emit this->downloadComplete(this->currentdownloaditem);
154 //emit this->bufferReady(this->currentdownloaditem);