]> git.sur5r.net Git - minitube/blob - src/MediaView.cpp
Imported Upstream version 1.4.1
[minitube] / src / MediaView.cpp
1 #include "MediaView.h"
2 #include "playlist/PrettyItemDelegate.h"
3 #include "networkaccess.h"
4 #include "videowidget.h"
5 #include "minisplitter.h"
6 #include "constants.h"
7 #include "downloadmanager.h"
8 #include "downloaditem.h"
9 #include "MainWindow.h"
10
11 namespace The {
12     NetworkAccess* http();
13 }
14
15 namespace The {
16     QMap<QString, QAction*>* globalActions();
17     QMap<QString, QMenu*>* globalMenus();
18     QNetworkAccessManager* networkAccessManager();
19 }
20
21 MediaView::MediaView(QWidget *parent) : QWidget(parent) {
22
23     reallyStopped = false;
24     downloadItem = 0;
25
26     QBoxLayout *layout = new QHBoxLayout();
27     layout->setMargin(0);
28
29     splitter = new MiniSplitter(this);
30     splitter->setChildrenCollapsible(false);
31
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);
54
55     listView = new QListView(this);
56     listView->setItemDelegate(new PrettyItemDelegate(this));
57     listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
58
59     // dragndrop
60     listView->setDragEnabled(true);
61     listView->setAcceptDrops(true);
62     listView->setDropIndicatorShown(true);
63     listView->setDragDropMode(QAbstractItemView::DragDrop);
64
65     // cosmetics
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);
71
72     // respond to the user doubleclicking a playlist item
73     connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
74
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);
80
81     connect(listView->selectionModel(),
82             SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
83             this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
84
85     playlistWidget = new PlaylistWidget(this, sortBar, listView);
86
87     splitter->addWidget(playlistWidget);
88
89     videoAreaWidget = new VideoAreaWidget(this);
90     videoAreaWidget->setMinimumSize(320,240);
91
92 #ifdef APP_MAC
93     // mouse autohide does not work on the Mac (no mouseMoveEvent)
94     videoWidget = new Phonon::VideoWidget(this);
95 #else
96     videoWidget = new VideoWidget(this);
97 #endif
98
99     videoAreaWidget->setVideoWidget(videoWidget);
100     videoAreaWidget->setListModel(listModel);
101
102     loadingWidget = new LoadingWidget(this);
103     videoAreaWidget->setLoadingWidget(loadingWidget);
104
105     splitter->addWidget(videoAreaWidget);
106
107     layout->addWidget(splitter);
108     setLayout(layout);
109
110     // restore splitter state
111     QSettings settings;
112     splitter->restoreState(settings.value("splitter").toByteArray());
113
114     errorTimer = new QTimer(this);
115     errorTimer->setSingleShot(true);
116     errorTimer->setInterval(3000);
117     connect(errorTimer, SIGNAL(timeout()), SLOT(skipVideo()));
118
119     workaroundTimer = new QTimer(this);
120     workaroundTimer->setSingleShot(true);
121     workaroundTimer->setInterval(3000);
122     connect(workaroundTimer, SIGNAL(timeout()), SLOT(timerPlay()));
123
124 #ifdef APP_DEMO
125     demoTimer = new QTimer(this);
126     demoTimer->setSingleShot(true);
127     demoTimer->setInterval(60000);
128     connect(demoTimer, SIGNAL(timeout()), SLOT(demoMessage()));
129 #endif
130
131 }
132
133 void MediaView::initialize() {
134     connect(videoAreaWidget, SIGNAL(doubleClicked()), The::globalActions()->value("fullscreen"), SLOT(trigger()));
135     videoAreaWidget->setContextMenuPolicy(Qt::CustomContextMenu);
136     connect(videoAreaWidget, SIGNAL(customContextMenuRequested(QPoint)),
137             this, SLOT(showVideoContextMenu(QPoint)));
138 }
139
140 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
141     this->mediaObject = mediaObject;
142     Phonon::createPath(this->mediaObject, videoWidget);
143     connect(mediaObject, SIGNAL(finished()), this, SLOT(playbackFinished()));
144     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
145             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
146     connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
147             this, SLOT(currentSourceChanged(Phonon::MediaSource)));
148     // connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
149 }
150
151 void MediaView::search(SearchParams *searchParams) {
152     reallyStopped = false;
153
154 #ifdef APP_DEMO
155     demoTimer->stop();
156 #endif
157
158     videoAreaWidget->clear();
159     workaroundTimer->stop();
160     errorTimer->stop();
161
162     mediaObject->pause();
163     if (downloadItem) {
164         delete downloadItem;
165         downloadItem = 0;
166     }
167
168     this->searchParams = searchParams;
169
170     // start serching for videos
171     listModel->search(searchParams);
172
173     // this implies that the enum and the bar action order is the same
174     sortBar->setCheckedAction(searchParams->sortBy()-1);
175
176     listView->setFocus();
177
178
179     QString keyword = searchParams->keywords();
180     QString display = keyword;
181     if (keyword.startsWith("http://")) {
182         int separator = keyword.indexOf("|");
183         if (separator > 0 && separator + 1 < keyword.length()) {
184             display = keyword.mid(separator+1);
185         }
186
187         // also hide sidebar
188         playlistWidget->hide();
189     } else playlistWidget->show();
190     // tr("You're watching \"%1\"").arg(searchParams->keywords())
191
192 }
193
194 void MediaView::disappear() {
195     timerPlayFlag = true;
196 }
197
198 void MediaView::handleError(QString message) {
199     videoAreaWidget->showError(message);
200     skippedVideo = listModel->activeVideo();
201     // recover from errors by skipping to the next video
202     errorTimer->start(2000);
203 }
204
205 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
206 {
207
208     // qDebug() << "Phonon state: " << newState << oldState;
209     // slider->setEnabled(newState == Phonon::PlayingState);
210
211     switch (newState) {
212
213     case Phonon::ErrorState:
214         qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
215         if (mediaObject->errorType() == Phonon::FatalError)
216             handleError(mediaObject->errorString());
217         break;
218
219     case Phonon::PlayingState:
220         // qDebug("playing");
221         videoAreaWidget->showVideo();
222         break;
223
224     case Phonon::StoppedState:
225         // qDebug("stopped");
226         // play() has already been called when setting the source
227         // but Phonon on Linux needs a little more help to start playback
228         // if (!reallyStopped) mediaObject->play();
229
230 #ifdef APP_MAC
231         // Workaround for Mac playback start problem
232         if (!timerPlayFlag) {
233             // workaroundTimer->start();
234         }
235 #endif
236
237         break;
238
239          case Phonon::PausedState:
240         qDebug("paused");
241         break;
242
243          case Phonon::BufferingState:
244         qDebug("buffering");
245         break;
246
247          case Phonon::LoadingState:
248         qDebug("loading");
249         break;
250
251          default:
252         ;
253     }
254 }
255
256 void MediaView::pause() {
257     // qDebug() << "pause() called" << mediaObject->state();
258     switch( mediaObject->state() ) {
259     case Phonon::PlayingState:
260         mediaObject->pause();
261         break;
262     default:
263         mediaObject->play();
264         break;
265     }
266 }
267
268 void MediaView::stop() {
269     listModel->abortSearch();
270     reallyStopped = true;
271     mediaObject->stop();
272     videoAreaWidget->clear();
273     workaroundTimer->stop();
274     errorTimer->stop();
275     listView->selectionModel()->clearSelection();
276     if (downloadItem) {
277         downloadItem->stop();
278         delete downloadItem;
279         downloadItem = 0;
280     }
281 }
282
283 void MediaView::activeRowChanged(int row) {
284     if (reallyStopped) return;
285
286     Video *video = listModel->videoAt(row);
287     if (!video) return;
288
289     // now that we have a new video to play
290     // stop all the timers
291     workaroundTimer->stop();
292     errorTimer->stop();
293
294     mediaObject->pause();
295     if (downloadItem) {
296         downloadItem->stop();
297         delete downloadItem;
298         downloadItem = 0;
299     }
300     // slider->setMinimum(0);
301
302     // immediately show the loading widget
303     videoAreaWidget->showLoading(video);
304
305     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
306     // TODO handle signal in a proper slot and impl item error status
307     connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
308
309     video->loadStreamUrl();
310
311     // reset the timer flag
312     timerPlayFlag = false;
313
314     // video title in the statusbar
315     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
316     if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
317
318     The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
319
320     // see you in gotStreamUrl...
321
322 }
323
324 void MediaView::gotStreamUrl(QUrl streamUrl) {
325     if (reallyStopped) return;
326
327     Video *video = static_cast<Video *>(sender());
328     if (!video) {
329         qDebug() << "Cannot get sender";
330         return;
331     }
332     video->disconnect(this);
333
334
335     QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
336 #ifdef Q_WS_X11
337     QString tempFile = tempDir + "/minitube-" + getenv("USERNAME") + ".mp4";
338 #else
339     QString tempFile = tempDir + "/minitube.mp4";
340 #endif
341     if (QFile::exists(tempFile) && !QFile::remove(tempFile)) {
342         qDebug() << "Cannot remove temp file";
343     }
344
345     Video *videoCopy = video->clone();
346     if (downloadItem) {
347         downloadItem->stop();
348         delete downloadItem;
349     }
350     downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
351     connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
352     // connect(downloadItem, SIGNAL(progress(int)), SLOT(downloadProgress(int)));
353     connect(downloadItem, SIGNAL(bufferProgress(int)), loadingWidget, SLOT(bufferStatus(int)));
354     // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
355     connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
356     connect(downloadItem, SIGNAL(error(QString)), SLOT(handleError(QString)));
357     downloadItem->start();
358
359 }
360
361 /*
362 void MediaView::downloadProgress(int percent) {
363     MainWindow* mainWindow = dynamic_cast<MainWindow*>(window());
364
365     mainWindow->getSeekSlider()->setStyleSheet(" QSlider::groove:horizontal {"
366         "border: 1px solid #999999;"
367         // "border-left: 50px solid rgba(255, 0, 0, 128);"
368         "height: 8px;"
369         "background: qlineargradient(x1:0, y1:0, x2:.5, y2:0, stop:0 rgba(255, 0, 0, 92), stop:"
370         + QString::number(percent/100.0) +
371
372         " rgba(255, 0, 0, 92), stop:" + QString::number((percent+1)/100.0) + " transparent, stop:1 transparent);"
373         "margin: 2px 0;"
374     "}"
375     "QSlider::handle:horizontal {"
376         "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);"
377         "border: 1px solid #5c5c5c;"
378         "width: 16px;"
379         "height: 16px;"
380         "margin: -2px 0;"
381         "border-radius: 8px;"
382     "}"
383
384     );
385 }
386
387 */
388
389 void MediaView::downloadStatusChanged() {
390     switch(downloadItem->status()) {
391     case Downloading:
392         startPlaying();
393         break;
394     case Starting:
395         // qDebug() << "Starting";
396         break;
397     case Finished:
398         // qDebug() << "Finished" << mediaObject->state();
399         // if (mediaObject->state() == Phonon::StoppedState) startPlaying();
400         break;
401     case Failed:
402         // qDebug() << "Failed";
403     case Idle:
404         // qDebug() << "Idle";
405         break;
406     }
407 }
408
409 void MediaView::startPlaying() {
410     if (reallyStopped) return;
411
412     // go!
413     qDebug() << "Playing" << downloadItem->currentFilename();
414     mediaObject->setCurrentSource(downloadItem->currentFilename());
415     mediaObject->play();
416
417     // ensure we always have 10 videos ahead
418     listModel->searchNeeded();
419
420     // ensure active item is visible
421     int row = listModel->activeRow();
422     if (row != -1) {
423         QModelIndex index = listModel->index(row, 0, QModelIndex());
424         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
425     }
426
427 #ifdef APP_DEMO
428     demoTimer->start(30000);
429 #endif
430
431 }
432
433 void MediaView::itemActivated(const QModelIndex &index) {
434     if (listModel->rowExists(index.row()))
435         listModel->setActiveRow(index.row());
436     // the user doubleclicked on the "Search More" item
437     else listModel->searchMore();
438 }
439
440 void MediaView::currentSourceChanged(const Phonon::MediaSource /* source */ ) {
441
442 }
443
444 void MediaView::skipVideo() {
445     // skippedVideo is useful for DELAYED skip operations
446     // in order to be sure that we're skipping the video we wanted
447     // and not another one
448     if (skippedVideo) {
449         if (listModel->activeVideo() != skippedVideo) {
450             qDebug() << "Skip of video canceled";
451             return;
452         }
453         int nextRow = listModel->rowForVideo(skippedVideo);
454         nextRow++;
455         if (nextRow == -1) return;
456         listModel->setActiveRow(nextRow);
457     }
458 }
459
460 void MediaView::skip() {
461     int nextRow = listModel->nextRow();
462     if (nextRow == -1) return;
463     listModel->setActiveRow(nextRow);
464 }
465
466 void MediaView::playbackFinished() {
467     // qDebug() << "finished" << mediaObject->currentTime() << mediaObject->totalTime();
468     // add 10 secs for imprecise Phonon backends (VLC, Xine)
469     if (mediaObject->currentTime() + 10000 < mediaObject->totalTime()) {
470         // mediaObject->seek(mediaObject->currentTime());
471         QTimer::singleShot(3000, this, SLOT(playbackResume()));
472     } else skip();
473 }
474
475 void MediaView::playbackResume() {
476     mediaObject->seek(mediaObject->currentTime());
477     mediaObject->play();
478 }
479
480 void MediaView::openWebPage() {
481     Video* video = listModel->activeVideo();
482     if (!video) return;
483     mediaObject->pause();
484     QDesktopServices::openUrl(video->webpage());
485 }
486
487 void MediaView::copyWebPage() {
488     Video* video = listModel->activeVideo();
489     if (!video) return;
490     QString address = video->webpage().toString();
491     address.remove("&feature=youtube_gdata");
492     QApplication::clipboard()->setText(address);
493     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
494     QString message = tr("You can now paste the YouTube link into another application");
495     if (mainWindow) mainWindow->statusBar()->showMessage(message);
496 }
497
498 void MediaView::copyVideoLink() {
499     Video* video = listModel->activeVideo();
500     if (!video) return;
501     QApplication::clipboard()->setText(video->getStreamUrl().toEncoded());
502     QString message = tr("You can now paste the video stream URL into another application")
503                       + ". " + tr("The link will be valid only for a limited time.");
504     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
505     if (mainWindow) mainWindow->statusBar()->showMessage(message);
506 }
507
508 void MediaView::removeSelected() {
509     if (!listView->selectionModel()->hasSelection()) return;
510     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
511     listModel->removeIndexes(indexes);
512 }
513
514 void MediaView::selectVideos(QList<Video*> videos) {
515     foreach (Video *video, videos) {
516         QModelIndex index = listModel->indexForVideo(video);
517         listView->selectionModel()->select(index, QItemSelectionModel::Select);
518         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
519     }
520 }
521
522 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
523     const bool gotSelection = listView->selectionModel()->hasSelection();
524     The::globalActions()->value("remove")->setEnabled(gotSelection);
525     The::globalActions()->value("moveUp")->setEnabled(gotSelection);
526     The::globalActions()->value("moveDown")->setEnabled(gotSelection);
527 }
528
529 void MediaView::moveUpSelected() {
530     if (!listView->selectionModel()->hasSelection()) return;
531
532     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
533     qStableSort(indexes.begin(), indexes.end());
534     listModel->move(indexes, true);
535
536     // set current index after row moves to something more intuitive
537     int row = indexes.first().row();
538     listView->selectionModel()->setCurrentIndex(listModel->index(row>1?row:1), QItemSelectionModel::NoUpdate);
539 }
540
541 void MediaView::moveDownSelected() {
542     if (!listView->selectionModel()->hasSelection()) return;
543
544     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
545     qStableSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
546     listModel->move(indexes, false);
547
548     // set current index after row moves to something more intuitive (respect 1 static item on bottom)
549     int row = indexes.first().row()+1, max = listModel->rowCount() - 2;
550     listView->selectionModel()->setCurrentIndex(listModel->index(row>max?max:row), QItemSelectionModel::NoUpdate);
551 }
552
553 void MediaView::showVideoContextMenu(QPoint point) {
554     The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
555 }
556
557 void MediaView::searchMostRelevant() {
558     searchParams->setSortBy(SearchParams::SortByRelevance);
559     search(searchParams);
560 }
561
562 void MediaView::searchMostRecent() {
563     searchParams->setSortBy(SearchParams::SortByNewest);
564     search(searchParams);
565 }
566
567 void MediaView::searchMostViewed() {
568     searchParams->setSortBy(SearchParams::SortByViewCount);
569     search(searchParams);
570 }
571
572 void MediaView::setPlaylistVisible(bool visible) {
573     playlistWidget->setVisible(visible);
574 }
575
576 void MediaView::timerPlay() {
577     // Workaround Phonon bug on Mac OSX
578     // qDebug() << mediaObject->currentTime();
579     if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
580         // qDebug() << "Mac playback workaround";
581         mediaObject->pause();
582         // QTimer::singleShot(1000, mediaObject, SLOT(play()));
583         mediaObject->play();
584     }
585 }
586
587 void MediaView::saveSplitterState() {
588     QSettings settings;
589     settings.setValue("splitter", splitter->saveState());
590 }
591
592 #ifdef APP_DEMO
593 void MediaView::demoMessage() {
594     if (mediaObject->state() != Phonon::PlayingState) return;
595     mediaObject->pause();
596
597     QMessageBox msgBox;
598     msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
599     msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::APP_NAME));
600     msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
601     msgBox.setModal(true);
602
603     QPushButton *quitButton = msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
604     QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
605
606     msgBox.exec();
607
608     if (msgBox.clickedButton() == buyButton) {
609         QDesktopServices::openUrl(QString(Constants::WEBSITE) + "#download");
610     } else {
611         mediaObject->play();
612         demoTimer->start(300000);
613     }
614 }
615 #endif
616
617 void MediaView::downloadVideo() {
618     Video* video = listModel->activeVideo();
619     if (!video) return;
620
621     DownloadManager::instance()->addItem(video);
622
623     // TODO animate
624
625     The::globalActions()->value("downloads")->setVisible(true);
626
627     // The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
628
629     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
630     QString message = tr("Downloading %1").arg(video->title());
631     if (mainWindow) mainWindow->statusBar()->showMessage(message);
632 }
633
634 void MediaView::fullscreen() {
635     videoAreaWidget->setParent(0);
636     videoAreaWidget->showFullScreen();
637 }
638
639 /*
640 void MediaView::setSlider(QSlider *slider) {
641     this->slider = slider;
642     // slider->setEnabled(false);
643     slider->setTracking(false);
644     // connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
645 }
646
647 void MediaView::sliderMoved(int value) {
648     qDebug() << __func__;
649     int sliderPercent = (value * 100) / (slider->maximum() - slider->minimum());
650     qDebug() << slider->minimum() << value << slider->maximum();
651     if (sliderPercent <= downloadItem->currentPercent()) {
652         qDebug() << sliderPercent << downloadItem->currentPercent();
653         mediaObject->seek(value);
654     } else {
655         seekTo(value);
656     }
657 }
658
659 void MediaView::seekTo(int value) {
660     qDebug() << __func__;
661     mediaObject->pause();
662     workaroundTimer->stop();
663     errorTimer->stop();
664     // mediaObject->clear();
665
666     QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
667     QString tempFile = tempDir + "/minitube" + QString::number(value) + ".mp4";
668     if (!QFile::remove(tempFile)) {
669         qDebug() << "Cannot remove temp file";
670     }
671     Video *videoCopy = downloadItem->getVideo()->clone();
672     QUrl streamUrl = videoCopy->getStreamUrl();
673     streamUrl.addQueryItem("begin", QString::number(value));
674     if (downloadItem) delete downloadItem;
675     downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
676     connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
677     // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
678     downloadItem->start();
679
680     // slider->setMinimum(value);
681
682 }
683
684 */