summaryrefslogtreecommitdiff
path: root/src/config.hpp
blob: bc5698e2e8e975f7524a77d5ae2e3e3d7c30c08c (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef GETSUYOMI_CONFIG_HPP
#define GETSUYOMI_CONFIG_HPP

#include <fud_result.hpp>
#include <fud_status.hpp>
#include <map>
#include <optional>
#include <qkeysequence.h>
#include <qlist.h>
#include <set>
#include <string>
#include <filesystem>

namespace getsuyomi {

constexpr const char* AppVendor = "libfud";

constexpr const char* AppName = "getsuyomi";

constexpr const char* AppVersionString = "1.0.0";

constexpr const char* HOME{"HOME"};

enum class ActionType
{
    OpenFile,
    OpenDirectory,
    Quit,
    Configure,
    // Help,
    // About
    // GotoFirst,
    Next,
    Back,
    // GotoLast,
    SinglePage,
    DualPage,
    MangaPage
};

struct GetsuyomiConfig;

using GetEnvResult = fud::Result<GetsuyomiConfig, fud::FudStatus>;
using GetConfigResult = fud::Result<GetsuyomiConfig, fud::FudStatus>;
using ShortcutList = QList<QKeySequence>;
using ShortcutSet = std::set<QKeySequence>;
using ShortcutMap = std::map<ActionType, ShortcutSet>;
using ShortcutRevMap = std::map<QKeySequence, ActionType>;

std::optional<std::string> getEnvVar(const char* envVar);

void getEnvVar(const std::string& homeVar, const std::string& envVar, std::string& envValue, const char* backup);

GetEnvResult getEnvironment();

fud::FudStatus createXdgDirectory(const std::string& directoryName);

void setUserConfig(GetsuyomiConfig& config, const ShortcutMap& shortcutMap);

GetConfigResult getUserConfig();

constexpr const char* actionTypeToString(ActionType action)
{
    switch (action) {
    case ActionType::OpenFile:
        return "OpenFile";
    case ActionType::OpenDirectory:
        return "OpenDirectory";
    case ActionType::Quit:
        return "Quit";
    case ActionType::Configure:
        return "Configure";
        // case ActionType::Help:
        return "Help";
        // case ActionType::About:
        // return "About";
        // case ActionType::GotoFirst:
        // return "GotoFirst";
    case ActionType::Next:
        return "Next";
    case ActionType::Back:
        return "Back";
        // case ActionType::GotoLast:
        // return "GotoLast";
    case ActionType::SinglePage:
        return "SinglePage";
    case ActionType::DualPage:
        return "DualPage";
    case ActionType::MangaPage:
        return "MangaPage";
    default:
        return "Unknown";
    }
}

ShortcutSet shortcutSetFromList(const ShortcutList& shortcutList);
ShortcutList shortcutListFromSet(const ShortcutSet& shortcutList);

class Shortcuts {
  public:
    Shortcuts(ShortcutMap&& shortcutMap);

    [[nodiscard]] bool contains(QKeySequence keySequence) const;
    [[nodiscard]] bool contains(ActionType action) const;

    std::vector<ActionType> actions() const;

    std::optional<ActionType> action(QKeySequence keySequence) const;

    std::optional<ShortcutList> shortcuts(ActionType action) const;

    fud::FudStatus bind(ActionType action, QKeySequence keySequence);
    fud::FudStatus remove(QKeySequence keySequence);
    fud::FudStatus clear(ActionType action);

    [[nodiscard]] bool valid() const;

    const ShortcutMap& shortcutMap() const;

    static ShortcutMap fromUserConfig(const std::filesystem::path& configFileName);

    fud::FudStatus save(const std::filesystem::path& configFileName) const;

  private:
    Shortcuts() = default;

    static Shortcuts fromLuaConfig(const std::filesystem::path& configFileName);

    // fud::FudStatus saveAsLuaConfig(const std::f

    bool m_valid{false};

    ShortcutMap m_actionToShortcuts{};
    ShortcutSet m_shortcuts{};
    ShortcutRevMap m_shortcutToAction{};
};

struct GetsuyomiConfig {
    std::string home{};
    std::string dataHome{};
    std::string configHome{};
    std::string stateHome{};
    std::filesystem::path configFilename{};

    ShortcutList openFileShortcuts{};
    ShortcutList openDirectoryShortcuts{};
    ShortcutList quitShortcuts{};

    ShortcutList settingsShortcuts{};

    ShortcutList nextShortcuts{};
    ShortcutList backShortcuts{};

    ShortcutList singlePageShortcuts{};
    ShortcutList dualPageShortcuts{};
    ShortcutList mangaPageShortcuts{};

    ShortcutMap shortcuts() const;
};

} // namespace getsuyomi

#endif