]> git.sur5r.net Git - minitube/blobdiff - src/mediaview.cpp
Fix updating QUrl query parameters with Qt5.
[minitube] / src / mediaview.cpp
index 5287af0051f70800e59537d33cc6afbeed3e2f98..a0a36dbbf35c23aa3e927372ff103bea1e08e33e 100644 (file)
@@ -44,8 +44,13 @@ $END_LICENSE */
 #include "searchparams.h"
 #include "ytsinglevideosource.h"
 #include "channelaggregator.h"
-#include "utils.h"
-#include "ytuser.h"
+#include "iconutils.h"
+#include "ytchannel.h"
+#ifdef APP_SNAPSHOT
+#include "snapshotsettings.h"
+#endif
+#include "datautils.h"
+#include "compatibility/qurlqueryhelper.h"
 
 namespace The {
 NetworkAccess* http();
@@ -61,6 +66,9 @@ MediaView* MediaView::instance() {
 
 MediaView::MediaView(QWidget *parent) : QWidget(parent),
     stopped(false),
+    #ifdef APP_SNAPSHOT
+    snapshotSettings(0),
+    #endif
     downloadItem(0) { }
 
 void MediaView::initialize() {
@@ -95,7 +103,7 @@ void MediaView::initialize() {
     connect(playlistModel, SIGNAL(haveSuggestions(const QStringList &)),
             sidebar, SLOT(showSuggestions(const QStringList &)));
     connect(sidebar, SIGNAL(suggestionAccepted(QString)),
-            MainWindow::instance(), SLOT(startToolbarSearch(QString)));
+            MainWindow::instance(), SLOT(search(QString)));
     splitter->addWidget(sidebar);
 
     videoAreaWidget = new VideoAreaWidget(this);
@@ -145,6 +153,9 @@ void MediaView::initialize() {
             << The::globalActions()->value("pagelink")
             << The::globalActions()->value("videolink")
             << The::globalActions()->value("open-in-browser")
+           #ifdef APP_SNAPSHOT
+            << The::globalActions()->value("snapshot")
+           #endif
             << The::globalActions()->value("findVideoParts")
             << The::globalActions()->value("skip")
             << The::globalActions()->value("previous")
@@ -156,8 +167,10 @@ void MediaView::initialize() {
             << The::globalActions()->value("buffer")
             << The::globalActions()->value("email");
 
+#ifndef APP_PHONON_SEEK
     QSlider *slider = MainWindow::instance()->getSlider();
     connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
+#endif
 }
 
 #ifdef APP_PHONON
@@ -193,10 +206,13 @@ 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) {
+void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool back) {
     stopped = false;
 
 #ifdef APP_ACTIVATION
@@ -220,6 +236,11 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory) {
         history.append(videoSource);
     }
 
+#ifdef APP_EXTRA
+    if (history.size() > 1)
+        Extra::slideTransition(playlistView->viewport(), playlistView->viewport(), back);
+#endif
+
     playlistModel->setVideoSource(videoSource);
 
     sidebar->showPlaylist();
@@ -228,8 +249,10 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory) {
     sidebar->getHeader()->updateInfo();
 
     SearchParams *searchParams = getSearchParams();
-    bool isChannel = searchParams && !searchParams->author().isEmpty();
+    bool isChannel = searchParams && !searchParams->channelId().isEmpty();
     playlistView->setClickableAuthors(!isChannel);
+
+
 }
 
 void MediaView::searchAgain() {
@@ -246,7 +269,7 @@ void MediaView::goBack() {
         int currentIndex = getHistoryIndex();
         if (currentIndex > 0) {
             VideoSource *previousVideoSource = history.at(currentIndex - 1);
-            setVideoSource(previousVideoSource, false);
+            setVideoSource(previousVideoSource, false, true);
         }
     }
 }
@@ -282,8 +305,13 @@ void MediaView::disappear() {
 
 }
 
-void MediaView::handleError(QString /* message */) {
+void MediaView::handleError(QString message) {
+    qWarning() << __PRETTY_FUNCTION__ << message;
+#ifdef APP_PHONON_SEEK
+    mediaObject->play();
+#else
     QTimer::singleShot(500, this, SLOT(startPlaying()));
+#endif
 }
 
 #ifdef APP_PHONON
@@ -291,7 +319,7 @@ void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
     if (newState == Phonon::PlayingState)
         videoAreaWidget->showVideo();
     else if (newState == Phonon::ErrorState) {
-        qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
+        qWarning() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
         if (mediaObject->errorType() == Phonon::FatalError)
             handleError(mediaObject->errorString());
     }
@@ -352,9 +380,16 @@ void MediaView::stop() {
 #endif
     currentVideoId.clear();
 
+#ifndef APP_PHONON_SEEK
     QSlider *slider = MainWindow::instance()->getSlider();
     slider->setEnabled(false);
     slider->setValue(0);
+#endif
+
+    if (snapshotSettings) {
+        delete snapshotSettings;
+        snapshotSettings = 0;
+    }
 }
 
 const QString & MediaView::getCurrentVideoId() {
@@ -384,7 +419,7 @@ void MediaView::activeRowChanged(int row) {
     connect(video, SIGNAL(gotStreamUrl(QUrl)),
             SLOT(gotStreamUrl(QUrl)), Qt::UniqueConnection);
     connect(video, SIGNAL(errorStreamUrl(QString)),
-            SLOT(handleError(QString)), Qt::UniqueConnection);
+            SLOT(skip()), Qt::UniqueConnection);
     video->loadStreamUrl();
 
     // video title in titlebar
@@ -404,7 +439,6 @@ void MediaView::activeRowChanged(int row) {
     The::globalActions()->value("stopafterthis")->setEnabled(true);
     The::globalActions()->value("related-videos")->setEnabled(true);
 
-#ifndef APP_NO_DOWNLOADS
     bool enableDownload = video->license() == Video::LicenseCC;
 #ifdef APP_ACTIVATION
     enableDownload = enableDownload || Activation::instance().isLegacy();
@@ -415,16 +449,22 @@ void MediaView::activeRowChanged(int row) {
     QAction *a = The::globalActions()->value("download");
     a->setEnabled(enableDownload);
     a->setVisible(enableDownload);
-#endif
 
-    updateSubscriptionAction(video, YTUser::isSubscribed(video->userId()));
+    updateSubscriptionAction(video, YTChannel::isSubscribed(video->channelId()));
 
     foreach (QAction *action, currentVideoActions)
         action->setEnabled(true);
 
+#ifndef APP_PHONON_SEEK
     QSlider *slider = MainWindow::instance()->getSlider();
     slider->setEnabled(false);
     slider->setValue(0);
+#endif
+
+    if (snapshotSettings) {
+        delete snapshotSettings;
+        snapshotSettings = 0;
+    }
 
     // see you in gotStreamUrl...
 }
@@ -445,14 +485,14 @@ void MediaView::gotStreamUrl(QUrl streamUrl) {
 
     currentVideoId = video->id();
 
-#ifdef Q_OS_LINUX_NO
+#ifdef APP_PHONON_SEEK
     mediaObject->setCurrentSource(streamUrl);
     mediaObject->play();
 #else
     startDownloading();
 #endif
 
-    // ensure we always have 10 videos ahead
+    // ensure we always have videos ahead
     playlistModel->searchNeeded();
 
     // ensure active item is visible
@@ -468,7 +508,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);
@@ -493,8 +533,8 @@ void MediaView::downloadStatusChanged() {
         break;
     case Finished:
         // qDebug() << "Finished" << mediaObject->state();
-#ifdef Q_OS_LINUX
-        // MainWindow::instance()->getSeekSlider()->setEnabled(mediaObject->isSeekable());
+#ifdef APP_PHONON_SEEK
+        MainWindow::instance()->getSeekSlider()->setEnabled(mediaObject->isSeekable());
 #endif
         break;
     case Failed:
@@ -524,15 +564,15 @@ void MediaView::startPlaying() {
     QString source = downloadItem->currentFilename();
     qDebug() << "Playing" << source << QFile::exists(source);
 #ifdef APP_PHONON
-    mediaObject->setCurrentSource(source);
+    mediaObject->setCurrentSource(QUrl::fromLocalFile(source));
     mediaObject->play();
 #endif
-#ifdef Q_OS_LINUX
-    // MainWindow::instance()->getSeekSlider()->setEnabled(false);
-#endif
-
+#ifdef APP_PHONON_SEEK
+    MainWindow::instance()->getSeekSlider()->setEnabled(false);
+#else
     QSlider *slider = MainWindow::instance()->getSlider();
     slider->setEnabled(true);
+#endif
 }
 
 void MediaView::itemActivated(const QModelIndex &index) {
@@ -640,7 +680,7 @@ void MediaView::openWebPage() {
 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);
@@ -791,16 +831,46 @@ void MediaView::downloadVideo() {
     MainWindow::instance()->showMessage(message);
 }
 
-/*
+#ifdef APP_SNAPSHOT
 void MediaView::snapshot() {
+    qint64 currentTime = mediaObject->currentTime() / 1000;
+
     QImage image = videoWidget->snapshot();
-    qDebug() << image.size();
+    if (image.isNull()) {
+        qWarning() << "Null snapshot";
+        return;
+    }
 
-    const QPixmap& pixmap = QPixmap::grabWindow(videoWidget->winId());
-    // qDebug() << pixmap.size();
+    // QPixmap pixmap = QPixmap::grabWindow(videoWidget->winId());
+    QPixmap pixmap = QPixmap::fromImage(image.scaled(videoWidget->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
     videoAreaWidget->showSnapshotPreview(pixmap);
+
+    Video* video = playlistModel->activeVideo();
+    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) + ")";
+    basename = DataUtils::stringToFilename(basename);
+    QString filename = location + "/" + basename + ".png";
+    qDebug() << filename;
+    image.save(filename, "PNG");
+
+    if (snapshotSettings) delete snapshotSettings;
+    snapshotSettings = new SnapshotSettings(videoWidget);
+    snapshotSettings->setSnapshot(pixmap, filename);
+    QStatusBar *statusBar = MainWindow::instance()->statusBar();
+#ifdef APP_EXTRA
+    Extra::fadeInWidget(statusBar, statusBar);
+#endif
+    statusBar->clearMessage();
+    statusBar->insertPermanentWidget(0, snapshotSettings);
+    snapshotSettings->show();
 }
-*/
+#endif
 
 void MediaView::fullscreen() {
     videoAreaWidget->setParent(0);
@@ -831,6 +901,8 @@ void MediaView::startDownloading() {
 
 void MediaView::sliderMoved(int value) {
 #ifdef APP_PHONON
+#ifndef APP_PHONON_SEEK
+
     if (currentVideoSize <= 0 || !downloadItem || !mediaObject->isSeekable())
         return;
 
@@ -855,6 +927,7 @@ void MediaView::sliderMoved(int value) {
         mediaObject->seek(offsetToTime(offset));
     }
 #endif
+#endif
 }
 
 qint64 MediaView::offsetToTime(qint64 offset) {
@@ -910,7 +983,7 @@ void MediaView::findVideoParts() {
     SearchParams *searchParams = new SearchParams();
     searchParams->setTransient(true);
     searchParams->setKeywords(query);
-    searchParams->setAuthor(video->author());
+    searchParams->setChannelId(video->channelId());
 
     /*
     if (!numberAsWords) {
@@ -929,7 +1002,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);
 }
@@ -938,18 +1012,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);
 }
 
@@ -957,17 +1025,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);
 }
 
@@ -975,19 +1037,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);
 }
 
@@ -995,21 +1051,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);
 }
 
@@ -1017,12 +1067,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!
@@ -1038,11 +1088,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);
     }
@@ -1055,26 +1105,26 @@ void MediaView::updateSubscriptionAction(Video *video, bool subscribed) {
         if (tintedIcon.isNull()) {
             QList<QSize> sizes;
             sizes << QSize(16, 16);
-            tintedIcon = Utils::tintedIcon("bookmark-new", QColor(254, 240, 0), sizes);
+            tintedIcon = IconUtils::tintedIcon("bookmark-new", QColor(254, 240, 0), sizes);
         }
         subscribeAction->setIcon(tintedIcon);
 #else
-        subscribeAction->setIcon(Utils::icon("bookmark-remove"));
+        subscribeAction->setIcon(IconUtils::icon("bookmark-remove"));
 #endif
     } else {
-        subscribeAction->setIcon(Utils::icon("bookmark-new"));
+        subscribeAction->setIcon(IconUtils::icon("bookmark-new"));
     }
 
-    Utils::setupAction(subscribeAction);
+    IconUtils::setupAction(subscribeAction);
 }
 
 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);
+    else YTChannel::subscribe(userId);
     updateSubscriptionAction(video, !subscribed);
 }