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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.2541  
changed lines
  Added in v.3637

  ViewVC Help
Powered by ViewVC