3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "channelview.h"
22 #include "ytchannel.h"
24 #include "searchparams.h"
25 #include "channelmodel.h"
26 #include "channelitemdelegate.h"
28 #include "channelaggregator.h"
29 #include "aggregatevideosource.h"
30 #include "mainwindow.h"
31 #include "iconutils.h"
35 #include "channellistview.h"
38 static const QString sortByKey = "subscriptionsSortBy";
39 static const QString showUpdatedKey = "subscriptionsShowUpdated";
42 ChannelView::ChannelView(QWidget *parent) : View(parent),
46 QBoxLayout *layout = new QVBoxLayout(this);
48 layout->setSpacing(0);
50 listView = new ChannelListView();
51 listView->setItemDelegate(new ChannelItemDelegate(this));
53 channelsModel = new ChannelModel(this);
54 listView->setModel(channelsModel);
56 connect(listView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemActivated(const QModelIndex &)));
57 connect(listView, SIGNAL(contextMenu(QPoint)), SLOT(showContextMenu(QPoint)));
58 connect(listView, SIGNAL(viewportEntered()), channelsModel, SLOT(clearHover()));
60 layout->addWidget(listView);
64 connect(ChannelAggregator::instance(), SIGNAL(channelChanged(YTChannel*)),
65 channelsModel, SLOT(updateChannel(YTChannel*)));
66 connect(ChannelAggregator::instance(), SIGNAL(unwatchedCountChanged(int)),
67 SLOT(unwatchedCountChanged(int)));
69 unwatchedCountChanged(ChannelAggregator::instance()->getUnwatchedCount());
72 void ChannelView::setupActions() {
75 sortBy = static_cast<SortBy>(settings.value(sortByKey, SortByName).toInt());
77 QMenu *sortMenu = new QMenu(this);
78 QActionGroup *sortGroup = new QActionGroup(this);
80 QAction *sortByNameAction = new QAction(tr("Name"), this);
81 sortByNameAction->setActionGroup(sortGroup);
82 sortByNameAction->setCheckable(true);
83 if (sortBy == SortByName) sortByNameAction->setChecked(true);
84 connect(sortByNameAction, SIGNAL(triggered()), SLOT(setSortByName()));
85 sortMenu->addAction(sortByNameAction);
87 QAction *sortByUpdatedAction = new QAction(tr("Last Updated"), this);
88 sortByUpdatedAction->setActionGroup(sortGroup);
89 sortByUpdatedAction->setCheckable(true);
90 if (sortBy == SortByUpdated) sortByUpdatedAction->setChecked(true);
91 connect(sortByUpdatedAction, SIGNAL(triggered()), SLOT(setSortByUpdated()));
92 sortMenu->addAction(sortByUpdatedAction);
94 QAction *sortByAddedAction = new QAction(tr("Last Added"), this);
95 sortByAddedAction->setActionGroup(sortGroup);
96 sortByAddedAction->setCheckable(true);
97 if (sortBy == SortByAdded) sortByAddedAction->setChecked(true);
98 connect(sortByAddedAction, SIGNAL(triggered()), SLOT(setSortByAdded()));
99 sortMenu->addAction(sortByAddedAction);
101 QAction *sortByLastWatched = new QAction(tr("Last Watched"), this);
102 sortByLastWatched->setActionGroup(sortGroup);
103 sortByLastWatched->setCheckable(true);
104 if (sortBy == SortByLastWatched) sortByLastWatched->setChecked(true);
105 connect(sortByLastWatched, SIGNAL(triggered()), SLOT(setSortByLastWatched()));
106 sortMenu->addAction(sortByLastWatched);
108 QAction *sortByMostWatched = new QAction(tr("Most Watched"), this);
109 sortByMostWatched->setActionGroup(sortGroup);
110 sortByMostWatched->setCheckable(true);
111 if (sortBy == SortByMostWatched) sortByMostWatched->setChecked(true);
112 connect(sortByMostWatched, SIGNAL(triggered()), SLOT(setSortByMostWatched()));
113 sortMenu->addAction(sortByMostWatched);
115 QToolButton *sortButton = new QToolButton(this);
116 sortButton->setText(tr("Sort by"));
117 sortButton->setIcon(IconUtils::icon("sort"));
118 sortButton->setIconSize(QSize(16, 16));
119 sortButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
120 sortButton->setPopupMode(QToolButton::InstantPopup);
121 sortButton->setMenu(sortMenu);
122 QWidgetAction *widgetAction = new QWidgetAction(this);
123 widgetAction->setDefaultWidget(sortButton);
124 widgetAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O));
125 statusActions << widgetAction;
127 markAsWatchedAction = new QAction(
128 IconUtils::icon("mark-watched"), tr("Mark all as watched"), this);
129 markAsWatchedAction->setEnabled(false);
130 markAsWatchedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_W));
131 connect(markAsWatchedAction, SIGNAL(triggered()), SLOT(markAllAsWatched()));
132 statusActions << markAsWatchedAction;
134 showUpdated = settings.value(showUpdatedKey, false).toBool();
135 QAction *showUpdatedAction = new QAction(
136 IconUtils::icon("show-updated"), tr("Show Updated"), this);
137 showUpdatedAction->setCheckable(true);
138 showUpdatedAction->setChecked(showUpdated);
139 showUpdatedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_U));
140 connect(showUpdatedAction, SIGNAL(toggled(bool)), SLOT(toggleShowUpdated(bool)));
141 statusActions << showUpdatedAction;
143 for (QAction *action : statusActions) {
144 window()->addAction(action);
145 IconUtils::setupAction(action);
149 QString ChannelView::noSubscriptionsMessage() {
150 return tr("You have no subscriptions. "
151 "Use the star symbol to subscribe to channels.");
154 void ChannelView::appear() {
156 for (QAction* action : statusActions)
157 MainWindow::instance()->showActionInStatusBar(action, true);
159 ChannelAggregator::instance()->start();
162 void ChannelView::disappear() {
163 for (QAction* action : statusActions)
164 MainWindow::instance()->showActionInStatusBar(action, false);
167 void ChannelView::itemActivated(const QModelIndex &index) {
168 ChannelModel::ItemTypes itemType = channelsModel->typeForIndex(index);
169 if (itemType == ChannelModel::ItemChannel) {
170 YTChannel *channel = channelsModel->channelForIndex(index);
171 SearchParams *params = new SearchParams();
172 params->setChannelId(channel->getChannelId());
173 params->setSortBy(SearchParams::SortByNewest);
174 params->setTransient(true);
175 YTSearch *videoSource = new YTSearch(params);
176 videoSource->setAsyncDetails(true);
177 emit activated(videoSource);
178 channel->updateWatched();
179 } else if (itemType == ChannelModel::ItemAggregate) {
180 AggregateVideoSource *videoSource = new AggregateVideoSource();
181 videoSource->setName(tr("All Videos"));
182 emit activated(videoSource);
183 } else if (itemType == ChannelModel::ItemUnwatched) {
184 AggregateVideoSource *videoSource = new AggregateVideoSource();
185 videoSource->setName(tr("Unwatched Videos"));
186 videoSource->setUnwatched(true);
187 emit activated(videoSource);
191 void ChannelView::showContextMenu(const QPoint &point) {
192 const QModelIndex index = listView->indexAt(point);
193 if (!index.isValid()) return;
195 YTChannel *channel = channelsModel->channelForIndex(index);
196 if (!channel) return;
202 if (channel->getNotifyCount() > 0) {
203 QAction *markAsWatchedAction = menu.addAction(tr("Mark as Watched"), channel, SLOT(updateWatched()));
204 connect(markAsWatchedAction, SIGNAL(triggered()),
205 ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));
211 QAction *notificationsAction = menu.addAction(tr("Receive Notifications"), user, SLOT(unsubscribe()));
212 notificationsAction->setCheckable(true);
213 notificationsAction->setChecked(true);
216 QAction *unsubscribeAction = menu.addAction(tr("Unsubscribe"), channel, SLOT(unsubscribe()));
217 connect(unsubscribeAction, SIGNAL(triggered()),
218 ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));
220 menu.exec(mapToGlobal(point));
223 void ChannelView::toggleShowUpdated(bool enable) {
224 showUpdated = enable;
227 settings.setValue(showUpdatedKey, showUpdated);
230 void ChannelView::updateQuery(bool transition) {
231 Q_UNUSED(transition);
232 listView->clearErrorMessage();
234 if (!Database::exists()) {
235 listView->setErrorMessage(noSubscriptionsMessage());
239 QString sql = "select user_id from subscriptions";
241 sql += " where notify_count>0";
245 sql += " order by updated desc";
248 sql += " order by added desc";
250 case SortByLastWatched:
251 sql += " order by watched desc";
253 case SortByMostWatched:
254 sql += " order by views desc";
257 sql += " order by name collate nocase";
263 Extra::fadeInWidget(this, this);
266 channelsModel->setQuery(sql, Database::instance().getConnection());
267 if (channelsModel->lastError().isValid()) {
268 qWarning() << channelsModel->lastError().text();
269 listView->setErrorMessage(channelsModel->lastError().text());
270 } else if (channelsModel->rowCount() < 3) {
273 msg = tr("There are no updated subscriptions at this time.");
275 msg = noSubscriptionsMessage();
276 listView->setErrorMessage(msg);
280 void ChannelView::setSortBy(SortBy sortBy) {
281 this->sortBy = sortBy;
284 settings.setValue(sortByKey, (int)sortBy);
287 void ChannelView::markAllAsWatched() {
288 ChannelAggregator::instance()->markAllAsWatched();
290 markAsWatchedAction->setEnabled(false);
293 void ChannelView::unwatchedCountChanged(int count) {
294 markAsWatchedAction->setEnabled(count > 0);
295 channelsModel->updateUnwatched();