]> git.sur5r.net Git - minitube/blobdiff - src/mediaview.cpp
Merge tag 'upstream/2.5.1'
[minitube] / src / mediaview.cpp
index 1f703181f425f850e451912a19487c3a1c5dda53..302c77a1ea478d6ca0c91329c42eb3fba6b48f86 100644 (file)
@@ -45,11 +45,12 @@ $END_LICENSE */
 #include "ytsinglevideosource.h"
 #include "channelaggregator.h"
 #include "iconutils.h"
-#include "ytuser.h"
+#include "ytchannel.h"
 #ifdef APP_SNAPSHOT
 #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) : View(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);
 
@@ -186,7 +190,7 @@ void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
 SearchParams* MediaView::getSearchParams() {
     VideoSource *videoSource = playlistModel->getVideoSource();
     if (videoSource && videoSource->metaObject()->className() == QLatin1String("YTSearch")) {
-        YTSearch *search = dynamic_cast<YTSearch *>(videoSource);
+        YTSearch *search = qobject_cast<YTSearch *>(videoSource);
         return search->getSearchParams();
     }
     return 0;
@@ -205,10 +209,14 @@ void MediaView::search(SearchParams *searchParams) {
             }
         }
     }
-    setVideoSource(new YTSearch(searchParams, this));
+    YTSearch *ytSearch = new YTSearch(searchParams, this);
+    ytSearch->setAsyncDetails(true);
+    connect(ytSearch, SIGNAL(gotDetails()), playlistModel, SLOT(emitDataChanged()));
+    setVideoSource(ytSearch);
 }
 
 void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool back) {
+    Q_UNUSED(back);
     stopped = false;
 
 #ifdef APP_ACTIVATION
@@ -245,7 +253,7 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool
     sidebar->getHeader()->updateInfo();
 
     SearchParams *searchParams = getSearchParams();
-    bool isChannel = searchParams && !searchParams->author().isEmpty();
+    bool isChannel = searchParams && !searchParams->channelId().isEmpty();
     playlistView->setClickableAuthors(!isChannel);
 
 
@@ -288,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();
@@ -312,10 +323,14 @@ 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) {
-        qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
+    else if (newState == Phonon::ErrorState) {
+        qWarning() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
         if (mediaObject->errorType() == Phonon::FatalError)
             handleError(mediaObject->errorString());
     }
@@ -327,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);
 }
 
