Import fixing.

This commit is contained in:
Martin Rotter 2014-09-03 07:50:29 +02:00
parent dd3fcaa1f6
commit 4017420e4f
5 changed files with 48 additions and 17 deletions

View file

@ -20,6 +20,8 @@
#include "core/feedsmodelfeed.h"
#include "core/feedsmodelcategory.h"
#include "definitions/definitions.h"
#include "miscellaneous/application.h"
#include "miscellaneous/iconfactory.h"
#include <QDomDocument>
#include <QDomElement>
@ -192,6 +194,8 @@ bool FeedsImportExportModel::importAsOPML20(const QByteArray &data) {
new_feed->setDescription(feed_description);
new_feed->setEncoding(feed_encoding);
new_feed->setUrl(feed_url);
new_feed->setCreationDate(QDateTime::currentDateTime());
new_feed->setIcon(qApp->icons()->fromTheme("folder-feed"));
new_feed->setAutoUpdateType(FeedsModelFeed::DefaultAutoUpdate);
if (feed_type == "RSS1") {
@ -214,6 +218,8 @@ bool FeedsImportExportModel::importAsOPML20(const QByteArray &data) {
FeedsModelCategory *new_category = new FeedsModelCategory(active_model_item);
new_category->setTitle(category_title);
new_category->setIcon(qApp->icons()->fromTheme("folder-category"));
new_category->setCreationDate(QDateTime::currentDateTime());
new_category->setDescription(category_description);
active_model_item->appendChild(new_category);

View file

@ -185,12 +185,14 @@ bool FeedsModel::addCategory(FeedsModelCategory *category, FeedsModelRootItem *p
query_add.bindValue(":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("SELECT id FROM Categories WHERE date_created = :date_created;");
query_add.bindValue(":date_created", category->creationDate().toMSecsSinceEpoch());
query_add.prepare("SELECT id FROM Categories WHERE title = :title;");
query_add.bindValue(":title", category->title());
if (query_add.exec() && query_add.next()) {
// New category was added, fetch is primary id
// from the database.
@ -288,12 +290,14 @@ bool FeedsModel::addFeed(FeedsModelFeed *feed, FeedsModelRootItem *parent) {
query_add_feed.bindValue(":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("SELECT id FROM Feeds WHERE date_created = :date_created;");
query_add_feed.bindValue(":date_created", feed->creationDate().toMSecsSinceEpoch());
query_add_feed.prepare("SELECT id FROM Feeds WHERE url = :url;");
query_add_feed.bindValue(":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());
@ -543,15 +547,19 @@ bool FeedsModel::mergeRootItem(FeedsModelRootItem *root_item, QString &output_me
foreach (FeedsModelRootItem *source_item, source_parent->childItems()) {
if (source_item->kind() == FeedsModelRootItem::Category) {
FeedsModelCategory *source_category = static_cast<FeedsModelCategory*>(source_item);
FeedsModelCategory *new_category = new FeedsModelCategory(source_category);
// Process all children of this category.
original_parents.push(new_category);
new_parents.push(source_category);
FeedsModelCategory *new_category = new FeedsModelCategory(*source_category);
// Add category to model.
new_category->clearChildren();
addCategory(new_category, target_parent);
if (addCategory(new_category, target_parent)) {
// Process all children of this category.
original_parents.push(new_category);
new_parents.push(source_category);
}
else {
some_feed_category_error = true;
}
}
else if (source_item->kind() == FeedsModelRootItem::Feed) {
FeedsModelFeed *source_feed = static_cast<FeedsModelFeed*>(source_item);
@ -559,7 +567,9 @@ bool FeedsModel::mergeRootItem(FeedsModelRootItem *root_item, QString &output_me
FeedsModelFeed *new_feed = new FeedsModelFeed(*source_feed);
// Append this feed and end this iteration.
addFeed(new_feed, target_parent);
if (!addFeed(new_feed, target_parent)) {
some_feed_category_error = true;
}
}
}
}
@ -567,7 +577,14 @@ bool FeedsModel::mergeRootItem(FeedsModelRootItem *root_item, QString &output_me
// Changes are done now. Finalize the new model.
emit layoutChanged();
return true;
if (some_feed_category_error) {
output_message = tr("Import successfull, but some feeds/categories were not imported due to error.");
}
else {
output_message = tr("Import was completely successfull.");
}
return !some_feed_category_error;
}
void FeedsModel::reloadChangedLayout(QModelIndexList list) {

View file

@ -47,8 +47,8 @@ FeedsModelFeed::FeedsModelFeed(FeedsModelRootItem *parent_item)
m_totalCount(0),
m_unreadCount(0),
m_autoUpdateType(DontAutoUpdate),
m_autoUpdateRemainingInterval(DEFAULT_AUTO_UPDATE_INTERVAL),
m_autoUpdateInitialInterval(DEFAULT_AUTO_UPDATE_INTERVAL),
m_autoUpdateRemainingInterval(DEFAULT_AUTO_UPDATE_INTERVAL),
m_encoding(QString()),
m_url(QString()) {
m_kind = FeedsModelRootItem::Feed;
@ -56,13 +56,18 @@ FeedsModelFeed::FeedsModelFeed(FeedsModelRootItem *parent_item)
FeedsModelFeed::FeedsModelFeed(const FeedsModelFeed &other)
: FeedsModelRootItem(NULL), m_totalCount(0), m_unreadCount(0) {
m_passwordProtected = other.passwordProtected();
m_username = other.username();
m_password = other.password();
m_status = other.status();
m_networkError = other.networkError();
m_type = other.type();
m_autoUpdateType = other.autoUpdateType();
m_autoUpdateInitialInterval = other.autoUpdateInitialInterval();
m_autoUpdateRemainingInterval = other.autoUpdateRemainingInterval();
m_encoding = other.encoding();
m_url = other.url();
m_kind = other.kind();
m_kind = FeedsModelRootItem::Feed;
m_title = other.title();
m_id = other.id();
m_icon = other.icon();
@ -533,6 +538,6 @@ void FeedsModelFeed::updateMessages(const QList<Message> &messages) {
}
}
QNetworkReply::NetworkError FeedsModelFeed::networkError() const {
return m_networkError;
}

View file

@ -169,6 +169,8 @@ class FeedsModelFeed : public FeedsModelRootItem {
m_status = status;
}
QNetworkReply::NetworkError networkError() const;
// Loads standard feed object from given SQL record.
static FeedsModelFeed *loadFromRecord(const QSqlRecord &record);

View file

@ -237,6 +237,7 @@ void FormImportExport::importFeeds() {
QString output_message;
if (qApp->mainForm()->tabWidget()->feedMessageViewer()->feedsView()->sourceModel()->mergeRootItem(m_model->rootItem(), output_message)) {
qApp->mainForm()->tabWidget()->feedMessageViewer()->feedsView()->expandAll();
m_ui->m_lblResult->setStatus(WidgetWithStatus::Ok, output_message, output_message);
}
else {