]> git.sur5r.net Git - minitube/blobdiff - src/mediaview.cpp
Get a new stream url after long pause
[minitube] / src / mediaview.cpp
index c1617f1e060c991ca029c2946f71fde9aae7523c..a2c4587cc17e231eb52c4934071a244c9403c189 100644 (file)
@@ -50,6 +50,7 @@ $END_LICENSE */
 #include "snapshotsettings.h"
 #endif
 #include "datautils.h"
+#include "compatibility/qurlqueryhelper.h"
 
 namespace The {
 NetworkAccess* http();
@@ -63,12 +64,14 @@ MediaView* MediaView::instance() {
     return i;
 }
 
-MediaView::MediaView(QWidget *parent) : QWidget(parent),
-    stopped(false),
-    #ifdef APP_SNAPSHOT
-    snapshotSettings(0),
-    #endif
-    downloadItem(0) { }
+MediaView::MediaView(QWidget *parent) : QWidget(parent)
+  , stopped(false)
+  , downloadItem(0)
+  #ifdef APP_SNAPSHOT
+  , snapshotSettings(0)
+  #endif
+  , pauseTime(0)
+{ }
 
 void MediaView::initialize() {
     QBoxLayout *layout = new QVBoxLayout(this);
@@ -126,6 +129,7 @@ void MediaView::initialize() {
     QSettings settings;
     splitter->restoreState(settings.value("splitter").toByteArray());
     splitter->setChildrenCollapsible(false);
+    connect(splitter, SIGNAL(splitterMoved(int,int)), SLOT(maybeAdjustWindowSize()));
 
     layout->addWidget(splitter);
 
@@ -212,6 +216,7 @@ void MediaView::search(SearchParams *searchParams) {
 }
 
 void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool back) {
+    Q_UNUSED(back);
     stopped = false;
 
 #ifdef APP_ACTIVATION
@@ -291,20 +296,23 @@ int MediaView::getHistoryIndex() {
 }
 
 void MediaView::appear() {
-    playlistView->setFocus();
     Video *currentVideo = playlistModel->activeVideo();
     if (currentVideo) {
         MainWindow::instance()->setWindowTitle(
                     currentVideo->title() + " - " + Constants::NAME);
-        MainWindow::instance()->showMessage(currentVideo->description());
     }
+
+    // optimize window for 16:9 video
+    QTimer::singleShot(50, this, SLOT(maybeAdjustWindowSize()));
+
+    playlistView->setFocus();
 }
 
 void MediaView::disappear() {
 
 }
 
-void MediaView::handleError(QString message) {
+void MediaView::handleError(const QString &message) {
     qWarning() << __PRETTY_FUNCTION__ << message;
 #ifdef APP_PHONON_SEEK
     mediaObject->play();
@@ -315,9 +323,13 @@ void MediaView::handleError(QString message) {
 
 #ifdef APP_PHONON
 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
-    if (newState == Phonon::PlayingState)
+    if (pauseTime > 0 && (newState == Phonon::PlayingState || newState == Phonon::BufferingState)) {
+        mediaObject->seek(pauseTime);
+        pauseTime = 0;
+    }
+    if (newState == Phonon::PlayingState) {
         videoAreaWidget->showVideo();
-    else if (newState == Phonon::ErrorState) {
+    else if (newState == Phonon::ErrorState) {
         qWarning() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
         if (mediaObject->errorType() == Phonon::FatalError)
             handleError(mediaObject->errorString());
@@ -330,15 +342,20 @@ void MediaView::pause() {
     switch( mediaObject->state() ) {
     case Phonon::PlayingState:
         mediaObject->pause();
+        pauseTimer.start();
         break;
     default:
-        mediaObject->play();
+        if (pauseTimer.hasExpired(60000)) {
+            pauseTimer.invalidate();
+            connect(playlistModel->activeVideo(), SIGNAL(gotStreamUrl(QUrl)), SLOT(resumeWithNewStreamUrl(QUrl)));
+            playlistModel->activeVideo()->loadStreamUrl();
+        } else mediaObject->play();
         break;
     }
 #endif
 }
 
-QRegExp MediaView::wordRE(QString s) {
+QRegExp MediaView::wordRE(const QString &s) {
     return QRegExp("\\W" + s + "\\W?", Qt::CaseInsensitive);
 }
 
@@ -423,7 +440,6 @@ void MediaView::activeRowChanged(int row) {
 
     // video title in titlebar
     MainWindow::instance()->setWindowTitle(video->title() + " - " + Constants::NAME);
-    MainWindow::instance()->showMessage(video->description());
 
     // ensure active item is visible
     if (row != -1) {
@@ -463,6 +479,7 @@ void MediaView::activeRowChanged(int row) {
     if (snapshotSettings) {
         delete snapshotSettings;
         snapshotSettings = 0;
+        MainWindow::instance()->adjustStatusBarVisibility();
     }
 
     // see you in gotStreamUrl...
@@ -644,7 +661,7 @@ void MediaView::playbackFinished() {
     const qint64 currentTime = mediaObject->currentTime();
     qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime() << totalTime;
     // add 10 secs for imprecise Phonon backends (VLC, Xine)
-    if (totalTime < 1 || (currentTime > 0 && currentTime + 10000 < totalTime)) {
+    if (currentTime > 0 && currentTime + 10000 < totalTime) {
         // mediaObject->seek(currentTime);
         QTimer::singleShot(500, this, SLOT(playbackResume()));
     } else {
@@ -865,9 +882,9 @@ void MediaView::snapshot() {
 #ifdef APP_EXTRA
     Extra::fadeInWidget(statusBar, statusBar);
 #endif
-    statusBar->clearMessage();
     statusBar->insertPermanentWidget(0, snapshotSettings);
     snapshotSettings->show();
+    MainWindow::instance()->setStatusBarVisibility(true);
 }
 #endif
 
@@ -898,7 +915,27 @@ void MediaView::startDownloading() {
     downloadItem->start();
 }
 
+void MediaView::resumeWithNewStreamUrl(const QUrl &streamUrl) {
+    pauseTime = mediaObject->currentTime();
+    mediaObject->setCurrentSource(streamUrl);
+    mediaObject->play();
+
+    Video *video = static_cast<Video *>(sender());
+    if (!video) {
+        qDebug() << "Cannot get sender in" << __PRETTY_FUNCTION__;
+        return;
+    }
+    video->disconnect(this);
+}
+
+void MediaView::maybeAdjustWindowSize() {
+    QSettings settings;
+    if (settings.value("adjustWindowSize", true).toBool())
+        adjustWindowSize();
+}
+
 void MediaView::sliderMoved(int value) {
+    Q_UNUSED(value);
 #ifdef APP_PHONON
 #ifndef APP_PHONON_SEEK
 
@@ -1011,18 +1048,12 @@ void MediaView::shareViaTwitter() {
     Video* video = playlistModel->activeVideo();
     if (!video) return;
     QUrl url("https://twitter.com/intent/tweet");
-#if QT_VERSION >= 0x050000
     {
-        QUrl &u = url;
-        QUrlQuery url;
-#endif
-        url.addQueryItem("via", "minitubeapp");
-        url.addQueryItem("text", video->title());
-        url.addQueryItem("url", video->webpage());
-#if QT_VERSION >= 0x050000
-        u.setQuery(url);
+        QUrlQueryHelper urlHelper(url);
+        urlHelper.addQueryItem("via", "minitubeapp");
+        urlHelper.addQueryItem("text", video->title());
+        urlHelper.addQueryItem("url", video->webpage());
     }
-#endif
     QDesktopServices::openUrl(url);
 }
 
@@ -1030,17 +1061,11 @@ void MediaView::shareViaFacebook() {
     Video* video = playlistModel->activeVideo();
     if (!video) return;
     QUrl url("https://www.facebook.com/sharer.php");
-#if QT_VERSION >= 0x050000
     {
-        QUrl &u = url;
-        QUrlQuery url;
-#endif
-        url.addQueryItem("t", video->title());
-        url.addQueryItem("u", video->webpage());
-#if QT_VERSION >= 0x050000
-        u.setQuery(url);
+        QUrlQueryHelper urlHelper(url);
+        urlHelper.addQueryItem("t", video->title());
+        urlHelper.addQueryItem("u", video->webpage());
     }
-#endif
     QDesktopServices::openUrl(url);
 }
 
@@ -1048,19 +1073,13 @@ void MediaView::shareViaBuffer() {
     Video* video = playlistModel->activeVideo();
     if (!video) return;
     QUrl url("http://bufferapp.com/add");
-#if QT_VERSION >= 0x050000
     {
-        QUrl &u = url;
-        QUrlQuery url;
-#endif
-        url.addQueryItem("via", "minitubeapp");
-        url.addQueryItem("text", video->title());
-        url.addQueryItem("url", video->webpage());
-        url.addQueryItem("picture", video->thumbnailUrl());
-#if QT_VERSION >= 0x050000
-        u.setQuery(url);
+        QUrlQueryHelper urlHelper(url);
+        urlHelper.addQueryItem("via", "minitubeapp");
+        urlHelper.addQueryItem("text", video->title());
+        urlHelper.addQueryItem("url", video->webpage());
+        urlHelper.addQueryItem("picture", video->thumbnailUrl());
     }
-#endif
     QDesktopServices::openUrl(url);
 }
 
@@ -1068,21 +1087,15 @@ void MediaView::shareViaEmail() {
     Video* video = playlistModel->activeVideo();
     if (!video) return;
     QUrl url("mailto:");
-#if QT_VERSION >= 0x050000
     {
-        QUrl &u = url;
-        QUrlQuery url;
-#endif
-        url.addQueryItem("subject", video->title());
-        QString body = video->title() + "\n" +
+        QUrlQueryHelper urlHelper(url);
+        urlHelper.addQueryItem("subject", video->title());
+        const QString body = video->title() + "\n" +
                 video->webpage() + "\n\n" +
                 tr("Sent from %1").arg(Constants::NAME) + "\n" +
                 Constants::WEBSITE;
-        url.addQueryItem("body", body);
-#if QT_VERSION >= 0x050000
-        u.setQuery(url);
+        urlHelper.addQueryItem("body", body);
     }
-#endif
     QDesktopServices::openUrl(url);
 }
 
@@ -1123,7 +1136,7 @@ void MediaView::updateSubscriptionAction(Video *video, bool subscribed) {
     subscribeAction->setStatusTip(subscribeTip);
 
     if (subscribed) {
-#ifdef Q_OS_LINUX
+#ifdef APP_LINUX
         static QIcon tintedIcon;
         if (tintedIcon.isNull()) {
             QList<QSize> sizes;
@@ -1151,3 +1164,23 @@ void MediaView::toggleSubscription() {
     else YTChannel::subscribe(userId);
     updateSubscriptionAction(video, !subscribed);
 }
+
+void MediaView::adjustWindowSize() {
+    if (!MainWindow::instance()->isMaximized() && !MainWindow::instance()->isFullScreen()) {
+        const double ratio = 16. / 9.;
+        const int w = videoAreaWidget->width();
+        const int h = videoAreaWidget->height();
+        const double currentVideoRatio = (double)w / (double)h;
+        if (currentVideoRatio != ratio) {
+            if (false && currentVideoRatio > ratio) {
+                // we have vertical black bars
+                int newWidth = (MainWindow::instance()->width() - w) + (h * ratio);
+                MainWindow::instance()->resize(newWidth, MainWindow::instance()->height());
+            } else {
+                // horizontal black bars
+                int newHeight = (MainWindow::instance()->height() - h) + (w / ratio);
+                MainWindow::instance()->resize(MainWindow::instance()->width(), newHeight);
+            }
+        }
+    }
+}