summaryrefslogtreecommitdiff
path: root/src/atomchannel.cpp
blob: 0f3bca2e68c9c9419835c82065950ccdc74c7ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include "atomchannel.h"

#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QXmlStreamReader>

#include "constants.h"
#include "macros.h"
#include "rsshit_db.h"

AtomChannel::AtomChannel(QXmlStreamReader *xmlReader)
{
    Q_ASSERT(xmlReader != nullptr);

    const QString titleTag{"title"};
    const QString linkTag{"link"};
    const QString descriptionTag{"description"};
    const QString lastBuildDateTag{"lastBuildDate"};
    const QString languageTag{"language"};

    while (!xmlReader->atEnd() && !xmlReader->hasError()) {
        const auto itemNext = xmlReader->readNext();

        switch (itemNext) {
        case QXmlStreamReader::TokenType::StartElement: {
            const auto name = xmlReader->name();
            // qDebug() << __func__ << ": StartElement" << name;
            // qDebug() << __func__ << "namespaceUri:" << xmlReader->namespaceUri();
            // qDebug() << __func__ << "prefix:" << xmlReader->prefix();
            // qDebug() << __func__ << "qualifiedName:" << xmlReader->qualifiedName();
            // const auto elementText = xmlReader->readElementText();

            if (name == titleTag)
                title = xmlReader->readElementText();
            else if (name == linkTag)
                link = xmlReader->readElementText();
            else if (name == descriptionTag)
                description = xmlReader->readElementText();
            else if (name == lastBuildDateTag)
                lastBuildDate = QDateTime::fromString(xmlReader->readElementText(),
                                                      Qt::DateFormat::RFC2822Date);
            else if (name == languageTag)
                language = QLocale{xmlReader->readElementText()}.language();
            else if (name == AtomChannelImage::tag) {
                // qDebug() << "got image tag";
                image = AtomChannelImage{xmlReader};
                // qDebug() << image;
            } else if (name == AtomItem::tag) {
                items << AtomItem{xmlReader};
                // qDebug() << items.constLast();
                // qDebug() << __func__ << "got feed item";
            } else {
                // qDebug() << "exit" << __func__;
                // qDebug() << __func__ << "unknown tag:" << name;
                continue;
            }

            break;
        }
        case QXmlStreamReader::TokenType::EndElement: {
            // qDebug() << "EndElement: " << xmlReader->name();

            if (xmlReader->name() == AtomChannel::xmlTag)
                return;
        }
        case QXmlStreamReader::TokenType::Characters: {
            const auto characters = xmlReader->text().toString().simplified();

            if (characters.isEmpty())
                break;

            qDebug() << "channel: characters: " << characters;
            break;
        }
        }
    }

    qDebug() << "exit " << __func__;
}

// TODO: move to a separate function, code is almost identical in all methods called `getDbId`
std::optional<int> AtomChannel::getDbId()
{
    if (m_id)
        return m_id;

    const auto db = rsshit::db::open();

    if (!db)
        return rsshit::db::IdNotFound;

    QSqlQuery selectQ{"select id from feeds where link=?"};
    selectQ.addBindValue(link);

    if (!selectQ.exec()) {
        qWarning() << "cannot exec query" << selectQ.lastQuery() << ":"
                   << selectQ.lastError().text();

        return rsshit::db::IdNotFound;
    }

    if (!selectQ.next())
        return rsshit::db::IdNotFound;

    const auto idVariant = selectQ.value(rsshit::db::idTag);

    if (!idVariant.isValid() || !idVariant.canConvert<int>())
        return rsshit::db::IdNotFound;

    bool ok{false};
    const auto result = idVariant.toInt(&ok);

    if (!ok) {
        qWarning() << "got invalid id from db:" << idVariant;

        return rsshit::db::IdNotFound;
    }

    return result;
}

// TODO: move to a separate function, code is almost identical in all methods called `createInDb`
std::optional<int> AtomChannel::createInDb()
{
    if (m_id)
        return m_id;

    const auto db = rsshit::db::open();

    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();

        return rsshit::db::IdNotFound;
    }
    insertQ.addBindValue(link);
    insertQ.addBindValue(title);
    insertQ.addBindValue(image.url);

    if (!insertQ.exec()) {
        qWarning() << "cannot exec query" << insertQ.lastQuery() << ":"
                   << insertQ.lastError().text();

        return rsshit::db::IdNotFound;
    }

    m_id = insertQ.lastInsertId().toInt();

    return m_id;
}

