/* * 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