File: pui-prefs-application.c

package info (click to toggle)
package-update-indicator 8-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 368 kB
  • sloc: ansic: 1,836; xml: 296; makefile: 213; sed: 26
file content (260 lines) | stat: -rw-r--r-- 8,453 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
/*
 * Copyright (C) 2018 Guido Berhoerster <guido+pui@berhoerster.name>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <glib/gi18n.h>
#include <gio/gio.h>

#include "pui-prefs-application.h"
#include "pui-settings.h"

#define	COLUMN_REFRESH_INTERVAL	2

struct _PuiPrefsApplication {
	GtkApplication	parent_instance;
	GSettings	*settings;
	GtkWidget	*update_command_entry;
	GtkWidget	*refresh_interval_combo_box;
	GtkWidget	*use_mobile_check_button;
};

G_DEFINE_TYPE(PuiPrefsApplication, pui_prefs_application, GTK_TYPE_APPLICATION)

static void	pui_prefs_application_quit(GSimpleAction *, GVariant *,
    gpointer);

static const GActionEntry pui_prefs_application_actions[] = {
    { "quit", pui_prefs_application_quit }
};

static void
pui_prefs_application_quit(GSimpleAction *simple, GVariant *parameter,
    gpointer user_data)
{
	PuiPrefsApplication	*self = user_data;

	g_application_quit(G_APPLICATION(self));
}

static gboolean
map_refresh_interval_to_index(GValue *value, GVariant *variant,
    gpointer user_data)
{
	GtkTreeModel	*tree_model = user_data;
	guint32		setting_interval;
	gint		index;
	gboolean	iter_continue;
	GtkTreeIter	iter = { 0 };
	GValue		model_value = G_VALUE_INIT;
	guint		model_interval;

	setting_interval = g_variant_get_uint32(variant);

	/* try to find a matching entry in the list */
	for (iter_continue = gtk_tree_model_get_iter_first(tree_model, &iter),
	    index = 0; iter_continue;
	    iter_continue = gtk_tree_model_iter_next(tree_model, &iter),
	    index++) {
		gtk_tree_model_get_value(tree_model, &iter,
		    COLUMN_REFRESH_INTERVAL, &model_value);
		model_interval = g_value_get_uint(&model_value);
		g_value_unset(&model_value);
		if (setting_interval == model_interval) {
			g_debug("mapping refresh-interval %" G_GUINT32_FORMAT
			    " to index %d", setting_interval, index);
			g_value_set_int(value, index);

			return (TRUE);
		}
	}

	g_debug("mapping refresh-interval %" G_GUINT32_FORMAT " to index -1",
	    setting_interval);
	g_value_set_int(value, -1);

	return (TRUE);
}

static GVariant *
map_index_to_refresh_interval(const GValue *value,
    const GVariantType *expected_type, gpointer user_data)
{
	GtkTreeModel	*tree_model = GTK_TREE_MODEL(user_data);
	gint		index;
	GtkTreeIter	iter = { 0 };
	GValue		model_value = G_VALUE_INIT;
	guint		model_interval;

	index = g_value_get_int(value);
	if (!gtk_tree_model_iter_nth_child(tree_model, &iter, NULL, index)) {
		return (NULL);
	}

	gtk_tree_model_get_value(tree_model, &iter, COLUMN_REFRESH_INTERVAL,
	    &model_value);
	model_interval = g_value_get_uint(&model_value);
	g_debug("mapping index %d to refresh-interval value %" G_GUINT32_FORMAT,
	    index, model_interval);
	g_value_unset(&model_value);

	return (g_variant_new_uint32(model_interval));
}

static void
update_widgets(PuiPrefsApplication *self)
{
	gchar **	disabled_preferences;

	g_return_if_fail(self->update_command_entry != NULL);
	g_return_if_fail(self->refresh_interval_combo_box != NULL);
	g_return_if_fail(self->use_mobile_check_button != NULL);

	disabled_preferences = g_settings_get_strv(self->settings,
	    "disable-preferences");
	g_return_if_fail(disabled_preferences != NULL);
	gtk_widget_set_sensitive(self->update_command_entry,
	    !g_strv_contains((const gchar * const *)disabled_preferences,
	    "update-command"));
	gtk_widget_set_sensitive(self->refresh_interval_combo_box,
	    !g_strv_contains((const gchar * const *)disabled_preferences,
	    "refresh-interval"));
	gtk_widget_set_sensitive(self->use_mobile_check_button,
	    !g_strv_contains((const gchar * const *)disabled_preferences,
	    "use-mobile-connection"));
	g_strfreev(disabled_preferences);
}

