]> git.sur5r.net Git - minitube/blobdiff - src/mediaview.cpp
New upstream version 3.8
[minitube] / src / mediaview.cpp
index c06444f0d65512e5d158fb721969257e293b7d7e..e0aebfd8d8b47ec4b66d1a53bbe1e87eecaa3fba 100644 (file)
@@ -45,7 +45,6 @@ $END_LICENSE */
 #include "videosource.h"
 #include "ytchannel.h"
 #include "ytsearch.h"
-#include "ytsinglevideosource.h"
 #ifdef APP_SNAPSHOT
 #include "snapshotsettings.h"
 #endif
@@ -53,15 +52,10 @@ $END_LICENSE */
 #include "idle.h"
 #include "videodefinition.h"
 
-#include "ivchannelsource.h"
-#include "ivsearch.h"
-#include "ivsinglevideosource.h"
+#include "searchvideosource.h"
+#include "singlevideosource.h"
 #include "videoapi.h"
 
-#include "ytjschannelsource.h"
-#include "ytjssearch.h"
-#include "ytjssinglevideosource.h"
-
 MediaView *MediaView::instance() {
     static MediaView *i = new MediaView();
     return i;
@@ -190,25 +184,14 @@ void MediaView::initialize() {
     QAction *leftAction = new QAction(tr("Rewind %1 seconds").arg(10));
     leftAction->setShortcut(Qt::Key_Left);
     leftAction->setAutoRepeat(false);
-    connect(leftAction, &QAction::triggered, this, [this] {
-        qint64 position = media->position();
-        position -= 10000;
-        if (position < 0) position = 0;
-        media->seek(position);
-    });
+    connect(leftAction, &QAction::triggered, this, [this] { media->relativeSeek(-10000); });
     addAction(leftAction);
     playingVideoActions << leftAction;
 
     QAction *rightAction = new QAction(tr("Fast forward %1 seconds").arg(10));
     rightAction->setShortcut(Qt::Key_Right);
     rightAction->setAutoRepeat(false);
-    connect(rightAction, &QAction::triggered, this, [this] {
-        qint64 position = media->position();
-        position += 10000;
-        qint64 duration = media->duration();
-        if (position > duration) position = duration;
-        media->seek(position);
-    });
+    connect(rightAction, &QAction::triggered, this, [this] { media->relativeSeek(10000); });
     addAction(rightAction);
     playingVideoActions << rightAction;
 }
@@ -229,25 +212,9 @@ SearchParams *MediaView::getSearchParams() {
     VideoSource *videoSource = playlistModel->getVideoSource();
     if (!videoSource) return nullptr;
     auto clazz = videoSource->metaObject()->className();
-    if (clazz == QLatin1String("YTSearch")) {
-        auto search = qobject_cast<YTSearch *>(videoSource);
-        return search->getSearchParams();
-    }
-    if (clazz == QLatin1String("IVSearch")) {
-        auto search = qobject_cast<IVSearch *>(videoSource);
-        return search->getSearchParams();
-    }
-    if (clazz == QLatin1String("IVChannelSource")) {
-        auto search = qobject_cast<IVChannelSource *>(videoSource);
-        return search->getSearchParams();
-    }
-    if (clazz == QLatin1String("YTJSSearch")) {
-        auto search = qobject_cast<YTJSSearch *>(videoSource);
-        return search->getSearchParams();
-    }
-    if (clazz == QLatin1String("YTJSChannelSource")) {
-        auto search = qobject_cast<YTJSChannelSource *>(videoSource);
-        return search->getSearchParams();
+    if (clazz == QLatin1String("SearchVideoSource")) {
+        auto search = qobject_cast<SearchVideoSource *>(videoSource);
+        if (search) return search->getSearchParams();
     }
     return nullptr;
 }
@@ -258,21 +225,9 @@ void MediaView::search(SearchParams *searchParams) {
             searchParams->keywords().startsWith("https://")) {
             QString videoId = YTSearch::videoIdFromUrl(searchParams->keywords());
             if (!videoId.isEmpty()) {
-                VideoSource *singleVideoSource = nullptr;
-                if (VideoAPI::impl() == VideoAPI::YT3) {
-                    auto source = new YTSingleVideoSource(this);
-                    source->setVideoId(videoId);
-                    singleVideoSource = source;
-                } else if (VideoAPI::impl() == VideoAPI::IV) {
-                    auto source = new IVSingleVideoSource(this);
-                    source->setVideoId(videoId);
-                    singleVideoSource = source;
-                } else if (VideoAPI::impl() == VideoAPI::JS) {
-                    auto source = new YTJSSingleVideoSource(this);
-                    source->setVideoId(videoId);
-                    singleVideoSource = source;
-                }
-                setVideoSource(singleVideoSource);
+                auto source = new SingleVideoSource(this);
+                source->setVideoId(videoId);
+                setVideoSource(source);
 
                 QTime tstamp = YTSearch::videoTimestampFromUrl(searchParams->keywords());
                 pauseTime = QTime(0, 0).msecsTo(tstamp);
@@ -281,25 +236,7 @@ void MediaView::search(SearchParams *searchParams) {
         }
     }
 
-    VideoSource *search = nullptr;
-    if (VideoAPI::impl() == VideoAPI::YT3) {
-        YTSearch *ytSearch = new YTSearch(searchParams);
-        ytSearch->setAsyncDetails(true);
-        connect(ytSearch, SIGNAL(gotDetails()), playlistModel, SLOT(emitDataChanged()));
-        search = ytSearch;
-    } else if (VideoAPI::impl() == VideoAPI::IV) {
-        if (searchParams->channelId().isEmpty()) {
-            search = new IVSearch(searchParams);
-        } else {
-            search = new IVChannelSource(searchParams);
-        }
-    } else if (VideoAPI::impl() == VideoAPI::JS) {
-        if (searchParams->channelId().isEmpty()) {
-            search = new YTJSSearch(searchParams);
-        } else {
-            search = new YTJSChannelSource(searchParams);
-        }
-    }
+    VideoSource *search = new SearchVideoSource(searchParams);
     setVideoSource(search);
 }
 
@@ -414,6 +351,7 @@ void MediaView::mediaStateChanged(Media::State state) {
     if (pauseTime > 0 && (state == Media::PlayingState || state == Media::BufferingState)) {
         qDebug() << "Seeking to" << pauseTime;
         media->seek(pauseTime);
+        media->play();
         pauseTime = 0;
     }
     if (state == Media::PlayingState) {
@@ -422,7 +360,7 @@ void MediaView::mediaStateChanged(Media::State state) {
         handleError(media->errorString());
     }
 
-    bool enablePlayingVideoActions = state == Media::PlayingState || state == Media::PausedState;
+    bool enablePlayingVideoActions = state != Media::StoppedState;
     for (QAction *action : qAsConst(playingVideoActions))
         action->setEnabled(enablePlayingVideoActions);
 
@@ -444,9 +382,12 @@ void MediaView::pause() {
     default:
         if (pauseTimer.hasExpired(60000)) {
             pauseTimer.invalidate();
-            connect(playlistModel->activeVideo(), &Video::gotStreamUrl, this,
-                    &MediaView::resumeWithNewStreamUrl);
-            playlistModel->activeVideo()->loadStreamUrl();
+            auto activeVideo = playlistModel->activeVideo();
+            if (activeVideo) {
+                connect(activeVideo, &Video::gotStreamUrl, this,
+                        &MediaView::resumeWithNewStreamUrl);
+                activeVideo->loadStreamUrl();
+            }
         } else
             media->play();
         break;
@@ -480,7 +421,7 @@ void MediaView::stop() {
     demoTimer->stop();
 #endif
 
-    for (QAction *action : currentVideoActions)
+    for (QAction *action : qAsConst(currentVideoActions))
         action->setEnabled(false);
 
     QAction *a = MainWindow::instance()->getAction("download");
@@ -663,14 +604,7 @@ void MediaView::skipBackward() {
 }
 
 void MediaView::onAboutToFinish() {
-    qint64 currentTime = media->position();
-    qint64 totalTime = media->duration();
-    // qDebug() << __PRETTY_FUNCTION__ << currentTime << totalTime;
-    if (totalTime < 1 || currentTime + 10000 < totalTime) {
-        // QTimer::singleShot(500, this, SLOT(playbackResume()));
-        media->seek(currentTime);
-        media->play();
-    }
+
 }
 
 void MediaView::onPlaybackFinished() {
@@ -760,7 +694,7 @@ void MediaView::moveUpSelected() {
     if (!playlistView->selectionModel()->hasSelection()) return;
 
     QModelIndexList indexes = playlistView->selectionModel()->selectedIndexes();
-    qStableSort(indexes.begin(), indexes.end());
+    std::stable_sort(indexes.begin(), indexes.end());
     playlistModel->move(indexes, true);
 
     // set current index after row moves to something more intuitive
@@ -773,7 +707,8 @@ void MediaView::moveDownSelected() {
     if (!playlistView->selectionModel()->hasSelection()) return;
 
     QModelIndexList indexes = playlistView->selectionModel()->selectedIndexes();
-    qStableSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
+    std::stable_sort(indexes.begin(), indexes.end(),
+                     [](const QModelIndex &a, const QModelIndex &b) { return b < a; });
     playlistModel->move(indexes, false);
 
     // set current index after row moves to something more intuitive
@@ -956,20 +891,9 @@ void MediaView::relatedVideos() {
     Video *video = playlistModel->activeVideo();
     if (!video) return;
 
-    if (VideoAPI::impl() == VideoAPI::YT3) {
-        YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource();
-        singleVideoSource->setVideo(video->clone());
-        singleVideoSource->setAsyncDetails(true);
-        setVideoSource(singleVideoSource);
-    } else if (VideoAPI::impl() == VideoAPI::IV) {
-        auto source = new IVSingleVideoSource(this);
-        source->setVideo(video->clone());
-        setVideoSource(source);
-    } else if (VideoAPI::impl() == VideoAPI::JS) {
-        auto source = new YTJSSingleVideoSource(this);
-        source->setVideo(video->clone());
-        setVideoSource(source);
-    }
+    auto source = new SingleVideoSource(this);
+    source->setVideo(video->clone());
+    setVideoSource(source);
 
     MainWindow::instance()->getAction("relatedVideos")->setEnabled(false);
 }