/* Copyright (c) 2014-2016 Christian Schoenebeck This file is part of "gigedit" and released under the terms of the GNU General Public License version 2. */ #include "Settings.h" #include #include "global.h" #include #include #include static std::string configDir() { //printf("configDir '%s'\n", g_get_user_config_dir()); return g_get_user_config_dir(); } static std::string dirSep() { //printf("sep '%s'\n", G_DIR_SEPARATOR_S); return G_DIR_SEPARATOR_S; } static std::string configFile() { return configDir() + dirSep() + "gigedit.conf"; } static std::string groupName(Settings::Group_t group) { return "Global"; } static Settings* _instance = NULL; Settings* Settings::singleton() { if (!_instance) { _instance = new Settings; _instance->load(); } return _instance; } Settings::Settings() : Glib::ObjectBase(typeid(Settings)), warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true), syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true), moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true), m_ignoreNotifies(false) { m_boolProps.push_back(&warnUserOnExtensions); m_boolProps.push_back(&syncSamplerInstrumentSelection); m_boolProps.push_back(&moveRootNoteWithRegionMoved); } void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) { if (m_ignoreNotifies) return; //printf("Settings::onPropertyChanged(%s)\n", pProperty->get_name().c_str()); Glib::KeyFile file; try { bool ok = file.load_from_file(configFile()); if (!ok) { std::cerr << "Could not load '" << configFile() << "'\n" << std::flush; } } catch (...) { std::cerr << "Could not load '" << configFile() << "'\n" << std::flush; } switch (type) { case BOOLEAN: { Property* prop = static_cast*>(pProperty); //std::cout << "Saving bool setting '" << prop->get_name() << "'\n" << std::flush; file.set_boolean(groupName(prop->group()), prop->get_name(), prop->get_value()); break; } case INTEGER: { Property* prop = static_cast*>(pProperty); //std::cout << "Saving int setting '" << prop->get_name() << "'\n" << std::flush; file.set_integer(groupName(prop->group()), prop->get_name(), prop->get_value()); break; } case UNKNOWN: std::cerr << "BUG: Unknown setting raw type of property '" << pProperty->get_name() << "'\n" << std::flush; return; } try { bool ok = file.save_to_file(configFile()); if (!ok) { std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush; } else { //std::cout <<"gigedit CONFIG SAVED\n"; } } catch (...) { std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush; } } void Settings::load() { Glib::KeyFile file; try { bool ok = file.load_from_file(configFile()); if (!ok) return; } catch (...) { std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush; return; } // ignore onPropertyChanged() calls during updating the property values below m_ignoreNotifies = true; for (int i = 0; i < m_boolProps.size(); ++i) { Property* prop = static_cast*>(m_boolProps[i]); try { const std::string group = groupName(prop->group()); if (!file.has_key(group, prop->get_name())) continue; const bool value = file.get_boolean(group, prop->get_name()); prop->set_value(value); } catch (...) { continue; } } for (int i = 0; i < m_intProps.size(); ++i) { Property* prop = static_cast*>(m_intProps[i]); try { const std::string group = groupName(prop->group()); if (!file.has_key(group, prop->get_name())) continue; const int value = file.get_integer(group, prop->get_name()); prop->set_value(value); } catch (...) { continue; } } m_ignoreNotifies = false; }