]> git.sur5r.net Git - minitube/blob - src/ytsinglevideosource.cpp
Preliminary Qt5 support
[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     url.addQueryItem("v", "2");
46
47     if (skip != 1) {
48         url.addQueryItem("max-results", QString::number(max));
49         url.addQueryItem("start-index", QString::number(skip-1));
50     }
51
52     QObject *reply = The::http()->get(url);
53     connect(reply, SIGNAL(data(QByteArray)), SLOT(parse(QByteArray)));
54     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
55 }
56
57 void YTSingleVideoSource::abort() {
58     aborted = true;
59 }
60
61 const QStringList & YTSingleVideoSource::getSuggestions() {
62     QStringList *l = new QStringList();
63     return *l;
64 }
65
66 QString YTSingleVideoSource::getName() {
67     return name;
68 }
69
70 void YTSingleVideoSource::parse(QByteArray data) {
71     if (aborted) return;
72
73     YTFeedReader reader(data);
74     QList<Video*> videos = reader.getVideos();
75
76     if (name.isEmpty() && !videos.isEmpty() && skip == 1) {
77         name = videos.first()->title();
78         emit nameChanged(name);
79     }
80
81     emit gotVideos(videos);
82
83     if (skip == 1) loadVideos(max - 1, 2);
84     else if (skip == 2) emit finished(videos.size() + 1);
85     else emit finished(videos.size());
86 }
87
88 void YTSingleVideoSource::requestError(QNetworkReply *reply) {
89     emit error(reply->errorString());
90 }