summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 22029a903070514937c3517354306fedf3cacf78 (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
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QTimer>

#include "constants.h"
#include "playground.h"
#include "rsshit_db.h"
#include "user.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // Set up code that uses the Qt event loop here.
    // Call a.quit() or a.exit() to quit the application.
    // A not very useful example would be including
    // #include <QTimer>
    // near the top of the file and calling
    // QTimer::singleShot(5000, &a, &QCoreApplication::quit);
    // which quits the application after 5 seconds.

    // If you do not need a running Qt event loop, remove the call
    // to a.exec() or use the Non-Qt Plain C++ Application template.

    QFile dbSqliteResource{rsshit::db::dbSqliteResourceFilename};
    const auto dataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    qDebug() << "data location:" << dataLocation;

    if (!QDir{}.mkpath(dataLocation)) {
        qWarning() << "cannot create " << dataLocation;
        return EXIT_FAILURE;
    }

    const QString newDbFilepath{dataLocation + '/' + QFileInfo{dbSqliteResource}.fileName()};

#define RECREATE_DB_FILE

#ifdef RECREATE_DB_FILE
    if (!QFile::remove(newDbFilepath)) {
        qWarning() << "cannot remove" << newDbFilepath;
    }

    if (!dbSqliteResource.copy(newDbFilepath)) {
        qWarning() << "cannot copy db file (" << dbSqliteResource.fileName()
                   << ") from resources to data dir (" << newDbFilepath
                   << "):" << dbSqliteResource.errorString();
        return EXIT_FAILURE;
    }
#endif

    QFile newDbFile{newDbFilepath};

    if (!newDbFile.setPermissions(QFileDevice::Permission::ReadUser
                                  | QFileDevice::Permission::WriteUser)) {
        qWarning() << "cannot set file permissions:" << newDbFile.fileName() << Qt::endl
                   << newDbFile.errorString();

        return EXIT_FAILURE;
    }

    User user{"admin", "123"};
    qDebug() << "verify password 123:" << user.verifyPassword("123");
    qDebug() << "verify password 321:" << user.verifyPassword("321");
    qDebug() << "user.getDbId()" << user.getDbId();
    qDebug() << "user.createInDb()" << user.createInDb();
    qDebug() << "user.createInDb()" << user.getOrInsertDbId();
    qDebug() << "user.createInDb()" << user.createInDb();

    Playground playground;

    QTimer::singleShot(0, [&]() {
        // const QString url{"https://jenngott.com/feed/"};
        // const QUrl url{"https://liliputing.com/feed"};
        // playground.fetchUrl(url);
        // return;
        // QFile f{"/tmp/rss/feed"};
        QFile f{"/home/nikita/tmp/rss/feed"};
        // QFile f{"/tmp/rss/feed.1"};
        // QFile f{"/tmp/rss/rss"};

        if (!f.open(QFile::ReadOnly)) {
            qWarning() << "cannot open file" << f.fileName() << ":" << f.errorString();
            return;
        }

        const auto channel = playground.parseFeed(&f);

        if (!applyToDb(channel)) {
            qWarning() << "cannot apply channel to db";

            return;
        }
    });

    return a.exec();
}