working feed operations regarding to sort order, categories and accounts to go

This commit is contained in:
Martin Rotter 2022-03-15 09:34:40 +01:00
parent 2ef9a373b3
commit 3f59abae2a
4 changed files with 39 additions and 26 deletions

View File

@ -1987,28 +1987,31 @@ void DatabaseQueries::createOverwriteCategory(const QSqlDatabase& db, Category*
} }
} }
void DatabaseQueries::createOverwriteFeed(const QSqlDatabase& db, Feed* feed, int account_id, int parent_id) { void DatabaseQueries::createOverwriteFeed(const QSqlDatabase& db, Feed* feed, int account_id, int new_parent_id) {
QSqlQuery q(db); QSqlQuery q(db);
int next_sort_order;
if (feed->id() <= 0) { if ((feed->id() <= 0 && feed->sortOrder() < 0) ||
// We need to insert feed first. (feed->parent() != nullptr && feed->parent()->id() != new_parent_id)) {
if (feed->sortOrder() < 0) { // We either insert completely new feed or we move feed
// to new parent. Get new viable sort order.
q.prepare(QSL("SELECT MAX(ordr) FROM Feeds WHERE account_id = :account_id AND category = :category;")); q.prepare(QSL("SELECT MAX(ordr) FROM Feeds WHERE account_id = :account_id AND category = :category;"));
q.bindValue(QSL(":account_id"), account_id); q.bindValue(QSL(":account_id"), account_id);
q.bindValue(QSL(":category"), parent_id); q.bindValue(QSL(":category"), new_parent_id);
if (!q.exec()) { if (!q.exec() || !q.next()) {
throw ApplicationException(q.lastError().text()); throw ApplicationException(q.lastError().text());
} }
q.next(); next_sort_order = (q.value(0).isNull() ? -1 : q.value(0).toInt()) + 1;
int next_order = (q.value(0).isNull() ? -1 : q.value(0).toInt()) + 1;
feed->setSortOrder(next_order);
q.finish(); q.finish();
} }
else {
next_sort_order = feed->sortOrder();
}
if (feed->id() <= 0) {
// We need to insert feed first.
q.prepare(QSL("INSERT INTO " q.prepare(QSL("INSERT INTO "
"Feeds (title, ordr, date_created, category, update_type, update_interval, account_id, custom_id) " "Feeds (title, ordr, date_created, category, update_type, update_interval, account_id, custom_id) "
"VALUES ('new', 0, 0, 0, 0, 1, %1, 'new');").arg(QString::number(account_id))); "VALUES ('new', 0, 0, 0, 0, 1, %1, 'new');").arg(QString::number(account_id)));
@ -2024,9 +2027,18 @@ void DatabaseQueries::createOverwriteFeed(const QSqlDatabase& db, Feed* feed, in
} }
} }
} }
else if (feed->parent() != nullptr && feed->parent()->id() != new_parent_id) {
// Feed is moving between categories.
// 1. Move feed to bottom of current category.
// 2. Assign proper new sort order.
//
// NOTE: The feed will get reassigned to new parent usually after this method
// completes by the caller.
moveItem(feed, false, true, {}, db);
}
// TODO: pokus se kanál přesouvá mezi kategoriemi či rootem // Restore to correct sort order.
// je třeba nejdříve kanál přesunout na dno a pak ho vložit do nové kategorie feed->setSortOrder(next_sort_order);
q.prepare("UPDATE Feeds " q.prepare("UPDATE Feeds "
"SET title = :title, ordr = :ordr, description = :description, date_created = :date_created, " "SET title = :title, ordr = :ordr, description = :description, date_created = :date_created, "
@ -2038,7 +2050,7 @@ void DatabaseQueries::createOverwriteFeed(const QSqlDatabase& db, Feed* feed, in
q.bindValue(QSL(":description"), feed->description()); q.bindValue(QSL(":description"), feed->description());
q.bindValue(QSL(":date_created"), feed->creationDate().toMSecsSinceEpoch()); q.bindValue(QSL(":date_created"), feed->creationDate().toMSecsSinceEpoch());
q.bindValue(QSL(":icon"), qApp->icons()->toByteArray(feed->icon())); q.bindValue(QSL(":icon"), qApp->icons()->toByteArray(feed->icon()));
q.bindValue(QSL(":category"), parent_id); q.bindValue(QSL(":category"), new_parent_id);
q.bindValue(QSL(":source"), feed->source()); q.bindValue(QSL(":source"), feed->source());
q.bindValue(QSL(":update_type"), int(feed->autoUpdateType())); q.bindValue(QSL(":update_type"), int(feed->autoUpdateType()));
q.bindValue(QSL(":update_interval"), feed->autoUpdateInitialInterval()); q.bindValue(QSL(":update_interval"), feed->autoUpdateInitialInterval());

View File

@ -122,7 +122,7 @@ class DatabaseQueries {
static bool cleanUnreadMessages(const QSqlDatabase& db, int account_id); static bool cleanUnreadMessages(const QSqlDatabase& db, int account_id);
static bool cleanFeeds(const QSqlDatabase& db, const QStringList& ids, bool clean_read_only, int account_id); static bool cleanFeeds(const QSqlDatabase& db, const QStringList& ids, bool clean_read_only, int account_id);
static void storeAccountTree(const QSqlDatabase& db, RootItem* tree_root, int account_id); static void storeAccountTree(const QSqlDatabase& db, RootItem* tree_root, int account_id);
static void createOverwriteFeed(const QSqlDatabase& db, Feed* feed, int account_id, int parent_id); static void createOverwriteFeed(const QSqlDatabase& db, Feed* feed, int account_id, int new_parent_id);
static void createOverwriteCategory(const QSqlDatabase& db, Category* category, int account_id, int parent_id); static void createOverwriteCategory(const QSqlDatabase& db, Category* category, int account_id, int parent_id);
static bool deleteFeed(const QSqlDatabase& db, Feed* feed, int account_id); static bool deleteFeed(const QSqlDatabase& db, Feed* feed, int account_id);
static bool deleteCategory(const QSqlDatabase& db, int id); static bool deleteCategory(const QSqlDatabase& db, int id);

View File

@ -196,6 +196,7 @@ void StandardFeed::fetchMetadataForItself() {
setType(metadata->type()); setType(metadata->type());
setEncoding(metadata->encoding()); setEncoding(metadata->encoding());
setIcon(metadata->icon()); setIcon(metadata->icon());
metadata->deleteLater(); metadata->deleteLater();
QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className());

View File

@ -270,7 +270,7 @@ QList<Message> StandardServiceRoot::obtainNewMessages(Feed* feed,
QList<QAction*> StandardServiceRoot::getContextMenuForFeed(StandardFeed* feed) { QList<QAction*> StandardServiceRoot::getContextMenuForFeed(StandardFeed* feed) {
if (m_feedContextMenu.isEmpty()) { if (m_feedContextMenu.isEmpty()) {
// Initialize. // Initialize.
auto* action_metadata = new QAction(qApp->icons()->fromTheme(QSL("emblem-downloads")), auto* action_metadata = new QAction(qApp->icons()->fromTheme(QSL("download"), QSL("emblem-downloads")),
tr("Fetch metadata"), tr("Fetch metadata"),
this); this);