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