restrict-applications-dialog: Drop accountsservice dependency

Instead of taking an `ActUser` as a property of the
`MctRestrictApplicationsDialog`, take the display name which we would
have queried from it.

This will allow the restrict applications dialog to be used in
situations where an `ActUser` isn’t available, such as when setting the
parental controls for a yet-to-be-created user.

This breaks the `MctRestrictApplicationsDialog` API, but the previous
API hadn’t yet been released.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2020-02-06 11:54:44 +00:00
parent acf402dcc5
commit 6b45ad9f76
3 changed files with 79 additions and 67 deletions

View File

@ -19,7 +19,6 @@
* - Philip Withnall <withnall@endlessm.com> * - Philip Withnall <withnall@endlessm.com>
*/ */
#include <act/act.h>
#include <gio/gio.h> #include <gio/gio.h>
#include <glib.h> #include <glib.h>
#include <glib-object.h> #include <glib-object.h>
@ -55,7 +54,7 @@ struct _MctRestrictApplicationsDialog
GtkLabel *description; GtkLabel *description;
MctAppFilter *app_filter; /* (owned) (not nullable) */ MctAppFilter *app_filter; /* (owned) (not nullable) */
ActUser *user; /* (owned) (nullable) */ gchar *user_display_name; /* (owned) (nullable) */
}; };
G_DEFINE_TYPE (MctRestrictApplicationsDialog, mct_restrict_applications_dialog, GTK_TYPE_DIALOG) G_DEFINE_TYPE (MctRestrictApplicationsDialog, mct_restrict_applications_dialog, GTK_TYPE_DIALOG)
@ -63,10 +62,10 @@ G_DEFINE_TYPE (MctRestrictApplicationsDialog, mct_restrict_applications_dialog,
typedef enum typedef enum
{ {
PROP_APP_FILTER = 1, PROP_APP_FILTER = 1,
PROP_USER, PROP_USER_DISPLAY_NAME,
} MctRestrictApplicationsDialogProperty; } MctRestrictApplicationsDialogProperty;
static GParamSpec *properties[PROP_USER + 1]; static GParamSpec *properties[PROP_USER_DISPLAY_NAME + 1];
static void static void
mct_restrict_applications_dialog_constructed (GObject *obj) mct_restrict_applications_dialog_constructed (GObject *obj)
@ -74,7 +73,9 @@ mct_restrict_applications_dialog_constructed (GObject *obj)
MctRestrictApplicationsDialog *self = MCT_RESTRICT_APPLICATIONS_DIALOG (obj); MctRestrictApplicationsDialog *self = MCT_RESTRICT_APPLICATIONS_DIALOG (obj);
g_assert (self->app_filter != NULL); g_assert (self->app_filter != NULL);
g_assert (self->user == NULL || ACT_IS_USER (self->user)); g_assert (self->user_display_name == NULL ||
(*self->user_display_name != '\0' &&
g_utf8_validate (self->user_display_name, -1, NULL)));
G_OBJECT_CLASS (mct_restrict_applications_dialog_parent_class)->constructed (obj); G_OBJECT_CLASS (mct_restrict_applications_dialog_parent_class)->constructed (obj);
} }
@ -93,8 +94,8 @@ mct_restrict_applications_dialog_get_property (GObject *object,
g_value_set_boxed (value, self->app_filter); g_value_set_boxed (value, self->app_filter);
break; break;
case PROP_USER: case PROP_USER_DISPLAY_NAME:
g_value_set_object (value, self->user); g_value_set_string (value, self->user_display_name);
break; break;
default: default:
@ -116,8 +117,8 @@ mct_restrict_applications_dialog_set_property (GObject *object,
mct_restrict_applications_dialog_set_app_filter (self, g_value_get_boxed (value)); mct_restrict_applications_dialog_set_app_filter (self, g_value_get_boxed (value));
break; break;
case PROP_USER: case PROP_USER_DISPLAY_NAME:
mct_restrict_applications_dialog_set_user (self, g_value_get_object (value)); mct_restrict_applications_dialog_set_user_display_name (self, g_value_get_string (value));
break; break;
default: default:
@ -131,7 +132,7 @@ mct_restrict_applications_dialog_dispose (GObject *object)
MctRestrictApplicationsDialog *self = (MctRestrictApplicationsDialog *)object; MctRestrictApplicationsDialog *self = (MctRestrictApplicationsDialog *)object;
g_clear_pointer (&self->app_filter, mct_app_filter_unref); g_clear_pointer (&self->app_filter, mct_app_filter_unref);
g_clear_object (&self->user); g_clear_pointer (&self->user_display_name, g_free);
G_OBJECT_CLASS (mct_restrict_applications_dialog_parent_class)->dispose (object); G_OBJECT_CLASS (mct_restrict_applications_dialog_parent_class)->dispose (object);
} }
@ -168,17 +169,21 @@ mct_restrict_applications_dialog_class_init (MctRestrictApplicationsDialogClass
G_PARAM_EXPLICIT_NOTIFY); G_PARAM_EXPLICIT_NOTIFY);
/** /**
* MctRestrictApplicationsDialog:user: (nullable) * MctRestrictApplicationsDialog:user-display-name: (nullable)
* *
* The currently selected user account, or %NULL if no user is selected. * The display name for the currently selected user account, or %NULL if no
* user is selected. This will typically be the users full name (if known)
* or their username.
*
* If set, it must be valid UTF-8 and non-empty.
* *
* Since: 0.5.0 * Since: 0.5.0
*/ */
properties[PROP_USER] = properties[PROP_USER_DISPLAY_NAME] =
g_param_spec_object ("user", g_param_spec_string ("user-display-name",
"User", "User Display Name",
"The currently selected user account, or %NULL if no user is selected.", "The display name for the currently selected user account, or %NULL if no user is selected.",
ACT_TYPE_USER, NULL,
G_PARAM_READWRITE | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS | G_PARAM_STATIC_STRINGS |
G_PARAM_EXPLICIT_NOTIFY); G_PARAM_EXPLICIT_NOTIFY);
@ -200,31 +205,12 @@ mct_restrict_applications_dialog_init (MctRestrictApplicationsDialog *self)
gtk_widget_init_template (GTK_WIDGET (self)); gtk_widget_init_template (GTK_WIDGET (self));
} }
static const gchar *
get_user_display_name (ActUser *user)
{
const gchar *display_name;
g_return_val_if_fail (ACT_IS_USER (user), _("unknown"));
display_name = act_user_get_real_name (user);
if (display_name != NULL)
return display_name;
display_name = act_user_get_user_name (user);
if (display_name != NULL)
return display_name;
/* Translators: this is the full name for an unknown user account. */
return _("unknown");
}
static void static void
update_description (MctRestrictApplicationsDialog *self) update_description (MctRestrictApplicationsDialog *self)
{ {
g_autofree gchar *description = NULL; g_autofree gchar *description = NULL;
if (self->user == NULL) if (self->user_display_name == NULL)
{ {
gtk_widget_hide (GTK_WIDGET (self->description)); gtk_widget_hide (GTK_WIDGET (self->description));
return; return;
@ -232,7 +218,7 @@ update_description (MctRestrictApplicationsDialog *self)
/* Translators: the placeholder is a users full name */ /* Translators: the placeholder is a users full name */
description = g_strdup_printf (_("Allow %s to use the following installed applications."), description = g_strdup_printf (_("Allow %s to use the following installed applications."),
get_user_display_name (self->user)); self->user_display_name);
gtk_label_set_text (self->description, description); gtk_label_set_text (self->description, description);
gtk_widget_show (GTK_WIDGET (self->description)); gtk_widget_show (GTK_WIDGET (self->description));
} }
@ -240,7 +226,8 @@ update_description (MctRestrictApplicationsDialog *self)
/** /**
* mct_restrict_applications_dialog_new: * mct_restrict_applications_dialog_new:
* @app_filter: (transfer none): the initial app filter configuration to show * @app_filter: (transfer none): the initial app filter configuration to show
* @user: (transfer none) (nullable): the user to show the app filter for * @user_display_name: (transfer none) (nullable): the display name of the user
* to show the app filter for, or %NULL if no user is selected
* *
* Create a new #MctRestrictApplicationsDialog widget. * Create a new #MctRestrictApplicationsDialog widget.
* *
@ -249,14 +236,16 @@ update_description (MctRestrictApplicationsDialog *self)
*/ */
MctRestrictApplicationsDialog * MctRestrictApplicationsDialog *
mct_restrict_applications_dialog_new (MctAppFilter *app_filter, mct_restrict_applications_dialog_new (MctAppFilter *app_filter,
ActUser *user) const gchar *user_display_name)
{ {
g_return_val_if_fail (app_filter != NULL, NULL); g_return_val_if_fail (app_filter != NULL, NULL);
g_return_val_if_fail (user == NULL || ACT_IS_USER (user), NULL); g_return_val_if_fail (user_display_name == NULL ||
(*user_display_name != '\0' &&
g_utf8_validate (user_display_name, -1, NULL)), NULL);
return g_object_new (MCT_TYPE_RESTRICT_APPLICATIONS_DIALOG, return g_object_new (MCT_TYPE_RESTRICT_APPLICATIONS_DIALOG,
"app-filter", app_filter, "app-filter", app_filter,
"user", user, "user-display-name", user_display_name,
NULL); NULL);
} }
@ -317,45 +306,50 @@ mct_restrict_applications_dialog_set_app_filter (MctRestrictApplicationsDialog *
} }
/** /**
* mct_restrict_applications_dialog_get_user: * mct_restrict_applications_dialog_get_user_display_name:
* @self: an #MctRestrictApplicationsDialog * @self: an #MctRestrictApplicationsDialog
* *
* Get the value of #MctRestrictApplicationsDialog:user. * Get the value of #MctRestrictApplicationsDialog:user-display-name.
* *
* Returns: (transfer none) (nullable): the user the dialog is configured for, * Returns: (transfer none) (nullable): the display name of the user the dialog
* or %NULL if unknown * is configured for, or %NULL if unknown
* Since: 0.5.0 * Since: 0.5.0
*/ */
ActUser * const gchar *
mct_restrict_applications_dialog_get_user (MctRestrictApplicationsDialog *self) mct_restrict_applications_dialog_get_user_display_name (MctRestrictApplicationsDialog *self)
{ {
g_return_val_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self), NULL); g_return_val_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self), NULL);
return self->user; return self->user_display_name;
} }
/** /**
* mct_restrict_applications_dialog_set_user: * mct_restrict_applications_dialog_set_user_display_name:
* @self: an #MctRestrictApplicationsDialog * @self: an #MctRestrictApplicationsDialog
* @user: (nullable) (transfer none): the user to configure the dialog for, * @user_display_name: (nullable) (transfer none): the display name of the user
* or %NULL if unknown * to configure the dialog for, or %NULL if unknown
* *
* Set the value of #MctRestrictApplicationsDialog:user. * Set the value of #MctRestrictApplicationsDialog:user-display-name.
* *
* Since: 0.5.0 * Since: 0.5.0
*/ */
void void
mct_restrict_applications_dialog_set_user (MctRestrictApplicationsDialog *self, mct_restrict_applications_dialog_set_user_display_name (MctRestrictApplicationsDialog *self,
ActUser *user) const gchar *user_display_name)
{ {
g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self)); g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self));
g_return_if_fail (user == NULL || ACT_IS_USER (user)); g_return_if_fail (user_display_name == NULL ||
(*user_display_name != '\0' &&
g_utf8_validate (user_display_name, -1, NULL)));
if (g_set_object (&self->user, user)) if (g_strcmp0 (self->user_display_name, user_display_name) == 0)
{ return;
update_description (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USER]); g_clear_pointer (&self->user_display_name, g_free);
} self->user_display_name = g_strdup (user_display_name);
update_description (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USER_DISPLAY_NAME]);
} }
/** /**

View File

@ -21,7 +21,6 @@
#pragma once #pragma once
#include <act/act.h>
#include <glib.h> #include <glib.h>
#include <glib-object.h> #include <glib-object.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
@ -34,15 +33,15 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE (MctRestrictApplicationsDialog, mct_restrict_applications_dialog, MCT, RESTRICT_APPLICATIONS_DIALOG, GtkDialog) G_DECLARE_FINAL_TYPE (MctRestrictApplicationsDialog, mct_restrict_applications_dialog, MCT, RESTRICT_APPLICATIONS_DIALOG, GtkDialog)
MctRestrictApplicationsDialog *mct_restrict_applications_dialog_new (MctAppFilter *app_filter, MctRestrictApplicationsDialog *mct_restrict_applications_dialog_new (MctAppFilter *app_filter,
ActUser *user); const gchar *user_display_name);
MctAppFilter *mct_restrict_applications_dialog_get_app_filter (MctRestrictApplicationsDialog *self); MctAppFilter *mct_restrict_applications_dialog_get_app_filter (MctRestrictApplicationsDialog *self);
void mct_restrict_applications_dialog_set_app_filter (MctRestrictApplicationsDialog *self, void mct_restrict_applications_dialog_set_app_filter (MctRestrictApplicationsDialog *self,
MctAppFilter *app_filter); MctAppFilter *app_filter);
ActUser *mct_restrict_applications_dialog_get_user (MctRestrictApplicationsDialog *self); const gchar *mct_restrict_applications_dialog_get_user_display_name (MctRestrictApplicationsDialog *self);
void mct_restrict_applications_dialog_set_user (MctRestrictApplicationsDialog *self, void mct_restrict_applications_dialog_set_user_display_name (MctRestrictApplicationsDialog *self,
ActUser *user); const gchar *user_display_name);
void mct_restrict_applications_dialog_build_app_filter (MctRestrictApplicationsDialog *self, void mct_restrict_applications_dialog_build_app_filter (MctRestrictApplicationsDialog *self,
MctAppFilterBuilder *builder); MctAppFilterBuilder *builder);

View File

@ -155,6 +155,25 @@ get_content_rating_system (ActUser *user)
return gs_utils_content_rating_system_from_locale (user_language); return gs_utils_content_rating_system_from_locale (user_language);
} }
static const gchar *
get_user_display_name (ActUser *user)
{
const gchar *display_name;
g_return_val_if_fail (ACT_IS_USER (user), _("unknown"));
display_name = act_user_get_real_name (user);
if (display_name != NULL)
return display_name;
display_name = act_user_get_user_name (user);
if (display_name != NULL)
return display_name;
/* Translators: this is the full name for an unknown user account. */
return _("unknown");
}
static void static void
schedule_update_blacklisted_apps (MctUserControls *self) schedule_update_blacklisted_apps (MctUserControls *self)
{ {
@ -549,7 +568,7 @@ on_restrict_applications_button_clicked_cb (GtkButton *button,
gtk_window_set_transient_for (GTK_WINDOW (self->restrict_applications_dialog), gtk_window_set_transient_for (GTK_WINDOW (self->restrict_applications_dialog),
GTK_WINDOW (toplevel)); GTK_WINDOW (toplevel));
mct_restrict_applications_dialog_set_user (self->restrict_applications_dialog, self->user); mct_restrict_applications_dialog_set_user_display_name (self->restrict_applications_dialog, get_user_display_name (self->user));
mct_restrict_applications_dialog_set_app_filter (self->restrict_applications_dialog, self->filter); mct_restrict_applications_dialog_set_app_filter (self->restrict_applications_dialog, self->filter);
gtk_widget_show (GTK_WIDGET (self->restrict_applications_dialog)); gtk_widget_show (GTK_WIDGET (self->restrict_applications_dialog));