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 --- lib/bookmarks/bookmarkitem.cpp | 14 +++---- lib/bookmarks/bookmarkitem.h | 2 +- src/bookmarks/builtins.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++ src/builtins.h | 2 + src/main.cpp | 5 ++- src/meson.build | 2 +- 6 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 src/bookmarks/builtins.cpp diff --git a/lib/bookmarks/bookmarkitem.cpp b/lib/bookmarks/bookmarkitem.cpp index 21d034f..d361e26 100644 --- a/lib/bookmarks/bookmarkitem.cpp +++ b/lib/bookmarks/bookmarkitem.cpp @@ -13,13 +13,7 @@ BookmarkItem::BookmarkItem(const QVector &data, Type type, BookmarkItem *parent) { m_parentItem = parent; - m_type = type; - if(m_type == Folder) { - m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal, QIcon::Off); - m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_DirOpenIcon), QIcon::Normal, QIcon::On); - } else if(m_type == Bookmark) - m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_FileIcon)); m_data.resize(FieldCount); for(int i = 0; i < FieldCount; ++i) { @@ -104,8 +98,14 @@ bool BookmarkItem::setData(Fields column, const QVariant &data) return true; } -QIcon BookmarkItem::icon() const +QIcon BookmarkItem::icon() { + if(m_icon.isNull() && m_type == Folder) { + m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal, QIcon::Off); + m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_DirOpenIcon), QIcon::Normal, QIcon::On); + } else if(m_icon.isNull() && m_type == Bookmark) + m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_FileIcon)); + return m_icon; } diff --git a/lib/bookmarks/bookmarkitem.h b/lib/bookmarks/bookmarkitem.h index 6751526..97d3e89 100644 --- a/lib/bookmarks/bookmarkitem.h +++ b/lib/bookmarks/bookmarkitem.h @@ -48,7 +48,7 @@ public: QVariant data(Fields column) const; bool setData(Fields column, const QVariant &data); - QIcon icon() const; + QIcon icon(); bool isExpanded() const; void setExpanded(bool expanded); 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; +} 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::const_iterator beginargs, std::vector::const_iterator endargs); + +int bookmarks(const std::string &progname, std::vector::const_iterator beginargs, std::vector::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 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', -- cgit v1.2.1