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