1 |
/* |
/* |
2 |
Copyright (c) 2014-2016 Christian Schoenebeck |
Copyright (c) 2014-2017 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 "global.h" |
9 |
#include "Settings.h" |
#include "Settings.h" |
10 |
#include <glib.h> |
#include <glib.h> |
|
#include "global.h" |
|
11 |
#include <glibmm/keyfile.h> |
#include <glibmm/keyfile.h> |
12 |
#include <iostream> |
#include <iostream> |
13 |
#include <stdio.h> |
#include <stdio.h> |
14 |
#include <fstream> |
#include <fstream> |
15 |
|
#include <string.h> |
16 |
|
|
17 |
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 40) || GTKMM_MAJOR_VERSION < 2 |
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 40) || GTKMM_MAJOR_VERSION < 2 |
18 |
# define HAS_GLIB_KEYFILE_SAVE_TO_FILE 0 |
# define HAS_GLIB_KEYFILE_SAVE_TO_FILE 0 |
46 |
case Settings::FILE_PROPS: return "FileProps"; |
case Settings::FILE_PROPS: return "FileProps"; |
47 |
case Settings::INSTR_PROPS: return "InstrProps"; |
case Settings::INSTR_PROPS: return "InstrProps"; |
48 |
case Settings::SAMPLE_REFS: return "SampleRefs"; |
case Settings::SAMPLE_REFS: return "SampleRefs"; |
49 |
|
case Settings::MACRO_EDITOR: return "MacroEditor"; |
50 |
|
case Settings::MACROS_SETUP: return "MacrosSetup"; |
51 |
|
case Settings::MACROS: return "Macros"; |
52 |
} |
} |
53 |
return "Global"; |
return "Global"; |
54 |
} |
} |
81 |
syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true), |
syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true), |
82 |
moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true), |
moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true), |
83 |
autoRestoreWindowDimension(*this, GLOBAL, "autoRestoreWindowDimension", false), |
autoRestoreWindowDimension(*this, GLOBAL, "autoRestoreWindowDimension", false), |
84 |
|
saveWithTemporaryFile(*this, GLOBAL, "saveWithTemporaryFile", false), |
85 |
mainWindowX(*this, MAIN_WINDOW, "x", -1), |
mainWindowX(*this, MAIN_WINDOW, "x", -1), |
86 |
mainWindowY(*this, MAIN_WINDOW, "y", -1), |
mainWindowY(*this, MAIN_WINDOW, "y", -1), |
87 |
mainWindowW(*this, MAIN_WINDOW, "w", -1), |
mainWindowW(*this, MAIN_WINDOW, "w", -1), |
90 |
scriptEditorWindowY(*this, SCRIPT_EDITOR, "y", -1), |
scriptEditorWindowY(*this, SCRIPT_EDITOR, "y", -1), |
91 |
scriptEditorWindowW(*this, SCRIPT_EDITOR, "w", -1), |
scriptEditorWindowW(*this, SCRIPT_EDITOR, "w", -1), |
92 |
scriptEditorWindowH(*this, SCRIPT_EDITOR, "h", -1), |
scriptEditorWindowH(*this, SCRIPT_EDITOR, "h", -1), |
93 |
|
scriptEditorFontSize(*this, SCRIPT_EDITOR, "fontSize", -1), |
94 |
dimensionManagerWindowX(*this, DIMENSION_MANAGER, "x", -1), |
dimensionManagerWindowX(*this, DIMENSION_MANAGER, "x", -1), |
95 |
dimensionManagerWindowY(*this, DIMENSION_MANAGER, "y", -1), |
dimensionManagerWindowY(*this, DIMENSION_MANAGER, "y", -1), |
96 |
dimensionManagerWindowW(*this, DIMENSION_MANAGER, "w", -1), |
dimensionManagerWindowW(*this, DIMENSION_MANAGER, "w", -1), |
119 |
sampleRefsWindowY(*this, SAMPLE_REFS, "y", -1), |
sampleRefsWindowY(*this, SAMPLE_REFS, "y", -1), |
120 |
sampleRefsWindowW(*this, SAMPLE_REFS, "w", -1), |
sampleRefsWindowW(*this, SAMPLE_REFS, "w", -1), |
121 |
sampleRefsWindowH(*this, SAMPLE_REFS, "h", -1), |
sampleRefsWindowH(*this, SAMPLE_REFS, "h", -1), |
122 |
|
macroEditorWindowX(*this, MACRO_EDITOR, "x", -1), |
123 |
|
macroEditorWindowY(*this, MACRO_EDITOR, "y", -1), |
124 |
|
macroEditorWindowW(*this, MACRO_EDITOR, "w", -1), |
125 |
|
macroEditorWindowH(*this, MACRO_EDITOR, "h", -1), |
126 |
|
macrosSetupWindowX(*this, MACROS_SETUP, "x", -1), |
127 |
|
macrosSetupWindowY(*this, MACROS_SETUP, "y", -1), |
128 |
|
macrosSetupWindowW(*this, MACROS_SETUP, "w", -1), |
129 |
|
macrosSetupWindowH(*this, MACROS_SETUP, "h", -1), |
130 |
m_ignoreNotifies(false) |
m_ignoreNotifies(false) |
131 |
{ |
{ |
132 |
m_boolProps.push_back(&warnUserOnExtensions); |
m_boolProps.push_back(&warnUserOnExtensions); |
133 |
m_boolProps.push_back(&syncSamplerInstrumentSelection); |
m_boolProps.push_back(&syncSamplerInstrumentSelection); |
134 |
m_boolProps.push_back(&moveRootNoteWithRegionMoved); |
m_boolProps.push_back(&moveRootNoteWithRegionMoved); |
135 |
m_boolProps.push_back(&autoRestoreWindowDimension); |
m_boolProps.push_back(&autoRestoreWindowDimension); |
136 |
|
m_boolProps.push_back(&saveWithTemporaryFile); |
137 |
m_intProps.push_back(&mainWindowX); |
m_intProps.push_back(&mainWindowX); |
138 |
m_intProps.push_back(&mainWindowY); |
m_intProps.push_back(&mainWindowY); |
139 |
m_intProps.push_back(&mainWindowW); |
m_intProps.push_back(&mainWindowW); |
142 |
m_intProps.push_back(&scriptEditorWindowY); |
m_intProps.push_back(&scriptEditorWindowY); |
143 |
m_intProps.push_back(&scriptEditorWindowW); |
m_intProps.push_back(&scriptEditorWindowW); |
144 |
m_intProps.push_back(&scriptEditorWindowH); |
m_intProps.push_back(&scriptEditorWindowH); |
145 |
|
m_intProps.push_back(&scriptEditorFontSize); |
146 |
m_intProps.push_back(&dimensionManagerWindowX); |
m_intProps.push_back(&dimensionManagerWindowX); |
147 |
m_intProps.push_back(&dimensionManagerWindowY); |
m_intProps.push_back(&dimensionManagerWindowY); |
148 |
m_intProps.push_back(&dimensionManagerWindowW); |
m_intProps.push_back(&dimensionManagerWindowW); |
171 |
m_intProps.push_back(&sampleRefsWindowY); |
m_intProps.push_back(&sampleRefsWindowY); |
172 |
m_intProps.push_back(&sampleRefsWindowW); |
m_intProps.push_back(&sampleRefsWindowW); |
173 |
m_intProps.push_back(&sampleRefsWindowH); |
m_intProps.push_back(&sampleRefsWindowH); |
174 |
|
m_intProps.push_back(¯oEditorWindowX); |
175 |
|
m_intProps.push_back(¯oEditorWindowY); |
176 |
|
m_intProps.push_back(¯oEditorWindowW); |
177 |
|
m_intProps.push_back(¯oEditorWindowH); |
178 |
|
m_intProps.push_back(¯osSetupWindowX); |
179 |
|
m_intProps.push_back(¯osSetupWindowY); |
180 |
|
m_intProps.push_back(¯osSetupWindowW); |
181 |
|
m_intProps.push_back(¯osSetupWindowH); |
182 |
} |
} |
183 |
|
|
184 |
void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) { |
void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) { |
247 |
Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]); |
Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]); |
248 |
try { |
try { |
249 |
const std::string group = groupName(prop->group()); |
const std::string group = groupName(prop->group()); |
250 |
|
if (!file.has_group(group)) continue; |
251 |
if (!file.has_key(group, prop->get_name())) continue; |
if (!file.has_key(group, prop->get_name())) continue; |
252 |
const bool value = file.get_boolean(group, prop->get_name()); |
const bool value = file.get_boolean(group, prop->get_name()); |
253 |
prop->set_value(value); |
prop->set_value(value); |
260 |
Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]); |
Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]); |
261 |
try { |
try { |
262 |
const std::string group = groupName(prop->group()); |
const std::string group = groupName(prop->group()); |
263 |
|
if (!file.has_group(group)) continue; |
264 |
if (!file.has_key(group, prop->get_name())) continue; |
if (!file.has_key(group, prop->get_name())) continue; |
265 |
const int value = file.get_integer(group, prop->get_name()); |
const int value = file.get_integer(group, prop->get_name()); |
266 |
prop->set_value(value); |
prop->set_value(value); |
272 |
m_ignoreNotifies = false; |
m_ignoreNotifies = false; |
273 |
} |
} |
274 |
|
|
275 |
|
#define MACRO_LIST_NAME "srlzl" |
276 |
|
|
277 |
|
void Settings::loadMacros(std::vector<Serialization::Archive>& macros) { |
278 |
|
const std::string group = groupName(MACROS); |
279 |
|
macros.clear(); |
280 |
|
Glib::KeyFile file; |
281 |
|
try { |
282 |
|
bool ok = file.load_from_file(configFile()); |
283 |
|
if (!ok) return; |
284 |
|
} catch (...) { |
285 |
|
std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush; |
286 |
|
return; |
287 |
|
} |
288 |
|
if (!file.has_group(group)) return; |
289 |
|
if (!file.has_key(group, MACRO_LIST_NAME)) |
290 |
|
return; |
291 |
|
std::vector<Glib::ustring> v = file.get_string_list(group, MACRO_LIST_NAME); |
292 |
|
for (int i = 0; i < v.size(); ++i) { |
293 |
|
Serialization::Archive macro; |
294 |
|
macro.decode((const uint8_t*)v[i].c_str(), v[i].length()); |
295 |
|
macros.push_back(macro); |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
void Settings::saveMacros(const std::vector<Serialization::Archive>& macros) { |
300 |
|
const std::string group = groupName(MACROS); |
301 |
|
Glib::KeyFile file; |
302 |
|
try { |
303 |
|
bool ok = file.load_from_file(configFile()); |
304 |
|
if (!ok) { |
305 |
|
std::cerr << "Could not load '" << configFile() << "'\n" << std::flush; |
306 |
|
} |
307 |
|
} catch (...) { |
308 |
|
std::cerr << "Could not load '" << configFile() << "'\n" << std::flush; |
309 |
|
return; |
310 |
|
} |
311 |
|
|
312 |
|
std::vector<Glib::ustring> v; |
313 |
|
for (int i = 0; i < macros.size(); ++i) { |
314 |
|
const Serialization::RawData& rawData = const_cast<Serialization::Archive&>(macros[i]).rawData(); |
315 |
|
std::string s((const char*)&rawData[0], rawData.size()); |
316 |
|
v.push_back(s); |
317 |
|
} |
318 |
|
|
319 |
|
file.set_string_list(group, MACRO_LIST_NAME, v); |
320 |
|
|
321 |
|
try { |
322 |
|
#if HAS_GLIB_KEYFILE_SAVE_TO_FILE |
323 |
|
bool ok = file.save_to_file(configFile()); |
324 |
|
#else |
325 |
|
bool ok = saveToFile(&file, configFile()); |
326 |
|
#endif |
327 |
|
if (!ok) { |
328 |
|
std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush; |
329 |
|
} else { |
330 |
|
//std::cout <<"gigedit CONFIG SAVED\n"; |
331 |
|
} |
332 |
|
} catch (...) { |
333 |
|
std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush; |
334 |
|
} |
335 |
|
|
336 |
|
} |