Some methods moved from model to feed/category classes, some leaks fixed.
This commit is contained in:
parent
c05ae65e66
commit
931bc1d905
8 changed files with 216 additions and 166 deletions
|
|
@ -20,6 +20,10 @@
|
||||||
<ul>
|
<ul>
|
||||||
<li><b>Key used for proxy/feed password coding is now stored in separate file. This file lies in the same directory as configuration file. If your use password-protected proxy/feeds, then make sure that NOBODY gets access to that file and your DB file in the same time.</b></li>
|
<li><b>Key used for proxy/feed password coding is now stored in separate file. This file lies in the same directory as configuration file. If your use password-protected proxy/feeds, then make sure that NOBODY gets access to that file and your DB file in the same time.</b></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
Fixed:
|
||||||
|
<ul>
|
||||||
|
<li>Fixed some memory leaks which might appear when adding/editing categories/feeds.</li>
|
||||||
|
<ul>
|
||||||
<hr/>
|
<hr/>
|
||||||
<center><h2>2.5.0</h2></center>
|
<center><h2>2.5.0</h2></center>
|
||||||
Added:
|
Added:
|
||||||
|
|
|
||||||
|
|
@ -275,78 +275,29 @@ bool FeedsModel::removeItem(const QModelIndex &index) {
|
||||||
bool FeedsModel::addCategory(FeedsModelCategory *category, FeedsModelRootItem *parent) {
|
bool FeedsModel::addCategory(FeedsModelCategory *category, FeedsModelRootItem *parent) {
|
||||||
// Get index of parent item (parent standard category).
|
// Get index of parent item (parent standard category).
|
||||||
QModelIndex parent_index = indexForItem(parent);
|
QModelIndex parent_index = indexForItem(parent);
|
||||||
|
bool result = category->addItself(parent);
|
||||||
|
|
||||||
// Now, add category to persistent storage.
|
if (result) {
|
||||||
// Children are removed, remove this standard category too.
|
// Category was added to the persistent storage,
|
||||||
QSqlDatabase database = qApp->database()->connection(objectName(),
|
// so add it to the model.
|
||||||
DatabaseFactory::FromSettings);
|
beginInsertRows(parent_index, parent->childCount(), parent->childCount());
|
||||||
QSqlQuery query_add(database);
|
parent->appendChild(category);
|
||||||
|
endInsertRows();
|
||||||
query_add.setForwardOnly(true);
|
|
||||||
query_add.prepare("INSERT INTO Categories "
|
|
||||||
"(parent_id, title, description, date_created, icon) "
|
|
||||||
"VALUES (:parent_id, :title, :description, :date_created, :icon);");
|
|
||||||
query_add.bindValue(QSL(":parent_id"), parent->id());
|
|
||||||
query_add.bindValue(QSL(":title"), category->title());
|
|
||||||
query_add.bindValue(QSL(":description"), category->description());
|
|
||||||
query_add.bindValue(QSL(":date_created"), category->creationDate().toMSecsSinceEpoch());
|
|
||||||
query_add.bindValue(QSL(":icon"), qApp->icons()->toByteArray(category->icon()));
|
|
||||||
|
|
||||||
if (!query_add.exec()) {
|
|
||||||
qDebug("Failed to add category to database: %s.", qPrintable(query_add.lastError().text()));
|
|
||||||
|
|
||||||
// Query failed.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
query_add.prepare(QSL("SELECT id FROM Categories WHERE title = :title;"));
|
|
||||||
query_add.bindValue(QSL(":title"), category->title());
|
|
||||||
if (query_add.exec() && query_add.next()) {
|
|
||||||
// New category was added, fetch is primary id
|
|
||||||
// from the database.
|
|
||||||
category->setId(query_add.value(0).toInt());
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Something failed.
|
// We cannot delete (*this) in its method, thus delete it here.
|
||||||
return false;
|
delete category;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Category was added to the persistent storage,
|
return result;
|
||||||
// so add it to the model.
|
|
||||||
beginInsertRows(parent_index, parent->childCount(), parent->childCount());
|
|
||||||
parent->appendChild(category);
|
|
||||||
endInsertRows();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FeedsModel::editCategory(FeedsModelCategory *original_category, FeedsModelCategory *new_category) {
|
bool FeedsModel::editCategory(FeedsModelCategory *original_category, FeedsModelCategory *new_category_data) {
|
||||||
QSqlDatabase database = qApp->database()->connection(objectName(), DatabaseFactory::FromSettings);
|
|
||||||
QSqlQuery query_update_category(database);
|
|
||||||
FeedsModelRootItem *original_parent = original_category->parent();
|
FeedsModelRootItem *original_parent = original_category->parent();
|
||||||
FeedsModelRootItem *new_parent = new_category->parent();
|
FeedsModelRootItem *new_parent = new_category_data->parent();
|
||||||
|
bool result = original_category->editItself(new_category_data);
|
||||||
|
|
||||||
query_update_category.setForwardOnly(true);
|
if (result && original_parent != new_parent) {
|
||||||
query_update_category.prepare("UPDATE Categories "
|
|
||||||
"SET title = :title, description = :description, icon = :icon, parent_id = :parent_id "
|
|
||||||
"WHERE id = :id;");
|
|
||||||
query_update_category.bindValue(QSL(":title"), new_category->title());
|
|
||||||
query_update_category.bindValue(QSL(":description"), new_category->description());
|
|
||||||
query_update_category.bindValue(QSL(":icon"), qApp->icons()->toByteArray(new_category->icon()));
|
|
||||||
query_update_category.bindValue(QSL(":parent_id"), new_parent->id());
|
|
||||||
query_update_category.bindValue(QSL(":id"), original_category->id());
|
|
||||||
|
|
||||||
if (!query_update_category.exec()) {
|
|
||||||
// Persistent storage update failed, no way to continue now.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup new model data for the original item.
|
|
||||||
original_category->setDescription(new_category->description());
|
|
||||||
original_category->setIcon(new_category->icon());
|
|
||||||
original_category->setTitle(new_category->title());
|
|
||||||
|
|
||||||
if (original_parent != new_parent) {
|
|
||||||
// User edited category and set it new parent item,
|
// User edited category and set it new parent item,
|
||||||
// se we need to move the item in the model too.
|
// se we need to move the item in the model too.
|
||||||
int original_index_of_category = original_parent->childItems().indexOf(original_category);
|
int original_index_of_category = original_parent->childItems().indexOf(original_category);
|
||||||
|
|
@ -357,115 +308,41 @@ bool FeedsModel::editCategory(FeedsModelCategory *original_category, FeedsModelC
|
||||||
original_parent->removeChild(original_category);
|
original_parent->removeChild(original_category);
|
||||||
endRemoveRows();
|
endRemoveRows();
|
||||||
|
|
||||||
// ... and insert it under the new parent.
|
// ...and insert it under the new parent.
|
||||||
beginInsertRows(indexForItem(new_parent), new_index_of_category, new_index_of_category);
|
beginInsertRows(indexForItem(new_parent), new_index_of_category, new_index_of_category);
|
||||||
new_parent->appendChild(original_category);
|
new_parent->appendChild(original_category);
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free temporary category from memory.
|
// Cleanup temporary new category data.
|
||||||
delete new_category;
|
delete new_category_data;
|
||||||
|
return result;
|
||||||
// Editing is done.
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FeedsModel::addFeed(FeedsModelFeed *feed, FeedsModelRootItem *parent) {
|
bool FeedsModel::addFeed(FeedsModelFeed *feed, FeedsModelRootItem *parent) {
|
||||||
// Get index of parent item (parent standard category or root item).
|
// Get index of parent item (parent standard category or root item).
|
||||||
QModelIndex parent_index = indexForItem(parent);
|
QModelIndex parent_index = indexForItem(parent);
|
||||||
|
bool result = feed->addItself(parent);
|
||||||
|
|
||||||
// Now, add feed to persistent storage.
|
if (result) {
|
||||||
QSqlDatabase database = qApp->database()->connection(objectName(), DatabaseFactory::FromSettings);
|
// Feed was added to the persistent storage so add it to the model.
|
||||||
QSqlQuery query_add_feed(database);
|
beginInsertRows(parent_index, parent->childCount(), parent->childCount());
|
||||||
|
parent->appendChild(feed);
|
||||||
query_add_feed.setForwardOnly(true);
|
endInsertRows();
|
||||||
query_add_feed.prepare("INSERT INTO Feeds "
|
|
||||||
"(title, description, date_created, icon, category, encoding, url, protected, username, password, update_type, update_interval, type) "
|
|
||||||
"VALUES (:title, :description, :date_created, :icon, :category, :encoding, :url, :protected, :username, :password, :update_type, :update_interval, :type);");
|
|
||||||
query_add_feed.bindValue(QSL(":title"), feed->title());
|
|
||||||
query_add_feed.bindValue(QSL(":description"), feed->description());
|
|
||||||
query_add_feed.bindValue(QSL(":date_created"), feed->creationDate().toMSecsSinceEpoch());
|
|
||||||
query_add_feed.bindValue(QSL(":icon"), qApp->icons()->toByteArray(feed->icon()));
|
|
||||||
query_add_feed.bindValue(QSL(":category"), parent->id());
|
|
||||||
query_add_feed.bindValue(QSL(":encoding"), feed->encoding());
|
|
||||||
query_add_feed.bindValue(QSL(":url"), feed->url());
|
|
||||||
query_add_feed.bindValue(QSL(":protected"), (int) feed->passwordProtected());
|
|
||||||
query_add_feed.bindValue(QSL(":username"), feed->username());
|
|
||||||
query_add_feed.bindValue(QSL(":password"), TextFactory::encrypt(feed->password()));
|
|
||||||
query_add_feed.bindValue(QSL(":update_type"), (int) feed->autoUpdateType());
|
|
||||||
query_add_feed.bindValue(QSL(":update_interval"), feed->autoUpdateInitialInterval());
|
|
||||||
query_add_feed.bindValue(QSL(":type"), (int) feed->type());
|
|
||||||
|
|
||||||
if (!query_add_feed.exec()) {
|
|
||||||
qDebug("Failed to add feed to database: %s.", qPrintable(query_add_feed.lastError().text()));
|
|
||||||
|
|
||||||
// Query failed.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
query_add_feed.prepare(QSL("SELECT id FROM Feeds WHERE url = :url;"));
|
|
||||||
query_add_feed.bindValue(QSL(":url"), feed->url());
|
|
||||||
if (query_add_feed.exec() && query_add_feed.next()) {
|
|
||||||
// New feed was added, fetch is primary id from the database.
|
|
||||||
feed->setId(query_add_feed.value(0).toInt());
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Something failed.
|
delete feed;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Feed was added to the persistent storage so add it to the model.
|
return result;
|
||||||
beginInsertRows(parent_index, parent->childCount(), parent->childCount());
|
|
||||||
parent->appendChild(feed);
|
|
||||||
endInsertRows();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FeedsModel::editFeed(FeedsModelFeed *original_feed, FeedsModelFeed *new_feed) {
|
bool FeedsModel::editFeed(FeedsModelFeed *original_feed, FeedsModelFeed *new_feed_data) {
|
||||||
QSqlDatabase database = qApp->database()->connection(objectName(), DatabaseFactory::FromSettings);
|
|
||||||
QSqlQuery query_update_feed(database);
|
|
||||||
FeedsModelRootItem *original_parent = original_feed->parent();
|
FeedsModelRootItem *original_parent = original_feed->parent();
|
||||||
FeedsModelRootItem *new_parent = new_feed->parent();
|
FeedsModelRootItem *new_parent = new_feed_data->parent();
|
||||||
|
bool result = original_feed->editItself(new_feed_data);
|
||||||
|
|
||||||
query_update_feed.setForwardOnly(true);
|
if (result && original_parent != new_parent) {
|
||||||
query_update_feed.prepare("UPDATE Feeds "
|
|
||||||
"SET title = :title, description = :description, icon = :icon, category = :category, encoding = :encoding, url = :url, protected = :protected, username = :username, password = :password, update_type = :update_type, update_interval = :update_interval, type = :type "
|
|
||||||
"WHERE id = :id;");
|
|
||||||
query_update_feed.bindValue(QSL(":title"), new_feed->title());
|
|
||||||
query_update_feed.bindValue(QSL(":description"), new_feed->description());
|
|
||||||
query_update_feed.bindValue(QSL(":icon"), qApp->icons()->toByteArray(new_feed->icon()));
|
|
||||||
query_update_feed.bindValue(QSL(":category"), new_parent->id());
|
|
||||||
query_update_feed.bindValue(QSL(":encoding"), new_feed->encoding());
|
|
||||||
query_update_feed.bindValue(QSL(":url"), new_feed->url());
|
|
||||||
query_update_feed.bindValue(QSL(":protected"), (int) new_feed->passwordProtected());
|
|
||||||
query_update_feed.bindValue(QSL(":username"), new_feed->username());
|
|
||||||
query_update_feed.bindValue(QSL(":password"), TextFactory::encrypt(new_feed->password()));
|
|
||||||
query_update_feed.bindValue(QSL(":update_type"), (int) new_feed->autoUpdateType());
|
|
||||||
query_update_feed.bindValue(QSL(":update_interval"), new_feed->autoUpdateInitialInterval());
|
|
||||||
query_update_feed.bindValue(QSL(":type"), new_feed->type());
|
|
||||||
query_update_feed.bindValue(QSL(":id"), original_feed->id());
|
|
||||||
|
|
||||||
if (!query_update_feed.exec()) {
|
|
||||||
// Persistent storage update failed, no way to continue now.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup new model data for the original item.
|
|
||||||
original_feed->setTitle(new_feed->title());
|
|
||||||
original_feed->setDescription(new_feed->description());
|
|
||||||
original_feed->setIcon(new_feed->icon());
|
|
||||||
original_feed->setEncoding(new_feed->encoding());
|
|
||||||
original_feed->setDescription(new_feed->description());
|
|
||||||
original_feed->setUrl(new_feed->url());
|
|
||||||
original_feed->setPasswordProtected(new_feed->passwordProtected());
|
|
||||||
original_feed->setUsername(new_feed->username());
|
|
||||||
original_feed->setPassword(new_feed->password());
|
|
||||||
original_feed->setAutoUpdateType(new_feed->autoUpdateType());
|
|
||||||
original_feed->setAutoUpdateInitialInterval(new_feed->autoUpdateInitialInterval());
|
|
||||||
original_feed->setType(new_feed->type());
|
|
||||||
|
|
||||||
if (original_parent != new_parent) {
|
|
||||||
// User edited category and set it new parent item,
|
// User edited category and set it new parent item,
|
||||||
// se we need to move the item in the model too.
|
// se we need to move the item in the model too.
|
||||||
int original_index_of_feed = original_parent->childItems().indexOf(original_feed);
|
int original_index_of_feed = original_parent->childItems().indexOf(original_feed);
|
||||||
|
|
@ -482,11 +359,8 @@ bool FeedsModel::editFeed(FeedsModelFeed *original_feed, FeedsModelFeed *new_fee
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free temporary category from memory.
|
delete new_feed_data;
|
||||||
delete new_feed;
|
return result;
|
||||||
|
|
||||||
// Editing is done.
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<FeedsModelFeed*> FeedsModel::feedsForScheduledUpdate(bool auto_update_now) {
|
QList<FeedsModelFeed*> FeedsModel::feedsForScheduledUpdate(bool auto_update_now) {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,9 @@ typedef QPair<int, FeedsModelFeed*> FeedAssignmentItem;
|
||||||
class FeedsModel : public QAbstractItemModel {
|
class FeedsModel : public QAbstractItemModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
friend class FeedsModelFeed;
|
||||||
|
friend class FeedsModelCategory;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructors and destructors.
|
// Constructors and destructors.
|
||||||
explicit FeedsModel(QObject *parent = 0);
|
explicit FeedsModel(QObject *parent = 0);
|
||||||
|
|
@ -76,7 +79,7 @@ class FeedsModel : public QAbstractItemModel {
|
||||||
|
|
||||||
// Standard category manipulators.
|
// Standard category manipulators.
|
||||||
bool addCategory(FeedsModelCategory *category, FeedsModelRootItem *parent);
|
bool addCategory(FeedsModelCategory *category, FeedsModelRootItem *parent);
|
||||||
bool editCategory(FeedsModelCategory *original_category, FeedsModelCategory *new_category);
|
bool editCategory(FeedsModelCategory *original_category, FeedsModelCategory *new_category_data);
|
||||||
|
|
||||||
// Standard feed manipulators.
|
// Standard feed manipulators.
|
||||||
bool addFeed(FeedsModelFeed *feed, FeedsModelRootItem *parent);
|
bool addFeed(FeedsModelFeed *feed, FeedsModelRootItem *parent);
|
||||||
|
|
@ -84,7 +87,7 @@ class FeedsModel : public QAbstractItemModel {
|
||||||
// New feed is just temporary feed, it is not added to the model.
|
// New feed is just temporary feed, it is not added to the model.
|
||||||
// It is used to fetch its data to the original feed
|
// It is used to fetch its data to the original feed
|
||||||
// and the original feed is moved if needed.
|
// and the original feed is moved if needed.
|
||||||
bool editFeed(FeedsModelFeed *original_feed, FeedsModelFeed *new_feed);
|
bool editFeed(FeedsModelFeed *original_feed, FeedsModelFeed *new_feed_data);
|
||||||
|
|
||||||
// Returns the list of updates which should be updated
|
// Returns the list of updates which should be updated
|
||||||
// according to auto-update schedule.
|
// according to auto-update schedule.
|
||||||
|
|
|
||||||
|
|
@ -23,15 +23,13 @@
|
||||||
#include "miscellaneous/settings.h"
|
#include "miscellaneous/settings.h"
|
||||||
#include "miscellaneous/iconfactory.h"
|
#include "miscellaneous/iconfactory.h"
|
||||||
#include "gui/dialogs/formcategorydetails.h"
|
#include "gui/dialogs/formcategorydetails.h"
|
||||||
|
#include "core/feedsmodel.h"
|
||||||
|
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
|
#include <QSqlError>
|
||||||
|
|
||||||
|
|
||||||
void FeedsModelCategory::init() {
|
|
||||||
m_kind = FeedsModelRootItem::Category;
|
|
||||||
}
|
|
||||||
|
|
||||||
FeedsModelCategory::FeedsModelCategory(FeedsModelRootItem *parent_item) : FeedsModelRootItem(parent_item) {
|
FeedsModelCategory::FeedsModelCategory(FeedsModelRootItem *parent_item) : FeedsModelRootItem(parent_item) {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
@ -52,6 +50,10 @@ FeedsModelCategory::~FeedsModelCategory() {
|
||||||
qDebug("Destroying FeedsModelCategory instance.");
|
qDebug("Destroying FeedsModelCategory instance.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FeedsModelCategory::init() {
|
||||||
|
m_kind = FeedsModelRootItem::Category;
|
||||||
|
}
|
||||||
|
|
||||||
QVariant FeedsModelCategory::data(int column, int role) const {
|
QVariant FeedsModelCategory::data(int column, int role) const {
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::ToolTipRole:
|
case Qt::ToolTipRole:
|
||||||
|
|
@ -145,6 +147,75 @@ bool FeedsModelCategory::removeItself() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FeedsModelCategory::addItself(FeedsModelRootItem *parent) {
|
||||||
|
// Now, add category to persistent storage.
|
||||||
|
// Children are removed, remove this standard category too.
|
||||||
|
QSqlDatabase database = qApp->database()->connection(QSL("FeedsModelCategory"), DatabaseFactory::FromSettings);
|
||||||
|
QSqlQuery query_add(database);
|
||||||
|
|
||||||
|
query_add.setForwardOnly(true);
|
||||||
|
query_add.prepare("INSERT INTO Categories "
|
||||||
|
"(parent_id, title, description, date_created, icon) "
|
||||||
|
"VALUES (:parent_id, :title, :description, :date_created, :icon);");
|
||||||
|
query_add.bindValue(QSL(":parent_id"), parent->id());
|
||||||
|
query_add.bindValue(QSL(":title"), title());
|
||||||
|
query_add.bindValue(QSL(":description"), description());
|
||||||
|
query_add.bindValue(QSL(":date_created"), creationDate().toMSecsSinceEpoch());
|
||||||
|
query_add.bindValue(QSL(":icon"), qApp->icons()->toByteArray(icon()));
|
||||||
|
|
||||||
|
if (!query_add.exec()) {
|
||||||
|
qDebug("Failed to add category to database: %s.", qPrintable(query_add.lastError().text()));
|
||||||
|
|
||||||
|
// Query failed.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
query_add.prepare(QSL("SELECT id FROM Categories WHERE title = :title;"));
|
||||||
|
query_add.bindValue(QSL(":title"), title());
|
||||||
|
|
||||||
|
if (query_add.exec() && query_add.next()) {
|
||||||
|
// New category was added, fetch is primary id
|
||||||
|
// from the database.
|
||||||
|
setId(query_add.value(0).toInt());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Something failed.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FeedsModelCategory::editItself(FeedsModelCategory *new_category_data) {
|
||||||
|
QSqlDatabase database = qApp->database()->connection(QSL("FeedsModelCategory"), DatabaseFactory::FromSettings);
|
||||||
|
QSqlQuery query_update_category(database);
|
||||||
|
FeedsModelCategory *original_category = this;
|
||||||
|
FeedsModelRootItem *new_parent = new_category_data->parent();
|
||||||
|
|
||||||
|
query_update_category.setForwardOnly(true);
|
||||||
|
query_update_category.prepare("UPDATE Categories "
|
||||||
|
"SET title = :title, description = :description, icon = :icon, parent_id = :parent_id "
|
||||||
|
"WHERE id = :id;");
|
||||||
|
query_update_category.bindValue(QSL(":title"), new_category_data->title());
|
||||||
|
query_update_category.bindValue(QSL(":description"), new_category_data->description());
|
||||||
|
query_update_category.bindValue(QSL(":icon"), qApp->icons()->toByteArray(new_category_data->icon()));
|
||||||
|
query_update_category.bindValue(QSL(":parent_id"), new_parent->id());
|
||||||
|
query_update_category.bindValue(QSL(":id"), original_category->id());
|
||||||
|
|
||||||
|
if (!query_update_category.exec()) {
|
||||||
|
// Persistent storage update failed, no way to continue now.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup new model data for the original item.
|
||||||
|
original_category->setDescription(new_category_data->description());
|
||||||
|
original_category->setIcon(new_category_data->icon());
|
||||||
|
original_category->setTitle(new_category_data->title());
|
||||||
|
|
||||||
|
// Editing is done.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
FeedsModelCategory::FeedsModelCategory(const QSqlRecord &record) : FeedsModelRootItem(NULL) {
|
FeedsModelCategory::FeedsModelCategory(const QSqlRecord &record) : FeedsModelRootItem(NULL) {
|
||||||
init();
|
init();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
|
||||||
|
|
||||||
class FeedsModelFeed;
|
class FeedsModel;
|
||||||
|
|
||||||
// Base class for all categories contained in FeedsModel.
|
// Base class for all categories contained in FeedsModel.
|
||||||
// NOTE: This class should be derived to create PARTICULAR category types.
|
// NOTE: This class should be derived to create PARTICULAR category types.
|
||||||
|
|
@ -46,6 +46,9 @@ class FeedsModelCategory : public FeedsModelRootItem {
|
||||||
// database.
|
// database.
|
||||||
bool removeItself();
|
bool removeItself();
|
||||||
|
|
||||||
|
bool addItself(FeedsModelRootItem *parent);
|
||||||
|
bool editItself(FeedsModelCategory *new_category_data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init();
|
void init();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "definitions/definitions.h"
|
#include "definitions/definitions.h"
|
||||||
#include "core/parsingfactory.h"
|
#include "core/parsingfactory.h"
|
||||||
|
#include "core/feedsmodel.h"
|
||||||
#include "miscellaneous/databasefactory.h"
|
#include "miscellaneous/databasefactory.h"
|
||||||
#include "miscellaneous/textfactory.h"
|
#include "miscellaneous/textfactory.h"
|
||||||
#include "miscellaneous/settings.h"
|
#include "miscellaneous/settings.h"
|
||||||
|
|
@ -466,6 +467,97 @@ bool FeedsModelFeed::removeItself() {
|
||||||
return query_remove.exec();
|
return query_remove.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FeedsModelFeed::addItself(FeedsModelRootItem *parent) {
|
||||||
|
// Now, add feed to persistent storage.
|
||||||
|
QSqlDatabase database = qApp->database()->connection(QSL("FeedsModelFeed"), DatabaseFactory::FromSettings);
|
||||||
|
QSqlQuery query_add_feed(database);
|
||||||
|
|
||||||
|
query_add_feed.setForwardOnly(true);
|
||||||
|
query_add_feed.prepare("INSERT INTO Feeds "
|
||||||
|
"(title, description, date_created, icon, category, encoding, url, protected, username, password, update_type, update_interval, type) "
|
||||||
|
"VALUES (:title, :description, :date_created, :icon, :category, :encoding, :url, :protected, :username, :password, :update_type, :update_interval, :type);");
|
||||||
|
query_add_feed.bindValue(QSL(":title"), title());
|
||||||
|
query_add_feed.bindValue(QSL(":description"), description());
|
||||||
|
query_add_feed.bindValue(QSL(":date_created"), creationDate().toMSecsSinceEpoch());
|
||||||
|
query_add_feed.bindValue(QSL(":icon"), qApp->icons()->toByteArray(icon()));
|
||||||
|
query_add_feed.bindValue(QSL(":category"), parent->id());
|
||||||
|
query_add_feed.bindValue(QSL(":encoding"), encoding());
|
||||||
|
query_add_feed.bindValue(QSL(":url"), url());
|
||||||
|
query_add_feed.bindValue(QSL(":protected"), (int) passwordProtected());
|
||||||
|
query_add_feed.bindValue(QSL(":username"), username());
|
||||||
|
query_add_feed.bindValue(QSL(":password"), TextFactory::encrypt(password()));
|
||||||
|
query_add_feed.bindValue(QSL(":update_type"), (int) autoUpdateType());
|
||||||
|
query_add_feed.bindValue(QSL(":update_interval"), autoUpdateInitialInterval());
|
||||||
|
query_add_feed.bindValue(QSL(":type"), (int) type());
|
||||||
|
|
||||||
|
if (!query_add_feed.exec()) {
|
||||||
|
qDebug("Failed to add feed to database: %s.", qPrintable(query_add_feed.lastError().text()));
|
||||||
|
|
||||||
|
// Query failed.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
query_add_feed.prepare(QSL("SELECT id FROM Feeds WHERE url = :url;"));
|
||||||
|
query_add_feed.bindValue(QSL(":url"), url());
|
||||||
|
if (query_add_feed.exec() && query_add_feed.next()) {
|
||||||
|
// New feed was added, fetch is primary id from the database.
|
||||||
|
setId(query_add_feed.value(0).toInt());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Something failed.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FeedsModelFeed::editItself(FeedsModelFeed *new_feed_data) {
|
||||||
|
QSqlDatabase database = qApp->database()->connection(QSL("FeedsModelFeed"), DatabaseFactory::FromSettings);
|
||||||
|
QSqlQuery query_update_feed(database);
|
||||||
|
FeedsModelFeed *original_feed = this;
|
||||||
|
FeedsModelRootItem *new_parent = new_feed_data->parent();
|
||||||
|
|
||||||
|
query_update_feed.setForwardOnly(true);
|
||||||
|
query_update_feed.prepare("UPDATE Feeds "
|
||||||
|
"SET title = :title, description = :description, icon = :icon, category = :category, encoding = :encoding, url = :url, protected = :protected, username = :username, password = :password, update_type = :update_type, update_interval = :update_interval, type = :type "
|
||||||
|
"WHERE id = :id;");
|
||||||
|
query_update_feed.bindValue(QSL(":title"), new_feed_data->title());
|
||||||
|
query_update_feed.bindValue(QSL(":description"), new_feed_data->description());
|
||||||
|
query_update_feed.bindValue(QSL(":icon"), qApp->icons()->toByteArray(new_feed_data->icon()));
|
||||||
|
query_update_feed.bindValue(QSL(":category"), new_parent->id());
|
||||||
|
query_update_feed.bindValue(QSL(":encoding"), new_feed_data->encoding());
|
||||||
|
query_update_feed.bindValue(QSL(":url"), new_feed_data->url());
|
||||||
|
query_update_feed.bindValue(QSL(":protected"), (int) new_feed_data->passwordProtected());
|
||||||
|
query_update_feed.bindValue(QSL(":username"), new_feed_data->username());
|
||||||
|
query_update_feed.bindValue(QSL(":password"), TextFactory::encrypt(new_feed_data->password()));
|
||||||
|
query_update_feed.bindValue(QSL(":update_type"), (int) new_feed_data->autoUpdateType());
|
||||||
|
query_update_feed.bindValue(QSL(":update_interval"), new_feed_data->autoUpdateInitialInterval());
|
||||||
|
query_update_feed.bindValue(QSL(":type"), new_feed_data->type());
|
||||||
|
query_update_feed.bindValue(QSL(":id"), original_feed->id());
|
||||||
|
|
||||||
|
if (!query_update_feed.exec()) {
|
||||||
|
// Persistent storage update failed, no way to continue now.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup new model data for the original item.
|
||||||
|
original_feed->setTitle(new_feed_data->title());
|
||||||
|
original_feed->setDescription(new_feed_data->description());
|
||||||
|
original_feed->setIcon(new_feed_data->icon());
|
||||||
|
original_feed->setEncoding(new_feed_data->encoding());
|
||||||
|
original_feed->setDescription(new_feed_data->description());
|
||||||
|
original_feed->setUrl(new_feed_data->url());
|
||||||
|
original_feed->setPasswordProtected(new_feed_data->passwordProtected());
|
||||||
|
original_feed->setUsername(new_feed_data->username());
|
||||||
|
original_feed->setPassword(new_feed_data->password());
|
||||||
|
original_feed->setAutoUpdateType(new_feed_data->autoUpdateType());
|
||||||
|
original_feed->setAutoUpdateInitialInterval(new_feed_data->autoUpdateInitialInterval());
|
||||||
|
original_feed->setType(new_feed_data->type());
|
||||||
|
|
||||||
|
// Editing is done.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int FeedsModelFeed::updateMessages(const QList<Message> &messages) {
|
int FeedsModelFeed::updateMessages(const QList<Message> &messages) {
|
||||||
int feed_id = id();
|
int feed_id = id();
|
||||||
int updated_messages = 0;
|
int updated_messages = 0;
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
|
|
||||||
class Message;
|
class Message;
|
||||||
|
class FeedsModel;
|
||||||
|
|
||||||
// Represents BASE class for feeds contained in FeedsModel.
|
// Represents BASE class for feeds contained in FeedsModel.
|
||||||
// NOTE: This class should be derived to create PARTICULAR feed types.
|
// NOTE: This class should be derived to create PARTICULAR feed types.
|
||||||
|
|
@ -85,6 +86,8 @@ class FeedsModelFeed : public FeedsModelRootItem {
|
||||||
// Removes this standard feed from persistent
|
// Removes this standard feed from persistent
|
||||||
// storage.
|
// storage.
|
||||||
bool removeItself();
|
bool removeItself();
|
||||||
|
bool addItself(FeedsModelRootItem *parent);
|
||||||
|
bool editItself(FeedsModelFeed *new_feed_data);
|
||||||
|
|
||||||
// Other getters/setters.
|
// Other getters/setters.
|
||||||
inline Type type() const {
|
inline Type type() const {
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ class DatabaseFactory : public QObject {
|
||||||
// If in-memory is true, then :memory: database is returned
|
// If in-memory is true, then :memory: database is returned
|
||||||
// In-memory database is DEFAULT database.
|
// In-memory database is DEFAULT database.
|
||||||
// NOTE: This always returns OPENED database.
|
// NOTE: This always returns OPENED database.
|
||||||
QSqlDatabase connection(const QString &connection_name, DesiredType desired_type);
|
QSqlDatabase connection(const QString &connection_name, DesiredType desired_type = FromSettings);
|
||||||
|
|
||||||
QString humanDriverName(UsedDriver driver);
|
QString humanDriverName(UsedDriver driver);
|
||||||
QString humanDriverName(const QString &driver_code);
|
QString humanDriverName(const QString &driver_code);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue