[gnome-commander/GSettings] Introduces functions for setting and getting GSetting string arrays



commit 98d0a1ab9bee4ecbb479ca63aef343fc5b2a1e83
Author: Uwe Scholz <uwescholz src gnome org>
Date:   Sat Jul 30 12:07:22 2016 +0200

    Introduces functions for setting and getting GSetting string arrays

 src/gnome-cmd-data.cc |   50 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/gnome-cmd-data.h  |    2 +
 2 files changed, 52 insertions(+), 0 deletions(-)
---
diff --git a/src/gnome-cmd-data.cc b/src/gnome-cmd-data.cc
index bd82833..5dc01e5 100644
--- a/src/gnome-cmd-data.cc
+++ b/src/gnome-cmd-data.cc
@@ -2378,6 +2378,37 @@ inline void GnomeCmdData::gnome_cmd_data_set_string_history (const gchar *format
     }
 }
 
+/**
+ * This function converts a GList into a NULL terminated array of char pointers.
+ * This array is stored into the given GSettings key.
+ * @returns The return value of g_settings_set_strv if the length of the GList is > 0, else true.
+ */
+gboolean GnomeCmdData::set_gsettings_string_array_from_glist (GSettings *settings, const gchar *key, GList 
*strings)
+{
+    gboolean rv = true;
+    guint number_of_strings = g_list_length (strings);
+    if (number_of_strings > 0)
+    {
+        gint ii;
+        gchar** str_array;
+        str_array = (gchar**) g_malloc ((number_of_strings + 1) * sizeof(char*));
+        GList *str_list = strings;
+
+        // build up a char array for storage in GSettings
+        for (ii = 0; str_list; str_list = str_list->next, ++ii)
+        {
+            str_array[ii] = g_strdup((const gchar*) str_list->data);
+        }
+        str_array[ii] = NULL;
+
+        // store the NULL terminated str_array in GSettings
+        rv = g_settings_set_strv(settings, key, str_array);
+
+        g_free(str_array);
+    }
+    return rv;
+}
+
 
 inline void GnomeCmdData::save_cmdline_history()
 {
@@ -2447,6 +2478,25 @@ inline GList* GnomeCmdData::load_string_history (const gchar *format, gint size)
 }
 
 
+inline GList* GnomeCmdData::get_list_from_gsettings_string_array (GSettings *settings, const gchar *key)
+{
+    GList *list = NULL;
+
+    gchar** gsettings_array;
+    gsettings_array = g_settings_get_strv (settings, key);
+
+    for(gint i = 0; gsettings_array[i]; ++i)
+    {
+        gchar *value;
+        value = g_strdup (gsettings_array[i]);
+        list = g_list_append (list, value);
+    }
+    g_free(gsettings_array);
+
+    return list;
+}
+
+
 inline void GnomeCmdData::load_cmdline_history()
 {
     cmdline_history = load_string_history ("/cmdline-history/line%d", -1);
diff --git a/src/gnome-cmd-data.h b/src/gnome-cmd-data.h
index 3a4ce4f..b3d460d 100644
--- a/src/gnome-cmd-data.h
+++ b/src/gnome-cmd-data.h
@@ -599,6 +599,8 @@ struct GnomeCmdData
 
     GList                       *cmdline_history;
     gint                         cmdline_history_length;
+    GList                       *get_list_from_gsettings_string_array (GSettings *settings, const gchar 
*key);
+    gboolean                     set_gsettings_string_array_from_glist (GSettings *settings, const gchar 
*key, GList *strings);
 
     gboolean                     use_gcmd_block;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]