]> git.sur5r.net Git - minitube/blobdiff - src/mediaview.cpp
New upstream version 3.1
[minitube] / src / mediaview.cpp
index c1617f1e060c991ca029c2946f71fde9aae7523c..e6343ab9b2c498507ff3973611047a040f157c1b 100644 (file)
@@ -19,99 +19,92 @@ along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
 $END_LICENSE */
 
 #include "mediaview.h"
-#include "playlistmodel.h"
-#include "playlistview.h"
-#include "loadingwidget.h"
-#include "videoareawidget.h"
-#include "networkaccess.h"
-#include "minisplitter.h"
 #include "constants.h"
 #include "downloadmanager.h"
-#include "downloaditem.h"
+#include "http.h"
+#include "loadingwidget.h"
 #include "mainwindow.h"
-#include "temporary.h"
+#include "minisplitter.h"
+#include "playlistmodel.h"
+#include "playlistview.h"
 #include "refinesearchwidget.h"
-#include "sidebarwidget.h"
 #include "sidebarheader.h"
+#include "sidebarwidget.h"
+#include "temporary.h"
+#include "videoarea.h"
 #ifdef APP_ACTIVATION
 #include "activation.h"
 #endif
 #ifdef APP_EXTRA
 #include "extra.h"
 #endif
-#include "videosource.h"
-#include "ytsearch.h"
-#include "searchparams.h"
-#include "ytsinglevideosource.h"
 #include "channelaggregator.h"
 #include "iconutils.h"
+#include "searchparams.h"
+#include "videosource.h"
 #include "ytchannel.h"
+#include "ytsearch.h"
+#include "ytsinglevideosource.h"
 #ifdef APP_SNAPSHOT
 #include "snapshotsettings.h"
 #endif
 #include "datautils.h"
+#include "idle.h"
+#include "videodefinition.h"
 
-namespace The {
-NetworkAccess* http();
-QHash<QString, QAction*>* globalActions();
-QHash<QString, QMenu*>* globalMenus();
-QNetworkAccessManager* networkAccessManager();
-}
-
-MediaView* MediaView::instance() {
+MediaView *MediaView::instance() {
     static MediaView *i = new MediaView();
     return i;
 }
 
-MediaView::MediaView(QWidget *parent) : QWidget(parent),
-    stopped(false),
-    #ifdef APP_SNAPSHOT
-    snapshotSettings(0),
-    #endif
-    downloadItem(0) { }
+MediaView::MediaView(QWidget *parent)
+    : View(parent), splitter(nullptr), stopped(false)
+#ifdef APP_SNAPSHOT
+      ,
+      snapshotSettings(nullptr)
+#endif
+      ,
+      pauseTime(0) {
+}
 
 void MediaView::initialize() {
+    MainWindow *mainWindow = MainWindow::instance();
+
     QBoxLayout *layout = new QVBoxLayout(this);
     layout->setMargin(0);
 
     splitter = new MiniSplitter();
+    layout->addWidget(splitter);
 
-    playlistView = new PlaylistView(this);
-    // respond to the user doubleclicking a playlist item
+    playlistView = new PlaylistView();
+    playlistView->setParent(this);
     connect(playlistView, SIGNAL(activated(const QModelIndex &)),
-            SLOT(itemActivated(const QModelIndex &)));
+            SLOT(onItemActivated(const QModelIndex &)));
 
     playlistModel = new PlaylistModel();
-    connect(playlistModel, SIGNAL(activeRowChanged(int)),
-            SLOT(activeRowChanged(int)));
+    connect(playlistModel, &PlaylistModel::activeVideoChanged, this,
+            &MediaView::activeVideoChanged);
     // needed to restore the selection after dragndrop
-    connect(playlistModel, SIGNAL(needSelectionFor(QList<Video*>)),
-            SLOT(selectVideos(QList<Video*>)));
+    connect(playlistModel, SIGNAL(needSelectionFor(QVector<Video *>)),
+            SLOT(selectVideos(QVector<Video *>)));
     playlistView->setModel(playlistModel);
 
     connect(playlistView->selectionModel(),
             SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
             SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
 
-    connect(playlistView, SIGNAL(authorPushed(QModelIndex)), SLOT(authorPushed(QModelIndex)));
+    connect(playlistView, SIGNAL(authorPushed(QModelIndex)), SLOT(onAuthorPushed(QModelIndex)));
 
     sidebar = new SidebarWidget(this);
     sidebar->setPlaylist(playlistView);
-    connect(sidebar->getRefineSearchWidget(), SIGNAL(searchRefined()),
-            SLOT(searchAgain()));
-    connect(playlistModel, SIGNAL(haveSuggestions(const QStringList &)),
-            sidebar, SLOT(showSuggestions(const QStringList &)));
-    connect(sidebar, SIGNAL(suggestionAccepted(QString)),
-            MainWindow::instance(), SLOT(search(QString)));
+    sidebar->setMaximumWidth(playlistView->minimumWidth() * 3);
+    connect(sidebar->getRefineSearchWidget(), SIGNAL(searchRefined()), SLOT(searchAgain()));
+    connect(playlistModel, SIGNAL(haveSuggestions(const QStringList &)), sidebar,
+            SLOT(showSuggestions(const QStringList &)));
+    connect(sidebar, SIGNAL(suggestionAccepted(QString)), mainWindow, SLOT(search(QString)));
     splitter->addWidget(sidebar);
 
-    videoAreaWidget = new VideoAreaWidget(this);
-    // videoAreaWidget->setMinimumSize(320,240);
-
-#ifdef APP_PHONON
-    videoWidget = new Phonon::VideoWidget(this);
-    videoAreaWidget->setVideoWidget(videoWidget);
-#endif
+    videoAreaWidget = new VideoArea(this);
     videoAreaWidget->setListModel(playlistModel);
 
     loadingWidget = new LoadingWidget(this);
@@ -119,15 +112,17 @@ void MediaView::initialize() {
 
     splitter->addWidget(videoAreaWidget);
 
-    splitter->setStretchFactor(0, 0);
-    splitter->setStretchFactor(1, 8);
-
     // restore splitter state
     QSettings settings;
-    splitter->restoreState(settings.value("splitter").toByteArray());
+    if (settings.contains("splitter"))
+        splitter->restoreState(settings.value("splitter").toByteArray());
+    else {
+        int sidebarDefaultWidth = 180;
+        splitter->setSizes(QList<int>() << sidebarDefaultWidth
+                                        << splitter->size().width() - sidebarDefaultWidth);
+    }
     splitter->setChildrenCollapsible(false);
-
-    layout->addWidget(splitter);
+    connect(splitter, SIGNAL(splitterMoved(int, int)), SLOT(adjustWindowSize()));
 
     errorTimer = new QTimer(this);
     errorTimer->setSingleShot(true);
@@ -137,90 +132,85 @@ void MediaView::initialize() {
 #ifdef APP_ACTIVATION
     demoTimer = new QTimer(this);
     demoTimer->setSingleShot(true);
-    connect(demoTimer, SIGNAL(timeout()), SLOT(demoMessage()));
+    connect(demoTimer, &QTimer::timeout, mainWindow, &MainWindow::showActivationView,
+            Qt::QueuedConnection);
 #endif
 
-    connect(videoAreaWidget, SIGNAL(doubleClicked()),
-            The::globalActions()->value("fullscreen"), SLOT(trigger()));
-
-    QAction* refineSearchAction = The::globalActions()->value("refine-search");
-    connect(refineSearchAction, SIGNAL(toggled(bool)),
-            sidebar, SLOT(toggleRefineSearch(bool)));
-
-    currentVideoActions
-            << The::globalActions()->value("webpage")
-            << 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")
-            << The::globalActions()->value("stopafterthis")
-            << The::globalActions()->value("related-videos")
-            << The::globalActions()->value("refine-search")
-            << The::globalActions()->value("twitter")
-            << The::globalActions()->value("facebook")
-            << 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)));
+    connect(videoAreaWidget, SIGNAL(doubleClicked()), mainWindow->getAction("fullscreen"),
+            SLOT(trigger()));
+
+    QAction *refineSearchAction = mainWindow->getAction("refineSearch");
+    connect(refineSearchAction, SIGNAL(toggled(bool)), sidebar, SLOT(toggleRefineSearch(bool)));
+
+    const QVector<const char *> videoActionNames = {
+#ifdef APP_SNAPSHOT
+            "snapshot",
 #endif
+            "webpage",  "pagelink", "videolink",     "openInBrowser", "findVideoParts",
+            "skip",     "previous", "stopafterthis", "relatedVideos", "refineSearch",
+            "twitter",  "facebook", "email"};
+    currentVideoActions.reserve(videoActionNames.size());
+    for (auto *name : videoActionNames) {
+        currentVideoActions.append(mainWindow->getAction(name));
+    }
 }
 