static void
on_disable_preferences_changed(GSettings *settings, gchar *key,
    gpointer user_data)
{
	g_debug("setting disable-preferences changed");
	update_widgets(user_data);
}

static void
pui_prefs_application_startup(GApplication *application)
{
	PuiPrefsApplication *self = PUI_PREFS_APPLICATION(application);
	GApplicationClass *application_class =
	    G_APPLICATION_CLASS(pui_prefs_application_parent_class);
	GtkBuilder	*builder;
	GtkWidget	*window;
	GtkTreeModel	*tree_model;

	application_class->startup(application);

	/* create actions */
	g_action_map_add_action_entries(G_ACTION_MAP(self),
	    pui_prefs_application_actions,
	    G_N_ELEMENTS(pui_prefs_application_actions), self);

	/* get widgets from builder */
	builder = gtk_builder_new_from_resource("/org/guido-berhoerster/code/"
	    "package-update-indicator/preferences/pui-prefs-window.ui");
	window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
	gtk_application_add_window(GTK_APPLICATION(self), GTK_WINDOW(window));
	self->update_command_entry = GTK_WIDGET(gtk_builder_get_object(builder,
	    "update-command"));
	self->refresh_interval_combo_box =
	    GTK_WIDGET(gtk_builder_get_object(builder, "refresh-interval"));
	tree_model = gtk_combo_box_get_model(
	    GTK_COMBO_BOX(self->refresh_interval_combo_box));
	self->use_mobile_check_button =
	    GTK_WIDGET(gtk_builder_get_object(builder,
	    "use-mobile-connection"));

	/* bind settings to widgets */
	self->settings = pui_settings_new();
	g_settings_bind(self->settings, "update-command",
	    self->update_command_entry, "text", G_SETTINGS_BIND_DEFAULT);
	g_settings_bind_with_mapping(self->settings, "refresh-interval",
	    self->refresh_interval_combo_box, "active",
	    G_SETTINGS_BIND_DEFAULT, map_refresh_interval_to_index,
	    map_index_to_refresh_interval, tree_model, NULL);
	g_settings_bind(self->settings, "use-mobile-connection",
	    self->use_mobile_check_button, "active", G_SETTINGS_BIND_DEFAULT);

	/* watch disable-preferences setting in order to update the window */
	g_signal_connect(self->settings, "changed::disable-preferences",
	    G_CALLBACK(on_disable_preferences_changed), self);
	update_widgets(self);

	/* show window */
	gtk_widget_show(window);
	gtk_window_present(GTK_WINDOW(window));

	g_object_unref(builder);
}

static void
pui_prefs_application_activate(GApplication *application) {
	GtkApplication	*gtk_application = GTK_APPLICATION(application);
	GApplicationClass *application_class =
	    G_APPLICATION_CLASS(pui_prefs_application_parent_class);

	/* raise window when activated */
	gtk_window_present(gtk_application_get_active_window(gtk_application));

	application_class->activate(application);
}

static void
pui_prefs_application_dispose(GObject *object)
{
	PuiPrefsApplication *self = PUI_PREFS_APPLICATION(object);

	if (self->settings != NULL) {
		g_clear_object(&self->settings);
	}

	self->update_command_entry = NULL;
	self->refresh_interval_combo_box = NULL;
	self->use_mobile_check_button = NULL;

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

static void
pui_prefs_application_class_init(PuiPrefsApplicationClass *klass)
{
	GObjectClass	*object_class = G_OBJECT_CLASS(klass);
	GApplicationClass *application_class = G_APPLICATION_CLASS(klass);

	object_class->dispose = pui_prefs_application_dispose;

	application_class->startup = pui_prefs_application_startup;
	application_class->activate = pui_prefs_application_activate;
}

static void
pui_prefs_application_init(PuiPrefsApplication *self)
{
	/* do nothing, implementation required */
}

PuiPrefsApplication *
pui_prefs_application_new(void)
{
	return (g_object_new(PUI_TYPE_PREFS_APPLICATION,
	    "application-id", APPLICATION_ID, NULL));
}