Playlist support (basic) and download progress dialogs
[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    this->currentplayingitem = -1;
10    this->currentSkeyItem = -1;
11 }
12 void playlist::markPlayed(int position)
13 {
14     pList->at(position)->played = true;
15     this->freeMemory(position);
16 }
17 void playlist::freeMemory(int position)
18 {
19    pList->at(position)->buffer->~QBuffer();
20    pList->at(position)->buffer = new QBuffer();
21 }
22 bool playlist::existAt(int position)
23 {
24     return (pList->size() > position);
25 }
26
27 int playlist::currentplaying()
28 {
29     return this->currentplayingitem;
30 }
31 bool playlist::bReady(int b)
32 {
33     if(pList->size() > b)
34         return pList->at(b)->bufferready;
35     else
36         return false;
37 }
38 void playlist::setBufferRdy(int b)
39 {
40     pList->at(b)->bufferready = true;
41 }
42 bool playlist::setCurrentPlaying(int position)
43 {
44     if(pList->size() > position)
45     {
46         this->currentplayingitem = position;
47         /*if(pList->at(position)->bufferready == false &&)
48         {
49             if(!pList->at(position)->downloaded)
50                 this->beginDownload(position);
51         }
52         else
53             emit this->bufferReady(position);
54         */
55         return true;
56     }
57     else
58         return false;
59 }
60 QIODevice * playlist::getBuffer(int position)
61 {
62     return pList->at(position)->buffer;
63 }
64
65 void playlist::beginDownload(int position)
66 {
67     this->currentdownloaditem = position;
68     qDebug() << "StartDownlaod:" << pList->at(position)->songId;
69     QNetworkRequest req;
70     req.setUrl(*pList->at(currentdownloaditem)->server);
71     qDebug() << pList->at(currentdownloaditem)->server;
72     req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
73     delete reply;
74     reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
75     pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
76     connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
77     connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
78     connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
79     startStreamT = QTime::currentTime();
80 }
81
82 void playlist::setGscom(gscom *comm)
83 {
84     gs = comm;
85     connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
86 }
87 void playlist::skeyFound()
88 {
89     emit this->unfreeze();
90     pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
91     pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
92     if(this->currentdownloaditem == -1)
93         this->beginDownload(this->currentSkeyItem);
94     else
95         if(this->currentplaying() == this->currentSkeyItem)
96             this->beginDownload(this->currentSkeyItem);
97     this->currentSkeyItem = -1;
98 }
99
100 int playlist::addSong(QStandardItem *item)
101 {
102     playlist::songElement *newelement = new playlist::songElement;
103     newelement->buffer = new QBuffer();
104     newelement->downloaded =false;
105     newelement->songId = new QString(item->text());
106     newelement->played = false;
107     newelement->server = new QUrl();
108     newelement->streamkey = new QString("noneatm");
109     newelement->bufferready = false;
110     newelement->type = playlist::EStream;
111     pList->append(newelement);
112     gs->getSong(item->text());
113     emit this->freeze();
114     this->currentSkeyItem = pList->size()-1;
115     return pList->size()-1;
116 }
117
118 void playlist::downloadDone(int position)
119 {
120     if(this->existAt(position+1) && this->currentSkeyItem == -1)
121         beginDownload(position+1);
122     else
123         this->currentdownloaditem = -1;
124     pList->at(position)->downloaded = true;
125 }
126 void playlist::networkReplyFinish()
127 {
128     qDebug() << "finish";
129     QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
130     if(url.toUrl().isValid())
131     {
132         QNetworkRequest req;
133         req.setUrl(url.toUrl());
134         qDebug() << url;
135         reply = manager->get(req);
136         startStreamT = QTime::currentTime();
137         //connect(reply,SIGNAL(finished()),this,SLOT(start()));
138         connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
139     }
140 }
141
142 void playlist::downloadSlot(qint64 b, qint64 t)
143 {
144     //qDebug() << "Download: " << b << "Total: " << t;
145     if(t != 0)
146     {
147         emit this->downloadProgress(this->currentdownloaditem,b,t);
148         pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
149         //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
150         if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
151             //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
152         {
153             this->setBufferRdy(this->currentdownloaditem);
154             emit this->bufferReady(this->currentdownloaditem);
155
156             qDebug() << "Buffer Ready";
157         }
158         if (b==t)
159         {
160             emit this->downloadComplete(this->currentdownloaditem);
161             //emit this->bufferReady(this->currentdownloaditem);
162         }
163     }
164 }