]> git.sur5r.net Git - minitube/blob - src/aggregatevideosource.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / aggregatevideosource.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 "aggregatevideosource.h"
22 #include "video.h"
23 #include "database.h"
24 #include <QtSql>
25
26 AggregateVideoSource::AggregateVideoSource(QObject *parent) :
27     VideoSource(parent),
28     unwatched(false), hasMore(true) { }
29
30 void AggregateVideoSource::loadVideos(int max, int startIndex) {
31     QSqlDatabase db = Database::instance().getConnection();
32     QSqlQuery query(db);
33     QString sql = "select v.video_id,"
34             "v.published,"
35             "v.title,"
36             "v.author,"
37             "v.user_id,"
38             "v.description,"
39             "v.url,"
40             "v.thumb_url,"
41             "v.views,"
42             "v.duration";
43     if (unwatched)
44         sql += " from subscriptions_videos v, subscriptions s where v.channel_id=s.id "
45                 "and v.added>s.watched and v.published>s.watched and v.watched=0 "
46                 "order by v.published desc ";
47     else
48         sql += " from subscriptions_videos v order by published desc ";
49     sql += "limit ?,?";
50     query.prepare(sql);
51     query.bindValue(0, startIndex - 1);
52     query.bindValue(1, max);
53     bool success = query.exec();
54     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
55     QVector<Video*> videos;
56     videos.reserve(query.size());
57     while (query.next()) {
58         Video *video = new Video();
59         video->setId(query.value(0).toString());
60         video->setPublished(QDateTime::fromTime_t(query.value(1).toUInt()));
61         video->setTitle(query.value(2).toString());
62         video->setChannelTitle(query.value(3).toString());
63         video->setChannelId(query.value(4).toString());
64         video->setDescription(query.value(5).toString());
65         video->setWebpage(query.value(6).toString());
66         video->setThumbnailUrl(query.value(7).toString());
67         video->setViewCount(query.value(8).toInt());
68         video->setDuration(query.value(9).toInt());
69         videos << video;
70     }
71
72     hasMore = videos.size() >= max;
73
74     emit gotVideos(videos);
75     emit finished(videos.size());
76 }
77
78 bool AggregateVideoSource::hasMoreVideos() {
79     return hasMore;
80 }
81
82 void AggregateVideoSource::abort() { }