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 |
|
|
return "Global"; |
38 |
|
|
} |
39 |
|
|
|
40 |
schoenebeck |
2892 |
#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 |
schoenebeck |
2891 |
static Settings* _instance = NULL; |
54 |
schoenebeck |
2541 |
|
55 |
|
|
Settings* Settings::singleton() { |
56 |
schoenebeck |
2891 |
if (!_instance) { |
57 |
|
|
_instance = new Settings; |
58 |
|
|
_instance->load(); |
59 |
|
|
} |
60 |
|
|
return _instance; |
61 |
schoenebeck |
2541 |
} |
62 |
|
|
|
63 |
schoenebeck |
2891 |
Settings::Settings() : Glib::ObjectBase(typeid(Settings)), |
64 |
|
|
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 |
schoenebeck |
2541 |
} |
73 |
schoenebeck |
2891 |
|
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 |
schoenebeck |
2892 |
#if HAS_GLIB_KEYFILE_SAVE_TO_FILE |
109 |
schoenebeck |
2891 |
bool ok = file.save_to_file(configFile()); |
110 |
schoenebeck |
2892 |
#else |
111 |
|
|
bool ok = saveToFile(&file, configFile()); |
112 |
|
|
#endif |
113 |
schoenebeck |
2891 |
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 |
|
|
|