LCOV - code coverage report
Current view: top level - libmalcontent-ui - restrict-applications-selector.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 16 292 5.5 %
Date: 2022-01-17 13:37:57 Functions: 4 22 18.2 %
Branches: 5 144 3.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 <flatpak.h>
      25                 :            : #include <gio/gdesktopappinfo.h>
      26                 :            : #include <gio/gio.h>
      27                 :            : #include <glib.h>
      28                 :            : #include <glib-object.h>
      29                 :            : #include <glib/gi18n-lib.h>
      30                 :            : #include <gtk/gtk.h>
      31                 :            : #include <libmalcontent/app-filter.h>
      32                 :            : 
      33                 :            : #include "restrict-applications-selector.h"
      34                 :            : 
      35                 :            : 
      36                 :            : #define WEB_BROWSERS_CONTENT_TYPE "x-scheme-handler/http"
      37                 :            : 
      38                 :            : static void app_info_changed_cb (GAppInfoMonitor *monitor,
      39                 :            :                                  gpointer         user_data);
      40                 :            : static void reload_apps (MctRestrictApplicationsSelector *self);
      41                 :            : static GtkWidget *create_row_for_app_cb (gpointer item,
      42                 :            :                                          gpointer user_data);
      43                 :            : 
      44                 :            : /**
      45                 :            :  * MctRestrictApplicationsSelector:
      46                 :            :  *
      47                 :            :  * The ‘Restrict Applications’ selector is a list box which shows the available
      48                 :            :  * applications on the system alongside a column of toggle switches, which
      49                 :            :  * allows the given user to be prevented from running each application.
      50                 :            :  *
      51                 :            :  * The selector takes an #MctRestrictApplicationsSelector:app-filter as input
      52                 :            :  * to set up the UI, and returns its output as set of modifications to a given
      53                 :            :  * #MctAppFilterBuilder using
      54                 :            :  * mct_restrict_applications_selector_build_app_filter().
      55                 :            :  *
      56                 :            :  * Since: 0.5.0
      57                 :            :  */
      58                 :            : struct _MctRestrictApplicationsSelector
      59                 :            : {
      60                 :            :   GtkBox parent_instance;
      61                 :            : 
      62                 :            :   GtkListBox *listbox;
      63                 :            : 
      64                 :            :   GList *cached_apps;  /* (nullable) (owned) (element-type GAppInfo) */
      65                 :            :   GListStore *apps;  /* (owned) */
      66                 :            :   GAppInfoMonitor *app_info_monitor;  /* (owned) */
      67                 :            :   gulong app_info_monitor_changed_id;
      68                 :            :   GHashTable *blocklisted_apps; /* (owned) (element-type GAppInfo) */
      69                 :            : 
      70                 :            :   MctAppFilter *app_filter;  /* (owned) */
      71                 :            : 
      72                 :            :   FlatpakInstallation *system_installation; /* (owned) */
      73                 :            :   FlatpakInstallation *user_installation; /* (owned) */
      74                 :            : 
      75                 :            :   GtkCssProvider *css_provider;  /* (owned) */
      76                 :            : };
      77                 :            : 
      78   [ +  +  +  -  :          4 : G_DEFINE_TYPE (MctRestrictApplicationsSelector, mct_restrict_applications_selector, GTK_TYPE_BOX)
                   +  + ]
      79                 :            : 
      80                 :            : typedef enum
      81                 :            : {
      82                 :            :   PROP_APP_FILTER = 1,
      83                 :            : } MctRestrictApplicationsSelectorProperty;
      84                 :            : 
      85                 :            : static GParamSpec *properties[PROP_APP_FILTER + 1];
      86                 :            : 
      87                 :            : enum {
      88                 :            :   SIGNAL_CHANGED,
      89                 :            : };
      90                 :            : 
      91                 :            : static guint signals[SIGNAL_CHANGED + 1];
      92                 :            : 
      93                 :            : static void
      94                 :          0 : mct_restrict_applications_selector_constructed (GObject *obj)
      95                 :            : {
      96                 :          0 :   MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (obj);
      97                 :            : 
      98                 :            :   /* Default app filter, typically for when we’re instantiated by #GtkBuilder. */
      99         [ #  # ]:          0 :   if (self->app_filter == NULL)
     100                 :            :     {
     101                 :          0 :       g_auto(MctAppFilterBuilder) builder = MCT_APP_FILTER_BUILDER_INIT ();
     102                 :          0 :       self->app_filter = mct_app_filter_builder_end (&builder);
     103                 :            :     }
     104                 :            : 
     105                 :          0 :   g_assert (self->app_filter != NULL);
     106                 :            : 
     107                 :            :   /* Load the apps. */
     108                 :          0 :   reload_apps (self);
     109                 :            : 
     110                 :          0 :   G_OBJECT_CLASS (mct_restrict_applications_selector_parent_class)->constructed (obj);
     111                 :          0 : }
     112                 :            : 
     113                 :            : static void
     114                 :          0 : mct_restrict_applications_selector_get_property (GObject    *object,
     115                 :            :                                                  guint       prop_id,
     116                 :            :                                                  GValue     *value,
     117                 :            :                                                  GParamSpec *pspec)
     118                 :            : {
     119                 :          0 :   MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (object);
     120                 :            : 
     121         [ #  # ]:          0 :   switch ((MctRestrictApplicationsSelectorProperty) prop_id)
     122                 :            :     {
     123                 :          0 :     case PROP_APP_FILTER:
     124                 :          0 :       g_value_set_boxed (value, self->app_filter);
     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_selector_set_property (GObject      *object,
     134                 :            :                                                  guint         prop_id,
     135                 :            :                                                  const GValue *value,
     136                 :            :                                                  GParamSpec   *pspec)
     137                 :            : {
     138                 :          0 :   MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (object);
     139                 :            : 
     140         [ #  # ]:          0 :   switch ((MctRestrictApplicationsSelectorProperty) prop_id)
     141                 :            :     {
     142                 :          0 :     case PROP_APP_FILTER:
     143                 :          0 :       mct_restrict_applications_selector_set_app_filter (self, g_value_get_boxed (value));
     144                 :          0 :       break;
     145                 :            : 
     146                 :          0 :     default:
     147                 :          0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     148                 :            :     }
     149                 :          0 : }
     150                 :            : 
     151                 :            : static void
     152                 :          0 : mct_restrict_applications_selector_dispose (GObject *object)
     153                 :            : {
     154                 :          0 :   MctRestrictApplicationsSelector *self = (MctRestrictApplicationsSelector *)object;
     155                 :            : 
     156         [ #  # ]:          0 :   g_clear_pointer (&self->blocklisted_apps, g_hash_table_unref);
     157         [ #  # ]:          0 :   g_clear_object (&self->apps);
     158         [ #  # ]:          0 :   g_clear_list (&self->cached_apps, g_object_unref);
     159                 :            : 
     160   [ #  #  #  # ]:          0 :   if (self->app_info_monitor != NULL && self->app_info_monitor_changed_id != 0)
     161                 :            :     {
     162                 :          0 :       g_signal_handler_disconnect (self->app_info_monitor, self->app_info_monitor_changed_id);
     163                 :          0 :       self->app_info_monitor_changed_id = 0;
     164                 :            :     }
     165         [ #  # ]:          0 :   g_clear_object (&self->app_info_monitor);
     166         [ #  # ]:          0 :   g_clear_pointer (&self->app_filter, mct_app_filter_unref);
     167         [ #  # ]:          0 :   g_clear_object (&self->system_installation);
     168         [ #  # ]:          0 :   g_clear_object (&self->user_installation);
     169         [ #  # ]:          0 :   g_clear_object (&self->css_provider);
     170                 :            : 
     171                 :          0 :   G_OBJECT_CLASS (mct_restrict_applications_selector_parent_class)->dispose (object);
     172                 :          0 : }
     173                 :            : 
     174                 :            : static void
     175                 :          1 : mct_restrict_applications_selector_class_init (MctRestrictApplicationsSelectorClass *klass)
     176                 :            : {
     177                 :          1 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     178                 :          1 :   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
     179                 :            : 
     180                 :          1 :   object_class->constructed = mct_restrict_applications_selector_constructed;
     181                 :          1 :   object_class->get_property = mct_restrict_applications_selector_get_property;
     182                 :          1 :   object_class->set_property = mct_restrict_applications_selector_set_property;
     183                 :          1 :   object_class->dispose = mct_restrict_applications_selector_dispose;
     184                 :            : 
     185                 :            :   /**
     186                 :            :    * MctRestrictApplicationsSelector:app-filter: (not nullable)
     187                 :            :    *
     188                 :            :    * The user’s current app filter, used to set up the selector. As app filters
     189                 :            :    * are immutable, it is not updated as the selector is changed. Use
     190                 :            :    * mct_restrict_applications_selector_build_app_filter() to build the new app
     191                 :            :    * filter.
     192                 :            :    *
     193                 :            :    * Since: 0.5.0
     194                 :            :    */
     195                 :          1 :   properties[PROP_APP_FILTER] =
     196                 :          1 :       g_param_spec_boxed ("app-filter",
     197                 :            :                           "App Filter",
     198                 :            :                           "The user’s current app filter, used to set up the selector.",
     199                 :            :                           MCT_TYPE_APP_FILTER,
     200                 :            :                           G_PARAM_READWRITE |
     201                 :            :                           G_PARAM_STATIC_STRINGS |
     202                 :            :                           G_PARAM_EXPLICIT_NOTIFY);
     203                 :            : 
     204                 :          1 :   g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
     205                 :            : 
     206                 :            :   /**
     207                 :            :    * MctRestrictApplicationsSelector::changed:
     208                 :            :    *
     209                 :            :    * Emitted whenever an application in the list is blocked or unblocked.
     210                 :            :    *
     211                 :            :    * Since: 0.5.0
     212                 :            :    */
     213                 :          1 :   signals[SIGNAL_CHANGED] =
     214                 :          1 :       g_signal_new ("changed",
     215                 :            :                     MCT_TYPE_RESTRICT_APPLICATIONS_SELECTOR,
     216                 :            :                     G_SIGNAL_RUN_LAST,
     217                 :            :                     0,
     218                 :            :                     NULL, NULL,
     219                 :            :                     g_cclosure_marshal_VOID__VOID,
     220                 :            :                     G_TYPE_NONE, 0);
     221                 :            : 
     222                 :          1 :   gtk_widget_class_set_template_from_resource (widget_class, "/org/freedesktop/MalcontentUi/ui/restrict-applications-selector.ui");
     223                 :            : 
     224                 :          1 :   gtk_widget_class_bind_template_child (widget_class, MctRestrictApplicationsSelector, listbox);
     225                 :          1 : }
     226                 :            : 
     227                 :            : static void
     228                 :          0 : mct_restrict_applications_selector_init (MctRestrictApplicationsSelector *self)
     229                 :            : {
     230                 :          0 :   gtk_widget_init_template (GTK_WIDGET (self));
     231                 :            : 
     232                 :          0 :   self->apps = g_list_store_new (G_TYPE_APP_INFO);
     233                 :          0 :   self->cached_apps = NULL;
     234                 :            : 
     235                 :          0 :   self->app_info_monitor = g_app_info_monitor_get ();
     236                 :          0 :   self->app_info_monitor_changed_id =
     237                 :          0 :       g_signal_connect (self->app_info_monitor, "changed",
     238                 :            :                         (GCallback) app_info_changed_cb, self);
     239                 :            : 
     240                 :          0 :   gtk_list_box_bind_model (self->listbox,
     241                 :          0 :                            G_LIST_MODEL (self->apps),
     242                 :            :                            create_row_for_app_cb,
     243                 :            :                            self,
     244                 :            :                            NULL);
     245                 :            : 
     246                 :          0 :   self->blocklisted_apps = g_hash_table_new_full (g_direct_hash,
     247                 :            :                                                   g_direct_equal,
     248                 :            :                                                   g_object_unref,
     249                 :            :                                                   NULL);
     250                 :            : 
     251                 :          0 :   self->system_installation = flatpak_installation_new_system (NULL, NULL);
     252                 :          0 :   self->user_installation = flatpak_installation_new_user (NULL, NULL);
     253                 :            : 
     254                 :          0 :   self->css_provider = gtk_css_provider_new ();
     255                 :          0 :   gtk_css_provider_load_from_resource (self->css_provider,
     256                 :            :                                        "/org/freedesktop/MalcontentUi/ui/restricts-switch.css");
     257                 :          0 : }
     258                 :            : 
     259                 :            : static void
     260                 :          0 : on_switch_active_changed_cb (GtkSwitch  *s,
     261                 :            :                              GParamSpec *pspec,
     262                 :            :                              gpointer    user_data)
     263                 :            : {
     264                 :          0 :   MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (user_data);
     265                 :            :   GAppInfo *app;
     266                 :            :   gboolean allowed;
     267                 :            : 
     268                 :          0 :   app = g_object_get_data (G_OBJECT (s), "GAppInfo");
     269                 :          0 :   allowed = !gtk_switch_get_active (s);
     270                 :            : 
     271         [ #  # ]:          0 :   if (allowed)
     272                 :            :     {
     273                 :            :       gboolean removed;
     274                 :            : 
     275                 :          0 :       g_debug ("Removing ‘%s’ from blocklisted apps", g_app_info_get_id (app));
     276                 :            : 
     277                 :          0 :       removed = g_hash_table_remove (self->blocklisted_apps, app);
     278                 :          0 :       g_assert (removed);
     279                 :            :     }
     280                 :            :   else
     281                 :            :     {
     282                 :            :       gboolean added;
     283                 :            : 
     284                 :          0 :       g_debug ("Blocklisting ‘%s’", g_app_info_get_id (app));
     285                 :            : 
     286                 :          0 :       added = g_hash_table_add (self->blocklisted_apps, g_object_ref (app));
     287                 :          0 :       g_assert (added);
     288                 :            :     }
     289                 :            : 
     290                 :          0 :   g_signal_emit (self, signals[SIGNAL_CHANGED], 0);
     291                 :          0 : }
     292                 :            : 
     293                 :            : static void
     294                 :          0 : update_listbox_row_switch (MctRestrictApplicationsSelector *self,
     295                 :            :                            GtkSwitch                       *w)
     296                 :            : {
     297                 :          0 :   GAppInfo *app = g_object_get_data (G_OBJECT (w), "GAppInfo");
     298                 :          0 :   gboolean allowed = mct_app_filter_is_appinfo_allowed (self->app_filter, app);
     299                 :            : 
     300                 :          0 :   gtk_switch_set_active (w, !allowed);
     301                 :            : 
     302         [ #  # ]:          0 :   if (allowed)
     303                 :          0 :     g_hash_table_remove (self->blocklisted_apps, app);
     304                 :            :   else
     305                 :          0 :     g_hash_table_add (self->blocklisted_apps, g_object_ref (app));
     306                 :          0 : }
     307                 :            : 
     308                 :            : static GtkWidget *
     309                 :          0 : create_row_for_app_cb (gpointer item,
     310                 :            :                        gpointer user_data)
     311                 :            : {
     312                 :          0 :   MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (user_data);
     313                 :          0 :   GAppInfo *app = G_APP_INFO (item);
     314                 :          0 :   g_autoptr(GIcon) icon = NULL;
     315                 :            :   GtkWidget *box, *w;
     316                 :            :   const gchar *app_name;
     317                 :            :   gint size;
     318                 :            :   GtkStyleContext *context;
     319                 :            : 
     320                 :          0 :   app_name = g_app_info_get_name (app);
     321                 :            : 
     322                 :          0 :   g_assert (G_IS_DESKTOP_APP_INFO (app));
     323                 :            : 
     324                 :          0 :   icon = g_app_info_get_icon (app);
     325         [ #  # ]:          0 :   if (icon == NULL)
     326                 :          0 :     icon = g_themed_icon_new ("application-x-executable");
     327                 :            :   else
     328                 :          0 :     g_object_ref (icon);
     329                 :            : 
     330                 :          0 :   box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
     331                 :          0 :   gtk_container_set_border_width (GTK_CONTAINER (box), 12);
     332                 :          0 :   gtk_widget_set_margin_end (box, 12);
     333                 :            : 
     334                 :            :   /* Icon */
     335                 :          0 :   w = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
     336                 :          0 :   gtk_icon_size_lookup (GTK_ICON_SIZE_DND, &size, NULL);
     337                 :          0 :   gtk_image_set_pixel_size (GTK_IMAGE (w), size);
     338                 :          0 :   gtk_container_add (GTK_CONTAINER (box), w);
     339                 :            : 
     340                 :            :   /* App name label */
     341                 :          0 :   w = g_object_new (GTK_TYPE_LABEL,
     342                 :            :                     "label", app_name,
     343                 :            :                     "hexpand", TRUE,
     344                 :            :                     "xalign", 0.0,
     345                 :            :                     NULL);
     346                 :          0 :   gtk_container_add (GTK_CONTAINER (box), w);
     347                 :            : 
     348                 :            :   /* Switch */
     349                 :          0 :   w = g_object_new (GTK_TYPE_SWITCH,
     350                 :            :                     "valign", GTK_ALIGN_CENTER,
     351                 :            :                     NULL);
     352                 :          0 :   context = gtk_widget_get_style_context (w);
     353                 :          0 :   gtk_style_context_add_class (context, "restricts");
     354                 :          0 :   gtk_style_context_add_provider (context,
     355                 :          0 :                                   GTK_STYLE_PROVIDER (self->css_provider),
     356                 :            :                                   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION - 1);
     357                 :          0 :   gtk_container_add (GTK_CONTAINER (box), w);
     358                 :            : 
     359                 :          0 :   gtk_widget_show_all (box);
     360                 :            : 
     361                 :            :   /* Fetch status from AccountService */
     362                 :          0 :   g_object_set_data_full (G_OBJECT (w), "GAppInfo", g_object_ref (app), g_object_unref);
     363                 :          0 :   update_listbox_row_switch (self, GTK_SWITCH (w));
     364                 :          0 :   g_signal_connect (w, "notify::active", G_CALLBACK (on_switch_active_changed_cb), self);
     365                 :            : 
     366                 :          0 :   return box;
     367                 :            : }
     368                 :            : 
     369                 :            : static gint
     370                 :          0 : compare_app_info_cb (gconstpointer a,
     371                 :            :                      gconstpointer b,
     372                 :            :                      gpointer      user_data)
     373                 :            : {
     374                 :          0 :   GAppInfo *app_a = (GAppInfo*) a;
     375                 :          0 :   GAppInfo *app_b = (GAppInfo*) b;
     376                 :            : 
     377                 :          0 :   return g_utf8_collate (g_app_info_get_name (app_a),
     378                 :          0 :                          g_app_info_get_name (app_b));
     379                 :            : }
     380                 :            : 
     381                 :            : static gint
     382                 :          0 : app_compare_id_length_cb (gconstpointer a,
     383                 :            :                           gconstpointer b)
     384                 :            : {
     385                 :          0 :   GAppInfo *info_a = (GAppInfo *) a, *info_b = (GAppInfo *) b;
     386                 :            :   const gchar *id_a, *id_b;
     387                 :            :   gsize id_a_len, id_b_len;
     388                 :            : 
     389                 :          0 :   id_a = g_app_info_get_id (info_a);
     390                 :          0 :   id_b = g_app_info_get_id (info_b);
     391                 :            : 
     392   [ #  #  #  # ]:          0 :   if (id_a == NULL && id_b == NULL)
     393                 :          0 :     return 0;
     394         [ #  # ]:          0 :   else if (id_a == NULL)
     395                 :          0 :     return -1;
     396         [ #  # ]:          0 :   else if (id_b == NULL)
     397                 :          0 :     return 1;
     398                 :            : 
     399                 :          0 :   id_a_len = strlen (id_a);
     400                 :          0 :   id_b_len = strlen (id_b);
     401         [ #  # ]:          0 :   if (id_a_len == id_b_len)
     402                 :          0 :     return strcmp (id_a, id_b);
     403                 :            :   else
     404                 :          0 :     return id_a_len - id_b_len;
     405                 :            : }
     406                 :            : 
     407                 :            : /* Elements in @added_out and @removed_out are valid as long as @old_apps and
     408                 :            :  * @new_apps are valid.
     409                 :            :  *
     410                 :            :  * Both lists have to be sorted the same before calling this function. */
     411                 :            : static void
     412                 :          0 : diff_app_lists (GList      *old_apps,
     413                 :            :                 GList      *new_apps,
     414                 :            :                 GPtrArray **added_out,
     415                 :            :                 GPtrArray **removed_out)
     416                 :            : {
     417         [ #  # ]:          0 :   g_autoptr(GPtrArray) added = g_ptr_array_new_with_free_func (NULL);
     418         [ #  # ]:          0 :   g_autoptr(GPtrArray) removed = g_ptr_array_new_with_free_func (NULL);
     419                 :            :   GList *o, *n;
     420                 :            : 
     421                 :          0 :   g_return_if_fail (added_out != NULL);
     422                 :          0 :   g_return_if_fail (removed_out != NULL);
     423                 :            : 
     424   [ #  #  #  # ]:          0 :   for (o = old_apps, n = new_apps; o != NULL || n != NULL;)
     425                 :            :     {
     426                 :            :       int comparison;
     427                 :            : 
     428         [ #  # ]:          0 :       if (o == NULL)
     429                 :          0 :         comparison = 1;
     430         [ #  # ]:          0 :       else if (n == NULL)
     431                 :          0 :         comparison = -1;
     432                 :            :       else
     433                 :          0 :         comparison = app_compare_id_length_cb (o->data, n->data);
     434                 :            : 
     435         [ #  # ]:          0 :       if (comparison < 0)
     436                 :            :         {
     437                 :          0 :           g_ptr_array_add (removed, o->data);
     438                 :          0 :           o = o->next;
     439                 :            :         }
     440         [ #  # ]:          0 :       else if (comparison > 0)
     441                 :            :         {
     442                 :          0 :           g_ptr_array_add (added, n->data);
     443                 :          0 :           n = n->next;
     444                 :            :         }
     445                 :            :       else
     446                 :            :         {
     447                 :          0 :           o = o->next;
     448                 :          0 :           n = n->next;
     449                 :            :         }
     450                 :            :     }
     451                 :            : 
     452                 :          0 :   *added_out = g_steal_pointer (&added);
     453                 :          0 :   *removed_out = g_steal_pointer (&removed);
     454                 :            : }
     455                 :            : 
     456                 :            : /* This is quite expensive to call, as there’s no way to avoid calling
     457                 :            :  * g_app_info_get_all() to see if anything’s changed; and that’s quite expensive. */
     458                 :            : static void
     459                 :          0 : reload_apps (MctRestrictApplicationsSelector *self)
     460                 :            : {
     461                 :          0 :   g_autolist(GAppInfo) old_apps = NULL;
     462                 :          0 :   g_autolist(GAppInfo) new_apps = NULL;
     463                 :          0 :   g_autoptr(GPtrArray) added_apps = NULL, removed_apps = NULL;
     464                 :          0 :   g_autoptr(GHashTable) seen_flatpak_ids = NULL;
     465                 :          0 :   g_autoptr(GHashTable) seen_executables = NULL;
     466                 :            : 
     467                 :          0 :   old_apps = g_steal_pointer (&self->cached_apps);
     468                 :          0 :   new_apps = g_app_info_get_all ();
     469                 :            : 
     470                 :            :   /* Sort the apps by increasing length of #GAppInfo ID. When coupled with the
     471                 :            :    * deduplication of flatpak IDs and executable paths, below, this should ensure that we
     472                 :            :    * pick the ‘base’ app out of any set with matching prefixes and identical app IDs (in
     473                 :            :    * case of flatpak apps) or executables (for non-flatpak apps), and show only that.
     474                 :            :    *
     475                 :            :    * This is designed to avoid listing all the components of LibreOffice for example,
     476                 :            :    * which all share an app ID and hence have the same entry in the parental controls
     477                 :            :    * app filter.
     478                 :            :    *
     479                 :            :    * Then diff the old and new lists so that the code below doesn’t end up
     480                 :            :    * removing more rows than are necessary, and hence potentially losing
     481                 :            :    * in-progress user input. */
     482                 :          0 :   new_apps = g_list_sort (new_apps, app_compare_id_length_cb);
     483                 :          0 :   diff_app_lists (old_apps, new_apps, &added_apps, &removed_apps);
     484                 :            : 
     485                 :          0 :   g_debug ("%s: Diffed old and new app lists: %u apps added, %u apps removed",
     486                 :            :            G_STRFUNC, added_apps->len, removed_apps->len);
     487                 :            : 
     488                 :          0 :   seen_flatpak_ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
     489                 :          0 :   seen_executables = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
     490                 :            : 
     491                 :            :   /* Remove items first. */
     492         [ #  # ]:          0 :   for (guint i = 0; i < removed_apps->len; i++)
     493                 :            :     {
     494                 :          0 :       GAppInfo *app = removed_apps->pdata[i];
     495                 :            :       guint pos;
     496                 :            :       gboolean found;
     497                 :            : 
     498                 :          0 :       found = g_list_store_find_with_equal_func (self->apps, app,
     499                 :            :                                                  (GEqualFunc) g_app_info_equal, &pos);
     500                 :            : 
     501                 :            :       /* The app being removed may have not passed the condition checks below
     502                 :            :        * to have been added to self->apps. */
     503         [ #  # ]:          0 :       if (!found)
     504                 :          0 :         continue;
     505                 :            : 
     506                 :          0 :       g_debug ("Removing app ‘%s’", g_app_info_get_id (app));
     507                 :          0 :       g_list_store_remove (self->apps, pos);
     508                 :            :     }
     509                 :            : 
     510                 :            :   /* Now add the new items. */
     511         [ #  # ]:          0 :   for (guint i = 0; i < added_apps->len; i++)
     512                 :            :     {
     513                 :          0 :       GAppInfo *app = added_apps->pdata[i];
     514                 :            :       const gchar *app_name;
     515                 :            :       const gchar * const *supported_types;
     516                 :            : 
     517                 :          0 :       app_name = g_app_info_get_name (app);
     518                 :            : 
     519                 :          0 :       supported_types = g_app_info_get_supported_types (app);
     520                 :            : 
     521   [ #  #  #  #  :          0 :       if (!G_IS_DESKTOP_APP_INFO (app) ||
          #  #  #  #  #  
                      # ]
     522                 :          0 :           !g_app_info_should_show (app) ||
     523   [ #  #  #  # ]:          0 :           app_name[0] == '\0' ||
     524                 :            :           /* FIXME: Only list flatpak apps and apps with X-Parental-Controls
     525                 :            :            * key set for now; we really need a system-wide MAC to be able to
     526                 :            :            * reliably support blocklisting system programs. */
     527         [ #  # ]:          0 :           (!g_desktop_app_info_has_key (G_DESKTOP_APP_INFO (app), "X-Flatpak") &&
     528         [ #  # ]:          0 :            !g_desktop_app_info_has_key (G_DESKTOP_APP_INFO (app), "X-Parental-Controls")) ||
     529                 :            :           /* Web browsers are special cased */
     530         [ #  # ]:          0 :           (supported_types && g_strv_contains (supported_types, WEB_BROWSERS_CONTENT_TYPE)))
     531                 :            :         {
     532                 :          0 :           continue;
     533                 :            :         }
     534                 :            : 
     535         [ #  # ]:          0 :       if (g_desktop_app_info_has_key (G_DESKTOP_APP_INFO (app), "X-Flatpak"))
     536                 :            :         {
     537         [ #  # ]:          0 :           g_autofree gchar *flatpak_id = NULL;
     538                 :            : 
     539                 :          0 :           flatpak_id = g_desktop_app_info_get_string (G_DESKTOP_APP_INFO (app), "X-Flatpak");
     540                 :          0 :           g_debug ("Processing app ‘%s’ (Exec=%s, X-Flatpak=%s)",
     541                 :            :                    g_app_info_get_id (app),
     542                 :            :                    g_app_info_get_executable (app),
     543                 :            :                    flatpak_id);
     544                 :            : 
     545                 :            :           /* Have we seen this flatpak ID before? */
     546         [ #  # ]:          0 :           if (!g_hash_table_add (seen_flatpak_ids, g_steal_pointer (&flatpak_id)))
     547                 :            :             {
     548                 :          0 :               g_debug (" → Skipping ‘%s’ due to seeing its flatpak ID already",
     549                 :            :                        g_app_info_get_id (app));
     550                 :          0 :               continue;
     551                 :            :             }
     552                 :            :         }
     553         [ #  # ]:          0 :       else if (g_desktop_app_info_has_key (G_DESKTOP_APP_INFO (app), "X-Parental-Controls"))
     554                 :            :         {
     555         [ #  # ]:          0 :           g_autofree gchar *parental_controls_type = NULL;
     556         [ #  # ]:          0 :           g_autofree gchar *executable = NULL;
     557                 :            : 
     558                 :          0 :           parental_controls_type = g_desktop_app_info_get_string (G_DESKTOP_APP_INFO (app),
     559                 :            :                                                                   "X-Parental-Controls");
     560                 :            :           /* Ignore X-Parental-Controls=none */
     561         [ #  # ]:          0 :           if (g_strcmp0 (parental_controls_type, "none") == 0)
     562                 :          0 :             continue;
     563                 :            : 
     564                 :          0 :           executable = g_strdup (g_app_info_get_executable (app));
     565                 :          0 :           g_debug ("Processing app ‘%s’ (Exec=%s, X-Parental-Controls=%s)",
     566                 :            :                    g_app_info_get_id (app),
     567                 :            :                    executable,
     568                 :            :                    parental_controls_type);
     569                 :            : 
     570                 :            :           /* Have we seen this executable before? */
     571         [ #  # ]:          0 :           if (!g_hash_table_add (seen_executables, g_steal_pointer (&executable)))
     572                 :            :             {
     573                 :          0 :               g_debug (" → Skipping ‘%s’ due to seeing its executable already",
     574                 :            :                        g_app_info_get_id (app));
     575                 :          0 :               continue;
     576                 :            :             }
     577                 :            :         }
     578                 :            : 
     579                 :          0 :       g_list_store_insert_sorted (self->apps,
     580                 :            :                                   app,
     581                 :            :                                   compare_app_info_cb,
     582                 :            :                                   self);
     583                 :            :     }
     584                 :            : 
     585                 :            :   /* Update the cache for next time. */
     586                 :          0 :   self->cached_apps = g_steal_pointer (&new_apps);
     587                 :          0 : }
     588                 :            : 
     589                 :            : static void
     590                 :          0 : app_info_changed_cb (GAppInfoMonitor *monitor,
     591                 :            :                      gpointer         user_data)
     592                 :            : {
     593                 :          0 :   MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (user_data);
     594                 :            : 
     595                 :          0 :   reload_apps (self);
     596                 :          0 : }
     597                 :            : 
     598                 :            : /* Will return %NULL if @flatpak_id is not installed. */
     599                 :            : static gchar *
     600                 :          0 : get_flatpak_ref_for_app_id (MctRestrictApplicationsSelector *self,
     601                 :            :                             const gchar                     *flatpak_id,
     602                 :            :                             GCancellable                    *cancellable)
     603                 :            : {
     604                 :          0 :   g_autoptr(FlatpakInstalledRef) ref = NULL;
     605                 :          0 :   g_autoptr(GError) local_error = NULL;
     606                 :            : 
     607                 :          0 :   g_assert (self->system_installation != NULL);
     608                 :          0 :   g_assert (self->user_installation != NULL);
     609                 :            : 
     610                 :            :   /* FIXME technically this does local file I/O and should be async */
     611                 :          0 :   ref = flatpak_installation_get_current_installed_app (self->user_installation,
     612                 :            :                                                         flatpak_id,
     613                 :            :                                                         cancellable,
     614                 :            :                                                         &local_error);
     615                 :            : 
     616   [ #  #  #  # ]:          0 :   if (local_error != NULL &&
     617                 :          0 :       !g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_NOT_INSTALLED))
     618                 :            :     {
     619                 :          0 :       g_warning ("Error searching for Flatpak ref: %s", local_error->message);
     620                 :          0 :       return NULL;
     621                 :            :     }
     622                 :            : 
     623                 :          0 :   g_clear_error (&local_error);
     624                 :            : 
     625   [ #  #  #  # ]:          0 :   if (!ref || !flatpak_installed_ref_get_is_current (ref))
     626                 :            :     {
     627                 :            :       /* FIXME technically this does local file I/O and should be async */
     628                 :          0 :       ref = flatpak_installation_get_current_installed_app (self->system_installation,
     629                 :            :                                                             flatpak_id,
     630                 :            :                                                             cancellable,
     631                 :            :                                                             &local_error);
     632         [ #  # ]:          0 :       if (local_error != NULL)
     633                 :            :         {
     634         [ #  # ]:          0 :           if (!g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_NOT_INSTALLED))
     635                 :          0 :             g_warning ("Error searching for Flatpak ref: %s", local_error->message);
     636                 :          0 :           return NULL;
     637                 :            :         }
     638                 :            :     }
     639                 :            : 
     640                 :          0 :   return flatpak_ref_format_ref (FLATPAK_REF (ref));
     641                 :            : }
     642                 :            : 
     643                 :            : /**
     644                 :            :  * mct_restrict_applications_selector_new:
     645                 :            :  * @app_filter: (transfer none): app filter to configure the selector from initially
     646                 :            :  *
     647                 :            :  * Create a new #MctRestrictApplicationsSelector widget.
     648                 :            :  *
     649                 :            :  * Returns: (transfer full): a new restricted applications selector
     650                 :            :  * Since: 0.5.0
     651                 :            :  */
     652                 :            : MctRestrictApplicationsSelector *
     653                 :          0 : mct_restrict_applications_selector_new (MctAppFilter *app_filter)
     654                 :            : {
     655                 :          0 :   g_return_val_if_fail (app_filter != NULL, NULL);
     656                 :            : 
     657                 :          0 :   return g_object_new (MCT_TYPE_RESTRICT_APPLICATIONS_SELECTOR,
     658                 :            :                        "app-filter", app_filter,
     659                 :            :                        NULL);
     660                 :            : }
     661                 :            : 
     662                 :            : /**
     663                 :            :  * mct_restrict_applications_selector_build_app_filter:
     664                 :            :  * @self: an #MctRestrictApplicationsSelector
     665                 :            :  * @builder: an existing #MctAppFilterBuilder to modify
     666                 :            :  *
     667                 :            :  * Get the app filter settings currently configured in the selector, by modifying
     668                 :            :  * the given @builder.
     669                 :            :  *
     670                 :            :  * Since: 0.5.0
     671                 :            :  */
     672                 :            : void
     673                 :          0 : mct_restrict_applications_selector_build_app_filter (MctRestrictApplicationsSelector *self,
     674                 :            :                                                      MctAppFilterBuilder             *builder)
     675                 :            : {
     676                 :            :   GDesktopAppInfo *app;
     677                 :            :   GHashTableIter iter;
     678                 :            : 
     679                 :          0 :   g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_SELECTOR (self));
     680                 :          0 :   g_return_if_fail (builder != NULL);
     681                 :            : 
     682                 :          0 :   g_hash_table_iter_init (&iter, self->blocklisted_apps);
     683         [ #  # ]:          0 :   while (g_hash_table_iter_next (&iter, (gpointer) &app, NULL))
     684                 :            :     {
     685         [ #  # ]:          0 :       g_autofree gchar *flatpak_id = NULL;
     686                 :            : 
     687                 :          0 :       flatpak_id = g_desktop_app_info_get_string (app, "X-Flatpak");
     688         [ #  # ]:          0 :       if (flatpak_id)
     689                 :          0 :         flatpak_id = g_strstrip (flatpak_id);
     690                 :            : 
     691         [ #  # ]:          0 :       if (flatpak_id)
     692                 :            :         {
     693         [ #  # ]:          0 :           g_autofree gchar *flatpak_ref = get_flatpak_ref_for_app_id (self, flatpak_id, NULL);
     694                 :            : 
     695         [ #  # ]:          0 :           if (!flatpak_ref)
     696                 :            :             {
     697                 :          0 :               g_warning ("Skipping blocklisting Flatpak ID ‘%s’ due to it not being installed", flatpak_id);
     698                 :          0 :               continue;
     699                 :            :             }
     700                 :            : 
     701                 :          0 :           g_debug ("\t\t → Blocklisting Flatpak ref: %s", flatpak_ref);
     702                 :          0 :           mct_app_filter_builder_blocklist_flatpak_ref (builder, flatpak_ref);
     703                 :            :         }
     704                 :            :       else
     705                 :            :         {
     706                 :          0 :           const gchar *executable = g_app_info_get_executable (G_APP_INFO (app));
     707         [ #  # ]:          0 :           g_autofree gchar *path = g_find_program_in_path (executable);
     708                 :            : 
     709         [ #  # ]:          0 :           if (!path)
     710                 :            :             {
     711                 :          0 :               g_warning ("Skipping blocklisting executable ‘%s’ due to it not being found", executable);
     712                 :          0 :               continue;
     713                 :            :             }
     714                 :            : 
     715                 :          0 :           g_debug ("\t\t → Blocklisting path: %s", path);
     716                 :          0 :           mct_app_filter_builder_blocklist_path (builder, path);
     717                 :            :         }
     718                 :            :     }
     719                 :            : }
     720                 :            : 
     721                 :            : /**
     722                 :            :  * mct_restrict_applications_selector_get_app_filter:
     723                 :            :  * @self: an #MctRestrictApplicationsSelector
     724                 :            :  *
     725                 :            :  * Get the value of #MctRestrictApplicationsSelector:app-filter. If the property
     726                 :            :  * was originally set to %NULL, this will be the empty app filter.
     727                 :            :  *
     728                 :            :  * Returns: (transfer none) (not nullable): the initial app filter used to
     729                 :            :  *    populate the selector
     730                 :            :  * Since: 0.5.0
     731                 :            :  */
     732                 :            : MctAppFilter *
     733                 :          0 : mct_restrict_applications_selector_get_app_filter (MctRestrictApplicationsSelector *self)
     734                 :            : {
     735                 :          0 :   g_return_val_if_fail (MCT_IS_RESTRICT_APPLICATIONS_SELECTOR (self), NULL);
     736                 :            : 
     737                 :          0 :   return self->app_filter;
     738                 :            : }
     739                 :            : 
     740                 :            : /**
     741                 :            :  * mct_restrict_applications_selector_set_app_filter:
     742                 :            :  * @self: an #MctRestrictApplicationsSelector
     743                 :            :  * @app_filter: (nullable) (transfer none): the app filter to configure the selector
     744                 :            :  *    from, or %NULL to use an empty app filter
     745                 :            :  *
     746                 :            :  * Set the value of #MctRestrictApplicationsSelector:app-filter.
     747                 :            :  *
     748                 :            :  * This will overwrite any user changes to the selector, so they should be saved
     749                 :            :  * first using mct_restrict_applications_selector_build_app_filter() if desired.
     750                 :            :  *
     751                 :            :  * Since: 0.5.0
     752                 :            :  */
     753                 :            : void
     754                 :          0 : mct_restrict_applications_selector_set_app_filter (MctRestrictApplicationsSelector *self,
     755                 :            :                                                    MctAppFilter                    *app_filter)
     756                 :            : {
     757         [ #  # ]:          0 :   g_autoptr(MctAppFilter) owned_app_filter = NULL;
     758                 :            :   guint n_apps;
     759                 :            : 
     760                 :          0 :   g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_SELECTOR (self));
     761                 :            : 
     762                 :            :   /* Default app filter, typically for when we’re instantiated by #GtkBuilder. */
     763         [ #  # ]:          0 :   if (app_filter == NULL)
     764                 :            :     {
     765                 :          0 :       g_auto(MctAppFilterBuilder) builder = MCT_APP_FILTER_BUILDER_INIT ();
     766                 :          0 :       owned_app_filter = mct_app_filter_builder_end (&builder);
     767                 :          0 :       app_filter = owned_app_filter;
     768                 :            :     }
     769                 :            : 
     770         [ #  # ]:          0 :   if (app_filter == self->app_filter)
     771                 :          0 :     return;
     772                 :            : 
     773         [ #  # ]:          0 :   g_clear_pointer (&self->app_filter, mct_app_filter_unref);
     774                 :          0 :   self->app_filter = mct_app_filter_ref (app_filter);
     775                 :            : 
     776                 :            :   /* Update the status of each app row. */
     777                 :          0 :   n_apps = g_list_model_get_n_items (G_LIST_MODEL (self->apps));
     778                 :            : 
     779         [ #  # ]:          0 :   for (guint i = 0; i < n_apps; i++)
     780                 :            :     {
     781                 :            :       GtkListBoxRow *row;
     782                 :            :       GtkWidget *box, *w;
     783                 :          0 :       g_autoptr(GList) children = NULL;  /* (element-type GtkWidget) */
     784                 :            : 
     785                 :            :       /* Navigate the widget hierarchy set up in create_row_for_app_cb(). */
     786                 :          0 :       row = gtk_list_box_get_row_at_index (self->listbox, i);
     787                 :          0 :       g_assert (row != NULL && GTK_IS_LIST_BOX_ROW (row));
     788                 :            : 
     789                 :          0 :       box = gtk_bin_get_child (GTK_BIN (row));
     790                 :          0 :       g_assert (box != NULL && GTK_IS_BOX (box));
     791                 :            : 
     792                 :          0 :       children = gtk_container_get_children (GTK_CONTAINER (box));
     793                 :          0 :       g_assert (children != NULL);
     794                 :            : 
     795                 :          0 :       w = g_list_nth_data (children, 2);
     796                 :          0 :       g_assert (w != NULL && GTK_IS_SWITCH (w));
     797                 :            : 
     798                 :          0 :       update_listbox_row_switch (self, GTK_SWITCH (w));
     799                 :            :     }
     800                 :            : 
     801                 :          0 :   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_APP_FILTER]);
     802                 :            : }

Generated by: LCOV version 1.14