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