3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
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"
29 #include "ivvideolist.h"
32 #include "ytjstrending.h"
34 StandardFeedsView::StandardFeedsView(QWidget *parent) : View(parent), layout(0) {
35 setBackgroundRole(QPalette::Base);
36 setAutoFillBackground(true);
38 connect(MainWindow::instance()->getAction("worldwideRegion"), SIGNAL(triggered()),
39 SLOT(selectWorldwideRegion()));
41 connect(MainWindow::instance()->getAction("localRegion"), SIGNAL(triggered()),
42 SLOT(selectLocalRegion()));
45 void StandardFeedsView::load() {
46 setUpdatesEnabled(false);
49 YTRegion region = YTRegions::currentRegion();
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()) {
66 new YTJSTrending(i.value(), {{"page", i.key()}, {"geoLocation", region.id}}));
70 setUpdatesEnabled(true);
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);
82 QAction *regionAction = MainWindow::instance()->getRegionAction();
83 regionAction->setText(region.name);
84 regionAction->setIcon(YTRegions::iconForRegionId(region.id));
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);
98 if (categories.size() > 1) setUpdatesEnabled(true);
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);
111 void StandardFeedsView::removeVideoSourceWidget(VideoSourceWidget *videoSourceWidget) {
112 qDebug() << videoSourceWidget->getVideoSource()->getName();
113 layout->removeWidget(videoSourceWidget);
114 videoSourceWidget->deleteLater();
116 const int layoutCount = layout->count();
117 QVector<QLayoutItem *> items;
118 items.reserve(layoutCount);
120 for (int i = layoutCount - 1; i >= 0; i--) {
121 QLayoutItem *item = layout->takeAt(i);
122 if (item && item->widget()) items.append(item);
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);
134 void StandardFeedsView::resetLayout() {
136 while (QLayoutItem *item = layout->takeAt(0)) {
137 delete item->widget();
143 layout = new QGridLayout(this);
144 layout->setMargin(0);
145 layout->setSpacing(0);
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());
158 void StandardFeedsView::appear() {
161 qApp->processEvents();
164 QAction *regionAction = MainWindow::instance()->getRegionAction();
165 MainWindow::instance()->showActionsInStatusBar({regionAction}, true);
168 void StandardFeedsView::disappear() {
169 QAction *regionAction = MainWindow::instance()->getRegionAction();
170 MainWindow::instance()->showActionsInStatusBar({regionAction}, false);
173 void StandardFeedsView::selectWorldwideRegion() {
174 YTRegions::setRegion(YTRegions::defaultRegion().id);
178 void StandardFeedsView::selectLocalRegion() {
179 YTRegions::setRegion(YTRegions::localRegion().id);
183 void StandardFeedsView::paintEvent(QPaintEvent *event) {
184 QWidget::paintEvent(event);