// TODO: can be moved to IDbObject
std::optional<int> AtomChannel::getOrInsertDbId()
{
    const auto id = getDbId();

    if (id != rsshit::db::IdNotFound)
        return id;

    return createInDb();
}

QList<int> AtomChannel::syncDbItems()
{
    if (!m_id)
        m_id = getOrInsertDbId();

    if (!m_id)
        return {};

    QList<int> result;
    result.reserve(items.count());

    for (auto &item : items) {
        auto id = item.getOrInsertDbId(*this->m_id);

        if (id)
            result << id;
    }

    return result;
}

QDebug operator<<(QDebug debug, const AtomChannel &feed)
{
    QDebugStateSaver saver{debug};

    debug.nospace() << typeid(AtomChannel).name() << " {" << Qt::endl;

    PRINT_ATOM_FIELD(feed, title);
    PRINT_ATOM_FIELD(feed, link);
    PRINT_ATOM_FIELD(feed, description);
    PRINT_ATOM_FIELD(feed, lastBuildDate);
    PRINT_ATOM_FIELD(feed, language);
    debug << "\timage:\n" << feed.image << Qt::endl;
    debug << "\titems count:" << feed.items.size() << Qt::endl;

    for (const auto &item : feed.items)
        debug << item << Qt::endl;

    debug.nospace() << "}";

    return debug;
}

QDebug operator<<(QDebug debug, const Feed &feed)
{
    QDebugStateSaver saver{debug};

    // const auto &mo = Channel::staticMetaObject;
    const auto &mo = *feed.metaObject();

    debug.nospace() << mo.className() << " {" << Qt::endl;

    for (int i = mo.propertyOffset(); i < mo.propertyCount(); ++i) {
        auto prop = mo.property(i);
        debug.nospace() << "\t" << prop.name() << ": " << prop.readOnGadget(&feed) << Qt::endl;
    }

    debug.nospace() << "}";

    return debug;
}

QDebug operator<<(QDebug debug, const Item &item)
{
    QDebugStateSaver saver{debug};

    // const auto &mo = Item::staticMetaObject;
    const auto &mo = *item.metaObject();

    debug.nospace() << mo.className() << " {" << Qt::endl;

    for (int i = mo.propertyOffset(); i < mo.propertyCount(); ++i) {
        auto prop = mo.property(i);
        debug.nospace() << "\t" << prop.name() << ": " << prop.readOnGadget(&item) << Qt::endl;
    }

    debug.nospace() << "}";

    return debug;
}

Feed::Feed(const QDomNode &node)
    : DbItem{node}
{
    init(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 Feed::isValid() const
{
    return !m_title.isEmpty() && m_link.isValid();
}

const QStringList &Feed::uniqueFields() const
{
    // TODO: validate once that this class has corresponding fields
    return m_uniqueFields;
}

DbItem::DbItem(const QDomNode &node)
{
    init(node);
}

void DbItem::init(const QDomNode &node)
{
    // for (int i = mo.propertyOffset(); i < mo.propertyCount(); ++i) {
    for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); ++i) {
        const auto prop = metaObject()->property(i);
        const auto propName = prop.name();
        const auto propElement = node.toElement().elementsByTagName(propName).at(0);
        const auto propText = propElement.toElement().text();
        const auto propType = QMetaType{prop.typeId()};

        switch (propType.id()) {
        case QMetaType::Type::QDateTime: {
            prop.writeOnGadget(this, QDateTime::fromString(propText, dateFormat));
            break;
        }
        case QMetaType::Type::QStringList: {
            const auto elements = node.toElement().elementsByTagName(propName);
            QStringList values;
            values.reserve(elements.size());

            for (const auto &element : elements) {
                values << element.toElement().text();
            }

            prop.writeOnGadget(this, std::move(values));

            break;
        }
        default: {
            QVariant variant{propText};
            variant.convert(propType);
            prop.writeOnGadget(this, std::move(variant));
        }
        }
    }
}

QStringList DbItem::propertyNames() const
{
    // TODO: init once to static var
    const auto propOffset = QObject::staticMetaObject.propertyCount();
    const auto propCount = metaObject()->propertyCount();

    QStringList result;
    result.reserve(propOffset - propCount);

    for (int i = propOffset; i < propCount; ++i) {
        result << metaObject()->property(i).name();
    }

    return result;
}

