Fix formating for viewport area (Playlist)
[groove] / playlist.cpp
1 #include "playlist.h"
2
3 playlist::playlist(QObject *parent) :
4     QAbstractTableModel(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
14 //Implemented model class information
15 QVariant playlist::data(const QModelIndex &index, int role) const
16 {
17     QVariant dat;
18     playlist* play = (playlist *)index.model();
19     if(play->existAt(index.row()))
20     {
21         if (!index.isValid())
22             return QVariant();
23         if (role == Qt::TextAlignmentRole) {
24             return int(Qt::AlignLeft | Qt::AlignVCenter);
25         } else if (role == Qt::DisplayRole) {
26             switch(index.column())
27             {
28             case sName:
29                 dat = QVariant(*play->pList->at(index.row())->name);
30                 break;
31             case sID:
32                 dat = QVariant(*play->pList->at(index.row())->songId);
33                 break;
34             case sKey:
35                 dat = QVariant(*play->pList->at(index.row())->streamkey);
36                 break;
37             case sDownloaded:
38                 dat = QVariant(play->pList->at(index.row())->downloaded);
39                 break;
40             case sReady:
41                 dat = QVariant(play->pList->at(index.row())->bufferready);
42                 break;
43             case sURL:
44                 dat = QVariant(play->pList->at(index.row())->server->toString());
45                 break;
46             case sPlayed:
47                 dat = QVariant(play->pList->at(index.row())->played);
48                 break;
49             default:
50                 dat = QVariant();
51             }
52         } else
53             dat = QVariant();
54     }
55     else
56         dat = QVariant();
57     return dat;
58 }
59 int playlist::rowCount(const QModelIndex &) const
60 {
61     return pList->size();
62 }
63 int playlist::columnCount(const QModelIndex &) const
64 {
65     return PLAYLISTENUMS;
66 }
67
68
69 QList<playlist::songElement *>* playlist::getList()
70 {
71     return pList;
72 }
73
74 void playlist::markPlayed(int position)
75 {
76     if(0 <= position && position < pList->size())
77     {
78         pList->at(position)->played = true;
79         this->freeMemory(position);
80     }
81 }
82 void playlist::freeMemory(int position)
83 {
84    pList->at(position)->downloaded = false;
85    pList->at(position)->bufferready = false;
86    delete pList->at(position)->buffer;
87    pList->at(position)->buffer = new QBuffer();
88 }
89 bool playlist::existAt(int position)
90 {
91     if(position < 0)
92         return false;
93     return (pList->size() > position);
94 }
95
96 int playlist::currentplaying()
97 {
98     return this->currentplayingitem;
99 }
100 bool playlist::bReady(int b)
101 {
102     if(pList->size() > b)
103         return pList->at(b)->bufferready;
104     else
105         return false;
106 }
107 void playlist::setBufferRdy(int b)
108 {
109     pList->at(b)->bufferready = true;
110 }
111 void playlist::setCurrentPlaying(int position)
112 {
113     if(this->existAt(position))
114     {
115         this->currentplayingitem = position;
116         if(!pList->at(position)->downloaded && this->currentdownloaditem != this->currentplayingitem)
117             this->beginDownload(position);
118         /*if(pList->at(position)->bufferready == false &&)
119         {
120             if(!pList->at(position)->downloaded)
121                 this->beginDownload(position);
122         }
123         else
124             emit this->bufferReady(position);
125         */
126         return;
127     }
128     else
129     {
130     if(position == -1)
131         {
132         this->currentplayingitem = -1;
133     }
134     else
135         return;
136     }
137 }
138 QIODevice * playlist::getBuffer(int position)
139 {
140     return pList->at(position)->buffer;
141 }
142
143 void playlist::beginDownload(int position)
144 {
145     this->currentdownloaditem = position;
146     qDebug() << "StartDownlaod:" << pList->at(position)->songId;
147     QNetworkRequest req;
148     req.setUrl(*pList->at(currentdownloaditem)->server);
149     qDebug() << pList->at(currentdownloaditem)->server;
150     req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
151     if(reply)
152     {
153         reply->disconnect();
154         reply->deleteLater();
155     }
156     reply = manager->post(req,QString("streamKey=" + pList->at(this->currentdownloaditem)->streamkey->toAscii()).toAscii());
157     pList->at(this->currentdownloaditem)->buffer->open(QBuffer::ReadWrite | QBuffer::Truncate);
158     connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
159     connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
160     connect(this,SIGNAL(downloadComplete(int)),this,SLOT(downloadDone(int)));
161     connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
162     startStreamT = QTime::currentTime();
163 }
164 void playlist::getNError(QNetworkReply::NetworkError error)
165 {
166     qDebug() << "Network Error (if this is 99 then it will retry" << error;
167     if(error == QNetworkReply::UnknownNetworkError && this->currentdownloaditem != -1)
168         beginDownload(this->currentdownloaditem);
169
170 }
171
172 void playlist::setGscom(gscom *comm)
173 {
174     gs = comm;
175     connect(gs,SIGNAL(sKeyFound()),this,SLOT(skeyFound()));
176 }
177 void playlist::skeyFound()
178 {
179     emit this->freeze(false);
180     pList->at(this->currentSkeyItem)->streamkey = new QString(gs->streamID);
181     pList->at(this->currentSkeyItem)->server = new QUrl(gs->sku);
182     if(this->currentdownloaditem == -1)
183         this->beginDownload(this->currentSkeyItem);
184     else
185         if(this->currentplaying() == this->currentSkeyItem)
186             this->beginDownload(this->currentSkeyItem);
187     this->currentSkeyItem = -1;
188 }
189
190 int playlist::addSong(QStandardItem *item, QString name)
191 {
192     playlist::songElement *newelement = new playlist::songElement;
193     newelement->name = new QString(name);
194     newelement->buffer = new QBuffer();
195     newelement->downloaded =false;
196     newelement->songId = new QString(item->text());
197     newelement->played = false;
198     newelement->server = new QUrl();
199     newelement->streamkey = new QString("noneatm");
200     newelement->bufferready = false;
201     newelement->type = playlist::EStream;
202     pList->append(newelement);
203     gs->getSong(item->text());
204     emit this->rowsInserted(QModelIndex(),pList->size(),pList->size());
205     this->currentSkeyItem = pList->size()-1;
206     emit this->freeze(true);
207     return pList->size()-1;
208 }
209
210 void playlist::downloadDone(int position)
211 {
212     if(this->existAt(position+1) && this->currentSkeyItem == -1 && !pList->at(position+1)->downloaded && this->currentdownloaditem != position+1)
213         beginDownload(position+1);
214     else
215         this->currentdownloaditem = -1;
216     pList->at(position)->downloaded = true;
217 }
218 void playlist::networkReplyFinish()
219 {
220     qDebug() << "finish";
221     QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
222     if(url.toUrl().isValid())
223     {
224         QNetworkRequest req;
225         req.setUrl(url.toUrl());
226         qDebug() << url;
227         if(reply)
228         {
229             reply->disconnect();
230             reply->deleteLater();
231         }
232         reply = manager->get(req);
233         startStreamT = QTime::currentTime();
234         //connect(reply,SIGNAL(finished()),this,SLOT(start()));
235         connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadSlot(qint64,qint64)));
236         connect(reply,SIGNAL(finished()),this,SLOT(networkReplyFinish()));
237         connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(getNError(QNetworkReply::NetworkError)));
238     }
239 }
240
241 void playlist::downloadSlot(qint64 b, qint64 t)
242 {
243     //qDebug() << "Download: " << b << "Total: " << t;
244     if(t != 0)
245     {
246         emit this->downloadProgress(this->currentdownloaditem,b,t);
247         if(existAt(this->currentdownloaditem))
248         {
249             pList->at(this->currentdownloaditem)->buffer->buffer().append(reply->readAll());
250             //qDebug() << !pList->at(this->currentdownloaditem)->bufferready << this->currentdownloaditem;
251             if ( b >= t*0.05 && !pList->at(this->currentdownloaditem)->bufferready)
252                 //if(!pList->at(currentdownloaditem)->bufferready && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
253             {
254                 this->setBufferRdy(this->currentdownloaditem);
255                 emit this->bufferReady(this->currentdownloaditem);
256
257                 qDebug() << "Buffer Ready";
258             }
259             if (b==t)
260             {
261             emit this->downloadComplete(this->currentdownloaditem);
262             //emit this->bufferReady(this->currentdownloaditem);
263             }
264         }
265     }
266 }