]> git.sur5r.net Git - minitube/blob - src/standardfeedsview.cpp
New upstream version 3.9.1
[minitube] / src / standardfeedsview.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 "standardfeedsview.h"
22 #include "mainwindow.h"
23 #include "painterutils.h"
24 #include "videosourcewidget.h"
25 #include "ytcategories.h"
26 #include "ytregions.h"
27 #include "ytstandardfeed.h"
28
29 #include "ivvideolist.h"
30 #include "videoapi.h"
31
32 #include "ytjstrending.h"
33
34 StandardFeedsView::StandardFeedsView(QWidget *parent) : View(parent), layout(0) {
35     setBackgroundRole(QPalette::Base);
36     setAutoFillBackground(true);
37
38     connect(MainWindow::instance()->getAction("worldwideRegion"), SIGNAL(triggered()),
39             SLOT(selectWorldwideRegion()));
40
41     connect(MainWindow::instance()->getAction("localRegion"), SIGNAL(triggered()),
42             SLOT(selectLocalRegion()));
43 }
44
45 void StandardFeedsView::load() {
46     setUpdatesEnabled(false);
47     resetLayout();
48
49     YTRegion region = YTRegions::currentRegion();
50
51     // TODO consolidate in YT
52     if (VideoAPI::impl() == VideoAPI::YT3) {
53         YTCategories *youTubeCategories = new YTCategories(this);
54         connect(youTubeCategories, SIGNAL(categoriesLoaded(const QVector<YTCategory> &)),
55                 SLOT(layoutCategories(const QVector<YTCategory> &)));
56         youTubeCategories->loadCategories();
57         addVideoSourceWidget(buildStandardFeed("most_popular", tr("Most Popular")));
58     } else if (VideoAPI::impl() == VideoAPI::JS) {
59         const QMap<QString, QString> pages = {{"default", tr("Trending")},
60                                               {"music", tr("Music")},
61                                               {"movies", tr("Movies")},
62                                               {"gaming", tr("Gaming")}};
63         auto i = pages.constBegin();
64         while (i != pages.constEnd()) {
65             addVideoSourceWidget(
66                     new YTJSTrending(i.value(), {{"page", i.key()}, {"geoLocation", region.id}}));
67             ++i;
68         }
69
70         setUpdatesEnabled(true);
71     } else {
72         QString regionParam = "region=" + region.id;
73         addVideoSourceWidget(new IVVideoList("popular?" + regionParam, tr("Most Popular")));
74         addVideoSourceWidget(new IVVideoList("trending?" + regionParam, tr("Trending")));
75         addVideoSourceWidget(new IVVideoList("trending?type=music&" + regionParam, tr("Music")));
76         addVideoSourceWidget(new IVVideoList("trending?type=news&" + regionParam, tr("News")));
77         addVideoSourceWidget(new IVVideoList("trending?type=movies&" + regionParam, tr("Movies")));
78         addVideoSourceWidget(new IVVideoList("trending?type=gaming&" + regionParam, tr("Gaming")));
79         setUpdatesEnabled(true);
80     }
81
82     QAction *regionAction = MainWindow::instance()->getRegionAction();
83     regionAction->setText(region.name);
84     regionAction->setIcon(YTRegions::iconForRegionId(region.id));
85 }
86
87 void StandardFeedsView::layoutCategories(const QVector<YTCategory> &categories) {
88     QString regionId = YTRegions::currentRegionId();
89     for (const YTCategory &category : categories) {
90         // assign a parent to this VideoSource  so it won't be deleted by MediaView
91         YTStandardFeed *feed = new YTStandardFeed(this);
92         feed->setCategory(category.term);
93         feed->setLabel(category.label);
94         feed->setRegionId(regionId);
95         feed->setFeedId("most_popular");
96         addVideoSourceWidget(feed);
97     }
98     if (categories.size() > 1) setUpdatesEnabled(true);
99 }
100
101 void StandardFeedsView::addVideoSourceWidget(VideoSource *videoSource) {
102     VideoSourceWidget *w = new VideoSourceWidget(videoSource);
103     connect(w, SIGNAL(activated(VideoSource *)), SIGNAL(activated(VideoSource *)));
104     connect(w, SIGNAL(unavailable(VideoSourceWidget *)),
105             SLOT(removeVideoSourceWidget(VideoSourceWidget *)));
106     int i = layout->count();
107     const int cols = VideoAPI::impl() == VideoAPI::YT3 ? 5 : 2;
108     layout->addWidget(w, i / cols, i % cols);
109 }
110
111 void StandardFeedsView::removeVideoSourceWidget(VideoSourceWidget *videoSourceWidget) {
112     qDebug() << videoSourceWidget->getVideoSource()->getName();
113     layout->removeWidget(videoSourceWidget);
114     videoSourceWidget->deleteLater();
115
116     const int layoutCount = layout->count();
117     QVector<QLayoutItem *> items;
118     items.reserve(layoutCount);
119
120     for (int i = layoutCount - 1; i >= 0; i--) {
121         QLayoutItem *item = layout->takeAt(i);
122         if (item && item->widget()) items.append(item);
123     }
124
125     const int itemCount = items.size();
126     const int cols = 2; // itemCount / 3;
127     for (int i = itemCount - 1; i >= 0; i--) {
128         QLayoutItem *item = items.at(i);
129         int index = itemCount - 1 - i;
130         layout->addItem(item, index / cols, index % cols);
131     }
132 }
133
134 void StandardFeedsView::resetLayout() {
135     if (layout) {
136         while (QLayoutItem *item = layout->takeAt(0)) {
137             delete item->widget();
138             delete item;
139         }
140         delete layout;
141     }
142
143     layout = new QGridLayout(this);
144     layout->setMargin(0);
145     layout->setSpacing(0);
146 }
147
148 YTStandardFeed *
149 StandardFeedsView::buildStandardFeed(const QString &feedId, const QString &label, QString time) {
150     YTStandardFeed *feed = new YTStandardFeed(this);
151     feed->setFeedId(feedId);
152     feed->setLabel(label);
153     if (!time.isEmpty()) feed->setTime(time);
154     feed->setRegionId(YTRegions::currentRegionId());
155     return feed;
156 }
157
158 void StandardFeedsView::appear() {
159     if (!layout) {
160         update();
161         qApp->processEvents();
162         load();
163     }
164     QAction *regionAction = MainWindow::instance()->getRegionAction();
165     MainWindow::instance()->showActionsInStatusBar({regionAction}, true);
166 }
167
168 void StandardFeedsView::disappear() {
169     QAction *regionAction = MainWindow::instance()->getRegionAction();
170     MainWindow::instance()->showActionsInStatusBar({regionAction}, false);
171 }
172
173 void StandardFeedsView::selectWorldwideRegion() {
174     YTRegions::setRegion(YTRegions::defaultRegion().id);
175     load();
176 }
177
178 void StandardFeedsView::selectLocalRegion() {
179     YTRegions::setRegion(YTRegions::localRegion().id);
180     load();
181 }
182
183 void StandardFeedsView::paintEvent(QPaintEvent *event) {
184     QWidget::paintEvent(event);
185 }