]> git.sur5r.net Git - minitube/blob - src/standardfeedsview.cpp
Imported Upstream version 2.0
[minitube] / src / standardfeedsview.cpp
1 #include "standardfeedsview.h"
2 #include "videosourcewidget.h"
3 #include "ytcategories.h"
4 #include "ytstandardfeed.h"
5 #include "ytregions.h"
6 #include "mainwindow.h"
7
8 namespace The {
9 QHash<QString, QAction*>* globalActions();
10 }
11
12 static const int cols = 5;
13
14 StandardFeedsView::StandardFeedsView(QWidget *parent) : QWidget(parent),
15     layout(0) {
16     QPalette p = palette();
17     p.setBrush(QPalette::Window, Qt::black);
18     setPalette(p);
19     setAutoFillBackground(true);
20
21     connect(The::globalActions()->value("worldwide-region"), SIGNAL(triggered()),
22             SLOT(selectWorldwideRegion()));
23
24     connect(The::globalActions()->value("local-region"), SIGNAL(triggered()),
25             SLOT(selectLocalRegion()));
26
27     /*
28     QAction *regionAction = MainWindow::instance()->getRegionAction();
29     connect(regionAction, SIGNAL(changed()), SLOT(load()));
30     */
31 }
32
33 void StandardFeedsView::load() {
34     YTCategories *youTubeCategories = new YTCategories(this);
35     connect(youTubeCategories, SIGNAL(categoriesLoaded(const QList<YTCategory> &)),
36             SLOT(layoutCategories(const QList<YTCategory> &)));
37     youTubeCategories->loadCategories();
38
39     if (layout) {
40         while (QLayoutItem *item = layout->takeAt(0)) {
41             delete item->widget();
42             delete item;
43         }
44         delete layout;
45     }
46
47     layout = new QGridLayout(this);
48     layout->setMargin(0);
49     layout->setSpacing(1);
50
51     QList<YTStandardFeed*> feeds = getMainFeeds();
52     foreach(YTStandardFeed *feed, feeds)
53         addVideoSourceWidget(feed);
54
55     YTRegion region = YTRegions::currentRegion();
56     QToolButton *regionButton = MainWindow::instance()->getRegionButton();
57     regionButton->setText(region.name);
58     regionButton->setIcon(YTRegions::iconForRegionId(region.id));
59 }
60
61 void StandardFeedsView::layoutCategories(const QList<YTCategory> &categories) {
62     QString regionId = YTRegions::currentRegionId();
63     foreach(YTCategory category, categories) {
64         // assign a parent to this VideoSource  so it won't be deleted by MediaView
65         YTStandardFeed *feed = new YTStandardFeed(this);
66         feed->setCategory(category.term);
67         feed->setLabel(category.label);
68         feed->setRegionId(regionId);
69         feed->setFeedId("most_popular");
70         addVideoSourceWidget(feed);
71     }
72 }
73
74 void StandardFeedsView::addVideoSourceWidget(VideoSource *videoSource) {
75     VideoSourceWidget *w = new VideoSourceWidget(videoSource);
76     connect(w, SIGNAL(activated(VideoSource*)),
77             SIGNAL(activated(VideoSource*)));
78     int i = layout->count();
79     layout->addWidget(w, i / cols, i % cols);
80 }
81
82 QList<YTStandardFeed*> StandardFeedsView::getMainFeeds() {
83     QList<YTStandardFeed*> feeds;
84
85     feeds << buildStardardFeed("most_popular", tr("Most Popular"))
86           << buildStardardFeed("recently_featured", tr("Featured"))
87           << buildStardardFeed("most_shared", tr("Most Shared"))
88           << buildStardardFeed("most_discussed", tr("Most Discussed"))
89           << buildStardardFeed("top_rated", tr("Top Rated"));
90
91     return feeds;
92 }
93
94 YTStandardFeed* StandardFeedsView::buildStardardFeed(QString feedId, QString label) {
95     YTStandardFeed *feed = new YTStandardFeed(this);
96     feed->setFeedId(feedId);
97     feed->setLabel(label);
98     feed->setRegionId(YTRegions::currentRegionId());
99     return feed;
100 }
101
102 void StandardFeedsView::appear() {
103     setFocus();
104     if (!layout) load();
105     QAction *regionAction = MainWindow::instance()->getRegionAction();
106     regionAction->setVisible(true);
107 }
108
109 void StandardFeedsView::disappear() {
110     QAction *regionAction = MainWindow::instance()->getRegionAction();
111     regionAction->setVisible(false);
112 }
113
114 void StandardFeedsView::selectWorldwideRegion() {
115     YTRegions::setRegion(YTRegions::worldwideRegion().id);
116     load();
117 }
118
119 void StandardFeedsView::selectLocalRegion() {
120     YTRegions::setRegion(YTRegions::localRegion().id);
121     load();
122 }
123
124