3 playlist::playlist(QObject *parent) :
6 manager = new QNetworkAccessManager();
7 this->currentdownloaditem = -1;
8 pList = new QList<songElement *>;
9 this->currentplayingitem =-1;
11 void playlist::markPlayed(int position)
13 pList->at(position)->played = true;
14 this->freeMemory(position);
16 void playlist::freeMemory(int position)
18 pList->at(position)->buffer->~QBuffer();
19 pList->at(position)->buffer = new QBuffer();
22 int playlist::currentplaying()
24 return this->currentplayingitem;
26 bool playlist::bReady(int b)
28 return pList->at(b)->bufferready;
30 void playlist::setBufferRdy(int b)
32 pList->at(b)->bufferready = true;
34 bool playlist::setCurrentPlaying(int position)
36 if(pList->size() > position)
38 this->currentplayingitem = position;
39 /*if(pList->at(position)->bufferready == false &&)
41 if(!pList->at(position)->downloaded)
42 this->beginDownload(position);
45 emit this->bufferReady(position);
52 QIODevice * playlist::getBuffer(int position)
54 return pList->at(position)->buffer;
57 void playlist::beginDownload(int position)
59 this->currentdownloaditem = position;
60 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
62 req.setUrl(*pList->at(currentdownloaditem)->server);
63 qDebug() << pList->at(currentdownloaditem)->server;
64 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
66 reply->~QNetworkReply();
67 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
68 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
69 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
70 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
71 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
72 startStreamT = QTime::currentTime();
75 void playlist::setGscom(gscom *comm)
78 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
80 void playlist::skeyFound()
82 emit this->unfreeze();
83 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
84 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
85 if(this->currentdownloaditem == -1)
86 this->beginDownload(this->currentSkeyItem);
88 if(this->currentplaying() == this->currentSkeyItem)
89 this->beginDownload(this->currentplayingitem);
92 int playlist::addSong(QStandardItem *item)
94 playlist::songElement *newelement = new playlist::songElement;
95 newelement->buffer = new QBuffer();
96 newelement->downloaded =false;
97 newelement->songId = new QString(item->text());
98 newelement->played = false;
99 newelement->server = new QUrl();
100 newelement->streamkey = new QString("noneatm");
101 newelement->bufferready = false;
102 newelement->type = playlist::EStream;
103 pList->append(newelement);
104 gs->getSong(item->text());
106 return pList->size()-1;
109 void playlist::downloadDone(int position)
111 if(pList->size() < position+1)
112 beginDownload(position+1);
114 this->currentdownloaditem = -1;
115 pList->at(position)->downloaded = true;
117 void playlist::networkReplyFinish()
119 qDebug() << "finish";
120 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
121 if(url.toUrl().isValid())
124 req.setUrl(url.toUrl());
126 reply = manager->get(req);
127 startStreamT = QTime::currentTime();
128 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
129 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
133 void playlist::downloadSlot(qint64 b, qint64 t)
135 //qDebug() << "Download: " << b << "Total: " << t;
138 emit this->downloadProgress(this->currentdownloaditem,b,t);
139 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
140 if ( b >= t*0.05 && !pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
142 this->setBufferRdy(this->currentdownloaditem);
143 emit this->bufferReady(this->currentdownloaditem);
145 qDebug() << "Buffer Ready";
149 emit this->downloadComplete(this->currentdownloaditem);
150 //emit this->bufferReady(this->currentdownloaditem);