2 #include "playlist/PrettyItemDelegate.h"
3 #include "networkaccess.h"
4 #include "videowidget.h"
5 #include "minisplitter.h"
7 #include "downloadmanager.h"
8 #include "downloaditem.h"
9 #include "MainWindow.h"
12 NetworkAccess* http();
16 QMap<QString, QAction*>* globalActions();
17 QMap<QString, QMenu*>* globalMenus();
18 QNetworkAccessManager* networkAccessManager();
21 MediaView::MediaView(QWidget *parent) : QWidget(parent) {
23 reallyStopped = false;
26 QBoxLayout *layout = new QHBoxLayout();
29 splitter = new MiniSplitter(this);
30 splitter->setChildrenCollapsible(false);
32 sortBar = new THBlackBar(this);
33 mostRelevantAction = new QAction(tr("Most relevant"), this);
34 QKeySequence keySequence(Qt::CTRL + Qt::Key_1);
35 mostRelevantAction->setShortcut(keySequence);
36 mostRelevantAction->setStatusTip(mostRelevantAction->text() + " (" + keySequence.toString(QKeySequence::NativeText) + ")");
37 addAction(mostRelevantAction);
38 connect(mostRelevantAction, SIGNAL(triggered()), this, SLOT(searchMostRelevant()), Qt::QueuedConnection);
39 sortBar->addAction(mostRelevantAction);
40 mostRecentAction = new QAction(tr("Most recent"), this);
41 keySequence = QKeySequence(Qt::CTRL + Qt::Key_2);
42 mostRecentAction->setShortcut(keySequence);
43 mostRecentAction->setStatusTip(mostRecentAction->text() + " (" + keySequence.toString(QKeySequence::NativeText) + ")");
44 addAction(mostRecentAction);
45 connect(mostRecentAction, SIGNAL(triggered()), this, SLOT(searchMostRecent()), Qt::QueuedConnection);
46 sortBar->addAction(mostRecentAction);
47 mostViewedAction = new QAction(tr("Most viewed"), this);
48 keySequence = QKeySequence(Qt::CTRL + Qt::Key_3);
49 mostViewedAction->setShortcut(keySequence);
50 mostViewedAction->setStatusTip(mostViewedAction->text() + " (" + keySequence.toString(QKeySequence::NativeText) + ")");
51 addAction(mostViewedAction);
52 connect(mostViewedAction, SIGNAL(triggered()), this, SLOT(searchMostViewed()), Qt::QueuedConnection);
53 sortBar->addAction(mostViewedAction);
55 listView = new QListView(this);
56 listView->setItemDelegate(new PrettyItemDelegate(this));
57 listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
60 listView->setDragEnabled(true);
61 listView->setAcceptDrops(true);
62 listView->setDropIndicatorShown(true);
63 listView->setDragDropMode(QAbstractItemView::DragDrop);
66 listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
67 listView->setFrameShape( QFrame::NoFrame );
68 listView->setAttribute(Qt::WA_MacShowFocusRect, false);
69 listView->setMinimumSize(320,240);
70 listView->setUniformItemSizes(true);
72 // respond to the user doubleclicking a playlist item
73 connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
75 listModel = new ListModel(this);
76 connect(listModel, SIGNAL(activeRowChanged(int)), this, SLOT(activeRowChanged(int)));
77 // needed to restore the selection after dragndrop
78 connect(listModel, SIGNAL(needSelectionFor(QList<Video*>)), this, SLOT(selectVideos(QList<Video*>)));
79 listView->setModel(listModel);
81 connect(listView->selectionModel(),
82 SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
83 this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
85 playlistWidget = new PlaylistWidget(this, sortBar, listView);
87 splitter->addWidget(playlistWidget);
89 videoAreaWidget = new VideoAreaWidget(this);
90 videoAreaWidget->setMinimumSize(320,240);
93 // mouse autohide does not work on the Mac (no mouseMoveEvent)
94 videoWidget = new Phonon::VideoWidget(this);
96 videoWidget = new VideoWidget(this);
99 videoAreaWidget->setVideoWidget(videoWidget);
100 videoAreaWidget->setListModel(listModel);
102 loadingWidget = new LoadingWidget(this);
103 videoAreaWidget->setLoadingWidget(loadingWidget);
105 splitter->addWidget(videoAreaWidget);
107 layout->addWidget(splitter);
110 // restore splitter state
112 splitter->restoreState(settings.value("splitter").toByteArray());
114 errorTimer = new QTimer(this);
115 errorTimer->setSingleShot(true);
116 errorTimer->setInterval(3000);
117 connect(errorTimer, SIGNAL(timeout()), SLOT(skipVideo()));
119 workaroundTimer = new QTimer(this);
120 workaroundTimer->setSingleShot(true);
121 workaroundTimer->setInterval(3000);
122 connect(workaroundTimer, SIGNAL(timeout()), SLOT(timerPlay()));
125 demoTimer = new QTimer(this);
126 demoTimer->setSingleShot(true);
127 connect(demoTimer, SIGNAL(timeout()), SLOT(demoMessage()));
132 void MediaView::initialize() {
133 connect(videoAreaWidget, SIGNAL(doubleClicked()), The::globalActions()->value("fullscreen"), SLOT(trigger()));
134 videoAreaWidget->setContextMenuPolicy(Qt::CustomContextMenu);
135 connect(videoAreaWidget, SIGNAL(customContextMenuRequested(QPoint)),
136 this, SLOT(showVideoContextMenu(QPoint)));
139 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
140 this->mediaObject = mediaObject;
141 Phonon::createPath(this->mediaObject, videoWidget);
142 connect(mediaObject, SIGNAL(finished()), this, SLOT(playbackFinished()));
143 connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
144 this, SLOT(stateChanged(Phonon::State, Phonon::State)));
145 connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
146 this, SLOT(currentSourceChanged(Phonon::MediaSource)));
147 // connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
150 void MediaView::search(SearchParams *searchParams) {
151 reallyStopped = false;
157 videoAreaWidget->clear();
158 workaroundTimer->stop();
161 mediaObject->pause();
167 this->searchParams = searchParams;
169 // start serching for videos
170 listModel->search(searchParams);
172 // this implies that the enum and the bar action order is the same
173 sortBar->setCheckedAction(searchParams->sortBy()-1);
175 listView->setFocus();
178 QString keyword = searchParams->keywords();
179 QString display = keyword;
180 if (keyword.startsWith("http://")) {
181 int separator = keyword.indexOf("|");
182 if (separator > 0 && separator + 1 < keyword.length()) {
183 display = keyword.mid(separator+1);
187 playlistWidget->hide();
188 } else playlistWidget->show();
189 // tr("You're watching \"%1\"").arg(searchParams->keywords())
193 void MediaView::disappear() {
194 timerPlayFlag = true;
197 void MediaView::handleError(QString message) {
198 // if (message.indexOf("movie atom") != -1 || message.indexOf("Could not open") != -1) {
199 QTimer::singleShot(1000, this, SLOT(startPlaying()));
204 videoAreaWidget->showError(message);
205 skippedVideo = listModel->activeVideo();
206 // recover from errors by skipping to the next video
207 errorTimer->start(2000);
211 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
214 // qDebug() << "Phonon state: " << newState << oldState;
215 // slider->setEnabled(newState == Phonon::PlayingState);
219 case Phonon::ErrorState:
220 qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
221 if (mediaObject->errorType() == Phonon::FatalError)
222 handleError(mediaObject->errorString());
225 case Phonon::PlayingState:
226 // qDebug("playing");
227 videoAreaWidget->showVideo();
230 case Phonon::StoppedState:
231 // qDebug("stopped");
232 // play() has already been called when setting the source
233 // but Phonon on Linux needs a little more help to start playback
234 // if (!reallyStopped) mediaObject->play();
237 // Workaround for Mac playback start problem
238 if (!timerPlayFlag) {
239 // workaroundTimer->start();
245 case Phonon::PausedState:
249 case Phonon::BufferingState:
253 case Phonon::LoadingState:
262 void MediaView::pause() {
263 // qDebug() << "pause() called" << mediaObject->state();
264 switch( mediaObject->state() ) {
265 case Phonon::PlayingState:
266 mediaObject->pause();
274 void MediaView::stop() {
275 listModel->abortSearch();
276 reallyStopped = true;
278 videoAreaWidget->clear();
279 workaroundTimer->stop();
281 listView->selectionModel()->clearSelection();
283 downloadItem->stop();
289 void MediaView::activeRowChanged(int row) {
290 if (reallyStopped) return;
292 Video *video = listModel->videoAt(row);
295 // now that we have a new video to play
296 // stop all the timers
297 workaroundTimer->stop();
300 mediaObject->pause();
302 downloadItem->stop();
306 // slider->setMinimum(0);
308 // immediately show the loading widget
309 videoAreaWidget->showLoading(video);
311 connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
312 // TODO handle signal in a proper slot and impl item error status
313 connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
315 video->loadStreamUrl();
317 // reset the timer flag
318 timerPlayFlag = false;
320 // video title in the statusbar
321 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
322 if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
324 The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
326 // see you in gotStreamUrl...
330 void MediaView::gotStreamUrl(QUrl streamUrl) {
331 if (reallyStopped) return;
333 Video *video = static_cast<Video *>(sender());
335 qDebug() << "Cannot get sender";
338 video->disconnect(this);
341 QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
343 QString tempFile = tempDir + "/minitube-" + getenv("USERNAME") + ".mp4";
345 QString tempFile = tempDir + "/minitube.mp4";
347 if (QFile::exists(tempFile) && !QFile::remove(tempFile)) {
348 qDebug() << "Cannot remove temp file";
351 Video *videoCopy = video->clone();
353 downloadItem->stop();
356 downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
357 connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
358 // connect(downloadItem, SIGNAL(progress(int)), SLOT(downloadProgress(int)));
359 connect(downloadItem, SIGNAL(bufferProgress(int)), loadingWidget, SLOT(bufferStatus(int)));
360 // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
361 connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
362 connect(downloadItem, SIGNAL(error(QString)), SLOT(handleError(QString)));
363 downloadItem->start();
368 void MediaView::downloadProgress(int percent) {
369 MainWindow* mainWindow = dynamic_cast<MainWindow*>(window());
371 mainWindow->getSeekSlider()->setStyleSheet(" QSlider::groove:horizontal {"
372 "border: 1px solid #999999;"
373 // "border-left: 50px solid rgba(255, 0, 0, 128);"
375 "background: qlineargradient(x1:0, y1:0, x2:.5, y2:0, stop:0 rgba(255, 0, 0, 92), stop:"
376 + QString::number(percent/100.0) +
378 " rgba(255, 0, 0, 92), stop:" + QString::number((percent+1)/100.0) + " transparent, stop:1 transparent);"
381 "QSlider::handle:horizontal {"
382 "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);"
383 "border: 1px solid #5c5c5c;"
387 "border-radius: 8px;"
395 void MediaView::downloadStatusChanged() {
396 switch(downloadItem->status()) {
401 // qDebug() << "Starting";
404 // qDebug() << "Finished" << mediaObject->state();
405 // if (mediaObject->state() == Phonon::StoppedState) startPlaying();
408 // qDebug() << "Failed";
410 // qDebug() << "Idle";
415 void MediaView::startPlaying() {
416 if (reallyStopped) return;
423 qDebug() << "Playing" << downloadItem->currentFilename();
424 mediaObject->setCurrentSource(downloadItem->currentFilename());
427 // ensure we always have 10 videos ahead
428 listModel->searchNeeded();
430 // ensure active item is visible
431 int row = listModel->activeRow();
433 QModelIndex index = listModel->index(row, 0, QModelIndex());
434 listView->scrollTo(index, QAbstractItemView::EnsureVisible);
438 demoTimer->start(30000);
443 void MediaView::itemActivated(const QModelIndex &index) {
444 if (listModel->rowExists(index.row()))
445 listModel->setActiveRow(index.row());
446 // the user doubleclicked on the "Search More" item
447 else listModel->searchMore();
450 void MediaView::currentSourceChanged(const Phonon::MediaSource /* source */ ) {
454 void MediaView::skipVideo() {
455 // skippedVideo is useful for DELAYED skip operations
456 // in order to be sure that we're skipping the video we wanted
457 // and not another one
459 if (listModel->activeVideo() != skippedVideo) {
460 qDebug() << "Skip of video canceled";
463 int nextRow = listModel->rowForVideo(skippedVideo);
465 if (nextRow == -1) return;
466 listModel->setActiveRow(nextRow);
470 void MediaView::skip() {
471 int nextRow = listModel->nextRow();
472 if (nextRow == -1) return;
473 listModel->setActiveRow(nextRow);
476 void MediaView::playbackFinished() {
477 // qDebug() << "finished" << mediaObject->currentTime() << mediaObject->totalTime();
478 // add 10 secs for imprecise Phonon backends (VLC, Xine)
479 if (mediaObject->currentTime() + 10000 < mediaObject->totalTime()) {
480 // mediaObject->seek(mediaObject->currentTime());
481 QTimer::singleShot(3000, this, SLOT(playbackResume()));
485 void MediaView::playbackResume() {
486 mediaObject->seek(mediaObject->currentTime());
490 void MediaView::openWebPage() {
491 Video* video = listModel->activeVideo();
493 mediaObject->pause();
494 QDesktopServices::openUrl(video->webpage());
497 void MediaView::copyWebPage() {
498 Video* video = listModel->activeVideo();
500 QString address = video->webpage().toString();
501 address.remove("&feature=youtube_gdata");
502 QApplication::clipboard()->setText(address);
503 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
504 QString message = tr("You can now paste the YouTube link into another application");
505 if (mainWindow) mainWindow->statusBar()->showMessage(message);
508 void MediaView::copyVideoLink() {
509 Video* video = listModel->activeVideo();
511 QApplication::clipboard()->setText(video->getStreamUrl().toEncoded());
512 QString message = tr("You can now paste the video stream URL into another application")
513 + ". " + tr("The link will be valid only for a limited time.");
514 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
515 if (mainWindow) mainWindow->statusBar()->showMessage(message);
518 void MediaView::removeSelected() {
519 if (!listView->selectionModel()->hasSelection()) return;
520 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
521 listModel->removeIndexes(indexes);
524 void MediaView::selectVideos(QList<Video*> videos) {
525 foreach (Video *video, videos) {
526 QModelIndex index = listModel->indexForVideo(video);
527 listView->selectionModel()->select(index, QItemSelectionModel::Select);
528 listView->scrollTo(index, QAbstractItemView::EnsureVisible);
532 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
533 const bool gotSelection = listView->selectionModel()->hasSelection();
534 The::globalActions()->value("remove")->setEnabled(gotSelection);
535 The::globalActions()->value("moveUp")->setEnabled(gotSelection);
536 The::globalActions()->value("moveDown")->setEnabled(gotSelection);
539 void MediaView::moveUpSelected() {
540 if (!listView->selectionModel()->hasSelection()) return;
542 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
543 qStableSort(indexes.begin(), indexes.end());
544 listModel->move(indexes, true);
546 // set current index after row moves to something more intuitive
547 int row = indexes.first().row();
548 listView->selectionModel()->setCurrentIndex(listModel->index(row>1?row:1), QItemSelectionModel::NoUpdate);
551 void MediaView::moveDownSelected() {
552 if (!listView->selectionModel()->hasSelection()) return;
554 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
555 qStableSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
556 listModel->move(indexes, false);
558 // set current index after row moves to something more intuitive (respect 1 static item on bottom)
559 int row = indexes.first().row()+1, max = listModel->rowCount() - 2;
560 listView->selectionModel()->setCurrentIndex(listModel->index(row>max?max:row), QItemSelectionModel::NoUpdate);
563 void MediaView::showVideoContextMenu(QPoint point) {
564 The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
567 void MediaView::searchMostRelevant() {
568 searchParams->setSortBy(SearchParams::SortByRelevance);
569 search(searchParams);
572 void MediaView::searchMostRecent() {
573 searchParams->setSortBy(SearchParams::SortByNewest);
574 search(searchParams);
577 void MediaView::searchMostViewed() {
578 searchParams->setSortBy(SearchParams::SortByViewCount);
579 search(searchParams);
582 void MediaView::setPlaylistVisible(bool visible) {
583 playlistWidget->setVisible(visible);
586 void MediaView::timerPlay() {
587 // Workaround Phonon bug on Mac OSX
588 // qDebug() << mediaObject->currentTime();
589 if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
590 // qDebug() << "Mac playback workaround";
591 mediaObject->pause();
592 // QTimer::singleShot(1000, mediaObject, SLOT(play()));
597 void MediaView::saveSplitterState() {
599 settings.setValue("splitter", splitter->saveState());
603 void MediaView::demoMessage() {
604 if (mediaObject->state() != Phonon::PlayingState) return;
605 mediaObject->pause();
607 QMessageBox msgBox(this);
608 msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
609 msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::APP_NAME));
610 msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
611 msgBox.setModal(true);
612 // make it a "sheet" on the Mac
613 msgBox.setWindowModality(Qt::WindowModal);
615 QPushButton *quitButton = msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
616 QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
620 if (msgBox.clickedButton() == buyButton) {
621 QDesktopServices::openUrl(QString(Constants::WEBSITE) + "#download");
624 demoTimer->start(300000);
629 void MediaView::downloadVideo() {
630 Video* video = listModel->activeVideo();
633 DownloadManager::instance()->addItem(video);
637 The::globalActions()->value("downloads")->setVisible(true);
639 // The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
641 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
642 QString message = tr("Downloading %1").arg(video->title());
643 if (mainWindow) mainWindow->statusBar()->showMessage(message);
646 void MediaView::fullscreen() {
647 videoAreaWidget->setParent(0);
648 videoAreaWidget->showFullScreen();
652 void MediaView::setSlider(QSlider *slider) {
653 this->slider = slider;
654 // slider->setEnabled(false);
655 slider->setTracking(false);
656 // connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
659 void MediaView::sliderMoved(int value) {
660 qDebug() << __func__;
661 int sliderPercent = (value * 100) / (slider->maximum() - slider->minimum());
662 qDebug() << slider->minimum() << value << slider->maximum();
663 if (sliderPercent <= downloadItem->currentPercent()) {
664 qDebug() << sliderPercent << downloadItem->currentPercent();
665 mediaObject->seek(value);
671 void MediaView::seekTo(int value) {
672 qDebug() << __func__;
673 mediaObject->pause();
674 workaroundTimer->stop();
676 // mediaObject->clear();
678 QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
679 QString tempFile = tempDir + "/minitube" + QString::number(value) + ".mp4";
680 if (!QFile::remove(tempFile)) {
681 qDebug() << "Cannot remove temp file";
683 Video *videoCopy = downloadItem->getVideo()->clone();
684 QUrl streamUrl = videoCopy->getStreamUrl();
685 streamUrl.addQueryItem("begin", QString::number(value));
686 if (downloadItem) delete downloadItem;
687 downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
688 connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
689 // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
690 downloadItem->start();
692 // slider->setMinimum(value);