3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "aggregatevideosource.h"
26 AggregateVideoSource::AggregateVideoSource(QObject *parent) :
28 unwatched(false), hasMore(true) { }
30 void AggregateVideoSource::loadVideos(int max, int startIndex) {
31 QSqlDatabase db = Database::instance().getConnection();
33 QString sql = "select v.video_id,"
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 ";
48 sql += " from subscriptions_videos v order by published desc ";
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());
67 QString thumbString = query.value(7).toString();
68 if (thumbString.startsWith('[')) {
69 const auto thumbs = QJsonDocument::fromJson(thumbString.toUtf8()).array();
70 for (const auto &t : thumbs) {
71 video->addThumb(t["width"].toInt(), t["height"].toInt(), t["url"].toString());
75 video->addThumb(0, 0, thumbString);
78 video->setViewCount(query.value(8).toInt());
79 video->setDuration(query.value(9).toInt());
83 hasMore = videos.size() >= max;
85 emit gotVideos(videos);
86 emit finished(videos.size());
89 bool AggregateVideoSource::hasMoreVideos() {
93 void AggregateVideoSource::abort() { }