aboutsummaryrefslogtreecommitdiff
path: root/src/bookmarks/builtin.cpp
blob: 732aa64af48deb300760c9e142145c0d7924cf79 (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
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/cgit/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "bookmarkmodel.h"
#include "browser.h"
#include "configuration.h"
#include <QBuffer>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QFile>
#include <cstdlib>
#include <iostream>
#include <spdlog/spdlog.h>

template <auto T>
[[nodiscard]] inline bool import(BookmarkModel *model, const QString &path)
{
    QFile f(path);
    if(f.open(QIODevice::ReadOnly | QIODevice::Text)) {
        BookmarkFormat<T>(&f) >> model;
        f.close();
        return true;
    }
    return false;
}

namespace builtins
{
int sub_bookmarks(const QStringList &l, Browser &)
{
    const QCommandLineOption output({ "o", "output" }, "Output location (default: append to browser bookmarks).", "file");

    const QCommandLineOption import_xbel({ "x", "import-xbel" }, "Import xbel format.", "xbel");
    const QCommandLineOption import_json({ "j", "import-json" }, "Import json format.", "json");

    QCommandLineParser parser;
    parser.setApplicationDescription("smolbote: bookmarks");
    parser.addHelpOption();

    parser.addOptions({ output, import_xbel, import_json });

    if(l.count() <= 1) {
        parser.showHelp();
    }

    parser.process(l);
    Configuration conf;
    const auto bookmarks_path = conf.value<QString>("bookmarks.path").value();
    auto *model = new BookmarkModel;

    // implicit bookmarks.path import
    if(!parser.isSet(output)) {
        if(!import<XbelFormat>(model, bookmarks_path)) {
            spdlog::error("Could not open %s", qUtf8Printable(bookmarks_path));
        }
    }

    for(const auto &i : parser.values(import_xbel)) {
        if(!import<XbelFormat>(model, i)) {
            spdlog::error("Could not open %s", qUtf8Printable(i));
        }
    }

    for(const auto &i : parser.values(import_json)) {
        if(!import<FirefoxJsonFormat>(model, i)) {
            spdlog::error("Could not open %s", qUtf8Printable(i));
        }
    }

    QIODevice *out = nullptr;
    if(!parser.isSet(output)) {
        out = new QFile(bookmarks_path);
    } else {
        const auto o = parser.value(output);

        if(o == "stdout") {
            out = new QBuffer;
            QObject::connect(out, &QIODevice::aboutToClose, [out]() {
                out->seek(0);
                std::cout << qUtf8Printable(out->readAll()) << std::endl;
            });

        } else {
            out = new QFile(o);
        }
    }

    if(!out->isOpen()) {
        if(!out->open(QIODevice::ReadWrite | QIODevice::Text)) {
            spdlog::error("Could not open output");
            return EXIT_FAILURE;
        }
    }

    BookmarkFormat<XbelFormat> format(out);
    format << model;
    out->close();

    delete out;
    delete model;
    return EXIT_FAILURE;
}
} // namespace