1 #include "singlevideosource.h"
6 #include "ivsinglevideosource.h"
7 #include "ytjssinglevideosource.h"
8 #include "ytsinglevideosource.h"
10 SingleVideoSource::SingleVideoSource(QObject *parent) : VideoSource(parent) {}
12 void SingleVideoSource::setVideo(Video *value) {
13 emittedVideoIds.clear();
15 // some wrapped source could delete the video
16 connect(video, &QObject::destroyed, this, [this] {
20 videoId = video->getId();
23 void SingleVideoSource::setVideoId(const QString &value) {
24 emittedVideoIds.clear();
28 void SingleVideoSource::loadVideos(int max, int startIndex) {
31 if (VideoAPI::impl() == VideoAPI::YT3) {
32 auto s = setupSource(new YTSingleVideoSource());
33 s->setAsyncDetails(true);
35 } else if (VideoAPI::impl() == VideoAPI::IV) {
36 source = setupSource(new IVSingleVideoSource());
37 } else if (VideoAPI::impl() == VideoAPI::JS) {
38 source = setupSource(new YTJSSingleVideoSource());
40 connectSource(max, startIndex);
42 source->loadVideos(max, startIndex);
45 bool SingleVideoSource::hasMoreVideos() {
46 if (source) return source->hasMoreVideos();
47 return VideoSource::hasMoreVideos();
50 QString SingleVideoSource::getName() {
51 if (source) return source->getName();
55 const QList<QAction *> &SingleVideoSource::getActions() {
56 if (source) return source->getActions();
57 return VideoSource::getActions();
60 int SingleVideoSource::maxResults() {
61 if (source) return source->maxResults();
62 return VideoSource::maxResults();
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) {
69 // avoid emitting duplicate videos
70 auto videosCopy = videos;
71 for (auto v : videos) {
72 if (emittedVideoIds.contains(v->getId())) {
73 videosCopy.removeOne(v);
75 emittedVideoIds << v->getId();
78 emit gotVideos(videosCopy);
80 connect(source, &VideoSource::error, this, [this, max, startIndex](auto msg) {
81 qDebug() << source << msg;
83 if (QLatin1String(source->metaObject()->className()).startsWith(QLatin1String("YTJS"))) {
84 qDebug() << "Falling back to IV";
85 source->deleteLater();
87 source = setupSource(new IVSingleVideoSource());
89 connectSource(max, startIndex);
90 source->loadVideos(max, startIndex);