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

Generated by: LCOV version 1.16