adding and loading of probes works

This commit is contained in:
Martin Rotter 2023-08-09 12:41:48 +02:00
parent e952debbaf
commit 037633d977
8 changed files with 90 additions and 18 deletions

View File

@ -288,6 +288,55 @@ bool DatabaseQueries::createLabel(const QSqlDatabase& db, Label* label, int acco
return q.exec() && res;
}
void DatabaseQueries::createProbe(const QSqlDatabase& db, Search* probe, int account_id) {
QSqlQuery q(db);
q.setForwardOnly(true);
q.prepare(QSL("INSERT INTO Probes (name, color, fltr, account_id) "
"VALUES (:name, :color, :fltr, :account_id);"));
q.bindValue(QSL(":name"), probe->title());
q.bindValue(QSL(":fltr"), probe->filter());
q.bindValue(QSL(":color"), probe->color().name());
q.bindValue(QSL(":account_id"), account_id);
auto res = q.exec();
if (res && q.lastInsertId().isValid()) {
probe->setId(q.lastInsertId().toInt());
probe->setCustomId(QString::number(probe->id()));
}
else {
throw ApplicationException(q.lastError().text());
}
}
QList<Search*> DatabaseQueries::getProbesForAccount(const QSqlDatabase& db, int account_id) {
QList<Search*> probes;
QSqlQuery q(db);
q.setForwardOnly(true);
q.prepare(QSL("SELECT * FROM Probes WHERE account_id = :account_id;"));
q.bindValue(QSL(":account_id"), account_id);
if (q.exec()) {
while (q.next()) {
Search* prob = new Search(q.value(QSL("name")).toString(),
q.value(QSL("fltr")).toString(),
QColor(q.value(QSL("color")).toString()));
prob->setId(q.value(QSL("id")).toInt());
prob->setCustomId(QString::number(prob->id()));
probes << prob;
}
}
else {
throw ApplicationException(q.lastError().text());
}
return probes;
}
bool DatabaseQueries::markLabelledMessagesReadUnread(const QSqlDatabase& db, Label* label, RootItem::ReadStatus read) {
QSqlQuery q(db);

View File

@ -12,6 +12,7 @@
#include "miscellaneous/textfactory.h"
#include "services/abstract/category.h"
#include "services/abstract/label.h"
#include "services/abstract/search.h"
#include "services/abstract/serviceroot.h"
#include <QJsonDocument>
@ -46,6 +47,10 @@ class DatabaseQueries {
static bool deleteLabel(const QSqlDatabase& db, Label* label);
static bool createLabel(const QSqlDatabase& db, Label* label, int account_id);
// Probe operators.
static void createProbe(const QSqlDatabase& db, Search* probe, int account_id);
static QList<Search*> getProbesForAccount(const QSqlDatabase& db, int account_id);
// Message operators.
static bool markLabelledMessagesReadUnread(const QSqlDatabase& db, Label* label, RootItem::ReadStatus read);
static bool markImportantMessagesReadUnread(const QSqlDatabase& db, int account_id, RootItem::ReadStatus read);
@ -143,7 +148,8 @@ class DatabaseQueries {
template <typename T>
static QList<ServiceRoot*> getAccounts(const QSqlDatabase& db, const QString& code, bool* ok = nullptr);
template <typename Categ, typename Fee> static void loadRootFromDatabase(ServiceRoot* root);
template <typename Categ, typename Fee>
static void loadRootFromDatabase(ServiceRoot* root);
static bool storeNewOauthTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id);
static void createOverwriteAccount(const QSqlDatabase& db, ServiceRoot* account);
@ -169,7 +175,8 @@ class DatabaseQueries {
static bool deleteFeed(const QSqlDatabase& db, Feed* feed, int account_id);
static bool deleteCategory(const QSqlDatabase& db, Category* category);
template <typename T> static Assignment getCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
template <typename T>
static Assignment getCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
template <typename T>
static Assignment getFeeds(const QSqlDatabase& db,
@ -249,7 +256,8 @@ QList<ServiceRoot*> DatabaseQueries::getAccounts(const QSqlDatabase& db, const Q
return roots;
}
template <typename T> Assignment DatabaseQueries::getCategories(const QSqlDatabase& db, int account_id, bool* ok) {
template <typename T>
Assignment DatabaseQueries::getCategories(const QSqlDatabase& db, int account_id, bool* ok) {
Assignment categories;
// Obtain data for categories from the database.
@ -381,13 +389,15 @@ Assignment DatabaseQueries::getFeeds(const QSqlDatabase& db,
return feeds;
}
template <typename Categ, typename Fee> void DatabaseQueries::loadRootFromDatabase(ServiceRoot* root) {
template <typename Categ, typename Fee>
void DatabaseQueries::loadRootFromDatabase(ServiceRoot* root) {
QSqlDatabase database = qApp->database()->driver()->connection(root->metaObject()->className());
Assignment categories = DatabaseQueries::getCategories<Categ>(database, root->accountId());
Assignment feeds = DatabaseQueries::getFeeds<Fee>(database, qApp->feedReader()->messageFilters(), root->accountId());
auto labels = DatabaseQueries::getLabelsForAccount(database, root->accountId());
auto probes = DatabaseQueries::getProbesForAccount(database, root->accountId());
root->performInitialAssembly(categories, feeds, labels);
root->performInitialAssembly(categories, feeds, labels, probes);
}
#endif // DATABASEQUERIES_H

View File

@ -601,8 +601,8 @@ void FeedsView::onIndexExpanded(const QModelIndex& idx) {
const RootItem* it = m_sourceModel->itemForIndex(m_proxyModel->mapToSource(idx));
if (it != nullptr &&
(int(it->kind()) & int(RootItem::Kind::Category | RootItem::Kind::ServiceRoot | RootItem::Kind::Labels)) > 0) {
if (it != nullptr && (int(it->kind()) & int(RootItem::Kind::Category | RootItem::Kind::ServiceRoot |
RootItem::Kind::Labels | RootItem::Kind::Probes)) > 0) {
const QString setting_name = it->hashCode();
qApp->settings()->setValue(GROUP(CategoriesExpandStates), setting_name, true);
@ -619,8 +619,8 @@ void FeedsView::onIndexCollapsed(const QModelIndex& idx) {
RootItem* it = m_sourceModel->itemForIndex(m_proxyModel->mapToSource(idx));
if (it != nullptr &&
(int(it->kind()) & int(RootItem::Kind::Category | RootItem::Kind::ServiceRoot | RootItem::Kind::Labels)) > 0) {
if (it != nullptr && (int(it->kind()) & int(RootItem::Kind::Category | RootItem::Kind::ServiceRoot |
RootItem::Kind::Labels | RootItem::Kind::Probes)) > 0) {
const QString setting_name = it->hashCode();
qApp->settings()->setValue(GROUP(CategoriesExpandStates), setting_name, false);
@ -637,8 +637,8 @@ void FeedsView::saveAllExpandStates() {
void FeedsView::saveExpandStates(RootItem* item) {
Settings* settings = qApp->settings();
QList<RootItem*> items =
item->getSubTree(RootItem::Kind::Category | RootItem::Kind::ServiceRoot | RootItem::Kind::Labels);
QList<RootItem*> items = item->getSubTree(RootItem::Kind::Category | RootItem::Kind::ServiceRoot |
RootItem::Kind::Labels | RootItem::Kind::Probes);
// Iterate all categories and save their expand statuses.
for (const RootItem* it : items) {
@ -655,7 +655,7 @@ void FeedsView::loadAllExpandStates() {
QList<RootItem*> expandable_items;
expandable_items.append(sourceModel()->rootItem()->getSubTree(RootItem::Kind::Category | RootItem::Kind::ServiceRoot |
RootItem::Kind::Labels));
RootItem::Kind::Labels | RootItem::Kind::Probes));
// Iterate all categories and save their expand statuses.
for (const RootItem* item : expandable_items) {

View File

@ -14,15 +14,16 @@ FormAddEditProbe::FormAddEditProbe(QWidget* parent) : QDialog(parent), m_editabl
connect(m_ui.m_txtName->lineEdit(), &QLineEdit::textChanged, this, [this](const QString& text) {
if (text.isEmpty()) {
m_ui.m_txtName->setStatus(LineEditWithStatus::StatusType::Error, tr("Label's name cannot be empty."));
m_ui.m_txtName->setStatus(LineEditWithStatus::StatusType::Error, tr("Probe name cannot be empty."));
}
else {
m_ui.m_txtName->setStatus(LineEditWithStatus::StatusType::Ok, tr("Perfect!"));
}
});
connect(m_ui.m_txtFilter->lineEdit(), &QLineEdit::textChanged, this, [this](const QString& text) {
if (text.isEmpty()) {
m_ui.m_txtFilter->setStatus(LineEditWithStatus::StatusType::Error, tr("Probe name cannot be empty."));
m_ui.m_txtFilter->setStatus(LineEditWithStatus::StatusType::Error, tr("Probe filter cannot be empty."));
}
else if (!QRegularExpression(text).isValid()) {
m_ui.m_txtFilter->setStatus(LineEditWithStatus::StatusType::Error, tr("Regular expression is not well-formed."));

View File

@ -132,6 +132,7 @@ void LabelsNode::createLabel() {
DatabaseQueries::createLabel(db, new_lbl, getParentServiceRoot()->accountId());
getParentServiceRoot()->requestItemReassignment(new_lbl, this);
getParentServiceRoot()->requestItemExpand({this}, true);
}
catch (const ApplicationException&) {
new_lbl->deleteLater();

View File

@ -128,9 +128,10 @@ void SearchsNode::createProbe() {
QSqlDatabase db = qApp->database()->driver()->connection(metaObject()->className());
try {
// DatabaseQueries::createLabel(db, new_prb, getParentServiceRoot()->accountId());
DatabaseQueries::createProbe(db, new_prb, getParentServiceRoot()->accountId());
getParentServiceRoot()->requestItemReassignment(new_prb, this);
getParentServiceRoot()->requestItemExpand({this}, true);
}
catch (const ApplicationException&) {
new_prb->deleteLater();

View File

@ -565,10 +565,13 @@ void ServiceRoot::syncIn() {
void ServiceRoot::performInitialAssembly(const Assignment& categories,
const Assignment& feeds,
const QList<Label*>& labels) {
const QList<Label*>& labels,
const QList<Search*>& probes) {
assembleCategories(categories);
assembleFeeds(feeds);
labelsNode()->loadLabels(labels);
probesNode()->loadProbes(probes);
updateCounts(true);
}

View File

@ -43,7 +43,11 @@ class ServiceRoot : public RootItem {
Synchronised = 8
};
enum class BagOfMessages { Read, Unread, Starred };
enum class BagOfMessages {
Read,
Unread,
Starred
};
public:
explicit ServiceRoot(RootItem* parent = nullptr);
@ -235,7 +239,10 @@ class ServiceRoot : public RootItem {
QStringList customIDSOfMessagesForItem(RootItem* item,
RootItem::ReadStatus target_read = RootItem::ReadStatus::Unknown);
void performInitialAssembly(const Assignment& categories, const Assignment& feeds, const QList<Label*>& labels);
void performInitialAssembly(const Assignment& categories,
const Assignment& feeds,
const QList<Label*>& labels,
const QList<Search*>& probes);
public slots:
virtual void addNewFeed(RootItem* selected_item, const QString& url = QString());