]> git.sur5r.net Git - minitube/blob - src/MainWindow.cpp
cc967d9db70c8e272910455693538cd17288f307
[minitube] / src / MainWindow.cpp
1 #include "MainWindow.h"
2 #include "spacer.h"
3 #include "Constants.h"
4 #include "iconloader/qticonloader.h"
5 #include "global.h"
6
7 MainWindow::MainWindow() {
8     m_fullscreen = false;
9     mediaObject = 0;
10     audioOutput = 0;
11
12     // views mechanism
13     history = new QStack<QWidget*>();
14     views = new QStackedWidget(this);
15
16     // views
17     searchView = new SearchView(this);
18     connect(searchView, SIGNAL(search(QString)), this, SLOT(showMedia(QString)));
19     views->addWidget(searchView);
20     mediaView = new MediaView(this);
21     views->addWidget(mediaView);
22
23     // lazily initialized views
24     aboutView = 0;
25     settingsView = 0;
26
27     toolbarSearch = new SearchLineEdit(this);
28     toolbarSearch->setFont(qApp->font());
29     connect(toolbarSearch, SIGNAL(search(const QString&)), searchView, SLOT(watch(const QString&)));
30
31     // build ui
32     createActions();
33     createMenus();
34     createToolBars();
35     createStatusBar();
36
37     // remove that useless menu/toolbar context menu
38     this->setContextMenuPolicy(Qt::NoContextMenu);
39
40     // mediaView init stuff thats needs actions
41     mediaView->initialize();
42
43     // restore window position
44     readSettings();
45
46     // show the initial view
47     showWidget(searchView);
48
49     setCentralWidget(views);
50 }
51
52 MainWindow::~MainWindow() {
53     delete history;
54 }
55
56 void MainWindow::createActions() {
57
58     QMap<QString, QAction*> *actions = The::globalActions();
59
60     /*
61     settingsAct = new QAction(tr("&Preferences..."), this);
62     settingsAct->setStatusTip(tr(QString("Configure ").append(Constants::APP_NAME).toUtf8()));
63     // Mac integration
64     settingsAct->setMenuRole(QAction::PreferencesRole);
65     actions->insert("settings", settingsAct);
66     connect(settingsAct, SIGNAL(triggered()), this, SLOT(showSettings()));
67     */
68     
69     backAct = new QAction(QIcon(":/images/go-previous.png"), tr("&Back"), this);
70     backAct->setEnabled(false);
71     backAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Left));
72     backAct->setStatusTip(tr("Go to the previous view"));
73     actions->insert("back", backAct);
74     connect(backAct, SIGNAL(triggered()), this, SLOT(goBack()));
75
76     stopAct = new QAction(QtIconLoader::icon("media-stop", QIcon(":/images/stop.png")), tr("&Stop"), this);
77     stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
78     stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
79     actions->insert("stop", stopAct);
80     connect(stopAct, SIGNAL(triggered()), this, SLOT(stop()));
81
82     skipAct = new QAction(QtIconLoader::icon("media-skip-forward", QIcon(":/images/skip.png")), tr("S&kip"), this);
83     skipAct->setStatusTip(tr("Skip to the next video"));
84     skipAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
85     skipAct->setEnabled(false);
86     actions->insert("skip", skipAct);
87     connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
88
89     pauseAct = new QAction(QtIconLoader::icon("media-pause", QIcon(":/images/pause.png")), tr("&Pause"), this);
90     pauseAct->setStatusTip(tr("Pause playback"));
91     pauseAct->setShortcut(QKeySequence(Qt::Key_Space));
92     pauseAct->setEnabled(false);
93     actions->insert("pause", pauseAct);
94     connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
95
96     fullscreenAct = new QAction(QtIconLoader::icon("view-fullscreen", QIcon(":/images/view-fullscreen.png")), tr("&Full Screen"), this);
97     fullscreenAct->setStatusTip(tr("Go full screen"));
98     fullscreenAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Return));
99     fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
100     actions->insert("fullscreen", fullscreenAct);
101     connect(fullscreenAct, SIGNAL(triggered()), this, SLOT(fullscreen()));
102
103     compactViewAct = new QAction(tr("&Compact mode"), this);
104     compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
105     compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
106     compactViewAct->setCheckable(true);
107     compactViewAct->setChecked(false);
108     compactViewAct->setEnabled(false);
109     actions->insert("compactView", compactViewAct);
110     connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
111
112     // icon should be document-save but it is ugly
113     downloadAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("&Download"), this);
114     downloadAct->setStatusTip(tr("Download this video"));
115     downloadAct->setShortcut(tr("Ctrl+S"));
116     downloadAct->setEnabled(false);
117     actions->insert("download", downloadAct);
118     connect(downloadAct, SIGNAL(triggered()), this, SLOT(download()));
119
120     webPageAct = new QAction(QtIconLoader::icon("internet-web-browser", QIcon(":/images/internet-web-browser.png")), tr("&YouTube"), this);
121     webPageAct->setStatusTip(tr("Open the YouTube video page"));
122     webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
123     webPageAct->setEnabled(false);
124     actions->insert("webpage", webPageAct);
125     connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
126
127     removeAct = new QAction(tr("&Remove"), this);
128     removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
129     QList<QKeySequence> shortcuts;
130     shortcuts << QKeySequence("Del") << QKeySequence("Backspace");
131     removeAct->setShortcuts(shortcuts);
132     removeAct->setEnabled(false);
133     actions->insert("remove", removeAct);
134     connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
135
136     moveUpAct = new QAction(QtIconLoader::icon("go-up", QIcon(":/images/go-up.png")), tr("Move &Up"), this);
137     moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
138     moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
139     moveUpAct->setEnabled(false);
140     actions->insert("moveUp", moveUpAct);
141     connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
142
143     moveDownAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("Move &Down"), this);
144     moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
145     moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
146     moveDownAct->setEnabled(false);
147     actions->insert("moveDown", moveDownAct);
148     connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
149
150     quitAct = new QAction(tr("&Quit"), this);
151     quitAct->setMenuRole(QAction::QuitRole);
152     quitAct->setShortcut(tr("Ctrl+Q"));
153     quitAct->setStatusTip(tr("Bye"));
154     actions->insert("quit", quitAct);
155     connect(quitAct, SIGNAL(triggered()), this, SLOT(quit()));
156
157     siteAct = new QAction(tr("&Website"), this);
158     siteAct->setShortcut(QKeySequence::HelpContents);
159     siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::APP_NAME));
160     actions->insert("site", siteAct);
161     connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
162
163     donateAct = new QAction(tr("&Donate via PayPal"), this);
164     donateAct->setStatusTip(tr("Please support the continued development of %1").arg(Constants::APP_NAME));
165     actions->insert("donate", donateAct);
166     connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
167
168     aboutAct = new QAction(tr("&About"), this);
169     aboutAct->setMenuRole(QAction::AboutRole);
170     aboutAct->setStatusTip(tr("Info about %1").arg(Constants::APP_NAME));
171     actions->insert("about", aboutAct);
172     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
173
174     searchFocusAct = new QAction(tr("&Search"), this);
175     searchFocusAct->setShortcut(QKeySequence::Find);
176     actions->insert("search", searchFocusAct);
177     connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
178     addAction(searchFocusAct);
179
180     // common action properties
181     foreach (QAction *action, actions->values()) {
182
183         // add actions to the MainWindow so that they work
184         // when the menu is hidden
185         addAction(action);
186
187         // never autorepeat.
188         // unexperienced users tend to keep keys pressed for a "long" time
189         action->setAutoRepeat(false);
190         action->setToolTip(action->statusTip());
191
192         // make the actions work when video is fullscreen
193         action->setShortcutContext(Qt::ApplicationShortcut);
194
195 #ifdef Q_WS_MAC
196         // OSX does not use icons in menus
197         action->setIconVisibleInMenu(false);
198 #endif
199
200     }
201
202 }
203
204 void MainWindow::createMenus() {
205
206     QMap<QString, QMenu*> *menus = The::globalMenus();
207
208     fileMenu = menuBar()->addMenu(tr("&Application"));
209     // menus->insert("file", fileMenu);
210     /*
211     fileMenu->addAction(settingsAct);
212     fileMenu->addSeparator();
213     */
214     fileMenu->addAction(quitAct);
215
216     playlistMenu = menuBar()->addMenu(tr("&Playlist"));
217     menus->insert("playlist", playlistMenu);
218     playlistMenu->addAction(removeAct);
219     playlistMenu->addSeparator();
220     playlistMenu->addAction(moveUpAct);
221     playlistMenu->addAction(moveDownAct);
222
223     viewMenu = menuBar()->addMenu(tr("&Video"));
224     menus->insert("video", viewMenu);
225     // viewMenu->addAction(backAct);
226     viewMenu->addAction(stopAct);
227     viewMenu->addAction(pauseAct);
228     viewMenu->addAction(skipAct);
229     viewMenu->addSeparator();
230     viewMenu->addAction(webPageAct);
231     viewMenu->addSeparator();
232     viewMenu->addAction(downloadAct);
233     viewMenu->addAction(compactViewAct);
234     viewMenu->addAction(fullscreenAct);
235
236     helpMenu = menuBar()->addMenu(tr("&Help"));
237     helpMenu->addAction(siteAct);
238     helpMenu->addAction(donateAct);
239     helpMenu->addAction(aboutAct);
240 }
241
242 void MainWindow::createToolBars() {
243
244     setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
245
246     mainToolBar = new QToolBar(this);
247     mainToolBar->setFloatable(false);
248     mainToolBar->setMovable(false);
249
250     QFont smallerFont;
251     smallerFont.setPointSize(smallerFont.pointSize()*.85);
252     mainToolBar->setFont(smallerFont);
253
254     mainToolBar->setIconSize(QSize(32,32));
255     // mainToolBar->addAction(backAct);
256     mainToolBar->addAction(stopAct);
257     mainToolBar->addAction(pauseAct);
258     mainToolBar->addAction(skipAct);
259     mainToolBar->addAction(fullscreenAct);
260
261     seekSlider = new Phonon::SeekSlider(this);
262     seekSlider->setIconVisible(false);
263     Spacer *seekSliderSpacer = new Spacer(mainToolBar, seekSlider);
264     seekSliderSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
265     mainToolBar->addWidget(seekSliderSpacer);
266
267     volumeSlider = new Phonon::VolumeSlider(this);
268     // this makes the volume slider smaller
269     volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
270     mainToolBar->addWidget(new Spacer(mainToolBar, volumeSlider));
271
272     mainToolBar->addWidget(new Spacer(mainToolBar, toolbarSearch));
273
274     addToolBar(mainToolBar);
275 }
276
277 void MainWindow::createStatusBar() {
278     currentTime = new QLabel(this);
279     statusBar()->addPermanentWidget(currentTime);
280
281     totalTime = new QLabel(this);
282     statusBar()->addPermanentWidget(totalTime);
283
284     // remove ugly borders on OSX
285     statusBar()->setStyleSheet("::item{border:0 solid}");
286
287     statusBar()->show();
288 }
289
290 void MainWindow::readSettings() {
291     QSettings settings;
292     restoreGeometry(settings.value("geometry").toByteArray());
293 }
294
295 void MainWindow::writeSettings() {
296     // do not save geometry when in full screen
297     if (m_fullscreen)
298         return;
299     QSettings settings;
300     settings.setValue("geometry", saveGeometry());
301 }
302
303 void MainWindow::goBack() {
304     if ( history->size() > 1 ) {
305         history->pop();
306         QWidget *widget = history->pop();
307         showWidget(widget);
308     }
309 }
310
311 void MainWindow::showWidget ( QWidget* widget ) {
312
313     setUpdatesEnabled(false);
314
315     // call hide method on the current view
316     View* oldView = dynamic_cast<View *> (views->currentWidget());
317     if (oldView != NULL) {
318         oldView->disappear();
319     }
320
321     // call show method on the new view
322     View* newView = dynamic_cast<View *> (widget);
323     if (newView != NULL) {
324         newView->appear();
325         QMap<QString,QVariant> metadata = newView->metadata();
326         QString windowTitle = metadata.value("title").toString();
327         if (windowTitle.length())
328             windowTitle += " - ";
329         setWindowTitle(windowTitle + Constants::APP_NAME);
330         statusBar()->showMessage((metadata.value("description").toString()));
331
332     }
333
334     // backAct->setEnabled(history->size() > 1);
335     // settingsAct->setEnabled(widget != settingsView);
336     stopAct->setEnabled(widget == mediaView);
337     fullscreenAct->setEnabled(widget == mediaView);
338     compactViewAct->setEnabled(widget == mediaView);
339     webPageAct->setEnabled(widget == mediaView);
340     aboutAct->setEnabled(widget != aboutView);
341     // this is not the best place to enable downloads, but the user is informed
342     // if there really is no video is playing
343     downloadAct->setEnabled(widget == mediaView);
344
345     // cool toolbar on the Mac
346     // setUnifiedTitleAndToolBarOnMac(widget == mediaView);
347
348     // toolbar only for the mediaView
349     mainToolBar->setVisible(widget == mediaView && !compactViewAct->isChecked());
350
351     history->push(widget);
352
353 #ifdef Q_WS_MAC
354     // crossfade only on OSX
355     // where we can be sure of video performance
356     fadeInWidget(views->currentWidget(), widget);
357 #endif
358
359     views->setCurrentWidget(widget);
360
361     setUpdatesEnabled(true);
362 }
363
364 void MainWindow::fadeInWidget(QWidget *oldWidget, QWidget *newWidget) {
365     if (faderWidget) faderWidget->close();
366     if (!oldWidget || !newWidget || oldWidget == mediaView || newWidget == mediaView) return;
367     QPixmap frozenView = QPixmap::grabWidget(oldWidget);
368     faderWidget = new FaderWidget(newWidget);
369     faderWidget->start(frozenView);
370 }
371
372 void MainWindow::about() {
373     if (!aboutView) {
374         aboutView = new AboutView(this);
375         views->addWidget(aboutView);
376     }
377     showWidget(aboutView);
378 }
379
380 void MainWindow::visitSite() {
381     QUrl url(Constants::WEBSITE);
382     statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
383     QDesktopServices::openUrl(url);
384 }
385
386 void MainWindow::donate() {
387     QUrl url(QString(Constants::WEBSITE) + "#donate");
388     statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
389     QDesktopServices::openUrl(url);
390 }
391
392 void MainWindow::quit() {
393     writeSettings();
394     qApp->quit();
395 }
396
397 void MainWindow::closeEvent(QCloseEvent *event) {
398     quit();
399     QWidget::closeEvent(event);
400 }
401
402 void MainWindow::showSettings() {
403     if (!settingsView) {
404         settingsView = new SettingsView(this);
405         views->addWidget(settingsView);
406     }
407     showWidget(settingsView);
408 }
409
410 void MainWindow::showSearch() {
411     showWidget(searchView);
412     currentTime->clear();
413     totalTime->clear();
414 }
415
416 void MainWindow::showMedia(QString query) {
417     initPhonon();
418     mediaView->setMediaObject(mediaObject);
419     SearchParams *searchParams = new SearchParams();
420     searchParams->setKeywords(query);
421     mediaView->search(searchParams);
422     showWidget(mediaView);
423 }
424
425 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */) {
426
427     // qDebug() << "Phonon state: " << newState;
428
429     switch (newState) {
430
431          case Phonon::ErrorState:
432         if (mediaObject->errorType() == Phonon::FatalError) {
433             statusBar()->showMessage(tr("Fatal error: %1").arg(mediaObject->errorString()));
434         } else {
435             statusBar()->showMessage(tr("Error: %1").arg(mediaObject->errorString()));
436         }
437         break;
438
439          case Phonon::PlayingState:
440         pauseAct->setEnabled(true);
441         pauseAct->setIcon(QtIconLoader::icon("media-pause", QIcon(":/images/pause.png")));
442         pauseAct->setText(tr("&Pause"));
443         pauseAct->setStatusTip(tr("Pause playback"));
444         skipAct->setEnabled(true);
445         break;
446
447          case Phonon::StoppedState:
448         pauseAct->setEnabled(false);
449         skipAct->setEnabled(false);
450         break;
451
452          case Phonon::PausedState:
453         skipAct->setEnabled(true);
454         pauseAct->setEnabled(true);
455         pauseAct->setIcon(QtIconLoader::icon("media-play", QIcon(":/images/play.png")));
456         pauseAct->setText(tr("&Play"));
457         pauseAct->setStatusTip(tr("Resume playback"));
458         break;
459
460          case Phonon::BufferingState:
461          case Phonon::LoadingState:
462         skipAct->setEnabled(true);
463         pauseAct->setEnabled(false);
464         currentTime->clear();
465         totalTime->clear();
466         break;
467
468          default:
469         ;
470     }
471 }
472
473 void MainWindow::stop() {
474     mediaView->stop();
475     showSearch();
476 }
477
478 void MainWindow::fullscreen() {
479
480     setUpdatesEnabled(false);
481
482     if (m_fullscreen) {
483         // use setShortucs instead of setShortcut
484         // the latter seems not to work
485         QList<QKeySequence> shortcuts;
486         shortcuts << QKeySequence(Qt::ALT + Qt::Key_Return);
487         fullscreenAct->setShortcuts(shortcuts);
488         fullscreenAct->setText(tr("&Full Screen"));
489         stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
490         if (m_maximized) showMaximized();
491         else showNormal();
492     } else {
493         stopAct->setShortcut(QString(""));
494         QList<QKeySequence> shortcuts;
495         // for some reason it is important that ESC comes first
496         shortcuts << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::ALT + Qt::Key_Return);
497         fullscreenAct->setShortcuts(shortcuts);
498         fullscreenAct->setText(tr("Exit &Full Screen"));
499         m_maximized = isMaximized();
500
501         // save geometry now, if the user quits when in full screen
502         // geometry won't be saved
503         writeSettings();
504
505         showFullScreen();
506     }
507
508     // No compact view action when in full screen
509     compactViewAct->setVisible(m_fullscreen);
510     // Also no Youtube action since it opens a new window
511     webPageAct->setVisible(m_fullscreen);
512
513     // Hide anything but the video
514     mediaView->setPlaylistVisible(m_fullscreen);
515     mainToolBar->setVisible(m_fullscreen);
516     statusBar()->setVisible(m_fullscreen);
517     menuBar()->setVisible(m_fullscreen);
518
519     m_fullscreen = !m_fullscreen;
520
521     setUpdatesEnabled(true);
522 }
523
524 void MainWindow::compactView(bool enable) {
525
526     setUpdatesEnabled(false);
527
528     // setUnifiedTitleAndToolBarOnMac(!enable);
529     mediaView->setPlaylistVisible(!enable);
530     mainToolBar->setVisible(!enable);
531     statusBar()->setVisible(!enable);
532
533     // ensure focus does not end up to the search box
534     // as it would steal the Space shortcut
535     toolbarSearch->setEnabled(!enable);
536
537     if (enable) {
538         stopAct->setShortcut(QString(""));
539         QList<QKeySequence> shortcuts;
540         // for some reason it is important that ESC comes first
541         shortcuts << QKeySequence(Qt::CTRL + Qt::Key_Return) << QKeySequence(Qt::Key_Escape);
542         compactViewAct->setShortcuts(shortcuts);
543     } else {
544         compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
545         stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
546     }
547
548     setUpdatesEnabled(true);
549 }
550
551 void MainWindow::searchFocus() {
552     QWidget *view = views->currentWidget();
553     if (view == mediaView) {
554         toolbarSearch->setFocus();
555     }
556 }
557
558 void MainWindow::initPhonon() {
559     // Phonon initialization
560     if (mediaObject) delete mediaObject;
561     if (audioOutput) delete audioOutput;
562     mediaObject = new Phonon::MediaObject(this);
563     mediaObject->setTickInterval(100);
564     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
565             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
566     connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
567     connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
568     seekSlider->setMediaObject(mediaObject);
569     audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
570     volumeSlider->setAudioOutput(audioOutput);
571     Phonon::createPath(mediaObject, audioOutput);
572 }
573
574 void MainWindow::tick(qint64 time) {
575     if (time <= 0) {
576         totalTime->clear();
577         return;
578     }
579     QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
580     currentTime->setText(displayTime.toString("mm:ss"));
581     // qDebug() << "currentTime" << time << displayTime.toString("mm:ss");
582 }
583
584 void MainWindow::totalTimeChanged(qint64 time) {
585     if (time <= 0) {
586         totalTime->clear();
587         return;
588     }
589     QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
590     totalTime->setText(displayTime.toString("/ mm:ss"));
591     // qDebug() << "totalTime" << time << displayTime.toString("mm:ss");
592 }
593
594 void MainWindow::abortDownload() {
595     QProgressDialog* dlg = dynamic_cast<QProgressDialog*>(this->sender());
596     QMap<QNetworkReply*, DownloadResource>::iterator cur;
597     QMap<QNetworkReply*, DownloadResource>::iterator end;
598     // locate the DownloadResource by its dialog address and trigger abortion
599     for(cur=m_downloads.begin(), end=m_downloads.end(); cur!=end; cur++){
600         if(cur.value().dialog == dlg) cur.key()->abort();
601     }
602 }
603
604 void MainWindow::download() {
605     if(mediaObject == NULL || mediaObject->currentSource().url().isEmpty()){
606         // complain unless video source apperas to be valid
607         QMessageBox::critical(this, tr("No Video playing"), tr("You must first play the video you intent to download !"));
608         return;
609     }
610     QString filename = QFileDialog::getSaveFileName(this,
611                                                     tr("Save video as..."),
612                                                     tr("minitube video.mp4"),
613                                                     "Video File(*.avi *.mp4)"
614                                                     );
615     if(!filename.isNull()) {
616         // open destination file and initialize download
617         DownloadResource res;
618         res.file = new QFile(filename);
619         if(res.file->open(QFile::WriteOnly) == true) {
620             res.dialog = new QProgressDialog(tr("Downloading: ") + res.file->fileName(),
621                                              tr("Abort Download"), 0, 100, this);
622             connect(res.dialog, SIGNAL(canceled()), this, SLOT(abortDownload()));
623             download(mediaObject->currentSource().url(), res);
624         }else{
625             QMessageBox::critical(this, tr("File creation failed"), res.file->errorString());
626             delete res.file;
627         }
628     }
629 }
630
631 void MainWindow::download(const QUrl& url, const DownloadResource& res) {
632     // create and store request and connect the reply signals
633     QNetworkReply *r = The::networkAccessManager()->get(QNetworkRequest(url));
634     m_downloads.insert(r, res);
635     connect(r, SIGNAL(finished()), this, SLOT(replyFinished()));
636     connect(r, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
637     connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(replyError(QNetworkReply::NetworkError)));
638     connect(r, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(replyDownloadProgress(qint64,qint64)));
639     connect(r, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
640 }
641
642 void MainWindow::replyReadyRead() {
643     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
644     m_downloads[r].file->write(r->readAll());
645 }
646
647 void MainWindow::replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
648     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
649     if (bytesTotal > 0 && bytesReceived >0)
650         m_downloads[r].dialog->setValue( double(100.0/bytesTotal)*bytesReceived );  // pssst :-X
651 }
652
653 void MainWindow::replyError(QNetworkReply::NetworkError code) {
654     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
655     QMessageBox::critical(this, tr("Download failed"), r->errorString());
656 }
657
658 void MainWindow::replyFinished() {
659     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
660     m_downloads[r].dialog->close();
661     m_downloads[r].file->close();
662     delete m_downloads[r].file;
663     m_downloads.remove(r);
664 }
665
666 void MainWindow::replyMetaDataChanged() {
667     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
668     QUrl url = r->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
669     if(url.isValid()) {
670         // redirect - request new url, but keep the resources
671         qDebug() << "redirecting to: " << url.toString();
672         download(url, m_downloads[r]);
673         m_downloads.remove(r);
674     }
675 }