]> git.sur5r.net Git - minitube/blob - src/channelview.cpp
eba059135686298690a3ae9b96d6edaebc09330f
[minitube] / src / channelview.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 "channelview.h"
22 #include "ytuser.h"
23 #include "ytsearch.h"
24 #include "searchparams.h"
25 #include "channelmodel.h"
26 #include "channelitemdelegate.h"
27 #include "database.h"
28 #include "ytsearch.h"
29 #include "channelaggregator.h"
30 #include "aggregatevideosource.h"
31 #include "painterutils.h"
32 #include "mainwindow.h"
33 #include "utils.h"
34 #ifdef APP_EXTRA
35 #include "extra.h"
36 #endif
37
38 static const char *sortByKey = "subscriptionsSortBy";
39 static const char *showUpdatedKey = "subscriptionsShowUpdated";
40
41 ChannelView::ChannelView(QWidget *parent) : QListView(parent),
42     showUpdated(false),
43     sortBy(SortByName) {
44
45     setItemDelegate(new ChannelItemDelegate(this));
46     setSelectionMode(QAbstractItemView::NoSelection);
47
48     // layout
49     setSpacing(15);
50     setFlow(QListView::LeftToRight);
51     setWrapping(true);
52     setResizeMode(QListView::Adjust);
53     setMovement(QListView::Static);
54     setUniformItemSizes(true);
55
56     // cosmetics
57     setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
58     setFrameShape(QFrame::NoFrame);
59     setAttribute(Qt::WA_MacShowFocusRect, false);
60
61     QPalette p = palette();
62     /*
63     p.setColor(QPalette::Base, p.window().color());
64     p.setColor(QPalette::Text, p.windowText().color());
65     */
66     p.setColor(QPalette::Disabled, QPalette::Base, p.base().color());
67     p.setColor(QPalette::Disabled, QPalette::Text, p.text().color());
68     setPalette(p);
69
70     verticalScrollBar()->setPageStep(3);
71     verticalScrollBar()->setSingleStep(1);
72
73     setMouseTracking(true);
74
75     connect(this, SIGNAL(clicked(const QModelIndex &)),
76             SLOT(itemActivated(const QModelIndex &)));
77     connect(this, SIGNAL(entered(const QModelIndex &)),
78             SLOT(itemEntered(const QModelIndex &)));
79
80     channelsModel = new ChannelModel(this);
81     setModel(channelsModel);
82     connect(this, SIGNAL(viewportEntered()),
83             channelsModel, SLOT(clearHover()));
84
85     setupActions();
86
87     connect(ChannelAggregator::instance(), SIGNAL(channelChanged(YTUser*)),
88             channelsModel, SLOT(updateChannel(YTUser*)));
89     connect(ChannelAggregator::instance(), SIGNAL(unwatchedCountChanged(int)),
90             SLOT(unwatchedCountChanged(int)));
91
92     unwatchedCountChanged(ChannelAggregator::instance()->getUnwatchedCount());
93 }
94
95 void ChannelView::setupActions() {
96     QSettings settings;
97
98     markAsWatchedAction = new QAction(
99                 Utils::icon("mark-watched"), tr("Mark all as watched"), this);
100     markAsWatchedAction->setEnabled(false);
101     markAsWatchedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_W));
102     connect(markAsWatchedAction, SIGNAL(triggered()), SLOT(markAllAsWatched()));
103     statusActions << markAsWatchedAction;
104
105     showUpdated = settings.value(showUpdatedKey, false).toBool();
106     QAction *showUpdatedAction = new QAction(
107                 Utils::icon("show-updated"), tr("Show Updated"), this);
108     showUpdatedAction->setCheckable(true);
109     showUpdatedAction->setChecked(showUpdated);
110     showUpdatedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_U));
111     connect(showUpdatedAction, SIGNAL(toggled(bool)), SLOT(toggleShowUpdated(bool)));
112     statusActions << showUpdatedAction;
113
114     SortBy sortBy = static_cast<SortBy>(settings.value(sortByKey, SortByName).toInt());
115
116     QMenu *sortMenu = new QMenu(this);
117     QActionGroup *sortGroup = new QActionGroup(this);
118
119     QAction *sortByNameAction = new QAction(tr("Name"), this);
120     sortByNameAction->setActionGroup(sortGroup);
121     sortByNameAction->setCheckable(true);
122     if (sortBy == SortByName) sortByNameAction->setChecked(true);
123     connect(sortByNameAction, SIGNAL(triggered()), SLOT(setSortByName()));
124     sortMenu->addAction(sortByNameAction);
125
126     QAction *sortByUpdatedAction = new QAction(tr("Last Updated"), this);
127     sortByUpdatedAction->setActionGroup(sortGroup);
128     sortByUpdatedAction->setCheckable(true);
129     if (sortBy == SortByUpdated) sortByUpdatedAction->setChecked(true);
130     connect(sortByUpdatedAction, SIGNAL(triggered()), SLOT(setSortByUpdated()));
131     sortMenu->addAction(sortByUpdatedAction);
132
133     QAction *sortByAddedAction = new QAction(tr("Last Added"), this);
134     sortByAddedAction->setActionGroup(sortGroup);
135     sortByAddedAction->setCheckable(true);
136     if (sortBy == SortByAdded) sortByAddedAction->setChecked(true);
137     connect(sortByAddedAction, SIGNAL(triggered()), SLOT(setSortByAdded()));
138     sortMenu->addAction(sortByAddedAction);
139
140     QAction *sortByLastWatched = new QAction(tr("Last Watched"), this);
141     sortByLastWatched->setActionGroup(sortGroup);
142     sortByLastWatched->setCheckable(true);
143     if (sortBy == SortByLastWatched) sortByLastWatched->setChecked(true);
144     connect(sortByLastWatched, SIGNAL(triggered()), SLOT(setSortByLastWatched()));
145     sortMenu->addAction(sortByLastWatched);
146
147     QAction *sortByMostWatched = new QAction(tr("Most Watched"), this);
148     sortByMostWatched->setActionGroup(sortGroup);
149     sortByMostWatched->setCheckable(true);
150     if (sortBy == SortByMostWatched) sortByMostWatched->setChecked(true);
151     connect(sortByMostWatched, SIGNAL(triggered()), SLOT(setSortByMostWatched()));
152     sortMenu->addAction(sortByMostWatched);
153
154     QToolButton *sortButton = new QToolButton(this);
155     sortButton->setText(tr("Sort by"));
156     sortButton->setIcon(Utils::icon("sort"));
157     sortButton->setIconSize(QSize(16, 16));
158     sortButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
159     sortButton->setPopupMode(QToolButton::InstantPopup);
160     sortButton->setMenu(sortMenu);
161     QWidgetAction *widgetAction = new QWidgetAction(this);
162     widgetAction->setDefaultWidget(sortButton);
163     widgetAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O));
164     statusActions << widgetAction;
165
166     foreach (QAction *action, statusActions) {
167         window()->addAction(action);
168         Utils::setupAction(action);
169     }
170 }
171
172 void ChannelView::appear() {
173     updateQuery();
174     foreach (QAction* action, statusActions)
175         MainWindow::instance()->showActionInStatusBar(action, true);
176     setFocus();
177     ChannelAggregator::instance()->run();
178 }
179
180 void ChannelView::disappear() {
181     foreach (QAction* action, statusActions)
182         MainWindow::instance()->showActionInStatusBar(action, false);
183 }
184
185 void ChannelView::mouseMoveEvent(QMouseEvent *event) {
186     QListView::mouseMoveEvent(event);
187     const QModelIndex index = indexAt(event->pos());
188     if (index.isValid()) setCursor(Qt::PointingHandCursor);
189     else unsetCursor();
190 }
191
192 void ChannelView::leaveEvent(QEvent *event) {
193     QListView::leaveEvent(event);
194     // channelsModel->clearHover();
195 }
196
197 void ChannelView::itemEntered(const QModelIndex &index) {
198     // channelsModel->setHoveredRow(index.row());
199 }
200
201 void ChannelView::itemActivated(const QModelIndex &index) {
202     ChannelModel::ItemTypes itemType = channelsModel->typeForIndex(index);
203     if (itemType == ChannelModel::ItemChannel) {
204         YTUser *user = channelsModel->userForIndex(index);
205         SearchParams *params = new SearchParams();
206         params->setAuthor(user->getUserId());
207         params->setSortBy(SearchParams::SortByNewest);
208         params->setTransient(true);
209         YTSearch *videoSource = new YTSearch(params, this);
210         emit activated(videoSource);
211         user->updateWatched();
212     } else if (itemType == ChannelModel::ItemAggregate) {
213         AggregateVideoSource *videoSource = new AggregateVideoSource(this);
214         videoSource->setName(tr("All Videos"));
215         emit activated(videoSource);
216     } else if (itemType == ChannelModel::ItemUnwatched) {
217         AggregateVideoSource *videoSource = new AggregateVideoSource(this);
218         videoSource->setName(tr("Unwatched Videos"));
219         videoSource->setUnwatched(true);
220         emit activated(videoSource);
221     }
222 }
223
224 void ChannelView::paintEvent(QPaintEvent *event) {
225     if (model()->rowCount() < 3) {
226         QString msg;
227         if (!errorMessage.isEmpty())
228             msg = errorMessage;
229         else if (showUpdated)
230             msg = tr("There are no updated subscriptions at this time.");
231         else
232             msg = tr("You have no subscriptions. "
233                      "Use the star symbol to subscribe to channels.");
234         PainterUtils::centeredMessage(msg, viewport());
235     } else QListView::paintEvent(event);
236     PainterUtils::topShadow(viewport());
237 }
238
239 void ChannelView::toggleShowUpdated(bool enable) {
240     showUpdated = enable;
241     updateQuery(true);
242     QSettings settings;
243     settings.setValue(showUpdatedKey, showUpdated);
244 }
245
246 void ChannelView::updateQuery(bool transition) {
247     errorMessage.clear();
248     if (!Database::exists()) return;
249
250     QString sql = "select user_id from subscriptions";
251     if (showUpdated)
252         sql += " where notify_count>0";
253
254     switch (sortBy) {
255     case SortByUpdated:
256         sql += " order by updated desc";
257         break;
258     case SortByAdded:
259         sql += " order by added desc";
260         break;
261     case SortByLastWatched:
262         sql += " order by watched desc";
263         break;
264     case SortByMostWatched:
265         sql += " order by views desc";
266         break;
267     default:
268         sql += " order by name collate nocase";
269         break;
270     }
271
272 #ifdef APP_EXTRA
273     if (transition)
274         Extra::fadeInWidget(this, this);
275 #endif
276
277     channelsModel->setQuery(sql, Database::instance().getConnection());
278     if (channelsModel->lastError().isValid()) {
279         qWarning() << channelsModel->lastError().text();
280         errorMessage = channelsModel->lastError().text();
281     }
282 }
283
284 void ChannelView::setSortBy(SortBy sortBy) {
285     this->sortBy = sortBy;
286     updateQuery(true);
287     QSettings settings;
288     settings.setValue(sortByKey, (int)sortBy);
289 }
290
291 void ChannelView::markAllAsWatched() {
292     ChannelAggregator::instance()->markAllAsWatched();
293     updateQuery();
294     markAsWatchedAction->setEnabled(false);
295 }
296
297 void ChannelView::unwatchedCountChanged(int count) {
298     markAsWatchedAction->setEnabled(count > 0);
299     channelsModel->updateUnwatched();
300     updateQuery();
301 }