aboutsummaryrefslogtreecommitdiff
path: root/src/session/builtin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/session/builtin.cpp')
-rw-r--r--src/session/builtin.cpp61
1 files changed, 61 insertions, 0 deletions
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 <QCommandLineParser>
+#include <variant>
+
+namespace builtins
+{
+int sub_session(const QStringList &args, Browser &app)
+{
+ const auto default_profile = []() {
+ Configuration conf;
+ return conf.value<QString>("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;
+}
+}