/[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 2893 by schoenebeck, Fri Apr 29 14:19:53 2016 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014 Christian Schoenebeck      Copyright (c) 2014-2016 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 "Settings.h"  #include "Settings.h"
9    #include <glib.h>
10    #include "global.h"
11    #include <glibmm/keyfile.h>
12    #include <iostream>
13    #include <stdio.h>
14    #include <fstream>
15    
16  static Settings _instance;  #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    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        switch (group) {
38            case Settings::GLOBAL: return "Global";
39            case Settings::MAIN_WINDOW: return "MainWindow";
40            case Settings::SCRIPT_EDITOR: return "ScriptEditor";
41        }
42        return "Global";
43    }
44    
45    #if !HAS_GLIB_KEYFILE_SAVE_TO_FILE
46    
47    static bool saveToFile(Glib::KeyFile* keyfile, std::string filename) {
48        Glib::ustring s = keyfile->to_data();
49        std::ofstream out;
50        out.open(filename.c_str(), std::ios_base::out | std::ios_base::trunc);
51        out << s;
52        out.close();
53        return true;
54    }
55    
56    #endif // ! HAS_GLIB_KEYFILE_SAVE_TO_FILE
57    
58    static Settings* _instance = NULL;
59            
60  Settings* Settings::singleton() {  Settings* Settings::singleton() {
61      return &_instance;      if (!_instance) {
62            _instance = new Settings;
63            _instance->load();
64        }
65        return _instance;
66  }  }
67    
68  Settings::Settings() {  Settings::Settings() : Glib::ObjectBase(typeid(Settings)),
69      warnUserOnExtensions = true;      warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),
70        syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),
71        moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true),
72        mainWindowX(*this, MAIN_WINDOW, "x", -1),
73        mainWindowY(*this, MAIN_WINDOW, "y", -1),
74        mainWindowW(*this, MAIN_WINDOW, "w", -1),
75        mainWindowH(*this, MAIN_WINDOW, "h", -1),
76        scriptEditorWindowX(*this, SCRIPT_EDITOR, "x", -1),
77        scriptEditorWindowY(*this, SCRIPT_EDITOR, "y", -1),
78        scriptEditorWindowW(*this, SCRIPT_EDITOR, "w", -1),
79        scriptEditorWindowH(*this, SCRIPT_EDITOR, "h", -1),
80        m_ignoreNotifies(false)
81    {
82        m_boolProps.push_back(&warnUserOnExtensions);
83        m_boolProps.push_back(&syncSamplerInstrumentSelection);
84        m_boolProps.push_back(&moveRootNoteWithRegionMoved);
85        m_intProps.push_back(&mainWindowX);
86        m_intProps.push_back(&mainWindowY);
87        m_intProps.push_back(&mainWindowW);
88        m_intProps.push_back(&mainWindowH);
89        m_intProps.push_back(&scriptEditorWindowX);
90        m_intProps.push_back(&scriptEditorWindowY);
91        m_intProps.push_back(&scriptEditorWindowW);
92        m_intProps.push_back(&scriptEditorWindowH);
93  }  }
94    
95    void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) {
96        if (m_ignoreNotifies) return;
97    
98        //printf("Settings::onPropertyChanged(%s)\n", pProperty->get_name().c_str());
99    
100        Glib::KeyFile file;
101        try {
102            bool ok = file.load_from_file(configFile());
103            if (!ok) {
104                std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
105            }
106        } catch (...) {
107            std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
108        }
109    
110        switch (type) {
111            case BOOLEAN: {
112                Property<bool>* prop = static_cast<Property<bool>*>(pProperty);
113                //std::cout << "Saving bool setting '" << prop->get_name() << "'\n" << std::flush;
114                file.set_boolean(groupName(prop->group()), prop->get_name(), prop->get_value());
115                break;
116            }
117            case INTEGER: {
118                Property<int>* prop = static_cast<Property<int>*>(pProperty);
119                //std::cout << "Saving int setting '" << prop->get_name() << "'\n" << std::flush;
120                file.set_integer(groupName(prop->group()), prop->get_name(), prop->get_value());
121                break;
122            }
123            case UNKNOWN:
124                std::cerr << "BUG: Unknown setting raw type of property '" << pProperty->get_name() << "'\n" << std::flush;
125                return;
126        }
127    
128        try {
129    #if HAS_GLIB_KEYFILE_SAVE_TO_FILE
130            bool ok = file.save_to_file(configFile());
131    #else
132            bool ok = saveToFile(&file, configFile());
133    #endif
134            if (!ok) {
135                std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
136            } else {
137                //std::cout <<"gigedit CONFIG SAVED\n";
138            }
139        } catch (...) {
140            std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
141        }
142    }
143    
144    void Settings::load() {
145        Glib::KeyFile file;
146        try {
147            bool ok = file.load_from_file(configFile());
148            if (!ok) return;
149        } catch (...) {
150            std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush;
151            return;
152        }
153    
154        // ignore onPropertyChanged() calls during updating the property values below
155        m_ignoreNotifies = true;
156    
157        for (int i = 0; i < m_boolProps.size(); ++i) {
158            Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);
159            try {
160                const std::string group = groupName(prop->group());
161                if (!file.has_key(group, prop->get_name())) continue;
162                const bool value = file.get_boolean(group, prop->get_name());
163                prop->set_value(value);
164            } catch (...) {
165                continue;
166            }
167        }
168    
169        for (int i = 0; i < m_intProps.size(); ++i) {
170            Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);
171            try {
172                const std::string group = groupName(prop->group());
173                if (!file.has_key(group, prop->get_name())) continue;
174                const int value = file.get_integer(group, prop->get_name());
175                prop->set_value(value);
176            } catch (...) {
177                continue;
178            }
179        }
180    
181        m_ignoreNotifies = false;
182    }
183    

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

  ViewVC Help
Powered by ViewVC