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