1 #include "singlevideosource.h"
2
3 #include "video.h"
4 #include "videoapi.h"
5
6 #include "ivsinglevideosource.h"
7 #include "ytjssinglevideosource.h"
8 #include "ytsinglevideosource.h"
9
SingleVideoSource(QObject * parent)10 SingleVideoSource::SingleVideoSource(QObject *parent) : VideoSource(parent) {}
11
setVideo(Video * value)12 void SingleVideoSource::setVideo(Video *value) {
13 emittedVideoIds.clear();
14 video = value;
15 // some wrapped source could delete the video
16 connect(video, &QObject::destroyed, this, [this] {
17 video = nullptr;
18 videoId.clear();
19 });
20 videoId = video->getId();
21 }
22
setVideoId(const QString & value)23 void SingleVideoSource::setVideoId(const QString &value) {
24 emittedVideoIds.clear();
25 videoId = value;
26 }
27
loadVideos(int max,int startIndex)28 void SingleVideoSource::loadVideos(int max, int startIndex) {
29 if (!source) {
30 aborted = false;
31 if (VideoAPI::impl() == VideoAPI::YT3) {
32 auto s = setupSource(new YTSingleVideoSource());
33 s->setAsyncDetails(true);
34 source = s;
35 } else if (VideoAPI::impl() == VideoAPI::IV) {
36 source = setupSource(new IVSingleVideoSource());
37 } else if (VideoAPI::impl() == VideoAPI::JS) {
38 source = setupSource(new YTJSSingleVideoSource());
39 }
40 connectSource(max, startIndex);
41 }
42 source->loadVideos(max, startIndex);
43 }
44
hasMoreVideos()45 bool SingleVideoSource::hasMoreVideos() {
46 if (source) return source->hasMoreVideos();
47 return VideoSource::hasMoreVideos();
48 }
49
getName()50 QString SingleVideoSource::getName() {
51 if (source) return source->getName();
52 return QString();
53 }
54
getActions()55 const QList<QAction *> &SingleVideoSource::getActions() {
56 if (source) return source->getActions();
57 return VideoSource::getActions();
58 }
59
maxResults()60 int SingleVideoSource::maxResults() {
61 if (source) return source->maxResults();
62 return VideoSource::maxResults();
63 }
64
connectSource(int max,int startIndex)65 void SingleVideoSource::connectSource(int max, int startIndex) {
66 connect(source, &VideoSource::finished, this, &VideoSource::finished);
67 connect(source, &VideoSource::gotVideos, this, [this](auto &videos) {
68 if (aborted) return;
69 // avoid emitting duplicate videos
70 auto videosCopy = videos;
71 for (auto v : videos) {
72 if (emittedVideoIds.contains(v->getId())) {
73 videosCopy.removeOne(v);
74 } else {
75 emittedVideoIds << v->getId();
76 }
77 }
78 emit gotVideos(videosCopy);
79 });
80 connect(source, &VideoSource::error, this, [this, max, startIndex](auto msg) {
81 qDebug() << source << msg;
82 if (aborted) return;
83 if (QLatin1String(source->metaObject()->className()).startsWith(QLatin1String("YTJS"))) {
84 qDebug() << "Falling back to IV";
85 source->deleteLater();
86
87 source = setupSource(new IVSingleVideoSource());
88
89 connectSource(max, startIndex);
90 source->loadVideos(max, startIndex);
91 } else {
92 emit error(msg);
93 }
94 });
95 }
96