+
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions minitube.pro
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ HEADERS += src/video.h \
src/yt3.h \
src/paginatedvideosource.h \
src/compatibility/qurlqueryhelper.h \
src/compatibility/pathsservice.h
src/compatibility/pathsservice.h \
src/channelcontroller.h
SOURCES += src/main.cpp \
src/searchlineedit.cpp \
src/urllineedit.cpp \
Expand Down Expand Up @@ -173,7 +174,8 @@ SOURCES += src/main.cpp \
src/ytchannel.cpp \
src/yt3.cpp \
src/paginatedvideosource.cpp \
src/compatibility/pathsservice.cpp
src/compatibility/pathsservice.cpp \
src/channelcontroller.cpp
RESOURCES += resources.qrc
DESTDIR = build/target/
OBJECTS_DIR = build/obj/
Expand Down
148 changes: 148 additions & 0 deletions src/channelcontroller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* $BEGIN_LICENSE

This file is part of Minitube.
Copyright 2015, Flavio Tordini <flavio.tordini@gmail.com>

Minitube is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Minitube is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Minitube. If not, see <http://www.gnu.org/licenses/>.

$END_LICENSE */

#include "aggregatevideosource.h"
#include "channelaggregator.h"
#include "channelcontroller.h"
#include "channelmodel.h"
#include "channelview.h"
#include "searchparams.h"
#include "ytchannel.h"
#include "ytsearch.h"

#include <QtDebug>

namespace {
static const char *sortByKey = "subscriptionsSortBy";
static const char *showUpdatedKey = "subscriptionsShowUpdated";
} // namespace

ChannelController::ChannelController(QObject *parent)
: QObject(parent),
channelModel(NULL) {
QSettings settings;
const ChannelModel::SortBy sortOrder = static_cast<ChannelModel::SortBy>(settings.value(sortByKey, ChannelModel::SortByName).toInt());
channelModel = new ChannelModel(this);
channelModel->setSortBy(sortOrder);
channelModel->toggleShowUpdated(settings.value(showUpdatedKey, false).toBool());
}

bool ChannelController::connectToView(ChannelView* channelsView) {
bool connected = false;
// Connect controller to the view
connected = connect(channelsView, SIGNAL(onToggleShowUpdated(bool)),
SLOT(toggleShowUpdated(bool)));
connected = connected && connect(channelsView, SIGNAL(onSortingOrderChanged(int)),
SLOT(sortingOrderChanged(int)));
connected = connected && connect(channelsView, SIGNAL(onUnwatchedCountChanged(int)),
SLOT(unwatchedCountChanged(int)));
connected = connected && connect(channelsView, SIGNAL(onMarkAllAsWatched()),
SLOT(markAllAsWatched()));
connected = connected && connect(channelsView, SIGNAL(onBeforeAppearance()),
SLOT(onBeforeAppearance()));
connected = connected && connect(channelsView, SIGNAL(onAppeared()),
SLOT(onAppeared()));
connected = connected && connect(channelsView, SIGNAL(onBeforeDisappearance()),
SLOT(onBeforeDisappearance()));
connected = connected && connect(channelsView, SIGNAL(onDisappeared()),
SLOT(onDisappeared()));
connected = connected && connect(channelsView, SIGNAL(onChannelActivated(YTChannel *)),
SLOT(channelActivated(YTChannel *)));
connected = connected && connect(channelsView, SIGNAL(onVideoActivated(const QString &, bool)),
SLOT(videoActivated(const QString &, bool)));

connected = connected && connect(ChannelAggregator::instance(), SIGNAL(channelChanged(YTChannel*)),
channelModel, SLOT(updateChannel(YTChannel*)));
connected = connected && connect(ChannelAggregator::instance(), SIGNAL(unwatchedCountChanged(int)),
channelsView, SLOT(unwatchedCountChanged(int)));

connected = connected && connect(channelsView, SLOT(onChannelUnsubscribe()),
ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));
connected = connected && connect(channelsView, SLOT(onMarkChannelWatched()),
ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));

connect(channelsView, SIGNAL(viewportEntered()),
SLOT(clearHover()));
return connected;
}

void ChannelController::updateModelData() {
channelModel->updateData();
}

//
void ChannelController::toggleShowUpdated(bool enable) {
channelModel->toggleShowUpdated(enable);
updateModelData();
QSettings settings;
settings.setValue(showUpdatedKey, enable);
}

void ChannelController::sortingOrderChanged(int sortOrder) {
channelModel->setSortBy(static_cast<ChannelModel::SortBy>(sortOrder));
updateModelData();
QSettings settings;
settings.setValue(sortByKey, (int)sortOrder);
}

void ChannelController::unwatchedCountChanged(int) {
//channelModel->updateUnwatched();
}

void ChannelController::markAllAsWatched() {
ChannelAggregator::instance()->markAllAsWatched();
}

void ChannelController::onBeforeAppearance() {
updateModelData();
}

void ChannelController::onAppeared() {
ChannelAggregator::instance()->start();
}

void ChannelController::onBeforeDisappearance() {
ChannelAggregator::instance()->stop();
}

void ChannelController::onDisappeared() {
}

void ChannelController::channelActivated(YTChannel *channel) {
SearchParams *params = new SearchParams();
params->setChannelId(channel->getChannelId());
params->setSortBy(SearchParams::SortByNewest);
params->setTransient(true);
YTSearch *videoSource = new YTSearch(params, this);
videoSource->setAsyncDetails(true);
emit activated(videoSource);
channel->updateWatched();
}

