Update gscom for api changes
[groove] / playlist.cpp
index 3004f86..c9c43c5 100644 (file)
@@ -1,7 +1,7 @@
 #include "playlist.h"
 
 playlist::playlist(QObject *parent) :
-    QObject(parent)
+    QAbstractTableModel(parent)
 {
    manager = new QNetworkAccessManager();
    this->currentdownloaditem = -1;
@@ -9,19 +9,116 @@ playlist::playlist(QObject *parent) :
    this->currentplayingitem = -1;
    this->currentSkeyItem = -1;
    this->reply = NULL;
+   invalid = new QVariant();
+   icon = new QVariant(QIcon(":/groove/icons/general_forward.png"));
 }
+
+//Implemented model class information
+QVariant playlist::data(const QModelIndex &index, int role) const
+{
+
+    playlist* play = (playlist *)index.model();
+    /*QVariant dat = *play->invalid;
+    if(play->existAt(index.row()))
+    {
+        if (!index.isValid())
+            return *play->invalid;
+        if (role == Qt::TextAlignmentRole) {
+            return int(Qt::AlignLeft | Qt::AlignVCenter);
+        } else if (role == Qt::DecorationRole) {
+            switch(index.column())
+            {
+            case sName:
+                if(play->currentplaying()==index.row())
+                    //dat = *play->icon;
+                    dat = *play->invalid;
+                else
+                    dat = *play->invalid;
+                break;
+            default:
+                dat = *play->invalid;
+            }
+        } else if (role == Qt::ForegroundRole) {
+            switch(index.column())
+            {
+            case sName:
+                if(!play->pList->at(index.row())->downloaded)
+                    dat = QVariant(Qt::gray);
+                else
+                    dat = *play->invalid;
+                break;
+            default:
+                dat = *play->invalid;
+            }
+        } else if (role == Qt::DisplayRole) {
+            switch(index.column())
+            {
+            case sName:
+                dat = QVariant(*play->pList->at(index.row())->name);
+                break;
+            case sID:
+                dat = QVariant(*play->pList->at(index.row())->songId);
+                break;
+            case sKey:
+                dat = QVariant(*play->pList->at(index.row())->streamkey);
+                break;
+            case sDownloaded:
+                dat = QVariant(play->pList->at(index.row())->downloaded);
+                break;
+            case sReady:
+                dat = QVariant(play->pList->at(index.row())->bufferready);
+                break;
+            case sURL:
+                dat = QVariant(play->pList->at(index.row())->server->toString());
+                break;
+            case sPlayed:
+                dat = QVariant(play->pList->at(index.row())->played);
+                break;
+            default:
+                dat = *play->invalid;
+            }
+        } else
+            dat = *play->invalid;
+    }
+    else
+        dat = *play->invalid;
+    return dat;*/
+    return *play->invalid;
+}
+int playlist::rowCount(const QModelIndex &) const
+{
+    return pList->size();
+}
+int playlist::columnCount(const QModelIndex &) const
+{
+    return PLAYLISTENUMS;
+}
+
+
+QList<playlist::songElement *>* playlist::getList()
+{
+    return pList;
+}
+
 void playlist::markPlayed(int position)
 {
-    pList->at(position)->played = true;
-    this->freeMemory(position);
+    if(0 <= position && position < pList->size())
+    {
+        pList->at(position)->played = true;
+        this->freeMemory(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);
 }
 
@@ -40,11 +137,13 @@ void playlist::setBufferRdy(int b)
 {
     pList->at(b)->bufferready = true;
 }
-bool playlist::setCurrentPlaying(int position)
+void 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)
@@ -53,10 +152,17 @@ bool playlist::setCurrentPlaying(int position)
         else
             emit this->bufferReady(position);
         */
-        return true;
+        return;
     }
     else
-        return false;
+    {
+    if(position == -1)
+        {
+        this->currentplayingitem = -1;
+    }
+    else
+        return;
+    }
 }
 QIODevice * playlist::getBuffer(int position)
 {
@@ -73,16 +179,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)
 {
@@ -102,9 +216,10 @@ void playlist::skeyFound()
     this->currentSkeyItem = -1;
 }
 
-int playlist::addSong(QStandardItem *item)
+int playlist::addSong(QStandardItem *item, QString name)
 {
     playlist::songElement *newelement = new playlist::songElement;
+    newelement->name = new QString(name);
     newelement->buffer = new QBuffer();
     newelement->downloaded =false;
     newelement->songId = new QString(item->text());
@@ -115,7 +230,7 @@ int playlist::addSong(QStandardItem *item)
     newelement->type = playlist::EStream;
     pList->append(newelement);
     gs->getSong(item->text());
-
+    emit this->rowsInserted(QModelIndex(),pList->size(),pList->size());
     this->currentSkeyItem = pList->size()-1;
     emit this->freeze(true);
     return pList->size()-1;
@@ -123,7 +238,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 +253,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)));
     }
 }