Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #define G_LOG_DOMAIN "valent-device-preferences-dialog"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <glib/gi18n-lib.h>
9 : : #include <adwaita.h>
10 : : #include <gtk/gtk.h>
11 : : #include <valent.h>
12 : :
13 : : #include "valent-plugin-row.h"
14 : :
15 : : #include "valent-device-preferences-dialog.h"
16 : :
17 : :
18 : : struct _ValentDevicePreferencesDialog
19 : : {
20 : : AdwPreferencesDialog parent_instance;
21 : :
22 : : ValentDevice *device;
23 : : GListStore *plugins;
24 : :
25 : : /* template */
26 : : AdwPreferencesPage *status_page;
27 : : AdwPreferencesPage *sync_page;
28 : : AdwPreferencesPage *other_page;
29 : : AdwPreferencesPage *plugin_page;
30 : : GtkListBox *plugin_list;
31 : : };
32 : :
33 [ + + + - ]: 83 : G_DEFINE_FINAL_TYPE (ValentDevicePreferencesDialog, valent_device_preferences_dialog, ADW_TYPE_PREFERENCES_DIALOG)
34 : :
35 : : typedef enum {
36 : : PROP_DEVICE = 1,
37 : : } ValentDevicePreferencesDialogProperty;
38 : :
39 : : static GParamSpec *properties[PROP_DEVICE + 1] = { NULL, };
40 : :
41 : : static GtkWidget *
42 : 33 : create_plugin_row (gpointer item,
43 : : gpointer user_data)
44 : : {
45 : 33 : ValentDevicePreferencesDialog *self = VALENT_DEVICE_PREFERENCES_DIALOG (user_data);
46 : 33 : PeasPluginInfo *plugin_info = PEAS_PLUGIN_INFO (item);
47 : 33 : ValentContext *context = NULL;
48 : 66 : g_autoptr (ValentContext) plugin_context = NULL;
49 : :
50 : 33 : context = valent_device_get_context (self->device);
51 : 33 : plugin_context = valent_context_get_plugin_context (context, plugin_info);
52 [ + - ]: 33 : return g_object_new (VALENT_TYPE_PLUGIN_ROW,
53 : : "context", plugin_context,
54 : : "plugin-info", plugin_info,
55 : : "title", peas_plugin_info_get_name (plugin_info),
56 : : "subtitle", peas_plugin_info_get_description (plugin_info),
57 : : NULL);
58 : : }
59 : :
60 : : static inline int
61 : 83 : plugin_sort_func (gconstpointer a,
62 : : gconstpointer b,
63 : : gpointer user_data)
64 : : {
65 : 83 : return g_utf8_collate (peas_plugin_info_get_name ((PeasPluginInfo *)a),
66 : 83 : peas_plugin_info_get_name ((PeasPluginInfo *)b));
67 : : }
68 : :
69 : : static void
70 : 39 : on_load_plugin (PeasEngine *engine,
71 : : PeasPluginInfo *plugin_info,
72 : : ValentDevicePreferencesDialog *self)
73 : : {
74 [ + - ]: 39 : g_assert (PEAS_IS_ENGINE (engine));
75 [ - + ]: 39 : g_assert (plugin_info != NULL);
76 [ - + ]: 39 : g_assert (VALENT_IS_DEVICE_PREFERENCES_DIALOG (self));
77 : :
78 [ + + ]: 39 : if (peas_engine_provides_extension (engine, plugin_info, VALENT_TYPE_DEVICE_PLUGIN))
79 : : {
80 : 33 : g_list_store_insert_sorted (self->plugins,
81 : : plugin_info,
82 : : plugin_sort_func, NULL);
83 : : }
84 : 39 : }
85 : :
86 : : static void
87 : 2 : on_unload_plugin (PeasEngine *engine,
88 : : PeasPluginInfo *plugin_info,
89 : : ValentDevicePreferencesDialog *self)
90 : : {
91 : 2 : unsigned int position = 0;
92 : :
93 [ + - ]: 2 : g_assert (PEAS_IS_ENGINE (engine));
94 [ - + ]: 2 : g_assert (plugin_info != NULL);
95 [ - + ]: 2 : g_assert (VALENT_IS_DEVICE_PREFERENCES_DIALOG (self));
96 : :
97 [ + - ]: 2 : if (g_list_store_find (self->plugins, plugin_info, &position))
98 : 2 : g_list_store_remove (self->plugins, position);
99 : 2 : }
100 : :
101 : : /*
102 : : * GObject
103 : : */
104 : : static void
105 : 3 : valent_device_preferences_dialog_constructed (GObject *object)
106 : : {
107 : 3 : ValentDevicePreferencesDialog *self = VALENT_DEVICE_PREFERENCES_DIALOG (object);
108 : 3 : PeasEngine *engine;
109 : 3 : unsigned int n_plugins = 0;
110 : :
111 : 3 : G_OBJECT_CLASS (valent_device_preferences_dialog_parent_class)->constructed (object);
112 : :
113 : 3 : g_object_bind_property (self->device, "name",
114 : : self, "title",
115 : : G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
116 : :
117 : 3 : gtk_widget_insert_action_group (GTK_WIDGET (self),
118 : : "device",
119 : 3 : G_ACTION_GROUP (self->device));
120 : :
121 : 3 : engine = valent_get_plugin_engine ();
122 : 3 : g_signal_connect_object (engine,
123 : : "load-plugin",
124 : : G_CALLBACK (on_load_plugin),
125 : : self,
126 : : G_CONNECT_AFTER);
127 : :
128 : 3 : g_signal_connect_object (engine,
129 : : "unload-plugin",
130 : : G_CALLBACK (on_unload_plugin),
131 : : self,
132 : : G_CONNECT_DEFAULT);
133 : :
134 : 3 : n_plugins = g_list_model_get_n_items (G_LIST_MODEL (engine));
135 [ + + ]: 42 : for (unsigned int i = 0; i < n_plugins; i++)
136 : : {
137 : 39 : g_autoptr (PeasPluginInfo) plugin_info = NULL;
138 : :
139 : 39 : plugin_info = g_list_model_get_item (G_LIST_MODEL (engine), i);
140 [ + + ]: 39 : if (peas_plugin_info_is_loaded (plugin_info))
141 : 38 : on_load_plugin (engine, plugin_info, self);
142 : : }
143 : 3 : }
144 : :
145 : : static void
146 : 3 : valent_device_preferences_dialog_dispose (GObject *object)
147 : : {
148 : 3 : GtkWidget *widget = GTK_WIDGET (object);
149 : :
150 : 3 : gtk_widget_dispose_template (widget, VALENT_TYPE_DEVICE_PREFERENCES_DIALOG);
151 : :
152 : 3 : G_OBJECT_CLASS (valent_device_preferences_dialog_parent_class)->dispose (object);
153 : 3 : }
154 : :
155 : : static void
156 : 3 : valent_device_preferences_dialog_finalize (GObject *object)
157 : : {
158 : 3 : ValentDevicePreferencesDialog *self = VALENT_DEVICE_PREFERENCES_DIALOG (object);
159 : :
160 [ + - ]: 3 : g_clear_object (&self->device);
161 : :
162 : 3 : G_OBJECT_CLASS (valent_device_preferences_dialog_parent_class)->finalize (object);
163 : 3 : }
164 : :
165 : : static void
166 : 37 : valent_device_preferences_dialog_get_property (GObject *object,
167 : : guint prop_id,
168 : : GValue *value,
169 : : GParamSpec *pspec)
170 : : {
171 : 37 : ValentDevicePreferencesDialog *self = VALENT_DEVICE_PREFERENCES_DIALOG (object);
172 : :
173 [ + - ]: 37 : switch ((ValentDevicePreferencesDialogProperty)prop_id)
174 : : {
175 : 37 : case PROP_DEVICE:
176 : 37 : g_value_set_object (value, self->device);
177 : 37 : break;
178 : :
179 : 0 : default:
180 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 : : }
182 : 37 : }
183 : :
184 : : static void
185 : 3 : valent_device_preferences_dialog_set_property (GObject *object,
186 : : guint prop_id,
187 : : const GValue *value,
188 : : GParamSpec *pspec)
189 : : {
190 : 3 : ValentDevicePreferencesDialog *self = VALENT_DEVICE_PREFERENCES_DIALOG (object);
191 : :
192 [ + - ]: 3 : switch ((ValentDevicePreferencesDialogProperty)prop_id)
193 : : {
194 : 3 : case PROP_DEVICE:
195 : 3 : self->device = g_value_dup_object (value);
196 : 3 : break;
197 : :
198 : 0 : default:
199 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 : : }
201 : 3 : }
202 : :
203 : : static void
204 : 2 : valent_device_preferences_dialog_class_init (ValentDevicePreferencesDialogClass *klass)
205 : : {
206 : 2 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
207 : 2 : GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
208 : :
209 : 2 : object_class->constructed = valent_device_preferences_dialog_constructed;
210 : 2 : object_class->dispose = valent_device_preferences_dialog_dispose;
211 : 2 : object_class->finalize = valent_device_preferences_dialog_finalize;
212 : 2 : object_class->get_property = valent_device_preferences_dialog_get_property;
213 : 2 : object_class->set_property = valent_device_preferences_dialog_set_property;
214 : :
215 : 2 : gtk_widget_class_set_template_from_resource (widget_class, "/plugins/gnome/valent-device-preferences-dialog.ui");
216 : 2 : gtk_widget_class_bind_template_child (widget_class, ValentDevicePreferencesDialog, status_page);
217 : 2 : gtk_widget_class_bind_template_child (widget_class, ValentDevicePreferencesDialog, sync_page);
218 : 2 : gtk_widget_class_bind_template_child (widget_class, ValentDevicePreferencesDialog, other_page);
219 : 2 : gtk_widget_class_bind_template_child (widget_class, ValentDevicePreferencesDialog, plugin_page);
220 : 2 : gtk_widget_class_bind_template_child (widget_class, ValentDevicePreferencesDialog, plugin_list);
221 : :
222 : 4 : properties [PROP_DEVICE] =
223 : 2 : g_param_spec_object ("device", NULL, NULL,
224 : : VALENT_TYPE_DEVICE,
225 : : (G_PARAM_READWRITE |
226 : : G_PARAM_CONSTRUCT_ONLY |
227 : : G_PARAM_STATIC_STRINGS));
228 : :
229 : 2 : g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
230 : 2 : }
231 : :
232 : : static void
233 : 3 : valent_device_preferences_dialog_init (ValentDevicePreferencesDialog *self)
234 : : {
235 : 3 : gtk_widget_init_template (GTK_WIDGET (self));
236 : :
237 : 3 : self->plugins = g_list_store_new (PEAS_TYPE_PLUGIN_INFO);
238 : 3 : gtk_list_box_bind_model (self->plugin_list,
239 : : G_LIST_MODEL (self->plugins),
240 : : create_plugin_row,
241 : : self, NULL);
242 : 3 : }
243 : :
|