Debian Codegraph @

Source/exadrumsConfig.h

raw
1 /*
2 * exadrumsConfig.h
3 *
4 * Created on: 9 Jun 2018
5 * Author: jeremy
6 */
7
8 #ifndef SOURCE_EXADRUMSCONFIG_H_
9 #define SOURCE_EXADRUMSCONFIG_H_
10
11 #include "../config.h"
12 #include "system.h"
13 #include "Util/ErrorHandler.h"
14 #include <libexadrums/Api/eXaDrums.hpp>
15 #include <libexadrums/Api/Config/Config_api.hpp>
16
17 #if __has_include(<filesystem>)
18 #include <filesystem>
19 namespace fs = std::filesystem;
20 #else
21 #include <experimental/filesystem>
22 namespace fs = std::experimental::filesystem;
23 #endif
24
25 #include <gtkmm/application.h>
26 #include <gtkmmconfig.h>
27 #include <glibmm/optiongroup.h>
28 #include <glibmm/optioncontext.h>
29 #include <giomm.h>
30
31 #include <utility>
32 #include <type_traits>
33 #include <string>
34 #include <iostream>
35 #include <cstdlib>
36
37 #include <unistd.h>
38
39 namespace eXaDrums
40 {
41
42 constexpr char VERSION_SEPARATOR[] = ".";
43 bool is_fullscreen = false;
44
45 class Config
46 {
47
48 public:
49
50 Config() = default;
51 ~Config() = default;
52
53 template <typename... T>
54 static auto VersionToStr(const T&... v)
55 {
56 std::string result = ( (std::to_string(v) + VERSION_SEPARATOR) + ... );
57 result.pop_back();
58 return result;
59 }
60
61 int CommandLineParser(const Glib::RefPtr<Gio::ApplicationCommandLine>& cmd, Glib::RefPtr<Gtk::Application>& app)
62 {
63 using namespace eXaDrumsApi;
64
65 Glib::OptionContext optionContext;
66 Glib::OptionGroup optionGroup("options", "Application Options: ");
67 optionContext.add_group(optionGroup);
68
69 // Add version entry
70 auto versionEntry = MakeOption<bool>("version", 'v', "Show version information and exit");
71 optionGroup.add_entry(versionEntry.first, versionEntry.second);
72
73 // Add fullscreen entry
74 auto fullscreenEntry = MakeOption<bool>("fullscreen", 'f', "Start in fullscreen mode");
75 optionGroup.add_entry(fullscreenEntry.first, fullscreenEntry.second);
76
77 // Import config from backup file
78 auto importConfigEntry = MakeOption<Glib::ustring>("import-config", 'i', "Import configuration from file");
79 optionGroup.add_entry(importConfigEntry.first, importConfigEntry.second);
80
81 // Export config to folder
82 auto exportConfigEntry = MakeOption<Glib::ustring>("export-config", 'e', "Export configuration to file");
83 optionGroup.add_entry(exportConfigEntry.first, exportConfigEntry.second);
84
85 // Add reset config entry
86 auto resetConfigEntry = MakeOption<bool>("reset-config", 'r', "Reset to default configuration");
87 optionGroup.add_entry(resetConfigEntry.first, resetConfigEntry.second);
88
89 // Get arguments
90 int argc;
91 char** argv = cmd->get_arguments(argc);
92 optionContext.parse(argc, argv);
93
94 // Handle results
95 if(versionEntry.second)
96 {
97 std::cout << "eXaDrums version " PACKAGE_VERSION "\n\n";
98 std::cout << "using libeXaDrums version " << eXaDrumsApi::eXaDrums::GetVersion() << " (compiled against version " << LIBEXADRUMS_VERSION << ")\n";
99 std::cout << "using gtkmm version " << VersionToStr(GTKMM_MAJOR_VERSION, GTKMM_MINOR_VERSION, GTKMM_MICRO_VERSION) << std::endl;
100 return 0;
101 }
102
103 if(fullscreenEntry.second)
104 {
105 is_fullscreen = true;
106 }
107
108 if(resetConfigEntry.second)
109 {
110
111 if(AnswerQuestion("This will reset the configuration to defaults. \n Current settings will be lost.\n"
112 "Do you want to continue? [Y/n] ", "y"))
113 {
114 ResetConfig();
115 std::cout << "The configuration has been reset." << std::endl;
116 }
117
118 if(!AnswerQuestion("Do you want to start eXaDrums? [Y/n] ", "y"))
119 {
120 return 0;
121 }
122
123 }
124
125 if(!importConfigEntry.second.empty())
126 {
127
128 std::cout << "Imported configuration from file " << importConfigEntry.second << std::endl;
129
130 try
131 {
132 eXaDrumsApi::Config::ImportConfig(importConfigEntry.second, userPath.string());
133 }
134 catch(const std::exception& e)
135 {
136 std::cerr << e.what() << '\n';
137 }
138
139 if(!AnswerQuestion("Do you want to start eXaDrums? [Y/n] ", "y"))
140 {
141 return 0;
142 }
143
144 }
145
146 if(!exportConfigEntry.second.empty())
147 {
148 try
149 {
150 eXaDrumsApi::Config::ExportConfig(userPath.string(), exportConfigEntry.second);
151 std::cout << "Configuration exported to " << exportConfigEntry.second << std::endl;
152 }
153 catch(const std::exception& e)
154 {
155 std::cerr << e.what() << '\n';
156 }
157
158 if(!AnswerQuestion("Do you want to start eXaDrums? [Y/n] ", "y"))
159 {
160 return 0;
161 }
162 }
163
164 // Activate the app
165 app->activate();
166
167 return 0;
168 }
169
170 private:
171
172 template <typename T>
173 std::pair<Glib::OptionEntry, T> MakeOption(const std::string& ln, char sn, const std::string& desc)
174 {
175 Glib::OptionEntry optionEntry;
176 optionEntry.set_long_name(ln.data());
177 optionEntry.set_short_name(sn);
178 optionEntry.set_description(desc);
179
180 return std::make_pair(optionEntry, T{});
181 }
182
183 /**
184 * @brief Ask the user a question, and return true if the answer is good (equals expectedAnswer).
185 *
186 * @param question Question to be submitted to the user.
187 * @param expectedAnswer Expected answer.
188 * @param isCaseSensitive Whether the answer is case sensitive or not.
189 * @return true If the answer is the same as expectedAnswer.
190 * @return false If the answer is not the same as expectedAnswer.
191 */
192 bool AnswerQuestion(const std::string& question, const std::string& expectedAnswer, bool isCaseSensitive = false)
193 {
194
195 std::cout << question;
196
197 std::string input;
198 std::cin >> input;
199
200 if(!isCaseSensitive)
201 {
202 std::transform(begin(input), end(input), begin(input), [](const auto c){ return std::tolower(c); });
203 }
204
205 if(input == expectedAnswer)
206 {
207 return true;
208 }
209
210 return false;
211 }
212
213 const fs::path userPath = systemUserPath;
214 const fs::path rootPath{systemRootPath};
215 const bool isRoot = systemIsRoot;
216
217 };
218
219 }
220
221
222 #endif /* SOURCE_EXADRUMSCONFIG_H_ */