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