/[svn]/gigedit/trunk/src/gigedit/Settings.cpp
ViewVC logotype

Contents of /gigedit/trunk/src/gigedit/Settings.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3409 - (show annotations) (download)
Tue Jan 23 16:30:56 2018 UTC (6 years, 3 months ago) by schoenebeck
File size: 13087 byte(s)
* Added new main menu item "View" -> "Tooltips for Beginners" which allows
  to disable tooltips intended for newbies only (default: on).
* Bumped version (1.1.0.svn3).

1 /*
2 Copyright (c) 2014-2018 Christian Schoenebeck
3
4 This file is part of "gigedit" and released under the terms of the
5 GNU General Public License version 2.
6 */
7
8 #include "global.h"
9 #include "Settings.h"
10 #ifdef GLIB_HEADER_FILE
11 # include GLIB_HEADER_FILE(glib.h)
12 #else
13 # include <glib.h>
14 #endif
15 #include <glibmm/keyfile.h>
16 #include <iostream>
17 #include <stdio.h>
18 #include <fstream>
19 #include <string.h>
20
21 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 40) || GTKMM_MAJOR_VERSION < 2
22 # define HAS_GLIB_KEYFILE_SAVE_TO_FILE 0
23 #else
24 # define HAS_GLIB_KEYFILE_SAVE_TO_FILE 1
25 #endif
26
27 static std::string configDir() {
28 //printf("configDir '%s'\n", g_get_user_config_dir());
29 return g_get_user_config_dir();
30 }
31
32 static std::string dirSep() {
33 //printf("sep '%s'\n", G_DIR_SEPARATOR_S);
34 return G_DIR_SEPARATOR_S;
35 }
36
37 static std::string configFile() {
38 return configDir() + dirSep() + "gigedit.conf";
39 }
40
41 static std::string groupName(Settings::Group_t group) {
42 switch (group) {
43 case Settings::GLOBAL: return "Global";
44 case Settings::MAIN_WINDOW: return "MainWindow";
45 case Settings::SCRIPT_EDITOR: return "ScriptEditor";
46 case Settings::DIMENSION_MANAGER: return "DimensionManager";
47 case Settings::SCRIPT_SLOTS: return "ScriptSlots";
48 case Settings::COMBINE_INSTRUMENTS: return "CombineInstruments";
49 case Settings::MIDI_RULES: return "MidiRules";
50 case Settings::FILE_PROPS: return "FileProps";
51 case Settings::INSTR_PROPS: return "InstrProps";
52 case Settings::SAMPLE_REFS: return "SampleRefs";
53 case Settings::MACRO_EDITOR: return "MacroEditor";
54 case Settings::MACROS_SETUP: return "MacrosSetup";
55 case Settings::MACROS: return "Macros";
56 }
57 return "Global";
58 }
59
60 #if !HAS_GLIB_KEYFILE_SAVE_TO_FILE
61
62 static bool saveToFile(Glib::KeyFile* keyfile, std::string filename) {
63 Glib::ustring s = keyfile->to_data();
64 std::ofstream out;
65 out.open(filename.c_str(), std::ios_base::out | std::ios_base::trunc);
66 out << s;
67 out.close();
68 return true;
69 }
70
71 #endif // ! HAS_GLIB_KEYFILE_SAVE_TO_FILE
72
73 static Settings* _instance = NULL;
74
75 Settings* Settings::singleton() {
76 if (!_instance) {
77 _instance = new Settings;
78 _instance->load();
79 }
80 return _instance;
81 }
82
83 Settings::Settings() : Glib::ObjectBase(typeid(Settings)),
84 warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),
85 syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),
86 moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true),
87 autoRestoreWindowDimension(*this, GLOBAL, "autoRestoreWindowDimension", false),
88 saveWithTemporaryFile(*this, GLOBAL, "saveWithTemporaryFile", false),
89 showTooltips(*this, GLOBAL, "showNewbieTooltips", true),
90 mainWindowX(*this, MAIN_WINDOW, "x", -1),
91 mainWindowY(*this, MAIN_WINDOW, "y", -1),
92 mainWindowW(*this, MAIN_WINDOW, "w", -1),
93 mainWindowH(*this, MAIN_WINDOW, "h", -1),
94 scriptEditorWindowX(*this, SCRIPT_EDITOR, "x", -1),
95 scriptEditorWindowY(*this, SCRIPT_EDITOR, "y", -1),
96 scriptEditorWindowW(*this, SCRIPT_EDITOR, "w", -1),
97 scriptEditorWindowH(*this, SCRIPT_EDITOR, "h", -1),
98 scriptEditorFontSize(*this, SCRIPT_EDITOR, "fontSize", -1),
99 dimensionManagerWindowX(*this, DIMENSION_MANAGER, "x", -1),
100 dimensionManagerWindowY(*this, DIMENSION_MANAGER, "y", -1),
101 dimensionManagerWindowW(*this, DIMENSION_MANAGER, "w", -1),
102 dimensionManagerWindowH(*this, DIMENSION_MANAGER, "h", -1),
103 scriptSlotsWindowX(*this, SCRIPT_SLOTS, "x", -1),
104 scriptSlotsWindowY(*this, SCRIPT_SLOTS, "y", -1),
105 scriptSlotsWindowW(*this, SCRIPT_SLOTS, "w", -1),
106 scriptSlotsWindowH(*this, SCRIPT_SLOTS, "h", -1),
107 combineInstrumentsWindowX(*this, COMBINE_INSTRUMENTS, "x", -1),
108 combineInstrumentsWindowY(*this, COMBINE_INSTRUMENTS, "y", -1),
109 combineInstrumentsWindowW(*this, COMBINE_INSTRUMENTS, "w", -1),
110 combineInstrumentsWindowH(*this, COMBINE_INSTRUMENTS, "h", -1),
111 midiRulesWindowX(*this, MIDI_RULES, "x", -1),
112 midiRulesWindowY(*this, MIDI_RULES, "y", -1),
113 midiRulesWindowW(*this, MIDI_RULES, "w", -1),
114 midiRulesWindowH(*this, MIDI_RULES, "h", -1),
115 filePropsWindowX(*this, FILE_PROPS, "x", -1),
116 filePropsWindowY(*this, FILE_PROPS, "y", -1),
117 filePropsWindowW(*this, FILE_PROPS, "w", -1),
118 filePropsWindowH(*this, FILE_PROPS, "h", -1),
119 instrPropsWindowX(*this, INSTR_PROPS, "x", -1),
120 instrPropsWindowY(*this, INSTR_PROPS, "y", -1),
121 instrPropsWindowW(*this, INSTR_PROPS, "w", -1),
122 instrPropsWindowH(*this, INSTR_PROPS, "h", -1),
123 sampleRefsWindowX(*this, SAMPLE_REFS, "x", -1),
124 sampleRefsWindowY(*this, SAMPLE_REFS, "y", -1),
125 sampleRefsWindowW(*this, SAMPLE_REFS, "w", -1),
126 sampleRefsWindowH(*this, SAMPLE_REFS, "h", -1),
127 macroEditorWindowX(*this, MACRO_EDITOR, "x", -1),
128 macroEditorWindowY(*this, MACRO_EDITOR, "y", -1),
129 macroEditorWindowW(*this, MACRO_EDITOR, "w", -1),
130 macroEditorWindowH(*this, MACRO_EDITOR, "h", -1),
131 macrosSetupWindowX(*this, MACROS_SETUP, "x", -1),
132 macrosSetupWindowY(*this, MACROS_SETUP, "y", -1),
133 macrosSetupWindowW(*this, MACROS_SETUP, "w", -1),
134 macrosSetupWindowH(*this, MACROS_SETUP, "h", -1),
135 m_ignoreNotifies(false)
136 {
137 m_boolProps.push_back(&warnUserOnExtensions);
138 m_boolProps.push_back(&syncSamplerInstrumentSelection);
139 m_boolProps.push_back(&moveRootNoteWithRegionMoved);
140 m_boolProps.push_back(&autoRestoreWindowDimension);
141 m_boolProps.push_back(&saveWithTemporaryFile);
142 m_boolProps.push_back(&showTooltips);
143 m_intProps.push_back(&mainWindowX);
144 m_intProps.push_back(&mainWindowY);
145 m_intProps.push_back(&mainWindowW);
146 m_intProps.push_back(&mainWindowH);
147 m_intProps.push_back(&scriptEditorWindowX);
148 m_intProps.push_back(&scriptEditorWindowY);
149 m_intProps.push_back(&scriptEditorWindowW);
150 m_intProps.push_back(&scriptEditorWindowH);
151 m_intProps.push_back(&scriptEditorFontSize);
152 m_intProps.push_back(&dimensionManagerWindowX);
153 m_intProps.push_back(&dimensionManagerWindowY);
154 m_intProps.push_back(&dimensionManagerWindowW);
155 m_intProps.push_back(&dimensionManagerWindowH);
156 m_intProps.push_back(&scriptSlotsWindowX);
157 m_intProps.push_back(&scriptSlotsWindowY);
158 m_intProps.push_back(&scriptSlotsWindowW);
159 m_intProps.push_back(&scriptSlotsWindowH);
160 m_intProps.push_back(&combineInstrumentsWindowX);
161 m_intProps.push_back(&combineInstrumentsWindowY);
162 m_intProps.push_back(&combineInstrumentsWindowW);
163 m_intProps.push_back(&combineInstrumentsWindowH);
164 m_intProps.push_back(&midiRulesWindowX);
165 m_intProps.push_back(&midiRulesWindowY);
166 m_intProps.push_back(&midiRulesWindowW);
167 m_intProps.push_back(&midiRulesWindowH);
168 m_intProps.push_back(&filePropsWindowX);
169 m_intProps.push_back(&filePropsWindowY);
170 m_intProps.push_back(&filePropsWindowW);
171 m_intProps.push_back(&filePropsWindowH);
172 m_intProps.push_back(&instrPropsWindowX);
173 m_intProps.push_back(&instrPropsWindowY);
174 m_intProps.push_back(&instrPropsWindowW);
175 m_intProps.push_back(&instrPropsWindowH);
176 m_intProps.push_back(&sampleRefsWindowX);
177 m_intProps.push_back(&sampleRefsWindowY);
178 m_intProps.push_back(&sampleRefsWindowW);
179 m_intProps.push_back(&sampleRefsWindowH);
180 m_intProps.push_back(&macroEditorWindowX);
181 m_intProps.push_back(&macroEditorWindowY);
182 m_intProps.push_back(&macroEditorWindowW);
183 m_intProps.push_back(&macroEditorWindowH);
184 m_intProps.push_back(&macrosSetupWindowX);
185 m_intProps.push_back(&macrosSetupWindowY);
186 m_intProps.push_back(&macrosSetupWindowW);
187 m_intProps.push_back(&macrosSetupWindowH);
188 }
189
190 void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) {
191 if (m_ignoreNotifies) return;
192
193 //printf("Settings::onPropertyChanged(%s)\n", pProperty->get_name().c_str());
194
195 Glib::KeyFile file;
196 try {
197 bool ok = file.load_from_file(configFile());
198 if (!ok) {
199 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
200 }
201 } catch (...) {
202 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
203 }
204
205 switch (type) {
206 case BOOLEAN: {
207 Property<bool>* prop = static_cast<Property<bool>*>(pProperty);
208 //std::cout << "Saving bool setting '" << prop->get_name() << "'\n" << std::flush;
209 file.set_boolean(groupName(prop->group()), prop->get_name(), prop->get_value());
210 break;
211 }
212 case INTEGER: {
213 Property<int>* prop = static_cast<Property<int>*>(pProperty);
214 //std::cout << "Saving int setting '" << prop->get_name() << "'\n" << std::flush;
215 file.set_integer(groupName(prop->group()), prop->get_name(), prop->get_value());
216 break;
217 }
218 case UNKNOWN:
219 std::cerr << "BUG: Unknown setting raw type of property '" << pProperty->get_name() << "'\n" << std::flush;
220 return;
221 }
222
223 try {
224 #if HAS_GLIB_KEYFILE_SAVE_TO_FILE
225 bool ok = file.save_to_file(configFile());
226 #else
227 bool ok = saveToFile(&file, configFile());
228 #endif
229 if (!ok) {
230 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
231 } else {
232 //std::cout <<"gigedit CONFIG SAVED\n";
233 }
234 } catch (...) {
235 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
236 }
237 }
238
239 void Settings::load() {
240 Glib::KeyFile file;
241 try {
242 bool ok = file.load_from_file(configFile());
243 if (!ok) return;
244 } catch (...) {
245 std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush;
246 return;
247 }
248
249 // ignore onPropertyChanged() calls during updating the property values below
250 m_ignoreNotifies = true;
251
252 for (int i = 0; i < m_boolProps.size(); ++i) {
253 Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);
254 try {
255 const std::string group = groupName(prop->group());
256 if (!file.has_group(group)) continue;
257 if (!file.has_key(group, prop->get_name())) continue;
258 const bool value = file.get_boolean(group, prop->get_name());
259 prop->set_value(value);
260 } catch (...) {
261 continue;
262 }
263 }
264
265 for (int i = 0; i < m_intProps.size(); ++i) {
266 Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);
267 try {
268 const std::string group = groupName(prop->group());
269 if (!file.has_group(group)) continue;
270 if (!file.has_key(group, prop->get_name())) continue;
271 const int value = file.get_integer(group, prop->get_name());
272 prop->set_value(value);
273 } catch (...) {
274 continue;
275 }
276 }
277
278 m_ignoreNotifies = false;
279 }
280
281 #define MACRO_LIST_NAME "srlzl"
282
283 void Settings::loadMacros(std::vector<Serialization::Archive>& macros) {
284 const std::string group = groupName(MACROS);
285 macros.clear();
286 Glib::KeyFile file;
287 try {
288 bool ok = file.load_from_file(configFile());
289 if (!ok) return;
290 } catch (...) {
291 std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush;
292 return;
293 }
294 if (!file.has_group(group)) return;
295 if (!file.has_key(group, MACRO_LIST_NAME))
296 return;
297 std::vector<Glib::ustring> v = file.get_string_list(group, MACRO_LIST_NAME);
298 for (int i = 0; i < v.size(); ++i) {
299 Serialization::Archive macro;
300 macro.decode((const uint8_t*)v[i].c_str(), v[i].length());
301 macros.push_back(macro);
302 }
303 }
304
305 void Settings::saveMacros(const std::vector<Serialization::Archive>& macros) {
306 const std::string group = groupName(MACROS);
307 Glib::KeyFile file;
308 try {
309 bool ok = file.load_from_file(configFile());
310 if (!ok) {
311 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
312 }
313 } catch (...) {
314 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
315 return;
316 }
317
318 std::vector<Glib::ustring> v;
319 for (int i = 0; i < macros.size(); ++i) {
320 const Serialization::RawData& rawData = const_cast<Serialization::Archive&>(macros[i]).rawData();
321 std::string s((const char*)&rawData[0], rawData.size());
322 v.push_back(s);
323 }
324
325 file.set_string_list(group, MACRO_LIST_NAME, v);
326
327 try {
328 #if HAS_GLIB_KEYFILE_SAVE_TO_FILE
329 bool ok = file.save_to_file(configFile());
330 #else
331 bool ok = saveToFile(&file, configFile());
332 #endif
333 if (!ok) {
334 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
335 } else {
336 //std::cout <<"gigedit CONFIG SAVED\n";
337 }
338 } catch (...) {
339 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
340 }
341
342 }

  ViewVC Help
Powered by ViewVC