/[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 2892 by schoenebeck, Tue Apr 26 18:06:31 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        return "Global";
38    }
39    
40    #if !HAS_GLIB_KEYFILE_SAVE_TO_FILE
41    
42    static bool saveToFile(Glib::KeyFile* keyfile, std::string filename) {
43        Glib::ustring s = keyfile->to_data();
44        std::ofstream out;
45        out.open(filename.c_str(), std::ios_base::out | std::ios_base::trunc);
46        out << s;
47        out.close();
48        return true;
49    }
50    
51    #endif // ! HAS_GLIB_KEYFILE_SAVE_TO_FILE
52    
53    static Settings* _instance = NULL;
54            
55  Settings* Settings::singleton() {  Settings* Settings::singleton() {
56      return &_instance;      if (!_instance) {
57            _instance = new Settings;
58            _instance->load();
59        }
60        return _instance;
61  }  }
62    
63  Settings::Settings() {  Settings::Settings() : Glib::ObjectBase(typeid(Settings)),
64      warnUserOnExtensions = true;      warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),
65        syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),
66        moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true),
67        m_ignoreNotifies(false)
68    {
69        m_boolProps.push_back(&warnUserOnExtensions);
70        m_boolProps.push_back(&syncSamplerInstrumentSelection);
71        m_boolProps.push_back(&moveRootNoteWithRegionMoved);
72  }  }
73    
74    void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) {
75        if (m_ignoreNotifies) return;
76    
77        //printf("Settings::onPropertyChanged(%s)\n", pProperty->get_name().c_str());
78    
79        Glib::KeyFile file;
80        try {
81            bool ok = file.load_from_file(configFile());
82            if (!ok) {
83                std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
84            }
85        } catch (...) {
86            std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
87        }
88    
89        switch (type) {
90            case BOOLEAN: {
91                Property<bool>* prop = static_cast<Property<bool>*>(pProperty);
92                //std::cout << "Saving bool setting '" << prop->get_name() << "'\n" << std::flush;
93                file.set_boolean(groupName(prop->group()), prop->get_name(), prop->get_value());
94                break;
95            }
96            case INTEGER: {
97                Property<int>* prop = static_cast<Property<int>*>(pProperty);
98                //std::cout << "Saving int setting '" << prop->get_name() << "'\n" << std::flush;
99                file.set_integer(groupName(prop->group()), prop->get_name(), prop->get_value());
100                break;
101            }
102            case UNKNOWN:
103                std::cerr << "BUG: Unknown setting raw type of property '" << pProperty->get_name() << "'\n" << std::flush;
104                return;
105        }
106    
107        try {
108    #if HAS_GLIB_KEYFILE_SAVE_TO_FILE
109            bool ok = file.save_to_file(configFile());
110    #else
111            bool ok = saveToFile(&file, configFile());
112    #endif
113            if (!ok) {
114                std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
115            } else {
116                //std::cout <<"gigedit CONFIG SAVED\n";
117            }
118        } catch (...) {
119            std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
120        }
121    }
122    
123    void Settings::load() {
124        Glib::KeyFile file;
125        try {
126            bool ok = file.load_from_file(configFile());
127            if (!ok) return;
128        } catch (...) {
129            std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush;
130            return;
131        }
132    
133        // ignore onPropertyChanged() calls during updating the property values below
134        m_ignoreNotifies = true;
135    
136        for (int i = 0; i < m_boolProps.size(); ++i) {
137            Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);
138            try {
139                const std::string group = groupName(prop->group());
140                if (!file.has_key(group, prop->get_name())) continue;
141                const bool value = file.get_boolean(group, prop->get_name());
142                prop->set_value(value);
143            } catch (...) {
144                continue;
145            }
146        }
147    
148        for (int i = 0; i < m_intProps.size(); ++i) {
149            Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);
150            try {
151                const std::string group = groupName(prop->group());
152                if (!file.has_key(group, prop->get_name())) continue;
153                const int value = file.get_integer(group, prop->get_name());
154                prop->set_value(value);
155            } catch (...) {
156                continue;
157            }
158        }
159    
160        m_ignoreNotifies = false;
161    }
162    

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

  ViewVC Help
Powered by ViewVC