Begin phasing in playlist support
[groove] / playlist.cpp
1 #include "playlist.h"
2
3 playlist::playlist(QObject *parent) :
4     QObject(parent)
5 {
6    manager = new QNetworkAccessManager();
7    this->currentdownloaditem = -1;
8    pList = new QList<songElement *>;
9 }
10 void playlist::beginDownload(int position)
11 {
12     startStreamT = QTime::currentTime();
13     this->currentdownloaditem = position;
14 }
15
16 void playlist::setGscom(gscom *comm)
17 {
18     gs = comm;
19     connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
20 }
21 void playlist::skeyFound()
22 {
23     emit this->unfreeze();
24     pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
25     pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
26     if(this->currentdownloaditem == -1)
27         this->beginDownload(this->currentSkeyItem);
28 }
29
30 void playlist::addSong(QStandardItem item)
31 {
32
33     playlist::songElement *newelement = new playlist::songElement;
34     newelement->buffer = new QBuffer();
35     newelement->downloaded =false;
36     newelement->songId = new QString(item.text());
37     newelement->played = false;
38     newelement->server = new QUrl();
39     newelement->streamkey = new QString("noneatm");
40     newelement->bufferready = false;
41     newelement->type = playlist::EStream;
42     pList->append(newelement);
43     gs->getSong(item.text());
44     emit this->freeze();
45     //this->currentdownloaditem = pList->size()-1;
46 }
47
48 void playlist::downloadDone(int position)
49 {
50     if(pList->size() < position+1)
51         beginDownload(position+1);
52     else
53         this->currentdownloaditem = -1;
54     pList->at(position)->downloaded = true;
55 }
56
57 void playlist::downloadSlot(qint64 b, qint64 t)
58 {
59     //qDebug() << "Download: " << b << "Total: " << t;
60     if(b == 0 && t == 0)
61     {
62         QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
63         if(url.toUrl().isValid())
64         {
65             QNetworkRequest req;
66             req.setUrl(url.toUrl());
67             qDebug() << url;
68             reply = manager->get(req);
69             startStreamT = QTime::currentTime();
70             //connect(reply,SIGNAL(finished()),this,SLOT(start()));
71             connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
72         }
73         else
74         {
75             //buffer->close();
76             emit this->sFailure(this->currentdownloaditem,playlist::Other);
77             reply->close();
78         }
79     }
80     else
81     {
82         emit this->downloadProgress(this->currentdownloaditem,b,t);
83         pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
84         //qDebug() << buffer->bytesAvailable();
85
86         /*
87         //buffer->seek(b);
88         qint64 last = buffer->pos();
89         buffer->seek(buffer->bytesAvailable()+buffer->pos());
90         qDebug() << buffer->write(reply->readAll());
91         qDebug() << buffer->pos();
92         //buffer->putChar()
93         buffer->seek(last);
94         //buffer->data().append(reply->readAll());*/
95         //qDebug() << "Download speed (KB/S): " << b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024;
96         if ( b >= t*0.05 && !pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
97         {
98             emit this->bufferReady(this->currentdownloaditem);
99             qDebug() << "Buffer Ready";
100         }
101         if (b==t)
102         {
103             emit this->downloadComplete(this->currentdownloaditem);
104         }
105     }
106 }