File: restrict-applications-dialog.c

package info (click to toggle)
malcontent 0.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,756 kB
  • sloc: ansic: 7,414; python: 418; xml: 377; sh: 36; makefile: 14
file content (429 lines) | stat: -rw-r--r-- 15,075 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 *
 * Copyright © 2020 Endless Mobile, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *  - Philip Withnall <withnall@endlessm.com>
 */

#include "config.h"

#include <gio/gio.h>
#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <adwaita.h>

#include "restrict-applications-dialog.h"
#include "restrict-applications-selector.h"


static void update_description (MctRestrictApplicationsDialog *self);

/**
 * MctRestrictApplicationsDialog:
 *
 * The ‘Restrict Applications’ dialog is a dialog which shows the available
 * applications on the system alongside a column of toggle switches, which
 * allows the given user to be prevented from running each application.
 *
 * The dialog contains a single #MctRestrictApplicationsSelector. It takes a
 * #MctRestrictApplicationsDialog:user and
 * #MctRestrictApplicationsDialog:app-filter as input to set up the UI, and
 * returns its output as set of modifications to a given #MctAppFilterBuilder
 * using mct_restrict_applications_dialog_build_app_filter().
 *
 * Since: 0.5.0
 */
struct _MctRestrictApplicationsDialog
{
  AdwPreferencesDialog parent_instance;

  MctRestrictApplicationsSelector *selector;
  AdwPreferencesGroup *group;
  GtkSearchEntry *search_entry;

  MctAppFilter *app_filter;  /* (owned) (not nullable) */
  gchar *user_display_name;  /* (owned) (nullable) */
};

static void search_entry_stop_search_cb (GtkSearchEntry *search_entry,
                                         gpointer        user_data);
static gboolean focus_search_cb (GtkWidget *widget,
                                 GVariant  *arguments,
                                 gpointer   user_data);

G_DEFINE_TYPE (MctRestrictApplicationsDialog, mct_restrict_applications_dialog, ADW_TYPE_PREFERENCES_DIALOG)

typedef enum
{
  PROP_APP_FILTER = 1,
  PROP_USER_DISPLAY_NAME,
} MctRestrictApplicationsDialogProperty;

static GParamSpec *properties[PROP_USER_DISPLAY_NAME + 1];

static void
mct_restrict_applications_dialog_constructed (GObject *obj)
{
  MctRestrictApplicationsDialog *self = MCT_RESTRICT_APPLICATIONS_DIALOG (obj);

  g_assert (self->app_filter != NULL);
  g_assert (self->user_display_name == NULL ||
            (*self->user_display_name != '\0' &&
             g_utf8_validate (self->user_display_name, -1, NULL)));

  G_OBJECT_CLASS (mct_restrict_applications_dialog_parent_class)->constructed (obj);
}

