aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bookmarks/builtins.cpp87
-rw-r--r--src/builtins.h2
-rw-r--r--src/main.cpp5
-rw-r--r--src/meson.build2
4 files changed, 93 insertions, 3 deletions
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 <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");
+ parser.Prog(progname);
+
+ args::HelpFlag help(parser, "help", "Display this help message and exit.", { 'h', "help" });
+ args::ValueFlag<std::string> save(parser, "file, stdout", "Export the bookmarks as (defaults to bookmark path)", { 'e', "export" });
+ args::ValueFlagList<std::string> import_xbel(parser, "bookmarks.xbel", "Import xbel bookmarks", { 'x', "import-xbel" });
+ args::ValueFlagList<std::string> 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<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;
+}
diff --git a/src/builtins.h b/src/builtins.h
index 088aa23..f805f6b 100644
--- a/src/builtins.h
+++ b/src/builtins.h
@@ -21,6 +21,8 @@ int version();
int build();
int configuration(const std::string &progname, std::vector<std::string>::const_iterator beginargs, std::vector<std::string>::const_iterator endargs);
+
+int bookmarks(const std::string &progname, std::vector<std::string>::const_iterator beginargs, std::vector<std::string>::const_iterator endargs);
}
#endif
diff --git a/src/main.cpp b/src/main.cpp
index be6a559..98e8ef9 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -47,7 +47,8 @@ int main(int argc, char **argv)
#endif
const command_map commands{
- { "configuration", builtins::configuration }
+ { "configuration", builtins::configuration },
+ { "bookmarks", builtins::bookmarks },
};
const std::vector<std::string> args(argv + 1, argv + argc);
@@ -77,7 +78,7 @@ int main(int argc, char **argv)
return builtins::build();
// create and load configuration
- spdlog::info("Loading configuration {}", init_conf(args::get(cmd_config)));
+ spdlog::debug("Loading configuration {}", init_conf(args::get(cmd_config)));
if(cmd_args) {
const auto front = args::get(cmd_args).front();
diff --git a/src/meson.build b/src/meson.build
index 1783455..4f37567 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -38,7 +38,7 @@ poi_sourceset.add(files(
'mainwindow/widgets/navigationbar.cpp',
'mainwindow/widgets/searchform.cpp',
- 'bookmarks/bookmarkswidget.cpp', 'bookmarks/editbookmarkdialog.cpp',
+ 'bookmarks/builtins.cpp', 'bookmarks/bookmarkswidget.cpp', 'bookmarks/editbookmarkdialog.cpp',
'session/session.cpp',
'session/savesessiondialog.cpp',