File indexing completed on 2024-11-24 03:56:28

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "settings.hpp"
0008 #include "app/application.hpp"
0009 
0010 #include <set>
0011 #include <QSettings>
0012 
0013 void app::settings::Settings::load()
0014 {
0015     QSettings settings = app::Application::instance()->qsettings();
0016 
0017     auto avail_groups = settings.childGroups();
0018     std::set<QString> unprocessed(avail_groups.begin(), avail_groups.end());
0019     avail_groups.clear();
0020 
0021     for ( const auto& group : groups_ )
0022     {
0023         unprocessed.erase(group->slug());
0024         settings.beginGroup(group->slug());
0025         group->load(settings);
0026         settings.endGroup();
0027     }
0028 }
0029 
0030 void app::settings::Settings::save()
0031 {
0032     QSettings settings = app::Application::instance()->qsettings();
0033 
0034     for ( const auto& group : groups_ )
0035     {
0036         settings.beginGroup(group->slug());
0037         group->save(settings);
0038         settings.endGroup();
0039     }
0040 }
0041 
0042 void app::settings::Settings::add_group(QString slug, utils::TranslatedString label, const QString& icon, SettingList settings)
0043 {
0044     add_group(std::make_unique<SettingsGroup>(std::move(slug), std::move(label), std::move(icon), std::move(settings)));
0045 }
0046 
0047 void app::settings::Settings::add_group(CustomSettingsGroup group)
0048 {
0049     auto slug = group->slug();
0050 
0051     if ( !order.contains(slug) )
0052         order[slug] = groups_.size();
0053 
0054     groups_.push_back(std::move(group));
0055 }
0056 
0057 QVariant app::settings::Settings::get_value ( const QString& group, const QString& setting ) const
0058 {
0059     if ( !order.contains(group) )
0060         return {};
0061 
0062     return groups_[order[group]]->get_variant(setting);
0063 }
0064 
0065 QVariant app::settings::Settings::get_default(const QString& group, const QString& setting) const
0066 {
0067     if ( !order.contains(group) )
0068         return {};
0069 
0070     return groups_[order[group]]->get_default(setting);
0071 }
0072 
0073 
0074 bool app::settings::Settings::set_value ( const QString& group, const QString& setting, const QVariant& value )
0075 {
0076     if ( !order.contains(group) )
0077         return false;
0078 
0079     return groups_[order[group]]->set_variant(setting, value);
0080 }
0081 
0082 QVariant app::settings::Settings::define(const QString& group, const QString& setting, const QVariant& default_value)
0083 {
0084     if ( !order.contains(group) )
0085         return default_value;
0086 
0087     return groups_[order[group]]->define(setting, default_value);
0088 }