static void
mct_restrict_applications_dialog_get_property (GObject    *object,
                                               guint       prop_id,
                                               GValue     *value,
                                               GParamSpec *pspec)
{
  MctRestrictApplicationsDialog *self = MCT_RESTRICT_APPLICATIONS_DIALOG (object);

  switch ((MctRestrictApplicationsDialogProperty) prop_id)
    {
    case PROP_APP_FILTER:
      g_value_set_boxed (value, self->app_filter);
      break;

    case PROP_USER_DISPLAY_NAME:
      g_value_set_string (value, self->user_display_name);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
mct_restrict_applications_dialog_set_property (GObject      *object,
                                               guint         prop_id,
                                               const GValue *value,
                                               GParamSpec   *pspec)
{
  MctRestrictApplicationsDialog *self = MCT_RESTRICT_APPLICATIONS_DIALOG (object);

  switch ((MctRestrictApplicationsDialogProperty) prop_id)
    {
    case PROP_APP_FILTER:
      mct_restrict_applications_dialog_set_app_filter (self, g_value_get_boxed (value));
      break;

    case PROP_USER_DISPLAY_NAME:
      mct_restrict_applications_dialog_set_user_display_name (self, g_value_get_string (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
mct_restrict_applications_dialog_dispose (GObject *object)
{
  MctRestrictApplicationsDialog *self = (MctRestrictApplicationsDialog *)object;

  g_clear_pointer (&self->app_filter, mct_app_filter_unref);
  g_clear_pointer (&self->user_display_name, g_free);

  G_OBJECT_CLASS (mct_restrict_applications_dialog_parent_class)->dispose (object);
}

static void
mct_restrict_applications_dialog_map (GtkWidget *widget)
{
  MctRestrictApplicationsDialog *self = (MctRestrictApplicationsDialog *)widget;

  GTK_WIDGET_CLASS (mct_restrict_applications_dialog_parent_class)->map (widget);

  /* Clear and focus the search entry, in case the dialogue is being shown for
   * a second time. */
  gtk_editable_set_text (GTK_EDITABLE (self->search_entry), "");
  gtk_widget_grab_focus (GTK_WIDGET (self->search_entry));
}

static void
mct_restrict_applications_dialog_class_init (MctRestrictApplicationsDialogClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->constructed = mct_restrict_applications_dialog_constructed;
  object_class->get_property = mct_restrict_applications_dialog_get_property;
  object_class->set_property = mct_restrict_applications_dialog_set_property;
  object_class->dispose = mct_restrict_applications_dialog_dispose;

  widget_class->map = mct_restrict_applications_dialog_map;

  /**
   * MctRestrictApplicationsDialog:app-filter: (not nullable)
   *
   * The user’s current app filter, used to set up the dialog. As app filters
   * are immutable, it is not updated as the dialog is changed. Use
   * mct_restrict_applications_dialog_build_app_filter() to build the new app
   * filter.
   *
   * Since: 0.5.0
   */
  properties[PROP_APP_FILTER] =
      g_param_spec_boxed ("app-filter",
                          "App Filter",
                          "The user’s current app filter, used to set up the dialog.",
                          MCT_TYPE_APP_FILTER,
                          G_PARAM_READWRITE |
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_STATIC_STRINGS |
                          G_PARAM_EXPLICIT_NOTIFY);

  /**
   * MctRestrictApplicationsDialog:user-display-name: (nullable)
   *
   * The display name for the currently selected user account, or %NULL if no
   * user is selected. This will typically be the user’s full name (if known)
   * or their username.
   *
   * If set, it must be valid UTF-8 and non-empty.
   *
   * Since: 0.5.0
   */
  properties[PROP_USER_DISPLAY_NAME] =
      g_param_spec_string ("user-display-name",
                           "User Display Name",
                           "The display name for the currently selected user account, or %NULL if no user is selected.",
                           NULL,
                           G_PARAM_READWRITE |
                           G_PARAM_STATIC_STRINGS |
                           G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);

  gtk_widget_class_set_template_from_resource (widget_class, "/org/freedesktop/MalcontentUi/ui/restrict-applications-dialog.ui");

  gtk_widget_class_bind_template_child (widget_class, MctRestrictApplicationsDialog, selector);
  gtk_widget_class_bind_template_child (widget_class, MctRestrictApplicationsDialog, group);
  gtk_widget_class_bind_template_child (widget_class, MctRestrictApplicationsDialog, search_entry);

  gtk_widget_class_bind_template_callback (widget_class, search_entry_stop_search_cb);

  gtk_widget_class_add_binding (widget_class,
                                GDK_KEY_f, GDK_CONTROL_MASK,
                                focus_search_cb,
                                NULL);

}

static void
mct_restrict_applications_dialog_init (MctRestrictApplicationsDialog *self)
{
  /* Ensure the types used in the UI are registered. */
  g_type_ensure (MCT_TYPE_RESTRICT_APPLICATIONS_SELECTOR);

  gtk_widget_init_template (GTK_WIDGET (self));

  gtk_search_entry_set_key_capture_widget (self->search_entry, GTK_WIDGET (self));
}

static void
update_description (MctRestrictApplicationsDialog *self)
{
  g_autofree gchar *description = NULL;

  if (self->user_display_name == NULL)
    {
      adw_preferences_group_set_description (self->group, NULL);
      return;
    }

  /* Translators: the placeholder is a user’s full name */
  description = g_strdup_printf (_("Restrict %s from using the following installed applications."),
                                 self->user_display_name);
  adw_preferences_group_set_description (self->group, description);
}

static void
search_entry_stop_search_cb (GtkSearchEntry *search_entry,
                             gpointer        user_data)
{
  /* Clear the search text as the search filtering is bound to that. */
  gtk_editable_set_text (GTK_EDITABLE (search_entry), "");
}

static gboolean
focus_search_cb (GtkWidget *widget,
                 GVariant  *arguments,
                 gpointer   user_data)
{
  MctRestrictApplicationsDialog *self = MCT_RESTRICT_APPLICATIONS_DIALOG (widget);

  gtk_widget_grab_focus (GTK_WIDGET (self->search_entry));
  return TRUE;
}

/**
 * mct_restrict_applications_dialog_new:
 * @app_filter: (transfer none): the initial app filter configuration to show
 * @user_display_name: (transfer none) (nullable): the display name of the user
 *    to show the app filter for, or %NULL if no user is selected
 *
 * Create a new #MctRestrictApplicationsDialog widget.
 *
 * Returns: (transfer full): a new restricted applications editing dialog
 * Since: 0.5.0
 */
MctRestrictApplicationsDialog *
mct_restrict_applications_dialog_new (MctAppFilter *app_filter,
                                      const gchar  *user_display_name)
{
  g_return_val_if_fail (app_filter != NULL, NULL);
  g_return_val_if_fail (user_display_name == NULL ||
                        (*user_display_name != '\0' &&
                         g_utf8_validate (user_display_name, -1, NULL)), NULL);

  return g_object_new (MCT_TYPE_RESTRICT_APPLICATIONS_DIALOG,
                       "app-filter", app_filter,
                       "user-display-name", user_display_name,
                       NULL);
}

/**
 * mct_restrict_applications_dialog_get_app_filter:
 * @self: an #MctRestrictApplicationsDialog
 *
 * Get the value of #MctRestrictApplicationsDialog:app-filter. If the property
 * was originally set to %NULL, this will be the empty app filter.
 *
 * Returns: (transfer none) (not nullable): the initial app filter used to
 *    populate the dialog
 * Since: 0.5.0
 */
MctAppFilter *
mct_restrict_applications_dialog_get_app_filter (MctRestrictApplicationsDialog *self)
{
  g_return_val_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self), NULL);

  return self->app_filter;
}

/**
 * mct_restrict_applications_dialog_set_app_filter:
 * @self: an #MctRestrictApplicationsDialog
 * @app_filter: (nullable) (transfer none): the app filter to configure the dialog
 *    from, or %NULL to use an empty app filter
 *
 * Set the value of #MctRestrictApplicationsDialog:app-filter.
 *
 * Since: 0.5.0
 */
void
mct_restrict_applications_dialog_set_app_filter (MctRestrictApplicationsDialog *self,
                                                 MctAppFilter                  *app_filter)
{
  g_autoptr(MctAppFilter) owned_app_filter = NULL;

  g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self));

  /* Default app filter, typically for when we’re instantiated by #GtkBuilder. */
  if (app_filter == NULL)
    {
      g_auto(MctAppFilterBuilder) builder = MCT_APP_FILTER_BUILDER_INIT ();
      owned_app_filter = mct_app_filter_builder_end (&builder);
      app_filter = owned_app_filter;
    }

  if (app_filter == self->app_filter)
    return;

  g_clear_pointer (&self->app_filter, mct_app_filter_unref);
  self->app_filter = mct_app_filter_ref (app_filter);

  mct_restrict_applications_selector_set_app_filter (self->selector, self->app_filter);

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_APP_FILTER]);
}

