aboutsummaryrefslogtreecommitdiff
path: root/src/bookmarks/builtins.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/bookmarks/builtins.cpp')
-rw-r--r--src/bookmarks/builtins.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/bookmarks/builtins.cpp b/src/bookmarks/builtins.cpp
new file mode 100644
index 0000000..259bfa6
--- /dev/null
+++ b/src/bookmarks/builtins.cpp
@@ -0,0 +1,100 @@
+/*
+ * 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 <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);
+ }
+
+ 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);
+ }
+
+ 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");
+ }
+
+ BookmarkFormat<XbelFormat> format(output);
+ format << model;
+ output->close();
+
+ delete output;
+
+ return 0;
+}