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 startStreamT = QTime::currentTime();
87 void playlist::setGscom(gscom *comm)
90 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
92 void playlist::skeyFound()
94 emit this->freeze(false);
95 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
96 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
97 if(this->currentdownloaditem == -1)
98 this->beginDownload(this->currentSkeyItem);
100 if(this->currentplaying() == this->currentSkeyItem)
101 this->beginDownload(this->currentSkeyItem);
102 this->currentSkeyItem = -1;
105 int playlist::addSong(QStandardItem *item)
107 playlist::songElement *newelement = new playlist::songElement;
108 newelement->buffer = new QBuffer();
109 newelement->downloaded =false;
110 newelement->songId = new QString(item->text());
111 newelement->played = false;
112 newelement->server = new QUrl();
113 newelement->streamkey = new QString("noneatm");
114 newelement->bufferready = false;
115 newelement->type = playlist::EStream;
116 pList->append(newelement);
117 gs->getSong(item->text());
119 this->currentSkeyItem = pList->size()-1;
120 emit this->freeze(true);
121 return pList->size()-1;
124 void playlist::downloadDone(int position)
126 if(this->existAt(position+1) && this->currentSkeyItem == -1)
127 beginDownload(position+1);
129 this->currentdownloaditem = -1;
130 pList->at(position)->downloaded = true;
132 void playlist::networkReplyFinish()
134 qDebug() << "finish";
135 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
136 if(url.toUrl().isValid())
139 req.setUrl(url.toUrl());
141 reply = manager->get(req);
142 startStreamT = QTime::currentTime();
143 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
144 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
148 void playlist::downloadSlot(qint64 b, qint64 t)
150 //qDebug() << "Download: " << b << "Total: " << t;
153 emit this->downloadProgress(this->currentdownloaditem,b,t);
154 if(existAt(this->currentdownloaditem))
156 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
157 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
158 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
159 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
161 this->setBufferRdy(this->currentdownloaditem);
162 emit this->bufferReady(this->currentdownloaditem);
164 qDebug() << "Buffer Ready";
168 emit this->downloadComplete(this->currentdownloaditem);
169 //emit this->bufferReady(this->currentdownloaditem);