diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-11-19 18:22:36 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-11-19 18:22:36 +0200 |
commit | 1bc1ae8cd09851faa05345f5a6b105b02fe75dd2 (patch) | |
tree | 4fd33c0f8c79157031defd9f87f0dee4331b85de /src/bookmarks | |
parent | Remove updater (diff) | |
download | smolbote-1bc1ae8cd09851faa05345f5a6b105b02fe75dd2.tar.xz |
Drop args.hxx dependency
Replace args.hxx with QCommandLineParser.
Diffstat (limited to 'src/bookmarks')
-rw-r--r-- | src/bookmarks/builtin.cpp | 108 | ||||
-rw-r--r-- | src/bookmarks/builtins.cpp | 105 | ||||
-rw-r--r-- | src/bookmarks/meson.build | 8 |
3 files changed, 116 insertions, 105 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 diff --git a/src/bookmarks/builtins.cpp b/src/bookmarks/builtins.cpp deleted file mode 100644 index f7b6f3f..0000000 --- a/src/bookmarks/builtins.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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 "bookmarkmodel.h" -#include "configuration.h" -#include <QBuffer> -#include <QFile> -#include <iostream> -#include <spdlog/spdlog.h> - -int builtins::bookmarks(const std::string &progname, std::vector<std::string>::const_iterator beginargs, std::vector<std::string>::const_iterator endargs) -{ - args::ArgumentParser parser("bookmarks", - "If an output location is not specified, this command will append imported bookmarks to the browser's bookmarks.\n" - "If an output location is specified, this command will load all XBEL-format bookmarks, then all JSON-format bookmarks, and write them to the output location."); - parser.Prog(progname); - - args::HelpFlag help(parser, "help", "Display this help message and exit.", { 'h', "help" }); - args::ValueFlag<std::string> save(parser, "file, stdout", "Output location.", { 'e', "export" }); - args::ValueFlagList<std::string> import_xbel(parser, "bookmarks.xbel", "Import xbel format.", { 'x', "import-xbel" }); - args::ValueFlagList<std::string> import_json(parser, "bookmarks.json", "Import json format.", { '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; - - // implicit bookmarks.path import - if(!save) { - Configuration conf; - QFile f(conf.value<QString>("bookmarks.path").value()); - if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { - BookmarkFormat<XbelFormat>(&f) >> model; - f.close(); - } - } - - for(const auto &i : args::get(import_xbel)) { - QFile f(QString::fromStdString(i)); - if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { - BookmarkFormat<XbelFormat>(&f) >> model; - f.close(); - } else { - spdlog::error("Could not open %s", i); - return -1; - } - } - - for(const auto &i : args::get(import_json)) { - QFile f(QString::fromStdString(i)); - if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { - BookmarkFormat<FirefoxJsonFormat>(&f) >> model; - f.close(); - } else { - spdlog::error("Could not open %s", i); - return -1; - } - } - - 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<QString>("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"); - return -1; - } - } - - BookmarkFormat<XbelFormat> format(output); - format << model; - output->close(); - - delete output; - - return 0; -} diff --git a/src/bookmarks/meson.build b/src/bookmarks/meson.build new file mode 100644 index 0000000..8fc93f0 --- /dev/null +++ b/src/bookmarks/meson.build @@ -0,0 +1,8 @@ +poi_sourceset.add(files('builtin.cpp', + 'bookmarkswidget.cpp', 'editbookmarkdialog.cpp', 'bookmarkstoolbar.cpp'), +mod_qt5.preprocess( + moc_headers: ['bookmarkswidget.h', 'editbookmarkdialog.h'], + ui_files: ['bookmarksform.ui', 'editbookmarkdialog.ui'], + dependencies: dep_qt5 +)) + |