From 1bc1ae8cd09851faa05345f5a6b105b02fe75dd2 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 19 Nov 2020 18:22:36 +0200 Subject: Drop args.hxx dependency Replace args.hxx with QCommandLineParser. --- src/session/builtin.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/session/builtin.cpp (limited to 'src/session/builtin.cpp') diff --git a/src/session/builtin.cpp b/src/session/builtin.cpp new file mode 100644 index 0000000..0359b42 --- /dev/null +++ b/src/session/builtin.cpp @@ -0,0 +1,61 @@ +/* + * 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 "browser.h" +#include "configuration.h" +#include "session/sessiondialog.h" +#include "session_json.hpp" +#include +#include + +namespace builtins +{ +int sub_session(const QStringList &args, Browser &app) +{ + const auto default_profile = []() { + Configuration conf; + return conf.value("profile.default").value(); + }(); + + const auto session = args.count() == 0 ? JsonSession(default_profile, args) : [&]() { + const QCommandLineOption profile({ "p", "profile" }, "Create session with specified profile.", "profile-id", default_profile); + const QCommandLineOption pick("pick", "Show all available sessions and select which one to open."); + const QCommandLineOption open({ "o", "open" }, "Open the specified session.", "file"); + + QCommandLineParser parser; + parser.setApplicationDescription("session"); + parser.addHelpOption(); + parser.addOptions({ pick, open, profile }); + + parser.process(args); + + if(parser.isSet(pick)) { + SessionDialog dlg; + if(const auto s = dlg.pickSession()) { + return JsonSession(s.value()); + } + + } else if(parser.isSet(open)) { + QFile sessionJson(parser.value(open)); + if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) { + return JsonSession(sessionJson.readAll()); + } + } + + return JsonSession(parser.isSet(profile) ? parser.value(profile) : default_profile, args); + }(); + + if(app.isPrimary() || !app.remoteEnabled()) { + app.open(session.get()); + return app.exec(); + } + + app.sendMessage(session.serialize()); + return 0; +} +} -- cgit v1.2.1