diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index 59c054175..14e892344 100644 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -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; } diff --git a/src/gui/feedmessageviewer.cpp b/src/gui/feedmessageviewer.cpp index a226e93f1..d51e6a534 100644 --- a/src/gui/feedmessageviewer.cpp +++ b/src/gui/feedmessageviewer.cpp @@ -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, diff --git a/src/gui/formstandardfeeddetails.cpp b/src/gui/formstandardfeeddetails.cpp index 3392d1860..7f953b984 100644 --- a/src/gui/formstandardfeeddetails.cpp +++ b/src/gui/formstandardfeeddetails.cpp @@ -135,9 +135,18 @@ void FormStandardFeedDetails::onUseDefaultIcon() { void FormStandardFeedDetails::apply() { FeedsModelRootItem *parent = static_cast(m_ui->m_cmbParentCategory->itemData(m_ui->m_cmbParentCategory->currentIndex()).value()); + FeedsModelStandardFeed::Type type = static_cast(m_ui->m_cmbType->itemData(m_ui->m_cmbType->currentIndex()).value()); 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 encodings = QTextCodec::availableCodecs();