]> git.cworth.org Git - vogl/blob - src/vogleditor/vogleditor_settings.cpp
UI: Add a settings file
[vogl] / src / vogleditor / vogleditor_settings.cpp
1 #include "vogleditor_settings.h"
2 #include "vogl_common.h"
3 #include "vogl_file_utils.h"
4
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;
7
8 vogleditor_settings::vogleditor_settings()
9     : m_file_format_version(VOGLEDITOR_SETTINGS_FILE_FORMAT_VERSION_1)
10 {
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;
16
17     m_settings = m_defaults;
18 }
19
20 dynamic_string vogleditor_settings::get_settings_path(const char* settingsFilename)
21 {
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)
26     {
27         settingsPath = xdgConfigHome;
28         settingsPath += "/vogleditor/";
29         if (vogl::file_utils::does_dir_exist(settingsPath.c_str()) == false)
30         {
31             if (vogl::file_utils::create_directories_from_full_path(settingsPath) == false)
32             {
33                 VOGL_ASSERT(!"Failed to create directories for settings file.");
34             }
35         }
36     }
37     else if (home != NULL && strlen(home) != 0)
38     {
39         settingsPath += home;
40         settingsPath += "/.config/vogleditor/";
41         if (vogl::file_utils::does_dir_exist(settingsPath.c_str())== false)
42         {
43             if (vogl::file_utils::create_directories_from_full_path(settingsPath) == false)
44             {
45                 VOGL_ASSERT(!"Failed to create directories for settings file.");
46             }
47         }
48     }
49     else
50     {
51         // the settings file will end up in the current working directory
52     }
53
54     settingsPath += settingsFilename;
55     return settingsPath;
56 }
57
58 bool vogleditor_settings::load(const char* settingsFile)
59 {
60     bool bLoaded = false;
61     json_document settingsDoc;
62     dynamic_string path = get_settings_path(settingsFile);
63     if (settingsDoc.deserialize_file(path.c_str()))
64     {
65         // validate metadata
66         json_node* pMetadata = settingsDoc.get_root()->find_child_object("metadata");
67         if (pMetadata == NULL)
68         {
69             return false;
70         }
71
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)
74         {
75             return false;
76         }
77
78         m_file_format_version = rFormatVersion.as_uint32(VOGLEDITOR_SETTINGS_FILE_FORMAT_VERSION);
79
80         // validate that settings node exists
81         json_node* pSettingsNode = settingsDoc.get_root()->find_child_object("settings");
82         if (pSettingsNode == NULL)
83         {
84             return false;
85         }
86
87         // if so, consider the file successfully loaded
88         bLoaded = true;
89
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);
92
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);
97     }
98
99     return bLoaded;
100 }
101
102 bool vogleditor_settings::save(const char* settingsFile)
103 {
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));
107
108     // settings
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);
111
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);
116
117     dynamic_string path = get_settings_path(settingsFile);
118     bool bSavedSuccessfully = settingsDoc.serialize_to_file(path.c_str());
119     return bSavedSuccessfully;
120 }