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