]> git.sur5r.net Git - minitube/blobdiff - src/mediaview.cpp
New upstream version 3.5
[minitube] / src / mediaview.cpp
index e6343ab9b2c498507ff3973611047a040f157c1b..bf60db1d42aeddf1cdbff337c5aa8b6357f93ac8 100644 (file)
@@ -34,6 +34,7 @@ $END_LICENSE */
 #include "videoarea.h"
 #ifdef APP_ACTIVATION
 #include "activation.h"
+#include "activationview.h"
 #endif
 #ifdef APP_EXTRA
 #include "extra.h"
@@ -52,6 +53,11 @@ $END_LICENSE */
 #include "idle.h"
 #include "videodefinition.h"
 
+#include "ivchannelsource.h"
+#include "ivsearch.h"
+#include "ivsinglevideosource.h"
+#include "videoapi.h"
+
 MediaView *MediaView::instance() {
     static MediaView *i = new MediaView();
     return i;
@@ -132,7 +138,16 @@ void MediaView::initialize() {
 #ifdef APP_ACTIVATION
     demoTimer = new QTimer(this);
     demoTimer->setSingleShot(true);
-    connect(demoTimer, &QTimer::timeout, mainWindow, &MainWindow::showActivationView,
+    connect(
+            demoTimer, &QTimer::timeout, this,
+            [this] {
+                if (media->state() != Media::PlayingState) return;
+                media->pause();
+                connect(
+                        ActivationView::instance(), &ActivationView::done, media,
+                        [this] { media->play(); }, Qt::UniqueConnection);
+                MainWindow::instance()->showActivationView();
+            },
             Qt::QueuedConnection);
 #endif
 
@@ -153,6 +168,45 @@ void MediaView::initialize() {
     for (auto *name : videoActionNames) {
         currentVideoActions.append(mainWindow->getAction(name));
     }
+
+    for (int i = 0; i < 10; ++i) {
+        QAction *action = new QAction(QString());
+        action->setShortcut(Qt::Key_0 + i);
+        action->setAutoRepeat(false);
+        connect(action, &QAction::triggered, this, [this, i] {
+            qint64 duration = media->duration();
+            // dur : pos = 100 : i*10
+            qint64 position = (duration * (i * 10)) / 100;
+            media->seek(position);
+        });
+        addAction(action);
+        playingVideoActions << action;
+    }
+
+    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);
+    });
+    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);
+    });
+    addAction(rightAction);
+    playingVideoActions << rightAction;
 }
 
 void MediaView::setMedia(Media *media) {
@@ -169,8 +223,18 @@ void MediaView::setMedia(Media *media) {
 
 SearchParams *MediaView::getSearchParams() {
     VideoSource *videoSource = playlistModel->getVideoSource();
-    if (videoSource && videoSource->metaObject()->className() == QLatin1String("YTSearch")) {
-        YTSearch *search = qobject_cast<YTSearch *>(videoSource);
+    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();
     }
     return nullptr;
@@ -182,19 +246,39 @@ void MediaView::search(SearchParams *searchParams) {
             searchParams->keywords().startsWith("https://")) {
             QString videoId = YTSearch::videoIdFromUrl(searchParams->keywords());
             if (!videoId.isEmpty()) {
-                YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource(this);
-                singleVideoSource->setVideoId(videoId);
+                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;
+                }
                 setVideoSource(singleVideoSource);
+
                 QTime tstamp = YTSearch::videoTimestampFromUrl(searchParams->keywords());
                 pauseTime = QTime(0, 0).msecsTo(tstamp);
                 return;
             }
         }
     }
-    YTSearch *ytSearch = new YTSearch(searchParams);
-    ytSearch->setAsyncDetails(true);
-    connect(ytSearch, SIGNAL(gotDetails()), playlistModel, SLOT(emitDataChanged()));
-    setVideoSource(ytSearch);
+
+    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);
+        }
+    }
+    setVideoSource(search);
 }
 
 void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool back) {
@@ -204,13 +288,6 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool
 
     // qDebug() << "Adding VideoSource" << videoSource->getName() << videoSource;
 
-    YTSearch * ytSearch = qobject_cast<YTSearch *>(videoSource);
-    if (nullptr != ytSearch) {
-        if (!ytSearch->getSearchParams()->channelId().isEmpty()) {
-            updateSubscriptionActionForChannel(ytSearch->getSearchParams()->channelId());
-        }
-    }
-
     if (addToHistory) {
         int currentIndex = getHistoryIndex();
         if (currentIndex >= 0 && currentIndex < history.size() - 1) {
@@ -239,13 +316,17 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool
         }
     }
 
+    SearchParams *searchParams = getSearchParams();
+
     sidebar->showPlaylist();
-    sidebar->getRefineSearchWidget()->setSearchParams(getSearchParams());
+    sidebar->getRefineSearchWidget()->setSearchParams(searchParams);
     sidebar->hideSuggestions();
     sidebar->getHeader()->updateInfo();
 
-    SearchParams *searchParams = getSearchParams();
     bool isChannel = searchParams && !searchParams->channelId().isEmpty();
+    if (isChannel) {
+        updateSubscriptionActionForChannel(searchParams->channelId());
+    }
     playlistView->setClickableAuthors(!isChannel);
 }
 
@@ -319,6 +400,10 @@ void MediaView::mediaStateChanged(Media::State state) {
         handleError(media->errorString());
     }
 
+    bool enablePlayingVideoActions = state == Media::PlayingState || state == Media::PausedState;
+    for (QAction *action : qAsConst(playingVideoActions))
+        action->setEnabled(enablePlayingVideoActions);
+
     if (state == Media::PlayingState) {
         bool res = Idle::preventDisplaySleep(QString("%1 is playing").arg(Constants::NAME));
         if (!res) qWarning() << "Error disabling idle display sleep" << Idle::displayErrorMessage();
@@ -497,8 +582,8 @@ void MediaView::gotStreamUrl(const QString &streamUrl, const QString &audioUrl)
     }
 
 #ifdef APP_ACTIVATION
-    if (!Activation::instance().isActivated() && !demoTimer->isActive()) {
-        int ms = (60000 * 5) + (qrand() % (60000 * 5));
+    if (!demoTimer->isActive() && !Activation::instance().isActivated()) {
+        int ms = (60000 * 2) + (QRandomGenerator::global()->generate() % (60000 * 2));
         demoTimer->start(ms);
     }
 #endif
@@ -848,10 +933,18 @@ void MediaView::findVideoParts() {
 void MediaView::relatedVideos() {
     Video *video = playlistModel->activeVideo();
     if (!video) return;
-    YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource();
-    singleVideoSource->setVideo(video->clone());
-    singleVideoSource->setAsyncDetails(true);
-    setVideoSource(singleVideoSource);
+
+    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);
+    }
+
     MainWindow::instance()->getAction("relatedVideos")->setEnabled(false);
 }