aboutsummaryrefslogtreecommitdiff
path: root/src/session/builtin.cpp
blob: 0359b42253bcc42917fc5a4e7d36574f2324e678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
}
}