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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/*
* exadrumsConfig.h
*
* Created on: 9 Jun 2018
* Author: jeremy
*/
#ifndef SOURCE_EXADRUMSCONFIG_H_
#define SOURCE_EXADRUMSCONFIG_H_
#include "../config.h"
#include "system.h"
#include "Util/ErrorHandler.h"
#include <libexadrums/Api/eXaDrums.hpp>
#include <libexadrums/Api/Config/Config_api.hpp>
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
#include <gtkmm/application.h>
#include <gtkmmconfig.h>
#include <glibmm/optiongroup.h>
#include <glibmm/optioncontext.h>
#include <giomm.h>
#include <utility>
#include <type_traits>
#include <string>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
namespace eXaDrums
{
constexpr char VERSION_SEPARATOR[] = ".";
bool is_fullscreen = false;
class Config
{
public:
Config() = default;
~Config() = default;
template <typename... T>
static auto VersionToStr(const T&... v)
{
std::string result = ( (std::to_string(v) + VERSION_SEPARATOR) + ... );
result.pop_back();
return result;
}
int CommandLineParser(const Glib::RefPtr<Gio::ApplicationCommandLine>& cmd, Glib::RefPtr<Gtk::Application>& app)
{
using namespace eXaDrumsApi;
Glib::OptionContext optionContext;
Glib::OptionGroup optionGroup("options", "Application Options: ");
optionContext.add_group(optionGroup);
// Add version entry
auto versionEntry = MakeOption<bool>("version", 'v', "Show version information and exit");
optionGroup.add_entry(versionEntry.first, versionEntry.second);
// Add fullscreen entry
auto fullscreenEntry = MakeOption<bool>("fullscreen", 'f', "Start in fullscreen mode");
optionGroup.add_entry(fullscreenEntry.first, fullscreenEntry.second);
// Import config from backup file
auto importConfigEntry = MakeOption<Glib::ustring>("import-config", 'i', "Import configuration from file");
optionGroup.add_entry(importConfigEntry.first, importConfigEntry.second);
// Export config to folder
auto exportConfigEntry = MakeOption<Glib::ustring>("export-config", 'e', "Export configuration to file");
optionGroup.add_entry(exportConfigEntry.first, exportConfigEntry.second);
// Add reset config entry
auto resetConfigEntry = MakeOption<bool>("reset-config", 'r', "Reset to default configuration");
optionGroup.add_entry(resetConfigEntry.first, resetConfigEntry.second);
// Get arguments
int argc;
char** argv = cmd->get_arguments(argc);
optionContext.parse(argc, argv);
// Handle results
if(versionEntry.second)
{
std::cout << "eXaDrums version " PACKAGE_VERSION "\n\n";
std::cout << "using libeXaDrums version " << eXaDrumsApi::eXaDrums::GetVersion() << " (compiled against version " << LIBEXADRUMS_VERSION << ")\n";
std::cout << "using gtkmm version " << VersionToStr(GTKMM_MAJOR_VERSION, GTKMM_MINOR_VERSION, GTKMM_MICRO_VERSION) << std::endl;
return 0;
}
if(fullscreenEntry.second)
{
is_fullscreen = true;
}
if(resetConfigEntry.second)
{
if(AnswerQuestion("This will reset the configuration to defaults. \n Current settings will be lost.\n"
"Do you want to continue? [Y/n] ", "y"))
{
ResetConfig();
std::cout << "The configuration has been reset." << std::endl;
}
if(!AnswerQuestion("Do you want to start eXaDrums? [Y/n] ", "y"))
{
return 0;
}
}
if(!importConfigEntry.second.empty())
{
std::cout << "Imported configuration from file " << importConfigEntry.second << std::endl;
try
{
eXaDrumsApi::Config::ImportConfig(importConfigEntry.second, userPath.string());
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
if(!AnswerQuestion("Do you want to start eXaDrums? [Y/n] ", "y"))
{
return 0;
}
}
if(!exportConfigEntry.second.empty())
{
try
{
eXaDrumsApi::Config::ExportConfig(userPath.string(), exportConfigEntry.second);
std::cout << "Configuration exported to " << exportConfigEntry.second << std::endl;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
if(!AnswerQuestion("Do you want to start eXaDrums? [Y/n] ", "y"))
{
return 0;
}
}
// Activate the app
app->activate();
return 0;
}
private:
template <typename T>
std::pair<Glib::OptionEntry, T> MakeOption(const std::string& ln, char sn, const std::string& desc)
{
Glib::OptionEntry optionEntry;
optionEntry.set_long_name(ln.data());
optionEntry.set_short_name(sn);
optionEntry.set_description(desc);
return std::make_pair(optionEntry, T{});
}
/**
* @brief Ask the user a question, and return true if the answer is good (equals expectedAnswer).
*
* @param question Question to be submitted to the user.
* @param expectedAnswer Expected answer.
* @param isCaseSensitive Whether the answer is case sensitive or not.
* @return true If the answer is the same as expectedAnswer.
* @return false If the answer is not the same as expectedAnswer.
*/
bool AnswerQuestion(const std::string& question, const std::string& expectedAnswer, bool isCaseSensitive = false)
{
std::cout << question;
std::string input;
std::cin >> input;
if(!isCaseSensitive)
{
std::transform(begin(input), end(input), begin(input), [](const auto c){ return std::tolower(c); });
}
if(input == expectedAnswer)
{
return true;
}
return false;
}
const fs::path userPath = systemUserPath;
const fs::path rootPath{systemRootPath};
const bool isRoot = systemIsRoot;
};
}
#endif /* SOURCE_EXADRUMSCONFIG_H_ */
|