/[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 3151 - (hide annotations) (download)
Fri May 5 18:44:59 2017 UTC (6 years, 11 months ago) by schoenebeck
File size: 10149 byte(s)
* WIP: Added initial draft implementation of macro editor
  (accessible for copied clipboard content via Alt+x).
* Bumped version (1.0.0.svn35).

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

  ViewVC Help
Powered by ViewVC