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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3364 - (hide annotations) (download)
Tue Nov 14 18:07:25 2017 UTC (6 years, 5 months ago) by schoenebeck
File size: 12984 byte(s)
* Added experimental support for upcoming GTK(MM)4
  (for now up to GTKMM 3.91.2 while still preserving backward compatibility
  down to GTKMM 2).
* Re-merged r2845 to compile now with and without Gtk "Stock ID" API
  (see also r3158).

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

  ViewVC Help
Powered by ViewVC