LCOV - code coverage report
Current view: top level - libmalcontent-ui - restrict-applications-dialog.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 17 100 17.0 %
Date: 2022-09-14 07:51:19 Functions: 4 16 25.0 %
Branches: 4 32 12.5 %

           Branch data     Line data    Source code
       1                 :            : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
       2                 :            :  *
       3                 :            :  * Copyright © 2020 Endless Mobile, Inc.
       4                 :            :  *
       5                 :            :  * This program is free software; you can redistribute it and/or modify
       6                 :            :  * it under the terms of the GNU General Public License as published by
       7                 :            :  * the Free Software Foundation; either version 2 of the License, or
       8                 :            :  * (at your option) any later version.
       9                 :            :  *
      10                 :            :  * This program is distributed in the hope that it will be useful,
      11                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13                 :            :  * GNU General Public License for more details.
      14                 :            :  *
      15                 :            :  * You should have received a copy of the GNU General Public License
      16                 :            :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      17                 :            :  *
      18                 :            :  * Authors:
      19                 :            :  *  - Philip Withnall <withnall@endlessm.com>
      20                 :            :  */
      21                 :            : 
      22                 :            : #include "config.h"
      23                 :            : 
      24                 :            : #include <gio/gio.h>
      25                 :            : #include <glib.h>
      26                 :            : #include <glib-object.h>
      27                 :            : #include <glib/gi18n-lib.h>
      28                 :            : #include <gtk/gtk.h>
      29                 :            : #include <adwaita.h>
      30                 :            : 
      31                 :            : #include "restrict-applications-dialog.h"
      32                 :            : #include "restrict-applications-selector.h"
      33                 :            : 
      34                 :            : 
      35                 :            : static void update_description (MctRestrictApplicationsDialog *self);
      36                 :            : 
      37                 :            : /**
      38                 :            :  * MctRestrictApplicationsDialog:
      39                 :            :  *
      40                 :            :  * The ‘Restrict Applications’ dialog is a dialog which shows the available
      41                 :            :  * applications on the system alongside a column of toggle switches, which
      42                 :            :  * allows the given user to be prevented from running each application.
      43                 :            :  *
      44                 :            :  * The dialog contains a single #MctRestrictApplicationsSelector. It takes a
      45                 :            :  * #MctRestrictApplicationsDialog:user and
      46                 :            :  * #MctRestrictApplicationsDialog:app-filter as input to set up the UI, and
      47                 :            :  * returns its output as set of modifications to a given #MctAppFilterBuilder
      48                 :            :  * using mct_restrict_applications_dialog_build_app_filter().
      49                 :            :  *
      50                 :            :  * Since: 0.5.0
      51                 :            :  */
      52                 :            : struct _MctRestrictApplicationsDialog
      53                 :            : {
      54                 :            :   GtkDialog parent_instance;
      55                 :            : 
      56                 :            :   MctRestrictApplicationsSelector *selector;
      57                 :            :   AdwPreferencesGroup *group;
      58                 :            : 
      59                 :            :   MctAppFilter *app_filter;  /* (owned) (not nullable) */
      60                 :            :   gchar *user_display_name;  /* (owned) (nullable) */
      61                 :            : };
      62                 :            : 
      63   [ +  +  +  -  :          3 : G_DEFINE_TYPE (MctRestrictApplicationsDialog, mct_restrict_applications_dialog, ADW_TYPE_PREFERENCES_WINDOW)
                   +  - ]
      64                 :            : 
      65                 :            : typedef enum
      66                 :            : {
      67                 :            :   PROP_APP_FILTER = 1,
      68                 :            :   PROP_USER_DISPLAY_NAME,
      69                 :            : } MctRestrictApplicationsDialogProperty;
      70                 :            : 
      71                 :            : static GParamSpec *properties[PROP_USER_DISPLAY_NAME + 1];
      72                 :            : 
      73                 :            : static void
      74                 :          0 : mct_restrict_applications_dialog_constructed (GObject *obj)
      75                 :            : {
      76                 :          0 :   MctRestrictApplicationsDialog *self = MCT_RESTRICT_APPLICATIONS_DIALOG (obj);
      77                 :            : 
      78                 :          0 :   g_assert (self->app_filter != NULL);
      79                 :          0 :   g_assert (self->user_display_name == NULL ||
      80                 :            :             (*self->user_display_name != '\0' &&
      81                 :            :              g_utf8_validate (self->user_display_name, -1, NULL)));
      82                 :            : 
      83                 :          0 :   G_OBJECT_CLASS (mct_restrict_applications_dialog_parent_class)->constructed (obj);
      84                 :          0 : }
      85                 :            : 
      86                 :            : static void
      87                 :          0 : mct_restrict_applications_dialog_get_property (GObject    *object,
      88                 :            :                                                guint       prop_id,
      89                 :            :                                                GValue     *value,
      90                 :            :                                                GParamSpec *pspec)
      91                 :            : {
      92                 :          0 :   MctRestrictApplicationsDialog *self = MCT_RESTRICT_APPLICATIONS_DIALOG (object);
      93                 :            : 
      94      [ #  #  # ]:          0 :   switch ((MctRestrictApplicationsDialogProperty) prop_id)
      95                 :            :     {
      96                 :          0 :     case PROP_APP_FILTER:
      97                 :          0 :       g_value_set_boxed (value, self->app_filter);
      98                 :          0 :       break;
      99                 :            : 
     100                 :          0 :     case PROP_USER_DISPLAY_NAME:
     101                 :          0 :       g_value_set_string (value, self->user_display_name);
     102                 :          0 :       break;
     103                 :            : 
     104                 :          0 :     default:
     105                 :          0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     106                 :            :     }
     107                 :          0 : }
     108                 :            : 
     109                 :            : static void
     110                 :          0 : mct_restrict_applications_dialog_set_property (GObject      *object,
     111                 :            :                                                guint         prop_id,
     112                 :            :                                                const GValue *value,
     113                 :            :                                                GParamSpec   *pspec)
     114                 :            : {
     115                 :          0 :   MctRestrictApplicationsDialog *self = MCT_RESTRICT_APPLICATIONS_DIALOG (object);
     116                 :            : 
     117      [ #  #  # ]:          0 :   switch ((MctRestrictApplicationsDialogProperty) prop_id)
     118                 :            :     {
     119                 :          0 :     case PROP_APP_FILTER:
     120                 :          0 :       mct_restrict_applications_dialog_set_app_filter (self, g_value_get_boxed (value));
     121                 :          0 :       break;
     122                 :            : 
     123                 :          0 :     case PROP_USER_DISPLAY_NAME:
     124                 :          0 :       mct_restrict_applications_dialog_set_user_display_name (self, g_value_get_string (value));
     125                 :          0 :       break;
     126                 :            : 
     127                 :          0 :     default:
     128                 :          0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     129                 :            :     }
     130                 :          0 : }
     131                 :            : 
     132                 :            : static void
     133                 :          0 : mct_restrict_applications_dialog_dispose (GObject *object)
     134                 :            : {
     135                 :          0 :   MctRestrictApplicationsDialog *self = (MctRestrictApplicationsDialog *)object;
     136                 :            : 
     137         [ #  # ]:          0 :   g_clear_pointer (&self->app_filter, mct_app_filter_unref);
     138         [ #  # ]:          0 :   g_clear_pointer (&self->user_display_name, g_free);
     139                 :            : 
     140                 :          0 :   G_OBJECT_CLASS (mct_restrict_applications_dialog_parent_class)->dispose (object);
     141                 :          0 : }
     142                 :            : 
     143                 :            : static void
     144                 :          1 : mct_restrict_applications_dialog_class_init (MctRestrictApplicationsDialogClass *klass)
     145                 :            : {
     146                 :          1 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     147                 :          1 :   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
     148                 :            : 
     149                 :          1 :   object_class->constructed = mct_restrict_applications_dialog_constructed;
     150                 :          1 :   object_class->get_property = mct_restrict_applications_dialog_get_property;
     151                 :          1 :   object_class->set_property = mct_restrict_applications_dialog_set_property;
     152                 :          1 :   object_class->dispose = mct_restrict_applications_dialog_dispose;
     153                 :            : 
     154                 :            :   /**
     155                 :            :    * MctRestrictApplicationsDialog:app-filter: (not nullable)
     156                 :            :    *
     157                 :            :    * The user’s current app filter, used to set up the dialog. As app filters
     158                 :            :    * are immutable, it is not updated as the dialog is changed. Use
     159                 :            :    * mct_restrict_applications_dialog_build_app_filter() to build the new app
     160                 :            :    * filter.
     161                 :            :    *
     162                 :            :    * Since: 0.5.0
     163                 :            :    */
     164                 :          1 :   properties[PROP_APP_FILTER] =
     165                 :          1 :       g_param_spec_boxed ("app-filter",
     166                 :            :                           "App Filter",
     167                 :            :                           "The user’s current app filter, used to set up the dialog.",
     168                 :            :                           MCT_TYPE_APP_FILTER,
     169                 :            :                           G_PARAM_READWRITE |
     170                 :            :                           G_PARAM_CONSTRUCT_ONLY |
     171                 :            :                           G_PARAM_STATIC_STRINGS |
     172                 :            :                           G_PARAM_EXPLICIT_NOTIFY);
     173                 :            : 
     174                 :            :   /**
     175                 :            :    * MctRestrictApplicationsDialog:user-display-name: (nullable)
     176                 :            :    *
     177                 :            :    * The display name for the currently selected user account, or %NULL if no
     178                 :            :    * user is selected. This will typically be the user’s full name (if known)
     179                 :            :    * or their username.
     180                 :            :    *
     181                 :            :    * If set, it must be valid UTF-8 and non-empty.
     182                 :            :    *
     183                 :            :    * Since: 0.5.0
     184                 :            :    */
     185                 :          1 :   properties[PROP_USER_DISPLAY_NAME] =
     186                 :          1 :       g_param_spec_string ("user-display-name",
     187                 :            :                            "User Display Name",
     188                 :            :                            "The display name for the currently selected user account, or %NULL if no user is selected.",
     189                 :            :                            NULL,
     190                 :            :                            G_PARAM_READWRITE |
     191                 :            :                            G_PARAM_STATIC_STRINGS |
     192                 :            :                            G_PARAM_EXPLICIT_NOTIFY);
     193                 :            : 
     194                 :          1 :   g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
     195                 :            : 
     196                 :          1 :   gtk_widget_class_set_template_from_resource (widget_class, "/org/freedesktop/MalcontentUi/ui/restrict-applications-dialog.ui");
     197                 :            : 
     198                 :          1 :   gtk_widget_class_bind_template_child (widget_class, MctRestrictApplicationsDialog, selector);
     199                 :          1 :   gtk_widget_class_bind_template_child (widget_class, MctRestrictApplicationsDialog, group);
     200                 :          1 : }
     201                 :            : 
     202                 :            : static void
     203                 :          0 : mct_restrict_applications_dialog_init (MctRestrictApplicationsDialog *self)
     204                 :            : {
     205                 :            :   /* Ensure the types used in the UI are registered. */
     206                 :          0 :   g_type_ensure (MCT_TYPE_RESTRICT_APPLICATIONS_SELECTOR);
     207                 :            : 
     208                 :          0 :   gtk_widget_init_template (GTK_WIDGET (self));
     209                 :          0 : }
     210                 :            : 
     211                 :            : static void
     212                 :          0 : update_description (MctRestrictApplicationsDialog *self)
     213                 :            : {
     214         [ #  # ]:          0 :   g_autofree gchar *description = NULL;
     215                 :            : 
     216         [ #  # ]:          0 :   if (self->user_display_name == NULL)
     217                 :            :     {
     218                 :          0 :       adw_preferences_group_set_description (self->group, NULL);
     219                 :          0 :       return;
     220                 :            :     }
     221                 :            : 
     222                 :            :   /* Translators: the placeholder is a user’s full name */
     223                 :          0 :   description = g_strdup_printf (_("Restrict %s from using the following installed applications."),
     224                 :            :                                  self->user_display_name);
     225                 :          0 :   adw_preferences_group_set_description (self->group, description);
     226                 :            : }
     227                 :            : 
     228                 :            : /**
     229                 :            :  * mct_restrict_applications_dialog_new:
     230                 :            :  * @app_filter: (transfer none): the initial app filter configuration to show
     231                 :            :  * @user_display_name: (transfer none) (nullable): the display name of the user
     232                 :            :  *    to show the app filter for, or %NULL if no user is selected
     233                 :            :  *
     234                 :            :  * Create a new #MctRestrictApplicationsDialog widget.
     235                 :            :  *
     236                 :            :  * Returns: (transfer full): a new restricted applications editing dialog
     237                 :            :  * Since: 0.5.0
     238                 :            :  */
     239                 :            : MctRestrictApplicationsDialog *
     240                 :          0 : mct_restrict_applications_dialog_new (MctAppFilter *app_filter,
     241                 :            :                                       const gchar  *user_display_name)
     242                 :            : {
     243                 :          0 :   g_return_val_if_fail (app_filter != NULL, NULL);
     244                 :          0 :   g_return_val_if_fail (user_display_name == NULL ||
     245                 :            :                         (*user_display_name != '\0' &&
     246                 :            :                          g_utf8_validate (user_display_name, -1, NULL)), NULL);
     247                 :            : 
     248                 :          0 :   return g_object_new (MCT_TYPE_RESTRICT_APPLICATIONS_DIALOG,
     249                 :            :                        "app-filter", app_filter,
     250                 :            :                        "user-display-name", user_display_name,
     251                 :            :                        NULL);
     252                 :            : }
     253                 :            : 
     254                 :            : /**
     255                 :            :  * mct_restrict_applications_dialog_get_app_filter:
     256                 :            :  * @self: an #MctRestrictApplicationsDialog
     257                 :            :  *
     258                 :            :  * Get the value of #MctRestrictApplicationsDialog:app-filter. If the property
     259                 :            :  * was originally set to %NULL, this will be the empty app filter.
     260                 :            :  *
     261                 :            :  * Returns: (transfer none) (not nullable): the initial app filter used to
     262                 :            :  *    populate the dialog
     263                 :            :  * Since: 0.5.0
     264                 :            :  */
     265                 :            : MctAppFilter *
     266                 :          0 : mct_restrict_applications_dialog_get_app_filter (MctRestrictApplicationsDialog *self)
     267                 :            : {
     268                 :          0 :   g_return_val_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self), NULL);
     269                 :            : 
     270                 :          0 :   return self->app_filter;
     271                 :            : }
     272                 :            : 
     273                 :            : /**
     274                 :            :  * mct_restrict_applications_dialog_set_app_filter:
     275                 :            :  * @self: an #MctRestrictApplicationsDialog
     276                 :            :  * @app_filter: (nullable) (transfer none): the app filter to configure the dialog
     277                 :            :  *    from, or %NULL to use an empty app filter
     278                 :            :  *
     279                 :            :  * Set the value of #MctRestrictApplicationsDialog:app-filter.
     280                 :            :  *
     281                 :            :  * Since: 0.5.0
     282                 :            :  */
     283                 :            : void
     284                 :          0 : mct_restrict_applications_dialog_set_app_filter (MctRestrictApplicationsDialog *self,
     285                 :            :                                                  MctAppFilter                  *app_filter)
     286                 :            : {
     287         [ #  # ]:          0 :   g_autoptr(MctAppFilter) owned_app_filter = NULL;
     288                 :            : 
     289                 :          0 :   g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self));
     290                 :            : 
     291                 :            :   /* Default app filter, typically for when we’re instantiated by #GtkBuilder. */
     292         [ #  # ]:          0 :   if (app_filter == NULL)
     293                 :            :     {
     294                 :          0 :       g_auto(MctAppFilterBuilder) builder = MCT_APP_FILTER_BUILDER_INIT ();
     295                 :          0 :       owned_app_filter = mct_app_filter_builder_end (&builder);
     296                 :          0 :       app_filter = owned_app_filter;
     297                 :            :     }
     298                 :            : 
     299         [ #  # ]:          0 :   if (app_filter == self->app_filter)
     300                 :          0 :     return;
     301                 :            : 
     302         [ #  # ]:          0 :   g_clear_pointer (&self->app_filter, mct_app_filter_unref);
     303                 :          0 :   self->app_filter = mct_app_filter_ref (app_filter);
     304                 :            : 
     305                 :          0 :   mct_restrict_applications_selector_set_app_filter (self->selector, self->app_filter);
     306                 :            : 
     307                 :          0 :   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_APP_FILTER]);
     308                 :            : }
     309                 :            : 
     310                 :            : /**
     311                 :            :  * mct_restrict_applications_dialog_get_user_display_name:
     312                 :            :  * @self: an #MctRestrictApplicationsDialog
     313                 :            :  *
     314                 :            :  * Get the value of #MctRestrictApplicationsDialog:user-display-name.
     315                 :            :  *
     316                 :            :  * Returns: (transfer none) (nullable): the display name of the user the dialog
     317                 :            :  *    is configured for, or %NULL if unknown
     318                 :            :  * Since: 0.5.0
     319                 :            :  */
     320                 :            : const gchar *
     321                 :          0 : mct_restrict_applications_dialog_get_user_display_name (MctRestrictApplicationsDialog *self)
     322                 :            : {
     323                 :          0 :   g_return_val_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self), NULL);
     324                 :            : 
     325                 :          0 :   return self->user_display_name;
     326                 :            : }
     327                 :            : 
     328                 :            : /**
     329                 :            :  * mct_restrict_applications_dialog_set_user_display_name:
     330                 :            :  * @self: an #MctRestrictApplicationsDialog
     331                 :            :  * @user_display_name: (nullable) (transfer none): the display name of the user
     332                 :            :  *    to configure the dialog for, or %NULL if unknown
     333                 :            :  *
     334                 :            :  * Set the value of #MctRestrictApplicationsDialog:user-display-name.
     335                 :            :  *
     336                 :            :  * Since: 0.5.0
     337                 :            :  */
     338                 :            : void
     339                 :          0 : mct_restrict_applications_dialog_set_user_display_name (MctRestrictApplicationsDialog *self,
     340                 :            :                                                         const gchar                   *user_display_name)
     341                 :            : {
     342                 :          0 :   g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self));
     343                 :          0 :   g_return_if_fail (user_display_name == NULL ||
     344                 :            :                     (*user_display_name != '\0' &&
     345                 :            :                      g_utf8_validate (user_display_name, -1, NULL)));
     346                 :            : 
     347         [ #  # ]:          0 :   if (g_strcmp0 (self->user_display_name, user_display_name) == 0)
     348                 :          0 :     return;
     349                 :            : 
     350         [ #  # ]:          0 :   g_clear_pointer (&self->user_display_name, g_free);
     351                 :          0 :   self->user_display_name = g_strdup (user_display_name);
     352                 :            : 
     353                 :          0 :   update_description (self);
     354                 :          0 :   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USER_DISPLAY_NAME]);
     355                 :            : }
     356                 :            : 
     357                 :            : /**
     358                 :            :  * mct_restrict_applications_dialog_build_app_filter:
     359                 :            :  * @self: an #MctRestrictApplicationsDialog
     360                 :            :  * @builder: an existing #MctAppFilterBuilder to modify
     361                 :            :  *
     362                 :            :  * Get the app filter settings currently configured in the dialog, by modifying
     363                 :            :  * the given @builder.
     364                 :            :  *
     365                 :            :  * Typically this will be called in the handler for #GtkDialog::response.
     366                 :            :  *
     367                 :            :  * Since: 0.5.0
     368                 :            :  */
     369                 :            : void
     370                 :          0 : mct_restrict_applications_dialog_build_app_filter (MctRestrictApplicationsDialog *self,
     371                 :            :                                                    MctAppFilterBuilder           *builder)
     372                 :            : {
     373                 :          0 :   g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self));
     374                 :          0 :   g_return_if_fail (builder != NULL);
     375                 :            : 
     376                 :          0 :   mct_restrict_applications_selector_build_app_filter (self->selector, builder);
     377                 :            : }

Generated by: LCOV version 1.16