|
| 1 | +/* |
| 2 | + gebaar |
| 3 | + Copyright (C) 2019 coffee2code |
| 4 | + |
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | + |
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | + |
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | + |
| 20 | +#include <zconf.h> |
| 21 | +#include "config.h" |
| 22 | + |
| 23 | +bool gebaar::config::Config::find_config_file() |
| 24 | +{ |
| 25 | + auto true_path = std::filesystem::path(config_file_path); |
| 26 | + return std::filesystem::exists(true_path); |
| 27 | +} |
| 28 | + |
| 29 | +void gebaar::config::Config::load_config() |
| 30 | +{ |
| 31 | + if (find_home_folder()) { |
| 32 | + if (find_config_file()) { |
| 33 | + config = cpptoml::parse_file(std::filesystem::path(config_file_path)); |
| 34 | + swipe_three_up_command = *config->get_qualified_as<std::string>("commands.swipe.three.up"); |
| 35 | + swipe_three_down_command = *config->get_qualified_as<std::string>("commands.swipe.three.down"); |
| 36 | + swipe_three_left_command = *config->get_qualified_as<std::string>("commands.swipe.three.left"); |
| 37 | + swipe_three_right_command = *config->get_qualified_as<std::string>("commands.swipe.three.right"); |
| 38 | + swipe_four_up_command = *config->get_qualified_as<std::string>("commands.swipe.four.up"); |
| 39 | + swipe_four_down_command = *config->get_qualified_as<std::string>("commands.swipe.four.down"); |
| 40 | + swipe_four_left_command = *config->get_qualified_as<std::string>("commands.swipe.four.left"); |
| 41 | + swipe_four_right_command = *config->get_qualified_as<std::string>("commands.swipe.four.right"); |
| 42 | + loaded = true; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +bool gebaar::config::Config::find_home_folder() |
| 49 | +{ |
| 50 | + const char* temp_path; |
| 51 | + temp_path = getenv("XDG_CONFIG_HOME"); |
| 52 | + if (temp_path==nullptr) { |
| 53 | + temp_path = getenv("HOME"); |
| 54 | + if (temp_path==nullptr) { |
| 55 | + temp_path = getpwuid(getuid())->pw_dir; |
| 56 | + } |
| 57 | + } |
| 58 | + if (temp_path!=nullptr) { |
| 59 | + config_file_path = temp_path; |
| 60 | + config_file_path.append("/.config/gebaar/gebaard.toml"); |
| 61 | + return true; |
| 62 | + } |
| 63 | + return false; |
| 64 | +} |
| 65 | + |
| 66 | +gebaar::config::Config::Config() |
| 67 | +{ |
| 68 | + if (!loaded) { |
| 69 | + load_config(); |
| 70 | + } |
| 71 | +} |
0 commit comments