-#ifdef APP_PHONON
-void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
-    this->mediaObject = mediaObject;
-    Phonon::createPath(mediaObject, videoWidget);
-    connect(mediaObject, SIGNAL(finished()), SLOT(playbackFinished()));
-    connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
-            SLOT(stateChanged(Phonon::State, Phonon::State)));
-    connect(mediaObject, SIGNAL(aboutToFinish()), SLOT(aboutToFinish()));
+void MediaView::setMedia(Media *media) {
+    this->media = media;
+
+    videoWidget = media->videoWidget();
+    videoAreaWidget->setVideoWidget(videoWidget);
+
+    connect(media, &Media::finished, this, &MediaView::onPlaybackFinished);
+    connect(media, &Media::stateChanged, this, &MediaView::mediaStateChanged);
+    connect(media, &Media::aboutToFinish, this, &MediaView::onAboutToFinish);
+    connect(media, &Media::bufferStatus, loadingWidget, &LoadingWidget::bufferStatus);
 }
-#endif
 
-SearchParamsMediaView::getSearchParams() {
+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;
+    return nullptr;
 }
 
 void MediaView::search(SearchParams *searchParams) {
     if (!searchParams->keywords().isEmpty()) {
         if (searchParams->keywords().startsWith("http://") ||
-                searchParams->keywords().startsWith("https://")) {
+            searchParams->keywords().startsWith("https://")) {
             QString videoId = YTSearch::videoIdFromUrl(searchParams->keywords());
             if (!videoId.isEmpty()) {
                 YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource(this);
                 singleVideoSource->setVideoId(videoId);
                 setVideoSource(singleVideoSource);
+                QTime tstamp = YTSearch::videoTimestampFromUrl(searchParams->keywords());
+                pauseTime = QTime(0, 0).msecsTo(tstamp);
                 return;
             }
         }
     }
-    YTSearch *ytSearch = new YTSearch(searchParams, this);
+    YTSearch *ytSearch = new YTSearch(searchParams);
     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
-    demoTimer->stop();
-#endif
     errorTimer->stop();
 
     // 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) {
@@ -228,7 +218,7 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool
                 VideoSource *vs = history.takeLast();
                 if (!vs->parent()) {
                     qDebug() << "Deleting VideoSource" << vs->getName() << vs;
-                    delete vs;
+                    vs->deleteLater();
                 }
             }
         }
@@ -242,6 +232,13 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool
 
     playlistModel->setVideoSource(videoSource);
 
+    if (media->state() == Media::StoppedState) {
+        QSettings settings;
+        if (settings.value("manualplay", false).toBool()) {
+            videoAreaWidget->showPickMessage();
+        }
+    }
+
     sidebar->showPlaylist();
     sidebar->getRefineSearchWidget()->setSearchParams(getSearchParams());
     sidebar->hideSuggestions();
@@ -250,8 +247,6 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool
     SearchParams *searchParams = getSearchParams();
     bool isChannel = searchParams && !searchParams->channelId().isEmpty();
     playlistView->setClickableAuthors(!isChannel);
