]> git.sur5r.net Git - minitube/blob - src/standardfeedsview.cpp
Merge tag 'upstream/2.5.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 "videosourcewidget.h"
23 #include "ytcategories.h"
24 #include "ytstandardfeed.h"
25 #include "ytregions.h"
26 #include "mainwindow.h"
27 #include "painterutils.h"
28
29 namespace The {
30 QHash<QString, QAction*>* globalActions();
31 }
32
33 StandardFeedsView::StandardFeedsView(QWidget *parent) : View(parent),
34     layout(0) {
35     QPalette p = palette();
36     p.setBrush(QPalette::Window, Qt::black);
37     setPalette(p);
38     setAutoFillBackground(true);
39
40     connect(The::globalActions()->value("worldwide-region"), SIGNAL(triggered()),
41             SLOT(selectWorldwideRegion()));
42
43     connect(The::globalActions()->value("local-region"), SIGNAL(triggered()),
44             SLOT(selectLocalRegion()));
45
46     /*
47     QAction *regionAction = MainWindow::instance()->getRegionAction();
48     connect(regionAction, SIGNAL(changed()), SLOT(load()));
49     */
50 }
51
52 void StandardFeedsView::load() {
53     setUpdatesEnabled(false);
54     YTCategories *youTubeCategories = new YTCategories(this);
55     connect(youTubeCategories, SIGNAL(categoriesLoaded(const QList<YTCategory> &)),
56             SLOT(layoutCategories(const QList<YTCategory> &)));
57     youTubeCategories->loadCategories();
58
59     if (layout) {
60         while (QLayoutItem *item = layout->takeAt(0)) {
61             delete item->widget();
62             delete item;
63         }
64         delete layout;
65     }
66
67     layout = new QGridLayout(this);
68     layout->setMargin(0);
69     layout->setSpacing(1);
70
71     QList<YTStandardFeed*> feeds = getMainFeeds();
72     foreach(YTStandardFeed *feed, feeds)
73         addVideoSourceWidget(feed);
74
75     YTRegion region = YTRegions::currentRegion();
76     QAction *regionAction = MainWindow::instance()->getRegionAction();
77     regionAction->setText(region.name);
78     regionAction->setIcon(YTRegions::iconForRegionId(region.id));
79 }
80
81 void StandardFeedsView::layoutCategories(const QList<YTCategory> &categories) {
82     QString regionId = YTRegions::currentRegionId();
83     foreach(YTCategory category, categories) {
84         // assign a parent to this VideoSource  so it won't be deleted by MediaView
85         YTStandardFeed *feed = new YTStandardFeed(this);
86         feed->setCategory(category.term);
87         feed->setLabel(category.label);
88         feed->setRegionId(regionId);
89         feed->setFeedId("most_popular");
90         addVideoSourceWidget(feed);
91     }
92     if (categories.size() > 1) setUpdatesEnabled(true);
93 }
94
95 void StandardFeedsView::addVideoSourceWidget(VideoSource *videoSource) {
96     VideoSourceWidget *w = new VideoSourceWidget(videoSource);
97     connect(w, SIGNAL(activated(VideoSource*)),
98             SIGNAL(activated(VideoSource*)));
99     int i = layout->count();
100     static const int cols = 4;
101     layout->addWidget(w, i / cols, i % cols);
102 }
103
104 QList<YTStandardFeed*> StandardFeedsView::getMainFeeds() {
105     QList<YTStandardFeed*> feeds;
106
107     feeds << buildStardardFeed("most_popular", tr("Most Popular"));
108           // << buildStardardFeed("recently_featured", tr("Featured"))
109           // << buildStardardFeed("most_shared", tr("Most Shared"))
110           // << buildStardardFeed("most_discussed", tr("Most Discussed"))
111           // << buildStardardFeed("top_rated", tr("Top Rated"))
112           // << buildStardardFeed("most_popular", tr("All Time Popular"), "all_time");
113
114     return feeds;
115 }
116
117 YTStandardFeed* StandardFeedsView::buildStardardFeed(const QString &feedId, const QString &label, QString time) {
118     YTStandardFeed *feed = new YTStandardFeed(this);
119     feed->setFeedId(feedId);
120     feed->setLabel(label);
121     if (!time.isEmpty()) feed->setTime(time);
122     feed->setRegionId(YTRegions::currentRegionId());
123     return feed;
124 }
125
126 void StandardFeedsView::appear() {
127     setFocus();
128     if (!layout) {
129         update();
130         qApp->processEvents();
131         load();
132     }
133     QAction *regionAction = MainWindow::instance()->getRegionAction();
134     MainWindow::instance()->showActionInStatusBar(regionAction, true);
135 }
136
137 void StandardFeedsView::disappear() {
138     QAction *regionAction = MainWindow::instance()->getRegionAction();
139     MainWindow::instance()->showActionInStatusBar(regionAction, false);
140 }
141
142 void StandardFeedsView::selectWorldwideRegion() {
143     YTRegions::setRegion(YTRegions::worldwideRegion().id);
144     load();
145 }
146
147 void StandardFeedsView::selectLocalRegion() {
148     YTRegions::setRegion(YTRegions::localRegion().id);
149     load();
150 }
151
152 void StandardFeedsView::paintEvent(QPaintEvent *event) {
153     QWidget::paintEvent(event);
154 }
155