Multiple download fix, Added pause button, Added next button
[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->abort();
77         delete reply;
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     startStreamT = QTime::currentTime();
85 }
86
87 void playlist::setGscom(gscom *comm)
88 {
89     gs = comm;
90     connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
91 }
92 void playlist::skeyFound()
93 {
94     emit this->freeze(false);
95     pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
96     pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
97     if(this->currentdownloaditem == -1)
98         this->beginDownload(this->currentSkeyItem);
99     else
100         if(this->currentplaying() == this->currentSkeyItem)
101             this->beginDownload(this->currentSkeyItem);
102     this->currentSkeyItem = -1;
103 }
104
105 int playlist::addSong(QStandardItem *item)
106 {
107     playlist::songElement *newelement = new playlist::songElement;
108     newelement->buffer = new QBuffer();
109     newelement->downloaded =false;
110     newelement->songId = new QString(item->text());
111     newelement->played = false;
112     newelement->server = new QUrl();
113     newelement->streamkey = new QString("noneatm");
114     newelement->bufferready = false;
115     newelement->type = playlist::EStream;
116     pList->append(newelement);
117     gs->getSong(item->text());
118
119     this->currentSkeyItem = pList->size()-1;
120     emit this->freeze(true);
121     return pList->size()-1;
122 }
123
124 void playlist::downloadDone(int position)
125 {
126     if(this->existAt(position+1) && this->currentSkeyItem == -1)
127         beginDownload(position+1);
128     else
129         this->currentdownloaditem = -1;
130     pList->at(position)->downloaded = true;
131 }
132 void playlist::networkReplyFinish()
133 {
134     qDebug() << "finish";
135     QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
136     if(url.toUrl().isValid())
137     {
138         QNetworkRequest req;
139         req.setUrl(url.toUrl());
140         qDebug() << url;
141         reply = manager->get(req);
142         startStreamT = QTime::currentTime();
143         //connect(reply,SIGNAL(finished()),this,SLOT(start()));
144         connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
145     }
146 }
147
148 void playlist::downloadSlot(qint64 b, qint64 t)
149 {
150     //qDebug() << "Download: " << b << "Total: " << t;
151     if(t != 0)
152     {
153         emit this->downloadProgress(this->currentdownloaditem,b,t);
154         if(existAt(this->currentdownloaditem))
155         {
156             pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
157             //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
158             if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
159                 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
160             {
161                 this->setBufferRdy(this->currentdownloaditem);
162                 emit this->bufferReady(this->currentdownloaditem);
163
164                 qDebug() << "Buffer Ready";
165             }
166             if (b==t)
167             {
168             emit this->downloadComplete(this->currentdownloaditem);
169             //emit this->bufferReady(this->currentdownloaditem);
170             }
171         }
172     }
173 }