From: Elias Woods Date: Thu, 24 Jun 2010 06:55:59 +0000 (-0400) Subject: Begin phasing in playlist support X-Git-Url: https://vcs.maemo.org/git/?p=groove;a=commitdiff_plain;h=14150f99aab8f21958b2698ceb97c38c6d478338 Begin phasing in playlist support --- diff --git a/Groove.pro.user b/Groove.pro.user index 2e2f61c..09eb720 100644 --- a/Groove.pro.user +++ b/Groove.pro.user @@ -194,7 +194,7 @@ 1 - 2010-06-20T16:49:36 + 2010-06-24T02:28:14 diff --git a/playlist.cpp b/playlist.cpp index 6c0cb7f..10e1e26 100644 --- a/playlist.cpp +++ b/playlist.cpp @@ -3,4 +3,104 @@ playlist::playlist(QObject *parent) : QObject(parent) { + manager = new QNetworkAccessManager(); + this->currentdownloaditem = -1; + pList = new QList; +} +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); + } + } } diff --git a/playlist.h b/playlist.h index 8b810ac..829707f 100644 --- a/playlist.h +++ b/playlist.h @@ -3,22 +3,75 @@ #include #include - +#include +#include +#include +#include +#include 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 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 *pList; + QSignalMapper *mapper; + QNetworkReply *reply; + QNetworkAccessManager *manager; + int currentdownloaditem; + gscom *gs; + QTime startStreamT; }; #endif // PLAYLIST_H