/** * FreeRDP: A Remote Desktop Protocol Implementation * SDL Prefs * * Copyright 2022 Armin Novak * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #if __has_include() #include namespace fs = std::filesystem; #elif __has_include() #include namespace fs = std::experimental::filesystem; #else #error Could not find system header "" or "" #endif #include "sdl_prefs.hpp" #include #include #include #include SdlPref::WINPR_JSONPtr SdlPref::get() { auto config = get_pref_file(); std::ifstream ifs(config); std::string content((std::istreambuf_iterator(ifs)), (std::istreambuf_iterator())); return { WINPR_JSON_ParseWithLength(content.c_str(), content.size()), WINPR_JSON_Delete }; } WINPR_JSON* SdlPref::get_item(const std::string& key) { if (!_config) return nullptr; return WINPR_JSON_GetObjectItem(_config.get(), key.c_str()); } std::string SdlPref::item_to_str(WINPR_JSON* item, const std::string& fallback) { if (!item || !WINPR_JSON_IsString(item)) return fallback; auto str = WINPR_JSON_GetStringValue(item); if (!str) return {}; return str; } std::string SdlPref::get_string(const std::string& key, const std::string& fallback) { auto item = get_item(key); return item_to_str(item, fallback); } bool SdlPref::get_bool(const std::string& key, bool fallback) { auto item = get_item(key); if (!item || !WINPR_JSON_IsBool(item)) return fallback; return WINPR_JSON_IsTrue(item); } int64_t SdlPref::get_int(const std::string& key, int64_t fallback) { auto item = get_item(key); if (!item || !WINPR_JSON_IsNumber(item)) return fallback; auto val = WINPR_JSON_GetNumberValue(item); return static_cast(val); } std::vector SdlPref::get_array(const std::string& key, const std::vector& fallback) { auto item = get_item(key); if (!item || !WINPR_JSON_IsArray(item)) return fallback; std::vector values; for (int x = 0; x < WINPR_JSON_GetArraySize(item); x++) { auto cur = WINPR_JSON_GetArrayItem(item, x); values.push_back(item_to_str(cur)); } return values; } SdlPref::SdlPref(const std::string& file) : _name(file), _config(get()) { } std::string SdlPref::get_pref_dir() { using CStringPtr = std::unique_ptr; CStringPtr path(GetKnownPath(KNOWN_PATH_XDG_CONFIG_HOME), free); if (!path) return {}; fs::path config{ path.get() }; config /= FREERDP_VENDOR; config /= FREERDP_PRODUCT; return config.string(); } std::string SdlPref::get_default_file() { fs::path config{ SdlPref::get_pref_dir() }; config /= "sdl-freerdp.json"; return config.string(); } std::shared_ptr SdlPref::instance(const std::string& name) { static std::shared_ptr _instance; if (!_instance || (_instance->get_pref_file() != name)) _instance.reset(new SdlPref(name)); return _instance; } std::string SdlPref::get_pref_file() { return _name; }