[gnome-initial-setup/parental-controls: 8/8] WIP: avatars in page header



commit fbd0c4afecf370311d3e2994d34d6cf989dcb779
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Feb 5 15:39:48 2020 +0000

    WIP: avatars in page header
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 gnome-initial-setup/gis-driver.c                   | 42 +++++++++++++++++++++-
 gnome-initial-setup/gis-driver.h                   |  4 +++
 gnome-initial-setup/gis-page-header.c              | 21 ++++++++---
 gnome-initial-setup/gis-page.c                     |  1 +
 .../pages/account/gis-account-page-local.c         | 31 +++++++++++-----
 .../parental-controls/gis-parental-controls-page.c |  9 ++++-
 .../gis-parental-controls-page.ui                  |  1 -
 .../pages/password/gis-password-page.c             | 15 +++++++-
 .../pages/password/gis-password-page.ui            |  1 -
 9 files changed, 107 insertions(+), 18 deletions(-)
---
diff --git a/gnome-initial-setup/gis-driver.c b/gnome-initial-setup/gis-driver.c
index fff43d5..38a13f8 100644
--- a/gnome-initial-setup/gis-driver.c
+++ b/gnome-initial-setup/gis-driver.c
@@ -63,6 +63,7 @@ enum {
   PROP_USERNAME,
   PROP_SMALL_SCREEN,
   PROP_FULL_NAME,
+  PROP_AVATAR,
   PROP_LAST,
 };
 
@@ -88,6 +89,8 @@ struct _GisDriverPrivate {
   gchar *username;
   gchar *full_name;  /* (owned) (nullable) */
 
+  GdkPixbuf *avatar;  /* (owned) (nullable) */
+
   GisDriverMode mode;
   UmAccountMode account_mode;
   gboolean small_screen;
@@ -124,6 +127,8 @@ gis_driver_finalize (GObject *object)
   g_free (priv->full_name);
   g_free (priv->user_password);
 
+  g_clear_object (&priv->avatar);
+
   g_clear_object (&priv->user_account);
   g_clear_pointer (&priv->vendor_conf_file, g_key_file_free);
 
@@ -281,6 +286,29 @@ gis_driver_get_full_name (GisDriver *driver)
   return priv->full_name;
 }
 
