code refactoring, removing duplicate code

This commit is contained in:
Martin Rotter 2021-02-26 13:50:47 +01:00
parent 1118d278d6
commit e0cb38f318
9 changed files with 50 additions and 30 deletions

View file

@ -0,0 +1,15 @@
#ifndef TYPEDEFS_H
#define TYPEDEFS_H
#include <QList>
#include <QPair>
#include "core/message.h"
#include "services/abstract/rootitem.h"
// First item here represents ID (int, primary key) of the item.
typedef QList<QPair<int, RootItem*>> Assignment;
typedef QPair<int, RootItem*> AssignmentItem;
typedef QPair<Message, RootItem::Importance> ImportanceChange;
#endif // TYPEDEFS_H

View file

@ -44,6 +44,7 @@ HEADERS += core/feeddownloader.h \
core/messagesmodelsqllayer.h \ core/messagesmodelsqllayer.h \
core/messagesproxymodel.h \ core/messagesproxymodel.h \
definitions/definitions.h \ definitions/definitions.h \
definitions/typedefs.h \
dynamic-shortcuts/dynamicshortcuts.h \ dynamic-shortcuts/dynamicshortcuts.h \
dynamic-shortcuts/dynamicshortcutswidget.h \ dynamic-shortcuts/dynamicshortcutswidget.h \
dynamic-shortcuts/shortcutcatcher.h \ dynamic-shortcuts/shortcutcatcher.h \

View file

@ -6,17 +6,18 @@
#include "services/abstract/rootitem.h" #include "services/abstract/rootitem.h"
#include "core/messagefilter.h" #include "core/messagefilter.h"
#include "definitions/typedefs.h"
#include "miscellaneous/textfactory.h" #include "miscellaneous/textfactory.h"
#include "services/abstract/category.h" #include "services/abstract/category.h"
#include "services/abstract/label.h" #include "services/abstract/label.h"
#include "services/abstract/serviceroot.h"
#include "services/greader/greaderserviceroot.h"
#include "services/standard/standardfeed.h" #include "services/standard/standardfeed.h"
#include <QMultiMap> #include <QMultiMap>
#include <QSqlError> #include <QSqlError>
#include <QSqlQuery> #include <QSqlQuery>
class ServiceRoot;
class DatabaseQueries { class DatabaseQueries {
public: public:

View file

@ -336,6 +336,14 @@ void ServiceRoot::restoreCustomFeedsData(const QMap<QString, QVariantMap>& data,
} }
} }
QSqlDatabase ServiceRoot::internalDatabase() const {
return qApp->database()->connection(metaObject()->className());
}
QList<MessageFilter*> ServiceRoot::internalFilters() const {
return qApp->feedReader()->messageFilters();
}
QNetworkProxy ServiceRoot::networkProxy() const { QNetworkProxy ServiceRoot::networkProxy() const {
return m_networkProxy; return m_networkProxy;
} }

View file

