From bfa620630eb1c45cbec740ff05d7655991eca777 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 17 Jan 2020 15:43:48 +0200 Subject: Add bookmarks subcommand --- src/bookmarks/builtins.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/bookmarks/builtins.cpp (limited to 'src/bookmarks/builtins.cpp') diff --git a/src/bookmarks/builtins.cpp b/src/bookmarks/builtins.cpp new file mode 100644 index 0000000..8881038 --- /dev/null +++ b/src/bookmarks/builtins.cpp @@ -0,0 +1,87 @@ +/* + * 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/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "builtins.h" +#include "bookmarkformat.h" +#include "configuration.h" +#include +#include +#include +#include + +int builtins::bookmarks(const std::string &progname, std::vector::const_iterator beginargs, std::vector::const_iterator endargs) +{ + args::ArgumentParser parser("bookmarks"); + parser.Prog(progname); + + args::HelpFlag help(parser, "help", "Display this help message and exit.", { 'h', "help" }); + args::ValueFlag save(parser, "file, stdout", "Export the bookmarks as (defaults to bookmark path)", { 'e', "export" }); + args::ValueFlagList import_xbel(parser, "bookmarks.xbel", "Import xbel bookmarks", { 'x', "import-xbel" }); + args::ValueFlagList import_json(parser, "bookmarks.json", "Import json bookmarks", { 'j', "import-json" }); + + try { + parser.ParseArgs(beginargs, endargs); + } catch(args::Help &) { + std::cout << parser; + return 0; + } catch(args::Error &e) { + std::cerr << e.what() << std::endl; + std::cerr << parser; + return -1; + } + + auto *model = new BookmarkModel; + + for(const auto &i : args::get(import_xbel)) { + QFile f(QString::fromStdString(i)); + if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { + BookmarkFormat(&f) >> model; + f.close(); + } else + spdlog::error("Could not open %s", i); + } + + for(const auto &i : args::get(import_json)) { + QFile f(QString::fromStdString(i)); + if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { + BookmarkFormat(&f) >> model; + f.close(); + } else + spdlog::error("Could not open %s", i); + } + + QIODevice *output = nullptr; + if(save && args::get(save) == "stdout") { + output = new QBuffer; + QObject::connect(output, &QIODevice::aboutToClose, [output]() { + output->seek(0); + std::cout << qUtf8Printable(output->readAll()) << std::endl; + }); + + output->open(QIODevice::ReadWrite | QIODevice::Text); + + } else if(!save) { + Configuration conf; + output = new QFile(conf.value("bookmarks.path").value()); + } else { + output = new QFile(QString::fromStdString(args::get(save))); + } + + if(!output->isOpen()) { + if(!output->open(QIODevice::ReadWrite | QIODevice::Text)) + spdlog::error("Could not open output"); + } + + BookmarkFormat format(output); + format << model; + output->close(); + + delete output; + + return 0; +} -- cgit v1.2.1