aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.cpp
blob: c114155145b20052074df08086652caaca134fec (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * 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/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "configuration.h"
#include <QStandardPaths>
#include <fstream>

namespace po = boost::program_options;

Configuration::Configuration()
{
    m_homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString();

    // create description
    desc.add_options()

        // Browser default settings
        // default profile name the browser should use; "" is off-the-record
        ("browser.profile", po::value<std::string>()->default_value(""))

        // default window size
        ("browser.window.height", po::value<int>()->default_value(720))
        ("browser.window.width", po::value<int>()->default_value(1280))
        ("browser.window.maximized", po::value<bool>()->default_value(true))
        ("browser.window.title", po::value<std::string>()->default_value("title — smolbote [profile]"))

        // window ui
        ("browser.ui.navtoolbarMovable", po::value<bool>()->default_value(false))
        ("browser.ui.tabtoolbarMovable", po::value<bool>()->default_value(false))

        // browser shortcuts

        // browser menu
        ("browser.shortcuts.newWindow", po::value<std::string>()->default_value("Ctrl+N"))
        ("browser.shortcuts.newTab", po::value<std::string>()->default_value("Ctrl+T"))
        ("browser.shortcuts.about", po::value<std::string>()->default_value("F1"))
        ("browser.shortcuts.quit", po::value<std::string>()->default_value("Ctrl+Q"))

        // navigation
        ("browser.shortcuts.back", po::value<std::string>()->default_value("Ctrl+Left"))
        ("browser.shortcuts.forward", po::value<std::string>()->default_value("Ctrl+Right"))
        ("browser.shortcuts.refresh", po::value<std::string>()->default_value("F5"))
        ("browser.shortcuts.reload", po::value<std::string>()->default_value("Ctrl+F5"))
        ("browser.shortcuts.home", po::value<std::string>()->default_value("Ctrl+Home"))

        // tabs
        ("browser.shortcuts.tabClose", po::value<std::string>()->default_value("Ctrl+X"))
        ("browser.shortcuts.tabLeft", po::value<std::string>()->default_value("Ctrl+O"))
        ("browser.shortcuts.tabRight", po::value<std::string>()->default_value("Ctrl+P"))

        // page
        ("browser.shortcuts.toggleSearchBox", po::value<std::string>()->default_value("F3"))
        ("browser.shortcuts.focusAddress", po::value<std::string>()->default_value("F4"))
        ("browser.shortcuts.fullscreen", po::value<std::string>()->default_value("F11"))

        // Filter settings
        ("filter.path", po::value<std::string>()->default_value("~/.config/smolbote/hosts.d"))

        // Plugin settings
        ("plugins.path", po::value<std::string>()->default_value("~/.config/smolbote/plugins.d"))

        // Profile settings
        ("profile.path", po::value<std::string>()->default_value("~/.config/smolbote/profiles.d"))
        ("profile.search", po::value<std::string>()->default_value("https://duckduckgo.com/?q=$term&ia=web"))
        ("profile.homepage", po::value<std::string>()->default_value("about:blank"))
        ("profile.newtab", po::value<std::string>()->default_value("about:blank"))

        // Bookmark settings
        ("bookmarks.path", po::value<std::string>()->default_value("~/.config/smolbote/bookmarks.xbel"))
        ("bookmarks.shortcut", po::value<std::string>()->default_value("Ctrl+B"))

        // Downloads settings
        ("downloads.path", po::value<std::string>()->default_value("~/Downloads"))
        ("downloads.shortcut", po::value<std::string>()->default_value("Ctrl+D"))
        ;

    // store the defaults into the vm
    {
        const char* argv[0];
        po::store(po::parse_command_line(0, argv, desc), vm);
    }

}

Configuration::~Configuration() = default;

bool Configuration::read(const QString &path)
{
    std::ifstream f(path.toStdString(), std::ifstream::in);
    po::store(po::parse_config_file(f, desc, false), vm);
    return true;
}

bool Configuration::parse(int argc, const char **argv)
{
    try {
        po::store(po::parse_command_line(argc, argv, desc), vm);
    } catch (const po::error &e) {
        return false;
    }

    return true;
}