Some memory fixes for import/export.
This commit is contained in:
parent
1d65d99f71
commit
1d45832c7b
@ -30,10 +30,15 @@
|
|||||||
|
|
||||||
FeedsImportExportModel::FeedsImportExportModel(QObject *parent)
|
FeedsImportExportModel::FeedsImportExportModel(QObject *parent)
|
||||||
: QAbstractItemModel(parent), m_checkStates(QHash<FeedsModelRootItem*, Qt::CheckState>()),
|
: QAbstractItemModel(parent), m_checkStates(QHash<FeedsModelRootItem*, Qt::CheckState>()),
|
||||||
m_rootItem(NULL), m_recursiveChange(false) {
|
m_rootItem(NULL), m_recursiveChange(false), m_mode(Import) {
|
||||||
}
|
}
|
||||||
|
|
||||||
FeedsImportExportModel::~FeedsImportExportModel() {
|
FeedsImportExportModel::~FeedsImportExportModel() {
|
||||||
|
if (m_rootItem != NULL && m_mode == Import) {
|
||||||
|
// Delete all model items, but only if we are in import mode. Export mode shares
|
||||||
|
// root item with main feed model, thus cannot be deleted from memory now.
|
||||||
|
delete m_rootItem;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FeedsModelRootItem *FeedsImportExportModel::itemForIndex(const QModelIndex &index) const {
|
FeedsModelRootItem *FeedsImportExportModel::itemForIndex(const QModelIndex &index) const {
|
||||||
@ -229,6 +234,14 @@ bool FeedsImportExportModel::importAsOPML20(const QByteArray &data) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FeedsImportExportModel::Mode FeedsImportExportModel::mode() const {
|
||||||
|
return m_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FeedsImportExportModel::setMode(const FeedsImportExportModel::Mode &mode) {
|
||||||
|
m_mode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
QModelIndex FeedsImportExportModel::index(int row, int column, const QModelIndex &parent) const {
|
QModelIndex FeedsImportExportModel::index(int row, int column, const QModelIndex &parent) const {
|
||||||
if (!hasIndex(row, column, parent)) {
|
if (!hasIndex(row, column, parent)) {
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
|
@ -27,6 +27,11 @@ class FeedsImportExportModel : public QAbstractItemModel {
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
enum Mode {
|
||||||
|
Import,
|
||||||
|
Export
|
||||||
|
};
|
||||||
|
|
||||||
// Constructors and destructors.
|
// Constructors and destructors.
|
||||||
explicit FeedsImportExportModel(QObject *parent = 0);
|
explicit FeedsImportExportModel(QObject *parent = 0);
|
||||||
virtual ~FeedsImportExportModel();
|
virtual ~FeedsImportExportModel();
|
||||||
@ -55,12 +60,16 @@ class FeedsImportExportModel : public QAbstractItemModel {
|
|||||||
bool exportToOMPL20(QByteArray &result);
|
bool exportToOMPL20(QByteArray &result);
|
||||||
bool importAsOPML20(const QByteArray &data);
|
bool importAsOPML20(const QByteArray &data);
|
||||||
|
|
||||||
|
Mode mode() const;
|
||||||
|
void setMode(const Mode &mode);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QHash<FeedsModelRootItem*, Qt::CheckState> m_checkStates;
|
QHash<FeedsModelRootItem*, Qt::CheckState> m_checkStates;
|
||||||
FeedsModelRootItem *m_rootItem;
|
FeedsModelRootItem *m_rootItem;
|
||||||
|
|
||||||
// When it's true, then
|
// When it's true, then
|
||||||
bool m_recursiveChange;
|
bool m_recursiveChange;
|
||||||
|
Mode m_mode;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FEEDIMPORTEXPORTMODEL_H
|
#endif // FEEDIMPORTEXPORTMODEL_H
|
||||||
|
@ -163,8 +163,7 @@ bool FeedsModel::removeItem(const QModelIndex &index) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FeedsModel::addCategory(FeedsModelCategory *category,
|
bool FeedsModel::addCategory(FeedsModelCategory *category, FeedsModelRootItem *parent) {
|
||||||
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);
|
||||||
|
|
||||||
@ -210,8 +209,7 @@ bool FeedsModel::addCategory(FeedsModelCategory *category,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FeedsModel::editCategory(FeedsModelCategory *original_category,
|
bool FeedsModel::editCategory(FeedsModelCategory *original_category, FeedsModelCategory *new_category) {
|
||||||
FeedsModelCategory *new_category) {
|
|
||||||
QSqlDatabase database = qApp->database()->connection(objectName(),
|
QSqlDatabase database = qApp->database()->connection(objectName(),
|
||||||
DatabaseFactory::FromSettings);
|
DatabaseFactory::FromSettings);
|
||||||
QSqlQuery query_update_category(database);
|
QSqlQuery query_update_category(database);
|
||||||
@ -245,16 +243,12 @@ bool FeedsModel::editCategory(FeedsModelCategory *original_category,
|
|||||||
int new_index_of_category = new_parent->childCount();
|
int new_index_of_category = new_parent->childCount();
|
||||||
|
|
||||||
// Remove the original item from the model...
|
// Remove the original item from the model...
|
||||||
beginRemoveRows(indexForItem(original_parent),
|
beginRemoveRows(indexForItem(original_parent), original_index_of_category, original_index_of_category);
|
||||||
original_index_of_category,
|
|
||||||
original_index_of_category);
|
|
||||||
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),
|
beginInsertRows(indexForItem(new_parent), new_index_of_category, new_index_of_category);
|
||||||
new_index_of_category,
|
|
||||||
new_index_of_category);
|
|
||||||
new_parent->appendChild(original_category);
|
new_parent->appendChild(original_category);
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
@ -266,13 +260,11 @@ bool FeedsModel::editCategory(FeedsModelCategory *original_category,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FeedsModel::addFeed(FeedsModelFeed *feed,
|
bool FeedsModel::addFeed(FeedsModelFeed *feed, FeedsModelRootItem *parent) {
|
||||||
FeedsModelRootItem *parent) {
|
// Get index of parent item (parent standard category or root item).
|
||||||
// Get index of parent item (parent standard category).
|
|
||||||
QModelIndex parent_index = indexForItem(parent);
|
QModelIndex parent_index = indexForItem(parent);
|
||||||
|
|
||||||
// Now, add category to persistent storage.
|
// Now, add feed to persistent storage.
|
||||||
// Children are removed, remove this standard category too.
|
|
||||||
QSqlDatabase database = qApp->database()->connection(objectName(), DatabaseFactory::FromSettings);
|
QSqlDatabase database = qApp->database()->connection(objectName(), DatabaseFactory::FromSettings);
|
||||||
QSqlQuery query_add_feed(database);
|
QSqlQuery query_add_feed(database);
|
||||||
|
|
||||||
@ -302,8 +294,7 @@ bool FeedsModel::addFeed(FeedsModelFeed *feed,
|
|||||||
query_add_feed.prepare("SELECT id FROM Feeds WHERE date_created = :date_created;");
|
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.bindValue(":date_created", feed->creationDate().toMSecsSinceEpoch());
|
||||||
if (query_add_feed.exec() && query_add_feed.next()) {
|
if (query_add_feed.exec() && query_add_feed.next()) {
|
||||||
// New category was added, fetch is primary id
|
// New feed was added, fetch is primary id from the database.
|
||||||
// from the database.
|
|
||||||
feed->setId(query_add_feed.value(0).toInt());
|
feed->setId(query_add_feed.value(0).toInt());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -311,7 +302,7 @@ bool FeedsModel::addFeed(FeedsModelFeed *feed,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Category was added to the persistent storage so add it to the model.
|
// Feed was added to the persistent storage so add it to the model.
|
||||||
beginInsertRows(parent_index, parent->childCount(), parent->childCount());
|
beginInsertRows(parent_index, parent->childCount(), parent->childCount());
|
||||||
parent->appendChild(feed);
|
parent->appendChild(feed);
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
@ -319,8 +310,7 @@ bool FeedsModel::addFeed(FeedsModelFeed *feed,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FeedsModel::editFeed(FeedsModelFeed *original_feed,
|
bool FeedsModel::editFeed(FeedsModelFeed *original_feed, FeedsModelFeed *new_feed) {
|
||||||
FeedsModelFeed *new_feed) {
|
|
||||||
QSqlDatabase database = qApp->database()->connection(objectName(),
|
QSqlDatabase database = qApp->database()->connection(objectName(),
|
||||||
DatabaseFactory::FromSettings);
|
DatabaseFactory::FromSettings);
|
||||||
QSqlQuery query_update_feed(database);
|
QSqlQuery query_update_feed(database);
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
|
||||||
|
|
||||||
FormImportExport::FormImportExport(QWidget *parent) : QDialog(parent), m_ui(new Ui::FormImportExport), m_mode(Import) {
|
FormImportExport::FormImportExport(QWidget *parent) : QDialog(parent), m_ui(new Ui::FormImportExport) {
|
||||||
m_ui->setupUi(this);
|
m_ui->setupUi(this);
|
||||||
m_model = new FeedsImportExportModel(m_ui->m_treeFeeds);
|
m_model = new FeedsImportExportModel(m_ui->m_treeFeeds);
|
||||||
|
|
||||||
@ -46,15 +46,11 @@ FormImportExport::~FormImportExport() {
|
|||||||
delete m_ui;
|
delete m_ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
FormImportExport::Mode FormImportExport::mode() const {
|
void FormImportExport::setMode(const FeedsImportExportModel::Mode &mode) {
|
||||||
return m_mode;
|
m_model->setMode(mode);
|
||||||
}
|
|
||||||
|
|
||||||
void FormImportExport::setMode(const Mode &mode) {
|
switch (mode) {
|
||||||
m_mode = mode;
|
case FeedsImportExportModel::Export: {
|
||||||
|
|
||||||
switch (m_mode) {
|
|
||||||
case Export: {
|
|
||||||
m_model->setRootItem(qApp->mainForm()->tabWidget()->feedMessageViewer()->feedsView()->sourceModel()->rootItem());
|
m_model->setRootItem(qApp->mainForm()->tabWidget()->feedMessageViewer()->feedsView()->sourceModel()->rootItem());
|
||||||
m_ui->m_treeFeeds->setModel(m_model);
|
m_ui->m_treeFeeds->setModel(m_model);
|
||||||
m_ui->m_treeFeeds->expandAll();
|
m_ui->m_treeFeeds->expandAll();
|
||||||
@ -65,7 +61,7 @@ void FormImportExport::setMode(const Mode &mode) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case Import: {
|
case FeedsImportExportModel::Import: {
|
||||||
m_ui->m_groupFile->setTitle(tr("Source file"));
|
m_ui->m_groupFile->setTitle(tr("Source file"));
|
||||||
m_ui->m_groupFeeds->setTitle(tr("Target feeds && categories"));
|
m_ui->m_groupFeeds->setTitle(tr("Target feeds && categories"));
|
||||||
m_ui->m_treeFeeds->setDisabled(true);
|
m_ui->m_treeFeeds->setDisabled(true);
|
||||||
@ -82,12 +78,12 @@ void FormImportExport::setMode(const Mode &mode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FormImportExport::selectFile() {
|
void FormImportExport::selectFile() {
|
||||||
switch (m_mode) {
|
switch (m_model->mode()) {
|
||||||
case Import:
|
case FeedsImportExportModel::Import:
|
||||||
selectImportFile();
|
selectImportFile();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Export: {
|
case FeedsImportExportModel::Export: {
|
||||||
selectExportFile();
|
selectExportFile();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -187,12 +183,12 @@ void FormImportExport::parseImportFile(const QString &file_name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FormImportExport::performAction() {
|
void FormImportExport::performAction() {
|
||||||
switch (m_mode) {
|
switch (m_model->mode()) {
|
||||||
case Import:
|
case FeedsImportExportModel::Import:
|
||||||
importFeeds();
|
importFeeds();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Export:
|
case FeedsImportExportModel::Export:
|
||||||
exportFeeds();
|
exportFeeds();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -21,14 +21,13 @@
|
|||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
|
|
||||||
#include "ui_formimportexport.h"
|
#include "ui_formimportexport.h"
|
||||||
|
#include "core/feedsimportexportmodel.h"
|
||||||
|
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class FormExport;
|
class FormExport;
|
||||||
}
|
}
|
||||||
|
|
||||||
class FeedsImportExportModel;
|
|
||||||
|
|
||||||
class FormImportExport : public QDialog {
|
class FormImportExport : public QDialog {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@ -37,16 +36,10 @@ class FormImportExport : public QDialog {
|
|||||||
OPML20 = 0
|
OPML20 = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Mode {
|
|
||||||
Import,
|
|
||||||
Export
|
|
||||||
};
|
|
||||||
|
|
||||||
explicit FormImportExport(QWidget *parent = 0);
|
explicit FormImportExport(QWidget *parent = 0);
|
||||||
virtual ~FormImportExport();
|
virtual ~FormImportExport();
|
||||||
|
|
||||||
Mode mode() const;
|
void setMode(const FeedsImportExportModel::Mode &mode);
|
||||||
void setMode(const Mode &mode);
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void performAction();
|
void performAction();
|
||||||
@ -61,7 +54,6 @@ class FormImportExport : public QDialog {
|
|||||||
void importFeeds();
|
void importFeeds();
|
||||||
|
|
||||||
Ui::FormImportExport *m_ui;
|
Ui::FormImportExport *m_ui;
|
||||||
Mode m_mode;
|
|
||||||
ConversionType m_conversionType;
|
ConversionType m_conversionType;
|
||||||
FeedsImportExportModel *m_model;
|
FeedsImportExportModel *m_model;
|
||||||
};
|
};
|
||||||
|
@ -439,56 +439,14 @@ void FormMain::loadWebBrowserMenu(int index) {
|
|||||||
|
|
||||||
void FormMain::exportFeeds() {
|
void FormMain::exportFeeds() {
|
||||||
QPointer<FormImportExport> form = new FormImportExport(this);
|
QPointer<FormImportExport> form = new FormImportExport(this);
|
||||||
form.data()->setMode(FormImportExport::Export);
|
form.data()->setMode(FeedsImportExportModel::Export);
|
||||||
form.data()->exec();
|
form.data()->exec();
|
||||||
delete form.data();
|
delete form.data();
|
||||||
|
|
||||||
/*
|
|
||||||
QString filter_opml20 = tr("OPML 2.0 files (*.opml)");
|
|
||||||
|
|
||||||
QString filter;
|
|
||||||
QString selected_filter;
|
|
||||||
|
|
||||||
// Add more filters here.
|
|
||||||
filter += filter_opml20;
|
|
||||||
|
|
||||||
QString selected_file = QFileDialog::getSaveFileName(this, tr("Select file for feeds import"),
|
|
||||||
QDir::homePath(), filter, &selected_filter);
|
|
||||||
|
|
||||||
|
|
||||||
if (!selected_file.isEmpty()) {
|
|
||||||
bool export_result;
|
|
||||||
QByteArray result_data;
|
|
||||||
|
|
||||||
if (selected_filter == filter_opml20) {
|
|
||||||
export_result = tabWidget()->feedMessageViewer()->feedsView()->sourceModel()->exportToFile(FeedsModel::OPML20,
|
|
||||||
result_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!export_result) {
|
|
||||||
qApp->showGuiMessage(tr("Export failed"),
|
|
||||||
tr("Export of feeds failed, is target file writtable?"),
|
|
||||||
QSystemTrayIcon::Critical);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Save exported data.
|
|
||||||
QFile output_file(selected_file);
|
|
||||||
|
|
||||||
if (output_file.open(QIODevice::Unbuffered | QIODevice::Truncate | QIODevice::WriteOnly)) {
|
|
||||||
QTextStream stream(&output_file);
|
|
||||||
|
|
||||||
stream.setCodec("UTF-8");
|
|
||||||
stream << QString::fromUtf8(result_data);
|
|
||||||
output_file.flush();
|
|
||||||
output_file.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FormMain::importFeeds() {
|
void FormMain::importFeeds() {
|
||||||
QPointer<FormImportExport> form = new FormImportExport(this);
|
QPointer<FormImportExport> form = new FormImportExport(this);
|
||||||
form.data()->setMode(FormImportExport::Import);
|
form.data()->setMode(FeedsImportExportModel::Import);
|
||||||
form.data()->exec();
|
form.data()->exec();
|
||||||
delete form.data();
|
delete form.data();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user