]> git.sur5r.net Git - minitube/blob - src/standardfeedsview.cpp
Imported Upstream version 2.1.6
[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) : QWidget(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     YTCategories *youTubeCategories = new YTCategories(this);
54     connect(youTubeCategories, SIGNAL(categoriesLoaded(const QList<YTCategory> &)),
55             SLOT(layoutCategories(const QList<YTCategory> &)));
56     youTubeCategories->loadCategories();
57
58     if (layout) {
59         while (QLayoutItem *item = layout->takeAt(0)) {
60             delete item->widget();
61             delete item;
62         }
63         delete layout;
64     }
65
66     layout = new QGridLayout(this);
67     layout->setMargin(0);
68     layout->setSpacing(1);
69
70     QList<YTStandardFeed*> feeds = getMainFeeds();
71     foreach(YTStandardFeed *feed, feeds)
72         addVideoSourceWidget(feed);
73
74     YTRegion region = YTRegions::currentRegion();
75     QToolButton *regionButton = MainWindow::instance()->getRegionButton();
76     regionButton->setText(region.name);
77     regionButton->setIcon(YTRegions::iconForRegionId(region.id));
78 }
79
80 void StandardFeedsView::layoutCategories(const QList<YTCategory> &categories) {
81     QString regionId = YTRegions::currentRegionId();
82     foreach(YTCategory category, categories) {
83         // assign a parent to this VideoSource  so it won't be deleted by MediaView
84         YTStandardFeed *feed = new YTStandardFeed(this);
85         feed->setCategory(category.term);
86         feed->setLabel(category.label);
87         feed->setRegionId(regionId);
88         feed->setFeedId("most_popular");
89         addVideoSourceWidget(feed);
90     }
91 }
92
93 void StandardFeedsView::addVideoSourceWidget(VideoSource *videoSource) {
94     VideoSourceWidget *w = new VideoSourceWidget(videoSource);
95     connect(w, SIGNAL(activated(VideoSource*)),
96             SIGNAL(activated(VideoSource*)));
97     int i = layout->count();
98     static const int cols = 4;
99     layout->addWidget(w, i / cols, i % cols);
100 }
101
102 QList<YTStandardFeed*> StandardFeedsView::getMainFeeds() {
103     QList<YTStandardFeed*> feeds;
104
105     feeds << buildStardardFeed("most_popular", tr("Most Popular"));
106           // << buildStardardFeed("recently_featured", tr("Featured"))
107           // << buildStardardFeed("most_shared", tr("Most Shared"))
108           // << buildStardardFeed("most_discussed", tr("Most Discussed"))
109           // << buildStardardFeed("top_rated", tr("Top Rated"))
110           // << buildStardardFeed("most_popular", tr("All Time Popular"), "all_time");
111
112     return feeds;
113 }
114
115 YTStandardFeed* StandardFeedsView::buildStardardFeed(
116         QString feedId, QString label, QString time) {
117     YTStandardFeed *feed = new YTStandardFeed(this);
118     feed->setFeedId(feedId);
119     feed->setLabel(label);
120     if (!time.isEmpty()) feed->setTime(time);
121     feed->setRegionId(YTRegions::currentRegionId());
122     return feed;
123 }
124
125 void StandardFeedsView::appear() {
126     setFocus();
127     if (!layout) load();
128     QAction *regionAction = MainWindow::instance()->getRegionAction();
129     regionAction->setVisible(true);
130 }
131
132 void StandardFeedsView::disappear() {
133     QAction *regionAction = MainWindow::instance()->getRegionAction();
134     regionAction->setVisible(false);
135 }
136
137 void StandardFeedsView::selectWorldwideRegion() {
138     YTRegions::setRegion(YTRegions::worldwideRegion().id);
139     load();
140 }
141
142 void StandardFeedsView::selectLocalRegion() {
143     YTRegions::setRegion(YTRegions::localRegion().id);
144     load();
145 }
146
147 void StandardFeedsView::paintEvent(QPaintEvent *event) {
148     QWidget::paintEvent(event);
149     PainterUtils::topShadow(this);
150 }
151