/[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 2890 by schoenebeck, Fri Jun 12 17:57:52 2015 UTC revision 2891 by schoenebeck, Tue Apr 26 17:42:26 2016 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014-2015 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    
15  static Settings _instance;  static std::string configDir() {
16        //printf("configDir '%s'\n", g_get_user_config_dir());
17        return g_get_user_config_dir();
18    }
19    
20    static std::string dirSep() {
21        //printf("sep '%s'\n", G_DIR_SEPARATOR_S);
22        return G_DIR_SEPARATOR_S;
23    }
24    
25    static std::string configFile() {
26        return configDir() + dirSep() + "gigedit.conf";
27    }
28    
29    static std::string groupName(Settings::Group_t group) {
30        return "Global";
31    }
32    
33    static Settings* _instance = NULL;
34            
35  Settings* Settings::singleton() {  Settings* Settings::singleton() {
36      return &_instance;      if (!_instance) {
37            _instance = new Settings;
38            _instance->load();
39        }
40        return _instance;
41  }  }
42    
43  Settings::Settings() {  Settings::Settings() : Glib::ObjectBase(typeid(Settings)),
44      warnUserOnExtensions = true;      warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),
45      syncSamplerInstrumentSelection = true;      syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),
46      moveRootNoteWithRegionMoved = true;      moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true),
47        m_ignoreNotifies(false)
48    {
49        m_boolProps.push_back(&warnUserOnExtensions);
50        m_boolProps.push_back(&syncSamplerInstrumentSelection);
51        m_boolProps.push_back(&moveRootNoteWithRegionMoved);
52  }  }
53    
54    void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) {
55        if (m_ignoreNotifies) return;
56    
57        //printf("Settings::onPropertyChanged(%s)\n", pProperty->get_name().c_str());
58    
59        Glib::KeyFile file;
60        try {
61            bool ok = file.load_from_file(configFile());
62            if (!ok) {
63                std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
64            }
65        } catch (...) {
66            std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
67        }
68    
69        switch (type) {
70            case BOOLEAN: {
71                Property<bool>* prop = static_cast<Property<bool>*>(pProperty);
72                //std::cout << "Saving bool setting '" << prop->get_name() << "'\n" << std::flush;
73                file.set_boolean(groupName(prop->group()), prop->get_name(), prop->get_value());
74                break;
75            }
76            case INTEGER: {
77                Property<int>* prop = static_cast<Property<int>*>(pProperty);
78                //std::cout << "Saving int setting '" << prop->get_name() << "'\n" << std::flush;
79                file.set_integer(groupName(prop->group()), prop->get_name(), prop->get_value());
80                break;
81            }
82            case UNKNOWN:
83                std::cerr << "BUG: Unknown setting raw type of property '" << pProperty->get_name() << "'\n" << std::flush;
84                return;
85        }
86    
87        try {
88            bool ok = file.save_to_file(configFile());
89            if (!ok) {
90                std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
91            } else {
92                //std::cout <<"gigedit CONFIG SAVED\n";
93            }
94        } catch (...) {
95            std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
96        }
97    }
98    
99    void Settings::load() {
100        Glib::KeyFile file;
101        try {
102            bool ok = file.load_from_file(configFile());
103            if (!ok) return;
104        } catch (...) {
105            std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush;
106            return;
107        }
108    
109        // ignore onPropertyChanged() calls during updating the property values below
110        m_ignoreNotifies = true;
111    
112        for (int i = 0; i < m_boolProps.size(); ++i) {
113            Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);
114            try {
115                const std::string group = groupName(prop->group());
116                if (!file.has_key(group, prop->get_name())) continue;
117                const bool value = file.get_boolean(group, prop->get_name());
118                prop->set_value(value);
119            } catch (...) {
120                continue;
121            }
122        }
123    
124        for (int i = 0; i < m_intProps.size(); ++i) {
125            Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);
126            try {
127                const std::string group = groupName(prop->group());
128                if (!file.has_key(group, prop->get_name())) continue;
129                const int value = file.get_integer(group, prop->get_name());
130                prop->set_value(value);
131            } catch (...) {
132                continue;
133            }
134        }
135    
136        m_ignoreNotifies = false;
137    }
138    

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

  ViewVC Help
Powered by ViewVC