/[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 2893 by schoenebeck, Fri Apr 29 14:19:53 2016 UTC revision 3711 by schoenebeck, Fri Jan 10 14:22:25 2020 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014-2016 Christian Schoenebeck      Copyright (c) 2014-2020 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>  #include <fstream>
19    #include <string.h>
20    
21  #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 40) || GTKMM_MAJOR_VERSION < 2  #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 40) || GTKMM_MAJOR_VERSION < 2
22  # define HAS_GLIB_KEYFILE_SAVE_TO_FILE 0  # define HAS_GLIB_KEYFILE_SAVE_TO_FILE 0
# Line 38  static std::string groupName(Settings::G Line 43  static std::string groupName(Settings::G
43          case Settings::GLOBAL: return "Global";          case Settings::GLOBAL: return "Global";
44          case Settings::MAIN_WINDOW: return "MainWindow";          case Settings::MAIN_WINDOW: return "MainWindow";
45          case Settings::SCRIPT_EDITOR: return "ScriptEditor";          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";      return "Global";
59  }  }
# Line 69  Settings::Settings() : Glib::ObjectBase( Line 85  Settings::Settings() : Glib::ObjectBase(
85      warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),      warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),
86      syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),      syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),
87      moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true),      moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true),
88        autoRestoreWindowDimension(*this, GLOBAL, "autoRestoreWindowDimension", false),
89        saveWithTemporaryFile(*this, GLOBAL, "saveWithTemporaryFile", false),
90        showTooltips(*this, GLOBAL, "showNewbieTooltips", true),
91        instrumentDoubleClickOpensProps(*this, GLOBAL, "openInstrPropsByDoubleClick", true),
92      mainWindowX(*this, MAIN_WINDOW, "x", -1),      mainWindowX(*this, MAIN_WINDOW, "x", -1),
93      mainWindowY(*this, MAIN_WINDOW, "y", -1),      mainWindowY(*this, MAIN_WINDOW, "y", -1),
94      mainWindowW(*this, MAIN_WINDOW, "w", -1),      mainWindowW(*this, MAIN_WINDOW, "w", -1),
# Line 77  Settings::Settings() : Glib::ObjectBase( Line 97  Settings::Settings() : Glib::ObjectBase(
97      scriptEditorWindowY(*this, SCRIPT_EDITOR, "y", -1),      scriptEditorWindowY(*this, SCRIPT_EDITOR, "y", -1),
98      scriptEditorWindowW(*this, SCRIPT_EDITOR, "w", -1),      scriptEditorWindowW(*this, SCRIPT_EDITOR, "w", -1),
99      scriptEditorWindowH(*this, SCRIPT_EDITOR, "h", -1),      scriptEditorWindowH(*this, SCRIPT_EDITOR, "h", -1),
100        scriptEditorFontSize(*this, SCRIPT_EDITOR, "fontSize", -1),
101        dimensionManagerWindowX(*this, DIMENSION_MANAGER, "x", -1),
102        dimensionManagerWindowY(*this, DIMENSION_MANAGER, "y", -1),
103        dimensionManagerWindowW(*this, DIMENSION_MANAGER, "w", -1),
104        dimensionManagerWindowH(*this, DIMENSION_MANAGER, "h", -1),
105        scriptSlotsWindowX(*this, SCRIPT_SLOTS, "x", -1),
106        scriptSlotsWindowY(*this, SCRIPT_SLOTS, "y", -1),
107        scriptSlotsWindowW(*this, SCRIPT_SLOTS, "w", -1),
108        scriptSlotsWindowH(*this, SCRIPT_SLOTS, "h", -1),
109        combineInstrumentsWindowX(*this, COMBINE_INSTRUMENTS, "x", -1),
110        combineInstrumentsWindowY(*this, COMBINE_INSTRUMENTS, "y", -1),
111        combineInstrumentsWindowW(*this, COMBINE_INSTRUMENTS, "w", -1),
112        combineInstrumentsWindowH(*this, COMBINE_INSTRUMENTS, "h", -1),
113        midiRulesWindowX(*this, MIDI_RULES, "x", -1),
114        midiRulesWindowY(*this, MIDI_RULES, "y", -1),
115        midiRulesWindowW(*this, MIDI_RULES, "w", -1),
116        midiRulesWindowH(*this, MIDI_RULES, "h", -1),
117        filePropsWindowX(*this, FILE_PROPS, "x", -1),
118        filePropsWindowY(*this, FILE_PROPS, "y", -1),
119        filePropsWindowW(*this, FILE_PROPS, "w", -1),
120        filePropsWindowH(*this, FILE_PROPS, "h", -1),
121        instrPropsWindowX(*this, INSTR_PROPS, "x", -1),
122        instrPropsWindowY(*this, INSTR_PROPS, "y", -1),
123        instrPropsWindowW(*this, INSTR_PROPS, "w", -1),
124        instrPropsWindowH(*this, INSTR_PROPS, "h", -1),
125        samplePropsWindowX(*this, SAMPLE_PROPS, "x", -1),
126        samplePropsWindowY(*this, SAMPLE_PROPS, "y", -1),
127        samplePropsWindowW(*this, SAMPLE_PROPS, "w", -1),
128        samplePropsWindowH(*this, SAMPLE_PROPS, "h", -1),
129        sampleRefsWindowX(*this, SAMPLE_REFS, "x", -1),
130        sampleRefsWindowY(*this, SAMPLE_REFS, "y", -1),
131        sampleRefsWindowW(*this, SAMPLE_REFS, "w", -1),
132        sampleRefsWindowH(*this, SAMPLE_REFS, "h", -1),
133        macroEditorWindowX(*this, MACRO_EDITOR, "x", -1),
134        macroEditorWindowY(*this, MACRO_EDITOR, "y", -1),
135        macroEditorWindowW(*this, MACRO_EDITOR, "w", -1),
136        macroEditorWindowH(*this, MACRO_EDITOR, "h", -1),
137        macrosSetupWindowX(*this, MACROS_SETUP, "x", -1),
138        macrosSetupWindowY(*this, MACROS_SETUP, "y", -1),
139        macrosSetupWindowW(*this, MACROS_SETUP, "w", -1),
140        macrosSetupWindowH(*this, MACROS_SETUP, "h", -1),
141      m_ignoreNotifies(false)      m_ignoreNotifies(false)
142  {  {
143      m_boolProps.push_back(&warnUserOnExtensions);      m_boolProps.push_back(&warnUserOnExtensions);
144      m_boolProps.push_back(&syncSamplerInstrumentSelection);      m_boolProps.push_back(&syncSamplerInstrumentSelection);
145      m_boolProps.push_back(&moveRootNoteWithRegionMoved);      m_boolProps.push_back(&moveRootNoteWithRegionMoved);
146        m_boolProps.push_back(&autoRestoreWindowDimension);
147        m_boolProps.push_back(&saveWithTemporaryFile);
148        m_boolProps.push_back(&showTooltips);
149        m_boolProps.push_back(&instrumentDoubleClickOpensProps);
150      m_intProps.push_back(&mainWindowX);      m_intProps.push_back(&mainWindowX);
151      m_intProps.push_back(&mainWindowY);      m_intProps.push_back(&mainWindowY);
152      m_intProps.push_back(&mainWindowW);      m_intProps.push_back(&mainWindowW);
# Line 90  Settings::Settings() : Glib::ObjectBase( Line 155  Settings::Settings() : Glib::ObjectBase(
155      m_intProps.push_back(&scriptEditorWindowY);      m_intProps.push_back(&scriptEditorWindowY);
156      m_intProps.push_back(&scriptEditorWindowW);      m_intProps.push_back(&scriptEditorWindowW);
157      m_intProps.push_back(&scriptEditorWindowH);      m_intProps.push_back(&scriptEditorWindowH);
158        m_intProps.push_back(&scriptEditorFontSize);
159        m_intProps.push_back(&dimensionManagerWindowX);
160        m_intProps.push_back(&dimensionManagerWindowY);
161        m_intProps.push_back(&dimensionManagerWindowW);
162        m_intProps.push_back(&dimensionManagerWindowH);
163        m_intProps.push_back(&scriptSlotsWindowX);
164        m_intProps.push_back(&scriptSlotsWindowY);
165        m_intProps.push_back(&scriptSlotsWindowW);
166        m_intProps.push_back(&scriptSlotsWindowH);
167        m_intProps.push_back(&combineInstrumentsWindowX);
168        m_intProps.push_back(&combineInstrumentsWindowY);
169        m_intProps.push_back(&combineInstrumentsWindowW);
170        m_intProps.push_back(&combineInstrumentsWindowH);
171        m_intProps.push_back(&midiRulesWindowX);
172        m_intProps.push_back(&midiRulesWindowY);
173        m_intProps.push_back(&midiRulesWindowW);
174        m_intProps.push_back(&midiRulesWindowH);
175        m_intProps.push_back(&filePropsWindowX);
176        m_intProps.push_back(&filePropsWindowY);
177        m_intProps.push_back(&filePropsWindowW);
178        m_intProps.push_back(&filePropsWindowH);
179        m_intProps.push_back(&instrPropsWindowX);
180        m_intProps.push_back(&instrPropsWindowY);
181        m_intProps.push_back(&instrPropsWindowW);
182        m_intProps.push_back(&instrPropsWindowH);
183        m_intProps.push_back(&samplePropsWindowX);
184        m_intProps.push_back(&samplePropsWindowY);
185        m_intProps.push_back(&samplePropsWindowW);
186        m_intProps.push_back(&samplePropsWindowH);
187        m_intProps.push_back(&sampleRefsWindowX);
188        m_intProps.push_back(&sampleRefsWindowY);
189        m_intProps.push_back(&sampleRefsWindowW);
190        m_intProps.push_back(&sampleRefsWindowH);
191        m_intProps.push_back(&macroEditorWindowX);
192        m_intProps.push_back(&macroEditorWindowY);
193        m_intProps.push_back(&macroEditorWindowW);
194        m_intProps.push_back(&macroEditorWindowH);
195        m_intProps.push_back(&macrosSetupWindowX);
196        m_intProps.push_back(&macrosSetupWindowY);
197        m_intProps.push_back(&macrosSetupWindowW);
198        m_intProps.push_back(&macrosSetupWindowH);
199  }  }
200    
201  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 158  void Settings::load() { Line 264  void Settings::load() {
264          Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);          Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);
265          try {          try {
266              const std::string group = groupName(prop->group());              const std::string group = groupName(prop->group());
267                if (!file.has_group(group)) continue;
268              if (!file.has_key(group, prop->get_name())) continue;              if (!file.has_key(group, prop->get_name())) continue;
269              const bool value = file.get_boolean(group, prop->get_name());              const bool value = file.get_boolean(group, prop->get_name());
270              prop->set_value(value);              prop->set_value(value);
# Line 170  void Settings::load() { Line 277  void Settings::load() {
277          Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);          Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);
278          try {          try {
279              const std::string group = groupName(prop->group());              const std::string group = groupName(prop->group());
280                if (!file.has_group(group)) continue;
281              if (!file.has_key(group, prop->get_name())) continue;              if (!file.has_key(group, prop->get_name())) continue;
282              const int value = file.get_integer(group, prop->get_name());              const int value = file.get_integer(group, prop->get_name());
283              prop->set_value(value);              prop->set_value(value);
# Line 181  void Settings::load() { Line 289  void Settings::load() {
289      m_ignoreNotifies = false;      m_ignoreNotifies = false;
290  }  }
291    
292    #define MACRO_LIST_NAME "srlzl"
293    
294    void Settings::loadMacros(std::vector<Serialization::Archive>& macros) {
295        const std::string group = groupName(MACROS);
296        macros.clear();
297        Glib::KeyFile file;
298        try {
299            bool ok = file.load_from_file(configFile());
300            if (!ok) return;
301        } catch (...) {
302            std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush;
303            return;
304        }
305        if (!file.has_group(group)) return;
306        if (!file.has_key(group, MACRO_LIST_NAME))
307            return;
308        std::vector<Glib::ustring> v = file.get_string_list(group, MACRO_LIST_NAME);
309        for (int i = 0; i < v.size(); ++i) {
310            Serialization::Archive macro;
311            macro.decode((const uint8_t*)v[i].c_str(), v[i].length());
312            macros.push_back(macro);
313        }
314    }
315    
316    void Settings::saveMacros(const std::vector<Serialization::Archive>& macros) {
317        const std::string group = groupName(MACROS);
318        Glib::KeyFile file;
319        try {
320            bool ok = file.load_from_file(configFile());
321            if (!ok) {
322                std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
323            }
324        } catch (...) {
325            std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
326            return;
327        }
328    
329        std::vector<Glib::ustring> v;
330        for (int i = 0; i < macros.size(); ++i) {
331            const Serialization::RawData& rawData = const_cast<Serialization::Archive&>(macros[i]).rawData();
332            std::string s((const char*)&rawData[0], rawData.size());
333            v.push_back(s);
334        }
335    
336        file.set_string_list(group, MACRO_LIST_NAME, v);
337    
338        try {
339    #if HAS_GLIB_KEYFILE_SAVE_TO_FILE
340            bool ok = file.save_to_file(configFile());
341    #else
342            bool ok = saveToFile(&file, configFile());
343    #endif
344            if (!ok) {
345                std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
346            } else {
347                //std::cout <<"gigedit CONFIG SAVED\n";
348            }
349        } catch (...) {
350            std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
351        }
352    
353    }

Legend:
Removed from v.2893  
changed lines
  Added in v.3711

  ViewVC Help
Powered by ViewVC