Move some common code to superclasses.
This commit is contained in:
parent
c625ba2301
commit
6fcdd5351c
@ -23,13 +23,21 @@
|
||||
Feed::Feed(RootItem *parent)
|
||||
: RootItem(parent), m_url(QString()), m_status(Normal), m_autoUpdateType(DefaultAutoUpdate),
|
||||
m_autoUpdateInitialInterval(DEFAULT_AUTO_UPDATE_INTERVAL), m_autoUpdateRemainingInterval(DEFAULT_AUTO_UPDATE_INTERVAL),
|
||||
m_totalCount(0), m_unreadCount(0) {
|
||||
m_totalCount(0), m_unreadCount(0), m_customId(NO_PARENT_CATEGORY) {
|
||||
setKind(RootItemKind::Feed);
|
||||
}
|
||||
|
||||
Feed::~Feed() {
|
||||
}
|
||||
|
||||
int Feed::customId() const {
|
||||
return m_customId;
|
||||
}
|
||||
|
||||
void Feed::setCustomId(int custom_id) {
|
||||
m_customId = custom_id;
|
||||
}
|
||||
|
||||
QVariant Feed::data(int column, int role) const {
|
||||
switch (role) {
|
||||
case Qt::ForegroundRole:
|
||||
|
@ -100,6 +100,11 @@ class Feed : public RootItem {
|
||||
m_url = url;
|
||||
}
|
||||
|
||||
int customId() const;
|
||||
void setCustomId(int custom_id);
|
||||
|
||||
virtual int messageForeignKeyId() const = 0;
|
||||
|
||||
private:
|
||||
QString m_url;
|
||||
Status m_status;
|
||||
@ -108,6 +113,7 @@ class Feed : public RootItem {
|
||||
int m_autoUpdateRemainingInterval;
|
||||
int m_totalCount;
|
||||
int m_unreadCount;
|
||||
int m_customId;
|
||||
};
|
||||
|
||||
#endif // FEED_H
|
||||
|
@ -204,7 +204,8 @@ class RootItem : public QObject {
|
||||
m_icon = icon;
|
||||
}
|
||||
|
||||
// Each item has some kind of id. Usually taken from primary key attribute from DB.
|
||||
// This ALWAYS represents primary column number/ID under which
|
||||
// the item is stored in DB.
|
||||
inline int id() const {
|
||||
return m_id;
|
||||
}
|
||||
|
@ -21,8 +21,10 @@
|
||||
#include "miscellaneous/application.h"
|
||||
#include "miscellaneous/textfactory.h"
|
||||
#include "services/abstract/category.h"
|
||||
#include "services/abstract/feed.h"
|
||||
#include "services/abstract/recyclebin.h"
|
||||
|
||||
#include <QSqlTableModel>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlError>
|
||||
|
||||
@ -135,6 +137,17 @@ void ServiceRoot::requestItemRemoval(RootItem *item) {
|
||||
emit itemRemovalRequested(item);
|
||||
}
|
||||
|
||||
QStringList ServiceRoot::textualFeedIds(const QList<Feed*> &feeds) const {
|
||||
QStringList stringy_ids;
|
||||
stringy_ids.reserve(feeds.size());
|
||||
|
||||
foreach (const Feed *feed, feeds) {
|
||||
stringy_ids.append(QString("'%1'").arg(QString::number(feed->messageForeignKeyId())));
|
||||
}
|
||||
|
||||
return stringy_ids;
|
||||
}
|
||||
|
||||
int ServiceRoot::accountId() const {
|
||||
return m_accountId;
|
||||
}
|
||||
@ -143,6 +156,22 @@ void ServiceRoot::setAccountId(int account_id) {
|
||||
m_accountId = account_id;
|
||||
}
|
||||
|
||||
bool ServiceRoot::loadMessagesForItem(RootItem *item, QSqlTableModel *model) {
|
||||
if (item->kind() == RootItemKind::Bin) {
|
||||
model->setFilter(QString("is_deleted = 1 AND is_pdeleted = 0 AND account_id = %1").arg(QString::number(accountId())));
|
||||
}
|
||||
else {
|
||||
QList<Feed*> children = item->getSubTreeFeeds();
|
||||
QString filter_clause = textualFeedIds(children).join(QSL(", "));
|
||||
|
||||
model->setFilter(QString("feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = %2").arg(filter_clause,
|
||||
QString::number(accountId())));
|
||||
qDebug("Loading messages from feeds: %s.", qPrintable(filter_clause));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ServiceRoot::onBeforeSetMessagesRead(RootItem *selected_item, const QList<Message> &messages, RootItem::ReadStatus read) {
|
||||
Q_UNUSED(messages)
|
||||
Q_UNUSED(read)
|
||||
|
@ -95,7 +95,7 @@ class ServiceRoot : public RootItem {
|
||||
// and then use method QSqlTableModel::setFilter(....).
|
||||
// NOTE: It would be more preferable if all messages are downloaded
|
||||
// right when feeds are updated.
|
||||
virtual bool loadMessagesForItem(RootItem *item, QSqlTableModel *model) = 0;
|
||||
virtual bool loadMessagesForItem(RootItem *item, QSqlTableModel *model);
|
||||
|
||||
// Called BEFORE this read status update (triggered by user in message list) is stored in DB,
|
||||
// when false is returned, change is aborted.
|
||||
@ -164,6 +164,8 @@ class ServiceRoot : public RootItem {
|
||||
virtual void addNewCategory() = 0;
|
||||
|
||||
protected:
|
||||
QStringList textualFeedIds(const QList<Feed*> &feeds) const;
|
||||
|
||||
// Takes lists of feeds/categories and assembles them into the tree structure.
|
||||
void assembleCategories(Assignment categories);
|
||||
void assembleFeeds(Assignment feeds);
|
||||
|
@ -605,6 +605,10 @@ bool StandardFeed::editItself(StandardFeed *new_feed_data) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int StandardFeed::messageForeignKeyId() const {
|
||||
return id();
|
||||
}
|
||||
|
||||
int StandardFeed::updateMessages(const QList<Message> &messages) {
|
||||
int feed_id = id();
|
||||
int updated_messages = 0;
|
||||
|
@ -90,6 +90,8 @@ class StandardFeed : public Feed {
|
||||
bool addItself(RootItem *parent);
|
||||
bool editItself(StandardFeed *new_feed_data);
|
||||
|
||||
int messageForeignKeyId() const;
|
||||
|
||||
// Other getters/setters.
|
||||
inline Type type() const {
|
||||
return m_type;
|
||||
|
@ -445,17 +445,6 @@ void StandardServiceRoot::exportFeeds() {
|
||||
form.data()->exec();
|
||||
}
|
||||
|
||||
QStringList StandardServiceRoot::textualFeedIds(const QList<Feed*> &feeds) {
|
||||
QStringList stringy_ids;
|
||||
stringy_ids.reserve(feeds.size());
|
||||
|
||||
foreach (Feed *feed, feeds) {
|
||||
stringy_ids.append(QString("'%1'").arg(QString::number(feed->id())));
|
||||
}
|
||||
|
||||
return stringy_ids;
|
||||
}
|
||||
|
||||
QList<QAction*> StandardServiceRoot::addItemMenu() {
|
||||
return QList<QAction*>();
|
||||
}
|
||||
@ -478,19 +467,3 @@ QList<QAction*> StandardServiceRoot::serviceMenu() {
|
||||
QList<QAction*> StandardServiceRoot::contextMenu() {
|
||||
return serviceMenu();
|
||||
}
|
||||
|
||||
bool StandardServiceRoot::loadMessagesForItem(RootItem *item, QSqlTableModel *model) {
|
||||
if (item->kind() == RootItemKind::Bin) {
|
||||
model->setFilter(QString("is_deleted = 1 AND is_pdeleted = 0 AND account_id = %1").arg(QString::number(accountId())));
|
||||
}
|
||||
else {
|
||||
QList<Feed*> children = item->getSubTreeFeeds();
|
||||
QString filter_clause = textualFeedIds(children).join(QSL(", "));
|
||||
|
||||
model->setFilter(QString(QSL("feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = %2")).arg(filter_clause,
|
||||
QString::number(accountId())));
|
||||
qDebug("Loading messages from feeds: %s.", qPrintable(filter_clause));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -67,9 +67,6 @@ class StandardServiceRoot : public ServiceRoot {
|
||||
// Returns context menu.
|
||||
QList<QAction*> contextMenu();
|
||||
|
||||
// Message stuff.
|
||||
bool loadMessagesForItem(RootItem *item, QSqlTableModel *model);
|
||||
|
||||
// Returns context specific menu actions for given feed.
|
||||
QList<QAction*> getContextMenuForFeed(StandardFeed *feed);
|
||||
|
||||
@ -93,10 +90,6 @@ class StandardServiceRoot : public ServiceRoot {
|
||||
private:
|
||||
void checkArgumentsForFeedAdding();
|
||||
|
||||
// Returns converted ids of given feeds
|
||||
// which are suitable as IN clause for SQL queries.
|
||||
QStringList textualFeedIds(const QList<Feed *> &feeds);
|
||||
|
||||
RecycleBin *m_recycleBin;
|
||||
QAction *m_actionExportFeeds;
|
||||
QAction *m_actionImportFeeds;
|
||||
|
@ -35,7 +35,7 @@
|
||||
|
||||
|
||||
TtRssFeed::TtRssFeed(RootItem *parent)
|
||||
: Feed(parent), m_customId(NO_PARENT_CATEGORY) {
|
||||
: Feed(parent) {
|
||||
}
|
||||
|
||||
TtRssFeed::TtRssFeed(const QSqlRecord &record) : Feed(NULL) {
|
||||
@ -57,6 +57,10 @@ QString TtRssFeed::hashCode() const {
|
||||
QString::number(customId());
|
||||
}
|
||||
|
||||
int TtRssFeed::messageForeignKeyId() const {
|
||||
return customId();
|
||||
}
|
||||
|
||||
TtRssServiceRoot *TtRssFeed::serviceRoot() const {
|
||||
return qobject_cast<TtRssServiceRoot*>(getParentServiceRoot());
|
||||
}
|
||||
@ -241,14 +245,6 @@ bool TtRssFeed::cleanMessages(bool clear_only_read) {
|
||||
return serviceRoot()->cleanFeeds(QList<Feed*>() << this, clear_only_read);
|
||||
}
|
||||
|
||||
int TtRssFeed::customId() const {
|
||||
return m_customId;
|
||||
}
|
||||
|
||||
void TtRssFeed::setCustomId(int custom_id) {
|
||||
m_customId = custom_id;
|
||||
}
|
||||
|
||||
bool TtRssFeed::editItself(TtRssFeed *new_feed_data) {
|
||||
QSqlDatabase database = qApp->database()->connection("aa", DatabaseFactory::FromSettings);
|
||||
QSqlQuery query_update(database);
|
||||
|
@ -35,6 +35,8 @@ class TtRssFeed : public Feed {
|
||||
|
||||
QString hashCode() const;
|
||||
|
||||
int messageForeignKeyId() const;
|
||||
|
||||
TtRssServiceRoot *serviceRoot() const;
|
||||
|
||||
QVariant data(int column, int role) const;
|
||||
@ -52,18 +54,11 @@ class TtRssFeed : public Feed {
|
||||
|
||||
bool markAsReadUnread(ReadStatus status);
|
||||
bool cleanMessages(bool clear_only_read);
|
||||
|
||||
int customId() const;
|
||||
void setCustomId(int custom_id);
|
||||
|
||||
bool editItself(TtRssFeed *new_feed_data);
|
||||
|
||||
private:
|
||||
bool removeItself();
|
||||
|
||||
int updateMessages(const QList<Message> &messages);
|
||||
|
||||
int m_customId;
|
||||
};
|
||||
|
||||
#endif // TTRSSFEED_H
|
||||
|
@ -177,22 +177,6 @@ RecycleBin *TtRssServiceRoot::recycleBin() const {
|
||||
return m_recycleBin;
|
||||
}
|
||||
|
||||
bool TtRssServiceRoot::loadMessagesForItem(RootItem *item, QSqlTableModel *model) {
|
||||
if (item->kind() == RootItemKind::Bin) {
|
||||
model->setFilter(QString("is_deleted = 1 AND is_pdeleted = 0 AND account_id = %1").arg(QString::number(accountId())));
|
||||
}
|
||||
else {
|
||||
QList<Feed*> children = item->getSubTreeFeeds();
|
||||
QString filter_clause = textualFeedIds(children).join(QSL(", "));
|
||||
|
||||
model->setFilter(QString(QSL("feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = '%2'")).arg(filter_clause,
|
||||
QString::number(accountId())));
|
||||
qDebug("Loading messages from feeds: %s.", qPrintable(filter_clause));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QList<QAction*> TtRssServiceRoot::serviceMenu() {
|
||||
if (m_serviceMenu.isEmpty()) {
|
||||
m_actionSyncIn = new QAction(qApp->icons()->fromTheme(QSL("item-sync")), tr("Sync in"), this);
|
||||
@ -603,17 +587,6 @@ QStringList TtRssServiceRoot::customIDsOfMessages(const QList<Message> &messages
|
||||
return list;
|
||||
}
|
||||
|
||||
QStringList TtRssServiceRoot::textualFeedIds(const QList<Feed*> &feeds) {
|
||||
QStringList stringy_ids;
|
||||
stringy_ids.reserve(feeds.size());
|
||||
|
||||
foreach (const Feed *feed, feeds) {
|
||||
stringy_ids.append(QString("'%1'").arg(QString::number(qobject_cast<const TtRssFeed*>(feed)->customId())));
|
||||
}
|
||||
|
||||
return stringy_ids;
|
||||
}
|
||||
|
||||
void TtRssServiceRoot::removeOldFeedTree(bool including_messages) {
|
||||
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
|
||||
QSqlQuery query(database);
|
||||
|
@ -58,7 +58,6 @@ class TtRssServiceRoot : public ServiceRoot {
|
||||
|
||||
RecycleBin *recycleBin() const;
|
||||
|
||||
bool loadMessagesForItem(RootItem *item, QSqlTableModel *model);
|
||||
bool onBeforeSetMessagesRead(RootItem *selected_item, const QList<Message> &messages, ReadStatus read);
|
||||
bool onBeforeSwitchMessageImportance(RootItem *selected_item, const QList<QPair<Message,RootItem::Importance> > &changes);
|
||||
|
||||
@ -84,10 +83,6 @@ class TtRssServiceRoot : public ServiceRoot {
|
||||
QStringList customIDsOfMessages(const QList<QPair<Message,Importance> > &changes);
|
||||
QStringList customIDsOfMessages(const QList<Message> &messages);
|
||||
|
||||
// Returns converted ids of given feeds
|
||||
// which are suitable as IN clause for SQL queries.
|
||||
QStringList textualFeedIds(const QList<Feed*> &feeds);
|
||||
|
||||
void removeOldFeedTree(bool including_messages);
|
||||
void removeLeftOverMessages();
|
||||
void cleanAllItems();
|
||||
|
Loading…
x
Reference in New Issue
Block a user