]> git.sur5r.net Git - minitube/blob - src/mainwindow.cpp
New upstream version 3.9.1
[minitube] / src / mainwindow.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "mainwindow.h"
22
23 #include "aboutview.h"
24 #include "downloadview.h"
25 #include "homeview.h"
26 #include "mediaview.h"
27 #include "regionsview.h"
28 #include "searchview.h"
29 #include "standardfeedsview.h"
30
31 #include "constants.h"
32 #include "fontutils.h"
33 #include "globalshortcuts.h"
34 #include "iconutils.h"
35 #include "searchparams.h"
36 #include "spacer.h"
37 #include "videodefinition.h"
38 #include "videosource.h"
39 #include "ytsearch.h"
40 #ifdef APP_LINUX
41 #include "gnomeglobalshortcutbackend.h"
42 #endif
43 #ifdef Q_OS_MAC
44 #include "mac_startup.h"
45 #include "macfullscreen.h"
46 #include "macsupport.h"
47 #include "macutils.h"
48 #endif
49 #include "downloadmanager.h"
50 #include "temporary.h"
51 #include "ytsuggester.h"
52 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
53 #include "searchlineedit_mac.h"
54 #else
55 #include "searchlineedit.h"
56 #endif
57 #ifdef APP_MAC_QMACTOOLBAR
58 #include "mactoolbar.h"
59 #endif
60 #include <iostream>
61 #ifdef APP_EXTRA
62 #include "compositefader.h"
63 #include "extra.h"
64 #include "updatedialog.h"
65 #endif
66 #ifdef APP_ACTIVATION
67 #include "activation.h"
68 #include "activationview.h"
69 #endif
70 #include "channelaggregator.h"
71 #include "database.h"
72 #include "httputils.h"
73 #include "jsfunctions.h"
74 #include "seekslider.h"
75 #include "sidebarwidget.h"
76 #include "toolbarmenu.h"
77 #include "videoarea.h"
78 #include "yt3.h"
79 #include "ytregions.h"
80
81 #include "invidious.h"
82 #include "js.h"
83 #include "videoapi.h"
84
85 #ifdef MEDIA_QTAV
86 #include "mediaqtav.h"
87 #endif
88 #ifdef MEDIA_MPV
89 #include "mediampv.h"
90 #endif
91
92 #ifdef UPDATER
93 #include "updater.h"
94 #endif
95
96 #include "subscriptionimportview.h"
97
98 namespace {
99 MainWindow *mainWindowInstance;
100 }
101
102 MainWindow *MainWindow::instance() {
103     return mainWindowInstance;
104 }
105
106 MainWindow::MainWindow()
107     : aboutView(nullptr), downloadView(nullptr), regionsView(nullptr), mainToolBar(nullptr),
108       fullScreenActive(false), compactModeActive(false), initialized(false), toolbarMenu(nullptr),
109       media(nullptr) {
110     mainWindowInstance = this;
111
112     // views mechanism
113     views = new QStackedWidget();
114     setCentralWidget(views);
115
116 #ifdef APP_EXTRA
117     Extra::windowSetup(this);
118 #endif
119
120     messageLabel = new QLabel(this);
121     messageLabel->setWordWrap(false);
122     messageLabel->setStyleSheet("padding:5px;border:0;background:palette(window)");
123     messageLabel->setAlignment(Qt::AlignCenter);
124     messageLabel->hide();
125     adjustMessageLabelPosition();
126     messageTimer = new QTimer(this);
127     messageTimer->setInterval(5000);
128     messageTimer->setSingleShot(true);
129     connect(messageTimer, SIGNAL(timeout()), SLOT(hideMessage()));
130
131     // views
132     homeView = new HomeView(this);
133     views->addWidget(homeView);
134
135     // TODO make this lazy
136     mediaView = MediaView::instance();
137     mediaView->setEnabled(false);
138     views->addWidget(mediaView);
139
140     // build ui
141     createActions();
142     createMenus();
143     createToolBar();
144     hideToolbar();
145     createStatusBar();
146
147     // remove that useless menu/toolbar context menu
148     this->setContextMenuPolicy(Qt::NoContextMenu);
149
150     // event filter to block ugly toolbar tooltips
151     qApp->installEventFilter(this);
152
153     setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
154
155     // restore window position
156     readSettings();
157
158     // fix stacked widget minimum size
159     for (int i = 0; i < views->count(); i++)
160         views->widget(i)->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
161     setMinimumWidth(0);
162
163 #ifdef APP_ACTIVATION
164     Activation::instance().initialCheck();
165 #else
166     showHome();
167 #endif
168
169     if (VideoAPI::impl() == VideoAPI::IV) {
170         Invidious::instance().initServers();
171     } else if (VideoAPI::impl() == VideoAPI::YT3) {
172         YT3::instance().initApiKeys();
173     } else if (VideoAPI::impl() == VideoAPI::JS) {
174         JS::instance().initialize(QUrl(QLatin1String(Constants::WEBSITE) + "-ws/bundle2.js"));
175         // JS::instance().initialize(QUrl("http://localhost:8000/bundle-test.js"));
176         Invidious::instance().initServers();
177     }
178
179     connect(JsFunctions::instance(), &JsFunctions::ready, this, [] {
180         auto ua = JsFunctions::instance()->string("userAgent()").toUtf8();
181         JS::instance().getNamFactory().setRequestHeaders({{"User-Agent", ua}});
182     });
183
184     QTimer::singleShot(100, this, &MainWindow::lazyInit);
185 }
186
187 void MainWindow::lazyInit() {
188     mediaView->initialize();
189     initMedia();
190     qApp->processEvents();
191
192     // CLI
193     if (qApp->arguments().size() > 1) {
194         QString query = qApp->arguments().at(1);
195         if (query.startsWith(QLatin1String("--"))) {
196             messageReceived(query);
197             qApp->quit();
198         } else {
199             SearchParams *searchParams = new SearchParams();
200             searchParams->setKeywords(query);
201             showMedia(searchParams);
202         }
203     } else
204         showMessage(tr("Make yourself comfortable"));
205
206     // Global shortcuts
207     GlobalShortcuts &shortcuts = GlobalShortcuts::instance();
208 #ifdef APP_LINUX
209     if (GnomeGlobalShortcutBackend::IsGsdAvailable())
210         shortcuts.setBackend(new GnomeGlobalShortcutBackend(&shortcuts));
211 #endif
212 #ifdef Q_OS_MAC
213     mac::MacSetup();
214 #endif
215     connect(&shortcuts, SIGNAL(PlayPause()), pauseAct, SLOT(trigger()));
216     connect(&shortcuts, SIGNAL(Stop()), this, SLOT(stop()));
217     connect(&shortcuts, SIGNAL(Next()), skipAct, SLOT(trigger()));
218     connect(&shortcuts, SIGNAL(Previous()), skipBackwardAct, SLOT(trigger()));
219     // connect(&shortcuts, SIGNAL(StopAfter()), getAction("stopafterthis"), SLOT(toggle()));
220
221     connect(DownloadManager::instance(), SIGNAL(statusMessageChanged(QString)),
222             SLOT(updateDownloadMessage(QString)));
223     connect(DownloadManager::instance(), SIGNAL(finished()), SLOT(downloadsFinished()));
224
225     setAcceptDrops(true);
226
227     fullscreenTimer = new QTimer(this);
228     fullscreenTimer->setInterval(3000);
229     fullscreenTimer->setSingleShot(true);
230     connect(fullscreenTimer, SIGNAL(timeout()), SLOT(hideFullscreenUI()));
231
232     // Hack to give focus to searchlineedit
233     View *view = qobject_cast<View *>(views->currentWidget());
234     if (view == homeView) {
235         QMetaObject::invokeMethod(views->currentWidget(), "appear");
236         const QString &desc = view->getDescription();
237         if (!desc.isEmpty()) showMessage(desc);
238     }
239
240     ChannelAggregator::instance()->start();
241
242 #ifdef UPDATER
243     Updater::instance().checkWithoutUI();
244 #endif
245
246     initialized = true;
247 }
248
249 void MainWindow::changeEvent(QEvent *e) {
250 #ifdef APP_MAC
251     if (e->type() == QEvent::WindowStateChange) {
252         getAction("minimize")->setEnabled(!isMinimized());
253     }
254 #endif
255     if (messageLabel->isVisible()) {
256         if (e->type() == QEvent::ActivationChange || e->type() == QEvent::WindowStateChange ||
257             e->type() == QEvent::WindowDeactivate || e->type() == QEvent::ApplicationStateChange) {
258             hideMessage();
259         }
260     }
261     QMainWindow::changeEvent(e);
262 }
263
264 bool MainWindow::eventFilter(QObject *obj, QEvent *e) {
265     const QEvent::Type t = e->type();
266
267 #ifndef APP_MAC
268     static bool altPressed = false;
269     if (t == QEvent::KeyRelease && altPressed) {
270         altPressed = false;
271         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
272         if (ke->key() == Qt::Key_Alt) {
273             toggleMenuVisibility();
274             return true;
275         }
276     } else if (t == QEvent::KeyPress) {
277         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
278         altPressed = ke->key() == Qt::Key_Alt;
279     }
280 #endif
281
282     if (fullScreenActive && views->currentWidget() == mediaView && t == QEvent::MouseMove &&
283         obj->isWidgetType() && qobject_cast<QWidget *>(obj)->window() == this) {
284         QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
285
286         bool toolBarVisible = mainToolBar && mainToolBar->isVisible();
287         bool sidebarVisible = mediaView->isSidebarVisible();
288
289         if (!sidebarVisible && !toolBarVisible) {
290             const int x = mouseEvent->pos().x();
291             if (x >= 0 && x < 5) {
292 #ifndef APP_MAC
293                 SidebarWidget *sidebar = mediaView->getSidebar();
294                 sidebar->resize(sidebar->width(), height());
295 #endif
296                 mediaView->setSidebarVisibility(true);
297                 sidebarVisible = true;
298             }
299         }
300
301 #ifndef APP_MAC
302         if (!toolBarVisible && !sidebarVisible) {
303             const int y = mouseEvent->pos().y();
304             if (y >= 0 && y < 5) {
305                 mainToolBar->resize(width(), mainToolBar->sizeHint().height());
306                 mainToolBar->setVisible(true);
307             }
308         }
309 #endif
310
311         // show the normal cursor
312         unsetCursor();
313         // then hide it again after a few seconds
314         fullscreenTimer->start();
315     }
316
317     if (t == QEvent::ToolTip) {
318         // kill tooltips
319         return true;
320     }
321
322     if (t == QEvent::Show && obj == toolbarMenu) {
323 #ifdef APP_MAC
324         int x = width() - toolbarMenu->sizeHint().width();
325         int y = views->y();
326 #else
327         int x = toolbarMenuButton->x() + toolbarMenuButton->width() -
328                 toolbarMenu->sizeHint().width();
329         int y = toolbarMenuButton->y() + toolbarMenuButton->height();
330 #endif
331         QPoint p(x, y);
332         toolbarMenu->move(mapToGlobal(p));
333     }
334
335     if (obj == this && t == QEvent::StyleChange) {
336         qDebug() << "Style change detected";
337         qApp->paletteChanged(qApp->palette());
338         return false;
339     }
340
341     // standard event processing
342     return QMainWindow::eventFilter(obj, e);
343 }
344
345 void MainWindow::createActions() {
346     stopAct = new QAction(tr("&Stop"), this);
347     IconUtils::setIcon(stopAct, "media-playback-stop");
348     stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
349     stopAct->setShortcuts(QList<QKeySequence>()
350                           << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
351     stopAct->setEnabled(false);
352     actionMap.insert("stop", stopAct);
353     connect(stopAct, SIGNAL(triggered()), SLOT(stop()));
354
355     skipBackwardAct = new QAction(tr("P&revious"), this);
356     skipBackwardAct->setStatusTip(tr("Go back to the previous track"));
357     skipBackwardAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
358     skipBackwardAct->setEnabled(false);
359     actionMap.insert("previous", skipBackwardAct);
360     connect(skipBackwardAct, SIGNAL(triggered()), mediaView, SLOT(skipBackward()));
361
362     skipAct = new QAction(tr("S&kip"), this);
363     IconUtils::setIcon(skipAct, "media-skip-forward");
364     skipAct->setStatusTip(tr("Skip to the next video"));
365     skipAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Right)
366                                                 << QKeySequence(Qt::Key_MediaNext));
367     skipAct->setEnabled(false);
368     actionMap.insert("skip", skipAct);
369     connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
370
371     pauseAct = new QAction(tr("&Play"), this);
372     IconUtils::setIcon(pauseAct, "media-playback-start");
373     pauseAct->setStatusTip(tr("Resume playback"));
374     pauseAct->setShortcuts(QList<QKeySequence>()
375                            << QKeySequence(Qt::Key_Space) << QKeySequence(Qt::Key_MediaPlay));
376     pauseAct->setEnabled(false);
377     actionMap.insert("pause", pauseAct);
378     connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
379
380     fullscreenAct = new QAction(tr("&Full Screen"), this);
381     IconUtils::setIcon(fullscreenAct, "view-fullscreen");
382     fullscreenAct->setStatusTip(tr("Go full screen"));
383     QList<QKeySequence> fsShortcuts;
384 #ifdef APP_MAC
385     fsShortcuts << QKeySequence(Qt::CTRL + Qt::META + Qt::Key_F);
386 #else
387     fsShortcuts << QKeySequence(Qt::Key_F11) << QKeySequence(Qt::ALT + Qt::Key_Return);
388 #endif
389     fullscreenAct->setShortcuts(fsShortcuts);
390     fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
391     fullscreenAct->setPriority(QAction::LowPriority);
392     actionMap.insert("fullscreen", fullscreenAct);
393     connect(fullscreenAct, SIGNAL(triggered()), SLOT(toggleFullscreen()));
394
395     compactViewAct = new QAction(tr("&Compact Mode"), this);
396     compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
397     compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C));
398     compactViewAct->setCheckable(true);
399     compactViewAct->setChecked(false);
400     compactViewAct->setEnabled(false);
401     actionMap.insert("compactView", compactViewAct);
402     connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
403
404     webPageAct = new QAction(tr("Open the &YouTube Page"), this);
405     webPageAct->setStatusTip(tr("Go to the YouTube video page and pause playback"));
406     webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
407     webPageAct->setEnabled(false);
408     actionMap.insert("webpage", webPageAct);
409     connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
410
411     copyPageAct = new QAction(tr("Copy the YouTube &Link"), this);
412     IconUtils::setIcon(copyPageAct, "link");
413     copyPageAct->setStatusTip(tr("Copy the current video YouTube link to the clipboard"));
414     copyPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
415     copyPageAct->setEnabled(false);
416     actionMap.insert("pagelink", copyPageAct);
417     connect(copyPageAct, SIGNAL(triggered()), mediaView, SLOT(copyWebPage()));
418
419     copyLinkAct = new QAction(tr("Copy the Video Stream &URL"), this);
420     copyLinkAct->setStatusTip(tr("Copy the current video stream URL to the clipboard"));
421     copyLinkAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
422     copyLinkAct->setEnabled(false);
423     actionMap.insert("videolink", copyLinkAct);
424     connect(copyLinkAct, SIGNAL(triggered()), mediaView, SLOT(copyVideoLink()));
425
426     findVideoPartsAct = new QAction(tr("Find Video &Parts"), this);
427     findVideoPartsAct->setStatusTip(tr("Find other video parts hopefully in the right order"));
428     findVideoPartsAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
429     findVideoPartsAct->setEnabled(false);
430     connect(findVideoPartsAct, SIGNAL(triggered()), mediaView, SLOT(findVideoParts()));
431     actionMap.insert("findVideoParts", findVideoPartsAct);
432
433     removeAct = new QAction(tr("&Remove"), this);
434     removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
435     removeAct->setShortcuts(QList<QKeySequence>()
436                             << QKeySequence("Del") << QKeySequence("Backspace"));
437     removeAct->setEnabled(false);
438     actionMap.insert("remove", removeAct);
439     connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
440
441     moveUpAct = new QAction(tr("Move &Up"), this);
442     moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
443     moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
444     moveUpAct->setEnabled(false);
445     actionMap.insert("moveUp", moveUpAct);
446     connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
447
448     moveDownAct = new QAction(tr("Move &Down"), this);
449     moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
450     moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
451     moveDownAct->setEnabled(false);
452     actionMap.insert("moveDown", moveDownAct);
453     connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
454
455     clearAct = new QAction(tr("&Clear Recent Searches"), this);
456     clearAct->setMenuRole(QAction::ApplicationSpecificRole);
457     clearAct->setShortcuts(QList<QKeySequence>()
458                            << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Delete)
459                            << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Backspace));
460     clearAct->setStatusTip(tr("Clear the search history. Cannot be undone."));
461     clearAct->setEnabled(true);
462     actionMap.insert("clearRecentKeywords", clearAct);
463     connect(clearAct, SIGNAL(triggered()), SLOT(clearRecentKeywords()));
464
465     quitAct = new QAction(tr("&Quit"), this);
466     quitAct->setMenuRole(QAction::QuitRole);
467     quitAct->setShortcut(QKeySequence(QKeySequence::Quit));
468     quitAct->setStatusTip(tr("Bye"));
469     actionMap.insert("quit", quitAct);
470     connect(quitAct, SIGNAL(triggered()), SLOT(quit()));
471
472     siteAct = new QAction(tr("&Website"), this);
473     siteAct->setShortcut(QKeySequence::HelpContents);
474     siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::NAME));
475     actionMap.insert("site", siteAct);
476     connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
477
478 #if !defined(APP_MAC) && !defined(APP_WIN)
479     donateAct = new QAction(tr("Make a &Donation"), this);
480     donateAct->setStatusTip(
481             tr("Please support the continued development of %1").arg(Constants::NAME));
482     actionMap.insert("donate", donateAct);
483     connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
484 #endif
485
486     aboutAct = new QAction(tr("&About"), this);
487     aboutAct->setMenuRole(QAction::AboutRole);
488     aboutAct->setStatusTip(tr("Info about %1").arg(Constants::NAME));
489     actionMap.insert("about", aboutAct);
490     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
491
492     // Invisible actions
493
494     searchFocusAct = new QAction(this);
495     searchFocusAct->setShortcut(QKeySequence::Find);
496     searchFocusAct->setStatusTip(tr("Search"));
497     actionMap.insert("search", searchFocusAct);
498     connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
499     addAction(searchFocusAct);
500
501     volumeUpAct = new QAction(this);
502     volumeUpAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Plus));
503     actionMap.insert("volumeUp", volumeUpAct);
504     connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp()));
505     addAction(volumeUpAct);
506
507     volumeDownAct = new QAction(this);
508     volumeDownAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Minus));
509     actionMap.insert("volumeDown", volumeDownAct);
510     connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown()));
511     addAction(volumeDownAct);
512
513     volumeMuteAct = new QAction(this);
514     IconUtils::setIcon(volumeMuteAct, "audio-volume-high");
515     volumeMuteAct->setStatusTip(tr("Mute volume"));
516     volumeMuteAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_M));
517     actionMap.insert("volumeMute", volumeMuteAct);
518     connect(volumeMuteAct, SIGNAL(triggered()), SLOT(toggleVolumeMute()));
519     addAction(volumeMuteAct);
520
521     QToolButton *definitionButton = new QToolButton(this);
522     definitionButton->setText(YT3::instance().maxVideoDefinition().getName());
523     IconUtils::setIcon(definitionButton, "video-display");
524     definitionButton->setIconSize(QSize(16, 16));
525     definitionButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
526     definitionButton->setPopupMode(QToolButton::InstantPopup);
527     QMenu *definitionMenu = new QMenu(this);
528     QActionGroup *group = new QActionGroup(this);
529     for (auto &defName : VideoDefinition::getDefinitionNames()) {
530         QAction *a = new QAction(defName);
531         a->setCheckable(true);
532         a->setActionGroup(group);
533         a->setChecked(defName == YT3::instance().maxVideoDefinition().getName());
534         connect(a, &QAction::triggered, this, [this, defName, definitionButton] {
535             setDefinitionMode(defName);
536             definitionButton->setText(defName);
537         });
538         connect(&YT3::instance(), &YT3::maxVideoDefinitionChanged, this,
539                 [defName, definitionButton](const QString &name) {
540                     if (defName == name) definitionButton->setChecked(true);
541                 });
542         definitionMenu->addAction(a);
543     }
544     definitionButton->setMenu(definitionMenu);
545     QWidgetAction *definitionAct = new QWidgetAction(this);
546     definitionAct->setDefaultWidget(definitionButton);
547     definitionAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_D));
548     actionMap.insert("definition", definitionAct);
549     addAction(definitionAct);
550
551     QAction *action;
552
553     action = new QAction(tr("&Manually Start Playing"), this);
554     IconUtils::setIcon(action, "media-playback-start");
555     action->setStatusTip(tr("Manually start playing videos"));
556     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_T));
557     action->setCheckable(true);
558     connect(action, SIGNAL(toggled(bool)), SLOT(setManualPlay(bool)));
559     actionMap.insert("manualplay", action);
560
561     action = new QAction(tr("&Downloads"), this);
562     IconUtils::setIcon(action, "document-save");
563     action->setStatusTip(tr("Show details about video downloads"));
564     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_J));
565     action->setCheckable(true);
566     connect(action, SIGNAL(toggled(bool)), SLOT(toggleDownloads(bool)));
567     actionMap.insert("downloads", action);
568
569     action = new QAction(tr("&Download"), this);
570     IconUtils::setIcon(action, "document-save");
571     action->setStatusTip(tr("Download the current video"));
572     action->setShortcut(QKeySequence::Save);
573     action->setEnabled(false);
574     action->setVisible(false);
575     action->setPriority(QAction::LowPriority);
576     connect(action, SIGNAL(triggered()), mediaView, SLOT(downloadVideo()));
577     actionMap.insert("download", action);
578
579 #ifdef APP_SNAPSHOT
580     action = new QAction(tr("Take &Snapshot"), this);
581     action->setShortcut(QKeySequence(Qt::Key_F9));
582     action->setEnabled(false);
583     actionMap.insert("snapshot", action);
584     connect(action, SIGNAL(triggered()), mediaView, SLOT(snapshot()));
585 #endif
586
587     action = new QAction(tr("&Subscribe to Channel"), this);
588     action->setProperty("originalText", action->text());
589     action->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
590     action->setEnabled(false);
591     connect(action, SIGNAL(triggered()), mediaView, SLOT(toggleSubscription()));
592     actionMap.insert("subscribeChannel", action);
593     mediaView->updateSubscriptionActionForVideo(0, false);
594
595     QString shareTip = tr("Share the current video using %1");
596
597     action = new QAction("&Twitter", this);
598     IconUtils::setIcon(action, "twitter");
599     action->setStatusTip(shareTip.arg("Twitter"));
600     action->setEnabled(false);
601     actionMap.insert("twitter", action);
602     connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaTwitter()));
603
604     action = new QAction("&Facebook", this);
605     IconUtils::setIcon(action, "facebook");
606     action->setStatusTip(shareTip.arg("Facebook"));
607     action->setEnabled(false);
608     actionMap.insert("facebook", action);
609     connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaFacebook()));
610
611     action = new QAction(tr("&Email"), this);
612     IconUtils::setIcon(action, "email");
613     action->setStatusTip(shareTip.arg(tr("Email")));
614     action->setEnabled(false);
615     actionMap.insert("email", action);
616     connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaEmail()));
617
618     action = new QAction(tr("&Close"), this);
619     action->setShortcut(QKeySequence(QKeySequence::Close));
620     actionMap.insert("close", action);
621     connect(action, SIGNAL(triggered()), SLOT(close()));
622
623     action = new QAction(Constants::NAME, this);
624     action->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_1));
625     actionMap.insert("restore", action);
626     connect(action, SIGNAL(triggered()), SLOT(restore()));
627
628     action = new QAction(tr("&Float on Top"), this);
629     IconUtils::setIcon(action, "go-top");
630     action->setCheckable(true);
631     actionMap.insert("ontop", action);
632     connect(action, SIGNAL(toggled(bool)), SLOT(floatOnTop(bool)));
633
634     action = new QAction(tr("&Stop After This Video"), this);
635     IconUtils::setIcon(action, "media-playback-stop");
636     action->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Escape));
637     action->setCheckable(true);
638     action->setEnabled(false);
639     actionMap.insert("stopafterthis", action);
640     connect(action, SIGNAL(toggled(bool)), SLOT(showStopAfterThisInStatusBar(bool)));
641
642     action = new QAction(tr("&Report an Issue..."), this);
643     actionMap.insert("reportIssue", action);
644     connect(action, SIGNAL(triggered()), SLOT(reportIssue()));
645
646     action = new QAction(tr("&Refine Search..."), this);
647     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_E));
648     action->setCheckable(true);
649     action->setEnabled(false);
650     actionMap.insert("refineSearch", action);
651
652     action = new QAction(YTRegions::defaultRegion().name, this);
653     actionMap.insert("worldwideRegion", action);
654
655     action = new QAction(YTRegions::localRegion().name, this);
656     actionMap.insert("localRegion", action);
657
658     action = new QAction(tr("More..."), this);
659     actionMap.insert("moreRegion", action);
660
661     action = new QAction(tr("&Related Videos"), this);
662     IconUtils::setIcon(action, "view-list");
663     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
664     action->setStatusTip(tr("Watch videos related to the current one"));
665     action->setEnabled(false);
666     action->setPriority(QAction::LowPriority);
667     connect(action, SIGNAL(triggered()), mediaView, SLOT(relatedVideos()));
668     actionMap.insert("relatedVideos", action);
669
670     action = new QAction(tr("Open in &Browser..."), this);
671     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));
672     action->setEnabled(false);
673     actionMap.insert("openInBrowser", action);
674     connect(action, SIGNAL(triggered()), mediaView, SLOT(openInBrowser()));
675
676     action = new QAction(tr("Restricted Mode"), this);
677     IconUtils::setIcon(action, "safesearch");
678     action->setStatusTip(tr("Hide videos that may contain inappropriate content"));
679     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K));
680     action->setCheckable(true);
681     action->setVisible(VideoAPI::impl() != VideoAPI::IV);
682     actionMap.insert("safeSearch", action);
683
684     action = new QAction(tr("Toggle &Menu Bar"), this);
685     connect(action, SIGNAL(triggered()), SLOT(toggleMenuVisibilityWithMessage()));
686     actionMap.insert("toggleMenu", action);
687
688     action = new QAction(tr("Menu"), this);
689     IconUtils::setIcon(action, "open-menu");
690     connect(action, SIGNAL(triggered()), SLOT(toggleToolbarMenu()));
691     actionMap.insert("toolbarMenu", action);
692
693     action = new QAction(tr("Import Subscriptions..."), this);
694     action->setMenuRole(QAction::ApplicationSpecificRole);
695     connect(action, &QAction::triggered, this, [this] {
696         if (!subscriptionImportView) {
697             subscriptionImportView = new SubscriptionImportView(this);
698             views->addWidget(subscriptionImportView);
699         }
700         showView(subscriptionImportView);
701     });
702     actionMap.insert("importSubscriptions", action);
703
704 #ifdef APP_MAC_STORE
705     action = new QAction(tr("&Love %1? Rate it!").arg(Constants::NAME), this);
706     actionMap.insert("appStore", action);
707     connect(action, SIGNAL(triggered()), SLOT(rateOnAppStore()));
708 #endif
709
710 #ifdef APP_ACTIVATION
711     ActivationView::createActivationAction(tr("Buy %1...").arg(Constants::NAME));
712 #endif
713
714     // common action properties
715     for (QAction *action : qAsConst(actionMap)) {
716         // add actions to the MainWindow so that they work
717         // when the menu is hidden
718         addAction(action);
719         setupAction(action);
720     }
721 }
722
723 void MainWindow::createMenus() {
724     fileMenu = menuBar()->addMenu(tr("&Application"));
725 #ifdef APP_ACTIVATION
726     QAction *buyAction = getAction("buy");
727     if (buyAction) fileMenu->addAction(buyAction);
728 #ifndef APP_MAC
729     fileMenu->addSeparator();
730 #endif
731 #endif
732     fileMenu->addAction(clearAct);
733 #ifndef APP_MAC
734     fileMenu->addSeparator();
735 #endif
736     fileMenu->addAction(quitAct);
737
738     QMenu *playbackMenu = menuBar()->addMenu(tr("&Playback"));
739     menuMap.insert("playback", playbackMenu);
740     playbackMenu->addAction(pauseAct);
741     playbackMenu->addAction(stopAct);
742     playbackMenu->addAction(getAction("stopafterthis"));
743     playbackMenu->addSeparator();
744     playbackMenu->addAction(skipAct);
745     playbackMenu->addAction(skipBackwardAct);
746     playbackMenu->addSeparator();
747     playbackMenu->addAction(getAction("manualplay"));
748 #ifdef APP_MAC
749     MacSupport::dockMenu(playbackMenu);
750 #endif
751
752     playlistMenu = menuBar()->addMenu(tr("&Playlist"));
753     menuMap.insert("playlist", playlistMenu);
754     playlistMenu->addAction(removeAct);
755     playlistMenu->addSeparator();
756     playlistMenu->addAction(moveUpAct);
757     playlistMenu->addAction(moveDownAct);
758     playlistMenu->addSeparator();
759     playlistMenu->addAction(getAction("refineSearch"));
760
761     QMenu *videoMenu = menuBar()->addMenu(tr("&Video"));
762     menuMap.insert("video", videoMenu);
763     videoMenu->addAction(getAction("relatedVideos"));
764     videoMenu->addAction(findVideoPartsAct);
765     videoMenu->addSeparator();
766     videoMenu->addAction(getAction("subscribeChannel"));
767 #ifdef APP_SNAPSHOT
768     videoMenu->addSeparator();
769     videoMenu->addAction(getAction("snapshot"));
770 #endif
771     videoMenu->addSeparator();
772     videoMenu->addAction(webPageAct);
773     videoMenu->addAction(copyLinkAct);
774     videoMenu->addAction(getAction("openInBrowser"));
775     videoMenu->addAction(getAction("download"));
776
777     QMenu *shareMenu = menuBar()->addMenu(tr("&Share"));
778     menuMap.insert("share", shareMenu);
779     shareMenu->addAction(copyPageAct);
780     shareMenu->addSeparator();
781     shareMenu->addAction(getAction("twitter"));
782     shareMenu->addAction(getAction("facebook"));
783     shareMenu->addSeparator();
784     shareMenu->addAction(getAction("email"));
785
786     QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
787     menuMap.insert("view", viewMenu);
788     viewMenu->addAction(getAction("ontop"));
789     viewMenu->addAction(compactViewAct);
790     viewMenu->addSeparator();
791     viewMenu->addAction(fullscreenAct);
792 #ifndef APP_MAC
793     viewMenu->addSeparator();
794     viewMenu->addAction(getAction("toggleMenu"));
795 #endif
796
797 #ifdef APP_MAC
798     MacSupport::windowMenu(this);
799 #endif
800
801     helpMenu = menuBar()->addMenu(tr("&Help"));
802     menuMap.insert("help", helpMenu);
803     helpMenu->addAction(siteAct);
804 #if !defined(APP_MAC) && !defined(APP_WIN)
805     helpMenu->addAction(donateAct);
806 #endif
807     helpMenu->addAction(getAction("reportIssue"));
808     helpMenu->addAction(aboutAct);
809 #ifdef UPDATER
810     helpMenu->addAction(Updater::instance().getAction());
811 #endif
812
813 #ifdef APP_MAC_STORE
814     helpMenu->addSeparator();
815     helpMenu->addAction(getAction("appStore"));
816 #endif
817 }
818
819 void MainWindow::createToolBar() {
820     // Create widgets
821     currentTimeLabel = new QLabel("00:00", this);
822
823     seekSlider = new SeekSlider(this);
824     seekSlider->setEnabled(false);
825     seekSlider->setTracking(false);
826     seekSlider->setMaximum(1000);
827     volumeSlider = new SeekSlider(this);
828     volumeSlider->setValue(volumeSlider->maximum());
829
830 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
831     SearchWrapper *searchWrapper = new SearchWrapper(this);
832     toolbarSearch = searchWrapper->getSearchLineEdit();
833 #else
834     toolbarSearch = new SearchLineEdit(this);
835 #endif
836     toolbarSearch->setMinimumWidth(toolbarSearch->fontInfo().pixelSize() * 15);
837     toolbarSearch->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
838     toolbarSearch->setSuggester(new YTSuggester(this));
839     connect(toolbarSearch, SIGNAL(search(const QString &)), SLOT(search(const QString &)));
840     connect(toolbarSearch, SIGNAL(suggestionAccepted(Suggestion *)),
841             SLOT(suggestionAccepted(Suggestion *)));
842     toolbarSearch->setStatusTip(searchFocusAct->statusTip());
843
844     // Add widgets to toolbar
845
846 #ifdef APP_MAC_QMACTOOLBAR
847     currentTimeLabel->hide();
848     toolbarSearch->hide();
849     volumeSlider->hide();
850     seekSlider->hide();
851     MacToolbar::instance().createToolbar(this);
852     return;
853 #endif
854
855     mainToolBar = new QToolBar(this);
856     mainToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
857     mainToolBar->setFloatable(false);
858     mainToolBar->setMovable(false);
859 #ifndef APP_LINUX
860     mainToolBar->setIconSize(QSize(32, 32));
861 #endif
862     mainToolBar->addAction(stopAct);
863     QToolButton *stopToolButton =
864             qobject_cast<QToolButton *>(mainToolBar->widgetForAction(stopAct));
865     if (stopToolButton) {
866         QMenu *stopMenu = new QMenu(this);
867         stopMenu->addAction(getAction("stopafterthis"));
868         stopToolButton->setMenu(stopMenu);
869         stopToolButton->setPopupMode(QToolButton::DelayedPopup);
870     }
871     mainToolBar->addAction(pauseAct);
872     mainToolBar->addAction(skipAct);
873     mainToolBar->addAction(getAction("relatedVideos"));
874
875     bool addFullScreenAct = true;
876 #ifdef Q_OS_MAC
877     addFullScreenAct = !mac::CanGoFullScreen(winId());
878 #endif
879     if (addFullScreenAct) mainToolBar->addAction(fullscreenAct);
880
881     mainToolBar->addWidget(new Spacer());
882
883     currentTimeLabel->setFont(FontUtils::small());
884     currentTimeLabel->setMinimumWidth(currentTimeLabel->fontInfo().pixelSize() * 4);
885     currentTimeLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
886     mainToolBar->addWidget(currentTimeLabel);
887
888 #ifdef APP_WIN
889     mainToolBar->addWidget(new Spacer(nullptr, 10));
890 #endif
891
892     seekSlider->setOrientation(Qt::Horizontal);
893     seekSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
894     seekSlider->setFocusPolicy(Qt::NoFocus);
895     mainToolBar->addWidget(seekSlider);
896
897     mainToolBar->addWidget(new Spacer());
898
899     mainToolBar->addAction(volumeMuteAct);
900 #ifndef APP_MAC_QMACTOOLBAR
901     QToolButton *volumeMuteButton =
902             qobject_cast<QToolButton *>(mainToolBar->widgetForAction(volumeMuteAct));
903     volumeMuteButton->setIconSize(QSize(16, 16));
904     auto fixVolumeMuteIconSize = [volumeMuteButton] {
905         volumeMuteButton->setIcon(volumeMuteButton->icon().pixmap(16));
906     };
907     fixVolumeMuteIconSize();
908     volumeMuteButton->connect(volumeMuteAct, &QAction::changed, volumeMuteButton,
909                               fixVolumeMuteIconSize);
910 #endif
911
912     volumeSlider->setStatusTip(
913             tr("Press %1 to raise the volume, %2 to lower it")
914                     .arg(volumeUpAct->shortcut().toString(QKeySequence::NativeText),
915                          volumeDownAct->shortcut().toString(QKeySequence::NativeText)));
916
917     volumeSlider->setOrientation(Qt::Horizontal);
918     // this makes the volume slider smaller
919     volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
920     volumeSlider->setFocusPolicy(Qt::NoFocus);
921     mainToolBar->addWidget(volumeSlider);
922
923     mainToolBar->addWidget(new Spacer());
924
925 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
926     mainToolBar->addWidget(searchWrapper);
927 #else
928     mainToolBar->addWidget(toolbarSearch);
929     mainToolBar->addWidget(new Spacer(this, toolbarSearch->height() / 2));
930
931     QAction *toolbarMenuAction = getAction("toolbarMenu");
932     mainToolBar->addAction(toolbarMenuAction);
933     toolbarMenuButton =
934             qobject_cast<QToolButton *>(mainToolBar->widgetForAction(toolbarMenuAction));
935 #endif
936
937     addToolBar(mainToolBar);
938 }
939
940 void MainWindow::createStatusBar() {
941     statusToolBar = new QToolBar(statusBar());
942     statusToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
943     statusToolBar->setIconSize(QSize(16, 16));
944
945     regionAction = new QAction(this);
946     regionAction->setStatusTip(tr("Choose your content location"));
947
948     QAction *localAction = getAction("localRegion");
949     if (!localAction->text().isEmpty()) {
950         QMenu *regionMenu = new QMenu(this);
951         regionMenu->addAction(getAction("worldwideRegion"));
952         regionMenu->addAction(localAction);
953         regionMenu->addSeparator();
954         QAction *moreRegionsAction = getAction("moreRegion");
955         regionMenu->addAction(moreRegionsAction);
956         connect(moreRegionsAction, SIGNAL(triggered()), SLOT(showRegionsView()));
957         regionAction->setMenu(regionMenu);
958     }
959     connect(regionAction, SIGNAL(triggered()), SLOT(showRegionsView()));
960
961     /* Stupid code that generates the QRC items
962     foreach(YTRegion r, YTRegions::list())
963         qDebug() << QString("<file>flags/%1.png</file>").arg(r.id.toLower());
964     */
965
966     statusBar()->addPermanentWidget(statusToolBar);
967     statusBar()->hide();
968 }
969
970 void MainWindow::showStopAfterThisInStatusBar(bool show) {
971     QAction *action = getAction("stopafterthis");
972     showActionsInStatusBar({action}, show);
973 }
974
975 void MainWindow::showActionsInStatusBar(const QVector<QAction *> &actions, bool show) {
976 #ifdef APP_EXTRA
977     Extra::fadeInWidget(statusBar(), statusBar());
978 #endif
979     for (auto action : actions) {
980         if (show) {
981             if (statusToolBar->actions().contains(action)) continue;
982             if (statusToolBar->actions().isEmpty()) {
983                 statusToolBar->addAction(action);
984             } else {
985                 statusToolBar->insertAction(statusToolBar->actions().at(0), action);
986             }
987         } else {
988             statusToolBar->removeAction(action);
989         }
990     }
991
992     if (show) {
993         if (statusBar()->isHidden() && !fullScreenActive) setStatusBarVisibility(true);
994     } else {
995         if (statusBar()->isVisible() && !needStatusBar()) setStatusBarVisibility(false);
996     }
997 }
998
999 void MainWindow::setStatusBarVisibility(bool show) {
1000     if (statusBar()->isVisible() != show) {
1001         statusBar()->setVisible(show);
1002         if (views->currentWidget() == mediaView)
1003             QTimer::singleShot(0, mediaView, SLOT(adjustWindowSize()));
1004     }
1005 }
1006
1007 void MainWindow::adjustStatusBarVisibility() {
1008     setStatusBarVisibility(needStatusBar());
1009 }
1010
1011 void MainWindow::hideToolbar() {
1012 #ifdef APP_MAC
1013     mac::showToolBar(winId(), false);
1014 #else
1015     mainToolBar->hide();
1016 #endif
1017 }
1018
1019 void MainWindow::showToolbar() {
1020 #ifdef APP_MAC
1021     mac::showToolBar(winId(), true);
1022 #else
1023     mainToolBar->show();
1024 #endif
1025 }
1026
1027 void MainWindow::readSettings() {
1028     QSettings settings;
1029     QByteArray geometrySettings = settings.value("geometry").toByteArray();
1030     if (!geometrySettings.isEmpty()) {
1031         restoreGeometry(geometrySettings);
1032     } else {
1033         const QRect desktopSize = QGuiApplication::primaryScreen()->availableGeometry();
1034         int w = desktopSize.width() * .9;
1035         int h = qMin(w / 2, desktopSize.height());
1036         setGeometry(
1037                 QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, QSize(w, h), desktopSize));
1038     }
1039     setDefinitionMode(settings.value("definition", YT3::instance().maxVideoDefinition().getName())
1040                               .toString());
1041     getAction("manualplay")->setChecked(settings.value("manualplay", false).toBool());
1042     getAction("safeSearch")->setChecked(settings.value("safeSearch", false).toBool());
1043 #ifndef APP_MAC
1044     menuBar()->setVisible(settings.value("menuBar", false).toBool());
1045 #endif
1046 }
1047
1048 void MainWindow::writeSettings() {
1049     QSettings settings;
1050
1051     if (!isReallyFullScreen()) {
1052         settings.setValue("geometry", saveGeometry());
1053         if (mediaView) mediaView->saveSplitterState();
1054     }
1055
1056     settings.setValue("manualplay", getAction("manualplay")->isChecked());
1057     settings.setValue("safeSearch", getAction("safeSearch")->isChecked());
1058 #ifndef APP_MAC
1059     settings.setValue("menuBar", menuBar()->isVisible());
1060 #endif
1061 }
1062
1063 void MainWindow::goBack() {
1064     if (history.size() > 1) {
1065         history.pop();
1066         showView(history.pop());
1067     }
1068 }
1069
1070 void MainWindow::showView(View *view, bool transition) {
1071     if (!history.isEmpty() && view == history.top()) {
1072         qDebug() << "Attempting to show same view" << view;
1073         return;
1074     }
1075
1076 #ifdef APP_MAC
1077     if (transition && !history.isEmpty()) CompositeFader::go(this, this->grab());
1078 #endif
1079
1080     if (compactViewAct->isChecked()) compactViewAct->toggle();
1081
1082     // call hide method on the current view
1083     View *oldView = qobject_cast<View *>(views->currentWidget());
1084     if (oldView) {
1085         oldView->willDisappear();
1086         oldView->disappear();
1087         oldView->setEnabled(false);
1088         oldView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
1089     } else
1090         qDebug() << "Cannot cast old view";
1091
1092     view->willAppear();
1093     view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
1094     view->setEnabled(true);
1095     views->setCurrentWidget(view);
1096     view->appear();
1097     view->didAppear();
1098     if (oldView) oldView->didDisappear();
1099
1100     QString title = view->getTitle();
1101     if (title.isEmpty())
1102         title = Constants::NAME;
1103     else
1104         title += QLatin1String(" - ") + Constants::NAME;
1105     setWindowTitle(title);
1106
1107     const bool isMediaView = view == mediaView;
1108     stopAct->setEnabled(isMediaView);
1109     compactViewAct->setEnabled(isMediaView);
1110     toolbarSearch->setEnabled(isMediaView);
1111     aboutAct->setEnabled(view != aboutView);
1112     getAction("downloads")->setChecked(view == downloadView);
1113
1114     // dynamic view actions
1115     /* Not currently used by any view
1116     showActionsInStatusBar(viewActions, false);
1117     viewActions = newView->getViewActions();
1118     showActionsInStatusBar(viewActions, true);
1119     */
1120
1121     history.push(view);
1122     emit viewChanged();
1123 }
1124
1125 void MainWindow::about() {
1126     if (!aboutView) {
1127         aboutView = new AboutView(this);
1128         views->addWidget(aboutView);
1129     }
1130     showView(aboutView);
1131 }
1132
1133 void MainWindow::visitSite() {
1134     QUrl url(Constants::WEBSITE);
1135     showMessage(QString(tr("Opening %1").arg(url.toString())));
1136     QDesktopServices::openUrl(url);
1137 }
1138
1139 void MainWindow::donate() {
1140     QUrl url("https://" + QLatin1String(Constants::ORG_DOMAIN) + "/donate");
1141     showMessage(QString(tr("Opening %1").arg(url.toString())));
1142     QDesktopServices::openUrl(url);
1143 }
1144
1145 void MainWindow::reportIssue() {
1146     QUrl url("https://flavio.tordini.org/forums/forum/minitube-forums/minitube-troubleshooting");
1147     QDesktopServices::openUrl(url);
1148 }
1149
1150 void MainWindow::quit() {
1151 #ifdef APP_MAC
1152     if (!confirmQuit()) {
1153         return;
1154     }
1155 #endif
1156     // do not save geometry when in full screen or in compact mode
1157     if (!fullScreenActive && !compactViewAct->isChecked()) {
1158 #ifdef APP_MAC
1159         hideToolbar();
1160 #endif
1161         writeSettings();
1162     }
1163     // mediaView->stop();
1164     Temporary::deleteAll();
1165     ChannelAggregator::instance()->stop();
1166     ChannelAggregator::instance()->cleanup();
1167     Database::shutdown();
1168     qApp->quit();
1169 }
1170
1171 void MainWindow::closeEvent(QCloseEvent *e) {
1172 #ifdef APP_MAC
1173     mac::closeWindow(winId());
1174     e->ignore();
1175 #else
1176     if (!confirmQuit()) {
1177         e->ignore();
1178         return;
1179     }
1180     QWidget::closeEvent(e);
1181     quit();
1182 #endif
1183     messageLabel->hide();
1184 }
1185
1186 void MainWindow::showEvent(QShowEvent *e) {
1187     QWidget::showEvent(e);
1188 #ifdef APP_MAC
1189     restore();
1190 #endif
1191 }
1192
1193 bool MainWindow::confirmQuit() {
1194     if (DownloadManager::instance()->activeItems() > 0) {
1195         QMessageBox msgBox(this);
1196         msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1197         msgBox.setText(
1198                 tr("Do you want to exit %1 with a download in progress?").arg(Constants::NAME));
1199         msgBox.setInformativeText(
1200                 tr("If you close %1 now, this download will be cancelled.").arg(Constants::NAME));
1201         msgBox.setModal(true);
1202         // make it a "sheet" on the Mac
1203         msgBox.setWindowModality(Qt::WindowModal);
1204
1205         msgBox.addButton(tr("Close and cancel download"), QMessageBox::RejectRole);
1206         QPushButton *waitButton =
1207                 msgBox.addButton(tr("Wait for download to finish"), QMessageBox::ActionRole);
1208
1209         msgBox.exec();
1210
1211         if (msgBox.clickedButton() == waitButton) {
1212             return false;
1213         }
1214     }
1215     return true;
1216 }
1217
1218 void MainWindow::showHome() {
1219     showView(homeView);
1220     currentTimeLabel->clear();
1221     seekSlider->setValue(0);
1222 }
1223
1224 void MainWindow::showMedia(SearchParams *searchParams) {
1225     showView(mediaView);
1226     if (getAction("safeSearch")->isChecked())
1227         searchParams->setSafeSearch(SearchParams::Strict);
1228     else
1229         searchParams->setSafeSearch(SearchParams::None);
1230     mediaView->search(searchParams);
1231 }
1232
1233 void MainWindow::showMedia(VideoSource *videoSource) {
1234     showView(mediaView);
1235     mediaView->setVideoSource(videoSource);
1236 }
1237
1238 void MainWindow::stateChanged(Media::State newState) {
1239     qDebug() << newState;
1240
1241     seekSlider->setEnabled(newState != Media::StoppedState);
1242
1243     switch (newState) {
1244     case Media::ErrorState:
1245         showMessage(tr("Error: %1").arg(media->errorString()));
1246         break;
1247
1248     case Media::PlayingState:
1249         pauseAct->setEnabled(true);
1250         pauseAct->setIcon(IconUtils::icon("media-playback-pause"));
1251         pauseAct->setText(tr("&Pause"));
1252         pauseAct->setStatusTip(tr("Pause playback") + " (" +
1253                                pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1254         break;
1255
1256     case Media::StoppedState:
1257         pauseAct->setEnabled(false);
1258         pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1259         pauseAct->setText(tr("&Play"));
1260         pauseAct->setStatusTip(tr("Resume playback") + " (" +
1261                                pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1262         break;
1263
1264     case Media::PausedState:
1265         pauseAct->setEnabled(true);
1266         pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1267         pauseAct->setText(tr("&Play"));
1268         pauseAct->setStatusTip(tr("Resume playback") + " (" +
1269                                pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1270         break;
1271
1272     case Media::BufferingState:
1273         pauseAct->setEnabled(false);
1274         pauseAct->setIcon(IconUtils::icon("content-loading"));
1275         pauseAct->setText(tr("&Loading..."));
1276         pauseAct->setStatusTip(QString());
1277         break;
1278
1279     case Media::LoadingState:
1280         pauseAct->setEnabled(false);
1281         currentTimeLabel->clear();
1282         break;
1283
1284     default:;
1285     }
1286 }
1287
1288 void MainWindow::stop() {
1289     showHome();
1290     mediaView->stop();
1291 }
1292
1293 void MainWindow::resizeEvent(QResizeEvent *e) {
1294     Q_UNUSED(e);
1295 #ifdef APP_MAC
1296     if (initialized && mac::CanGoFullScreen(winId())) {
1297         bool isFullscreen = mac::IsFullScreen(winId());
1298         if (isFullscreen != fullScreenActive) {
1299             if (compactViewAct->isChecked()) {
1300                 compactViewAct->setChecked(false);
1301                 compactView(false);
1302             }
1303             fullScreenActive = isFullscreen;
1304             updateUIForFullscreen();
1305         }
1306     }
1307 #endif
1308 #ifdef APP_MAC_QMACTOOLBAR
1309     int moreButtonWidth = 40;
1310     toolbarSearch->move(width() - toolbarSearch->width() - moreButtonWidth - 7, -34);
1311 #endif
1312     hideMessage();
1313 }
1314
1315 void MainWindow::enterEvent(QEvent *e) {
1316     Q_UNUSED(e);
1317 #ifdef APP_MAC
1318     // Workaround cursor bug on macOS
1319     unsetCursor();
1320 #endif
1321 }
1322
1323 void MainWindow::leaveEvent(QEvent *e) {
1324     Q_UNUSED(e);
1325     if (fullScreenActive) hideFullscreenUI();
1326 }
1327
1328 void MainWindow::toggleFullscreen() {
1329     if (compactViewAct->isChecked()) compactViewAct->toggle();
1330
1331 #ifdef APP_MAC
1332     WId handle = winId();
1333     if (mac::CanGoFullScreen(handle)) {
1334         if (mainToolBar) mainToolBar->setVisible(true);
1335         mac::ToggleFullScreen(handle);
1336         return;
1337     }
1338 #endif
1339
1340     fullScreenActive = !fullScreenActive;
1341
1342     if (fullScreenActive) {
1343         // Enter full screen
1344
1345         maximizedBeforeFullScreen = isMaximized();
1346
1347         // save geometry now, if the user quits when in full screen
1348         // geometry won't be saved
1349         writeSettings();
1350
1351 #ifdef APP_MAC
1352         MacSupport::enterFullScreen(this, views);
1353 #else
1354         menuVisibleBeforeFullScreen = menuBar()->isVisible();
1355         menuBar()->hide();
1356         if (mainToolBar) mainToolBar->hide();
1357         showFullScreen();
1358 #endif
1359
1360     } else {
1361         // Exit full screen
1362
1363 #ifdef APP_MAC
1364         MacSupport::exitFullScreen(this, views);
1365 #else
1366         menuBar()->setVisible(menuVisibleBeforeFullScreen);
1367         if (mainToolBar) mainToolBar->setVisible(views->currentWidget() == mediaView);
1368         if (maximizedBeforeFullScreen)
1369             showMaximized();
1370         else
1371             showNormal();
1372 #endif
1373
1374         // Make sure the window has focus
1375         activateWindow();
1376     }
1377
1378     qApp->processEvents();
1379     updateUIForFullscreen();
1380 }
1381
1382 void MainWindow::updateUIForFullscreen() {
1383     static QList<QKeySequence> fsShortcuts;
1384     static QString fsText;
1385
1386     if (fullScreenActive) {
1387         fsShortcuts = fullscreenAct->shortcuts();
1388         fsText = fullscreenAct->text();
1389         if (fsText.isEmpty()) qDebug() << "[taking Empty!]";
1390         fullscreenAct->setShortcuts(QList<QKeySequence>(fsShortcuts)
1391                                     << QKeySequence(Qt::Key_Escape));
1392         fullscreenAct->setText(tr("Leave &Full Screen"));
1393         fullscreenAct->setIcon(IconUtils::icon("view-restore"));
1394         setStatusBarVisibility(false);
1395
1396         if (mainToolBar) {
1397             removeToolBar(mainToolBar);
1398             mainToolBar->move(0, 0);
1399         }
1400
1401         mediaView->removeSidebar();
1402
1403     } else {
1404         fullscreenAct->setShortcuts(fsShortcuts);
1405         if (fsText.isEmpty()) fsText = "[Empty!]";
1406         fullscreenAct->setText(fsText);
1407         fullscreenAct->setIcon(IconUtils::icon("view-fullscreen"));
1408
1409         if (needStatusBar()) setStatusBarVisibility(true);
1410
1411         if (mainToolBar) {
1412             addToolBar(mainToolBar);
1413         }
1414
1415         mediaView->restoreSidebar();
1416     }
1417
1418     // No compact view action when in full screen
1419     compactViewAct->setVisible(!fullScreenActive);
1420     compactViewAct->setChecked(false);
1421
1422     // Hide anything but the video
1423     mediaView->setSidebarVisibility(!fullScreenActive);
1424
1425     if (fullScreenActive) {
1426         stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1427     } else {
1428         stopAct->setShortcuts(QList<QKeySequence>()
1429                               << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1430     }
1431
1432 #ifdef Q_OS_MAC
1433     MacSupport::fullScreenActions(actionMap, fullScreenActive);
1434 #endif
1435
1436     if (views->currentWidget() == mediaView) mediaView->setFocus();
1437
1438     if (fullScreenActive) {
1439         if (views->currentWidget() == mediaView) hideFullscreenUI();
1440     } else {
1441         fullscreenTimer->stop();
1442         unsetCursor();
1443     }
1444 }
1445
1446 bool MainWindow::isReallyFullScreen() {
1447 #ifdef Q_OS_MAC
1448     WId handle = winId();
1449     if (mac::CanGoFullScreen(handle))
1450         return mac::IsFullScreen(handle);
1451     else
1452         return isFullScreen();
1453 #else
1454     return isFullScreen();
1455 #endif
1456 }
1457
1458 void MainWindow::missingKeyWarning() {
1459     static bool shown = false;
1460     if (shown) return;
1461     shown = true;
1462     QMessageBox msgBox(this);
1463     msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1464     msgBox.setText(QString("%1 was built without a Google API key.").arg(Constants::NAME));
1465     msgBox.setInformativeText(QString("It won't work unless you enter one."
1466                                       "<p>In alternative you can get %1 from the developer site.")
1467                                       .arg(Constants::NAME));
1468     msgBox.setModal(true);
1469     msgBox.setWindowModality(Qt::WindowModal);
1470     msgBox.addButton(QMessageBox::Close);
1471     QPushButton *enterKeyButton =
1472             msgBox.addButton(QString("Enter API key..."), QMessageBox::AcceptRole);
1473     QPushButton *devButton = msgBox.addButton(QString("Get from %1").arg(Constants::WEBSITE),
1474                                               QMessageBox::AcceptRole);
1475     QPushButton *helpButton = msgBox.addButton(QMessageBox::Help);
1476
1477     msgBox.exec();
1478
1479     if (msgBox.clickedButton() == helpButton) {
1480         QDesktopServices::openUrl(QUrl("https://github.com/flaviotordini/minitube/blob/master/"
1481                                        "README.md#google-api-key"));
1482     } else if (msgBox.clickedButton() == enterKeyButton) {
1483         bool ok;
1484         QString text = QInputDialog::getText(this, QString(), "Google API key:", QLineEdit::Normal,
1485                                              QString(), &ok);
1486         if (ok && !text.isEmpty()) {
1487             QSettings settings;
1488             settings.setValue("googleApiKey", text);
1489             YT3::instance().initApiKeys();
1490         }
1491     } else if (msgBox.clickedButton() == devButton) {
1492         QDesktopServices::openUrl(QUrl(Constants::WEBSITE));
1493     }
1494     shown = false;
1495 }
1496
1497 void MainWindow::compactView(bool enable) {
1498     setUpdatesEnabled(false);
1499
1500     compactModeActive = enable;
1501
1502     static QList<QKeySequence> compactShortcuts;
1503     static QList<QKeySequence> stopShortcuts;
1504
1505     const QString key = "compactGeometry";
1506     QSettings settings;
1507
1508     if (enable) {
1509         setMinimumSize(320, 180);
1510 #ifdef Q_OS_MAC
1511         mac::RemoveFullScreenWindow(winId());
1512 #endif
1513         writeSettings();
1514
1515         if (settings.contains(key))
1516             restoreGeometry(settings.value(key).toByteArray());
1517         else
1518             resize(480, 270);
1519
1520 #ifdef APP_MAC_QMACTOOLBAR
1521         mac::showToolBar(winId(), !enable);
1522 #else
1523         mainToolBar->setVisible(!enable);
1524 #endif
1525         mediaView->setSidebarVisibility(!enable);
1526         statusBar()->hide();
1527
1528         compactShortcuts = compactViewAct->shortcuts();
1529         stopShortcuts = stopAct->shortcuts();
1530
1531         QList<QKeySequence> newStopShortcuts(stopShortcuts);
1532         newStopShortcuts.removeAll(QKeySequence(Qt::Key_Escape));
1533         stopAct->setShortcuts(newStopShortcuts);
1534         compactViewAct->setShortcuts(QList<QKeySequence>(compactShortcuts)
1535                                      << QKeySequence(Qt::Key_Escape));
1536
1537         // ensure focus does not end up to the search box
1538         // as it would steal the Space shortcut
1539         mediaView->setFocus();
1540
1541     } else {
1542         settings.setValue(key, saveGeometry());
1543
1544         // unset minimum size
1545         setMinimumSize(0, 0);
1546
1547 #ifdef Q_OS_MAC
1548         mac::SetupFullScreenWindow(winId());
1549 #endif
1550 #ifdef APP_MAC_QMACTOOLBAR
1551         mac::showToolBar(winId(), !enable);
1552 #else
1553         mainToolBar->setVisible(!enable);
1554 #endif
1555         mediaView->setSidebarVisibility(!enable);
1556         if (needStatusBar()) setStatusBarVisibility(true);
1557
1558         readSettings();
1559
1560         compactViewAct->setShortcuts(compactShortcuts);
1561         stopAct->setShortcuts(stopShortcuts);
1562     }
1563
1564     // auto float on top
1565     floatOnTop(enable, false);
1566
1567 #ifdef APP_MAC
1568     mac::compactMode(winId(), enable);
1569 #else
1570     if (enable) {
1571         menuVisibleBeforeCompactMode = menuBar()->isVisible();
1572         menuBar()->hide();
1573     } else {
1574         menuBar()->setVisible(menuVisibleBeforeCompactMode);
1575     }
1576 #endif
1577
1578     setUpdatesEnabled(true);
1579 }
1580
1581 void MainWindow::toggleToolbarMenu() {
1582     if (!toolbarMenu) toolbarMenu = new ToolbarMenu(this);
1583     if (toolbarMenu->isVisible())
1584         toolbarMenu->hide();
1585     else
1586         toolbarMenu->show();
1587 }
1588
1589 void MainWindow::searchFocus() {
1590     toolbarSearch->selectAll();
1591     toolbarSearch->setFocus();
1592 }
1593
1594 void MainWindow::initMedia() {
1595 #ifdef MEDIA_QTAV
1596     qFatal("QtAV has a showstopper bug. Audio stops randomly. See bug "
1597            "https://github.com/wang-bin/QtAV/issues/1184");
1598     media = new MediaQtAV(this);
1599 #elif defined MEDIA_MPV
1600     media = new MediaMPV();
1601 #else
1602     qFatal("No media backend defined");
1603 #endif
1604     media->init();
1605     media->setUserAgent(HttpUtils::stealthUserAgent());
1606
1607     QSettings settings;
1608     qreal volume = settings.value("volume", 1.).toReal();
1609     media->setVolume(volume);
1610
1611     connect(media, &Media::error, this, &MainWindow::handleError);
1612     connect(media, &Media::stateChanged, this, &MainWindow::stateChanged);
1613     connect(media, &Media::positionChanged, this, &MainWindow::tick);
1614
1615     connect(seekSlider, &QSlider::sliderMoved, this, [this](int value) {
1616         // value : maxValue = posit ion : duration
1617         qint64 ms = (value * media->duration()) / seekSlider->maximum();
1618         qDebug() << "Seeking to" << ms;
1619         media->seek(ms);
1620         if (media->state() == Media::PausedState) media->play();
1621     });
1622     connect(seekSlider, &QSlider::sliderPressed, this, [this]() {
1623         // value : maxValue = position : duration
1624         qint64 ms = (seekSlider->value() * media->duration()) / seekSlider->maximum();
1625         media->seek(ms);
1626         if (media->state() == Media::PausedState) media->play();
1627     });
1628     connect(media, &Media::started, this, [this]() { seekSlider->setValue(0); });
1629
1630     connect(media, &Media::volumeChanged, this, &MainWindow::volumeChanged);
1631     connect(media, &Media::volumeMutedChanged, this, &MainWindow::volumeMutedChanged);
1632     connect(volumeSlider, &QSlider::sliderMoved, this, [this](int value) {
1633         qreal volume = (qreal)value / volumeSlider->maximum();
1634         media->setVolume(volume);
1635     });
1636     connect(volumeSlider, &QSlider::sliderPressed, this, [this]() {
1637         qreal volume = (qreal)volumeSlider->value() / volumeSlider->maximum();
1638         media->setVolume(volume);
1639     });
1640
1641     mediaView->setMedia(media);
1642 }
1643
1644 void MainWindow::tick(qint64 time) {
1645 #ifdef APP_MAC
1646     bool isDown = seekSlider->property("down").isValid();
1647 #else
1648     bool isDown = seekSlider->isSliderDown();
1649 #endif
1650     if (!isDown && media->state() == Media::PlayingState) {
1651         // value : maxValue = position : duration
1652         qint64 duration = media->duration();
1653         if (duration <= 0) return;
1654         int value = (seekSlider->maximum() * media->position()) / duration;
1655         seekSlider->setValue(value);
1656     }
1657
1658     const QString s = formatTime(time);
1659     if (s != currentTimeLabel->text()) {
1660         currentTimeLabel->setText(s);
1661         emit currentTimeChanged(s);
1662
1663         // remaining time
1664         const qint64 remainingTime = media->remainingTime();
1665         currentTimeLabel->setStatusTip(tr("Remaining time: %1").arg(formatTime(remainingTime)));
1666     }
1667 }
1668
1669 QString MainWindow::formatTime(qint64 duration) {
1670     duration /= 1000;
1671     QString res;
1672     int seconds = (int)(duration % 60);
1673     duration /= 60;
1674     int minutes = (int)(duration % 60);
1675     duration /= 60;
1676     int hours = (int)(duration % 24);
1677     if (hours == 0) return res.sprintf("%02d:%02d", minutes, seconds);
1678     return res.sprintf("%02d:%02d:%02d", hours, minutes, seconds);
1679 }
1680
1681 void MainWindow::volumeUp() {
1682     qreal newVolume = media->volume() + .1;
1683     if (newVolume > 1.) newVolume = 1.;
1684     media->setVolume(newVolume);
1685 }
1686
1687 void MainWindow::volumeDown() {
1688     qreal newVolume = media->volume() - .1;
1689     if (newVolume < 0) newVolume = 0;
1690     media->setVolume(newVolume);
1691 }
1692
1693 void MainWindow::toggleVolumeMute() {
1694     bool muted = media->volumeMuted();
1695     media->setVolumeMuted(!muted);
1696 }
1697
1698 void MainWindow::volumeChanged(qreal newVolume) {
1699     // automatically unmute when volume changes
1700     if (media->volumeMuted()) media->setVolumeMuted(false);
1701     showMessage(tr("Volume at %1%").arg((int)(newVolume * 100)));
1702     // newVolume : 1.0 = x : 1000
1703     int value = newVolume * volumeSlider->maximum();
1704     volumeSlider->blockSignals(true);
1705     volumeSlider->setValue(value);
1706     volumeSlider->blockSignals(false);
1707 }
1708
1709 void MainWindow::volumeMutedChanged(bool muted) {
1710     if (muted) {
1711         volumeMuteAct->setIcon(IconUtils::icon("audio-volume-muted"));
1712         showMessage(tr("Volume is muted"));
1713     } else {
1714         volumeMuteAct->setIcon(IconUtils::icon("audio-volume-high"));
1715         showMessage(tr("Volume is unmuted"));
1716     }
1717 }
1718
1719 void MainWindow::setDefinitionMode(const QString &definitionName) {
1720     QAction *definitionAct = getAction("definition");
1721     definitionAct->setText(definitionName);
1722     definitionAct->setStatusTip(
1723             tr("Maximum video definition set to %1").arg(definitionAct->text()) + " (" +
1724             definitionAct->shortcut().toString(QKeySequence::NativeText) + ")");
1725     showMessage(definitionAct->statusTip());
1726     YT3::instance().setMaxVideoDefinition(definitionName);
1727     if (views->currentWidget() == mediaView) {
1728         mediaView->reloadCurrentVideo();
1729     }
1730 }
1731
1732 void MainWindow::toggleDefinitionMode() {
1733     const QVector<VideoDefinition> &definitions = VideoDefinition::getDefinitions();
1734     const VideoDefinition &currentDefinition = YT3::instance().maxVideoDefinition();
1735
1736     int index = definitions.indexOf(currentDefinition);
1737     if (index != definitions.size() - 1) {
1738         index++;
1739     } else {
1740         index = 0;
1741     }
1742     setDefinitionMode(definitions.at(index).getName());
1743 }
1744
1745 void MainWindow::clearRecentKeywords() {
1746     QSettings settings;
1747     settings.remove("recentKeywords");
1748     settings.remove("recentChannels");
1749     if (views->currentWidget() == homeView) {
1750         SearchView *searchView = homeView->getSearchView();
1751         searchView->updateRecentKeywords();
1752         searchView->updateRecentChannels();
1753     }
1754     HttpUtils::clearCaches();
1755     showMessage(tr("Your privacy is now safe"));
1756 }
1757
1758 void MainWindow::setManualPlay(bool enabled) {
1759     QSettings settings;
1760     settings.setValue("manualplay", QVariant::fromValue(enabled));
1761     if (views->currentWidget() == homeView &&
1762         homeView->currentWidget() == homeView->getSearchView())
1763         return;
1764     showActionsInStatusBar({getAction("manualplay")}, enabled);
1765 }
1766
1767 void MainWindow::updateDownloadMessage(const QString &message) {
1768     getAction("downloads")->setText(message);
1769 }
1770
1771 void MainWindow::downloadsFinished() {
1772     getAction("downloads")->setText(tr("&Downloads"));
1773     showMessage(tr("Downloads complete"));
1774 }
1775
1776 void MainWindow::toggleDownloads(bool show) {
1777     if (show) {
1778         stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1779         getAction("downloads")
1780                 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J)
1781                                                      << QKeySequence(Qt::Key_Escape));
1782     } else {
1783         getAction("downloads")
1784                 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J));
1785         stopAct->setShortcuts(QList<QKeySequence>()
1786                               << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1787     }
1788
1789     if (!downloadView) {
1790         downloadView = new DownloadView(this);
1791         views->addWidget(downloadView);
1792     }
1793     if (show)
1794         showView(downloadView);
1795     else
1796         goBack();
1797 }
1798
1799 void MainWindow::suggestionAccepted(Suggestion *suggestion) {
1800     search(suggestion->value);
1801 }
1802
1803 void MainWindow::search(const QString &query) {
1804     QString q = query.simplified();
1805     if (q.isEmpty()) return;
1806     SearchParams *searchParams = new SearchParams();
1807     searchParams->setKeywords(q);
1808     showMedia(searchParams);
1809 }
1810
1811 void MainWindow::dragEnterEvent(QDragEnterEvent *e) {
1812     if (e->mimeData()->hasFormat("text/uri-list")) {
1813         QList<QUrl> urls = e->mimeData()->urls();
1814         if (urls.isEmpty()) return;
1815         const QUrl &url = urls.at(0);
1816         QString videoId = YTSearch::videoIdFromUrl(url.toString());
1817         if (!videoId.isEmpty()) e->acceptProposedAction();
1818     }
1819 }
1820
1821 void MainWindow::dropEvent(QDropEvent *e) {
1822     if (!toolbarSearch->isEnabled()) return;
1823
1824     QList<QUrl> urls = e->mimeData()->urls();
1825     if (urls.isEmpty()) return;
1826     const QUrl &url = urls.at(0);
1827     QString videoId = YTSearch::videoIdFromUrl(url.toString());
1828     if (!videoId.isEmpty()) {
1829         setWindowTitle(url.toString());
1830         SearchParams *searchParams = new SearchParams();
1831         searchParams->setKeywords(videoId);
1832         showMedia(searchParams);
1833     }
1834 }
1835
1836 bool MainWindow::needStatusBar() {
1837     return !statusToolBar->actions().isEmpty();
1838 }
1839
1840 void MainWindow::adjustMessageLabelPosition() {
1841     if (messageLabel->parent() == this)
1842         messageLabel->move(0, height() - messageLabel->height());
1843     else
1844         messageLabel->move(mapToGlobal(QPoint(0, height() - messageLabel->height())));
1845 }
1846
1847 void MainWindow::floatOnTop(bool onTop, bool showAction) {
1848     if (showAction) showActionsInStatusBar({getAction("ontop")}, onTop);
1849 #ifdef APP_MAC
1850     mac::floatOnTop(winId(), onTop);
1851 #else
1852     if (onTop) {
1853         setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1854         show();
1855     } else {
1856         setWindowFlags(windowFlags() ^ Qt::WindowStaysOnTopHint);
1857         show();
1858     }
1859 #endif
1860 }
1861
1862 void MainWindow::restore() {
1863 #ifdef APP_MAC
1864     mac::uncloseWindow(winId());
1865 #endif
1866 }
1867
1868 void MainWindow::messageReceived(const QString &message) {
1869     if (message == QLatin1String("--toggle-playing")) {
1870         if (pauseAct->isEnabled()) pauseAct->trigger();
1871     } else if (message == QLatin1String("--next")) {
1872         if (skipAct->isEnabled()) skipAct->trigger();
1873     } else if (message == QLatin1String("--previous")) {
1874         if (skipBackwardAct->isEnabled()) skipBackwardAct->trigger();
1875     } else if (message == QLatin1String("--stop-after-this")) {
1876         getAction("stopafterthis")->toggle();
1877     } else if (message.startsWith("--")) {
1878         MainWindow::printHelp();
1879     } else if (!message.isEmpty()) {
1880         SearchParams *searchParams = new SearchParams();
1881         searchParams->setKeywords(message);
1882         showMedia(searchParams);
1883     }
1884 }
1885
1886 void MainWindow::hideFullscreenUI() {
1887     if (views->currentWidget() != mediaView) return;
1888     setCursor(Qt::BlankCursor);
1889
1890     QPoint p = mapFromGlobal(QCursor::pos());
1891     const int x = p.x();
1892
1893     if (x > mediaView->getSidebar()->width()) mediaView->setSidebarVisibility(false);
1894
1895 #ifndef APP_MAC
1896     const int y = p.y();
1897     bool shouldHideToolbar = !toolbarSearch->hasFocus() && y > mainToolBar->height();
1898     if (shouldHideToolbar) mainToolBar->setVisible(false);
1899 #endif
1900 }
1901
1902 void MainWindow::toggleMenuVisibility() {
1903     bool show = !menuBar()->isVisible();
1904     menuBar()->setVisible(show);
1905 }
1906
1907 void MainWindow::toggleMenuVisibilityWithMessage() {
1908     bool show = !menuBar()->isVisible();
1909     menuBar()->setVisible(show);
1910     if (!show) {
1911         QMessageBox msgBox(this);
1912         msgBox.setText(tr("You can still access the menu bar by pressing the ALT key"));
1913         msgBox.setModal(true);
1914         msgBox.setWindowModality(Qt::WindowModal);
1915         msgBox.exec();
1916     }
1917 }
1918
1919 #ifdef APP_MAC_STORE
1920 void MainWindow::rateOnAppStore() {
1921     QDesktopServices::openUrl(QUrl("macappstore://userpub.itunes.apple.com"
1922                                    "/WebObjects/MZUserPublishing.woa/wa/addUserReview"
1923                                    "?id=422006190&type=Purple+Software"));
1924 }
1925 #endif
1926
1927 void MainWindow::printHelp() {
1928     QString msg = QString("%1 %2\n\n").arg(Constants::NAME, Constants::VERSION);
1929     msg += "Usage: minitube [options]\n";
1930     msg += "Options:\n";
1931     msg += "  --toggle-playing\t";
1932     msg += "Start or pause playback.\n";
1933     msg += "  --next\t\t";
1934     msg += "Skip to the next video.\n";
1935     msg += "  --previous\t\t";
1936     msg += "Go back to the previous video.\n";
1937     msg += "  --stop-after-this\t";
1938     msg += "Stop playback at the end of the video.\n";
1939     std::cout << msg.toLocal8Bit().data();
1940 }
1941
1942 void MainWindow::setupAction(QAction *action) {
1943     // never autorepeat.
1944     // unexperienced users tend to keep keys pressed for a "long" time
1945     action->setAutoRepeat(false);
1946
1947     // show keyboard shortcuts in the status bar
1948     if (!action->shortcut().isEmpty())
1949         action->setStatusTip(action->statusTip() + QLatin1String(" (") +
1950                              action->shortcut().toString(QKeySequence::NativeText) +
1951                              QLatin1String(")"));
1952 }
1953
1954 QAction *MainWindow::getAction(const char *name) {
1955     return actionMap.value(QByteArray::fromRawData(name, strlen(name)));
1956 }
1957
1958 void MainWindow::addNamedAction(const QByteArray &name, QAction *action) {
1959     actionMap.insert(name, action);
1960 }
1961
1962 QMenu *MainWindow::getMenu(const char *name) {
1963     return menuMap.value(QByteArray::fromRawData(name, strlen(name)));
1964 }
1965
1966 void MainWindow::showMessage(const QString &message) {
1967     if (!isVisible()) return;
1968 #ifdef APP_MAC
1969     if (!mac::isVisible(winId())) return;
1970 #endif
1971     if (statusBar()->isVisible())
1972         statusBar()->showMessage(message, 60000);
1973     else if (isActiveWindow()) {
1974         messageLabel->setText(message);
1975         QSize size = messageLabel->sizeHint();
1976         // round width to avoid flicker with fast changing messages (e.g. volume
1977         // changes)
1978         int w = size.width() + 10;
1979         const int multiple = 15;
1980         w = w + multiple / 2;
1981         w -= w % multiple;
1982         size.setWidth(w);
1983         messageLabel->resize(size);
1984         if (messageLabel->isHidden()) {
1985             adjustMessageLabelPosition();
1986             messageLabel->show();
1987         }
1988         messageTimer->start();
1989     }
1990 }
1991
1992 void MainWindow::hideMessage() {
1993     if (messageLabel->isVisible()) {
1994         messageLabel->hide();
1995         messageLabel->clear();
1996     }
1997 }
1998
1999 void MainWindow::handleError(const QString &message) {
2000     qWarning() << message;
2001     showMessage(message);
2002 }
2003
2004 #ifdef APP_ACTIVATION
2005 void MainWindow::showActivationView() {
2006     View *activationView = ActivationView::instance();
2007     views->addWidget(activationView);
2008     if (views->currentWidget() != activationView) showView(activationView);
2009 }
2010 #endif
2011
2012 void MainWindow::showRegionsView() {
2013     if (!regionsView) {
2014         regionsView = new RegionsView(this);
2015         connect(regionsView, SIGNAL(regionChanged()), homeView->getStandardFeedsView(),
2016                 SLOT(load()));
2017         views->addWidget(regionsView);
2018     }
2019     showView(regionsView);
2020 }