[gnome-initial-setup/more-vendor-config-hooks: 34/35] driver: Load and add the vendor configuration file



commit ed70998ef446c3a056fd1b713c0443e30eaddf8d
Author: Joaquim Rocha <jrocha endlessm com>
Date:   Thu Jul 12 12:35:10 2018 +0200

    driver: Load and add the vendor configuration file
    
    The vendor configuration file was being loaded by the main file and just
    used for skipping pages. However, by adding it to the driver, it can be
    accessed by any page, which allows us to add page specific options to
    the file and only process in the pages' code.
    
    We also abstract the specifics of the configuration in GisDriver so that
    pages don't have to deal with the key file directly, as that would lead
    to a lot of repeated code (for error checking and such).

 gnome-initial-setup/gis-driver.c          | 96 +++++++++++++++++++++++++++++++
 gnome-initial-setup/gis-driver.h          | 16 ++++++
 gnome-initial-setup/gnome-initial-setup.c | 41 +++++--------
 3 files changed, 125 insertions(+), 28 deletions(-)
---
diff --git a/gnome-initial-setup/gis-driver.c b/gnome-initial-setup/gis-driver.c
index 64fe7ba..f90d8c1 100644
--- a/gnome-initial-setup/gis-driver.c
+++ b/gnome-initial-setup/gis-driver.c
@@ -87,6 +87,8 @@ struct _GisDriverPrivate {
   gboolean small_screen;
 
   locale_t locale;
+
+  GKeyFile *vendor_conf_file;
 };
 typedef struct _GisDriverPrivate GisDriverPrivate;
 
@@ -116,6 +118,7 @@ gis_driver_finalize (GObject *object)
   g_free (priv->user_password);
 
   g_clear_object (&priv->user_account);
+  g_clear_pointer (&priv->vendor_conf_file, g_key_file_free);
 
   if (priv->locale != (locale_t) 0)
     {
@@ -305,6 +308,97 @@ gis_driver_hide_window (GisDriver *driver)
   gtk_widget_hide (GTK_WIDGET (priv->main_window));
 }
 
+static void
+load_vendor_conf_file (GisDriver *driver)
+{
+  GisDriverPrivate *priv = gis_driver_get_instance_private (driver);
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GKeyFile) vendor_conf_file = g_key_file_new ();
+
+  if(!g_key_file_load_from_file (vendor_conf_file, VENDOR_CONF_FILE,
+                                 G_KEY_FILE_NONE, &error))
+    {
+      if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+        g_warning ("Could not read file %s: %s", VENDOR_CONF_FILE, error->message);
+      return;
+    }
+
+  priv->vendor_conf_file = g_steal_pointer (&vendor_conf_file);
+}
+
+static void
+report_conf_error_if_needed (const gchar *group,
+                             const gchar *key,
+                             const GError *error)
+{
+  if (!g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) &&
+      !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND))
+    g_warning ("Error getting the value for key '%s' of group [%s] in "
+               "%s: %s", group, key, VENDOR_CONF_FILE, error->message);
+}
+
+gboolean
+gis_driver_conf_get_boolean (GisDriver *driver,
+                             const gchar *group,
+                             const gchar *key,
+                             gboolean default_value)
+{
+  GisDriverPrivate *priv = gis_driver_get_instance_private (driver);
+
+  if (priv->vendor_conf_file) {
+    g_autoptr(GError) error = NULL;
+    gboolean new_value = g_key_file_get_boolean (priv->vendor_conf_file, group,
+                                                 key, &error);
+    if (error == NULL)
+      return new_value;
+
+    report_conf_error_if_needed (group, key, error);
+  }
+
+  return default_value;
+}
+
+GStrv
+gis_driver_conf_get_string_list (GisDriver *driver,
+                                 const gchar *group,
+                                 const gchar *key,
+                                 gsize *out_length)
+{
+  GisDriverPrivate *priv = gis_driver_get_instance_private (driver);
+
+  if (priv->vendor_conf_file) {
+    g_autoptr(GError) error = NULL;
+    GStrv new_value = g_key_file_get_string_list (priv->vendor_conf_file, group,
+                                                  key, out_length, &error);
+    if (error == NULL)
+      return new_value;
+
+    report_conf_error_if_needed (group, key, error);
+  }
+
+  return NULL;
+}
+
+gchar *
+gis_driver_conf_get_string (GisDriver *driver,
+                            const gchar *group,
+                            const gchar *key)
+{
+  GisDriverPrivate *priv = gis_driver_get_instance_private (driver);
+
+  if (priv->vendor_conf_file) {
+    g_autoptr(GError) error = NULL;
+    gchar *new_value = g_key_file_get_string (priv->vendor_conf_file, group,
+                                              key, &error);
+    if (error == NULL)
+      return new_value;
+
+    report_conf_error_if_needed (group, key, error);
+  }
+
+  return NULL;
+}
+
 GisDriverMode
 gis_driver_get_mode (GisDriver *driver)
 {
@@ -581,6 +675,8 @@ gis_driver_init (GisDriver *driver)
 
   set_small_screen_based_on_primary_monitor (driver);
 
+  load_vendor_conf_file (driver);
+
   g_signal_connect (screen, "size-changed",
                     G_CALLBACK (screen_size_changed), driver);
 }
