-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathexm-installed-page.c
More file actions
552 lines (448 loc) · 17.8 KB
/
exm-installed-page.c
File metadata and controls
552 lines (448 loc) · 17.8 KB
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
* exm-installed-page.c
*
* Copyright 2022-2025 Matthew Jakeman <mjakeman26@outlook.co.nz>
*
* 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 3 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/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "exm-installed-page.h"
#include "exm-config.h"
#include "exm-enums.h"
#include "exm-extension-row.h"
#include "exm-types.h"
#include "exm-window.h"
#include "local/exm-manager.h"
#include <glib/gi18n.h>
struct _ExmInstalledPage
{
GtkWidget parent_instance;
ExmManager *manager;
// Template Widgets
GtkStack *stack;
AdwBanner *updates_banner;
AdwSwitchRow *global_toggle;
AdwSwitchRow *auto_check_updates_toggle;
AdwPreferencesGroup *user_prefs_group;
AdwPreferencesGroup *system_prefs_group;
AdwPreferencesGroup *search_prefs_group;
GtkFilterListModel *search_list_model;
gboolean sort_enabled_first;
gboolean auto_check_updates;
gboolean search_mode_enabled;
const char *search_query;
guint signal_id;
};
G_DEFINE_FINAL_TYPE (ExmInstalledPage, exm_installed_page, GTK_TYPE_WIDGET)
enum {
PROP_0,
PROP_MANAGER,
PROP_SORT_ENABLED_FIRST,
PROP_AUTO_CHECK_UPDATES,
PROP_SEARCH_MODE_ENABLED,
PROP_SEARCH_QUERY,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
static void
invalidate_model_bindings (ExmInstalledPage *self);
static void
switch_page (ExmInstalledPage *self);
ExmInstalledPage *
exm_installed_page_new (void)
{
return g_object_new (EXM_TYPE_INSTALLED_PAGE, NULL);
}
static void
exm_installed_page_finalize (GObject *object)
{
GtkWidget *child;
ExmInstalledPage *self = (ExmInstalledPage *)object;
child = gtk_widget_get_first_child (GTK_WIDGET (self));
gtk_widget_unparent (child);
G_OBJECT_CLASS (exm_installed_page_parent_class)->finalize (object);
}
static void
exm_installed_page_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
ExmInstalledPage *self = EXM_INSTALLED_PAGE (object);
switch (prop_id)
{
case PROP_MANAGER:
g_value_set_object (value, self->manager);
break;
case PROP_SORT_ENABLED_FIRST:
g_value_set_boolean (value, self->sort_enabled_first);
break;
case PROP_AUTO_CHECK_UPDATES:
g_value_set_boolean (value, self->auto_check_updates);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
exm_installed_page_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
ExmInstalledPage *self = EXM_INSTALLED_PAGE (object);
switch (prop_id)
{
case PROP_MANAGER:
self->manager = g_value_get_object (value);
break;
case PROP_SORT_ENABLED_FIRST:
self->sort_enabled_first = g_value_get_boolean (value);
invalidate_model_bindings (self);
break;
case PROP_AUTO_CHECK_UPDATES:
self->auto_check_updates = g_value_get_boolean (value);
break;
case PROP_SEARCH_MODE_ENABLED:
self->search_mode_enabled = g_value_get_boolean (value);
switch_page (self);
break;
case PROP_SEARCH_QUERY:
self->search_query = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static GtkWidget *
widget_factory (ExmExtension *extension,
ExmInstalledPage *self)
{
ExmExtensionRow *row;
g_return_val_if_fail (EXM_IS_EXTENSION (extension), GTK_WIDGET (NULL));
g_return_val_if_fail (EXM_IS_INSTALLED_PAGE (self), GTK_WIDGET (NULL));
row = exm_extension_row_new (extension, self->manager);
return GTK_WIDGET (row);
}
static int
compare_enabled (ExmExtension *this,
ExmExtension *other,
gpointer user_data G_GNUC_UNUSED)
{
g_return_val_if_fail (EXM_IS_EXTENSION (this), 2);
g_return_val_if_fail (EXM_IS_EXTENSION (other), 2); // Crash
ExmExtensionState this_state;
ExmExtensionState other_state;
g_object_get (this, "state", &this_state, NULL);
g_object_get (other, "state", &other_state, NULL);
gboolean this_enabled = (this_state == EXM_EXTENSION_STATE_ACTIVE);
gboolean other_enabled = (other_state == EXM_EXTENSION_STATE_ACTIVE);
if ((this_enabled && other_enabled) || (!this_enabled && !other_enabled))
return 0;
else if (this_enabled && !other_enabled)
return -1;
else if (!this_enabled && other_enabled)
return 1;
g_assert_not_reached ();
}
static void
on_search_changed (ExmInstalledPage *self,
GParamSpec *pspec G_GNUC_UNUSED,
gpointer user_data)
{
GtkStringFilter *search_filter = GTK_STRING_FILTER (user_data);
gtk_string_filter_set_search (search_filter, self->search_query);
}
static void
switch_page (ExmInstalledPage *self)
{
if (self->search_mode_enabled
&& g_list_model_get_n_items (G_LIST_MODEL (self->search_list_model)) > 0)
gtk_stack_set_visible_child_name (self->stack , "page_results");
else if (self->search_mode_enabled)
gtk_stack_set_visible_child_name (self->stack , "page_no_results");
else if (gtk_widget_get_visible (GTK_WIDGET (self->user_prefs_group))
|| gtk_widget_get_visible (GTK_WIDGET (self->system_prefs_group)))
gtk_stack_set_visible_child_name (self->stack, "page_list");
else
gtk_stack_set_visible_child_name (self->stack , "page_empty");
}
static void
on_visible_stack_changed (GObject *object G_GNUC_UNUSED,
GParamSpec *pspec G_GNUC_UNUSED,
gpointer user_data)
{
ExmInstalledPage *self = EXM_INSTALLED_PAGE (user_data);
switch_page (self);
}
static void
bind_list_box (GListModel *model,
ExmInstalledPage *self)
{
GtkExpression *expression;
GtkStringSorter *alphabetical_sorter;
GtkSortListModel *sorted_model;
GtkStringFilter *search_filter;
GtkBoolFilter *is_user_filter;
GtkFilterListModel *filtered_model;
g_return_if_fail (G_IS_LIST_MODEL (model));
// Sort alphabetically
expression = gtk_property_expression_new (EXM_TYPE_EXTENSION, NULL, "name");
alphabetical_sorter = gtk_string_sorter_new (expression);
if (self->sort_enabled_first)
{
GtkCustomSorter *enabled_sorter;
GtkMultiSorter *multi_sorter;
// Sort by enabled
enabled_sorter = gtk_custom_sorter_new ((GCompareDataFunc) compare_enabled, NULL, NULL);
multi_sorter = gtk_multi_sorter_new ();
gtk_multi_sorter_append (multi_sorter, GTK_SORTER (enabled_sorter));
gtk_multi_sorter_append (multi_sorter, GTK_SORTER (alphabetical_sorter));
sorted_model = gtk_sort_list_model_new (model, GTK_SORTER (multi_sorter));
}
else
{
sorted_model = gtk_sort_list_model_new (model, GTK_SORTER (alphabetical_sorter));
}
search_filter = gtk_string_filter_new (expression);
self->search_list_model = gtk_filter_list_model_new (G_LIST_MODEL (sorted_model), GTK_FILTER (search_filter));
adw_preferences_group_bind_model (self->search_prefs_group, G_LIST_MODEL (self->search_list_model),
(GtkListBoxCreateWidgetFunc) widget_factory,
self, NULL);
// Filter by user/system extension
expression = gtk_property_expression_new (EXM_TYPE_EXTENSION, NULL, "is-user");
is_user_filter = gtk_bool_filter_new (expression);
filtered_model = gtk_filter_list_model_new (G_LIST_MODEL (sorted_model), GTK_FILTER (is_user_filter));
g_object_bind_property (filtered_model, "n-items",
self->user_prefs_group, "visible",
G_BINDING_SYNC_CREATE);
adw_preferences_group_bind_model (self->user_prefs_group, G_LIST_MODEL (filtered_model),
(GtkListBoxCreateWidgetFunc) widget_factory,
self, NULL);
is_user_filter = gtk_bool_filter_new (expression);
gtk_bool_filter_set_invert (is_user_filter, TRUE);
filtered_model = gtk_filter_list_model_new (G_LIST_MODEL (sorted_model), GTK_FILTER (is_user_filter));
g_object_bind_property (filtered_model, "n-items",
self->system_prefs_group, "visible",
G_BINDING_SYNC_CREATE);
adw_preferences_group_bind_model (self->system_prefs_group, G_LIST_MODEL (filtered_model),
(GtkListBoxCreateWidgetFunc) widget_factory,
self, NULL);
// Refilter when sort-enabled-first changes and there is an ongoing search
if (self->search_query)
gtk_string_filter_set_search (search_filter, self->search_query);
g_signal_connect (self,
"notify::search-query",
G_CALLBACK (on_search_changed),
search_filter);
g_signal_connect (self->search_list_model,
"notify::n-items",
G_CALLBACK (on_visible_stack_changed),
self);
}
static guint
show_updates_banner (ExmInstalledPage *self)
{
adw_banner_set_revealed (self->updates_banner, TRUE);
return G_SOURCE_REMOVE;
}
static void
on_updates_available (ExmManager *manager G_GNUC_UNUSED,
int n_updates,
ExmInstalledPage *self)
{
char *label;
// Translators: '%d' = number of extensions that will be updated
label = g_strdup_printf(ngettext("%d extension will be updated on next login",
"%d extensions will be updated on next login",
n_updates), n_updates);
adw_banner_set_title (self->updates_banner, label);
g_free (label);
// Short delay to draw user attention
g_timeout_add (500, G_SOURCE_FUNC (show_updates_banner), self);
}
static gboolean
focus_matching_extension (AdwPreferencesGroup *prefs_group,
ExmExtension *extension)
{
int index = 0;
ExmExtensionRow *row;
while ((row = EXM_EXTENSION_ROW (adw_preferences_group_get_row (prefs_group, index))))
{
ExmExtension *row_extension;
g_object_get (row, "extension", &row_extension, NULL);
if (is_extension_equal (extension, row_extension))
{
exm_search_row_focus_toggle (row);
g_object_unref (row_extension);
return TRUE;
}
g_object_unref (row_extension);
index++;
}
return FALSE;
}
static void
on_extensions_changed (GListModel *model,
guint position,
guint removed,
guint added,
ExmInstalledPage *self)
{
if (!self->sort_enabled_first || (removed > 0 && added > 0))
return;
if (position >= g_list_model_get_n_items (model))
return;
ExmExtension *extension = EXM_EXTENSION (g_list_model_get_object (model, position));
if (extension == NULL)
return;
GtkRoot *toplevel = gtk_widget_get_root (GTK_WIDGET (self));
if (toplevel == NULL)
{
g_object_unref (extension);
return;
}
GtkWidget *focused_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
if (focused_widget == NULL)
{
g_object_unref (extension);
return;
}
if (g_list_store_find_with_equal_func (G_LIST_STORE (model), extension, (GEqualFunc)is_extension_equal, &position))
g_list_model_items_changed (model, position, 1, 1);
if (gtk_widget_has_focus (focused_widget)
&& gtk_widget_get_child_visible (GTK_WIDGET (self)))
{
if (g_strcmp0 (gtk_stack_get_visible_child_name (self->stack), "page_results") == 0)
{
focus_matching_extension (self->search_prefs_group, extension);
}
else
{
if (!focus_matching_extension (self->user_prefs_group, extension))
focus_matching_extension (self->system_prefs_group, extension);
}
}
g_object_unref (extension);
}
static void
invalidate_model_bindings (ExmInstalledPage *self)
{
GListModel *ext_model;
if (!self->manager)
return;
g_object_get (self->manager,
"extensions", &ext_model,
NULL);
if (ext_model)
{
bind_list_box (ext_model, self);
if (self->signal_id > 0)
g_signal_handler_disconnect (ext_model, self->signal_id);
self->signal_id = g_signal_connect (ext_model,
"items-changed",
G_CALLBACK (on_extensions_changed),
self);
}
}
static void
on_bind_manager (ExmInstalledPage *self)
{
// Bind (or rebind) models
invalidate_model_bindings (self);
g_signal_connect (self->manager,
"updates-available",
G_CALLBACK (on_updates_available),
self);
g_object_bind_property (self->manager,
"extensions-enabled",
self->global_toggle,
"active",
G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
switch_page (self);
// Check if updates are available
// NOTE: We need to do this *after* connecting the signal
// handler above, otherwise we will not be notified.
if (self->auto_check_updates)
exm_manager_check_for_updates (self->manager);
}
static void
exm_installed_page_class_init (ExmInstalledPageClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = exm_installed_page_finalize;
object_class->get_property = exm_installed_page_get_property;
object_class->set_property = exm_installed_page_set_property;
properties [PROP_MANAGER]
= g_param_spec_object ("manager",
"Manager",
"Manager",
EXM_TYPE_MANAGER,
G_PARAM_READWRITE);
properties [PROP_SORT_ENABLED_FIRST]
= g_param_spec_boolean ("sort-enabled-first",
"Sort Enabled First",
"Sort Enabled First",
FALSE,
G_PARAM_READWRITE);
properties [PROP_AUTO_CHECK_UPDATES]
= g_param_spec_boolean ("auto-check-updates",
"Auto Check Updates",
"Auto Check Updates",
TRUE,
G_PARAM_READWRITE);
properties [PROP_SEARCH_MODE_ENABLED]
= g_param_spec_boolean ("search-mode-enabled",
"Search Mode Enabled",
"Search Mode Enabled",
FALSE,
G_PARAM_READWRITE);
properties [PROP_SEARCH_QUERY] =
g_param_spec_string ("search-query",
"Search Query",
"Search Query",
NULL,
G_PARAM_READWRITE);
g_object_class_install_properties (object_class, N_PROPS, properties);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
gtk_widget_class_set_template_from_resource (widget_class, g_strdup_printf ("%s/exm-installed-page.ui", RESOURCE_PATH));
gtk_widget_class_bind_template_child (widget_class, ExmInstalledPage, stack);
gtk_widget_class_bind_template_child (widget_class, ExmInstalledPage, updates_banner);
gtk_widget_class_bind_template_child (widget_class, ExmInstalledPage, global_toggle);
gtk_widget_class_bind_template_child (widget_class, ExmInstalledPage, auto_check_updates_toggle);
gtk_widget_class_bind_template_child (widget_class, ExmInstalledPage, user_prefs_group);
gtk_widget_class_bind_template_child (widget_class, ExmInstalledPage, system_prefs_group);
gtk_widget_class_bind_template_child (widget_class, ExmInstalledPage, search_prefs_group);
gtk_widget_class_bind_template_callback (widget_class, on_bind_manager);
gtk_widget_class_bind_template_callback (widget_class, on_visible_stack_changed);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
}
static void
exm_installed_page_init (ExmInstalledPage *self)
{
GSettings *settings;
gtk_widget_init_template (GTK_WIDGET (self));
settings = g_settings_new (APP_ID);
g_settings_bind (settings, "sort-enabled-first",
self, "sort-enabled-first",
G_SETTINGS_BIND_GET);
g_settings_bind (settings, "auto-check-updates",
self, "auto-check-updates",
G_SETTINGS_BIND_DEFAULT);
g_object_bind_property (self, "auto-check-updates",
self->auto_check_updates_toggle, "active",
G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
g_object_unref (settings);
}