void ChannelController::videoActivated(const QString &title, bool unwatched) {
AggregateVideoSource * const videoSource = new AggregateVideoSource(this);
videoSource->setName(title);
videoSource->setUnwatched(unwatched);
emit activated(videoSource);
}

void ChannelController::clearHover() {
channelModel->clearHover();
}
63 changes: 63 additions & 0 deletions src/channelcontroller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* $BEGIN_LICENSE

This file is part of Minitube.
Copyright 2015, Flavio Tordini <flavio.tordini@gmail.com>

Minitube is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Minitube is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Minitube. If not, see <http://www.gnu.org/licenses/>.

$END_LICENSE */

#ifndef CHANNELCONTROLLER_H
#define CHANNELCONTROLLER_H

#include <QtCore>

class ChannelModel;
class ChannelView;
class VideoSource;
class YTChannel;

class ChannelController : public QObject {

Q_OBJECT

public:
explicit ChannelController(QObject *parent = NULL);

ChannelModel *model() const { return channelModel; }
bool connectToView(ChannelView* channelsView);

public slots:
void toggleShowUpdated(bool enable);
void sortingOrderChanged(int sortOrder);
void unwatchedCountChanged(int count);
void markAllAsWatched();
void onBeforeAppearance();
void onAppeared();
void onBeforeDisappearance();
void onDisappeared();
void channelActivated(YTChannel *channel);
void videoActivated(const QString &title, bool unwatched);
void clearHover();

signals:
void activated(VideoSource *videoSource);

private:
void updateModelData();

ChannelModel *channelModel;
};

#endif // CHANNELCONTROLLER_H
51 changes: 45 additions & 6 deletions src/channelmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,27 @@ along with Minitube. If not, see <http://www.gnu.org/licenses/>.
$END_LICENSE */

#include "channelmodel.h"
#include "channelaggregator.h"
#include "database.h"
#include "ytchannel.h"

namespace {
static const int channelOffset = 2;
}

ChannelModel::ChannelModel(QObject *parent) :
QAbstractListModel(parent),
hoveredRow(-1) { }

ChannelModel::ChannelModel(QObject *parent)
: QAbstractListModel(parent)
, hoveredRow(-1)
, sortBy(SortByName)
, showUpdated(false) {
}

int ChannelModel::rowCount(const QModelIndex &) const {
return channels.isEmpty() ? 0 : channelOffset + channels.size();
}

QVariant ChannelModel::data(const QModelIndex &index, int role) const {
switch (role) {

case ChannelModel::ItemTypeRole:
return typeForIndex(index);

Expand All @@ -49,7 +54,6 @@ QVariant ChannelModel::data(const QModelIndex &index, int role) const {
case Qt::StatusTipRole:
if (typeForIndex(index) == ChannelModel::ItemChannel)
return channelForIndex(index)->getDescription();

}

return QVariant();
Expand Down Expand Up @@ -145,3 +149,38 @@ void ChannelModel::clearHover() {
emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, 0 ) );
hoveredRow = -1;
}

void ChannelModel::updateData() {
if (!Database::exists()) return;

QString sql = "select user_id from subscriptions";
if (shouldShowUpdated())
sql += " where notify_count>0";

switch (getSortingOrder()) {
case SortByUpdated:
sql += " order by updated desc";
break;
case SortByAdded:
sql += " order by added desc";
break;
case SortByLastWatched:
sql += " order by watched desc";
break;
case SortByMostWatched:
sql += " order by views desc";
break;
default:
sql += " order by name collate nocase";
break;
}

setQuery(sql, Database::instance().getConnection());
if (sqlError.isValid()) {
qWarning() << sqlError.text();
}
}

int ChannelModel::getUnwatchedCount() const {
return ChannelAggregator::instance()->getUnwatchedCount();
}
21 changes: 19 additions & 2 deletions src/channelmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,33 @@ class ChannelModel : public QAbstractListModel {
ItemUnwatched
};

enum SortBy {
SortByName = 0,
SortByAdded,
SortByUpdated,
SortByLastWatched,
SortByMostWatched
};

void setQuery(const QString &query, const QSqlDatabase &db);
QSqlError lastError() const;
ItemTypes typeForIndex(const QModelIndex &index) const;
YTChannel* channelForIndex(const QModelIndex &index) const;
void setHoveredRow(int row);
void clearHover();

SortBy getSortingOrder() const { return sortBy; }
bool shouldShowUpdated() const { return showUpdated; }
void setSortBy(SortBy sortingOrder) { sortBy = sortingOrder; }
void toggleShowUpdated(bool enable) { showUpdated = enable; }
int getUnwatchedCount() const;

int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;

void updateData();

public slots:
void clearHover();
void updateSender();
void updateChannel(YTChannel *channel);
void updateUnwatched();
Expand All @@ -65,7 +81,8 @@ public slots:
QList<YTChannel*> channels;
int hoveredRow;
QSqlError sqlError;

SortBy sortBy;
bool showUpdated;
};

#endif // CHANNELMODEL_H
3 changes: 3 additions & 0 deletions src/channelsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class ChannelsModel : public QSqlQueryModel {
ItemAggregate
};

signals:
void stateChanged();

public slots:
void clearHover();

Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载