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 "homeview.h"
22 #include "channelaggregator.h"
23 #include "channelview.h"
24 #include "iconutils.h"
25 #include "mainwindow.h"
26 #include "mediaview.h"
27 #include "searchview.h"
28 #include "segmentedcontrol.h"
29 #include "standardfeedsview.h"
30 #include "ytstandardfeed.h"
31 #ifdef APP_MAC
32 #include "macutils.h"
33 #endif
34 
HomeView(QWidget * parent)35 HomeView::HomeView(QWidget *parent)
36     : View(parent), searchView(nullptr), standardFeedsView(nullptr), channelsView(nullptr) {
37     QBoxLayout *layout = new QVBoxLayout(this);
38     layout->setMargin(0);
39     layout->setSpacing(0);
40 
41     setupBar();
42     layout->addWidget(bar);
43 
44     stackedWidget = new QStackedWidget();
45     layout->addWidget(stackedWidget);
46 }
47 
setupBar()48 void HomeView::setupBar() {
49     bar = new SegmentedControl();
50 
51     QAction *action = new QAction(tr("Search"), this);
52     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_1));
53     action->setStatusTip(tr("Find videos and channels by keyword"));
54     connect(action, SIGNAL(triggered()), SLOT(showSearch()));
55     bar->addAction(action);
56     bar->setCheckedAction(action);
57 
58     action = new QAction(tr("Browse"), this);
59     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_2));
60     action->setStatusTip(tr("Browse videos by category"));
61     connect(action, SIGNAL(triggered()), SLOT(showStandardFeeds()));
62     bar->addAction(action);
63 
64     subscriptionsAction = new QAction(tr("Subscriptions"), this);
65     subscriptionsAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_3));
66     subscriptionsAction->setStatusTip(tr("Channel subscriptions"));
67     connect(subscriptionsAction, SIGNAL(triggered()), SLOT(showChannels()));
68     bar->addAction(subscriptionsAction);
69     connect(ChannelAggregator::instance(), SIGNAL(unwatchedCountChanged(int)),
70             SLOT(unwatchedCountChanged(int)));
71 
72     const auto &a = bar->actions();
73     for (QAction *action : a) {
74         addAction(action);
75         MainWindow::instance()->setupAction(action);
76     }
77 }
78 
showWidget(QWidget * widget)79 void HomeView::showWidget(QWidget *widget) {
80     QWidget *currentWidget = stackedWidget->currentWidget();
81     if (currentWidget && currentWidget != widget) {
82         QMetaObject::invokeMethod(currentWidget, "disappear");
83         currentWidget->setEnabled(false);
84     }
85     stackedWidget->setCurrentWidget(widget);
86     widget->setEnabled(true);
87     QMetaObject::invokeMethod(widget, "appear", Qt::QueuedConnection);
88 }
89 
appear()90 void HomeView::appear() {
91     if (stackedWidget->count() == 0)
92         showSearch();
93     else
94         QMetaObject::invokeMethod(stackedWidget->currentWidget(), "appear", Qt::QueuedConnection);
95 }
96 
disappear()97 void HomeView::disappear() {
98     QMetaObject::invokeMethod(stackedWidget->currentWidget(), "disappear");
99 }
100 
showSearch()101 void HomeView::showSearch() {
102     if (!searchView) {
103         searchView = new SearchView(this);
104         connect(searchView, SIGNAL(search(SearchParams *)), MainWindow::instance(),
105                 SLOT(showMedia(SearchParams *)));
106         stackedWidget->addWidget(searchView);
107     }
108     showWidget(searchView);
109     bar->setCheckedAction(0);
110 }
111 
showStandardFeeds()112 void HomeView::showStandardFeeds() {
113     if (!standardFeedsView) {
114         standardFeedsView = new StandardFeedsView();
115         connect(standardFeedsView, SIGNAL(activated(VideoSource *)), MainWindow::instance(),
116                 SLOT(showMedia(VideoSource *)));
117         stackedWidget->addWidget(standardFeedsView);
118     }
119     showWidget(standardFeedsView);
120     bar->setCheckedAction(1);
121 }
122 
showChannels()123 void HomeView::showChannels() {
124     if (!channelsView) {
125         channelsView = new ChannelView();
126         connect(channelsView, SIGNAL(activated(VideoSource *)), MainWindow::instance(),
127                 SLOT(showMedia(VideoSource *)));
128         stackedWidget->addWidget(channelsView);
129     }
130     showWidget(channelsView);
131     bar->setCheckedAction(2);
132 }
133 
unwatchedCountChanged(int count)134 void HomeView::unwatchedCountChanged(int count) {
135     QVariant v;
136     QString s;
137     if (count > 0) {
138         s = QString::number(count);
139         v = s;
140     }
141     subscriptionsAction->setProperty("notifyCount", v);
142     bar->update();
143 #ifdef APP_MAC
144     mac::dockBadge(s);
145 #endif
146 }
147