3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "mediaview.h"
22 #include "playlistmodel.h"
23 #include "playlistview.h"
24 #include "loadingwidget.h"
25 #include "videoareawidget.h"
26 #include "networkaccess.h"
27 #include "minisplitter.h"
28 #include "constants.h"
29 #include "downloadmanager.h"
30 #include "downloaditem.h"
31 #include "mainwindow.h"
32 #include "temporary.h"
33 #include "refinesearchwidget.h"
34 #include "sidebarwidget.h"
35 #include "sidebarheader.h"
37 #include "activation.h"
42 #include "videosource.h"
44 #include "searchparams.h"
45 #include "ytsinglevideosource.h"
46 #include "channelaggregator.h"
47 #include "iconutils.h"
48 #include "ytchannel.h"
50 #include "snapshotsettings.h"
52 #include "datautils.h"
53 #include "compatibility/qurlqueryhelper.h"
56 NetworkAccess* http();
57 QHash<QString, QAction*>* globalActions();
58 QHash<QString, QMenu*>* globalMenus();
59 QNetworkAccessManager* networkAccessManager();
62 MediaView* MediaView::instance() {
63 static MediaView *i = new MediaView();
67 MediaView::MediaView(QWidget *parent) : QWidget(parent)
75 void MediaView::initialize() {
76 QBoxLayout *layout = new QVBoxLayout(this);
79 splitter = new MiniSplitter();
81 playlistView = new PlaylistView(this);
82 // respond to the user doubleclicking a playlist item
83 connect(playlistView, SIGNAL(activated(const QModelIndex &)),
84 SLOT(itemActivated(const QModelIndex &)));
86 playlistModel = new PlaylistModel();
87 connect(playlistModel, SIGNAL(activeRowChanged(int)),
88 SLOT(activeRowChanged(int)));
89 // needed to restore the selection after dragndrop
90 connect(playlistModel, SIGNAL(needSelectionFor(QList<Video*>)),
91 SLOT(selectVideos(QList<Video*>)));
92 playlistView->setModel(playlistModel);
94 connect(playlistView->selectionModel(),
95 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
96 SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
98 connect(playlistView, SIGNAL(authorPushed(QModelIndex)), SLOT(authorPushed(QModelIndex)));
100 sidebar = new SidebarWidget(this);
101 sidebar->setPlaylist(playlistView);
102 connect(sidebar->getRefineSearchWidget(), SIGNAL(searchRefined()),
103 SLOT(searchAgain()));
104 connect(playlistModel, SIGNAL(haveSuggestions(const QStringList &)),
105 sidebar, SLOT(showSuggestions(const QStringList &)));
106 connect(sidebar, SIGNAL(suggestionAccepted(QString)),
107 MainWindow::instance(), SLOT(search(QString)));
108 splitter->addWidget(sidebar);
110 videoAreaWidget = new VideoAreaWidget(this);
111 // videoAreaWidget->setMinimumSize(320,240);
114 videoWidget = new Phonon::VideoWidget(this);
115 videoAreaWidget->setVideoWidget(videoWidget);
117 videoAreaWidget->setListModel(playlistModel);
119 loadingWidget = new LoadingWidget(this);
120 videoAreaWidget->setLoadingWidget(loadingWidget);
122 splitter->addWidget(videoAreaWidget);
124 splitter->setStretchFactor(0, 0);
125 splitter->setStretchFactor(1, 8);
127 // restore splitter state
129 splitter->restoreState(settings.value("splitter").toByteArray());
130 splitter->setChildrenCollapsible(false);
132 layout->addWidget(splitter);
134 errorTimer = new QTimer(this);
135 errorTimer->setSingleShot(true);
136 errorTimer->setInterval(3000);
137 connect(errorTimer, SIGNAL(timeout()), SLOT(skipVideo()));
139 #ifdef APP_ACTIVATION
140 demoTimer = new QTimer(this);
141 demoTimer->setSingleShot(true);
142 connect(demoTimer, SIGNAL(timeout()), SLOT(demoMessage()));
145 connect(videoAreaWidget, SIGNAL(doubleClicked()),
146 The::globalActions()->value("fullscreen"), SLOT(trigger()));
148 QAction* refineSearchAction = The::globalActions()->value("refine-search");
149 connect(refineSearchAction, SIGNAL(toggled(bool)),
150 sidebar, SLOT(toggleRefineSearch(bool)));
153 << The::globalActions()->value("webpage")
154 << The::globalActions()->value("pagelink")
155 << The::globalActions()->value("videolink")
156 << The::globalActions()->value("open-in-browser")
158 << The::globalActions()->value("snapshot")
160 << The::globalActions()->value("findVideoParts")
161 << The::globalActions()->value("skip")
162 << The::globalActions()->value("previous")
163 << The::globalActions()->value("stopafterthis")
164 << The::globalActions()->value("related-videos")
165 << The::globalActions()->value("refine-search")
166 << The::globalActions()->value("twitter")
167 << The::globalActions()->value("facebook")
168 << The::globalActions()->value("buffer")
169 << The::globalActions()->value("email");
171 #ifndef APP_PHONON_SEEK
172 QSlider *slider = MainWindow::instance()->getSlider();
173 connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
178 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
179 this->mediaObject = mediaObject;
180 Phonon::createPath(mediaObject, videoWidget);
181 connect(mediaObject, SIGNAL(finished()), SLOT(playbackFinished()));
182 connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
183 SLOT(stateChanged(Phonon::State, Phonon::State)));
184 connect(mediaObject, SIGNAL(aboutToFinish()), SLOT(aboutToFinish()));
188 SearchParams* MediaView::getSearchParams() {
189 VideoSource *videoSource = playlistModel->getVideoSource();
190 if (videoSource && videoSource->metaObject()->className() == QLatin1String("YTSearch")) {
191 YTSearch *search = dynamic_cast<YTSearch *>(videoSource);
192 return search->getSearchParams();
197 void MediaView::search(SearchParams *searchParams) {
198 if (!searchParams->keywords().isEmpty()) {
199 if (searchParams->keywords().startsWith("http://") ||
200 searchParams->keywords().startsWith("https://")) {
201 QString videoId = YTSearch::videoIdFromUrl(searchParams->keywords());
202 if (!videoId.isEmpty()) {
203 YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource(this);
204 singleVideoSource->setVideoId(videoId);
205 setVideoSource(singleVideoSource);
210 YTSearch *ytSearch = new YTSearch(searchParams, this);
211 ytSearch->setAsyncDetails(true);
212 connect(ytSearch, SIGNAL(gotDetails()), playlistModel, SLOT(emitDataChanged()));
213 setVideoSource(ytSearch);
216 void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool back) {
220 #ifdef APP_ACTIVATION
225 // qDebug() << "Adding VideoSource" << videoSource->getName() << videoSource;
228 int currentIndex = getHistoryIndex();
229 if (currentIndex >= 0 && currentIndex < history.size() - 1) {
230 while (history.size() > currentIndex + 1) {
231 VideoSource *vs = history.takeLast();
233 qDebug() << "Deleting VideoSource" << vs->getName() << vs;
238 history.append(videoSource);
242 if (history.size() > 1)
243 Extra::slideTransition(playlistView->viewport(), playlistView->viewport(), back);
246 playlistModel->setVideoSource(videoSource);
248 sidebar->showPlaylist();
249 sidebar->getRefineSearchWidget()->setSearchParams(getSearchParams());
250 sidebar->hideSuggestions();
251 sidebar->getHeader()->updateInfo();
253 SearchParams *searchParams = getSearchParams();
254 bool isChannel = searchParams && !searchParams->channelId().isEmpty();
255 playlistView->setClickableAuthors(!isChannel);
260 void MediaView::searchAgain() {
261 VideoSource *currentVideoSource = playlistModel->getVideoSource();
262 setVideoSource(currentVideoSource, false);
265 bool MediaView::canGoBack() {
266 return getHistoryIndex() > 0;
269 void MediaView::goBack() {
270 if (history.size() > 1) {
271 int currentIndex = getHistoryIndex();
272 if (currentIndex > 0) {
273 VideoSource *previousVideoSource = history.at(currentIndex - 1);
274 setVideoSource(previousVideoSource, false, true);
279 bool MediaView::canGoForward() {
280 int currentIndex = getHistoryIndex();
281 return currentIndex >= 0 && currentIndex < history.size() - 1;
284 void MediaView::goForward() {
285 if (canGoForward()) {
286 int currentIndex = getHistoryIndex();
287 VideoSource *nextVideoSource = history.at(currentIndex + 1);
288 setVideoSource(nextVideoSource, false);
292 int MediaView::getHistoryIndex() {
293 return history.lastIndexOf(playlistModel->getVideoSource());
296 void MediaView::appear() {
297 playlistView->setFocus();
298 Video *currentVideo = playlistModel->activeVideo();
300 MainWindow::instance()->setWindowTitle(
301 currentVideo->title() + " - " + Constants::NAME);
302 MainWindow::instance()->showMessage(currentVideo->description());
306 void MediaView::disappear() {
310 void MediaView::handleError(QString message) {
311 qWarning() << __PRETTY_FUNCTION__ << message;
312 #ifdef APP_PHONON_SEEK
315 QTimer::singleShot(500, this, SLOT(startPlaying()));
320 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
321 if (newState == Phonon::PlayingState)
322 videoAreaWidget->showVideo();
323 else if (newState == Phonon::ErrorState) {
324 qWarning() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
325 if (mediaObject->errorType() == Phonon::FatalError)
326 handleError(mediaObject->errorString());
331 void MediaView::pause() {
333 switch( mediaObject->state() ) {
334 case Phonon::PlayingState:
335 mediaObject->pause();
344 QRegExp MediaView::wordRE(QString s) {
345 return QRegExp("\\W" + s + "\\W?", Qt::CaseInsensitive);
348 void MediaView::stop() {
351 while (!history.isEmpty()) {
352 VideoSource *videoSource = history.takeFirst();
353 if (!videoSource->parent()) delete videoSource;
356 playlistModel->abortSearch();
357 videoAreaWidget->clear();
358 videoAreaWidget->update();
360 playlistView->selectionModel()->clearSelection();
362 downloadItem->stop();
365 currentVideoSize = 0;
367 The::globalActions()->value("refine-search")->setChecked(false);
368 updateSubscriptionAction(0, false);
369 #ifdef APP_ACTIVATION
373 foreach (QAction *action, currentVideoActions)
374 action->setEnabled(false);
376 QAction *a = The::globalActions()->value("download");
377 a->setEnabled(false);
378 a->setVisible(false);
383 currentVideoId.clear();
385 #ifndef APP_PHONON_SEEK
386 QSlider *slider = MainWindow::instance()->getSlider();
387 slider->setEnabled(false);
391 if (snapshotSettings) {
392 delete snapshotSettings;
393 snapshotSettings = 0;
397 const QString & MediaView::getCurrentVideoId() {
398 return currentVideoId;
401 void MediaView::activeRowChanged(int row) {
410 downloadItem->stop();
413 currentVideoSize = 0;
416 Video *video = playlistModel->videoAt(row);
419 videoAreaWidget->showLoading(video);
421 connect(video, SIGNAL(gotStreamUrl(QUrl)),
422 SLOT(gotStreamUrl(QUrl)), Qt::UniqueConnection);
423 connect(video, SIGNAL(errorStreamUrl(QString)),
424 SLOT(skip()), Qt::UniqueConnection);
425 video->loadStreamUrl();
427 // video title in titlebar
428 MainWindow::instance()->setWindowTitle(video->title() + " - " + Constants::NAME);
429 MainWindow::instance()->showMessage(video->description());
431 // ensure active item is visible
433 QModelIndex index = playlistModel->index(row, 0, QModelIndex());
434 playlistView->scrollTo(index, QAbstractItemView::EnsureVisible);
437 // enable/disable actions
438 The::globalActions()->value("download")->setEnabled(
439 DownloadManager::instance()->itemForVideo(video) == 0);
440 The::globalActions()->value("previous")->setEnabled(row > 0);
441 The::globalActions()->value("stopafterthis")->setEnabled(true);
442 The::globalActions()->value("related-videos")->setEnabled(true);
444 bool enableDownload = video->license() == Video::LicenseCC;
445 #ifdef APP_ACTIVATION
446 enableDownload = enableDownload || Activation::instance().isLegacy();
449 enableDownload = true;
451 QAction *a = The::globalActions()->value("download");
452 a->setEnabled(enableDownload);
453 a->setVisible(enableDownload);
455 updateSubscriptionAction(video, YTChannel::isSubscribed(video->channelId()));
457 foreach (QAction *action, currentVideoActions)
458 action->setEnabled(true);
460 #ifndef APP_PHONON_SEEK
461 QSlider *slider = MainWindow::instance()->getSlider();
462 slider->setEnabled(false);
466 if (snapshotSettings) {
467 delete snapshotSettings;
468 snapshotSettings = 0;
471 // see you in gotStreamUrl...
474 void MediaView::gotStreamUrl(QUrl streamUrl) {
476 if (!streamUrl.isValid()) {
481 Video *video = static_cast<Video *>(sender());
483 qDebug() << "Cannot get sender in" << __PRETTY_FUNCTION__;
486 video->disconnect(this);
488 currentVideoId = video->id();
490 #ifdef APP_PHONON_SEEK
491 mediaObject->setCurrentSource(streamUrl);
497 // ensure we always have videos ahead
498 playlistModel->searchNeeded();
500 // ensure active item is visible
501 int row = playlistModel->activeRow();
503 QModelIndex index = playlistModel->index(row, 0, QModelIndex());
504 playlistView->scrollTo(index, QAbstractItemView::EnsureVisible);
507 #ifdef APP_ACTIVATION
508 if (!Activation::instance().isActivated())
509 demoTimer->start(180000);
513 Extra::notify(video->title(), video->channelTitle(), video->formattedDuration());
516 ChannelAggregator::instance()->videoWatched(video);
519 void MediaView::downloadStatusChanged() {
520 // qDebug() << __PRETTY_FUNCTION__;
521 switch(downloadItem->status()) {
523 // qDebug() << "Downloading";
524 if (downloadItem->offset() == 0) startPlaying();
527 // qDebug() << "Seeking to" << downloadItem->offset();
528 mediaObject->seek(offsetToTime(downloadItem->offset()));
534 // qDebug() << "Starting";
537 // qDebug() << "Finished" << mediaObject->state();
538 #ifdef APP_PHONON_SEEK
539 MainWindow::instance()->getSeekSlider()->setEnabled(mediaObject->isSeekable());
543 // qDebug() << "Failed";
547 // qDebug() << "Idle";
552 void MediaView::startPlaying() {
553 // qDebug() << __PRETTY_FUNCTION__;
560 if (downloadItem->offset() == 0) {
561 currentVideoSize = downloadItem->bytesTotal();
562 // qDebug() << "currentVideoSize" << currentVideoSize;
566 QString source = downloadItem->currentFilename();
567 qDebug() << "Playing" << source << QFile::exists(source);
569 mediaObject->setCurrentSource(QUrl::fromLocalFile(source));
572 #ifdef APP_PHONON_SEEK
573 MainWindow::instance()->getSeekSlider()->setEnabled(false);
575 QSlider *slider = MainWindow::instance()->getSlider();
576 slider->setEnabled(true);
580 void MediaView::itemActivated(const QModelIndex &index) {
581 if (playlistModel->rowExists(index.row())) {
583 // if it's the current video, just rewind and play
584 Video *activeVideo = playlistModel->activeVideo();
585 Video *video = playlistModel->videoAt(index.row());
586 if (activeVideo && video && activeVideo == video) {
587 // mediaObject->seek(0);
592 } else playlistModel->setActiveRow(index.row());
594 // the user doubleclicked on the "Search More" item
596 playlistModel->searchMore();
597 playlistView->selectionModel()->clearSelection();
601 void MediaView::skipVideo() {
602 // skippedVideo is useful for DELAYED skip operations
603 // in order to be sure that we're skipping the video we wanted
604 // and not another one
606 if (playlistModel->activeVideo() != skippedVideo) {
607 qDebug() << "Skip of video canceled";
610 int nextRow = playlistModel->rowForVideo(skippedVideo);
612 if (nextRow == -1) return;
613 playlistModel->setActiveRow(nextRow);
617 void MediaView::skip() {
618 int nextRow = playlistModel->nextRow();
619 if (nextRow == -1) return;
620 playlistModel->setActiveRow(nextRow);
623 void MediaView::skipBackward() {
624 int prevRow = playlistModel->previousRow();
625 if (prevRow == -1) return;
626 playlistModel->setActiveRow(prevRow);
629 void MediaView::aboutToFinish() {
631 qint64 currentTime = mediaObject->currentTime();
632 qint64 totalTime = mediaObject->totalTime();
633 qDebug() << __PRETTY_FUNCTION__ << currentTime << totalTime;
634 if (totalTime < 1 || currentTime + 10000 < totalTime) {
635 // QTimer::singleShot(500, this, SLOT(playbackResume()));
636 mediaObject->seek(currentTime);
642 void MediaView::playbackFinished() {
646 const qint64 totalTime = mediaObject->totalTime();
647 const qint64 currentTime = mediaObject->currentTime();
648 qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime() << totalTime;
649 // add 10 secs for imprecise Phonon backends (VLC, Xine)
650 if (totalTime < 1 || (currentTime > 0 && currentTime + 10000 < totalTime)) {
651 // mediaObject->seek(currentTime);
652 QTimer::singleShot(500, this, SLOT(playbackResume()));
654 QAction* stopAfterThisAction = The::globalActions()->value("stopafterthis");
655 if (stopAfterThisAction->isChecked()) {
656 stopAfterThisAction->setChecked(false);
662 void MediaView::playbackResume() {
665 const qint64 currentTime = mediaObject->currentTime();
666 qDebug() << __PRETTY_FUNCTION__ << currentTime;
668 mediaObject->seek(currentTime);
673 void MediaView::openWebPage() {
674 Video* video = playlistModel->activeVideo();
677 mediaObject->pause();
679 QDesktopServices::openUrl(video->webpage());
682 void MediaView::copyWebPage() {
683 Video* video = playlistModel->activeVideo();
685 QString address = video->webpage();
686 QApplication::clipboard()->setText(address);
687 QString message = tr("You can now paste the YouTube link into another application");
688 MainWindow::instance()->showMessage(message);
691 void MediaView::copyVideoLink() {
692 Video* video = playlistModel->activeVideo();
694 QApplication::clipboard()->setText(video->getStreamUrl().toEncoded());
695 QString message = tr("You can now paste the video stream URL into another application")
696 + ". " + tr("The link will be valid only for a limited time.");
697 MainWindow::instance()->showMessage(message);
700 void MediaView::openInBrowser() {
701 Video* video = playlistModel->activeVideo();
704 mediaObject->pause();
706 QDesktopServices::openUrl(video->getStreamUrl());
709 void MediaView::removeSelected() {
710 if (!playlistView->selectionModel()->hasSelection()) return;
711 QModelIndexList indexes = playlistView->selectionModel()->selectedIndexes();
712 playlistModel->removeIndexes(indexes);
715 void MediaView::selectVideos(QList<Video*> videos) {
716 foreach (Video *video, videos) {
717 QModelIndex index = playlistModel->indexForVideo(video);
718 playlistView->selectionModel()->select(index, QItemSelectionModel::Select);
719 playlistView->scrollTo(index, QAbstractItemView::EnsureVisible);
723 void MediaView::selectionChanged(const QItemSelection & /*selected*/,
724 const QItemSelection & /*deselected*/) {
725 const bool gotSelection = playlistView->selectionModel()->hasSelection();
726 The::globalActions()->value("remove")->setEnabled(gotSelection);
727 The::globalActions()->value("moveUp")->setEnabled(gotSelection);
728 The::globalActions()->value("moveDown")->setEnabled(gotSelection);
731 void MediaView::moveUpSelected() {
732 if (!playlistView->selectionModel()->hasSelection()) return;
734 QModelIndexList indexes = playlistView->selectionModel()->selectedIndexes();
735 qStableSort(indexes.begin(), indexes.end());
736 playlistModel->move(indexes, true);
738 // set current index after row moves to something more intuitive
739 int row = indexes.first().row();
740 playlistView->selectionModel()->setCurrentIndex(playlistModel->index(row>1?row:1),
741 QItemSelectionModel::NoUpdate);
744 void MediaView::moveDownSelected() {
745 if (!playlistView->selectionModel()->hasSelection()) return;
747 QModelIndexList indexes = playlistView->selectionModel()->selectedIndexes();
748 qStableSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
749 playlistModel->move(indexes, false);
751 // set current index after row moves to something more intuitive
752 // (respect 1 static item on bottom)
753 int row = indexes.first().row()+1, max = playlistModel->rowCount() - 2;
754 playlistView->selectionModel()->setCurrentIndex(
755 playlistModel->index(row>max?max:row), QItemSelectionModel::NoUpdate);
758 void MediaView::setPlaylistVisible(bool visible) {
759 if (splitter->widget(0)->isVisible() == visible) return;
760 splitter->widget(0)->setVisible(visible);
761 playlistView->setFocus();
764 bool MediaView::isPlaylistVisible() {
765 return splitter->widget(0)->isVisible();
768 void MediaView::saveSplitterState() {
770 settings.setValue("splitter", splitter->saveState());
773 #ifdef APP_ACTIVATION
775 static QPushButton *continueButton;
777 void MediaView::demoMessage() {
779 if (mediaObject->state() != Phonon::PlayingState) return;
780 mediaObject->pause();
783 QMessageBox msgBox(this);
784 msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
785 msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::NAME));
786 msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
787 msgBox.setModal(true);
788 // make it a "sheet" on the Mac
789 msgBox.setWindowModality(Qt::WindowModal);
791 continueButton = msgBox.addButton("5", QMessageBox::RejectRole);
792 continueButton->setEnabled(false);
793 QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
795 QTimeLine *timeLine = new QTimeLine(6000, this);
796 timeLine->setCurveShape(QTimeLine::LinearCurve);
797 timeLine->setFrameRange(5, 0);
798 connect(timeLine, SIGNAL(frameChanged(int)), SLOT(updateContinueButton(int)));
803 if (msgBox.clickedButton() == buyButton) {
804 MainWindow::instance()->showActivationView();
809 demoTimer->start(600000);
816 void MediaView::updateContinueButton(int value) {
818 continueButton->setText(tr("Continue"));
819 continueButton->setEnabled(true);
821 continueButton->setText(QString::number(value));
827 void MediaView::downloadVideo() {
828 Video* video = playlistModel->activeVideo();
830 DownloadManager::instance()->addItem(video);
831 The::globalActions()->value("downloads")->setVisible(true);
832 QString message = tr("Downloading %1").arg(video->title());
833 MainWindow::instance()->showMessage(message);
837 void MediaView::snapshot() {
838 qint64 currentTime = mediaObject->currentTime() / 1000;
840 QImage image = videoWidget->snapshot();
841 if (image.isNull()) {
842 qWarning() << "Null snapshot";
846 // QPixmap pixmap = QPixmap::grabWindow(videoWidget->winId());
847 QPixmap pixmap = QPixmap::fromImage(image.scaled(videoWidget->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
848 videoAreaWidget->showSnapshotPreview(pixmap);
850 Video* video = playlistModel->activeVideo();
853 QString location = SnapshotSettings::getCurrentLocation();
855 if (!dir.exists()) dir.mkpath(location);
856 QString basename = video->title();
857 QString format = video->duration() > 3600 ? "h_mm_ss" : "m_ss";
858 basename += " (" + QTime().addSecs(currentTime).toString(format) + ")";
859 basename = DataUtils::stringToFilename(basename);
860 QString filename = location + "/" + basename + ".png";
861 qDebug() << filename;
862 image.save(filename, "PNG");
864 if (snapshotSettings) delete snapshotSettings;
865 snapshotSettings = new SnapshotSettings(videoWidget);
866 snapshotSettings->setSnapshot(pixmap, filename);
867 QStatusBar *statusBar = MainWindow::instance()->statusBar();
869 Extra::fadeInWidget(statusBar, statusBar);
871 statusBar->clearMessage();
872 statusBar->insertPermanentWidget(0, snapshotSettings);
873 snapshotSettings->show();
877 void MediaView::fullscreen() {
878 videoAreaWidget->setParent(0);
879 videoAreaWidget->showFullScreen();
882 void MediaView::startDownloading() {
883 Video *video = playlistModel->activeVideo();
885 Video *videoCopy = video->clone();
887 downloadItem->stop();
890 QString tempFile = Temporary::filename();
891 downloadItem = new DownloadItem(videoCopy, video->getStreamUrl(), tempFile, this);
892 connect(downloadItem, SIGNAL(statusChanged()),
893 SLOT(downloadStatusChanged()), Qt::UniqueConnection);
894 connect(downloadItem, SIGNAL(bufferProgress(int)),
895 loadingWidget, SLOT(bufferStatus(int)), Qt::UniqueConnection);
896 // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
897 connect(video, SIGNAL(errorStreamUrl(QString)),
898 SLOT(handleError(QString)), Qt::UniqueConnection);
899 connect(downloadItem, SIGNAL(error(QString)),
900 SLOT(handleError(QString)), Qt::UniqueConnection);
901 downloadItem->start();
904 void MediaView::sliderMoved(int value) {
907 #ifndef APP_PHONON_SEEK
909 if (currentVideoSize <= 0 || !downloadItem || !mediaObject->isSeekable())
912 QSlider *slider = MainWindow::instance()->getSlider();
913 if (slider->isSliderDown()) return;
915 qint64 offset = (currentVideoSize * value) / slider->maximum();
917 bool needsDownload = downloadItem->needsDownload(offset);
919 if (downloadItem->isBuffered(offset)) {
920 qint64 realOffset = downloadItem->blankAtOffset(offset);
921 if (offset < currentVideoSize)
922 downloadItem->seekTo(realOffset, false);
923 mediaObject->seek(offsetToTime(offset));
925 mediaObject->pause();
926 downloadItem->seekTo(offset);
929 // qDebug() << "simple seek";
930 mediaObject->seek(offsetToTime(offset));
936 qint64 MediaView::offsetToTime(qint64 offset) {
938 const qint64 totalTime = mediaObject->totalTime();
939 return ((offset * totalTime) / currentVideoSize);
943 void MediaView::findVideoParts() {
946 Video* video = playlistModel->activeVideo();
949 QString query = video->title();
951 static QString optionalSpace = "\\s*";
952 static QString staticCounterSeparators = "[\\/\\-]";
953 QString counterSeparators = "( of | " +
954 tr("of", "Used in video parts, as in '2 of 3'") +
955 " |" + staticCounterSeparators + ")";
957 // numbers from 1 to 15
958 static QString counterNumber = "([1-9]|1[0-5])";
960 // query.remove(QRegExp(counterSeparators + optionalSpace + counterNumber));
961 query.remove(QRegExp(counterNumber + optionalSpace +
962 counterSeparators + optionalSpace + counterNumber));
963 query.remove(wordRE("pr?t\\.?" + optionalSpace + counterNumber));
964 query.remove(wordRE("ep\\.?" + optionalSpace + counterNumber));
965 query.remove(wordRE("part" + optionalSpace + counterNumber));
966 query.remove(wordRE("episode" + optionalSpace + counterNumber));
967 query.remove(wordRE(tr("part", "This is for video parts, as in 'Cool video - part 1'") +
968 optionalSpace + counterNumber));
969 query.remove(wordRE(tr("episode",
970 "This is for video parts, as in 'Cool series - episode 1'") +
971 optionalSpace + counterNumber));
972 query.remove(QRegExp("[\\(\\)\\[\\]]"));
974 #define NUMBERS "one|two|three|four|five|six|seven|eight|nine|ten"
976 QRegExp englishNumberRE = QRegExp(QLatin1String(".*(") + NUMBERS + ").*",
977 Qt::CaseInsensitive);
978 // bool numberAsWords = englishNumberRE.exactMatch(query);
979 query.remove(englishNumberRE);
981 QRegExp localizedNumberRE = QRegExp(QLatin1String(".*(") + tr(NUMBERS) + ").*",
982 Qt::CaseInsensitive);
983 // if (!numberAsWords) numberAsWords = localizedNumberRE.exactMatch(query);
984 query.remove(localizedNumberRE);
986 SearchParams *searchParams = new SearchParams();
987 searchParams->setTransient(true);
988 searchParams->setKeywords(query);
989 searchParams->setChannelId(video->channelId());
992 if (!numberAsWords) {
993 qDebug() << "We don't have number as words";
994 // searchParams->setSortBy(SearchParams::SortByNewest);
995 // TODO searchParams->setReverseOrder(true);
996 // TODO searchParams->setMax(50);
1000 search(searchParams);
1004 void MediaView::relatedVideos() {
1005 Video* video = playlistModel->activeVideo();
1007 YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource();
1008 singleVideoSource->setVideo(video->clone());
1009 singleVideoSource->setAsyncDetails(true);
1010 setVideoSource(singleVideoSource);
1011 The::globalActions()->value("related-videos")->setEnabled(false);
1014 void MediaView::shareViaTwitter() {
1015 Video* video = playlistModel->activeVideo();
1017 QUrl url("https://twitter.com/intent/tweet");
1019 QUrlQueryHelper urlHelper(url);
1020 urlHelper.addQueryItem("via", "minitubeapp");
1021 urlHelper.addQueryItem("text", video->title());
1022 urlHelper.addQueryItem("url", video->webpage());
1024 QDesktopServices::openUrl(url);
1027 void MediaView::shareViaFacebook() {
1028 Video* video = playlistModel->activeVideo();
1030 QUrl url("https://www.facebook.com/sharer.php");
1032 QUrlQueryHelper urlHelper(url);
1033 urlHelper.addQueryItem("t", video->title());
1034 urlHelper.addQueryItem("u", video->webpage());
1036 QDesktopServices::openUrl(url);
1039 void MediaView::shareViaBuffer() {
1040 Video* video = playlistModel->activeVideo();
1042 QUrl url("http://bufferapp.com/add");
1044 QUrlQueryHelper urlHelper(url);
1045 urlHelper.addQueryItem("via", "minitubeapp");
1046 urlHelper.addQueryItem("text", video->title());
1047 urlHelper.addQueryItem("url", video->webpage());
1048 urlHelper.addQueryItem("picture", video->thumbnailUrl());
1050 QDesktopServices::openUrl(url);
1053 void MediaView::shareViaEmail() {
1054 Video* video = playlistModel->activeVideo();
1056 QUrl url("mailto:");
1058 QUrlQueryHelper urlHelper(url);
1059 urlHelper.addQueryItem("subject", video->title());
1060 const QString body = video->title() + "\n" +
1061 video->webpage() + "\n\n" +
1062 tr("Sent from %1").arg(Constants::NAME) + "\n" +
1064 urlHelper.addQueryItem("body", body);
1066 QDesktopServices::openUrl(url);
1069 void MediaView::authorPushed(QModelIndex index) {
1070 Video* video = playlistModel->videoAt(index.row());
1073 QString channelId = video->channelId();
1074 // if (channelId.isEmpty()) channelId = video->channelTitle();
1075 if (channelId.isEmpty()) return;
1077 SearchParams *searchParams = new SearchParams();
1078 searchParams->setChannelId(channelId);
1079 searchParams->setSortBy(SearchParams::SortByNewest);
1082 search(searchParams);
1085 void MediaView::updateSubscriptionAction(Video *video, bool subscribed) {
1086 QAction *subscribeAction = The::globalActions()->value("subscribe-channel");
1088 QString subscribeTip;
1089 QString subscribeText;
1091 subscribeText = subscribeAction->property("originalText").toString();
1092 subscribeAction->setEnabled(false);
1093 } else if (subscribed) {
1094 subscribeText = tr("Unsubscribe from %1").arg(video->channelTitle());
1095 subscribeTip = subscribeText;
1096 subscribeAction->setEnabled(true);
1098 subscribeText = tr("Subscribe to %1").arg(video->channelTitle());
1099 subscribeTip = subscribeText;
1100 subscribeAction->setEnabled(true);
1102 subscribeAction->setText(subscribeText);
1103 subscribeAction->setStatusTip(subscribeTip);
1107 static QIcon tintedIcon;
1108 if (tintedIcon.isNull()) {
1110 sizes << QSize(16, 16);
1111 tintedIcon = IconUtils::tintedIcon("bookmark-new", QColor(254, 240, 0), sizes);
1113 subscribeAction->setIcon(tintedIcon);
1115 subscribeAction->setIcon(IconUtils::icon("bookmark-remove"));
1118 subscribeAction->setIcon(IconUtils::icon("bookmark-new"));
1121 IconUtils::setupAction(subscribeAction);
1124 void MediaView::toggleSubscription() {
1125 Video *video = playlistModel->activeVideo();
1127 QString userId = video->channelId();
1128 if (userId.isEmpty()) return;
1129 bool subscribed = YTChannel::isSubscribed(userId);
1130 if (subscribed) YTChannel::unsubscribe(userId);
1131 else YTChannel::subscribe(userId);
1132 updateSubscriptionAction(video, !subscribed);