Audacity 3.2.0
SettingsWX.cpp
Go to the documentation of this file.
1#include "SettingsWX.h"
2
3#include <wx/confbase.h>
4#include <wx/fileconf.h>
5
6
7void SettingsWX::DoBeginGroup(const wxString& prefix)
8{
9 if(prefix.StartsWith("/"))
10 mGroupStack.push_back(prefix);
11 else
12 {
13 if(mGroupStack.size() > 1)
14 mGroupStack.push_back(mGroupStack.Last() + "/" + prefix);
15 else
16 mGroupStack.push_back("/" + prefix);
17 }
18 //This creates group if it didn't exist
19 mConfig->SetPath(mGroupStack.Last());
20}
21
23{
24 assert(mGroupStack.size() > 1);// "No matching DoBeginGroup"
25
26 if(mGroupStack.size() > 1)
27 mGroupStack.pop_back();
28
29 mConfig->SetPath(mGroupStack.Last());
30}
31
32SettingsWX::SettingsWX(std::shared_ptr<wxConfigBase> config)
33 : mConfig{std::move(config)}
34{
35 mGroupStack.push_back("/");
36}
37
38SettingsWX::SettingsWX(const wxString& filepath)
39{
40 mConfig = std::make_shared<wxFileConfig>(wxEmptyString, wxEmptyString, filepath);
41 mGroupStack.push_back("/");
42}
43
45{
46 mConfig->Flush();
47}
48
49wxString SettingsWX::GetGroup() const
50{
51 assert(!mGroupStack.empty());
52 if(mGroupStack.size() > 1)
53 {
54 const auto& path = mGroupStack.Last();
55 return path.Right(path.Length() - 1);
56 }
57 return {};
58}
59
60wxArrayString SettingsWX::GetChildGroups() const
61{
62 long index;
63 wxString group;
64
65 if(mConfig->GetFirstGroup(group, index))
66 {
67 wxArrayString groups;
68 groups.push_back(group);
69 while(mConfig->GetNextGroup(group, index))
70 groups.push_back(group);
71 return groups;
72 }
73 return {};
74}
75
76wxArrayString SettingsWX::GetChildKeys() const
77{
78 long index;
79 wxString key;
80 if(mConfig->GetFirstEntry(key, index))
81 {
82 wxArrayString keys;
83 keys.push_back(key);
84 while(mConfig->GetNextEntry(key, index))
85 keys.push_back(key);
86 return keys;
87 }
88 return {};
89}
90
91bool SettingsWX::HasEntry(const wxString& key) const
92{
93 return mConfig->HasEntry(MakePath(key));
94}
95
96bool SettingsWX::HasGroup(const wxString& key) const
97{
98 return mConfig->HasGroup(MakePath(key));
99}
100
101bool SettingsWX::Remove(const wxString& key)
102{
103 if(key.empty())
104 {
105 for(auto& group : GetChildGroups())
106 mConfig->DeleteGroup(group);
107 for(auto& entry : GetChildKeys())
108 mConfig->DeleteEntry(entry, false);
109 return true;
110 }
111 const auto path = MakePath(key);
112 if(mConfig->HasEntry(path))
113 return mConfig->DeleteEntry(path, false);
114 if(mConfig->HasGroup(path))
115 return mConfig->DeleteGroup(path);
116 return false;
117}
118
120{
121 mConfig->DeleteAll();
122}
123
124bool SettingsWX::Read(const wxString& key, bool* value) const
125{
126 return mConfig->Read(MakePath(key), value);
127}
128
129bool SettingsWX::Read(const wxString& key, int* value) const
130{
131 return mConfig->Read(MakePath(key), value);
132}
133
134bool SettingsWX::Read(const wxString& key, long* value) const
135{
136 return mConfig->Read(MakePath(key), value);
137}
138
139bool SettingsWX::Read(const wxString& key, long long* value) const
140{
141 wxString str;
142 if(mConfig->Read(MakePath(key), &str))
143 {
144 if(str.ToLongLong(value))
145 return true;
146 }
147 return false;
148}
149
150bool SettingsWX::Read(const wxString& key, double* value) const
151{
152 return mConfig->Read(MakePath(key), value);
153}
154
155bool SettingsWX::Read(const wxString& key, wxString* value) const
156{
157 return mConfig->Read(MakePath(key), value);
158}
159
160bool SettingsWX::Write(const wxString& key, bool value)
161{
162 return mConfig->Write(MakePath(key), value);
163}
164
165bool SettingsWX::Write(const wxString& key, int value)
166{
167 return mConfig->Write(MakePath(key), value);
168}
169
170bool SettingsWX::Write(const wxString& key, long value)
171{
172 return mConfig->Write(MakePath(key), value);
173}
174
175bool SettingsWX::Write(const wxString& key, long long value)
176{
177 return mConfig->Write(MakePath(key), wxString::Format("%lld", value));
178}
179
180bool SettingsWX::Write(const wxString& key, double value)
181{
182 return mConfig->Write(MakePath(key), value);
183}
184
185bool SettingsWX::Write(const wxString& key, const wxString& value)
186{
187 return mConfig->Write(MakePath(key), value);
188}
189
190bool SettingsWX::Flush() noexcept
191{
192 try
193 {
194 return mConfig->Flush();
195 }
196 catch(...)
197 {
198 //TODO: log error
199 }
200 return false;
201}
202
203wxString SettingsWX::MakePath(const wxString& key) const
204{
205 if(key.StartsWith("/"))
206 return key;
207 if(mGroupStack.size() > 1)
208 return mGroupStack.Last() + "/" + key;
209 return "/" + key;
210}
#define str(a)
static ProjectFileIORegistry::AttributeWriterEntry entry
static const AudacityProject::AttachedObjects::RegisteredFactory key
wxString MakePath(const wxString &key) const
Definition: SettingsWX.cpp:203
bool Read(const wxString &key, bool *value) const override
Definition: SettingsWX.cpp:124
bool Flush() noexcept override
Definition: SettingsWX.cpp:190
std::shared_ptr< wxConfigBase > mConfig
Definition: SettingsWX.h:14
wxArrayString GetChildGroups() const override
Returns all child groups within the current group.
Definition: SettingsWX.cpp:60
wxString GetGroup() const override
Returns current group prefix.
Definition: SettingsWX.cpp:49
bool HasEntry(const wxString &key) const override
Checks whether specified key exists within the current group.
Definition: SettingsWX.cpp:91
void DoBeginGroup(const wxString &prefix) override
Definition: SettingsWX.cpp:7
wxArrayString GetChildKeys() const override
Returns all child keys within the current group.
Definition: SettingsWX.cpp:76
wxArrayString mGroupStack
Definition: SettingsWX.h:13
bool Remove(const wxString &key) override
Removes group or entry within the current group, if exists. Pass empty string to remove each entry in...
Definition: SettingsWX.cpp:101
~SettingsWX() override
Definition: SettingsWX.cpp:44
void Clear() override
Remove all groups and keys.
Definition: SettingsWX.cpp:119
bool HasGroup(const wxString &key) const override
Checks whether specified group exists relative to the current group.
Definition: SettingsWX.cpp:96
SettingsWX(std::shared_ptr< wxConfigBase > config)
Definition: SettingsWX.cpp:32
void DoEndGroup() noexcept override
Definition: SettingsWX.cpp:22
bool Write(const wxString &key, bool value) override
Definition: SettingsWX.cpp:160
STL namespace.