[gnome-online-accounts/gnome-3-18] daemon, utils: Simplify saving a GKeyFile to a path



commit a93dc2ebfe4cb851852e1f9bec3c31012cb31848
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed May 4 16:59:28 2016 +0200

    daemon, utils: Simplify saving a GKeyFile to a path
    
    We can now use g_key_file_save_to_file which wraps g_key_file_to_data
    and g_file_set_contents.
    
    At the moment, g_key_file_to_data is documented as never throwing an
    error. So we don't lose anything in terms of error reporting. Even if
    this changes in the future, I think we can live without a separate
    error message for g_key_file_to_data.
    
    Bump minimum GLib version to 2.40.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=688041

 configure.ac              |    2 +-
 src/daemon/goadaemon.c    |   37 ++-----------------------------------
 src/goabackend/goautils.c |   24 +++---------------------
 3 files changed, 6 insertions(+), 57 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index f41e53d..9922372 100644
--- a/configure.ac
+++ b/configure.ac
@@ -85,7 +85,7 @@ GTK_DOC_CHECK([1.3])
 # Libraries
 #
 
-PKG_CHECK_MODULES(GLIB, [glib-2.0 gio-2.0 gio-unix-2.0 >= 2.35])
+PKG_CHECK_MODULES(GLIB, [glib-2.0 gio-2.0 gio-unix-2.0 >= 2.40])
 AC_SUBST(GLIB_CFLAGS)
 AC_SUBST(GLIB_LIBS)
 
diff --git a/src/daemon/goadaemon.c b/src/daemon/goadaemon.c
index 4ae13dc..0e04be7 100644
--- a/src/daemon/goadaemon.c
+++ b/src/daemon/goadaemon.c
@@ -910,23 +910,8 @@ get_all_providers_cb (GObject      *source,
       g_key_file_set_string (key_file, group, key, value);
     }
 
-  g_free (key_file_data);
-  error = NULL;
-  key_file_data = g_key_file_to_data (key_file,
-                                      &length,
-                                      &error);
-  if (key_file_data == NULL)
-    {
-      g_prefix_error (&error, "Error generating key-value-file: ");
-      g_dbus_method_invocation_take_error (data->invocation, error);
-      goto out;
-    }
-
   error = NULL;
