From 1bc1ae8cd09851faa05345f5a6b105b02fe75dd2 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 19 Nov 2020 18:22:36 +0200 Subject: Drop args.hxx dependency Replace args.hxx with QCommandLineParser. --- src/bookmarks/builtin.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/bookmarks/builtin.cpp (limited to 'src/bookmarks/builtin.cpp') 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 +#include +#include +#include +#include +#include +#include + +template +[[nodiscard]] inline bool import(BookmarkModel *model, const QString &path) +{ + QFile f(path); + if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { + BookmarkFormat(&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("bookmarks.path").value(); + auto *model = new BookmarkModel; + + // implicit bookmarks.path import + if(!parser.isSet(output)) { + if(!import(model, bookmarks_path)) { + spdlog::error("Could not open %s", qUtf8Printable(bookmarks_path)); + } + } + + for(const auto &i : parser.values(import_xbel)) { + if(!import(model, i)) { + spdlog::error("Could not open %s", qUtf8Printable(i)); + } + } + + for(const auto &i : parser.values(import_json)) { + if(!import(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 format(out); + format << model; + out->close(); + + delete out; + delete model; + return EXIT_FAILURE; +} +} // namespace -- cgit v1.2.1