[gnome-commander/GSettings] Store GSettings values only when the value is different from the current one
- From: Uwe Scholz <uwescholz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-commander/GSettings] Store GSettings values only when the value is different from the current one
- Date: Sun, 15 May 2016 21:34:41 +0000 (UTC)
commit e077e08c54849652b7dcf494e9f13f2c4e7ebec2
Author: Uwe Scholz <uwescholz src gnome org>
Date: Sun May 15 23:33:26 2016 +0200
Store GSettings values only when the value is different from the current one
src/gnome-cmd-data.cc | 101 +++++++++++++++++++++++++++++++++++++++++++++++--
src/gnome-cmd-data.h | 2 +
2 files changed, 99 insertions(+), 4 deletions(-)
---
diff --git a/src/gnome-cmd-data.cc b/src/gnome-cmd-data.cc
index cb57972..b73d00a 100644
--- a/src/gnome-cmd-data.cc
+++ b/src/gnome-cmd-data.cc
@@ -2089,10 +2089,10 @@ void GnomeCmdData::load_more()
void GnomeCmdData::save()
{
- g_settings_set_enum (options.gcmd_settings->general, GCMD_SETTINGS_SIZE_DISP_MODE,
options.size_disp_mode);
- g_settings_set_enum (options.gcmd_settings->general, GCMD_SETTINGS_PERM_DISP_MODE,
options.perm_disp_mode);
- g_settings_set_enum (options.gcmd_settings->general, GCMD_SETTINGS_GRAPHICAL_LAYOUT_MODE,
options.layout);
- g_settings_set_uint (options.gcmd_settings->general, GCMD_SETTINGS_LIST_ROW_HEIGHT,
options.list_row_height);
+ set_gsettings_enum_when_changed (options.gcmd_settings->general, GCMD_SETTINGS_SIZE_DISP_MODE,
options.size_disp_mode);
+ set_gsettings_enum_when_changed (options.gcmd_settings->general, GCMD_SETTINGS_PERM_DISP_MODE,
options.perm_disp_mode);
+ set_gsettings_enum_when_changed (options.gcmd_settings->general, GCMD_SETTINGS_GRAPHICAL_LAYOUT_MODE,
options.layout);
+ set_gsettings_when_changed (options.gcmd_settings->general, GCMD_SETTINGS_LIST_ROW_HEIGHT,
&(options.list_row_height));
gchar *utf8_date_format = g_locale_to_utf8 (options.date_format, -1, NULL, NULL, NULL);
gnome_cmd_data_set_string ("/options/date_disp_mode", utf8_date_format);
@@ -2333,6 +2333,99 @@ void GnomeCmdData::gnome_cmd_data_set_int (const gchar *path, int value)
}
+/**
+ * As GSettings enum-type is of GVARIANT_CLASS String, we need a seperate function for
+ * finding out if a key value has changed. This is done here. For storing the other GSettings
+ * types, see @link set_gsettings_when_changed @endlink .
+ * @returns TRUE if new value could be stored, else FALSE
+ */
+gboolean GnomeCmdData::set_gsettings_enum_when_changed (GSettings *settings, const char *key, gint new_value)
+{
+ GVariant *default_val;
+ gboolean rv = true;
+
+ default_val = g_settings_get_default_value (settings, key);
+
+ // An enum key must be of type G_VARIANT_CLASS_STRING
+ if (g_variant_classify(default_val) == G_VARIANT_CLASS_STRING)
+ {
+ gint old_value;
+ old_value = g_settings_get_enum(settings, key);
+ if (old_value != new_value)
+ rv = g_settings_set_enum (settings, key, new_value);
+ }
+ else
+ {
+ g_warning("Could not store value of type '%s' for key '%s'\n", g_variant_get_type_string
(default_val), key);
+ rv = false;
+ }
+
+ if (default_val)
+ g_variant_unref (default_val);
+
+ return rv;
+}
+
+
+/**
+ * This method stores the value for a given key if the value is different from the currently stored one
+ * under the keys value. This function is able of storing several types of GSettings values (except enums
+ * which is done in @link set_gsettings_enum_when_changed @endlink ). Therefore, it first checks the type
+ * of GVariant of the default value of the given key. Depending on the result, the gpointer is than casted
+ * to the correct type so that *value can be saved.
+ * @returns TRUE if new value could be stored, else FALSE
+ */
+gboolean GnomeCmdData::set_gsettings_when_changed (GSettings *settings, const char *key, gpointer value)
+{
+ GVariant *default_val;
+ gboolean rv = true;
+ default_val = g_settings_get_default_value (settings, key);
+
+ switch (g_variant_classify(default_val))
+ {
+ case G_VARIANT_CLASS_UINT32:
+ {
+ gint old_value;
+ gint new_value = *(gint*) value;
+
+ old_value = g_settings_get_uint (settings, key);
+ if (old_value != new_value)
+ rv = g_settings_set_uint (settings, key, new_value);
+ break;
+ }
+ // For later usage
+ // case SOME_OTHER_CLASS:
+ // {
+ // GVariant *user_val;
+ // gint old_value;
+ // gint new_value = *(gint*) value;
+ //
+ // user_val = g_settings_get_user_value (settings, key);
+ // if (user_val)
+ // {
+ // old_value = g_variant_get_uint32 (user_val);
+ //
+ // if (old_value != new_value)
+ // rv = g_settings_set_int (settings, key, new_value);
+ //
+ // g_variant_unref (user_val);
+ // }
+ // break;
+ // }
+ default:
+ {
+ g_warning("Could not store value of type '%s' for key '%s'\n", g_variant_get_type_string
(default_val), key);
+ rv = false;
+ break;
+ }
+ }
+ if (default_val)
+ g_variant_unref (default_val);
+
+ return rv;
+}
+
+
GnomeCmdFileList::ColumnID GnomeCmdData::get_sort_col(FileSelectorID id) const
{
return (GnomeCmdFileList::ColumnID) priv->sort_column[id];
diff --git a/src/gnome-cmd-data.h b/src/gnome-cmd-data.h
index 5949f04..d69bdcd 100644
--- a/src/gnome-cmd-data.h
+++ b/src/gnome-cmd-data.h
@@ -507,6 +507,8 @@ struct GnomeCmdData
void save();
gint gnome_cmd_data_get_int (const gchar *path, int def);
void gnome_cmd_data_set_int (const gchar *path, int value);
+ gboolean set_gsettings_when_changed (GSettings *settings, const char *key, gpointer value);
+ gboolean set_gsettings_enum_when_changed (GSettings *settings, const char *key, gint value);
GnomeCmdConRemote *get_quick_connect() const { return quick_connect; }
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]