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"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <gio/gio.h>
9 : : #include <gtk/gtk.h>
10 : : #include <adwaita.h>
11 : : #include <libvalent-core.h>
12 : : #include <libvalent-device.h>
13 : :
14 : : #include "valent-device-preferences-group.h"
15 : :
16 : :
17 : : /**
18 : : * ValentDevicePreferencesGroup:
19 : : *
20 : : * An abstract base class for device plugin preferences.
21 : : *
22 : : * `ValentDevicePreferencesGroup` is a base class for [class@Valent.DevicePlugin]
23 : : * implementations that want to provide a group of preferences.
24 : : *
25 : : * Since: 1.0
26 : : */
27 : :
28 : : typedef struct
29 : : {
30 : : ValentContext *context;
31 : : PeasPluginInfo *plugin_info;
32 : : GSettings *settings;
33 : : } ValentDevicePreferencesGroupPrivate;
34 : :
35 [ + + + - ]: 466 : G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ValentDevicePreferencesGroup, valent_device_preferences_group, ADW_TYPE_PREFERENCES_GROUP)
36 : :
37 : : enum {
38 : : PROP_0,
39 : : PROP_CONTEXT,
40 : : PROP_PLUGIN_INFO,
41 : : PROP_SETTINGS,
42 : : N_PROPERTIES
43 : : };
44 : :
45 : : static GParamSpec *properties[N_PROPERTIES] = { NULL, };
46 : :
47 : :
48 : : /*
49 : : * GObject
50 : : */
51 : : static void
52 : 13 : valent_device_preferences_group_finalize (GObject *object)
53 : : {
54 : 13 : ValentDevicePreferencesGroup *self = VALENT_DEVICE_PREFERENCES_GROUP (object);
55 : 13 : ValentDevicePreferencesGroupPrivate *priv = valent_device_preferences_group_get_instance_private (self);
56 : :
57 [ + - ]: 13 : g_clear_object (&priv->context);
58 [ + - ]: 13 : g_clear_object (&priv->plugin_info);
59 [ + + ]: 13 : g_clear_object (&priv->settings);
60 : :
61 : 13 : G_OBJECT_CLASS (valent_device_preferences_group_parent_class)->finalize (object);
62 : 13 : }
63 : :
64 : : static void
65 : 2 : valent_device_preferences_group_get_property (GObject *object,
66 : : guint prop_id,
67 : : GValue *value,
68 : : GParamSpec *pspec)
69 : : {
70 : 2 : ValentDevicePreferencesGroup *self = VALENT_DEVICE_PREFERENCES_GROUP (object);
71 : 2 : ValentDevicePreferencesGroupPrivate *priv = valent_device_preferences_group_get_instance_private (self);
72 : :
73 [ - + + - ]: 2 : switch (prop_id)
74 : : {
75 : 0 : case PROP_CONTEXT:
76 : 0 : g_value_set_object (value, valent_device_preferences_group_get_context (self));
77 : 0 : break;
78 : :
79 : 1 : case PROP_PLUGIN_INFO:
80 : 1 : g_value_set_object (value, priv->plugin_info);
81 : 1 : break;
82 : :
83 : 1 : case PROP_SETTINGS:
84 : 1 : g_value_set_object (value, valent_device_preferences_group_get_settings (self));
85 : 1 : break;
86 : :
87 : 0 : default:
88 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
89 : : }
90 : 2 : }
91 : :
92 : : static void
93 : 26 : valent_device_preferences_group_set_property (GObject *object,
94 : : guint prop_id,
95 : : const GValue *value,
96 : : GParamSpec *pspec)
97 : : {
98 : 26 : ValentDevicePreferencesGroup *self = VALENT_DEVICE_PREFERENCES_GROUP (object);
99 : 26 : ValentDevicePreferencesGroupPrivate *priv = valent_device_preferences_group_get_instance_private (self);
100 : :
101 [ + + - ]: 26 : switch (prop_id)
102 : : {
103 : 13 : case PROP_CONTEXT:
104 : 13 : priv->context = g_value_dup_object (value);
105 : 13 : break;
106 : :
107 : 13 : case PROP_PLUGIN_INFO:
108 : 13 : priv->plugin_info = g_value_dup_object (value);
109 : 13 : break;
110 : :
111 : 0 : default:
112 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 : : }
114 : 26 : }
115 : :
116 : : static void
117 : 66 : valent_device_preferences_group_class_init (ValentDevicePreferencesGroupClass *klass)
118 : : {
119 : 66 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
120 : :
121 : 66 : object_class->finalize = valent_device_preferences_group_finalize;
122 : 66 : object_class->get_property = valent_device_preferences_group_get_property;
123 : 66 : object_class->set_property = valent_device_preferences_group_set_property;
124 : :
125 : : /**
126 : : * ValentDevicePreferencesGroup:context: (getter get_context)
127 : : *
128 : : * The [class@Valent.Context] for the [class@Valent.DevicePlugin].
129 : : *
130 : : * Since: 1.0
131 : : */
132 : 132 : properties [PROP_CONTEXT] =
133 : 66 : g_param_spec_object ("context", NULL, NULL,
134 : : VALENT_TYPE_CONTEXT,
135 : : (G_PARAM_READWRITE |
136 : : G_PARAM_CONSTRUCT_ONLY |
137 : : G_PARAM_EXPLICIT_NOTIFY |
138 : : G_PARAM_STATIC_STRINGS));
139 : :
140 : : /**
141 : : * ValentDevicePreferencesGroup:plugin-info:
142 : : *
143 : : * The [class@Peas.PluginInfo] describing this plugin.
144 : : *
145 : : * Since: 1.0
146 : : */
147 : 132 : properties [PROP_PLUGIN_INFO] =
148 : 66 : g_param_spec_object ("plugin-info", NULL, NULL,
149 : : PEAS_TYPE_PLUGIN_INFO,
150 : : (G_PARAM_READWRITE |
151 : : G_PARAM_CONSTRUCT_ONLY |
152 : : G_PARAM_EXPLICIT_NOTIFY |
153 : : G_PARAM_STATIC_STRINGS));
154 : :
155 : : /**
156 : : * ValentDevicePreferencesGroup:settings: (getter get_settings)
157 : : *
158 : : * The [class@Gio.Settings] for the [class@Valent.DevicePlugin].
159 : : *
160 : : * Since: 1.0
161 : : */
162 : 132 : properties [PROP_SETTINGS] =
163 : 66 : g_param_spec_object ("settings", NULL, NULL,
164 : : G_TYPE_SETTINGS,
165 : : (G_PARAM_READABLE |
166 : : G_PARAM_EXPLICIT_NOTIFY |
167 : : G_PARAM_STATIC_STRINGS));
168 : :
169 : 66 : g_object_class_install_properties (object_class, N_PROPERTIES, properties);
170 : 66 : }
171 : :
172 : : static void
173 : 13 : valent_device_preferences_group_init (ValentDevicePreferencesGroup *self)
174 : : {
175 : 13 : }
176 : :
177 : : /**
178 : : * valent_device_preferences_group_get_context:
179 : : * @group: a `ValentDevicePreferencesGroup`
180 : : *
181 : : * Get the [class@Valent.Context] for the [class@Valent.DevicePlugin].
182 : : *
183 : : * Returns: (transfer none) (nullable): a `ValentContext`
184 : : *
185 : : * Since: 1.0
186 : : */
187 : : ValentContext *
188 : 11 : valent_device_preferences_group_get_context (ValentDevicePreferencesGroup *group)
189 : : {
190 : 11 : ValentDevicePreferencesGroupPrivate *priv = valent_device_preferences_group_get_instance_private (group);
191 : :
192 [ + - ]: 11 : g_return_val_if_fail (VALENT_IS_DEVICE_PREFERENCES_GROUP (group), NULL);
193 : :
194 [ + + ]: 11 : if (priv->context == NULL)
195 : : {
196 : 21 : g_autoptr (ValentContext) context = NULL;
197 : :
198 : 10 : context = valent_context_new (NULL, "device", "default");
199 [ + - ]: 10 : priv->context = valent_context_get_plugin_context (context, priv->plugin_info);
200 : : }
201 : :
202 : 11 : return priv->context;
203 : : }
204 : :
205 : : /**
206 : : * valent_device_preferences_group_get_settings:
207 : : * @group: a `ValentDevicePreferencesGroup`
208 : : *
209 : : * Get the [class@Gio.Settings] for the [class@Valent.DevicePlugin].
210 : : *
211 : : * Returns: (transfer none) (nullable): a `GSettings`
212 : : *
213 : : * Since: 1.0
214 : : */
215 : : GSettings *
216 : 13 : valent_device_preferences_group_get_settings (ValentDevicePreferencesGroup *group)
217 : : {
218 : 13 : ValentDevicePreferencesGroupPrivate *priv = valent_device_preferences_group_get_instance_private (group);
219 : :
220 [ + - ]: 13 : g_return_val_if_fail (VALENT_IS_DEVICE_PREFERENCES_GROUP (group), NULL);
221 : :
222 [ + + ]: 13 : if (priv->settings == NULL)
223 : : {
224 : 10 : ValentContext *context = NULL;
225 : :
226 : 10 : context = valent_device_preferences_group_get_context (group);
227 : 10 : priv->settings = valent_context_get_plugin_settings (context,
228 : : priv->plugin_info,
229 : : "X-DevicePluginSettings");
230 : : }
231 : :
232 : 13 : return priv->settings;
233 : : }
234 : :
|