comparison pui-prefs-application.c @ 58:9cbb0f8a66c4

Add setting to disable preferences widgets This just makes the widgets in the preferences application insensitive, settings can still be changed by directly editing the configuration file.
author Guido Berhoerster <guido+pui@berhoerster.name>
date Tue, 06 Sep 2022 19:11:21 +0200
parents 3d72ca76538d
children
comparison
equal deleted inserted replaced
57:76989c84bd20 58:9cbb0f8a66c4
30 #define COLUMN_REFRESH_INTERVAL 2 30 #define COLUMN_REFRESH_INTERVAL 2
31 31
32 struct _PuiPrefsApplication { 32 struct _PuiPrefsApplication {
33 GtkApplication parent_instance; 33 GtkApplication parent_instance;
34 GSettings *settings; 34 GSettings *settings;
35 GtkWidget *update_command_entry;
36 GtkWidget *refresh_interval_combo_box;
37 GtkWidget *use_mobile_check_button;
35 }; 38 };
36 39
37 G_DEFINE_TYPE(PuiPrefsApplication, pui_prefs_application, GTK_TYPE_APPLICATION) 40 G_DEFINE_TYPE(PuiPrefsApplication, pui_prefs_application, GTK_TYPE_APPLICATION)
38 41
39 static void pui_prefs_application_quit(GSimpleAction *, GVariant *, 42 static void pui_prefs_application_quit(GSimpleAction *, GVariant *,
115 118
116 return (g_variant_new_uint32(model_interval)); 119 return (g_variant_new_uint32(model_interval));
117 } 120 }
118 121
119 static void 122 static void
123 update_widgets(PuiPrefsApplication *self)
124 {
125 gchar ** disabled_preferences;
126
127 g_return_if_fail(self->update_command_entry != NULL);
128 g_return_if_fail(self->refresh_interval_combo_box != NULL);
129 g_return_if_fail(self->use_mobile_check_button != NULL);
130
131 disabled_preferences = g_settings_get_strv(self->settings,
132 "disable-preferences");
133 g_return_if_fail(disabled_preferences != NULL);
134 gtk_widget_set_sensitive(self->update_command_entry,
135 !g_strv_contains((const gchar * const *)disabled_preferences,
136 "update-command"));
137 gtk_widget_set_sensitive(self->refresh_interval_combo_box,
138 !g_strv_contains((const gchar * const *)disabled_preferences,
139 "refresh-interval"));
140 gtk_widget_set_sensitive(self->use_mobile_check_button,
141 !g_strv_contains((const gchar * const *)disabled_preferences,
142 "use-mobile-connection"));
143 g_strfreev(disabled_preferences);
144 }
145
146 static void
147 on_disable_preferences_changed(GSettings *settings, gchar *key,
148 gpointer user_data)
149 {
150 g_debug("setting disable-preferences changed");
151 update_widgets(user_data);
152 }
153
154 static void
120 pui_prefs_application_startup(GApplication *application) 155 pui_prefs_application_startup(GApplication *application)
121 { 156 {
122 PuiPrefsApplication *self = PUI_PREFS_APPLICATION(application); 157 PuiPrefsApplication *self = PUI_PREFS_APPLICATION(application);
123 GApplicationClass *application_class = 158 GApplicationClass *application_class =
124 G_APPLICATION_CLASS(pui_prefs_application_parent_class); 159 G_APPLICATION_CLASS(pui_prefs_application_parent_class);
125 GtkBuilder *builder; 160 GtkBuilder *builder;
126 GtkWidget *window; 161 GtkWidget *window;
127 GtkTreeModel *tree_model; 162 GtkTreeModel *tree_model;
128 GtkWidget *update_command_entry;
129 GtkWidget *refresh_interval_combo_box;
130 GtkWidget *use_mobile_check_button;
131 163
132 application_class->startup(application); 164 application_class->startup(application);
133 165
134 /* create actions */ 166 /* create actions */
135 g_action_map_add_action_entries(G_ACTION_MAP(self), 167 g_action_map_add_action_entries(G_ACTION_MAP(self),
139 /* get widgets from builder */ 171 /* get widgets from builder */
140 builder = gtk_builder_new_from_resource("/org/guido-berhoerster/code/" 172 builder = gtk_builder_new_from_resource("/org/guido-berhoerster/code/"
141 "package-update-indicator/preferences/pui-prefs-window.ui"); 173 "package-update-indicator/preferences/pui-prefs-window.ui");
142 window = GTK_WIDGET(gtk_builder_get_object(builder, "window")); 174 window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
143 gtk_application_add_window(GTK_APPLICATION(self), GTK_WINDOW(window)); 175 gtk_application_add_window(GTK_APPLICATION(self), GTK_WINDOW(window));
144 update_command_entry = GTK_WIDGET(gtk_builder_get_object(builder, 176 self->update_command_entry = GTK_WIDGET(gtk_builder_get_object(builder,
145 "update-command")); 177 "update-command"));
146 refresh_interval_combo_box = GTK_WIDGET(gtk_builder_get_object(builder, 178 self->refresh_interval_combo_box =
147 "refresh-interval")); 179 GTK_WIDGET(gtk_builder_get_object(builder, "refresh-interval"));
148 tree_model = 180 tree_model = gtk_combo_box_get_model(
149 gtk_combo_box_get_model(GTK_COMBO_BOX(refresh_interval_combo_box)); 181 GTK_COMBO_BOX(self->refresh_interval_combo_box));
150 use_mobile_check_button = GTK_WIDGET(gtk_builder_get_object(builder, 182 self->use_mobile_check_button =
183 GTK_WIDGET(gtk_builder_get_object(builder,
151 "use-mobile-connection")); 184 "use-mobile-connection"));
152 185
153 /* bind settings to widgets */ 186 /* bind settings to widgets */
154 self->settings = pui_settings_new(); 187 self->settings = pui_settings_new();
155 g_settings_bind(self->settings, "update-command", 188 g_settings_bind(self->settings, "update-command",
156 update_command_entry, "text", G_SETTINGS_BIND_DEFAULT); 189 self->update_command_entry, "text", G_SETTINGS_BIND_DEFAULT);
157 g_settings_bind_with_mapping(self->settings, "refresh-interval", 190 g_settings_bind_with_mapping(self->settings, "refresh-interval",
158 refresh_interval_combo_box, "active", G_SETTINGS_BIND_DEFAULT, 191 self->refresh_interval_combo_box, "active",
159 map_refresh_interval_to_index, map_index_to_refresh_interval, 192 G_SETTINGS_BIND_DEFAULT, map_refresh_interval_to_index,
160 tree_model, NULL); 193 map_index_to_refresh_interval, tree_model, NULL);
161 g_settings_bind(self->settings, "use-mobile-connection", 194 g_settings_bind(self->settings, "use-mobile-connection",
162 use_mobile_check_button, "active", G_SETTINGS_BIND_DEFAULT); 195 self->use_mobile_check_button, "active", G_SETTINGS_BIND_DEFAULT);
196
197 /* watch disable-preferences setting in order to update the window */
198 g_signal_connect(self->settings, "changed::disable-preferences",
199 G_CALLBACK(on_disable_preferences_changed), self);
200 update_widgets(self);
163 201
164 /* show window */ 202 /* show window */
165 gtk_widget_show(window); 203 gtk_widget_show(window);
166 gtk_window_present(GTK_WINDOW(window)); 204 gtk_window_present(GTK_WINDOW(window));
167 205
187 225
188 if (self->settings != NULL) { 226 if (self->settings != NULL) {
189 g_clear_object(&self->settings); 227 g_clear_object(&self->settings);
190 } 228 }
191 229
230 self->update_command_entry = NULL;
231 self->refresh_interval_combo_box = NULL;
232 self->use_mobile_check_button = NULL;
233
192 G_OBJECT_CLASS(pui_prefs_application_parent_class)->dispose(object); 234 G_OBJECT_CLASS(pui_prefs_application_parent_class)->dispose(object);
193 } 235 }
194 236
195 static void 237 static void
196 pui_prefs_application_class_init(PuiPrefsApplicationClass *klass) 238 pui_prefs_application_class_init(PuiPrefsApplicationClass *klass)