/[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 2894 - (hide annotations) (download)
Sat Apr 30 14:42:14 2016 UTC (7 years, 11 months ago) by schoenebeck
File size: 9367 byte(s)
* Enabled auto save & restore of window size & position of all
  remaining windows.
* Bumped version (1.0.0.svn7).

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

  ViewVC Help
Powered by ViewVC