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