Begin phasing in playlist support
authorElias Woods <EliasWoods@gmail.com>
Thu, 24 Jun 2010 06:55:59 +0000 (02:55 -0400)
committerElias Woods <EliasWoods@gmail.com>
Thu, 24 Jun 2010 06:55:59 +0000 (02:55 -0400)
Groove.pro.user
playlist.cpp
playlist.h

index 2e2f61c..09eb720 100644 (file)
     </valuemap>
     <value key="Qt4ProjectManager.MaemoRunConfiguration.DeviceId" type="qulonglong">1</value>
     <valuemap key="Qt4ProjectManager.MaemoRunConfiguration.LastDeployed" type="QVariantMap">
-     <value key="192.168.1.3" type="QDateTime">2010-06-20T16:49:36</value>
+     <value key="192.168.1.3" type="QDateTime">2010-06-24T02:28:14</value>
     </valuemap>
     <value key="Qt4ProjectManager.MaemoRunConfiguration.Simulator" type="QString"></value>
    </valuemap>
index 6c0cb7f..10e1e26 100644 (file)
@@ -3,4 +3,104 @@
 playlist::playlist(QObject *parent) :
     QObject(parent)
 {
+   manager = new QNetworkAccessManager();
+   this->currentdownloaditem = -1;
+   pList = new QList<songElement *>;
+}
+void playlist::beginDownload(int position)
+{
+    startStreamT = QTime::currentTime();
+    this->currentdownloaditem = position;
+}
+
+void playlist::setGscom(gscom *comm)
+{
+    gs = comm;
+    connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
+}
+void playlist::skeyFound()
+{
+    emit this->unfreeze();
+    pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
+    pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
+    if(this->currentdownloaditem == -1)
+        this->beginDownload(this->currentSkeyItem);
+}
+
+void playlist::addSong(QStandardItem item)
+{
+
+    playlist::songElement *newelement = new playlist::songElement;
+    newelement->buffer = new QBuffer();
+    newelement->downloaded =false;
+    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());
+    emit this->freeze();
+    //this->currentdownloaditem = pList->size()-1;
+}
+
+void playlist::downloadDone(int position)
+{
+    if(pList->size() < position+1)
+        beginDownload(position+1);
+    else
+        this->currentdownloaditem = -1;
+    pList->at(position)->downloaded = true;
+}
+
+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
+    {
+        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)
+        {
+            emit this->bufferReady(this->currentdownloaditem);
+            qDebug() << "Buffer Ready";
+        }
+        if (b==t)
+        {
+            emit this->downloadComplete(this->currentdownloaditem);
+        }
+    }
 }
index 8b810ac..829707f 100644 (file)
@@ -3,22 +3,75 @@
 
 #include <QObject>
 #include <QStandardItem>
-
+#include <QBuffer>
+#include <QNetworkReply>
+#include <QList>
+#include <QSignalMapper>
+#include <gscom.h>
 class playlist : public QObject
 {
     Q_OBJECT
 public:
+    enum failType
+    {
+        none = 0,
+        Aborted = 1,
+        Other =2
+    };
     explicit playlist(QObject *parent = 0);
     void addSong(QStandardItem item);
+    void addSong(QString songid);
+    void addsong(QString streamkey, QUrl server);
     QList<QStandardItem *> getPlaylist();
     void removeSong(int position);
     void getSong(int position);
-
+    QIODevice * getBuffer(int position);
+    bool isStream(int position);
+    void markComplete(int position);
+    void deleteSong(int position);
+    void setGscom(gscom *comm);
 
 signals:
+    void downloadProgress(int position, qint64 d, qint64 t);
+    void bufferReady(int position);
+    void sFailure(int position,failType);
+    void downloadComplete(int position);
+    void freeze();
+    void unfreeze();
 
 public slots:
+private slots:
+    void downloadSlot(qint64 d, qint64 t);
+    void downloadDone(int position);
+    void skeyFound();
+private:
+    void beginDownload(int position);
+    enum elementType
+    {
+        EStream = 1,
+        EFile = 2,
+    };
 
+    struct songElement
+    {
+        QString *songId;
+        QString *streamkey;
+        QUrl *server;
+        QBuffer *buffer;
+        QIODevice *data;
+        elementType type;
+        bool downloaded;
+        bool played;
+        bool bufferready;
+    };
+    int currentSkeyItem;
+    QList<songElement *> *pList;
+    QSignalMapper *mapper;
+    QNetworkReply *reply;
+    QNetworkAccessManager *manager;
+    int currentdownloaditem;
+    gscom *gs;
+    QTime startStreamT;
 };
 
 #endif // PLAYLIST_H