Very initial implementation of feed adding.

This commit is contained in:
Martin Rotter 2014-01-30 17:19:45 +01:00
parent 45414e9949
commit f96f120aea
3 changed files with 65 additions and 11 deletions

View File

@ -169,8 +169,6 @@ bool FeedsModel::addStandardCategory(FeedsModelStandardCategory *category,
QSqlQuery query_add(database);
query_add.setForwardOnly(true);
// Remove all messages from this standard feed.
query_add.prepare("INSERT INTO Categories "
"(parent_id, title, description, date_created, icon, type) "
"VALUES (:parent_id, :title, :description, :date_created, :icon, :type);");
@ -265,6 +263,53 @@ bool FeedsModel::editStandardCategory(FeedsModelStandardCategory *original_categ
bool FeedsModel::addStandardFeed(FeedsModelStandardFeed *feed,
FeedsModelRootItem *parent) {
// Get index of parent item (parent standard category).
QModelIndex parent_index = indexForItem(parent);
// Now, add category to persistent storage.
// Children are removed, remove this standard category too.
QSqlDatabase database = DatabaseFactory::instance()->connection(objectName(),
DatabaseFactory::FromSettings);
QSqlQuery query_add(database);
query_add.setForwardOnly(true);
query_add.prepare("INSERT INTO Feeds "
"(title, description, date_created, icon, category, encoding, url, type) "
"VALUES (:title, :description, :date_created, :icon, :category, :encoding, :url, :type);");
query_add.bindValue(":title", feed->title());
query_add.bindValue(":description", feed->description());
query_add.bindValue(":date_created", feed->creationDate().toMSecsSinceEpoch());
query_add.bindValue(":icon", IconFactory::toByteArray(feed->icon()));
query_add.bindValue(":category", parent->id());
query_add.bindValue(":encoding", feed->encoding());
query_add.bindValue(":url", feed->url());
query_add.bindValue(":type", (int) FeedsModelCategory::Standard);
if (!query_add.exec()) {
// Query failed.
return false;
}
query_add.prepare("SELECT id FROM Feeds WHERE date_created = :date_created;");
query_add.bindValue(":date_created", feed->creationDate().toMSecsSinceEpoch());
if (query_add.exec() && query_add.next()) {
// New category was added, fetch is primary id
// from the database.
feed->setId(query_add.value(0).toInt());
}
else {
// Something failed.
return false;
}
// Category was added to the persistent storage,
// so add it to the model.
beginInsertRows(parent_index, parent->childCount(), parent->childCount());
parent->appendChild(feed);
endInsertRows();
return true;
return false;
}

View File

@ -55,10 +55,10 @@ void FeedMessageViewer::saveSize() {
// Store offsets of splitters.
settings->setValue(APP_CFG_GUI,
"splitter_feeds",
m_feedSplitter->saveState());
m_feedSplitter->saveState().toBase64());
settings->setValue(APP_CFG_GUI,
"splitter_messages",
m_messageSplitter->saveState());
m_messageSplitter->saveState().toBase64());
// States of splitters are stored, let's store
// widths of columns.
@ -75,8 +75,8 @@ void FeedMessageViewer::loadSize() {
int default_msg_section_size = m_messagesView->header()->defaultSectionSize();
// Restore offsets of splitters.
m_feedSplitter->restoreState(settings->value(APP_CFG_GUI, "splitter_feeds").toByteArray());
m_messageSplitter->restoreState(settings->value(APP_CFG_GUI, "splitter_messages").toByteArray());
m_feedSplitter->restoreState(QByteArray::fromBase64(settings->value(APP_CFG_GUI, "splitter_feeds").toByteArray()));
m_messageSplitter->restoreState(QByteArray::fromBase64(settings->value(APP_CFG_GUI, "splitter_messages").toByteArray()));
// Splitters are restored, now, restore widths of columns.
m_messagesView->setColumnWidth(MSG_DB_AUTHOR_INDEX,

View File

@ -135,9 +135,18 @@ void FormStandardFeedDetails::onUseDefaultIcon() {
void FormStandardFeedDetails::apply() {
FeedsModelRootItem *parent = static_cast<FeedsModelRootItem*>(m_ui->m_cmbParentCategory->itemData(m_ui->m_cmbParentCategory->currentIndex()).value<void*>());
FeedsModelStandardFeed::Type type = static_cast<FeedsModelStandardFeed::Type>(m_ui->m_cmbType->itemData(m_ui->m_cmbType->currentIndex()).value<int>());
FeedsModelStandardFeed *new_feed = new FeedsModelStandardFeed();
// TODO: Setup data for new_feed.
new_feed->setTitle(m_ui->m_txtTitle->lineEdit()->text());
new_feed->setCreationDate(QDateTime::currentDateTime());
new_feed->setDescription(m_ui->m_txtDescription->lineEdit()->text());
new_feed->setIcon(m_ui->m_btnIcon->icon());
new_feed->setEncoding(m_ui->m_cmbEncoding->currentText());
new_feed->setType(type);
new_feed->setUrl(m_ui->m_txtUrl->lineEdit()->text());
new_feed->setParent(parent);
if (m_editableFeed == NULL) {
// TODO: Add the feed.
@ -184,7 +193,7 @@ void FormStandardFeedDetails::setEditableFeed(FeedsModelStandardFeed *editable_f
m_ui->m_txtTitle->lineEdit()->setText(editable_feed->title());
m_ui->m_txtDescription->lineEdit()->setText(editable_feed->description());
m_ui->m_btnIcon->setIcon(editable_feed->icon());
m_ui->m_cmbType->setCurrentIndex(m_ui->m_cmbType->findData(QVariant::fromValue((void*) editable_feed->type())));
m_ui->m_cmbType->setCurrentIndex(m_ui->m_cmbType->findData(QVariant::fromValue((int) editable_feed->type())));
m_ui->m_cmbEncoding->setCurrentIndex(m_ui->m_cmbEncoding->findData(editable_feed->encoding(), Qt::DisplayRole));
m_ui->m_txtUrl->lineEdit()->setText(editable_feed->url());
}
@ -215,10 +224,10 @@ void FormStandardFeedDetails::initialize() {
#endif
// Add standard feed types.
m_ui->m_cmbType->addItem(FeedsModelFeed::typeToString(FeedsModelFeed::StandardAtom10), QVariant::fromValue((void*) FeedsModelFeed::StandardAtom10));
m_ui->m_cmbType->addItem(FeedsModelFeed::typeToString(FeedsModelFeed::StandardRdf), QVariant::fromValue((void*) FeedsModelFeed::StandardRdf));
m_ui->m_cmbType->addItem(FeedsModelFeed::typeToString(FeedsModelFeed::StandardRss0X), QVariant::fromValue((void*) FeedsModelFeed::StandardRss0X));
m_ui->m_cmbType->addItem(FeedsModelFeed::typeToString(FeedsModelFeed::StandardRss2X), QVariant::fromValue((void*) FeedsModelFeed::StandardRss2X));
m_ui->m_cmbType->addItem(FeedsModelFeed::typeToString(FeedsModelFeed::StandardAtom10), QVariant::fromValue((int) FeedsModelFeed::StandardAtom10));
m_ui->m_cmbType->addItem(FeedsModelFeed::typeToString(FeedsModelFeed::StandardRdf), QVariant::fromValue((int) FeedsModelFeed::StandardRdf));
m_ui->m_cmbType->addItem(FeedsModelFeed::typeToString(FeedsModelFeed::StandardRss0X), QVariant::fromValue((int) FeedsModelFeed::StandardRss0X));
m_ui->m_cmbType->addItem(FeedsModelFeed::typeToString(FeedsModelFeed::StandardRss2X), QVariant::fromValue((int) FeedsModelFeed::StandardRss2X));
// Load available encodings.
QList<QByteArray> encodings = QTextCodec::availableCodecs();