]> git.sur5r.net Git - minitube/blob - src/ytsinglevideosource.cpp
Imported Upstream version 2.0
[minitube] / src / ytsinglevideosource.cpp
1 #include "ytsinglevideosource.h"
2 #include <QtXml>
3 #include "networkaccess.h"
4 #include "video.h"
5 #include "ytfeedreader.h"
6
7 namespace The {
8 NetworkAccess* http();
9 }
10
11 YTSingleVideoSource::YTSingleVideoSource(QObject *parent) : VideoSource(parent) {
12     skip = 0;
13     max = 0;
14 }
15
16 void YTSingleVideoSource::loadVideos(int max, int skip) {
17     aborted = false;
18     this->skip = skip;
19     this->max = max;
20
21     QString s;
22     if (skip == 1) s = "http://gdata.youtube.com/feeds/api/videos/" + videoId;
23     else s = QString("http://gdata.youtube.com/feeds/api/videos/%1/related").arg(videoId);
24     QUrl url(s);
25     url.addQueryItem("v", "2");
26
27     if (skip != 1) {
28         url.addQueryItem("max-results", QString::number(max));
29         url.addQueryItem("start-index", QString::number(skip-1));
30     }
31
32     QObject *reply = The::http()->get(url);
33     connect(reply, SIGNAL(data(QByteArray)), SLOT(parse(QByteArray)));
34     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
35 }
36
37 void YTSingleVideoSource::abort() {
38     aborted = true;
39 }
40
41 const QStringList & YTSingleVideoSource::getSuggestions() {
42     QStringList *l = new QStringList();
43     return *l;
44 }
45
46 QString YTSingleVideoSource::getName() {
47     return name;
48 }
49
50 void YTSingleVideoSource::parse(QByteArray data) {
51     if (aborted) return;
52
53     YTFeedReader reader(data);
54     QList<Video*> videos = reader.getVideos();
55
56     if (name.isEmpty() && !videos.isEmpty() && skip == 1) {
57         name = videos.first()->title();
58         emit nameChanged(name);
59     }
60
61     emit gotVideos(videos);
62
63     if (skip == 1) loadVideos(max - 1, 2);
64     else if (skip == 2) emit finished(videos.size() + 1);
65     else emit finished(videos.size());
66 }
67
68 void YTSingleVideoSource::requestError(QNetworkReply *reply) {
69     emit error(reply->errorString());
70 }