/[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 2891 by schoenebeck, Tue Apr 26 17:42:26 2016 UTC revision 3409 by schoenebeck, Tue Jan 23 16:30:56 2018 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014-2016 Christian Schoenebeck      Copyright (c) 2014-2018 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    
 #include "Settings.h"  
 #include <glib.h>  
8  #include "global.h"  #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>  #include <glibmm/keyfile.h>
16  #include <iostream>  #include <iostream>
17  #include <stdio.h>  #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() {  static std::string configDir() {
28      //printf("configDir '%s'\n", g_get_user_config_dir());      //printf("configDir '%s'\n", g_get_user_config_dir());
# Line 27  static std::string configFile() { Line 39  static std::string configFile() {
39  }  }
40    
41  static std::string groupName(Settings::Group_t group) {  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";      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;  static Settings* _instance = NULL;
74            
75  Settings* Settings::singleton() {  Settings* Settings::singleton() {
# Line 44  Settings::Settings() : Glib::ObjectBase( Line 84  Settings::Settings() : Glib::ObjectBase(
84      warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),      warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),
85      syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),      syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),
86      moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true),      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)      m_ignoreNotifies(false)
136  {  {
137      m_boolProps.push_back(&warnUserOnExtensions);      m_boolProps.push_back(&warnUserOnExtensions);
138      m_boolProps.push_back(&syncSamplerInstrumentSelection);      m_boolProps.push_back(&syncSamplerInstrumentSelection);
139      m_boolProps.push_back(&moveRootNoteWithRegionMoved);      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) {  void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) {
# Line 85  void Settings::onPropertyChanged(Glib::P Line 221  void Settings::onPropertyChanged(Glib::P
221      }      }
222    
223      try {      try {
224    #if HAS_GLIB_KEYFILE_SAVE_TO_FILE
225          bool ok = file.save_to_file(configFile());          bool ok = file.save_to_file(configFile());
226    #else
227            bool ok = saveToFile(&file, configFile());
228    #endif
229          if (!ok) {          if (!ok) {
230              std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;              std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
231          } else {          } else {
# Line 113  void Settings::load() { Line 253  void Settings::load() {
253          Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);          Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);
254          try {          try {
255              const std::string group = groupName(prop->group());              const std::string group = groupName(prop->group());
256                if (!file.has_group(group)) continue;
257              if (!file.has_key(group, prop->get_name())) continue;              if (!file.has_key(group, prop->get_name())) continue;
258              const bool value = file.get_boolean(group, prop->get_name());              const bool value = file.get_boolean(group, prop->get_name());
259              prop->set_value(value);              prop->set_value(value);
# Line 125  void Settings::load() { Line 266  void Settings::load() {
266          Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);          Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);
267          try {          try {
268              const std::string group = groupName(prop->group());              const std::string group = groupName(prop->group());
269                if (!file.has_group(group)) continue;
270              if (!file.has_key(group, prop->get_name())) continue;              if (!file.has_key(group, prop->get_name())) continue;
271              const int value = file.get_integer(group, prop->get_name());              const int value = file.get_integer(group, prop->get_name());
272              prop->set_value(value);              prop->set_value(value);
# Line 136  void Settings::load() { Line 278  void Settings::load() {
278      m_ignoreNotifies = false;      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    }

Legend:
Removed from v.2891  
changed lines
  Added in v.3409

  ViewVC Help
Powered by ViewVC