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();
22 bool playlist::existAt(int position)
24 return (pList->size() > position);
27 int playlist::currentplaying()
29 return this->currentplayingitem;
31 bool playlist::bReady(int b)
34 return pList->at(b)->bufferready;
38 void playlist::setBufferRdy(int b)
40 pList->at(b)->bufferready = true;
42 bool playlist::setCurrentPlaying(int position)
44 if(pList->size() > position)
46 this->currentplayingitem = position;
47 /*if(pList->at(position)->bufferready == false &&)
49 if(!pList->at(position)->downloaded)
50 this->beginDownload(position);
53 emit this->bufferReady(position);
60 QIODevice * playlist::getBuffer(int position)
62 return pList->at(position)->buffer;
65 void playlist::beginDownload(int position)
67 this->currentdownloaditem = position;
68 qDebug() << "StartDownlaod:" << pList->at(position)->songId;
70 req.setUrl(*pList->at(currentdownloaditem)->server);
71 qDebug() << pList->at(currentdownloaditem)->server;
72 req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
74 reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
75 pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
76 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
77 connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
78 connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
79 startStreamT = QTime::currentTime();
82 void playlist::setGscom(gscom *comm)
85 connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
87 void playlist::skeyFound()
89 emit this->unfreeze();
90 pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
91 pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
92 if(this->currentdownloaditem == -1)
93 this->beginDownload(this->currentSkeyItem);
95 if(this->currentplaying() == this->currentSkeyItem)
96 this->beginDownload(this->currentSkeyItem);
97 this->currentSkeyItem = -1;
100 int playlist::addSong(QStandardItem *item)
102 playlist::songElement *newelement = new playlist::songElement;
103 newelement->buffer = new QBuffer();
104 newelement->downloaded =false;
105 newelement->songId = new QString(item->text());
106 newelement->played = false;
107 newelement->server = new QUrl();
108 newelement->streamkey = new QString("noneatm");
109 newelement->bufferready = false;
110 newelement->type = playlist::EStream;
111 pList->append(newelement);
112 gs->getSong(item->text());
114 this->currentSkeyItem = pList->size()-1;
115 return pList->size()-1;
118 void playlist::downloadDone(int position)
120 if(this->existAt(position+1) && this->currentSkeyItem == -1)
121 beginDownload(position+1);
123 this->currentdownloaditem = -1;
124 pList->at(position)->downloaded = true;
126 void playlist::networkReplyFinish()
128 qDebug() << "finish";
129 QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
130 if(url.toUrl().isValid())
133 req.setUrl(url.toUrl());
135 reply = manager->get(req);
136 startStreamT = QTime::currentTime();
137 //connect(reply,SIGNAL(finished()),this,SLOT(start()));
138 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
142 void playlist::downloadSlot(qint64 b, qint64 t)
144 //qDebug() << "Download: " << b << "Total: " << t;
147 emit this->downloadProgress(this->currentdownloaditem,b,t);
148 pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
149 //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
150 if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
151 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
153 this->setBufferRdy(this->currentdownloaditem);
154 emit this->bufferReady(this->currentdownloaditem);
156 qDebug() << "Buffer Ready";
160 emit this->downloadComplete(this->currentdownloaditem);
161 //emit this->bufferReady(this->currentdownloaditem);