]> git.sur5r.net Git - minitube/blobdiff - src/MediaView.cpp
Imported Debian patch 1.5-1
[minitube] / src / MediaView.cpp
index 739c595a048b4bafd7449409c2d1fa57f550f6cf..d41c69eae29e8970cd34ec805be6f986b639d5cc 100644 (file)
@@ -5,6 +5,12 @@
 #include "minisplitter.h"
 #include "constants.h"
 #include "downloadmanager.h"
+#include "downloaditem.h"
+#include "MainWindow.h"
+
+namespace The {
+    NetworkAccess* http();
+}
 
 namespace The {
     QMap<QString, QAction*>* globalActions();
@@ -15,6 +21,7 @@ namespace The {
 MediaView::MediaView(QWidget *parent) : QWidget(parent) {
 
     reallyStopped = false;
+    downloadItem = 0;
 
     QBoxLayout *layout = new QHBoxLayout();
     layout->setMargin(0);
@@ -117,7 +124,6 @@ MediaView::MediaView(QWidget *parent) : QWidget(parent) {
 #ifdef APP_DEMO
     demoTimer = new QTimer(this);
     demoTimer->setSingleShot(true);
-    demoTimer->setInterval(60000);
     connect(demoTimer, SIGNAL(timeout()), SLOT(demoMessage()));
 #endif
 
@@ -133,12 +139,12 @@ void MediaView::initialize() {
 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
     this->mediaObject = mediaObject;
     Phonon::createPath(this->mediaObject, videoWidget);
-    connect(mediaObject, SIGNAL(finished()), this, SLOT(skip()));
+    connect(mediaObject, SIGNAL(finished()), this, SLOT(playbackFinished()));
     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
     connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
             this, SLOT(currentSourceChanged(Phonon::MediaSource)));
-    connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
+    // connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
 }
 
 void MediaView::search(SearchParams *searchParams) {
@@ -152,6 +158,12 @@ void MediaView::search(SearchParams *searchParams) {
     workaroundTimer->stop();
     errorTimer->stop();
 
+    mediaObject->pause();
+    if (downloadItem) {
+        delete downloadItem;
+        downloadItem = 0;
+    }
+
     this->searchParams = searchParams;
 
     // start serching for videos
@@ -162,6 +174,20 @@ void MediaView::search(SearchParams *searchParams) {
 
     listView->setFocus();
 
+
+    QString keyword = searchParams->keywords();
+    QString display = keyword;
+    if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
+        int separator = keyword.indexOf("|");
+        if (separator > 0 && separator + 1 < keyword.length()) {
+            display = keyword.mid(separator+1);
+        }
+
+        // also hide sidebar
+        playlistWidget->hide();
+    } else playlistWidget->show();
+    // tr("You're watching \"%1\"").arg(searchParams->keywords())
+
 }
 
 void MediaView::disappear() {
@@ -169,54 +195,63 @@ void MediaView::disappear() {
 }
 
 void MediaView::handleError(QString message) {
+    // if (message.indexOf("movie atom") != -1 || message.indexOf("Could not open") != -1) {
+        QTimer::singleShot(1000, this, SLOT(startPlaying()));
+        return;
+    // }
+
+    /*
     videoAreaWidget->showError(message);
     skippedVideo = listModel->activeVideo();
     // recover from errors by skipping to the next video
     errorTimer->start(2000);
+    */
 }
 
 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
 {
 
     // qDebug() << "Phonon state: " << newState << oldState;
+    // slider->setEnabled(newState == Phonon::PlayingState);
 
     switch (newState) {
 
     case Phonon::ErrorState:
         qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
-        handleError(mediaObject->errorString());
+        if (mediaObject->errorType() == Phonon::FatalError)
+            handleError(mediaObject->errorString());
         break;
 
     case Phonon::PlayingState:
-        //qDebug("playing");
+        // qDebug("playing");
         videoAreaWidget->showVideo();
         break;
 
     case Phonon::StoppedState:
-        //qDebug("stopped");
+        // qDebug("stopped");
         // play() has already been called when setting the source
         // but Phonon on Linux needs a little more help to start playback
-        if (!reallyStopped) mediaObject->play();
+        // if (!reallyStopped) mediaObject->play();
 
 #ifdef APP_MAC
         // Workaround for Mac playback start problem
         if (!timerPlayFlag) {
-            workaroundTimer->start();
+            // workaroundTimer->start();
         }
 #endif
 
         break;
 
          case Phonon::PausedState:
-        //qDebug("paused");
+        qDebug("paused");
         break;
 
          case Phonon::BufferingState:
-        //qDebug("buffering");
+        qDebug("buffering");
         break;
 
          case Phonon::LoadingState:
-        //qDebug("loading");
+        qDebug("loading");
         break;
 
          default:
@@ -244,6 +279,11 @@ void MediaView::stop() {
     workaroundTimer->stop();
     errorTimer->stop();
     listView->selectionModel()->clearSelection();
+    if (downloadItem) {
+        downloadItem->stop();
+        delete downloadItem;
+        downloadItem = 0;
+    }
 }
 
 void MediaView::activeRowChanged(int row) {
@@ -257,6 +297,14 @@ void MediaView::activeRowChanged(int row) {
     workaroundTimer->stop();
     errorTimer->stop();
 
+    mediaObject->pause();
+    if (downloadItem) {
+        downloadItem->stop();
+        delete downloadItem;
+        downloadItem = 0;
+    }
+    // slider->setMinimum(0);
+
     // immediately show the loading widget
     videoAreaWidget->showLoading(video);
 
@@ -275,6 +323,13 @@ void MediaView::activeRowChanged(int row) {
 
     The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
 
+    // ensure active item is visible
+    // int row = listModel->activeRow();
+    if (row != -1) {
+        QModelIndex index = listModel->index(row, 0, QModelIndex());
+        listView->scrollTo(index, QAbstractItemView::EnsureVisible);
+    }
+
     // see you in gotStreamUrl...
 
 }
@@ -289,9 +344,91 @@ void MediaView::gotStreamUrl(QUrl streamUrl) {
     }
     video->disconnect(this);
 
+
+    QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
+#ifdef Q_WS_X11
+    QString tempFile = tempDir + "/minitube-" + getenv("USERNAME") + ".mp4";
+#else
+    QString tempFile = tempDir + "/minitube.mp4";
+#endif
+    if (QFile::exists(tempFile) && !QFile::remove(tempFile)) {
+        qDebug() << "Cannot remove temp file";
+    }
+
+    Video *videoCopy = video->clone();
+    if (downloadItem) {
+        downloadItem->stop();
+        delete downloadItem;
+    }
+    downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
+    connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
+    // connect(downloadItem, SIGNAL(progress(int)), SLOT(downloadProgress(int)));
+    connect(downloadItem, SIGNAL(bufferProgress(int)), loadingWidget, SLOT(bufferStatus(int)));
+    // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
+    connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
+    connect(downloadItem, SIGNAL(error(QString)), SLOT(handleError(QString)));
+    downloadItem->start();
+
+}
+
+/*
+void MediaView::downloadProgress(int percent) {
+    MainWindow* mainWindow = dynamic_cast<MainWindow*>(window());
+
+    mainWindow->getSeekSlider()->setStyleSheet(" QSlider::groove:horizontal {"
+        "border: 1px solid #999999;"
+        // "border-left: 50px solid rgba(255, 0, 0, 128);"
+        "height: 8px;"
+        "background: qlineargradient(x1:0, y1:0, x2:.5, y2:0, stop:0 rgba(255, 0, 0, 92), stop:"
+        + QString::number(percent/100.0) +
+
+        " rgba(255, 0, 0, 92), stop:" + QString::number((percent+1)/100.0) + " transparent, stop:1 transparent);"
+        "margin: 2px 0;"
+    "}"
+    "QSlider::handle:horizontal {"
+        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);"
+        "border: 1px solid #5c5c5c;"
+        "width: 16px;"
+        "height: 16px;"
+        "margin: -2px 0;"
+        "border-radius: 8px;"
+    "}"
+
+    );
+}
+
+*/
+
+void MediaView::downloadStatusChanged() {
+    switch(downloadItem->status()) {
+    case Downloading:
+        startPlaying();
+        break;
+    case Starting:
+        // qDebug() << "Starting";
+        break;
+    case Finished:
+        // qDebug() << "Finished" << mediaObject->state();
+        // if (mediaObject->state() == Phonon::StoppedState) startPlaying();
+        break;
+    case Failed:
+        // qDebug() << "Failed";
+    case Idle:
+        // qDebug() << "Idle";
+        break;
+    }
+}
+
+void MediaView::startPlaying() {
+    if (reallyStopped) return;
+    if (!downloadItem) {
+        skip();
+        return;
+    }
+
     // go!
-    qDebug() << "Playing" << streamUrl.toString();
-    mediaObject->setCurrentSource(streamUrl);
+    qDebug() << "Playing" << downloadItem->currentFilename();
+    mediaObject->setCurrentSource(downloadItem->currentFilename());
     mediaObject->play();
 
     // ensure we always have 10 videos ahead
@@ -305,7 +442,7 @@ void MediaView::gotStreamUrl(QUrl streamUrl) {
     }
 
 #ifdef APP_DEMO
-    demoTimer->start();
+    demoTimer->start(30000);
 #endif
 
 }
@@ -343,6 +480,20 @@ void MediaView::skip() {
     listModel->setActiveRow(nextRow);
 }
 
+void MediaView::playbackFinished() {
+    // qDebug() << "finished" << mediaObject->currentTime() << mediaObject->totalTime();
+    // add 10 secs for imprecise Phonon backends (VLC, Xine)
+    if (mediaObject->currentTime() + 10000 < mediaObject->totalTime()) {
+        // mediaObject->seek(mediaObject->currentTime());
+        QTimer::singleShot(3000, this, SLOT(playbackResume()));
+    } else skip();
+}
+
+void MediaView::playbackResume() {
+    mediaObject->seek(mediaObject->currentTime());
+    mediaObject->play();
+}
+
 void MediaView::openWebPage() {
     Video* video = listModel->activeVideo();
     if (!video) return;
@@ -364,7 +515,7 @@ void MediaView::copyWebPage() {
 void MediaView::copyVideoLink() {
     Video* video = listModel->activeVideo();
     if (!video) return;
-    QApplication::clipboard()->setText(video->getStreamUrl().toString());
+    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.");
     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
@@ -460,11 +611,13 @@ void MediaView::demoMessage() {
     if (mediaObject->state() != Phonon::PlayingState) return;
     mediaObject->pause();
 
-    QMessageBox msgBox;
+    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::APP_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);
 
     QPushButton *quitButton = msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
     QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
@@ -475,6 +628,7 @@ void MediaView::demoMessage() {
         QDesktopServices::openUrl(QString(Constants::WEBSITE) + "#download");
     } else {
         mediaObject->play();
+        demoTimer->start(300000);
     }
 }
 #endif
@@ -500,3 +654,50 @@ void MediaView::fullscreen() {
     videoAreaWidget->setParent(0);
     videoAreaWidget->showFullScreen();
 }
+
+/*
+void MediaView::setSlider(QSlider *slider) {
+    this->slider = slider;
+    // slider->setEnabled(false);
+    slider->setTracking(false);
+    // connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
+}
+
+void MediaView::sliderMoved(int value) {
+    qDebug() << __func__;
+    int sliderPercent = (value * 100) / (slider->maximum() - slider->minimum());
+    qDebug() << slider->minimum() << value << slider->maximum();
+    if (sliderPercent <= downloadItem->currentPercent()) {
+        qDebug() << sliderPercent << downloadItem->currentPercent();
+        mediaObject->seek(value);
+    } else {
+        seekTo(value);
+    }
+}
+
+void MediaView::seekTo(int value) {
+    qDebug() << __func__;
+    mediaObject->pause();
+    workaroundTimer->stop();
+    errorTimer->stop();
+    // mediaObject->clear();
+
+    QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
+    QString tempFile = tempDir + "/minitube" + QString::number(value) + ".mp4";
+    if (!QFile::remove(tempFile)) {
+        qDebug() << "Cannot remove temp file";
+    }
+    Video *videoCopy = downloadItem->getVideo()->clone();
+    QUrl streamUrl = videoCopy->getStreamUrl();
+    streamUrl.addQueryItem("begin", QString::number(value));
+    if (downloadItem) delete downloadItem;
+    downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
+    connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
+    // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
+    downloadItem->start();
+
+    // slider->setMinimum(value);
+
+}
+
+*/