-  if (!g_file_set_contents (path,
-                            key_file_data,
-                            length,
-                            &error))
+  if (!g_key_file_save_to_file (key_file, path, &error))
     {
       g_prefix_error (&error, "Error writing key-value-file %s: ", path);
       g_dbus_method_invocation_take_error (data->invocation, error);
@@ -1007,8 +992,6 @@ on_account_handle_remove (GoaAccount            *account,
   const gchar *provider_type;
   gchar *path;
   gchar *group;
-  gchar *data;
-  gsize length;
   GError *error;
 
   provider = NULL;
@@ -1016,7 +999,6 @@ on_account_handle_remove (GoaAccount            *account,
   path = NULL;
   group = NULL;
   key_file = NULL;
-  data = NULL;
 
   if (goa_account_get_is_locked (account))
     {
@@ -1056,21 +1038,7 @@ on_account_handle_remove (GoaAccount            *account,
     }
 
   error = NULL;
-  data = g_key_file_to_data (key_file,
-                             &length,
-                             &error);
-  if (data == NULL)
-    {
-      g_prefix_error (&error, "Error generating key-value-file: ");
-      g_dbus_method_invocation_take_error (invocation, error);
-      goto out;
-    }
-
-  error = NULL;
-  if (!g_file_set_contents (path,
-                            data,
-                            length,
-                            &error))
+  if (!g_key_file_save_to_file (key_file, path, &error))
     {
       g_prefix_error (&error, "Error writing key-value-file %s: ", path);
       g_dbus_method_invocation_take_error (invocation, error);
@@ -1115,7 +1083,6 @@ on_account_handle_remove (GoaAccount            *account,
 
  out:
   g_clear_object (&provider);
-  g_free (data);
   g_clear_pointer (&key_file, (GDestroyNotify) g_key_file_free);
   g_free (group);
   g_free (path);
diff --git a/src/goabackend/goautils.c b/src/goabackend/goautils.c
index deb8236..6dbd53a 100644
--- a/src/goabackend/goautils.c
+++ b/src/goabackend/goautils.c
@@ -380,12 +380,8 @@ goa_utils_keyfile_remove_key (GoaAccount *account, const gchar *key)
 {
   GError *error;
   GKeyFile *key_file;
-  gchar *contents;
   gchar *group;
   gchar *path;
-  gsize length;
-
-  contents = NULL;
 
   path = g_strdup_printf ("%s/goa-1.0/accounts.conf", g_get_user_config_dir ());
   group = g_strdup_printf ("Account %s", goa_account_get_id (account));
@@ -407,10 +403,9 @@ goa_utils_keyfile_remove_key (GoaAccount *account, const gchar *key)
     }
 
   g_key_file_remove_key (key_file, group, key, NULL);
-  contents = g_key_file_to_data (key_file, &length, NULL);
 
   error = NULL;
-  if (!g_file_set_contents (path, contents, length, &error))
+  if (!g_key_file_save_to_file (key_file, path, &error))
     {
       g_prefix_error (&error, "Error writing key-value-file %s: ", path);
       g_warning ("%s (%s, %d)", error->message, g_quark_to_string (error->domain), error->code);
@@ -419,7 +414,6 @@ goa_utils_keyfile_remove_key (GoaAccount *account, const gchar *key)
     }
 
  out:
-  g_free (contents);
   g_key_file_free (key_file);
   g_free (group);
   g_free (path);
@@ -430,12 +424,8 @@ goa_utils_keyfile_set_boolean (GoaAccount *account, const gchar *key, gboolean v
 {
   GError *error;
   GKeyFile *key_file;
-  gchar *contents;
   gchar *group;
   gchar *path;
-  gsize length;
-
-  contents = NULL;
 
   path = g_strdup_printf ("%s/goa-1.0/accounts.conf", g_get_user_config_dir ());
   group = g_strdup_printf ("Account %s", goa_account_get_id (account));
@@ -457,10 +447,9 @@ goa_utils_keyfile_set_boolean (GoaAccount *account, const gchar *key, gboolean v
     }
 
   g_key_file_set_boolean (key_file, group, key, value);
-  contents = g_key_file_to_data (key_file, &length, NULL);
 
   error = NULL;
-  if (!g_file_set_contents (path, contents, length, &error))
+  if (!g_key_file_save_to_file (key_file, path, &error))
     {
       g_prefix_error (&error, "Error writing key-value-file %s: ", path);
       g_warning ("%s (%s, %d)", error->message, g_quark_to_string (error->domain), error->code);
@@ -469,7 +458,6 @@ goa_utils_keyfile_set_boolean (GoaAccount *account, const gchar *key, gboolean v
     }
 
  out:
-  g_free (contents);
   g_key_file_free (key_file);
   g_free (group);
   g_free (path);
@@ -480,12 +468,8 @@ goa_utils_keyfile_set_string (GoaAccount *account, const gchar *key, const gchar
 {
   GError *error;
   GKeyFile *key_file;
-  gchar *contents;
   gchar *group;
   gchar *path;
-  gsize length;
-
-  contents = NULL;
 
   path = g_strdup_printf ("%s/goa-1.0/accounts.conf", g_get_user_config_dir ());
   group = g_strdup_printf ("Account %s", goa_account_get_id (account));
@@ -507,10 +491,9 @@ goa_utils_keyfile_set_string (GoaAccount *account, const gchar *key, const gchar
     }
 
   g_key_file_set_string (key_file, group, key, value);
-  contents = g_key_file_to_data (key_file, &length, NULL);
 
   error = NULL;
-  if (!g_file_set_contents (path, contents, length, &error))
+  if (!g_key_file_save_to_file (key_file, path, &error))
     {
       g_prefix_error (&error, "Error writing key-value-file %s: ", path);
       g_warning ("%s (%s, %d)", error->message, g_quark_to_string (error->domain), error->code);
@@ -519,7 +502,6 @@ goa_utils_keyfile_set_string (GoaAccount *account, const gchar *key, const gchar
     }
 
  out:
-  g_free (contents);
   g_key_file_free (key_file);
   g_free (group);
   g_free (path);


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