2 #include "playlistview.h"
3 #include "playlist/PrettyItemDelegate.h"
4 #include "networkaccess.h"
5 #include "videowidget.h"
6 #include "minisplitter.h"
8 #include "downloadmanager.h"
9 #include "downloaditem.h"
10 #include "MainWindow.h"
11 #include "temporary.h"
14 NetworkAccess* http();
18 QMap<QString, QAction*>* globalActions();
19 QMap<QString, QMenu*>* globalMenus();
20 QNetworkAccessManager* networkAccessManager();
23 MediaView::MediaView(QWidget *parent) : QWidget(parent) {
25 reallyStopped = false;
28 QBoxLayout *layout = new QVBoxLayout();
31 splitter = new MiniSplitter(this);
32 splitter->setChildrenCollapsible(false);
34 sortBar = new SegmentedControl(this);
35 mostRelevantAction = new QAction(tr("Most relevant"), this);
36 QKeySequence keySequence(Qt::CTRL + Qt::Key_1);
37 mostRelevantAction->setShortcut(keySequence);
38 mostRelevantAction->setStatusTip(mostRelevantAction->text() + " (" + keySequence.toString(QKeySequence::NativeText) + ")");
39 addAction(mostRelevantAction);
40 connect(mostRelevantAction, SIGNAL(triggered()), this, SLOT(searchMostRelevant()), Qt::QueuedConnection);
41 sortBar->addAction(mostRelevantAction);
42 mostRecentAction = new QAction(tr("Most recent"), this);
43 keySequence = QKeySequence(Qt::CTRL + Qt::Key_2);
44 mostRecentAction->setShortcut(keySequence);
45 mostRecentAction->setStatusTip(mostRecentAction->text() + " (" + keySequence.toString(QKeySequence::NativeText) + ")");
46 addAction(mostRecentAction);
47 connect(mostRecentAction, SIGNAL(triggered()), this, SLOT(searchMostRecent()), Qt::QueuedConnection);
48 sortBar->addAction(mostRecentAction);
49 mostViewedAction = new QAction(tr("Most viewed"), this);
50 keySequence = QKeySequence(Qt::CTRL + Qt::Key_3);
51 mostViewedAction->setShortcut(keySequence);
52 mostViewedAction->setStatusTip(mostViewedAction->text() + " (" + keySequence.toString(QKeySequence::NativeText) + ")");
53 addAction(mostViewedAction);
54 connect(mostViewedAction, SIGNAL(triggered()), this, SLOT(searchMostViewed()), Qt::QueuedConnection);
55 sortBar->addAction(mostViewedAction);
57 listView = new PlaylistView(this);
58 listView->setItemDelegate(new PrettyItemDelegate(this));
59 listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
62 listView->setDragEnabled(true);
63 listView->setAcceptDrops(true);
64 listView->setDropIndicatorShown(true);
65 listView->setDragDropMode(QAbstractItemView::DragDrop);
68 listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
69 listView->setFrameShape( QFrame::NoFrame );
70 listView->setAttribute(Qt::WA_MacShowFocusRect, false);
71 listView->setMinimumSize(320,240);
72 listView->setUniformItemSizes(true);
74 // respond to the user doubleclicking a playlist item
75 connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
77 listModel = new ListModel(this);
78 connect(listModel, SIGNAL(activeRowChanged(int)), this, SLOT(activeRowChanged(int)));
79 // needed to restore the selection after dragndrop
80 connect(listModel, SIGNAL(needSelectionFor(QList<Video*>)), this, SLOT(selectVideos(QList<Video*>)));
81 listView->setModel(listModel);
83 connect(listView->selectionModel(),
84 SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
85 this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
87 connect(listView, SIGNAL(authorPushed(QModelIndex)), SLOT(authorPushed(QModelIndex)));
89 playlistWidget = new PlaylistWidget(this, sortBar, listView);
91 splitter->addWidget(playlistWidget);
93 videoAreaWidget = new VideoAreaWidget(this);
94 // videoAreaWidget->setMinimumSize(320,240);
97 // mouse autohide does not work on the Mac (no mouseMoveEvent)
98 videoWidget = new Phonon::VideoWidget(this);
100 videoWidget = new VideoWidget(this);
103 videoAreaWidget->setVideoWidget(videoWidget);
104 videoAreaWidget->setListModel(listModel);
106 loadingWidget = new LoadingWidget(this);
107 videoAreaWidget->setLoadingWidget(loadingWidget);
109 splitter->addWidget(videoAreaWidget);
111 layout->addWidget(splitter);
114 splitter->setStretchFactor(0, 1);
115 splitter->setStretchFactor(1, 6);
117 // restore splitter state
119 splitter->restoreState(settings.value("splitter").toByteArray());
121 errorTimer = new QTimer(this);
122 errorTimer->setSingleShot(true);
123 errorTimer->setInterval(3000);
124 connect(errorTimer, SIGNAL(timeout()), SLOT(skipVideo()));
126 workaroundTimer = new QTimer(this);
127 workaroundTimer->setSingleShot(true);
128 workaroundTimer->setInterval(3000);
129 connect(workaroundTimer, SIGNAL(timeout()), SLOT(timerPlay()));
132 demoTimer = new QTimer(this);
133 demoTimer->setSingleShot(true);
134 connect(demoTimer, SIGNAL(timeout()), SLOT(demoMessage()));
139 void MediaView::initialize() {
140 connect(videoAreaWidget, SIGNAL(doubleClicked()), The::globalActions()->value("fullscreen"), SLOT(trigger()));
143 videoAreaWidget->setContextMenuPolicy(Qt::CustomContextMenu);
144 connect(videoAreaWidget, SIGNAL(customContextMenuRequested(QPoint)),
145 this, SLOT(showVideoContextMenu(QPoint)));
149 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
150 this->mediaObject = mediaObject;
151 Phonon::createPath(this->mediaObject, videoWidget);
152 connect(mediaObject, SIGNAL(finished()), this, SLOT(playbackFinished()));
153 connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
154 this, SLOT(stateChanged(Phonon::State, Phonon::State)));
155 connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
156 this, SLOT(currentSourceChanged(Phonon::MediaSource)));
157 // connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
158 connect(mediaObject, SIGNAL(aboutToFinish()), SLOT(aboutToFinish()));
161 void MediaView::search(SearchParams *searchParams) {
162 reallyStopped = false;
167 workaroundTimer->stop();
170 this->searchParams = searchParams;
172 // start serching for videos
173 listModel->search(searchParams);
175 // this implies that the enum and the bar action order is the same
176 sortBar->setCheckedAction(searchParams->sortBy()-1);
178 listView->setFocus();
180 QString keyword = searchParams->keywords();
181 QString display = keyword;
182 if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
183 int separator = keyword.indexOf("|");
184 if (separator > 0 && separator + 1 < keyword.length()) {
185 display = keyword.mid(separator+1);
191 void MediaView::appear() {
192 listView->setFocus();
195 void MediaView::disappear() {
196 timerPlayFlag = true;
199 void MediaView::handleError(QString /* message */) {
201 QTimer::singleShot(500, 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*/) {
212 // qDebug() << "Phonon state: " << newState;
213 // slider->setEnabled(newState == Phonon::PlayingState);
217 case Phonon::ErrorState:
218 qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
219 if (mediaObject->errorType() == Phonon::FatalError)
220 handleError(mediaObject->errorString());
223 case Phonon::PlayingState:
224 // qDebug("playing");
225 videoAreaWidget->showVideo();
228 case Phonon::StoppedState:
229 // qDebug("stopped");
230 // play() has already been called when setting the source
231 // but Phonon on Linux needs a little more help to start playback
232 // if (!reallyStopped) mediaObject->play();
235 // Workaround for Mac playback start problem
236 if (!timerPlayFlag) {
237 // workaroundTimer->start();
243 case Phonon::PausedState:
247 case Phonon::BufferingState:
251 case Phonon::LoadingState:
258 void MediaView::pause() {
259 // qDebug() << "pause() called" << mediaObject->state();
261 switch( mediaObject->state() ) {
262 case Phonon::PlayingState:
263 mediaObject->pause();
272 QRegExp MediaView::wordRE(QString s) {
273 return QRegExp("\\W" + s + "\\W?", Qt::CaseInsensitive);
276 void MediaView::stop() {
277 listModel->abortSearch();
278 reallyStopped = true;
280 videoAreaWidget->clear();
281 workaroundTimer->stop();
283 listView->selectionModel()->clearSelection();
285 downloadItem->stop();
291 void MediaView::activeRowChanged(int row) {
292 if (reallyStopped) return;
294 Video *video = listModel->videoAt(row);
297 // now that we have a new video to play
298 // stop all the timers
299 workaroundTimer->stop();
304 downloadItem->stop();
308 // slider->setMinimum(0);
310 // immediately show the loading widget
311 videoAreaWidget->showLoading(video);
313 connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)), Qt::UniqueConnection);
314 // TODO handle signal in a proper slot and impl item error status
315 connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)), Qt::UniqueConnection);
317 video->loadStreamUrl();
319 // reset the timer flag
320 timerPlayFlag = false;
322 // video title in the statusbar
323 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
324 if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
327 // ensure active item is visible
328 // int row = listModel->activeRow();
330 QModelIndex index = listModel->index(row, 0, QModelIndex());
331 listView->scrollTo(index, QAbstractItemView::EnsureVisible);
334 // enable/disable actions
335 The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
336 The::globalActions()->value("skip")->setEnabled(true);
337 The::globalActions()->value("previous")->setEnabled(row > 0);
338 The::globalActions()->value("stopafterthis")->setEnabled(true);
340 // see you in gotStreamUrl...
344 void MediaView::gotStreamUrl(QUrl streamUrl) {
345 if (reallyStopped) return;
347 Video *video = static_cast<Video *>(sender());
349 qDebug() << "Cannot get sender";
352 video->disconnect(this);
354 QString tempFile = Temporary::filename();
356 Video *videoCopy = video->clone();
358 downloadItem->stop();
361 downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
362 connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()), Qt::UniqueConnection);
363 // connect(downloadItem, SIGNAL(progress(int)), SLOT(downloadProgress(int)));
364 connect(downloadItem, SIGNAL(bufferProgress(int)), loadingWidget, SLOT(bufferStatus(int)), Qt::UniqueConnection);
365 // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
366 connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)), Qt::UniqueConnection);
367 connect(downloadItem, SIGNAL(error(QString)), SLOT(handleError(QString)), Qt::UniqueConnection);
368 downloadItem->start();
373 void MediaView::downloadProgress(int percent) {
374 MainWindow* mainWindow = dynamic_cast<MainWindow*>(window());
376 mainWindow->getSeekSlider()->setStyleSheet(" QSlider::groove:horizontal {"
377 "border: 1px solid #999999;"
378 // "border-left: 50px solid rgba(255, 0, 0, 128);"
380 "background: qlineargradient(x1:0, y1:0, x2:.5, y2:0, stop:0 rgba(255, 0, 0, 92), stop:"
381 + QString::number(percent/100.0) +
383 " rgba(255, 0, 0, 92), stop:" + QString::number((percent+1)/100.0) + " transparent, stop:1 transparent);"
386 "QSlider::handle:horizontal {"
387 "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);"
388 "border: 1px solid #5c5c5c;"
392 "border-radius: 8px;"
400 void MediaView::downloadStatusChanged() {
401 switch(downloadItem->status()) {
406 // qDebug() << "Starting";
409 // qDebug() << "Finished" << mediaObject->state();
410 // if (mediaObject->state() == Phonon::StoppedState) startPlaying();
412 seekSlider->setEnabled(mediaObject->isSeekable());
416 // qDebug() << "Failed";
418 // qDebug() << "Idle";
423 void MediaView::startPlaying() {
424 if (reallyStopped) return;
431 QString source = downloadItem->currentFilename();
432 qDebug() << "Playing" << source;
433 mediaObject->setCurrentSource(source);
436 seekSlider->setEnabled(false);
439 // ensure we always have 10 videos ahead
440 listModel->searchNeeded();
442 // ensure active item is visible
443 int row = listModel->activeRow();
445 QModelIndex index = listModel->index(row, 0, QModelIndex());
446 listView->scrollTo(index, QAbstractItemView::EnsureVisible);
450 demoTimer->start(60000);
455 void MediaView::itemActivated(const QModelIndex &index) {
456 if (listModel->rowExists(index.row())) {
458 // if it's the current video, just rewind and play
459 Video *activeVideo = listModel->activeVideo();
460 Video *video = listModel->videoAt(index.row());
461 if (activeVideo && video && activeVideo == video) {
462 mediaObject->seek(0);
464 } else listModel->setActiveRow(index.row());
466 // the user doubleclicked on the "Search More" item
468 listModel->searchMore();
469 listView->selectionModel()->clearSelection();
473 void MediaView::currentSourceChanged(const Phonon::MediaSource /* source */ ) {
477 void MediaView::skipVideo() {
478 // skippedVideo is useful for DELAYED skip operations
479 // in order to be sure that we're skipping the video we wanted
480 // and not another one
482 if (listModel->activeVideo() != skippedVideo) {
483 qDebug() << "Skip of video canceled";
486 int nextRow = listModel->rowForVideo(skippedVideo);
488 if (nextRow == -1) return;
489 listModel->setActiveRow(nextRow);
493 void MediaView::skip() {
494 int nextRow = listModel->nextRow();
495 if (nextRow == -1) return;
496 listModel->setActiveRow(nextRow);
499 void MediaView::skipBackward() {
500 int prevRow = listModel->previousRow();
501 if (prevRow == -1) return;
502 listModel->setActiveRow(prevRow);
505 void MediaView::aboutToFinish() {
506 qint64 currentTime = mediaObject->currentTime();
507 qDebug() << __PRETTY_FUNCTION__ << currentTime;
508 if (currentTime + 10000 < mediaObject->totalTime()) {
509 // mediaObject->seek(mediaObject->currentTime());
510 // QTimer::singleShot(500, this, SLOT(playbackResume()));
511 mediaObject->seek(currentTime);
516 void MediaView::playbackFinished() {
517 qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime();
518 // qDebug() << "finished" << mediaObject->currentTime() << mediaObject->totalTime();
519 // add 10 secs for imprecise Phonon backends (VLC, Xine)
520 if (mediaObject->currentTime() + 10000 < mediaObject->totalTime()) {
521 // mediaObject->seek(mediaObject->currentTime());
522 QTimer::singleShot(500, this, SLOT(playbackResume()));
524 QAction* stopAfterThisAction = The::globalActions()->value("stopafterthis");
525 if (stopAfterThisAction->isChecked()) {
526 stopAfterThisAction->setChecked(false);
531 void MediaView::playbackResume() {
532 qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime();
533 mediaObject->seek(mediaObject->currentTime());
537 void MediaView::openWebPage() {
538 Video* video = listModel->activeVideo();
540 mediaObject->pause();
541 QDesktopServices::openUrl(video->webpage());
544 void MediaView::copyWebPage() {
545 Video* video = listModel->activeVideo();
547 QString address = video->webpage().toString();
548 QApplication::clipboard()->setText(address);
549 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
550 QString message = tr("You can now paste the YouTube link into another application");
551 if (mainWindow) mainWindow->statusBar()->showMessage(message);
554 void MediaView::copyVideoLink() {
555 Video* video = listModel->activeVideo();
557 QApplication::clipboard()->setText(video->getStreamUrl().toEncoded());
558 QString message = tr("You can now paste the video stream URL into another application")
559 + ". " + tr("The link will be valid only for a limited time.");
560 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
561 if (mainWindow) mainWindow->statusBar()->showMessage(message);
564 void MediaView::removeSelected() {
565 if (!listView->selectionModel()->hasSelection()) return;
566 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
567 listModel->removeIndexes(indexes);
570 void MediaView::selectVideos(QList<Video*> videos) {
571 foreach (Video *video, videos) {
572 QModelIndex index = listModel->indexForVideo(video);
573 listView->selectionModel()->select(index, QItemSelectionModel::Select);
574 listView->scrollTo(index, QAbstractItemView::EnsureVisible);
578 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
579 const bool gotSelection = listView->selectionModel()->hasSelection();
580 The::globalActions()->value("remove")->setEnabled(gotSelection);
581 The::globalActions()->value("moveUp")->setEnabled(gotSelection);
582 The::globalActions()->value("moveDown")->setEnabled(gotSelection);
585 void MediaView::moveUpSelected() {
586 if (!listView->selectionModel()->hasSelection()) return;
588 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
589 qStableSort(indexes.begin(), indexes.end());
590 listModel->move(indexes, true);
592 // set current index after row moves to something more intuitive
593 int row = indexes.first().row();
594 listView->selectionModel()->setCurrentIndex(listModel->index(row>1?row:1), QItemSelectionModel::NoUpdate);
597 void MediaView::moveDownSelected() {
598 if (!listView->selectionModel()->hasSelection()) return;
600 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
601 qStableSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
602 listModel->move(indexes, false);
604 // set current index after row moves to something more intuitive (respect 1 static item on bottom)
605 int row = indexes.first().row()+1, max = listModel->rowCount() - 2;
606 listView->selectionModel()->setCurrentIndex(listModel->index(row>max?max:row), QItemSelectionModel::NoUpdate);
609 void MediaView::showVideoContextMenu(QPoint point) {
610 The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
613 void MediaView::searchMostRelevant() {
614 searchParams->setSortBy(SearchParams::SortByRelevance);
615 search(searchParams);
618 void MediaView::searchMostRecent() {
619 searchParams->setSortBy(SearchParams::SortByNewest);
620 search(searchParams);
623 void MediaView::searchMostViewed() {
624 searchParams->setSortBy(SearchParams::SortByViewCount);
625 search(searchParams);
628 void MediaView::setPlaylistVisible(bool visible) {
629 playlistWidget->setVisible(visible);
632 void MediaView::timerPlay() {
633 // Workaround Phonon bug on Mac OSX
634 // qDebug() << mediaObject->currentTime();
635 if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
636 // qDebug() << "Mac playback workaround";
637 mediaObject->pause();
638 // QTimer::singleShot(1000, mediaObject, SLOT(play()));
643 void MediaView::saveSplitterState() {
645 settings.setValue("splitter", splitter->saveState());
650 static QPushButton *continueButton;
652 void MediaView::demoMessage() {
653 if (mediaObject->state() != Phonon::PlayingState) return;
654 mediaObject->pause();
656 QMessageBox msgBox(this);
657 msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
658 msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::NAME));
659 msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
660 msgBox.setModal(true);
661 // make it a "sheet" on the Mac
662 msgBox.setWindowModality(Qt::WindowModal);
664 continueButton = msgBox.addButton("5", QMessageBox::RejectRole);
665 continueButton->setEnabled(false);
666 QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
668 QTimeLine *timeLine = new QTimeLine(6000, this);
669 timeLine->setCurveShape(QTimeLine::LinearCurve);
670 timeLine->setFrameRange(5, 0);
671 connect(timeLine, SIGNAL(frameChanged(int)), SLOT(updateContinueButton(int)));
676 if (msgBox.clickedButton() == buyButton) {
677 QDesktopServices::openUrl(QUrl(QString(Constants::WEBSITE) + "#download"));
680 demoTimer->start(600000);
687 void MediaView::updateContinueButton(int value) {
689 continueButton->setText(tr("Continue"));
690 continueButton->setEnabled(true);
692 continueButton->setText(QString::number(value));
698 void MediaView::downloadVideo() {
699 Video* video = listModel->activeVideo();
702 DownloadManager::instance()->addItem(video);
706 The::globalActions()->value("downloads")->setVisible(true);
708 // The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
710 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
711 QString message = tr("Downloading %1").arg(video->title());
712 if (mainWindow) mainWindow->statusBar()->showMessage(message);
715 void MediaView::fullscreen() {
716 videoAreaWidget->setParent(0);
717 videoAreaWidget->showFullScreen();
721 void MediaView::setSlider(QSlider *slider) {
722 this->slider = slider;
723 // slider->setEnabled(false);
724 slider->setTracking(false);
725 // connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
728 void MediaView::sliderMoved(int value) {
729 qDebug() << __func__;
730 int sliderPercent = (value * 100) / (slider->maximum() - slider->minimum());
731 qDebug() << slider->minimum() << value << slider->maximum();
732 if (sliderPercent <= downloadItem->currentPercent()) {
733 qDebug() << sliderPercent << downloadItem->currentPercent();
734 mediaObject->seek(value);
740 void MediaView::seekTo(int value) {
741 qDebug() << __func__;
742 mediaObject->pause();
743 workaroundTimer->stop();
745 // mediaObject->clear();
747 QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
748 QString tempFile = tempDir + "/minitube" + QString::number(value) + ".mp4";
749 if (!QFile::remove(tempFile)) {
750 qDebug() << "Cannot remove temp file";
752 Video *videoCopy = downloadItem->getVideo()->clone();
753 QUrl streamUrl = videoCopy->getStreamUrl();
754 streamUrl.addQueryItem("begin", QString::number(value));
755 if (downloadItem) delete downloadItem;
756 downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
757 connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
758 // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
759 downloadItem->start();
761 // slider->setMinimum(value);
767 void MediaView::findVideoParts() {
770 Video* video = listModel->activeVideo();
773 QString query = video->title();
775 static QString optionalSpace = "\\s*";
776 static QString staticCounterSeparators = "[\\/\\-]";
777 QString counterSeparators = "( of | " +
778 tr("of", "Used in video parts, as in '2 of 3'") +
779 " |" + staticCounterSeparators + ")";
781 // numbers from 1 to 15
782 static QString counterNumber = "([1-9]|1[0-5])";
784 // query.remove(QRegExp(counterSeparators + optionalSpace + counterNumber));
785 query.remove(QRegExp(counterNumber + optionalSpace + counterSeparators + optionalSpace + counterNumber));
786 query.remove(wordRE("pr?t\\.?" + optionalSpace + counterNumber));
787 query.remove(wordRE("ep\\.?" + optionalSpace + counterNumber));
788 query.remove(wordRE("part" + optionalSpace + counterNumber));
789 query.remove(wordRE("episode" + optionalSpace + counterNumber));
790 query.remove(wordRE(tr("part", "This is for video parts, as in 'Cool video - part 1'") +
791 optionalSpace + counterNumber));
792 query.remove(wordRE(tr("episode", "This is for video parts, as in 'Cool series - episode 1'") +
793 optionalSpace + counterNumber));
794 query.remove(QRegExp("[\\(\\)\\[\\]]"));
796 #define NUMBERS "one|two|three|four|five|six|seven|eight|nine|ten"
798 QRegExp englishNumberRE = QRegExp(QLatin1String(".*(") + NUMBERS + ").*", Qt::CaseInsensitive);
799 // bool numberAsWords = englishNumberRE.exactMatch(query);
800 query.remove(englishNumberRE);
802 QRegExp localizedNumberRE = QRegExp(QLatin1String(".*(") + tr(NUMBERS) + ").*", Qt::CaseInsensitive);
803 // if (!numberAsWords) numberAsWords = localizedNumberRE.exactMatch(query);
804 query.remove(localizedNumberRE);
806 SearchParams *searchParams = new SearchParams();
807 searchParams->setTransient(true);
808 searchParams->setKeywords(query);
809 searchParams->setAuthor(video->author());
812 if (!numberAsWords) {
813 qDebug() << "We don't have number as words";
814 // searchParams->setSortBy(SearchParams::SortByNewest);
815 // TODO searchParams->setReverseOrder(true);
816 // TODO searchParams->setMax(50);
820 search(searchParams);
824 void MediaView::shareViaTwitter() {
825 Video* video = listModel->activeVideo();
827 QUrl url("https://twitter.com/intent/tweet");
828 url.addQueryItem("via", "minitubeapp");
829 url.addQueryItem("text", video->title());
830 url.addQueryItem("url", video->webpage().toString());
831 QDesktopServices::openUrl(url);
834 void MediaView::shareViaFacebook() {
835 Video* video = listModel->activeVideo();
837 QUrl url("https://www.facebook.com/sharer.php");
838 url.addQueryItem("t", video->title());
839 url.addQueryItem("u", video->webpage().toString());
840 QDesktopServices::openUrl(url);
843 void MediaView::shareViaEmail() {
844 Video* video = listModel->activeVideo();
847 url.addQueryItem("subject", video->title());
848 QString body = video->title() + "\n" +
849 video->webpage().toString() + "\n\n" +
850 tr("Sent from %1").arg(Constants::NAME) + "\n" +
852 url.addQueryItem("body", body);
853 QDesktopServices::openUrl(url);
856 void MediaView::authorPushed(QModelIndex index) {
857 Video* video = listModel->videoAt(index.row());
860 QString channel = video->author();
861 if (channel.isEmpty()) return;
863 SearchParams *searchParams = new SearchParams();
864 searchParams->setAuthor(channel);
865 searchParams->setSortBy(SearchParams::SortByNewest);
868 search(searchParams);