/* * 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/gitea/aqua/smolbote * * SPDX-License-Identifier: GPL-3.0 */ #include "configuration.h" #include #include #include #include #include static std::unique_ptr s_conf; inline void strip(std::string &value) { value.erase(value.begin(), std::find_if(value.begin(), value.end(), std::bind1st(std::not_equal_to(), ' '))); value.erase(std::find_if(value.rbegin(), value.rend(), std::bind1st(std::not_equal_to(), ' ')).base(), value.end()); } Configuration::Configuration() : use_global(true) { if(!s_conf) throw new std::runtime_error("Trying to use default Configuration, but none has been set!"); } Configuration::Configuration(std::initializer_list> l) noexcept : m_homePath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString()) { for(const auto &i : l) { insert_or_assign(i.first, i.second); } } void Configuration::read(std::basic_istream &input) { std::string line, key, value; std::istringstream is_line; while(std::getline(input, line)) { if(line[0] == '#' || line.length() == 0) continue; is_line.clear(); is_line.str(line); if(std::getline(is_line, key, '=')) { is_line >> value; strip(key); strip(value); if(this->count(key) == 0) { // no type has been specified for this key, assuming std::string insert(std::make_pair(key, value)); continue; } auto v = at(key); if(std::holds_alternative(v)) { at(key) = value; } else if(std::holds_alternative(v)) { at(key) = std::stoi(value); } else if(std::holds_alternative(v)) { at(key) = (value == "true"); } } } } void Configuration::move_global(std::unique_ptr &&conf) { s_conf = std::move(conf); } Configuration *Configuration::instance() { return s_conf.get(); } void setShortcut(QAction *action, const char *name) { if(!s_conf) throw new std::runtime_error("Trying to set a shortcut, but no configuration has been set!"); if(const auto shortcutText = s_conf->value(name)) { const QString tooltip = action->toolTip(); action->setShortcut(QKeySequence::fromString(shortcutText.value())); action->setToolTip(QString("%1 (%2)").arg(tooltip, shortcutText.value())); } #ifdef QT_DEBUG else { std::cout << "fixme: setShortcut called for " << name << ", but no such value exists!" << std::endl; } #endif } std::ostream &operator<<(std::ostream &out, const Configuration &obj) { // unordered_map is, well, unordered, so grab the keys and sort them before printing them std::vector keys; if(obj.use_global) { if(!s_conf) throw new std::runtime_error("Trying to use default Configuration, but none has been set!"); for(const auto &pair : *s_conf) keys.emplace_back(pair.first); } else { for(const auto &pair : obj) out << pair.first << "\n"; } std::sort(keys.begin(), keys.end()); for(const auto &key : keys) { if(obj.use_global) out << key << "=" << s_conf->value(key.c_str()).value() << "\n"; else out << key << "=" << obj.value(key.c_str()).value() << "\n"; } return out; }