/[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 2956 - (show annotations) (download)
Sat Jul 16 15:31:47 2016 UTC (7 years, 8 months ago) by schoenebeck
File size: 9620 byte(s)
* Script Editor: Added "Font Size ..." to editor's menu.
* Bumped version (1.0.0.svn18).

1 /*
2 Copyright (c) 2014-2016 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 "Settings.h"
9 #include <glib.h>
10 #include "global.h"
11 #include <glibmm/keyfile.h>
12 #include <iostream>
13 #include <stdio.h>
14 #include <fstream>
15
16 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 40) || GTKMM_MAJOR_VERSION < 2
17 # define HAS_GLIB_KEYFILE_SAVE_TO_FILE 0
18 #else
19 # define HAS_GLIB_KEYFILE_SAVE_TO_FILE 1
20 #endif
21
22 static std::string configDir() {
23 //printf("configDir '%s'\n", g_get_user_config_dir());
24 return g_get_user_config_dir();
25 }
26
27 static std::string dirSep() {
28 //printf("sep '%s'\n", G_DIR_SEPARATOR_S);
29 return G_DIR_SEPARATOR_S;
30 }
31
32 static std::string configFile() {
33 return configDir() + dirSep() + "gigedit.conf";
34 }
35
36 static std::string groupName(Settings::Group_t group) {
37 switch (group) {
38 case Settings::GLOBAL: return "Global";
39 case Settings::MAIN_WINDOW: return "MainWindow";
40 case Settings::SCRIPT_EDITOR: return "ScriptEditor";
41 case Settings::DIMENSION_MANAGER: return "DimensionManager";
42 case Settings::SCRIPT_SLOTS: return "ScriptSlots";
43 case Settings::COMBINE_INSTRUMENTS: return "CombineInstruments";
44 case Settings::MIDI_RULES: return "MidiRules";
45 case Settings::FILE_PROPS: return "FileProps";
46 case Settings::INSTR_PROPS: return "InstrProps";
47 case Settings::SAMPLE_REFS: return "SampleRefs";
48 }
49 return "Global";
50 }
51
52 #if !HAS_GLIB_KEYFILE_SAVE_TO_FILE
53
54 static bool saveToFile(Glib::KeyFile* keyfile, std::string filename) {
55 Glib::ustring s = keyfile->to_data();
56 std::ofstream out;
57 out.open(filename.c_str(), std::ios_base::out | std::ios_base::trunc);
58 out << s;
59 out.close();
60 return true;
61 }
62
63 #endif // ! HAS_GLIB_KEYFILE_SAVE_TO_FILE
64
65 static Settings* _instance = NULL;
66
67 Settings* Settings::singleton() {
68 if (!_instance) {
69 _instance = new Settings;
70 _instance->load();
71 }
72 return _instance;
73 }
74
75 Settings::Settings() : Glib::ObjectBase(typeid(Settings)),
76 warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),
77 syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),
78 moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true),
79 autoRestoreWindowDimension(*this, GLOBAL, "autoRestoreWindowDimension", false),
80 mainWindowX(*this, MAIN_WINDOW, "x", -1),
81 mainWindowY(*this, MAIN_WINDOW, "y", -1),
82 mainWindowW(*this, MAIN_WINDOW, "w", -1),
83 mainWindowH(*this, MAIN_WINDOW, "h", -1),
84 scriptEditorWindowX(*this, SCRIPT_EDITOR, "x", -1),
85 scriptEditorWindowY(*this, SCRIPT_EDITOR, "y", -1),
86 scriptEditorWindowW(*this, SCRIPT_EDITOR, "w", -1),
87 scriptEditorWindowH(*this, SCRIPT_EDITOR, "h", -1),
88 scriptEditorFontSize(*this, SCRIPT_EDITOR, "fontSize", -1),
89 dimensionManagerWindowX(*this, DIMENSION_MANAGER, "x", -1),
90 dimensionManagerWindowY(*this, DIMENSION_MANAGER, "y", -1),
91 dimensionManagerWindowW(*this, DIMENSION_MANAGER, "w", -1),
92 dimensionManagerWindowH(*this, DIMENSION_MANAGER, "h", -1),
93 scriptSlotsWindowX(*this, SCRIPT_SLOTS, "x", -1),
94 scriptSlotsWindowY(*this, SCRIPT_SLOTS, "y", -1),
95 scriptSlotsWindowW(*this, SCRIPT_SLOTS, "w", -1),
96 scriptSlotsWindowH(*this, SCRIPT_SLOTS, "h", -1),
97 combineInstrumentsWindowX(*this, COMBINE_INSTRUMENTS, "x", -1),
98 combineInstrumentsWindowY(*this, COMBINE_INSTRUMENTS, "y", -1),
99 combineInstrumentsWindowW(*this, COMBINE_INSTRUMENTS, "w", -1),
100 combineInstrumentsWindowH(*this, COMBINE_INSTRUMENTS, "h", -1),
101 midiRulesWindowX(*this, MIDI_RULES, "x", -1),
102 midiRulesWindowY(*this, MIDI_RULES, "y", -1),
103 midiRulesWindowW(*this, MIDI_RULES, "w", -1),
104 midiRulesWindowH(*this, MIDI_RULES, "h", -1),
105 filePropsWindowX(*this, FILE_PROPS, "x", -1),
106 filePropsWindowY(*this, FILE_PROPS, "y", -1),
107 filePropsWindowW(*this, FILE_PROPS, "w", -1),
108 filePropsWindowH(*this, FILE_PROPS, "h", -1),
109 instrPropsWindowX(*this, INSTR_PROPS, "x", -1),
110 instrPropsWindowY(*this, INSTR_PROPS, "y", -1),
111 instrPropsWindowW(*this, INSTR_PROPS, "w", -1),
112 instrPropsWindowH(*this, INSTR_PROPS, "h", -1),
113 sampleRefsWindowX(*this, SAMPLE_REFS, "x", -1),
114 sampleRefsWindowY(*this, SAMPLE_REFS, "y", -1),
115 sampleRefsWindowW(*this, SAMPLE_REFS, "w", -1),
116 sampleRefsWindowH(*this, SAMPLE_REFS, "h", -1),
117 m_ignoreNotifies(false)
118 {
119 m_boolProps.push_back(&warnUserOnExtensions);
120 m_boolProps.push_back(&syncSamplerInstrumentSelection);
121 m_boolProps.push_back(&moveRootNoteWithRegionMoved);
122 m_boolProps.push_back(&autoRestoreWindowDimension);
123 m_intProps.push_back(&mainWindowX);
124 m_intProps.push_back(&mainWindowY);
125 m_intProps.push_back(&mainWindowW);
126 m_intProps.push_back(&mainWindowH);
127 m_intProps.push_back(&scriptEditorWindowX);
128 m_intProps.push_back(&scriptEditorWindowY);
129 m_intProps.push_back(&scriptEditorWindowW);
130 m_intProps.push_back(&scriptEditorWindowH);
131 m_intProps.push_back(&scriptEditorFontSize);
132 m_intProps.push_back(&dimensionManagerWindowX);
133 m_intProps.push_back(&dimensionManagerWindowY);
134 m_intProps.push_back(&dimensionManagerWindowW);
135 m_intProps.push_back(&dimensionManagerWindowH);
136 m_intProps.push_back(&scriptSlotsWindowX);
137 m_intProps.push_back(&scriptSlotsWindowY);
138 m_intProps.push_back(&scriptSlotsWindowW);
139 m_intProps.push_back(&scriptSlotsWindowH);
140 m_intProps.push_back(&combineInstrumentsWindowX);
141 m_intProps.push_back(&combineInstrumentsWindowY);
142 m_intProps.push_back(&combineInstrumentsWindowW);
143 m_intProps.push_back(&combineInstrumentsWindowH);
144 m_intProps.push_back(&midiRulesWindowX);
145 m_intProps.push_back(&midiRulesWindowY);
146 m_intProps.push_back(&midiRulesWindowW);
147 m_intProps.push_back(&midiRulesWindowH);
148 m_intProps.push_back(&filePropsWindowX);
149 m_intProps.push_back(&filePropsWindowY);
150 m_intProps.push_back(&filePropsWindowW);
151 m_intProps.push_back(&filePropsWindowH);
152 m_intProps.push_back(&instrPropsWindowX);
153 m_intProps.push_back(&instrPropsWindowY);
154 m_intProps.push_back(&instrPropsWindowW);
155 m_intProps.push_back(&instrPropsWindowH);
156 m_intProps.push_back(&sampleRefsWindowX);
157 m_intProps.push_back(&sampleRefsWindowY);
158 m_intProps.push_back(&sampleRefsWindowW);
159 m_intProps.push_back(&sampleRefsWindowH);
160 }
161
162 void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) {
163 if (m_ignoreNotifies) return;
164
165 //printf("Settings::onPropertyChanged(%s)\n", pProperty->get_name().c_str());
166
167 Glib::KeyFile file;
168 try {
169 bool ok = file.load_from_file(configFile());
170 if (!ok) {
171 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
172 }
173 } catch (...) {
174 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
175 }
176
177 switch (type) {
178 case BOOLEAN: {
179 Property<bool>* prop = static_cast<Property<bool>*>(pProperty);
180 //std::cout << "Saving bool setting '" << prop->get_name() << "'\n" << std::flush;
181 file.set_boolean(groupName(prop->group()), prop->get_name(), prop->get_value());
182 break;
183 }
184 case INTEGER: {
185 Property<int>* prop = static_cast<Property<int>*>(pProperty);
186 //std::cout << "Saving int setting '" << prop->get_name() << "'\n" << std::flush;
187 file.set_integer(groupName(prop->group()), prop->get_name(), prop->get_value());
188 break;
189 }
190 case UNKNOWN:
191 std::cerr << "BUG: Unknown setting raw type of property '" << pProperty->get_name() << "'\n" << std::flush;
192 return;
193 }
194
195 try {
196 #if HAS_GLIB_KEYFILE_SAVE_TO_FILE
197 bool ok = file.save_to_file(configFile());
198 #else
199 bool ok = saveToFile(&file, configFile());
200 #endif
201 if (!ok) {
202 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
203 } else {
204 //std::cout <<"gigedit CONFIG SAVED\n";
205 }
206 } catch (...) {
207 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
208 }
209 }
210
211 void Settings::load() {
212 Glib::KeyFile file;
213 try {
214 bool ok = file.load_from_file(configFile());
215 if (!ok) return;
216 } catch (...) {
217 std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush;
218 return;
219 }
220
221 // ignore onPropertyChanged() calls during updating the property values below
222 m_ignoreNotifies = true;
223
224 for (int i = 0; i < m_boolProps.size(); ++i) {
225 Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);
226 try {
227 const std::string group = groupName(prop->group());
228 if (!file.has_key(group, prop->get_name())) continue;
229 const bool value = file.get_boolean(group, prop->get_name());
230 prop->set_value(value);
231 } catch (...) {
232 continue;
233 }
234 }
235
236 for (int i = 0; i < m_intProps.size(); ++i) {
237 Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);
238 try {
239 const std::string group = groupName(prop->group());
240 if (!file.has_key(group, prop->get_name())) continue;
241 const int value = file.get_integer(group, prop->get_name());
242 prop->set_value(value);
243 } catch (...) {
244 continue;
245 }
246 }
247
248 m_ignoreNotifies = false;
249 }
250

  ViewVC Help
Powered by ViewVC