]> git.sur5r.net Git - minitube/blob - src/channelview.cpp
Imported Upstream version 2.2
[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     SortBy sortBy = static_cast<SortBy>(settings.value(sortByKey, SortByName).toInt());
99
100     QMenu *sortMenu = new QMenu(this);
101     QActionGroup *sortGroup = new QActionGroup(this);
102
103     QAction *sortByNameAction = new QAction(tr("Name"), this);
104     sortByNameAction->setActionGroup(sortGroup);
105     sortByNameAction->setCheckable(true);
106     if (sortBy == SortByName) sortByNameAction->setChecked(true);
107     connect(sortByNameAction, SIGNAL(triggered()), SLOT(setSortByName()));
108     sortMenu->addAction(sortByNameAction);
109
110     QAction *sortByUpdatedAction = new QAction(tr("Last Updated"), this);
111     sortByUpdatedAction->setActionGroup(sortGroup);
112     sortByUpdatedAction->setCheckable(true);
113     if (sortBy == SortByUpdated) sortByUpdatedAction->setChecked(true);
114     connect(sortByUpdatedAction, SIGNAL(triggered()), SLOT(setSortByUpdated()));
115     sortMenu->addAction(sortByUpdatedAction);
116
117     QAction *sortByAddedAction = new QAction(tr("Last Added"), this);
118     sortByAddedAction->setActionGroup(sortGroup);
119     sortByAddedAction->setCheckable(true);
120     if (sortBy == SortByAdded) sortByAddedAction->setChecked(true);
121     connect(sortByAddedAction, SIGNAL(triggered()), SLOT(setSortByAdded()));
122     sortMenu->addAction(sortByAddedAction);
123
124     QAction *sortByLastWatched = new QAction(tr("Last Watched"), this);
125     sortByLastWatched->setActionGroup(sortGroup);
126     sortByLastWatched->setCheckable(true);
127     if (sortBy == SortByLastWatched) sortByLastWatched->setChecked(true);
128     connect(sortByLastWatched, SIGNAL(triggered()), SLOT(setSortByLastWatched()));
129     sortMenu->addAction(sortByLastWatched);
130
131     QAction *sortByMostWatched = new QAction(tr("Most Watched"), this);
132     sortByMostWatched->setActionGroup(sortGroup);
133     sortByMostWatched->setCheckable(true);
134     if (sortBy == SortByMostWatched) sortByMostWatched->setChecked(true);
135     connect(sortByMostWatched, SIGNAL(triggered()), SLOT(setSortByMostWatched()));
136     sortMenu->addAction(sortByMostWatched);
137
138     QToolButton *sortButton = new QToolButton(this);
139     sortButton->setText(tr("Sort by"));
140     sortButton->setIcon(Utils::icon("sort"));
141     sortButton->setIconSize(QSize(16, 16));
142     sortButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
143     sortButton->setPopupMode(QToolButton::InstantPopup);
144     sortButton->setMenu(sortMenu);
145     QWidgetAction *widgetAction = new QWidgetAction(this);
146     widgetAction->setDefaultWidget(sortButton);
147     widgetAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O));
148     statusActions << widgetAction;
149
150     markAsWatchedAction = new QAction(
151                 Utils::icon("mark-watched"), tr("Mark all as watched"), this);
152     markAsWatchedAction->setEnabled(false);
153     markAsWatchedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_W));
154     connect(markAsWatchedAction, SIGNAL(triggered()), SLOT(markAllAsWatched()));
155     statusActions << markAsWatchedAction;
156
157     showUpdated = settings.value(showUpdatedKey, false).toBool();
158     QAction *showUpdatedAction = new QAction(
159                 Utils::icon("show-updated"), tr("Show Updated"), this);
160     showUpdatedAction->setCheckable(true);
161     showUpdatedAction->setChecked(showUpdated);
162     showUpdatedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_U));
163     connect(showUpdatedAction, SIGNAL(toggled(bool)), SLOT(toggleShowUpdated(bool)));
164     statusActions << showUpdatedAction;
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::mousePressEvent(QMouseEvent *event) {
186     if (event->button() == Qt::RightButton)
187         showContextMenu(event->pos());
188     else
189         QListView::mousePressEvent(event);
190 }
191
192 void ChannelView::mouseMoveEvent(QMouseEvent *event) {
193     QListView::mouseMoveEvent(event);
194     const QModelIndex index = indexAt(event->pos());
195     if (index.isValid()) setCursor(Qt::PointingHandCursor);
196     else unsetCursor();
197 }
198
199 void ChannelView::leaveEvent(QEvent *event) {
200     QListView::leaveEvent(event);
201     // channelsModel->clearHover();
202 }
203
204 void ChannelView::itemEntered(const QModelIndex &index) {
205     // channelsModel->setHoveredRow(index.row());
206 }
207
208 void ChannelView::itemActivated(const QModelIndex &index) {
209     ChannelModel::ItemTypes itemType = channelsModel->typeForIndex(index);
210     if (itemType == ChannelModel::ItemChannel) {
211         YTUser *user = channelsModel->userForIndex(index);
212         SearchParams *params = new SearchParams();
213         params->setAuthor(user->getUserId());
214         params->setSortBy(SearchParams::SortByNewest);
215         params->setTransient(true);
216         YTSearch *videoSource = new YTSearch(params, this);
217         emit activated(videoSource);
218         user->updateWatched();
219     } else if (itemType == ChannelModel::ItemAggregate) {
220         AggregateVideoSource *videoSource = new AggregateVideoSource(this);
221         videoSource->setName(tr("All Videos"));
222         emit activated(videoSource);
223     } else if (itemType == ChannelModel::ItemUnwatched) {
224         AggregateVideoSource *videoSource = new AggregateVideoSource(this);
225         videoSource->setName(tr("Unwatched Videos"));
226         videoSource->setUnwatched(true);
227         emit activated(videoSource);
228     }
229 }
230
231 void ChannelView::showContextMenu(const QPoint &point) {
232     const QModelIndex index = indexAt(point);
233     if (!index.isValid()) return;
234
235     YTUser *user = channelsModel->userForIndex(index);
236     if (!user) return;
237
238     unsetCursor();
239
240     QMenu menu;
241
242     if (user->getNotifyCount() > 0) {
243         QAction *markAsWatchedAction = menu.addAction(tr("Mark as Watched"), user, SLOT(updateWatched()));
244         connect(markAsWatchedAction, SIGNAL(triggered()),
245                 ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));
246         menu.addSeparator();
247     }
248
249     /*
250     // TODO
251     QAction *notificationsAction = menu.addAction(tr("Receive Notifications"), user, SLOT(unsubscribe()));
252     notificationsAction->setCheckable(true);
253     notificationsAction->setChecked(true);
254     */
255
256     QAction *unsubscribeAction = menu.addAction(tr("Unsubscribe"), user, SLOT(unsubscribe()));
257     connect(unsubscribeAction, SIGNAL(triggered()),
258             ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));
259
260     menu.exec(mapToGlobal(point));
261 }
262
263 void ChannelView::paintEvent(QPaintEvent *event) {
264     if (model()->rowCount() < 3) {
265         QString msg;
266         if (!errorMessage.isEmpty())
267             msg = errorMessage;
268         else if (showUpdated)
269             msg = tr("There are no updated subscriptions at this time.");
270         else
271             msg = tr("You have no subscriptions. "
272                      "Use the star symbol to subscribe to channels.");
273         PainterUtils::centeredMessage(msg, viewport());
274     } else QListView::paintEvent(event);
275     PainterUtils::topShadow(viewport());
276 }
277
278 void ChannelView::toggleShowUpdated(bool enable) {
279     showUpdated = enable;
280     updateQuery(true);
281     QSettings settings;
282     settings.setValue(showUpdatedKey, showUpdated);
283 }
284
285 void ChannelView::updateQuery(bool transition) {
286     errorMessage.clear();
287     if (!Database::exists()) return;
288
289     QString sql = "select user_id from subscriptions";
290     if (showUpdated)
291         sql += " where notify_count>0";
292
293     switch (sortBy) {
294     case SortByUpdated:
295         sql += " order by updated desc";
296         break;
297     case SortByAdded:
298         sql += " order by added desc";
299         break;
300     case SortByLastWatched:
301         sql += " order by watched desc";
302         break;
303     case SortByMostWatched:
304         sql += " order by views desc";
305         break;
306     default:
307         sql += " order by name collate nocase";
308         break;
309     }
310
311 #ifdef APP_EXTRA
312     if (transition)
313         Extra::fadeInWidget(this, this);
314 #endif
315
316     channelsModel->setQuery(sql, Database::instance().getConnection());
317     if (channelsModel->lastError().isValid()) {
318         qWarning() << channelsModel->lastError().text();
319         errorMessage = channelsModel->lastError().text();
320     }
321 }
322
323 void ChannelView::setSortBy(SortBy sortBy) {
324     this->sortBy = sortBy;
325     updateQuery(true);
326     QSettings settings;
327     settings.setValue(sortByKey, (int)sortBy);
328 }
329
330 void ChannelView::markAllAsWatched() {
331     ChannelAggregator::instance()->markAllAsWatched();
332     updateQuery();
333     markAsWatchedAction->setEnabled(false);
334 }
335
336 void ChannelView::unwatchedCountChanged(int count) {
337     markAsWatchedAction->setEnabled(count > 0);
338     channelsModel->updateUnwatched();
339     updateQuery();
340 }