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