-
-
 }
 
 void MediaView::searchAgain() {
@@ -291,54 +286,67 @@ int MediaView::getHistoryIndex() {
 }
 
 void MediaView::appear() {
-    playlistView->setFocus();
+    MainWindow::instance()->showToolbar();
+
     Video *currentVideo = playlistModel->activeVideo();
     if (currentVideo) {
-        MainWindow::instance()->setWindowTitle(
-                    currentVideo->title() + " - " + Constants::NAME);
-        MainWindow::instance()->showMessage(currentVideo->description());
+        MainWindow::instance()->setWindowTitle(currentVideo->getTitle() + " - " + Constants::NAME);
     }
+
+    playlistView->setFocus();
 }
 
 void MediaView::disappear() {
-
+    MainWindow::instance()->hideToolbar();
 }
 
-void MediaView::handleError(QString message) {
+void MediaView::handleError(const QString &message) {
     qWarning() << __PRETTY_FUNCTION__ << message;
-#ifdef APP_PHONON_SEEK
-    mediaObject->play();
-#else
-    QTimer::singleShot(500, this, SLOT(startPlaying()));
+#ifndef QT_NO_DEBUG_OUTPUT
+    MainWindow::instance()->showMessage(message);
 #endif
 }
 
-#ifdef APP_PHONON
-void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
-    if (newState == Phonon::PlayingState)
+void MediaView::mediaStateChanged(Media::State state) {
+    if (pauseTime > 0 && (state == Media::PlayingState || state == Media::BufferingState)) {
+        qDebug() << "Seeking to" << pauseTime;
+        media->seek(pauseTime);
+        pauseTime = 0;
+    }
+    if (state == Media::PlayingState) {
         videoAreaWidget->showVideo();
-    else if (newState == Phonon::ErrorState) {
-        qWarning() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
-        if (mediaObject->errorType() == Phonon::FatalError)
-            handleError(mediaObject->errorString());
+    } else if (state == Media::ErrorState) {
+        handleError(media->errorString());
+    }
+
+    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();
+    } else if (state == Media::PausedState || state == Media::StoppedState) {
+        bool res = Idle::allowDisplaySleep();
+        if (!res) qWarning() << "Error enabling idle display sleep" << Idle::displayErrorMessage();
     }
 }
-#endif
 
 void MediaView::pause() {
-#ifdef APP_PHONON
-    switch( mediaObject->state() ) {
-    case Phonon::PlayingState:
-        mediaObject->pause();
+    switch (media->state()) {
+    case Media::PlayingState:
+        media->pause();
+        pauseTimer.start();
         break;
     default:
-        mediaObject->play();
+        if (pauseTimer.hasExpired(60000)) {
+            pauseTimer.invalidate();
+            connect(playlistModel->activeVideo(), &Video::gotStreamUrl, this,
+                    &MediaView::resumeWithNewStreamUrl);
+            playlistModel->activeVideo()->loadStreamUrl();
+        } else
+            media->play();
         break;
     }
-#endif
 }
 
-QRegExp MediaView::wordRE(QString s) {
+QRegExp MediaView::wordRE(const QString &s) {
     return QRegExp("\\W" + s + "\\W?", Qt::CaseInsensitive);
 }
 
@@ -347,7 +355,10 @@ void MediaView::stop() {
 
     while (!history.isEmpty()) {
         VideoSource *videoSource = history.takeFirst();
-        if (!videoSource->parent()) delete videoSource;
+        // Don't delete videoSource in the Browse view
+        if (!videoSource->parent()) {
+            videoSource->deleteLater();
+        }
     }
 
     playlistModel->abortSearch();
@@ -355,122 +366,105 @@ void MediaView::stop() {
     videoAreaWidget->update();
     errorTimer->stop();
     playlistView->selectionModel()->clearSelection();
-    if (downloadItem) {
-        downloadItem->stop();
-        delete downloadItem;
-        downloadItem = 0;
-        currentVideoSize = 0;
-    }
-    The::globalActions()->value("refine-search")->setChecked(false);
-    updateSubscriptionAction(0, false);
+
+    MainWindow::instance()->getAction("refineSearch")->setChecked(false);
+    updateSubscriptionActionForVideo(nullptr, false);
 #ifdef APP_ACTIVATION
     demoTimer->stop();
 #endif
 
-    foreach (QAction *action, currentVideoActions)
+    for (QAction *action : currentVideoActions)
         action->setEnabled(false);
 
-    QAction *a = The::globalActions()->value("download");
+    QAction *a = MainWindow::instance()->getAction("download");
     a->setEnabled(false);
     a->setVisible(false);
 
-#ifdef APP_PHONON
-    mediaObject->stop();
-#endif
+    media->stop();
+    media->clearQueue();
     currentVideoId.clear();
 
-#ifndef APP_PHONON_SEEK
-    QSlider *slider = MainWindow::instance()->getSlider();
-    slider->setEnabled(false);
-    slider->setValue(0);
-#endif
-
+#ifdef APP_SNAPSHOT
     if (snapshotSettings) {
         delete snapshotSettings;
-        snapshotSettings = 0;
+        snapshotSettings = nullptr;
     }
+#endif
 }
 
-const QString & MediaView::getCurrentVideoId() {
+const QString &MediaView::getCurrentVideoId() {
     return currentVideoId;
 }
 
-void MediaView::activeRowChanged(int row) {
+void MediaView::activeVideoChanged(Video *video, Video *previousVideo) {
     if (stopped) return;
 
+    media->stop();
     errorTimer->stop();
 
-#ifdef APP_PHONON
-    mediaObject->stop();
-#endif
-    if (downloadItem) {
-        downloadItem->stop();
-        delete downloadItem;
-        downloadItem = 0;
-        currentVideoSize = 0;
+    if (previousVideo && previousVideo != video) {
+        if (previousVideo->isLoadingStreamUrl()) previousVideo->abortLoadStreamUrl();
     }
 
-    Video *video = playlistModel->videoAt(row);
-    if (!video) return;
+    // optimize window for 16:9 video
+    adjustWindowSize();
 
     videoAreaWidget->showLoading(video);
 
-    connect(video, SIGNAL(gotStreamUrl(QUrl)),
-            SLOT(gotStreamUrl(QUrl)), Qt::UniqueConnection);
-    connect(video, SIGNAL(errorStreamUrl(QString)),
-            SLOT(skip()), Qt::UniqueConnection);
+    connect(video, &Video::gotStreamUrl, this, &MediaView::gotStreamUrl, Qt::UniqueConnection);
+    connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(skip()), Qt::UniqueConnection);
     video->loadStreamUrl();
 
     // video title in titlebar
-    MainWindow::instance()->setWindowTitle(video->title() + " - " + Constants::NAME);
-    MainWindow::instance()->showMessage(video->description());
+    MainWindow::instance()->setWindowTitle(video->getTitle() + QLatin1String(" - ") +
+                                           QLatin1String(Constants::NAME));
 
     // ensure active item is visible
+    int row = playlistModel->rowForVideo(video);
     if (row != -1) {
         QModelIndex index = playlistModel->index(row, 0, QModelIndex());
         playlistView->scrollTo(index, QAbstractItemView::EnsureVisible);
     }
 
     // enable/disable actions
-    The::globalActions()->value("download")->setEnabled(
-                DownloadManager::instance()->itemForVideo(video) == 0);
-    The::globalActions()->value("previous")->setEnabled(row > 0);
-    The::globalActions()->value("stopafterthis")->setEnabled(true);
-    The::globalActions()->value("related-videos")->setEnabled(true);
-
-    bool enableDownload = video->license() == Video::LicenseCC;
+    MainWindow::instance()
+            ->getAction("download")
+            ->setEnabled(DownloadManager::instance()->itemForVideo(video) == nullptr);
+    MainWindow::instance()->getAction("previous")->setEnabled(row > 0);
+    MainWindow::instance()->getAction("stopafterthis")->setEnabled(true);
+    MainWindow::instance()->getAction("relatedVideos")->setEnabled(true);
+
+    bool enableDownload = video->getLicense() == Video::LicenseCC;
 #ifdef APP_ACTIVATION
     enableDownload = enableDownload || Activation::instance().isLegacy();
 #endif
 #ifdef APP_DOWNLOADS
     enableDownload = true;
 #endif
-    QAction *a = The::globalActions()->value("download");
+    QAction *a = MainWindow::instance()->getAction("download");
     a->setEnabled(enableDownload);
     a->setVisible(enableDownload);
 
-    updateSubscriptionAction(video, YTChannel::isSubscribed(video->channelId()));
+    updateSubscriptionActionForVideo(video, YTChannel::isSubscribed(video->getChannelId()));
 
-    foreach (QAction *action, currentVideoActions)
+    for (QAction *action : currentVideoActions)
         action->setEnabled(true);
 
-#ifndef APP_PHONON_SEEK
-    QSlider *slider = MainWindow::instance()->getSlider();
-    slider->setEnabled(false);
-    slider->setValue(0);
-#endif
-
+#ifdef APP_SNAPSHOT
     if (snapshotSettings) {
         delete snapshotSettings;
-        snapshotSettings = 0;
+        snapshotSettings = nullptr;
+        MainWindow::instance()->adjustStatusBarVisibility();
     }
+#endif
 
     // see you in gotStreamUrl...
 }
 
-void MediaView::gotStreamUrl(QUrl streamUrl) {
+void MediaView::gotStreamUrl(const QString &streamUrl, const QString &audioUrl) {
     if (stopped) return;
-    if (!streamUrl.isValid()) {
+    if (streamUrl.isEmpty()) {
+        qWarning() << "Empty stream url";
         skip();
         return;
     }
@@ -482,14 +476,15 @@ void MediaView::gotStreamUrl(QUrl streamUrl) {
     }
     video->disconnect(this);
 
-    currentVideoId = video->id();
+    currentVideoId = video->getId();
 
-#ifdef APP_PHONON_SEEK
-    mediaObject->setCurrentSource(streamUrl);
-    mediaObject->play();
-#else
-    startDownloading();
-#endif
+    if (audioUrl.isEmpty()) {
+        qDebug() << "Playing" << streamUrl;
+        media->play(streamUrl);
+    } else {
+        qDebug() << "Playing" << streamUrl << audioUrl;
+        media->playSeparateAudioAndVideo(streamUrl, audioUrl);
+    }
 
     // ensure we always have videos ahead
     playlistModel->searchNeeded();
@@ -502,91 +497,28 @@ void MediaView::gotStreamUrl(QUrl streamUrl) {
     }
 
 #ifdef APP_ACTIVATION
-    if (!Activation::instance().isActivated())
-        demoTimer->start(180000);
+    if (!Activation::instance().isActivated() && !demoTimer->isActive()) {
+        int ms = (60000 * 5) + (qrand() % (60000 * 5));
+        demoTimer->start(ms);
+    }
 #endif
 
 #ifdef APP_EXTRA
-    Extra::notify(video->title(), video->channelTitle(), video->formattedDuration());
+    Extra::notify(video->getTitle(), video->getChannelTitle(), video->getFormattedDuration());
 #endif
 
     ChannelAggregator::instance()->videoWatched(video);
 }
 
-void MediaView::downloadStatusChanged() {
-    // qDebug() << __PRETTY_FUNCTION__;
-    switch(downloadItem->status()) {
-    case Downloading:
-        // qDebug() << "Downloading";
-        if (downloadItem->offset() == 0) startPlaying();
-        else {
-#ifdef APP_PHONON
-            // qDebug() << "Seeking to" << downloadItem->offset();
-            mediaObject->seek(offsetToTime(downloadItem->offset()));
-            mediaObject->play();
-#endif
-        }
-        break;
-    case Starting:
-        // qDebug() << "Starting";
-        break;
-    case Finished:
-        // qDebug() << "Finished" << mediaObject->state();
-#ifdef APP_PHONON_SEEK
-        MainWindow::instance()->getSeekSlider()->setEnabled(mediaObject->isSeekable());
-#endif
-        break;
-    case Failed:
-        // qDebug() << "Failed";
-        skip();
-        break;
-    case Idle:
-        // qDebug() << "Idle";
-        break;
-    }
-}
-
-void MediaView::startPlaying() {
-    // qDebug() << __PRETTY_FUNCTION__;
-    if (stopped) return;
-    if (!downloadItem) {
-        skip();
-        return;
-    }
-
-    if (downloadItem->offset() == 0) {
-        currentVideoSize = downloadItem->bytesTotal();
-        // qDebug() << "currentVideoSize" << currentVideoSize;
-    }
-
-    // go!
-    QString source = downloadItem->currentFilename();
-    qDebug() << "Playing" << source << QFile::exists(source);
-#ifdef APP_PHONON
-    mediaObject->setCurrentSource(QUrl::fromLocalFile(source));
-    mediaObject->play();
-#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) {
+void MediaView::onItemActivated(const QModelIndex &index) {
     if (playlistModel->rowExists(index.row())) {
-
         // if it's the current video, just rewind and play
         Video *activeVideo = playlistModel->activeVideo();
         Video *video = playlistModel->videoAt(index.row());
         if (activeVideo && video && activeVideo == video) {
-            // mediaObject->seek(0);
-            sliderMoved(0);
-#ifdef APP_PHONON
-            mediaObject->play();
-#endif
-        } else playlistModel->setActiveRow(index.row());
+            media->play();
+        } else
+            playlistModel->setActiveRow(index.row());
 
         // the user doubleclicked on the "Search More" item
     } else {
@@ -623,83 +555,75 @@ void MediaView::skipBackward() {
     playlistModel->setActiveRow(prevRow);
 }
 
-void MediaView::aboutToFinish() {
-#ifdef APP_PHONON
-    qint64 currentTime = mediaObject->currentTime();
-    qint64 totalTime = mediaObject->totalTime();
-    qDebug() << __PRETTY_FUNCTION__ << currentTime << totalTime;
+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()));
-        mediaObject->seek(currentTime);
-        mediaObject->play();
+        media->seek(currentTime);
+        media->play();
     }
-#endif
 }
 
-void MediaView::playbackFinished() {
+void MediaView::onPlaybackFinished() {
     if (stopped) return;
 
-#ifdef APP_PHONON
-    const qint64 totalTime = mediaObject->totalTime();
-    const qint64 currentTime = mediaObject->currentTime();
-    qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime() << totalTime;
+    const qint64 totalTime = media->duration();
+    const qint64 currentTime = media->position();
+    // 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()));
+        QTimer::singleShot(500, this, SLOT(resumePlayback()));
     } else {
-        QAction* stopAfterThisAction = The::globalActions()->value("stopafterthis");
+        QAction *stopAfterThisAction = MainWindow::instance()->getAction("stopafterthis");
         if (stopAfterThisAction->isChecked()) {
             stopAfterThisAction->setChecked(false);
-        } else skip();
+        } else
+            skip();
     }
-#endif
 }
 
-void MediaView::playbackResume() {
+void MediaView::resumePlayback() {
     if (stopped) return;
-#ifdef APP_PHONON
-    const qint64 currentTime = mediaObject->currentTime();
-    qDebug() << __PRETTY_FUNCTION__ << currentTime;
-    if (currentTime > 0)
-        mediaObject->seek(currentTime);
-    mediaObject->play();
-#endif
+    const qint64 currentTime = media->position();
+    // qDebug() << __PRETTY_FUNCTION__ << currentTime;
+    if (currentTime > 0) media->seek(currentTime);
+    media->play();
 }
 
 void MediaView::openWebPage() {
-    Videovideo = playlistModel->activeVideo();
+    Video *video = playlistModel->activeVideo();
     if (!video) return;
-#ifdef APP_PHONON
-    mediaObject->pause();
-#endif
-    QDesktopServices::openUrl(video->webpage());
+    media->pause();
+    QString url =
+            video->getWebpage() + QLatin1String("&t=") + QString::number(media->position() / 1000);
+    QDesktopServices::openUrl(url);
 }
 
 void MediaView::copyWebPage() {
-    Videovideo = playlistModel->activeVideo();
+    Video *video = playlistModel->activeVideo();
     if (!video) return;
-    QString address = video->webpage();
+    QString address = video->getWebpage();
     QApplication::clipboard()->setText(address);
     QString message = tr("You can now paste the YouTube link into another application");
     MainWindow::instance()->showMessage(message);
 }
 
 void MediaView::copyVideoLink() {
-    Videovideo = playlistModel->activeVideo();
+    Video *video = playlistModel->activeVideo();
     if (!video) return;
-    QApplication::clipboard()->setText(video->getStreamUrl().toEncoded());
-    QString message = tr("You can now paste the video stream URL into another application")
-            + ". " + tr("The link will be valid only for a limited time.");
+    QApplication::clipboard()->setText(video->getStreamUrl());
+    QString message = tr("You can now paste the video stream URL into another application") + ". " +
+                      tr("The link will be valid only for a limited time.");
     MainWindow::instance()->showMessage(message);
 }
 
 void MediaView::openInBrowser() {
-    Videovideo = playlistModel->activeVideo();
+    Video *video = playlistModel->activeVideo();
     if (!video) return;
-#ifdef APP_PHONON
-    mediaObject->pause();
-#endif
+    media->pause();
     QDesktopServices::openUrl(video->getStreamUrl());
 }
 
@@ -709,8 +633,8 @@ void MediaView::removeSelected() {
     playlistModel->removeIndexes(indexes);
 }
 
-void MediaView::selectVideos(QList<Video*> videos) {
-    foreach (Video *video, videos) {
+void MediaView::selectVideos(const QVector<Video *> &videos) {
+    for (Video *video : videos) {
         QModelIndex index = playlistModel->indexForVideo(video);
         playlistView->selectionModel()->select(index, QItemSelectionModel::Select);
         playlistView->scrollTo(index, QAbstractItemView::EnsureVisible);
@@ -720,9 +644,9 @@ void MediaView::selectVideos(QList<Video*> videos) {
 void MediaView::selectionChanged(const QItemSelection & /*selected*/,
                                  const QItemSelection & /*deselected*/) {
     const bool gotSelection = playlistView->selectionModel()->hasSelection();
-    The::globalActions()->value("remove")->setEnabled(gotSelection);
-    The::globalActions()->value("moveUp")->setEnabled(gotSelection);
-    The::globalActions()->value("moveDown")->setEnabled(gotSelection);
+    MainWindow::instance()->getAction("remove")->setEnabled(gotSelection);
+    MainWindow::instance()->getAction("moveUp")->setEnabled(gotSelection);
+    MainWindow::instance()->getAction("moveDown")->setEnabled(gotSelection);
 }
 
 void MediaView::moveUpSelected() {
@@ -733,8 +657,8 @@ void MediaView::moveUpSelected() {
     playlistModel->move(indexes, true);
 
     // set current index after row moves to something more intuitive
-    int row = indexes.first().row();
-    playlistView->selectionModel()->setCurrentIndex(playlistModel->index(row>1?row:1),
+    int row = indexes.at(0).row();
+    playlistView->selectionModel()->setCurrentIndex(playlistModel->index(row > 1 ? row : 1),
                                                     QItemSelectionModel::NoUpdate);
 }
 
@@ -747,242 +671,167 @@ void MediaView::moveDownSelected() {
 
     // set current index after row moves to something more intuitive
     // (respect 1 static item on bottom)
-    int row = indexes.first().row()+1, max = playlistModel->rowCount() - 2;
-    playlistView->selectionModel()->setCurrentIndex(
-                playlistModel->index(row>max?max:row), QItemSelectionModel::NoUpdate);
+    int row = indexes.at(0).row() + 1, max = playlistModel->rowCount() - 2;
+    playlistView->selectionModel()->setCurrentIndex(playlistModel->index(row > max ? max : row),
+                                                    QItemSelectionModel::NoUpdate);
 }
 
-void MediaView::setPlaylistVisible(bool visible) {
-    if (splitter->widget(0)->isVisible() == visible) return;
-    splitter->widget(0)->setVisible(visible);
-    playlistView->setFocus();
+void MediaView::setSidebarVisibility(bool visible) {
+    if (sidebar->isVisible() == visible) return;
+    sidebar->setVisible(visible);
+    if (visible) {
+        sidebar->move(0, 0);
+        sidebar->resize(sidebar->width(), window()->height());
+        sidebar->raise();
+        playlistView->setFocus();
+    }
 }
 
-bool MediaView::isPlaylistVisible() {
-    return splitter->widget(0)->isVisible();
+void MediaView::removeSidebar() {
+    sidebar->hide();
+    sidebar->setParent(window());
 }
 
-void MediaView::saveSplitterState() {
-    QSettings settings;
-    settings.setValue("splitter", splitter->saveState());
+void MediaView::restoreSidebar() {
+    sidebar->show();
+    splitter->insertWidget(0, sidebar);
 }
 
-#ifdef APP_ACTIVATION
-
-static QPushButton *continueButton;
-
-void MediaView::demoMessage() {
-#ifdef APP_PHONON
-    if (mediaObject->state() != Phonon::PlayingState) return;
-    mediaObject->pause();
-#endif
-
-    QMessageBox msgBox(this);
-    msgBox.setIconPixmap(QPixmap(":/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);
-    // make it a "sheet" on the Mac
-    msgBox.setWindowModality(Qt::WindowModal);
-
-    continueButton = msgBox.addButton("5", QMessageBox::RejectRole);
-    continueButton->setEnabled(false);
-    QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
-
-    QTimeLine *timeLine = new QTimeLine(6000, this);
-    timeLine->setCurveShape(QTimeLine::LinearCurve);
-    timeLine->setFrameRange(5, 0);
-    connect(timeLine, SIGNAL(frameChanged(int)), SLOT(updateContinueButton(int)));
-    timeLine->start();
-
-    msgBox.exec();
-
-    if (msgBox.clickedButton() == buyButton) {
-        MainWindow::instance()->showActivationView();
-    } else {
-#ifdef APP_PHONON
-        mediaObject->play();
-#endif
-        demoTimer->start(600000);
-    }
-
-    delete timeLine;
-
+bool MediaView::isSidebarVisible() {
+    return sidebar->isVisible();
 }
 
-void MediaView::updateContinueButton(int value) {
-    if (value == 0) {
-        continueButton->setText(tr("Continue"));
-        continueButton->setEnabled(true);
-    } else {
-        continueButton->setText(QString::number(value));
-    }
+void MediaView::saveSplitterState() {
+    QSettings settings;
+    if (splitter) settings.setValue("splitter", splitter->saveState());
 }
 
-#endif
-
 void MediaView::downloadVideo() {
-    Videovideo = playlistModel->activeVideo();
+    Video *video = playlistModel->activeVideo();
     if (!video) return;
     DownloadManager::instance()->addItem(video);
-    The::globalActions()->value("downloads")->setVisible(true);
-    QString message = tr("Downloading %1").arg(video->title());
+    MainWindow::instance()->showActionsInStatusBar({MainWindow::instance()->getAction("downloads")},
+                                                   true);
+    QString message = tr("Downloading %1").arg(video->getTitle());
     MainWindow::instance()->showMessage(message);
 }
 
 #ifdef APP_SNAPSHOT
 void MediaView::snapshot() {
-    qint64 currentTime = mediaObject->currentTime() / 1000;
+    qint64 currentTime = media->position() / 1000;
 
-    QImage image = videoWidget->snapshot();
-    if (image.isNull()) {
-        qWarning() << "Null snapshot";
-        return;
-    }
+    QObject *context = new QObject();
+    connect(media, &Media::snapshotReady, context,
+            [this, currentTime, context](const QImage &image) {
+                context->deleteLater();
 
-    // 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;
+                if (image.isNull()) {
+                    qWarning() << "Null snapshot";
+                    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();
+                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->getTitle();
+                QString format = video->getDuration() > 3600 ? "h_mm_ss" : "m_ss";
+                basename += " (" + QTime(0, 0, 0).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);
+                Extra::fadeInWidget(statusBar, statusBar);
 #endif
-    statusBar->clearMessage();
-    statusBar->insertPermanentWidget(0, snapshotSettings);
-    snapshotSettings->show();
-}
+                statusBar->insertPermanentWidget(0, snapshotSettings);
+                snapshotSettings->show();
+                MainWindow::instance()->setStatusBarVisibility(true);
+            }
 #endif
+    );
 
-void MediaView::fullscreen() {
-    videoAreaWidget->setParent(0);
-    videoAreaWidget->showFullScreen();
+    media->snapshot();
 }
 
-void MediaView::startDownloading() {
-    Video *video = playlistModel->activeVideo();
-    if (!video) return;
-    Video *videoCopy = video->clone();
-    if (downloadItem) {
-        downloadItem->stop();
-        delete downloadItem;
-    }
-    QString tempFile = Temporary::filename();
-    downloadItem = new DownloadItem(videoCopy, video->getStreamUrl(), tempFile, this);
-    connect(downloadItem, SIGNAL(statusChanged()),
-            SLOT(downloadStatusChanged()), Qt::UniqueConnection);
-    connect(downloadItem, SIGNAL(bufferProgress(int)),
-            loadingWidget, SLOT(bufferStatus(int)), Qt::UniqueConnection);
-    // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
-    connect(video, SIGNAL(errorStreamUrl(QString)),
-            SLOT(handleError(QString)), Qt::UniqueConnection);
-    connect(downloadItem, SIGNAL(error(QString)),
-            SLOT(handleError(QString)), Qt::UniqueConnection);
-    downloadItem->start();
+void MediaView::fullscreen() {
+    videoAreaWidget->setParent(nullptr);
+    videoAreaWidget->showFullScreen();
 }
 
-void MediaView::sliderMoved(int value) {
-#ifdef APP_PHONON
-#ifndef APP_PHONON_SEEK
-
-    if (currentVideoSize <= 0 || !downloadItem || !mediaObject->isSeekable())
-        return;
+void MediaView::resumeWithNewStreamUrl(const QString &streamUrl, const QString &audioUrl) {
+    pauseTime = media->position();
 
-    QSlider *slider = MainWindow::instance()->getSlider();
-    if (slider->isSliderDown()) return;
-
-    qint64 offset = (currentVideoSize * value) / slider->maximum();
-
-    bool needsDownload = downloadItem->needsDownload(offset);
-    if (needsDownload) {
-        if (downloadItem->isBuffered(offset)) {
-            qint64 realOffset = downloadItem->blankAtOffset(offset);
-            if (offset < currentVideoSize)
-                downloadItem->seekTo(realOffset, false);
-            mediaObject->seek(offsetToTime(offset));
-        } else {
-            mediaObject->pause();
-            downloadItem->seekTo(offset);
-        }
+    if (audioUrl.isEmpty()) {
+        qDebug() << "Playing" << streamUrl;
+        media->play(streamUrl);
     } else {
-        // qDebug() << "simple seek";
-        mediaObject->seek(offsetToTime(offset));
+        qDebug() << "Playing" << streamUrl << audioUrl;
+        media->playSeparateAudioAndVideo(streamUrl, audioUrl);
     }
-#endif
-#endif
-}
 
-qint64 MediaView::offsetToTime(qint64 offset) {
-#ifdef APP_PHONON
-    const qint64 totalTime = mediaObject->totalTime();
-    return ((offset * totalTime) / currentVideoSize);
-#endif
+    Video *video = static_cast<Video *>(sender());
+    if (!video) {
+        qDebug() << "Cannot get sender in" << __PRETTY_FUNCTION__;
+        return;
+    }
+    video->disconnect(this);
 }
 
 void MediaView::findVideoParts() {
-
-    // parts
-    Video* video = playlistModel->activeVideo();
+    Video *video = playlistModel->activeVideo();
     if (!video) return;
 
-    QString query = video->title();
+    QString query = video->getTitle();
 
-    static QString optionalSpace = "\\s*";
-    static QString staticCounterSeparators = "[\\/\\-]";
-    QString counterSeparators = "( of | " +
-            tr("of", "Used in video parts, as in '2 of 3'") +
-            " |" + staticCounterSeparators + ")";
+    const QLatin1String optionalSpace("\\s*");
+    const QLatin1String staticCounterSeparators("[\\/\\-]");
+    const QString counterSeparators =
+            QLatin1String("( of | ") + tr("of", "Used in video parts, as in '2 of 3'") +
+            QLatin1String(" |") + staticCounterSeparators + QLatin1String(")");
 
     // numbers from 1 to 15
-    static QString counterNumber = "([1-9]|1[0-5])";
+    const QLatin1String counterNumber("([1-9]|1[0-5])");
 
     // query.remove(QRegExp(counterSeparators + optionalSpace + counterNumber));
-    query.remove(QRegExp(counterNumber + optionalSpace +
-                         counterSeparators + optionalSpace + counterNumber));
+    query.remove(QRegExp(counterNumber + optionalSpace + counterSeparators + optionalSpace +
+                         counterNumber));
     query.remove(wordRE("pr?t\\.?" + optionalSpace + counterNumber));
     query.remove(wordRE("ep\\.?" + optionalSpace + counterNumber));
     query.remove(wordRE("part" + optionalSpace + counterNumber));
     query.remove(wordRE("episode" + optionalSpace + counterNumber));
     query.remove(wordRE(tr("part", "This is for video parts, as in 'Cool video - part 1'") +
                         optionalSpace + counterNumber));
-    query.remove(wordRE(tr("episode",
-                           "This is for video parts, as in 'Cool series - episode 1'") +
+    query.remove(wordRE(tr("episode", "This is for video parts, as in 'Cool series - episode 1'") +
                         optionalSpace + counterNumber));
     query.remove(QRegExp("[\\(\\)\\[\\]]"));
 
 #define NUMBERS "one|two|three|four|five|six|seven|eight|nine|ten"
 
-    QRegExp englishNumberRE = QRegExp(QLatin1String(".*(") + NUMBERS + ").*",
-                                      Qt::CaseInsensitive);
+    QRegExp englishNumberRE = QRegExp(QLatin1String(".*(") + NUMBERS + ").*", Qt::CaseInsensitive);
     // bool numberAsWords = englishNumberRE.exactMatch(query);
     query.remove(englishNumberRE);
 
-    QRegExp localizedNumberRE = QRegExp(QLatin1String(".*(") + tr(NUMBERS) + ").*",
-                                        Qt::CaseInsensitive);
+    QRegExp localizedNumberRE =
+            QRegExp(QLatin1String(".*(") + tr(NUMBERS) + ").*", Qt::CaseInsensitive);
     // if (!numberAsWords) numberAsWords = localizedNumberRE.exactMatch(query);
     query.remove(localizedNumberRE);
 
     SearchParams *searchParams = new SearchParams();
     searchParams->setTransient(true);
     searchParams->setKeywords(query);
-    searchParams->setChannelId(video->channelId());
+    searchParams->setChannelId(video->getChannelId());
 
     /*
     if (!numberAsWords) {
@@ -994,103 +843,59 @@ void MediaView::findVideoParts() {
     */
 
     search(searchParams);
-
 }
 
 void MediaView::relatedVideos() {
-    Videovideo = playlistModel->activeVideo();
+    Video *video = playlistModel->activeVideo();
     if (!video) return;
     YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource();
     singleVideoSource->setVideo(video->clone());
     singleVideoSource->setAsyncDetails(true);
     setVideoSource(singleVideoSource);
-    The::globalActions()->value("related-videos")->setEnabled(false);
+    MainWindow::instance()->getAction("relatedVideos")->setEnabled(false);
 }
 
 void MediaView::shareViaTwitter() {
-    Videovideo = playlistModel->activeVideo();
+    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);
-    }
-#endif
+    QUrlQuery q;
+    q.addQueryItem("via", "minitubeapp");
+    q.addQueryItem("text", video->getTitle());
+    q.addQueryItem("url", video->getWebpage());
+    url.setQuery(q);
     QDesktopServices::openUrl(url);
 }
 
 void MediaView::shareViaFacebook() {
-    Videovideo = playlistModel->activeVideo();
+    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);
-    }
-#endif
-    QDesktopServices::openUrl(url);
-}
-
-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);
-    }
-#endif
+    QUrlQuery q;
+    q.addQueryItem("t", video->getTitle());
+    q.addQueryItem("u", video->getWebpage());
+    url.setQuery(q);
     QDesktopServices::openUrl(url);
 }
 
 void MediaView::shareViaEmail() {
-    Videovideo = playlistModel->activeVideo();
+    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() + "\n\n" +
-                tr("Sent from %1").arg(Constants::NAME) + "\n" +
-                Constants::WEBSITE;
-        url.addQueryItem("body", body);
-#if QT_VERSION >= 0x050000
-        u.setQuery(url);
-    }
-#endif
+    QUrlQuery q;
+    q.addQueryItem("subject", video->getTitle());
+    const QString body = video->getTitle() + "\n" + video->getWebpage() + "\n\n" +
+                         tr("Sent from %1").arg(Constants::NAME) + "\n" + Constants::WEBSITE;
+    q.addQueryItem("body", body);
+    url.setQuery(q);
     QDesktopServices::openUrl(url);
 }
 
-void MediaView::authorPushed(QModelIndex index) {
-    Videovideo = playlistModel->videoAt(index.row());
+void MediaView::onAuthorPushed(QModelIndex index) {
+    Video *video = playlistModel->videoAt(index.row());
     if (!video) return;
 
-    QString channelId = video->channelId();
+    QString channelId = video->getChannelId();
     // if (channelId.isEmpty()) channelId = video->channelTitle();
     if (channelId.isEmpty()) return;
 
@@ -1102,20 +907,22 @@ void MediaView::authorPushed(QModelIndex index) {
     search(searchParams);
 }
 
-void MediaView::updateSubscriptionAction(Video *video, bool subscribed) {
-    QAction *subscribeAction = The::globalActions()->value("subscribe-channel");
+
+void MediaView::updateSubscriptionAction(bool subscribed) {
+    QAction *subscribeAction = MainWindow::instance()->getAction("subscribeChannel");
 
     QString subscribeTip;
     QString subscribeText;
-    if (!video) {
+
+    if (currentSubscriptionChannelId.isEmpty()) {
         subscribeText = subscribeAction->property("originalText").toString();
         subscribeAction->setEnabled(false);
     } else if (subscribed) {
-        subscribeText = tr("Unsubscribe from %1").arg(video->channelTitle());
+        subscribeText = tr("Unsubscribe from %1").arg(currentSubscriptionChannelTitle);
         subscribeTip = subscribeText;
         subscribeAction->setEnabled(true);
     } else {
-        subscribeText = tr("Subscribe to %1").arg(video->channelTitle());
+        subscribeText = tr("Subscribe to %1").arg(currentSubscriptionChannelTitle);
         subscribeTip = subscribeText;
         subscribeAction->setEnabled(true);
     }
@@ -1123,31 +930,109 @@ void MediaView::updateSubscriptionAction(Video *video, bool subscribed) {
     subscribeAction->setStatusTip(subscribeTip);
 
     if (subscribed) {
-#ifdef Q_OS_LINUX
-        static QIcon tintedIcon;
-        if (tintedIcon.isNull()) {
-            QList<QSize> sizes;
-            sizes << QSize(16, 16);
-            tintedIcon = IconUtils::tintedIcon("bookmark-new", QColor(254, 240, 0), sizes);
-        }
-        subscribeAction->setIcon(tintedIcon);
-#else
         subscribeAction->setIcon(IconUtils::icon("bookmark-remove"));
-#endif
     } else {
         subscribeAction->setIcon(IconUtils::icon("bookmark-new"));
     }
 
-    IconUtils::setupAction(subscribeAction);
+    MainWindow::instance()->setupAction(subscribeAction);
+}
+
+void MediaView::updateSubscriptionActionForChannel(const QString & channelId) {
+    QString channelTitle = tr("channel");
+    YTChannel *channel = YTChannel::forId(channelId);
+    if (nullptr != channel && !channel->getDisplayName().isEmpty()) {
+        channelTitle = channel->getDisplayName();
+    }
+
+    bool subscribed = YTChannel::isSubscribed(channelId);
+
+    currentSubscriptionChannelId = channelId;
+    currentSubscriptionChannelTitle = channelTitle;
+    updateSubscriptionAction(subscribed);
+}
+
+void MediaView::updateSubscriptionActionForVideo(Video *video, bool subscribed) {
+    if (!video) {
+        currentSubscriptionChannelId = "";
+        currentSubscriptionChannelTitle = "";
+        updateSubscriptionAction(false);
+    } else {
+        currentSubscriptionChannelId = video->getChannelId();
+        currentSubscriptionChannelTitle = video->getChannelTitle();
+        updateSubscriptionAction(subscribed);
+    }
+}
+
+void MediaView::reloadCurrentVideo() {
+    Video *video = playlistModel->activeVideo();
+    if (!video) return;
+
+    int oldFormat = video->getDefinitionCode();
+
+    QObject *context = new QObject();
+    connect(video, &Video::gotStreamUrl, context,
+            [this, oldFormat, video, context](const QString &videoUrl, const QString &audioUrl) {
+                context->deleteLater();
+                if (oldFormat == video->getDefinitionCode()) return;
+                QObject *context2 = new QObject();
+                const qint64 position = media->position();
+                connect(media, &Media::stateChanged, context2,
+                        [position, this, context2](Media::State state) {
+                            if (state == Media::PlayingState) {
+                                media->seek(position);
+                                context2->deleteLater();
+                                Video *video = playlistModel->activeVideo();
+                                QString msg = tr("Switched to %1")
+                                                      .arg(VideoDefinition::forCode(
+                                                                   video->getDefinitionCode())
+                                                                   .getName());
+                                MainWindow::instance()->showMessage(msg);
+                            }
+                        });
+
+                if (audioUrl.isEmpty()) {
+                    media->play(videoUrl);
+                } else {
+                    media->playSeparateAudioAndVideo(videoUrl, audioUrl);
+                }
+            });
+    video->loadStreamUrl();
 }
 
 void MediaView::toggleSubscription() {
+    //Video *video = playlistModel->activeVideo();
+    if (currentSubscriptionChannelId.isEmpty()) {
+        return;
+    }
+
+    bool subscribed = YTChannel::isSubscribed(currentSubscriptionChannelId);
+    if (subscribed) {
+        YTChannel::unsubscribe(currentSubscriptionChannelId);
+        MainWindow::instance()->showMessage(
+                tr("Unsubscribed from %1").arg(currentSubscriptionChannelTitle));
+    } else {
+        YTChannel::subscribe(currentSubscriptionChannelId);
+        MainWindow::instance()->showMessage(tr("Subscribed to %1").arg(currentSubscriptionChannelTitle));
+    }
+
+    updateSubscriptionAction(!subscribed);
+}
+
+void MediaView::adjustWindowSize() {
+    qDebug() << "Adjusting window size";
     Video *video = playlistModel->activeVideo();
     if (!video) return;
-    QString userId = video->channelId();
-    if (userId.isEmpty()) return;
-    bool subscribed = YTChannel::isSubscribed(userId);
-    if (subscribed) YTChannel::unsubscribe(userId);
-    else YTChannel::subscribe(userId);
-    updateSubscriptionAction(video, !subscribed);
+    QWidget *window = this->window();
+    if (!window->isMaximized() && !window->isFullScreen()) {
+        const double ratio = 16. / 9.;
+        const double w = (double)videoAreaWidget->width();
+        const double h = (double)videoAreaWidget->height();
+        const double currentVideoRatio = w / h;
+        if (currentVideoRatio != ratio) {
+            qDebug() << "Adjust size";
+            int newHeight = std::round((window->height() - h) + (w / ratio));
+            window->resize(window->width(), newHeight);
+        }
+    }
 }