@@ -380,12 +400,16 @@ void MediaView::stop() {
     QSlider *slider = MainWindow::instance()->getSlider();
     slider->setEnabled(false);
     slider->setValue(0);
+#else
+    Phonon::SeekSlider *slider = MainWindow::instance()->getSeekSlider();
 #endif
 
+#ifdef APP_SNAPSHOT
     if (snapshotSettings) {
         delete snapshotSettings;
         snapshotSettings = 0;
     }
+#endif
 }
 
 const QString & MediaView::getCurrentVideoId() {
@@ -420,7 +444,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) {
@@ -446,7 +469,7 @@ void MediaView::activeRowChanged(int row) {
     a->setEnabled(enableDownload);
     a->setVisible(enableDownload);
 
-    updateSubscriptionAction(video, YTUser::isSubscribed(video->userId()));
+    updateSubscriptionAction(video, YTChannel::isSubscribed(video->channelId()));
 
     foreach (QAction *action, currentVideoActions)
         action->setEnabled(true);
@@ -457,10 +480,13 @@ void MediaView::activeRowChanged(int row) {
     slider->setValue(0);
 #endif
 
+#ifdef APP_SNAPSHOT
     if (snapshotSettings) {
         delete snapshotSettings;
         snapshotSettings = 0;
+        MainWindow::instance()->adjustStatusBarVisibility();
     }
+#endif
 
     // see you in gotStreamUrl...
 }
@@ -488,7 +514,7 @@ void MediaView::gotStreamUrl(QUrl streamUrl) {
     startDownloading();
 #endif
 
-    // ensure we always have 10 videos ahead
+    // ensure we always have videos ahead
     playlistModel->searchNeeded();
 
     // ensure active item is visible
@@ -504,7 +530,7 @@ void MediaView::gotStreamUrl(QUrl streamUrl) {
 #endif
 
 #ifdef APP_EXTRA
-    Extra::notify(video->title(), video->author(), video->formattedDuration());
+    Extra::notify(video->title(), video->channelTitle(), video->formattedDuration());
 #endif
 
     ChannelAggregator::instance()->videoWatched(video);
@@ -624,7 +650,7 @@ void MediaView::aboutToFinish() {
 #ifdef APP_PHONON
     qint64 currentTime = mediaObject->currentTime();
     qint64 totalTime = mediaObject->totalTime();
-    qDebug() << __PRETTY_FUNCTION__ << currentTime << totalTime;
+    // qDebug() << __PRETTY_FUNCTION__ << currentTime << totalTime;
     if (totalTime < 1 || currentTime + 10000 < totalTime) {
         // QTimer::singleShot(500, this, SLOT(playbackResume()));
         mediaObject->seek(currentTime);
@@ -639,9 +665,9 @@ void MediaView::playbackFinished() {
 #ifdef APP_PHONON
     const qint64 totalTime = mediaObject->totalTime();
     const qint64 currentTime = mediaObject->currentTime();
-    qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime() << totalTime;
+    // 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 {
@@ -657,7 +683,7 @@ void MediaView::playbackResume() {
     if (stopped) return;
 #ifdef APP_PHONON
     const qint64 currentTime = mediaObject->currentTime();
-    qDebug() << __PRETTY_FUNCTION__ << currentTime;
+    // qDebug() << __PRETTY_FUNCTION__ << currentTime;
     if (currentTime > 0)
         mediaObject->seek(currentTime);
     mediaObject->play();
@@ -670,13 +696,14 @@ void MediaView::openWebPage() {
 #ifdef APP_PHONON
     mediaObject->pause();
 #endif
-    QDesktopServices::openUrl(video->webpage());
+    QString url = video->webpage() + QLatin1String("&t=") + QString::number(mediaObject->currentTime() / 1000);
+    QDesktopServices::openUrl(url);
 }
 
 void MediaView::copyWebPage() {
     Video* video = playlistModel->activeVideo();
     if (!video) return;
-    QString address = video->webpage().toString();
+    QString address = video->webpage();
     QApplication::clipboard()->setText(address);
     QString message = tr("You can now paste the YouTube link into another application");
     MainWindow::instance()->showMessage(message);
@@ -775,7 +802,7 @@ void MediaView::demoMessage() {
 #endif
 
     QMessageBox msgBox(this);
-    msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
+    msgBox.setIconPixmap(IconUtils::pixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
     msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::NAME));
     msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
     msgBox.setModal(true);
@@ -822,7 +849,7 @@ void MediaView::downloadVideo() {
     Video* video = playlistModel->activeVideo();
     if (!video) return;
     DownloadManager::instance()->addItem(video);
-    The::globalActions()->value("downloads")->setVisible(true);
+    MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("downloads"), true);
     QString message = tr("Downloading %1").arg(video->title());
     MainWindow::instance()->showMessage(message);
 }
@@ -845,6 +872,8 @@ void MediaView::snapshot() {
     if (!video) return;
 
     QString location = SnapshotSettings::getCurrentLocation();
+    QDir dir(location);
+    if (!dir.exists()) dir.mkpath(location);
     QString basename = video->title();
     QString format = video->duration() > 3600 ? "h_mm_ss" : "m_ss";
     basename += " (" + QTime().addSecs(currentTime).toString(format) + ")";
@@ -860,9 +889,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
 
@@ -893,7 +922,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
 
@@ -977,7 +1026,7 @@ void MediaView::findVideoParts() {
     SearchParams *searchParams = new SearchParams();
     searchParams->setTransient(true);
     searchParams->setKeywords(query);
-    searchParams->setAuthor(video->author());
+    searchParams->setChannelId(video->channelId());
 
     /*
     if (!numberAsWords) {
@@ -996,7 +1045,8 @@ void MediaView::relatedVideos() {
     Video* video = playlistModel->activeVideo();
     if (!video) return;
     YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource();
-    singleVideoSource->setVideoId(video->id());
+    singleVideoSource->setVideo(video->clone());
+    singleVideoSource->setAsyncDetails(true);
     setVideoSource(singleVideoSource);
     The::globalActions()->value("related-videos")->setEnabled(false);
 }
@@ -1005,18 +1055,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().toString());
-#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);
 }
 
@@ -1024,17 +1068,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().toString());
-#if QT_VERSION >= 0x050000
-        u.setQuery(url);
+        QUrlQueryHelper urlHelper(url);
+        urlHelper.addQueryItem("t", video->title());
+        urlHelper.addQueryItem("u", video->webpage());
     }
-#endif
     QDesktopServices::openUrl(url);
 }
 
@@ -1042,19 +1080,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().toString());
-        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);
 }
 
@@ -1062,21 +1094,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" +
-                video->webpage().toString() + "\n\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);
 }
 
@@ -1084,12 +1110,12 @@ void MediaView::authorPushed(QModelIndex index) {
     Video* video = playlistModel->videoAt(index.row());
     if (!video) return;
 
-    QString channel = video->userId();
-    if (channel.isEmpty()) channel = video->author();
-    if (channel.isEmpty()) return;
+    QString channelId = video->channelId();
+    // if (channelId.isEmpty()) channelId = video->channelTitle();
+    if (channelId.isEmpty()) return;
 
     SearchParams *searchParams = new SearchParams();
-    searchParams->setAuthor(channel);
+    searchParams->setChannelId(channelId);
     searchParams->setSortBy(SearchParams::SortByNewest);
 
     // go!
@@ -1105,11 +1131,11 @@ void MediaView::updateSubscriptionAction(Video *video, bool subscribed) {
         subscribeText = subscribeAction->property("originalText").toString();
         subscribeAction->setEnabled(false);
     } else if (subscribed) {
-        subscribeText = tr("Unsubscribe from %1").arg(video->author());
+        subscribeText = tr("Unsubscribe from %1").arg(video->channelTitle());
         subscribeTip = subscribeText;
         subscribeAction->setEnabled(true);
     } else {
-        subscribeText = tr("Subscribe to %1").arg(video->author());
+        subscribeText = tr("Subscribe to %1").arg(video->channelTitle());
         subscribeTip = subscribeText;
         subscribeAction->setEnabled(true);
     }
@@ -1117,7 +1143,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;
@@ -1138,10 +1164,35 @@ void MediaView::updateSubscriptionAction(Video *video, bool subscribed) {
 void MediaView::toggleSubscription() {
     Video *video = playlistModel->activeVideo();
     if (!video) return;
-    QString userId = video->userId();
+    QString userId = video->channelId();
     if (userId.isEmpty()) return;
-    bool subscribed = YTUser::isSubscribed(userId);
-    if (subscribed) YTUser::unsubscribe(userId);
-    else YTUser::subscribe(userId);
+    bool subscribed = YTChannel::isSubscribed(userId);
+    if (subscribed) {
+        YTChannel::unsubscribe(userId);
+        MainWindow::instance()->showMessage(tr("Unsubscribed from %1").arg(video->channelTitle()));
+    } else {
+        YTChannel::subscribe(userId);
+        MainWindow::instance()->showMessage(tr("Subscribed to %1").arg(video->channelTitle()));
+    }
     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);
+            }
+        }
+    }
+}