]> git.sur5r.net Git - minitube/blob - src/standardfeedsview.cpp
Imported Upstream version 2.4
[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     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     QToolButton *regionButton = MainWindow::instance()->getRegionButton();
77     regionButton->setText(region.name);
78     regionButton->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(
118         QString feedId, QString label, QString time) {
119     YTStandardFeed *feed = new YTStandardFeed(this);
120     feed->setFeedId(feedId);
121     feed->setLabel(label);
122     if (!time.isEmpty()) feed->setTime(time);
123     feed->setRegionId(YTRegions::currentRegionId());
124     return feed;
125 }
126
127 void StandardFeedsView::appear() {
128     setFocus();
129     if (!layout) {
130         update();
131         qApp->processEvents();
132         load();
133     }
134     QAction *regionAction = MainWindow::instance()->getRegionAction();
135     regionAction->setVisible(true);
136 }
137
138 void StandardFeedsView::disappear() {
139     QAction *regionAction = MainWindow::instance()->getRegionAction();
140     regionAction->setVisible(false);
141 }
142
143 void StandardFeedsView::selectWorldwideRegion() {
144     YTRegions::setRegion(YTRegions::worldwideRegion().id);
145     load();
146 }
147
148 void StandardFeedsView::selectLocalRegion() {
149     YTRegions::setRegion(YTRegions::localRegion().id);
150     load();
151 }
152
153 void StandardFeedsView::paintEvent(QPaintEvent *event) {
154     QWidget::paintEvent(event);
155     // PainterUtils::topShadow(this);
156 }
157