+/* TODO docs */
+void
+gis_driver_set_avatar (GisDriver *driver,
+                       GdkPixbuf *avatar)
+{
+  GisDriverPrivate *priv = gis_driver_get_instance_private (driver);
+  g_return_if_fail (GIS_IS_DRIVER (driver));
+  g_return_if_fail (avatar == NULL || GDK_IS_PIXBUF (avatar));
+
+  if (g_set_object (&priv->avatar, avatar))
+    g_object_notify (G_OBJECT (driver), "avatar");
+}
+
+/* TODO docs */
+GdkPixbuf *
+gis_driver_get_avatar (GisDriver *driver)
+{
+  GisDriverPrivate *priv = gis_driver_get_instance_private (driver);
+  g_return_val_if_fail (GIS_IS_DRIVER (driver), NULL);
+
+  return priv->avatar;
+}
+
 void
 gis_driver_set_user_permissions (GisDriver   *driver,
                                  ActUser     *user,
@@ -537,6 +565,9 @@ gis_driver_get_property (GObject      *object,
     case PROP_FULL_NAME:
       g_value_set_string (value, priv->full_name);
       break;
+    case PROP_AVATAR:
+      g_value_set_object (value, priv->avatar);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -563,7 +594,10 @@ gis_driver_set_property (GObject      *object,
     case PROP_FULL_NAME:
       gis_driver_set_full_name (driver, g_value_get_string (value));
       break;
-   default:
+    case PROP_AVATAR:
+      gis_driver_set_avatar (driver, g_value_get_object (value));
+      break;
+    default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
     }
@@ -837,6 +871,12 @@ gis_driver_class_init (GisDriverClass *klass)
                          NULL,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
 
+  /* TODO docs */
+  obj_props[PROP_AVATAR] =
+    g_param_spec_object ("avatar", "", "",
+                         GDK_TYPE_PIXBUF,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
   g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
 }
 
diff --git a/gnome-initial-setup/gis-driver.h b/gnome-initial-setup/gis-driver.h
index 6571a1d..aace51d 100644
--- a/gnome-initial-setup/gis-driver.h
+++ b/gnome-initial-setup/gis-driver.h
@@ -107,6 +107,10 @@ void gis_driver_set_full_name (GisDriver   *driver,
                                const gchar *full_name);
 const gchar *gis_driver_get_full_name (GisDriver *driver);
 
+void gis_driver_set_avatar (GisDriver *driver,
+                            GdkPixbuf *avatar);
+GdkPixbuf *gis_driver_get_avatar (GisDriver *driver);
+
 gboolean gis_driver_get_gdm_objects (GisDriver        *driver,
                                      GdmGreeter      **greeter,
                                      GdmUserVerifier **user_verifier);
diff --git a/gnome-initial-setup/gis-page-header.c b/gnome-initial-setup/gis-page-header.c
index 0b1a5af..4f48f6b 100644
--- a/gnome-initial-setup/gis-page-header.c
+++ b/gnome-initial-setup/gis-page-header.c
@@ -31,6 +31,7 @@ enum {
   PROP_TITLE,
   PROP_SUBTITLE,
   PROP_ICON_NAME,
+  PROP_PIXBUF,
   PROP_SHOW_ICON,
   PROP_LAST,
 };
@@ -80,7 +81,6 @@ gis_page_header_get_property (GObject    *object,
                               GParamSpec *pspec)
 {
   GisPageHeader *header = GIS_PAGE_HEADER (object);
-  gchar *icon_name = NULL;
 
   switch (prop_id)
     {
@@ -93,8 +93,11 @@ gis_page_header_get_property (GObject    *object,
       break;
 
     case PROP_ICON_NAME:
-      g_object_get (header->icon, "icon-name", &icon_name, NULL);
-      g_value_take_string (value, icon_name);
+      g_object_get_property (header->icon, "icon-name", value);
+      break;
+
+    case PROP_PIXBUF:
+      g_object_get_property (header->icon, "pixbuf", value);
       break;
 
     case PROP_SHOW_ICON:
@@ -128,7 +131,11 @@ gis_page_header_set_property (GObject      *object,
       break;
 
     case PROP_ICON_NAME:
-      g_object_set (header->icon, "icon-name", g_value_get_string (value), NULL);
+      g_object_set_property (header->icon, "icon-name", value);
+      break;
+
+    case PROP_PIXBUF:
+      g_object_set_property (header->icon, "pixbuf", value);
       break;
 
     case PROP_SHOW_ICON:
@@ -174,6 +181,12 @@ gis_page_header_class_init (GisPageHeaderClass *klass)
                          NULL,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
 
+  obj_props[PROP_PIXBUF] =
+    g_param_spec_object ("pixbuf",
+                         "", "",
+                         GDK_TYPE_PIXBUF,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
   obj_props[PROP_SHOW_ICON] =
     g_param_spec_boolean ("show-icon",
                           "", "",
diff --git a/gnome-initial-setup/gis-page.c b/gnome-initial-setup/gis-page.c
index 8564935..1439f83 100644
--- a/gnome-initial-setup/gis-page.c
+++ b/gnome-initial-setup/gis-page.c
@@ -390,3 +390,4 @@ gis_page_skip (GisPage *page)
     return GIS_PAGE_GET_CLASS (page)->skip (page);
   return TRUE;
 }
+
diff --git a/gnome-initial-setup/pages/account/gis-account-page-local.c 
b/gnome-initial-setup/pages/account/gis-account-page-local.c
index 54c55e5..59fd768 100644
--- a/gnome-initial-setup/pages/account/gis-account-page-local.c
+++ b/gnome-initial-setup/pages/account/gis-account-page-local.c
@@ -356,28 +356,29 @@ avatar_callback (GdkPixbuf   *pixbuf,
 {
   GisAccountPageLocal *page = user_data;
   GisAccountPageLocalPrivate *priv = gis_account_page_local_get_instance_private (page);
-  GdkPixbuf *tmp, *rounded;
+  g_autoptr(GdkPixbuf) tmp = NULL;
+  g_autoptr(GdkPixbuf) rounded = NULL;
 
   g_clear_object (&priv->avatar_pixbuf);
-  g_free (priv->avatar_filename);
-  priv->avatar_filename = NULL;
+  g_clear_pointer (&priv->avatar_filename, g_free);
 
   if (pixbuf) {
     priv->avatar_pixbuf = g_object_ref (pixbuf);
-    tmp = round_image (pixbuf);
-    gtk_image_set_from_pixbuf (GTK_IMAGE (priv->avatar_image), tmp);
-    g_object_unref (tmp);
+    rounded = round_image (pixbuf);
   }
   else if (filename) {
     priv->avatar_filename = g_strdup (filename);
     tmp = gdk_pixbuf_new_from_file_at_size (filename, 96, 96, NULL);
-    rounded = round_image (tmp);
-    g_object_unref (tmp);
 
+    if (tmp != NULL)
+      rounded = round_image (tmp);
+  }
+
+  if (rounded != NULL) {
     gtk_image_set_from_pixbuf (GTK_IMAGE (priv->avatar_image), rounded);
-    g_object_unref (rounded);
   }
   else {
+    /* Fallback. */
     gtk_image_set_pixel_size (GTK_IMAGE (priv->avatar_image), 96);
     gtk_image_set_from_icon_name (GTK_IMAGE (priv->avatar_image), "avatar-default-symbolic", 1);
   }
@@ -642,6 +643,18 @@ gis_account_page_local_apply (GisAccountPageLocal *local, GisPage *page)
   full_name = gtk_entry_get_text (GTK_ENTRY (priv->fullname_entry));
   gis_driver_set_full_name (GIS_PAGE (page)->driver, full_name);
 
+  if (priv->avatar_pixbuf != NULL)
+    {
+      gis_driver_set_avatar (GIS_PAGE (page)->driver, priv->avatar_pixbuf);
+    }
+  else if (priv->avatar_filename != NULL)
+    {
+      g_autoptr(GdkPixbuf) pixbuf = NULL;
+
+      pixbuf = gdk_pixbuf_new_from_file_at_size (priv->avatar_filename, 96, 96, NULL);
+      gis_driver_set_avatar (GIS_PAGE (page)->driver, pixbuf);
+    }
+
   parental_controls_enabled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON 
(priv->enable_parental_controls_check_button));
   gis_driver_set_parental_controls_enabled (GIS_PAGE (page)->driver, parental_controls_enabled);
 
diff --git a/gnome-initial-setup/pages/parental-controls/gis-parental-controls-page.c 
b/gnome-initial-setup/pages/parental-controls/gis-parental-controls-page.c
index 95be685..0a73b70 100644
--- a/gnome-initial-setup/pages/parental-controls/gis-parental-controls-page.c
+++ b/gnome-initial-setup/pages/parental-controls/gis-parental-controls-page.c
@@ -100,17 +100,24 @@ static void
 update_header (GisParentalControlsPage *page)
 {
   g_autofree gchar *title = NULL;
-  const gchar *subtitle;
+  const gchar *subtitle, *icon_name;
+  GdkPixbuf *pixbuf;
 
   /* Translators: The placeholder is the user’s full name. */
   title = g_strdup_printf (_("Parental Controls for %s"),
                            gis_driver_get_full_name (GIS_PAGE (page)->driver));
   subtitle = _("TODO blasd farsd hngh wuf");
+  pixbuf = gis_driver_get_avatar (GIS_PAGE (page)->driver);
+  icon_name = (pixbuf != NULL) ? NULL : "dialog-password-symbolic";
 
   g_object_set (G_OBJECT (page->header),
                 "title", title,
                 "subtitle", subtitle,
                 NULL);
+  if (pixbuf != NULL)
+    g_object_set (G_OBJECT (priv->header), "pixbuf", pixbuf, NULL);
+  else if (icon_name != NULL)
+    g_object_set (G_OBJECT (priv->header), "icon-name", icon_name, NULL);
 }
 
 static void
diff --git a/gnome-initial-setup/pages/parental-controls/gis-parental-controls-page.ui 
b/gnome-initial-setup/pages/parental-controls/gis-parental-controls-page.ui
index dee401b..8187632 100644
--- a/gnome-initial-setup/pages/parental-controls/gis-parental-controls-page.ui
+++ b/gnome-initial-setup/pages/parental-controls/gis-parental-controls-page.ui
@@ -13,7 +13,6 @@
             <property name="visible">True</property>
             <property name="margin-top">24</property>
             <!-- title and subtitle are set in code, so are not set here -->
-            <!-- TODO: Would be good to use the user avatar instead of an icon -->
             <property name="icon-name">dialog-password-symbolic</property>
             <property name="show-icon" bind-source="GisParentalControlsPage" bind-property="small-screen" 
bind-flags="invert-boolean|sync-create"/>
           </object>
diff --git a/gnome-initial-setup/pages/password/gis-password-page.c 
b/gnome-initial-setup/pages/password/gis-password-page.c
index 48b6052..32358e8 100644
--- a/gnome-initial-setup/pages/password/gis-password-page.c
+++ b/gnome-initial-setup/pages/password/gis-password-page.c
@@ -70,7 +70,8 @@ update_header (GisPasswordPage *page)
 {
   GisPasswordPagePrivate *priv = gis_password_page_get_instance_private (page);
   g_autofree gchar *title = NULL;
-  const gchar *subtitle;
+  const gchar *subtitle, *icon_name;
+  GdkPixbuf *pixbuf;
 
   if (!priv->parent_mode)
     {
@@ -78,17 +79,29 @@ update_header (GisPasswordPage *page)
       title = g_strdup_printf (_("Set a Password for %s"),
                                gis_driver_get_full_name (GIS_PAGE (page)->driver));
       subtitle = _("Be careful not to lose your password.");
+      pixbuf = gis_driver_get_avatar (GIS_PAGE (page)->driver);
+      icon_name = (pixbuf != NULL) ? NULL : "dialog-password-symbolic";
     }
   else
     {
       title = g_strdup (_("Set a Parent Password"));
       subtitle = _("This password will control access to the parental controls for the system.");
+      /* TODO: Can we get a different icon for the parent password? */
+      icon_name = "dialog-password-symbolic";
+      pixbuf = NULL;
     }
 
+  /* Doesn’t make sense to set both. */
+  g_assert (icon_name == NULL || pixbuf == NULL);
+
   g_object_set (G_OBJECT (priv->header),
                 "title", title,
                 "subtitle", subtitle,
                 NULL);
+  if (pixbuf != NULL)
+    g_object_set (G_OBJECT (priv->header), "pixbuf", pixbuf, NULL);
+  else if (icon_name != NULL)
+    g_object_set (G_OBJECT (priv->header), "icon-name", icon_name, NULL);
 }
 
 static void
diff --git a/gnome-initial-setup/pages/password/gis-password-page.ui 
b/gnome-initial-setup/pages/password/gis-password-page.ui
index b773036..b21010c 100644
--- a/gnome-initial-setup/pages/password/gis-password-page.ui
+++ b/gnome-initial-setup/pages/password/gis-password-page.ui
@@ -14,7 +14,6 @@
             <property name="visible">True</property>
             <property name="margin_top">24</property>
             <!-- title and subtitle are set in code, so are not set here -->
-            <!-- TODO: Can we get a different icon for the parent password? -->
             <property name="icon_name">dialog-password-symbolic</property>
             <property name="show_icon" bind-source="GisPasswordPage" bind-property="small-screen" 
bind-flags="invert-boolean|sync-create"/>
           </object>


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