1 #include "vogleditor_settings.h"
2 #include "vogl_common.h"
3 #include "vogl_file_utils.h"
5 static const unsigned int VOGLEDITOR_SETTINGS_FILE_FORMAT_VERSION_1 = 1;
6 static const unsigned int VOGLEDITOR_SETTINGS_FILE_FORMAT_VERSION = VOGLEDITOR_SETTINGS_FILE_FORMAT_VERSION_1;
8 vogleditor_settings::vogleditor_settings()
9 : m_file_format_version(VOGLEDITOR_SETTINGS_FILE_FORMAT_VERSION_1)
11 m_defaults.trim_large_trace_prompt_size = 200;
12 m_defaults.window_position_left = 0;
13 m_defaults.window_position_top = 0;
14 m_defaults.window_size_width = 1024;
15 m_defaults.window_size_height = 768;
17 m_settings = m_defaults;
20 dynamic_string vogleditor_settings::get_settings_path(const char* settingsFilename)
22 dynamic_string settingsPath;
23 const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");
24 const char* home = getenv("HOME");
25 if (xdgConfigHome != NULL && strlen(xdgConfigHome) != 0)
27 settingsPath = xdgConfigHome;
28 settingsPath += "/vogleditor/";
29 if (vogl::file_utils::does_dir_exist(settingsPath.c_str()) == false)
31 if (vogl::file_utils::create_directories_from_full_path(settingsPath) == false)
33 VOGL_ASSERT(!"Failed to create directories for settings file.");
37 else if (home != NULL && strlen(home) != 0)
40 settingsPath += "/.config/vogleditor/";
41 if (vogl::file_utils::does_dir_exist(settingsPath.c_str())== false)
43 if (vogl::file_utils::create_directories_from_full_path(settingsPath) == false)
45 VOGL_ASSERT(!"Failed to create directories for settings file.");
51 // the settings file will end up in the current working directory
54 settingsPath += settingsFilename;
58 bool vogleditor_settings::load(const char* settingsFile)
61 json_document settingsDoc;
62 dynamic_string path = get_settings_path(settingsFile);
63 if (settingsDoc.deserialize_file(path.c_str()))
66 json_node* pMetadata = settingsDoc.get_root()->find_child_object("metadata");
67 if (pMetadata == NULL)
72 const json_value& rFormatVersion = pMetadata->find_value("vogleditor_settings_file_format_version");
73 if (!rFormatVersion.is_valid() || rFormatVersion.as_uint32() != VOGLEDITOR_SETTINGS_FILE_FORMAT_VERSION_1)
78 m_file_format_version = rFormatVersion.as_uint32(VOGLEDITOR_SETTINGS_FILE_FORMAT_VERSION);
80 // validate that settings node exists
81 json_node* pSettingsNode = settingsDoc.get_root()->find_child_object("settings");
82 if (pSettingsNode == NULL)
87 // if so, consider the file successfully loaded
90 // all settings should be considered optional, if they are not in the file, then the default value is used
91 m_settings.trim_large_trace_prompt_size = pSettingsNode->value_as_uint32("trim_large_trace_prompt_size", m_defaults.trim_large_trace_prompt_size);
93 m_settings.window_position_left = pSettingsNode->value_as_int("window_position_left", m_defaults.window_position_left);
94 m_settings.window_position_top = pSettingsNode->value_as_int("window_position_top", m_defaults.window_position_top);
95 m_settings.window_size_width = pSettingsNode->value_as_int("window_size_width", m_defaults.window_size_width);
96 m_settings.window_size_height = pSettingsNode->value_as_int("window_size_height", m_defaults.window_size_height);
102 bool vogleditor_settings::save(const char* settingsFile)
104 json_document settingsDoc;
105 json_node& metadata = settingsDoc.get_root()->add_object("metadata");
106 metadata.add_key_value("vogleditor_settings_file_format_version", to_hex_string(VOGLEDITOR_SETTINGS_FILE_FORMAT_VERSION));
109 json_node& settings = settingsDoc.get_root()->add_object("settings");
110 settings.add_key_value("trim_large_trace_prompt_size", m_settings.trim_large_trace_prompt_size);
112 settings.add_key_value("window_position_left", m_settings.window_position_left);
113 settings.add_key_value("window_position_top", m_settings.window_position_top);
114 settings.add_key_value("window_size_width", m_settings.window_size_width);
115 settings.add_key_value("window_size_height", m_settings.window_size_height);
117 dynamic_string path = get_settings_path(settingsFile);
118 bool bSavedSuccessfully = settingsDoc.serialize_to_file(path.c_str());
119 return bSavedSuccessfully;