/**
 * mct_restrict_applications_dialog_get_user_display_name:
 * @self: an #MctRestrictApplicationsDialog
 *
 * Get the value of #MctRestrictApplicationsDialog:user-display-name.
 *
 * Returns: (transfer none) (nullable): the display name of the user the dialog
 *    is configured for, or %NULL if unknown
 * Since: 0.5.0
 */
const gchar *
mct_restrict_applications_dialog_get_user_display_name (MctRestrictApplicationsDialog *self)
{
  g_return_val_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self), NULL);

  return self->user_display_name;
}

/**
 * mct_restrict_applications_dialog_set_user_display_name:
 * @self: an #MctRestrictApplicationsDialog
 * @user_display_name: (nullable) (transfer none): the display name of the user
 *    to configure the dialog for, or %NULL if unknown
 *
 * Set the value of #MctRestrictApplicationsDialog:user-display-name.
 *
 * Since: 0.5.0
 */
void
mct_restrict_applications_dialog_set_user_display_name (MctRestrictApplicationsDialog *self,
                                                        const gchar                   *user_display_name)
{
  g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self));
  g_return_if_fail (user_display_name == NULL ||
                    (*user_display_name != '\0' &&
                     g_utf8_validate (user_display_name, -1, NULL)));

  if (g_strcmp0 (self->user_display_name, user_display_name) == 0)
    return;

  g_clear_pointer (&self->user_display_name, g_free);
  self->user_display_name = g_strdup (user_display_name);

  update_description (self);
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USER_DISPLAY_NAME]);
}

/**
 * mct_restrict_applications_dialog_build_app_filter:
 * @self: an #MctRestrictApplicationsDialog
 * @builder: an existing #MctAppFilterBuilder to modify
 *
 * Get the app filter settings currently configured in the dialog, by modifying
 * the given @builder.
 *
 * Typically this will be called in the handler for #GtkDialog::response.
 *
 * Since: 0.5.0
 */
void
mct_restrict_applications_dialog_build_app_filter (MctRestrictApplicationsDialog *self,
                                                   MctAppFilterBuilder           *builder)
{
  g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_DIALOG (self));
  g_return_if_fail (builder != NULL);

  mct_restrict_applications_selector_build_app_filter (self->selector, builder);
}