1 #include "MainWindow.h"
4 #include "iconloader/qticonloader.h"
7 MainWindow::MainWindow() {
14 history = new QStack<QWidget*>();
15 views = new QStackedWidget(this);
18 searchView = new SearchView(this);
19 connect(searchView, SIGNAL(search(QString)), this, SLOT(showMedia(QString)));
20 views->addWidget(searchView);
21 mediaView = new MediaView(this);
22 views->addWidget(mediaView);
24 // lazily initialized views
28 toolbarSearch = new SearchLineEdit(this);
29 toolbarSearch->setFont(qApp->font());
30 connect(toolbarSearch, SIGNAL(search(const QString&)), searchView, SLOT(watch(const QString&)));
38 // remove that useless menu/toolbar context menu
39 this->setContextMenuPolicy(Qt::NoContextMenu);
41 // mediaView init stuff thats needs actions
42 mediaView->initialize();
44 // restore window position
47 // show the initial view
48 showWidget(searchView);
50 setCentralWidget(views);
53 MainWindow::~MainWindow() {
57 void MainWindow::createActions() {
59 QMap<QString, QAction*> *actions = The::globalActions();
62 settingsAct = new QAction(tr("&Preferences..."), this);
63 settingsAct->setStatusTip(tr(QString("Configure ").append(Constants::APP_NAME).toUtf8()));
65 settingsAct->setMenuRole(QAction::PreferencesRole);
66 actions->insert("settings", settingsAct);
67 connect(settingsAct, SIGNAL(triggered()), this, SLOT(showSettings()));
70 backAct = new QAction(QIcon(":/images/go-previous.png"), tr("&Back"), this);
71 backAct->setEnabled(false);
72 backAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Left));
73 backAct->setStatusTip(tr("Go to the previous view"));
74 actions->insert("back", backAct);
75 connect(backAct, SIGNAL(triggered()), this, SLOT(goBack()));
77 stopAct = new QAction(QtIconLoader::icon("media-stop", QIcon(":/images/stop.png")), tr("&Stop"), this);
78 stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
79 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
80 actions->insert("stop", stopAct);
81 connect(stopAct, SIGNAL(triggered()), this, SLOT(stop()));
83 skipAct = new QAction(QtIconLoader::icon("media-skip-forward", QIcon(":/images/skip.png")), tr("S&kip"), this);
84 skipAct->setStatusTip(tr("Skip to the next video"));
85 skipAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Right) << QKeySequence(Qt::Key_MediaNext));
86 skipAct->setEnabled(false);
87 actions->insert("skip", skipAct);
88 connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
90 pauseAct = new QAction(QtIconLoader::icon("media-pause", QIcon(":/images/pause.png")), tr("&Pause"), this);
91 pauseAct->setStatusTip(tr("Pause playback"));
92 pauseAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Space) << QKeySequence(Qt::Key_MediaPlay));
93 pauseAct->setEnabled(false);
94 actions->insert("pause", pauseAct);
95 connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
97 fullscreenAct = new QAction(QtIconLoader::icon("view-fullscreen", QIcon(":/images/view-fullscreen.png")), tr("&Full Screen"), this);
98 fullscreenAct->setStatusTip(tr("Go full screen"));
99 fullscreenAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Return));
100 fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
101 actions->insert("fullscreen", fullscreenAct);
102 connect(fullscreenAct, SIGNAL(triggered()), this, SLOT(fullscreen()));
104 compactViewAct = new QAction(tr("&Compact mode"), this);
105 compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
106 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
107 compactViewAct->setCheckable(true);
108 compactViewAct->setChecked(false);
109 compactViewAct->setEnabled(false);
110 actions->insert("compactView", compactViewAct);
111 connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
114 // icon should be document-save but it is ugly
115 downloadAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("&Download"), this);
116 downloadAct->setStatusTip(tr("Download this video"));
117 downloadAct->setShortcut(tr("Ctrl+S"));
118 downloadAct->setEnabled(false);
119 actions->insert("download", downloadAct);
120 connect(downloadAct, SIGNAL(triggered()), this, SLOT(download()));
123 webPageAct = new QAction(QtIconLoader::icon("internet-web-browser", QIcon(":/images/internet-web-browser.png")), tr("&YouTube"), this);
124 webPageAct->setStatusTip(tr("Open the YouTube video page"));
125 webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
126 webPageAct->setEnabled(false);
127 actions->insert("webpage", webPageAct);
128 connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
130 removeAct = new QAction(tr("&Remove"), this);
131 removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
132 removeAct->setShortcuts(QList<QKeySequence>() << QKeySequence("Del") << QKeySequence("Backspace"));
133 removeAct->setEnabled(false);
134 actions->insert("remove", removeAct);
135 connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
137 moveUpAct = new QAction(QtIconLoader::icon("go-up", QIcon(":/images/go-up.png")), tr("Move &Up"), this);
138 moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
139 moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
140 moveUpAct->setEnabled(false);
141 actions->insert("moveUp", moveUpAct);
142 connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
144 moveDownAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("Move &Down"), this);
145 moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
146 moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
147 moveDownAct->setEnabled(false);
148 actions->insert("moveDown", moveDownAct);
149 connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
151 quitAct = new QAction(tr("&Quit"), this);
152 quitAct->setMenuRole(QAction::QuitRole);
153 quitAct->setShortcut(tr("Ctrl+Q"));
154 quitAct->setStatusTip(tr("Bye"));
155 actions->insert("quit", quitAct);
156 connect(quitAct, SIGNAL(triggered()), this, SLOT(quit()));
158 siteAct = new QAction(tr("&Website"), this);
159 siteAct->setShortcut(QKeySequence::HelpContents);
160 siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::APP_NAME));
161 actions->insert("site", siteAct);
162 connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
164 donateAct = new QAction(tr("&Donate via PayPal"), this);
165 donateAct->setStatusTip(tr("Please support the continued development of %1").arg(Constants::APP_NAME));
166 actions->insert("donate", donateAct);
167 connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
169 aboutAct = new QAction(tr("&About"), this);
170 aboutAct->setMenuRole(QAction::AboutRole);
171 aboutAct->setStatusTip(tr("Info about %1").arg(Constants::APP_NAME));
172 actions->insert("about", aboutAct);
173 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
177 searchFocusAct = new QAction(this);
178 searchFocusAct->setShortcut(QKeySequence::Find);
179 actions->insert("search", searchFocusAct);
180 connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
181 addAction(searchFocusAct);
183 volumeUpAct = new QAction(this);
184 volumeUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Plus));
185 actions->insert("volume-up", volumeUpAct);
186 connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp()));
187 addAction(volumeUpAct);
189 volumeDownAct = new QAction(this);
190 volumeDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Minus));
191 actions->insert("volume-down", volumeDownAct);
192 connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown()));
193 addAction(volumeDownAct);
195 volumeMuteAct = new QAction(this);
196 volumeMuteAct->setShortcut(tr("Ctrl+M"));
197 actions->insert("volume-mute", volumeMuteAct);
198 connect(volumeMuteAct, SIGNAL(triggered()), this, SLOT(volumeMute()));
199 addAction(volumeMuteAct);
201 // common action properties
202 foreach (QAction *action, actions->values()) {
204 // add actions to the MainWindow so that they work
205 // when the menu is hidden
209 // unexperienced users tend to keep keys pressed for a "long" time
210 action->setAutoRepeat(false);
211 action->setToolTip(action->statusTip());
213 // make the actions work when video is fullscreen
214 action->setShortcutContext(Qt::ApplicationShortcut);
217 // OSX does not use icons in menus
218 action->setIconVisibleInMenu(false);
225 void MainWindow::createMenus() {
227 QMap<QString, QMenu*> *menus = The::globalMenus();
229 fileMenu = menuBar()->addMenu(tr("&Application"));
230 // menus->insert("file", fileMenu);
231 // fileMenu->addAction(settingsAct);
232 fileMenu->addSeparator();
233 fileMenu->addAction(quitAct);
235 playlistMenu = menuBar()->addMenu(tr("&Playlist"));
236 menus->insert("playlist", playlistMenu);
237 playlistMenu->addAction(removeAct);
238 playlistMenu->addSeparator();
239 playlistMenu->addAction(moveUpAct);
240 playlistMenu->addAction(moveDownAct);
242 viewMenu = menuBar()->addMenu(tr("&Video"));
243 menus->insert("video", viewMenu);
244 // viewMenu->addAction(backAct);
245 viewMenu->addAction(stopAct);
246 viewMenu->addAction(pauseAct);
247 viewMenu->addAction(skipAct);
248 viewMenu->addSeparator();
249 viewMenu->addAction(webPageAct);
250 viewMenu->addSeparator();
251 // viewMenu->addAction(downloadAct);
252 viewMenu->addAction(compactViewAct);
253 viewMenu->addAction(fullscreenAct);
255 helpMenu = menuBar()->addMenu(tr("&Help"));
256 helpMenu->addAction(siteAct);
257 helpMenu->addAction(donateAct);
258 helpMenu->addAction(aboutAct);
261 void MainWindow::createToolBars() {
263 setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
265 mainToolBar = new QToolBar(this);
266 mainToolBar->setFloatable(false);
267 mainToolBar->setMovable(false);
270 smallerFont.setPointSize(smallerFont.pointSize()*.85);
271 mainToolBar->setFont(smallerFont);
273 mainToolBar->setIconSize(QSize(32,32));
274 // mainToolBar->addAction(backAct);
275 mainToolBar->addAction(stopAct);
276 mainToolBar->addAction(pauseAct);
277 mainToolBar->addAction(skipAct);
278 mainToolBar->addAction(fullscreenAct);
280 seekSlider = new Phonon::SeekSlider(this);
281 seekSlider->setIconVisible(false);
282 Spacer *seekSliderSpacer = new Spacer(mainToolBar, seekSlider);
283 seekSliderSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
284 mainToolBar->addWidget(seekSliderSpacer);
286 volumeSlider = new Phonon::VolumeSlider(this);
287 // this makes the volume slider smaller
288 volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
289 mainToolBar->addWidget(new Spacer(mainToolBar, volumeSlider));
291 mainToolBar->addWidget(new Spacer(mainToolBar, toolbarSearch));
293 addToolBar(mainToolBar);
296 void MainWindow::createStatusBar() {
297 currentTime = new QLabel(this);
298 statusBar()->addPermanentWidget(currentTime);
300 totalTime = new QLabel(this);
301 statusBar()->addPermanentWidget(totalTime);
303 // remove ugly borders on OSX
304 statusBar()->setStyleSheet("::item{border:0 solid}");
309 void MainWindow::readSettings() {
311 restoreGeometry(settings.value("geometry").toByteArray());
314 void MainWindow::writeSettings() {
315 // do not save geometry when in full screen
319 settings.setValue("geometry", saveGeometry());
322 void MainWindow::goBack() {
323 if ( history->size() > 1 ) {
325 QWidget *widget = history->pop();
330 void MainWindow::showWidget ( QWidget* widget ) {
332 setUpdatesEnabled(false);
334 // call hide method on the current view
335 View* oldView = dynamic_cast<View *> (views->currentWidget());
336 if (oldView != NULL) {
337 oldView->disappear();
340 // call show method on the new view
341 View* newView = dynamic_cast<View *> (widget);
342 if (newView != NULL) {
344 QMap<QString,QVariant> metadata = newView->metadata();
345 QString windowTitle = metadata.value("title").toString();
346 if (windowTitle.length())
347 windowTitle += " - ";
348 setWindowTitle(windowTitle + Constants::APP_NAME);
349 statusBar()->showMessage((metadata.value("description").toString()));
353 // backAct->setEnabled(history->size() > 1);
354 // settingsAct->setEnabled(widget != settingsView);
355 stopAct->setEnabled(widget == mediaView);
356 fullscreenAct->setEnabled(widget == mediaView);
357 compactViewAct->setEnabled(widget == mediaView);
358 webPageAct->setEnabled(widget == mediaView);
359 aboutAct->setEnabled(widget != aboutView);
362 // this is not the best place to enable downloads, but the user is informed
363 // if there really is no video is playing
364 downloadAct->setEnabled(widget == mediaView);
367 // cool toolbar on the Mac
368 // setUnifiedTitleAndToolBarOnMac(widget == mediaView);
370 // toolbar only for the mediaView
371 mainToolBar->setVisible(widget == mediaView && !compactViewAct->isChecked());
373 history->push(widget);
376 // crossfade only on OSX
377 // where we can be sure of video performance
378 fadeInWidget(views->currentWidget(), widget);
381 views->setCurrentWidget(widget);
383 setUpdatesEnabled(true);
386 void MainWindow::fadeInWidget(QWidget *oldWidget, QWidget *newWidget) {
387 if (faderWidget) faderWidget->close();
388 if (!oldWidget || !newWidget || oldWidget == mediaView || newWidget == mediaView) return;
389 QPixmap frozenView = QPixmap::grabWidget(oldWidget);
390 faderWidget = new FaderWidget(newWidget);
391 faderWidget->start(frozenView);
394 void MainWindow::about() {
396 aboutView = new AboutView(this);
397 views->addWidget(aboutView);
399 showWidget(aboutView);
402 void MainWindow::visitSite() {
403 QUrl url(Constants::WEBSITE);
404 statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
405 QDesktopServices::openUrl(url);
408 void MainWindow::donate() {
409 QUrl url(QString(Constants::WEBSITE) + "#donate");
410 statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
411 QDesktopServices::openUrl(url);
414 void MainWindow::quit() {
419 void MainWindow::closeEvent(QCloseEvent *event) {
421 QWidget::closeEvent(event);
424 void MainWindow::showSettings() {
426 settingsView = new SettingsView(this);
427 views->addWidget(settingsView);
429 showWidget(settingsView);
432 void MainWindow::showSearch() {
433 showWidget(searchView);
434 currentTime->clear();
438 void MainWindow::showMedia(QString query) {
440 mediaView->setMediaObject(mediaObject);
441 SearchParams *searchParams = new SearchParams();
442 searchParams->setKeywords(query);
443 mediaView->search(searchParams);
444 showWidget(mediaView);
447 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */) {
449 // qDebug() << "Phonon state: " << newState;
453 case Phonon::ErrorState:
454 if (mediaObject->errorType() == Phonon::FatalError) {
455 statusBar()->showMessage(tr("Fatal error: %1").arg(mediaObject->errorString()));
457 statusBar()->showMessage(tr("Error: %1").arg(mediaObject->errorString()));
461 case Phonon::PlayingState:
462 pauseAct->setEnabled(true);
463 pauseAct->setIcon(QtIconLoader::icon("media-pause", QIcon(":/images/pause.png")));
464 pauseAct->setText(tr("&Pause"));
465 pauseAct->setStatusTip(tr("Pause playback"));
466 skipAct->setEnabled(true);
469 case Phonon::StoppedState:
470 pauseAct->setEnabled(false);
471 skipAct->setEnabled(false);
474 case Phonon::PausedState:
475 skipAct->setEnabled(true);
476 pauseAct->setEnabled(true);
477 pauseAct->setIcon(QtIconLoader::icon("media-play", QIcon(":/images/play.png")));
478 pauseAct->setText(tr("&Play"));
479 pauseAct->setStatusTip(tr("Resume playback"));
482 case Phonon::BufferingState:
483 case Phonon::LoadingState:
484 skipAct->setEnabled(true);
485 pauseAct->setEnabled(false);
486 currentTime->clear();
495 void MainWindow::stop() {
500 void MainWindow::fullscreen() {
502 setUpdatesEnabled(false);
505 // use setShortucs instead of setShortcut
506 // the latter seems not to work
507 fullscreenAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::ALT + Qt::Key_Return));
508 fullscreenAct->setText(tr("&Full Screen"));
509 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
510 if (m_maximized) showMaximized();
513 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
514 fullscreenAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::ALT + Qt::Key_Return));
515 fullscreenAct->setText(tr("Exit &Full Screen"));
516 m_maximized = isMaximized();
518 // save geometry now, if the user quits when in full screen
519 // geometry won't be saved
525 // No compact view action when in full screen
526 compactViewAct->setVisible(m_fullscreen);
527 // Also no Youtube action since it opens a new window
528 webPageAct->setVisible(m_fullscreen);
530 // Hide anything but the video
531 mediaView->setPlaylistVisible(m_fullscreen);
532 mainToolBar->setVisible(m_fullscreen);
533 statusBar()->setVisible(m_fullscreen);
534 menuBar()->setVisible(m_fullscreen);
536 m_fullscreen = !m_fullscreen;
538 setUpdatesEnabled(true);
541 void MainWindow::compactView(bool enable) {
543 setUpdatesEnabled(false);
545 // setUnifiedTitleAndToolBarOnMac(!enable);
546 mediaView->setPlaylistVisible(!enable);
547 mainToolBar->setVisible(!enable);
548 statusBar()->setVisible(!enable);
550 // ensure focus does not end up to the search box
551 // as it would steal the Space shortcut
552 toolbarSearch->setEnabled(!enable);
555 stopAct->setShortcut(QString(""));
556 QList<QKeySequence> shortcuts;
557 // for some reason it is important that ESC comes first
558 shortcuts << QKeySequence(Qt::CTRL + Qt::Key_Return) << QKeySequence(Qt::Key_Escape);
559 compactViewAct->setShortcuts(shortcuts);
561 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
562 stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
565 setUpdatesEnabled(true);
568 void MainWindow::searchFocus() {
569 QWidget *view = views->currentWidget();
570 if (view == mediaView) {
571 toolbarSearch->setFocus();
575 void MainWindow::initPhonon() {
576 // Phonon initialization
577 if (mediaObject) delete mediaObject;
578 if (audioOutput) delete audioOutput;
579 mediaObject = new Phonon::MediaObject(this);
580 mediaObject->setTickInterval(100);
581 connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
582 this, SLOT(stateChanged(Phonon::State, Phonon::State)));
583 connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
584 connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
585 seekSlider->setMediaObject(mediaObject);
586 audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
587 connect(audioOutput, SIGNAL(volumeChanged(qreal)), this, SLOT(volumeChanged(qreal)));
588 connect(audioOutput, SIGNAL(mutedChanged(bool)), this, SLOT(volumeMutedChanged(bool)));
589 volumeSlider->setAudioOutput(audioOutput);
590 Phonon::createPath(mediaObject, audioOutput);
593 void MainWindow::tick(qint64 time) {
595 currentTime->clear();
598 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
599 currentTime->setText(displayTime.toString("mm:ss"));
600 // qDebug() << "currentTime" << time << displayTime.toString("mm:ss");
603 void MainWindow::totalTimeChanged(qint64 time) {
608 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
609 totalTime->setText(displayTime.toString("/ mm:ss"));
610 // qDebug() << "totalTime" << time << displayTime.toString("mm:ss");
613 void MainWindow::volumeUp() {
614 qreal newVolume = volumeSlider->audioOutput()->volume() + .1;
615 if (newVolume > volumeSlider->maximumVolume())
616 newVolume = volumeSlider->maximumVolume();
617 volumeSlider->audioOutput()->setVolume(newVolume);
620 void MainWindow::volumeDown() {
621 qreal newVolume = volumeSlider->audioOutput()->volume() - .1;
624 volumeSlider->audioOutput()->setVolume(newVolume);
627 void MainWindow::volumeMute() {
628 volumeSlider->audioOutput()->setMuted(!volumeSlider->audioOutput()->isMuted());
631 void MainWindow::volumeChanged(qreal newVolume) {
632 // automatically unmute when volume changes
633 if (volumeSlider->audioOutput()->isMuted())
634 volumeSlider->audioOutput()->setMuted(false);
635 statusBar()->showMessage(tr("Volume at %1%").arg(newVolume*100));
638 void MainWindow::volumeMutedChanged(bool muted) {
640 statusBar()->showMessage(tr("Volume is muted"));
642 statusBar()->showMessage(tr("Volume is unmuted"));
646 void MainWindow::abortDownload() {
647 QProgressDialog* dlg = dynamic_cast<QProgressDialog*>(this->sender());
648 QMap<QNetworkReply*, DownloadResource>::iterator cur;
649 QMap<QNetworkReply*, DownloadResource>::iterator end;
650 // locate the DownloadResource by its dialog address and trigger abortion
651 for(cur=m_downloads.begin(), end=m_downloads.end(); cur!=end; cur++){
652 if(cur.value().dialog == dlg) cur.key()->abort();
656 void MainWindow::download() {
657 if(mediaObject == NULL || mediaObject->currentSource().url().isEmpty()){
658 // complain unless video source apperas to be valid
659 QMessageBox::critical(this, tr("No Video playing"), tr("You must first play the video you intent to download !"));
662 QString filename = QFileDialog::getSaveFileName(this,
663 tr("Save video as..."),
664 tr("minitube video.mp4"),
665 "Video File(*.avi *.mp4)"
667 if(!filename.isNull()) {
668 // open destination file and initialize download
669 DownloadResource res;
670 res.file = new QFile(filename);
671 if(res.file->open(QFile::WriteOnly) == true) {
672 res.dialog = new QProgressDialog(tr("Downloading: ") + res.file->fileName(),
673 tr("Abort Download"), 0, 100, this);
674 connect(res.dialog, SIGNAL(canceled()), this, SLOT(abortDownload()));
675 download(mediaObject->currentSource().url(), res);
677 QMessageBox::critical(this, tr("File creation failed"), res.file->errorString());
683 void MainWindow::download(const QUrl& url, const DownloadResource& res) {
684 // create and store request and connect the reply signals
685 QNetworkReply *r = The::networkAccessManager()->get(QNetworkRequest(url));
686 m_downloads.insert(r, res);
687 connect(r, SIGNAL(finished()), this, SLOT(replyFinished()));
688 connect(r, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
689 connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(replyError(QNetworkReply::NetworkError)));
690 connect(r, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(replyDownloadProgress(qint64,qint64)));
691 connect(r, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
694 void MainWindow::replyReadyRead() {
695 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
696 m_downloads[r].file->write(r->readAll());
699 void MainWindow::replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
700 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
701 if (bytesTotal > 0 && bytesReceived >0)
702 m_downloads[r].dialog->setValue( double(100.0/bytesTotal)*bytesReceived ); // pssst :-X
705 void MainWindow::replyError(QNetworkReply::NetworkError code) {
706 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
707 QMessageBox::critical(this, tr("Download failed"), r->errorString());
710 void MainWindow::replyFinished() {
711 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
712 m_downloads[r].dialog->close();
713 m_downloads[r].file->close();
714 delete m_downloads[r].file;
715 m_downloads.remove(r);
718 void MainWindow::replyMetaDataChanged() {
719 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
720 QUrl url = r->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
722 // redirect - request new url, but keep the resources
723 qDebug() << "redirecting to: " << url.toString();
724 download(url, m_downloads[r]);
725 m_downloads.remove(r);