QList<QMetaProperty> DbItem::properties() const
{
    // TODO: init once to static var
    const auto propOffset = QObject::staticMetaObject.propertyCount();
    const auto propCount = metaObject()->propertyCount();

    QList<QMetaProperty> result;
    result.reserve(propOffset - propCount);

    for (int i = propOffset; i < propCount; ++i) {
        result << metaObject()->property(i);
    }

    return result;
}

DbItem::DbId DbItem::syncWithDb()
{
    // const auto propOffset = QObject::staticMetaObject.propertyCount();
    // const auto propCount = metaObject()->propertyCount();
    // const auto propNames = propertyNames();

    // "Item" -> "items"
    QString tableName{metaObject()->className()};
    tableName[0] = tableName.at(0).toLower();
    tableName.append('s');

    qDebug() << Q_FUNC_INFO << "table name:" << tableName;

    // exists in db, update data
    if (m_id.has_value()) {
        // TODO: what to do if item was removed from db?
        return m_id;
    }
    // select id from db or insert
    else {
        QSqlQuery q;

        // TODO: validate once statically
        if (uniqueFields().isEmpty()) {
            qFatal() << metaObject()->className() << "has no unique fields, cannot select";

            return {};
        }

        // TODO: init once
        QStringList whereConditions;

        // for (const auto &name : propertyNames()) {
        for (const auto &name : uniqueFields()) {
            whereConditions << name + QStringLiteral("=:") + name;
        }

        const QString where{QStringLiteral(" where (")
                            + whereConditions.join(QStringLiteral(" and ")) + ')'};
        // const QString queryTemplate{QStringLiteral("select * from ") + tableName + ' ' + where};
        qDebug() << "where:" << where;
        const QString queryTemplate{QStringLiteral("select * from ") + tableName + where};
        qDebug().noquote() << "template:" << queryTemplate;

        const bool prepared = q.prepare(queryTemplate);

        if (!prepared) {
            qCritical() << tableName + ": cannot prepare select query:" << q.lastError().text();
            qCritical().noquote() << "template: " << queryTemplate;

            return {};
        }

        for (const auto &prop : properties()) {
            // FIXME: create uniqueProperties
            if (!uniqueFields().contains(prop.name()))
                continue;
            q.addBindValue(prop.read(this).toString());
            // q.addBindValue("'asdfsdf'");
        }

        if (!q.exec()) {
            qCritical() << "cannot select" << tableName << "from db:" << q.lastError().text();
            qCritical().noquote() << "template: " << queryTemplate;

            return {};
        }

        if (!q.next()) {
            qCritical() << "no results for" << tableName;
            // TODO: inset this shit. always inset it
            return {};
        }

        const auto rec = q.record();

        if (rec.isEmpty()) {
            qCritical() << "got empty record for" << tableName;
            return {};
        }

        // qDebug() << "rec:" << rec;

        bool dbNeedsUpdate{false};

        for (const auto &prop : properties()) {
            const auto val = rec.value(prop.name());

            if (val.isNull() || !val.isValid()) {
                qCritical() << "got invalid value for table" << tableName << "fieldl"
                            << prop.name();
                return {};
            }

            // if (prop.read(this) != val) {
            //     // TODO: don't use this shit with tmp vars, call update here and break
            //     dbNeedsUpdate = true;
            //     break;
            // }

            prop.write(this, val);
        }

        if (dbNeedsUpdate) {
        }

        qDebug() << "finished reading from" << tableName;
    }

    // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    // FIXME: implement
    return {};
}

Item::Item(const QDomNode &node)
    : DbItem{node}
{
    init(node);

    if (m_content.isEmpty()) {
        const QString contentEncodedTag = QStringLiteral("content:encoded");
        set_content(node.toElement().elementsByTagName(contentEncodedTag).at(0).toElement().text());
    }
}

bool Item::isValid() const
{
    // TODO: check if isNull is needed
    return !m_pubDate.isNull() && m_pubDate.isValid() && !m_title.isEmpty() && !m_link.isEmpty()
           && !m_description.isEmpty();
}

const QStringList &Item::uniqueFields() const
{
    return m_uniqueFields;
}