this will break all functionality for now
[groove] / playlist.cpp
index 10e1e26..fbcf8f4 100644 (file)
@@ -6,11 +6,70 @@ playlist::playlist(QObject *parent) :
    manager = new QNetworkAccessManager();
    this->currentdownloaditem = -1;
    pList = new QList<songElement *>;
+   this->currentplayingitem =-1;
 }
+void playlist::markPlayed(int position)
+{
+    pList->at(position)->played = true;
+    this->freeMemory(position);
+}
+void playlist::freeMemory(int position)
+{
+   pList->at(position)->buffer->~QBuffer();
+   pList->at(position)->buffer = new QBuffer();
+}
+
+int playlist::currentplaying()
+{
+    return this->currentplayingitem;
+}
+bool playlist::bReady(int b)
+{
+    return pList->at(b)->bufferready;
+}
+void playlist::setBufferRdy(int b)
+{
+    pList->at(b)->bufferready = true;
+}
+bool playlist::setCurrentPlaying(int position)
+{
+    if(pList->size() > position)
+    {
+        this->currentplayingitem = position;
+        /*if(pList->at(position)->bufferready == false &&)
+        {
+            if(!pList->at(position)->downloaded)
+                this->beginDownload(position);
+        }
+        else
+            emit this->bufferReady(position);
+        */
+        return true;
+    }
+    else
+        return false;
+}
+QIODevice * playlist::getBuffer(int position)
+{
+    return pList->at(position)->buffer;
+}
+
 void playlist::beginDownload(int position)
 {
-    startStreamT = QTime::currentTime();
     this->currentdownloaditem = position;
+    qDebug() << "StartDownlaod:" << pList->at(position)->songId;
+    QNetworkRequest req;
+    req.setUrl(*pList->at(currentdownloaditem)->server);
+    qDebug() << pList->at(currentdownloaditem)->server;
+    req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
+    if(reply)
+        reply->~QNetworkReply();
+    reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
+    pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
+    connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
+    connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
+    connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
+    startStreamT = QTime::currentTime();
 }
 
 void playlist::setGscom(gscom *comm)
@@ -25,24 +84,26 @@ void playlist::skeyFound()
     pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
     if(this->currentdownloaditem == -1)
         this->beginDownload(this->currentSkeyItem);
+    else
+        if(this->currentplaying() == this->currentSkeyItem)
+            this->beginDownload(this->currentplayingitem);
 }
 
-void playlist::addSong(QStandardItem item)
+int playlist::addSong(QStandardItem *item)
 {
-
     playlist::songElement *newelement = new playlist::songElement;
     newelement->buffer = new QBuffer();
     newelement->downloaded =false;
-    newelement->songId = new QString(item.text());
+    newelement->songId = new QString(item->text());
     newelement->played = false;
     newelement->server = new QUrl();
     newelement->streamkey = new QString("noneatm");
     newelement->bufferready = false;
     newelement->type = playlist::EStream;
     pList->append(newelement);
-    gs->getSong(item.text());
+    gs->getSong(item->text());
     emit this->freeze();
-    //this->currentdownloaditem = pList->size()-1;
+    return pList->size()-1;
 }
 
 void playlist::downloadDone(int position)
@@ -53,54 +114,40 @@ void playlist::downloadDone(int position)
         this->currentdownloaditem = -1;
     pList->at(position)->downloaded = true;
 }
+void playlist::networkReplyFinish()
+{
+    qDebug() << "finish";
+    QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
+    if(url.toUrl().isValid())
+    {
+        QNetworkRequest req;
+        req.setUrl(url.toUrl());
+        qDebug() << url;
+        reply = manager->get(req);
+        startStreamT = QTime::currentTime();
+        //connect(reply,SIGNAL(finished()),this,SLOT(start()));
+        connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
+    }
+}
 
 void playlist::downloadSlot(qint64 b, qint64 t)
 {
     //qDebug() << "Download: " << b << "Total: " << t;
-    if(b == 0 && t == 0)
-    {
-        QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
-        if(url.toUrl().isValid())
-        {
-            QNetworkRequest req;
-            req.setUrl(url.toUrl());
-            qDebug() << url;
-            reply = manager->get(req);
-            startStreamT = QTime::currentTime();
-            //connect(reply,SIGNAL(finished()),this,SLOT(start()));
-            connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
-        }
-        else
-        {
-            //buffer->close();
-            emit this->sFailure(this->currentdownloaditem,playlist::Other);
-            reply->close();
-        }
-    }
-    else
+    if(t != 0)
     {
         emit this->downloadProgress(this->currentdownloaditem,b,t);
         pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
-        //qDebug() << buffer->bytesAvailable();
-
-        /*
-        //buffer->seek(b);
-        qint64 last = buffer->pos();
-        buffer->seek(buffer->bytesAvailable()+buffer->pos());
-        qDebug() << buffer->write(reply->readAll());
-        qDebug() << buffer->pos();
-        //buffer->putChar()
-        buffer->seek(last);
-        //buffer->data().append(reply->readAll());*/
-        //qDebug() << "Download speed (KB/S): " << b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024;
         if ( b >= t*0.05 && !pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
         {
+            this->setBufferRdy(this->currentdownloaditem);
             emit this->bufferReady(this->currentdownloaditem);
+
             qDebug() << "Buffer Ready";
         }
         if (b==t)
         {
             emit this->downloadComplete(this->currentdownloaditem);
+            //emit this->bufferReady(this->currentdownloaditem);
         }
     }
 }