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