summaryrefslogtreecommitdiff
path: root/src/atomchannel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/atomchannel.cpp')
-rw-r--r--src/atomchannel.cpp107
1 files changed, 96 insertions, 11 deletions
diff --git a/src/atomchannel.cpp b/src/atomchannel.cpp
index 95f4f0d..d89d4ef 100644
--- a/src/atomchannel.cpp
+++ b/src/atomchannel.cpp
@@ -79,9 +79,9 @@ AtomChannel::AtomChannel(QXmlStreamReader *xmlReader)
}
// TODO: move to a separate function, code is almost identical in all methods called `getDbId`
-int AtomChannel::getDbId()
+std::optional<int> AtomChannel::getDbId()
{
- if (m_id != rsshit::db::IdNotFound)
+ if (m_id)
return m_id;
const auto db = rsshit::db::open();
@@ -120,17 +120,23 @@ int AtomChannel::getDbId()
}
// TODO: move to a separate function, code is almost identical in all methods called `createInDb`
-int AtomChannel::createInDb()
+std::optional<int> AtomChannel::createInDb()
{
- if (m_id != rsshit::db::IdNotFound)
+ if (m_id)
return m_id;
const auto db = rsshit::db::open();
- if (!db)
+ if (!db) {
return rsshit::db::IdNotFound;
+ }
+
+ QSqlQuery insertQ;
+ if (!insertQ.prepare("insert into feeds(link, title, image_url) values(?, ?, ?)")) {
+ qCritical() << "cannot prepare query:" << insertQ.lastError().text();
- QSqlQuery insertQ{"insert into feeds(link, title, image_url) values(?, ?, ?)"};
+ return rsshit::db::IdNotFound;
+ }
insertQ.addBindValue(link);
insertQ.addBindValue(title);
insertQ.addBindValue(image.url);
@@ -148,7 +154,7 @@ int AtomChannel::createInDb()
}
// TODO: can be moved to IDbObject
-int AtomChannel::getOrInsertDbId()
+std::optional<int> AtomChannel::getOrInsertDbId()
{
const auto id = getDbId();
@@ -160,18 +166,19 @@ int AtomChannel::getOrInsertDbId()
QList<int> AtomChannel::syncDbItems()
{
- if (m_id == rsshit::db::IdNotFound)
+ if (!m_id)
m_id = getOrInsertDbId();
- if (m_id == rsshit::db::IdNotFound)
+ if (!m_id)
return {};
QList<int> result;
+ result.reserve(items.count());
for (auto &item : items) {
- auto id = item.getOrInsertDbId(this->m_id);
+ auto id = item.getOrInsertDbId(*this->m_id);
- if (id != rsshit::db::IdNotFound)
+ if (id)
result << id;
}
@@ -199,3 +206,81 @@ QDebug operator<<(QDebug debug, const AtomChannel &channel)
return debug;
}
+
+QDebug operator<<(QDebug debug, const Channel &channel)
+{
+ QDebugStateSaver saver{debug};
+
+ const auto &mo = Channel::staticMetaObject;
+
+ debug.nospace() << mo.className() << " {" << Qt::endl;
+
+ for (int i = 0; i < mo.propertyCount(); ++i) {
+ auto prop = mo.property(i);
+ debug.nospace() << "\t" << prop.name() << ": " << prop.readOnGadget(&channel) << Qt::endl;
+ }
+
+ debug.nospace() << "}";
+
+ return debug;
+}
+
+QDebug operator<<(QDebug debug, const Item &item)
+{
+ QDebugStateSaver saver{debug};
+
+ const auto &mo = Item::staticMetaObject;
+
+ debug.nospace() << mo.className() << " {" << Qt::endl;
+
+ for (int i = 0; i < mo.propertyCount(); ++i) {
+ auto prop = mo.property(i);
+ debug.nospace() << "\t" << prop.name() << ": " << prop.readOnGadget(&item) << Qt::endl;
+ }
+
+ debug.nospace() << "}";
+
+ return debug;
+}
+
+Channel::Channel(const QDomNode &node)
+ : DbItem{node}
+{
+ init(staticMetaObject, node);
+
+ // use 'atom:link href="LINK"' if any
+ const QString atomLinkTag = QStringLiteral("atom:link");
+ const QString hrefTag = QStringLiteral("href");
+ set_link(node.toElement()
+ .elementsByTagName(atomLinkTag)
+ .at(0)
+ .toElement()
+ .attribute(hrefTag, get_link().toString()));
+}
+
+bool Channel::isValid() const
+{
+ staticMetaObject;
+ return !m_title.isEmpty() && m_link.isValid();
+}
+
+DbItem::DbItem(const QDomNode &node)
+{
+ init(staticMetaObject, node);
+}
+
+void DbItem::init(const QMetaObject &mo, const QDomNode &node)
+{
+ for (int i = mo.propertyOffset(); i < mo.propertyCount(); ++i) {
+ auto prop = mo.property(i);
+ QVariant variant{node.toElement().elementsByTagName(prop.name()).at(0).toElement().text()};
+ const bool converted = variant.convert(QMetaType{prop.typeId()});
+ prop.writeOnGadget(this, converted ? std::move(variant) : QVariant{});
+ }
+}
+
+Item::Item(const QDomNode &node)
+ : DbItem{node}
+{
+ init(staticMetaObject, node);
+}