UI bugfixes
[groove] / playlist.cpp
index 3004f86..3d437a5 100644 (file)
@@ -17,11 +17,15 @@ void playlist::markPlayed(int position)
 }
 void playlist::freeMemory(int position)
 {
+   pList->at(position)->downloaded = false;
+   pList->at(position)->bufferready = false;
    delete pList->at(position)->buffer;
    pList->at(position)->buffer = new QBuffer();
 }
 bool playlist::existAt(int position)
 {
+    if(position < 0)
+        return false;
     return (pList->size() > position);
 }
 
@@ -42,9 +46,11 @@ void playlist::setBufferRdy(int b)
 }
 bool playlist::setCurrentPlaying(int position)
 {
-    if(pList->size() > position)
+    if(this->existAt(position))
     {
         this->currentplayingitem = position;
+        if(!pList->at(position)->downloaded && this->currentdownloaditem != this->currentplayingitem)
+            this->beginDownload(position);
         /*if(pList->at(position)->bufferready == false &&)
         {
             if(!pList->at(position)->downloaded)
@@ -56,7 +62,14 @@ bool playlist::setCurrentPlaying(int position)
         return true;
     }
     else
+    {
+    if(position = -1)
+        {
+        this->currentplayingitem = -1;
+    }
+    else
         return false;
+    }
 }
 QIODevice * playlist::getBuffer(int position)
 {
@@ -73,16 +86,24 @@ void playlist::beginDownload(int position)
     req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
     if(reply)
     {
-        reply->abort();
-        delete reply;
+        reply->disconnect();
+        reply->deleteLater();
     }
     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)));
+    connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
     startStreamT = QTime::currentTime();
 }
+void playlist::getNError(QNetworkReply::NetworkError error)
+{
+    qDebug() << "Network Error (if this is 99 then it will retry" << error;
+    if(error == QNetworkReply::UnknownNetworkError && this->currentdownloaditem != -1)
+        beginDownload(this->currentdownloaditem);
+
+}
 
 void playlist::setGscom(gscom *comm)
 {
@@ -123,7 +144,7 @@ int playlist::addSong(QStandardItem *item)
 
 void playlist::downloadDone(int position)
 {
-    if(this->existAt(position+1) && this->currentSkeyItem == -1)
+    if(this->existAt(position+1) && this->currentSkeyItem == -1 && !pList->at(position+1)->downloaded && this->currentdownloaditem != position+1)
         beginDownload(position+1);
     else
         this->currentdownloaditem = -1;
@@ -138,10 +159,17 @@ void playlist::networkReplyFinish()
         QNetworkRequest req;
         req.setUrl(url.toUrl());
         qDebug() << url;
+        if(reply)
+        {
+            reply->disconnect();
+            reply->deleteLater();
+        }
         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)));
+        connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
+        connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
     }
 }