]> git.sur5r.net Git - minitube/blob - src/ytsinglevideosource.cpp
Imported Upstream version 2.3
[minitube] / src / ytsinglevideosource.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "ytsinglevideosource.h"
22 #include <QtXml>
23 #include "networkaccess.h"
24 #include "video.h"
25 #include "ytfeedreader.h"
26
27 namespace The {
28 NetworkAccess* http();
29 }
30
31 YTSingleVideoSource::YTSingleVideoSource(QObject *parent) : VideoSource(parent) {
32     skip = 0;
33     max = 0;
34 }
35
36 void YTSingleVideoSource::loadVideos(int max, int skip) {
37     aborted = false;
38     this->skip = skip;
39     this->max = max;
40
41     QString s;
42     if (skip == 1) s = "http://gdata.youtube.com/feeds/api/videos/" + videoId;
43     else s = QString("http://gdata.youtube.com/feeds/api/videos/%1/related").arg(videoId);
44     QUrl url(s);
45 #if QT_VERSION >= 0x050000
46 {
47     QUrl &u = url;
48     QUrlQuery url;
49 #endif
50     url.addQueryItem("v", "2");
51
52     if (skip != 1) {
53         url.addQueryItem("max-results", QString::number(max));
54         url.addQueryItem("start-index", QString::number(skip-1));
55     }
56
57 #if QT_VERSION >= 0x050000
58         u.setQuery(url);
59     }
60 #endif
61     QObject *reply = The::http()->get(url);
62     connect(reply, SIGNAL(data(QByteArray)), SLOT(parse(QByteArray)));
63     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
64 }
65
66 void YTSingleVideoSource::abort() {
67     aborted = true;
68 }
69
70 const QStringList & YTSingleVideoSource::getSuggestions() {
71     QStringList *l = new QStringList();
72     return *l;
73 }
74
75 QString YTSingleVideoSource::getName() {
76     return name;
77 }
78
79 void YTSingleVideoSource::parse(QByteArray data) {
80     if (aborted) return;
81
82     YTFeedReader reader(data);
83     QList<Video*> videos = reader.getVideos();
84
85     if (name.isEmpty() && !videos.isEmpty() && skip == 1) {
86         name = videos.first()->title();
87         emit nameChanged(name);
88     }
89
90     emit gotVideos(videos);
91
92     if (skip == 1) loadVideos(max - 1, 2);
93     else if (skip == 2) emit finished(videos.size() + 1);
94     else emit finished(videos.size());
95 }
96
97 void YTSingleVideoSource::requestError(QNetworkReply *reply) {
98     emit error(reply->errorString());
99 }