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