diff options
Diffstat (limited to 'src/bookmarks/builtin.cpp')
-rw-r--r-- | src/bookmarks/builtin.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/bookmarks/builtin.cpp b/src/bookmarks/builtin.cpp new file mode 100644 index 0000000..732aa64 --- /dev/null +++ b/src/bookmarks/builtin.cpp @@ -0,0 +1,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 |