@ -6,6 +6,8 @@
#include "services/abstract/rootitem.h" #include "services/abstract/rootitem.h"
#include "core/message.h" #include "core/message.h"
#include "definitions/typedefs.h"
#include "miscellaneous/databasequeries.h"
#include <QNetworkProxy> #include <QNetworkProxy>
#include <QPair> #include <QPair>
@ -19,11 +21,6 @@ class QAction;
class MessagesModel; class MessagesModel;
class CacheForServiceRoot; class CacheForServiceRoot;
// First item here represents ID (int, primary key) of the item.
typedef QList<QPair<int, RootItem*>> Assignment;
typedef QPair<int, RootItem*> AssignmentItem;
typedef QPair<Message, RootItem::Importance> ImportanceChange;
struct CustomDatabaseEntry { struct CustomDatabaseEntry {
public: public:
CustomDatabaseEntry(const QString& name, bool encrypted = false) : m_name(name), m_encrypted(encrypted) {} CustomDatabaseEntry(const QString& name, bool encrypted = false) : m_name(name), m_encrypted(encrypted) {}
@ -65,6 +62,9 @@ class ServiceRoot : public RootItem {
virtual QList<CustomDatabaseEntry> customDatabaseAttributes() const; virtual QList<CustomDatabaseEntry> customDatabaseAttributes() const;
virtual void saveAccountDataToDatabase(); virtual void saveAccountDataToDatabase();
template<typename Categ, typename Feed>
void loadFromDatabase();
// Returns list of specific actions for "Add new item" main window menu. // Returns list of specific actions for "Add new item" main window menu.
// So typical list of returned actions could look like: // So typical list of returned actions could look like:
// a) Add new feed // a) Add new feed
@ -255,6 +255,11 @@ class ServiceRoot : public RootItem {
virtual QMap<QString, QVariantMap> storeCustomFeedsData(); virtual QMap<QString, QVariantMap> storeCustomFeedsData();
virtual void restoreCustomFeedsData(const QMap<QString, QVariantMap>& data, const QHash<QString, Feed*>& feeds); virtual void restoreCustomFeedsData(const QMap<QString, QVariantMap>& data, const QHash<QString, Feed*>& feeds);
// Helper methods to keep "application.h" inclusion
// out of this file.
QSqlDatabase internalDatabase() const;
QList<MessageFilter*> internalFilters() const;
protected: protected:
RecycleBin* m_recycleBin; RecycleBin* m_recycleBin;
ImportantNode* m_importantNode; ImportantNode* m_importantNode;
@ -267,4 +272,15 @@ class ServiceRoot : public RootItem {
ServiceRoot::LabelOperation operator|(ServiceRoot::LabelOperation lhs, ServiceRoot::LabelOperation rhs); ServiceRoot::LabelOperation operator|(ServiceRoot::LabelOperation lhs, ServiceRoot::LabelOperation rhs);
ServiceRoot::LabelOperation operator&(ServiceRoot::LabelOperation lhs, ServiceRoot::LabelOperation rhs); ServiceRoot::LabelOperation operator&(ServiceRoot::LabelOperation lhs, ServiceRoot::LabelOperation rhs);
template<typename Categ, typename Fee>
void ServiceRoot::loadFromDatabase() {
QSqlDatabase database = internalDatabase();
Assignment categories = DatabaseQueries::getCategories<Categ>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<Fee>(database, internalFilters(), accountId());
auto labels = DatabaseQueries::getLabels(database, accountId());
performInitialAssembly(categories, feeds, labels);
}
#endif // SERVICEROOT_H #endif // SERVICEROOT_H

View file

@ -43,7 +43,7 @@ StandardServiceRoot::~StandardServiceRoot() {
void StandardServiceRoot::start(bool freshly_activated) { void StandardServiceRoot::start(bool freshly_activated) {
loadFromDatabase(); loadFromDatabase();
if (freshly_activated && getSubTree(RootItem::Kind::Feed).isEmpty()) { if (freshly_activated && getSubTreeFeeds().isEmpty()) {
// In other words, if there are no feeds or categories added. // In other words, if there are no feeds or categories added.
if (MessageBox::show(qApp->mainFormWidget(), QMessageBox::Question, QObject::tr("Load initial set of feeds"), if (MessageBox::show(qApp->mainFormWidget(), QMessageBox::Question, QObject::tr("Load initial set of feeds"),
tr("This new account does not include any feeds. You can now add default set of feeds."), tr("This new account does not include any feeds. You can now add default set of feeds."),
@ -92,10 +92,6 @@ bool StandardServiceRoot::canBeEdited() const {
return true; return true;
} }
bool StandardServiceRoot::canBeDeleted() const {
return true;
}
bool StandardServiceRoot::editViaGui() { bool StandardServiceRoot::editViaGui() {
FormEditStandardAccount form_pointer(qApp->mainFormWidget()); FormEditStandardAccount form_pointer(qApp->mainFormWidget());
@ -103,10 +99,6 @@ bool StandardServiceRoot::editViaGui() {
return true; return true;
} }
bool StandardServiceRoot::deleteViaGui() {
return ServiceRoot::deleteViaGui();
}
bool StandardServiceRoot::supportsFeedAdding() const { bool StandardServiceRoot::supportsFeedAdding() const {
return true; return true;
} }

View file

@ -28,9 +28,7 @@ class StandardServiceRoot : public ServiceRoot {
QString code() const; QString code() const;
bool canBeEdited() const; bool canBeEdited() const;
bool canBeDeleted() const;
bool editViaGui(); bool editViaGui();
bool deleteViaGui();
bool supportsFeedAdding() const; bool supportsFeedAdding() const;
bool supportsCategoryAdding() const; bool supportsCategoryAdding() const;

View file

@ -38,7 +38,7 @@ ServiceRoot::LabelOperation TtRssServiceRoot::supportedLabelOperations() const {
void TtRssServiceRoot::start(bool freshly_activated) { void TtRssServiceRoot::start(bool freshly_activated) {
if (!freshly_activated) { if (!freshly_activated) {
loadFromDatabase(); loadFromDatabase<Category, TtRssFeed>();
loadCacheFromFile(); loadCacheFromFile();
} }
@ -206,15 +206,6 @@ TtRssNetworkFactory* TtRssServiceRoot::network() const {
return m_network; return m_network;
} }
void TtRssServiceRoot::loadFromDatabase() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
Assignment categories = DatabaseQueries::getCategories<Category>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<TtRssFeed>(database, qApp->feedReader()->messageFilters(), accountId());
auto labels = DatabaseQueries::getLabels(database, accountId());
performInitialAssembly(categories, feeds, labels);
}
void TtRssServiceRoot::updateTitle() { void TtRssServiceRoot::updateTitle() {
QString host = QUrl(m_network->url()).host(); QString host = QUrl(m_network->url()).host();

View file

@ -75,8 +75,6 @@ class TtRssServiceRoot : public ServiceRoot, public CacheForServiceRoot {
virtual RootItem* obtainNewTreeForSyncIn() const; virtual RootItem* obtainNewTreeForSyncIn() const;
private: private:
void loadFromDatabase();
TtRssNetworkFactory* m_network; TtRssNetworkFactory* m_network;
}; };