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