diff --git a/gnome-initial-setup/gis-driver.h b/gnome-initial-setup/gis-driver.h
index 1184582..cd49ffd 100644
--- a/gnome-initial-setup/gis-driver.h
+++ b/gnome-initial-setup/gis-driver.h
@@ -98,6 +98,8 @@ GisDriverMode gis_driver_get_mode (GisDriver *driver);
 
 gboolean gis_driver_is_small_screen (GisDriver *driver);
 
+GKeyFile *gis_driver_get_vendor_conf_file (GisDriver *driver);
+
 void gis_driver_add_page (GisDriver *driver,
                           GisPage   *page);
 
@@ -105,6 +107,20 @@ void gis_driver_hide_window (GisDriver *driver);
 
 void gis_driver_save_data (GisDriver *driver);
 
+gboolean gis_driver_conf_get_boolean (GisDriver *driver,
+                                      const gchar *group,
+                                      const gchar *key,
+                                      gboolean default_value);
+
+GStrv gis_driver_conf_get_string_list (GisDriver *driver,
+                                       const gchar *group,
+                                       const gchar *key,
+                                       gsize *out_length);
+
+gchar *gis_driver_conf_get_string (GisDriver *driver,
+                                   const gchar *group,
+                                   const gchar *key);
+
 GisDriver *gis_driver_new (GisDriverMode mode);
 
 G_END_DECLS
diff --git a/gnome-initial-setup/gnome-initial-setup.c b/gnome-initial-setup/gnome-initial-setup.c
index e2136de..e42639e 100644
--- a/gnome-initial-setup/gnome-initial-setup.c
+++ b/gnome-initial-setup/gnome-initial-setup.c
@@ -117,16 +117,14 @@ strv_append (gchar **a,
 }
 
 static gchar **
-pages_to_skip_from_file (gboolean is_new_user)
+pages_to_skip_from_file (GisDriver *driver,
+                         gboolean   is_new_user)
 {
-  GKeyFile *skip_pages_file;
-  gchar **skip_pages = NULL;
-  gchar **additional_skip_pages = NULL;
-  GError *error = NULL;
+  GStrv skip_pages = NULL;
+  GStrv additional_skip_pages = NULL;
 
-  /* VENDOR_CONF_FILE points to a keyfile containing vendor customization
-   * options. This code will look for options under the "pages" group, and
-   * supports the following keys:
+  /* This code will read the keyfile containing vendor customization options and
+   * look for options under the "pages" group, and supports the following keys:
    *   - skip (optional): list of pages to be skipped always
    *   - new_user_only (optional): list of pages to be skipped in existing user mode
    *   - existing_user_only (optional): list of pages to be skipped in new user mode
@@ -137,23 +135,13 @@ pages_to_skip_from_file (gboolean is_new_user)
    *   skip=timezone
    *   existing_user_only=language;keyboard
    */
-  skip_pages_file = g_key_file_new ();
-  if (!g_key_file_load_from_file (skip_pages_file, VENDOR_CONF_FILE,
-                                  G_KEY_FILE_NONE, &error)) {
-    if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
-      g_warning ("Could not read file %s: %s", VENDOR_CONF_FILE, error->message);
-
-    g_error_free (error);
-    goto out;
-  }
 
-  skip_pages = g_key_file_get_string_list (skip_pages_file,
-                                           VENDOR_PAGES_GROUP,
-                                           VENDOR_SKIP_KEY, NULL, NULL);
-  additional_skip_pages = g_key_file_get_string_list (skip_pages_file,
-                                                      VENDOR_PAGES_GROUP,
-                                                      is_new_user ? VENDOR_EXISTING_USER_ONLY_KEY : 
VENDOR_NEW_USER_ONLY_KEY,
-                                                      NULL, NULL);
+  skip_pages = gis_driver_conf_get_string_list (driver, VENDOR_PAGES_GROUP,
+                                                VENDOR_SKIP_KEY, NULL);
+  additional_skip_pages =
+       gis_driver_conf_get_string_list (driver, VENDOR_PAGES_GROUP,
+                                     is_new_user ? VENDOR_EXISTING_USER_ONLY_KEY : VENDOR_NEW_USER_ONLY_KEY,
+                                     NULL);
 
   if (!skip_pages && additional_skip_pages) {
     skip_pages = additional_skip_pages;
@@ -162,9 +150,6 @@ pages_to_skip_from_file (gboolean is_new_user)
     g_strfreev (additional_skip_pages);
   }
 
- out:
-  g_key_file_free (skip_pages_file);
-
   return skip_pages;
 }
 
@@ -215,7 +200,7 @@ rebuild_pages_cb (GisDriver *driver)
   }
 
   is_new_user = (gis_driver_get_mode (driver) == GIS_DRIVER_MODE_NEW_USER);
-  skip_pages = pages_to_skip_from_file (is_new_user);
+  skip_pages = pages_to_skip_from_file (driver, is_new_user);
 
   for (; page_data->page_id != NULL; ++page_data) {
     skipped = FALSE;


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