comparison pui-prefs-application.c @ 1:2f04ec9e0506

Add preferences application Split settings into separate file.
author Guido Berhoerster <guido+pui@berhoerster.name>
date Fri, 08 Jun 2018 08:38:42 +0200
parents
children 3d72ca76538d
comparison
equal deleted inserted replaced
0:6884bb8130ca 1:2f04ec9e0506
1 /*
2 * Copyright (C) 2018 Guido Berhoerster <guido+pui@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <glib/gi18n.h>
25 #include <gio/gio.h>
26
27 #include "pui-prefs-application.h"
28 #include "pui-settings.h"
29
30 #define COLUMN_REFRESH_INTERVAL 2
31
32 struct _PuiPrefsApplication {
33 GtkApplication parent_instance;
34 GSettings *settings;
35 };
36
37 G_DEFINE_TYPE(PuiPrefsApplication, pui_prefs_application, GTK_TYPE_APPLICATION)
38
39 static void pui_prefs_application_quit(GSimpleAction *, GVariant *,
40 gpointer);
41
42 static const GActionEntry pui_prefs_application_actions[] = {
43 { "quit", pui_prefs_application_quit }
44 };
45
46 static void
47 pui_prefs_application_quit(GSimpleAction *simple, GVariant *parameter,
48 gpointer user_data)
49 {
50 PuiPrefsApplication *self = user_data;
51
52 g_application_quit(G_APPLICATION(self));
53 }
54
55 static gboolean
56 map_refresh_interval_to_index(GValue *value, GVariant *variant,
57 gpointer user_data)
58 {
59 GtkTreeModel *tree_model = user_data;
60 guint32 setting_interval;
61 gint index;
62 gboolean iter_continue;
63 GtkTreeIter iter = { 0 };
64 GValue model_value = G_VALUE_INIT;
65 guint model_interval;
66
67 setting_interval = g_variant_get_uint32(variant);
68
69 /* try to find a matching entry in the list */
70 for (iter_continue = gtk_tree_model_get_iter_first(tree_model, &iter),
71 index = 0; iter_continue;
72 iter_continue = gtk_tree_model_iter_next(tree_model, &iter),
73 index++) {
74 gtk_tree_model_get_value(tree_model, &iter,
75 COLUMN_REFRESH_INTERVAL, &model_value);
76 model_interval = g_value_get_uint(&model_value);
77 g_value_unset(&model_value);
78 if (setting_interval == model_interval) {
79 g_debug("mapping refresh-interval %" G_GUINT32_FORMAT
80 " to index %d", setting_interval, index);
81 g_value_set_int(value, index);
82
83 return (TRUE);
84 }
85 }
86
87 g_debug("mapping refresh-interval %" G_GUINT32_FORMAT " to index -1",
88 setting_interval);
89 g_value_set_int(value, -1);
90
91 return (TRUE);
92 }
93
94 static GVariant *
95 map_index_to_refresh_interval(const GValue *value,
96 const GVariantType *expected_type, gpointer user_data)
97 {
98 GtkTreeModel *tree_model = GTK_TREE_MODEL(user_data);
99 gint index;
100 GtkTreeIter iter = { 0 };
101 GValue model_value = G_VALUE_INIT;
102 guint model_interval;
103
104 index = g_value_get_int(value);
105 if (!gtk_tree_model_iter_nth_child(tree_model, &iter, NULL, index)) {
106 return (NULL);
107 }
108
109 gtk_tree_model_get_value(tree_model, &iter, COLUMN_REFRESH_INTERVAL,
110 &model_value);
111 model_interval = g_value_get_uint(&model_value);
112 g_debug("mapping index %d to refresh-interval value %" G_GUINT32_FORMAT,
113 index, model_interval);
114 g_value_unset(&model_value);
115
116 return (g_variant_new_uint32(model_interval));
117 }
118
119 static void
120 pui_prefs_application_startup(GApplication *application)
121 {
122 PuiPrefsApplication *self = PUI_PREFS_APPLICATION(application);
123 GApplicationClass *application_class =
124 G_APPLICATION_CLASS(pui_prefs_application_parent_class);
125 GtkBuilder *builder;
126 GtkWidget *window;
127 GtkTreeModel *tree_model;
128 GtkWidget *update_command_entry;
129 GtkWidget *refresh_interval_combo_box;
130
131 application_class->startup(application);
132
133 /* create actions */
134 g_action_map_add_action_entries(G_ACTION_MAP(self),
135 pui_prefs_application_actions,
136 G_N_ELEMENTS(pui_prefs_application_actions), self);
137
138 /* get widgets from builder */
139 builder = gtk_builder_new_from_resource("/org/guido-berhoerster/code/"
140 "package-update-indicator/preferences/pui-prefs-window.ui");
141 window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
142 gtk_application_add_window(GTK_APPLICATION(self), GTK_WINDOW(window));
143 update_command_entry = GTK_WIDGET(gtk_builder_get_object(builder,
144 "update-command"));
145 refresh_interval_combo_box = GTK_WIDGET(gtk_builder_get_object(builder,
146 "refresh-interval"));
147 tree_model =
148 gtk_combo_box_get_model(GTK_COMBO_BOX(refresh_interval_combo_box));
149
150 /* bind settings to widgets */
151 self->settings = pui_settings_new();
152 g_settings_bind(self->settings, "update-command",
153 update_command_entry, "text", G_SETTINGS_BIND_DEFAULT);
154 g_settings_bind_with_mapping(self->settings, "refresh-interval",
155 refresh_interval_combo_box, "active", G_SETTINGS_BIND_DEFAULT,
156 map_refresh_interval_to_index, map_index_to_refresh_interval,
157 tree_model, NULL);
158
159 /* show window */
160 gtk_widget_show(window);
161 gtk_window_present(GTK_WINDOW(window));
162
163 g_object_unref(builder);
164 }
165
166 static void
167 pui_prefs_application_activate(GApplication *application) {
168 GtkApplication *gtk_application = GTK_APPLICATION(application);
169 GApplicationClass *application_class =
170 G_APPLICATION_CLASS(pui_prefs_application_parent_class);
171
172 /* raise window when activated */
173 gtk_window_present(gtk_application_get_active_window(gtk_application));
174
175 application_class->activate(application);
176 }
177
178 static void
179 pui_prefs_application_dispose(GObject *object)
180 {
181 PuiPrefsApplication *self = PUI_PREFS_APPLICATION(object);
182
183 if (self->settings != NULL) {
184 g_clear_object(&self->settings);
185 }
186
187 G_OBJECT_CLASS(pui_prefs_application_parent_class)->dispose(object);
188 }
189
190 static void
191 pui_prefs_application_class_init(PuiPrefsApplicationClass *klass)
192 {
193 GObjectClass *object_class = G_OBJECT_CLASS(klass);
194 GApplicationClass *application_class = G_APPLICATION_CLASS(klass);
195
196 object_class->dispose = pui_prefs_application_dispose;
197
198 application_class->startup = pui_prefs_application_startup;
199 application_class->activate = pui_prefs_application_activate;
200 }
201
202 static void
203 pui_prefs_application_init(PuiPrefsApplication *self)
204 {
205 /* do nothing, implementation required */
206 }
207
208 PuiPrefsApplication *
209 pui_prefs_application_new(void)
210 {
211 return (g_object_new(PUI_TYPE_PREFS_APPLICATION,
212 "application-id", APPLICATION_ID, NULL));
213 }