projects/package-update-indicator

view pui-prefs-application.c @ 52:707e9e3deeac

Do not hardcode pkg-config

Using a macro allows passing a different executable in case of
cross-compilation.
author Helmut Grohne <helmut@subdivi.de>
date Sat Dec 12 01:08:21 2020 -0500 (11 months ago)
parents 2f04ec9e0506
children
line source
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 */
24 #include <glib/gi18n.h>
25 #include <gio/gio.h>
27 #include "pui-prefs-application.h"
28 #include "pui-settings.h"
30 #define COLUMN_REFRESH_INTERVAL 2
32 struct _PuiPrefsApplication {
33 GtkApplication parent_instance;
34 GSettings *settings;
35 };
37 G_DEFINE_TYPE(PuiPrefsApplication, pui_prefs_application, GTK_TYPE_APPLICATION)
39 static void pui_prefs_application_quit(GSimpleAction *, GVariant *,
40 gpointer);
42 static const GActionEntry pui_prefs_application_actions[] = {
43 { "quit", pui_prefs_application_quit }
44 };
46 static void
47 pui_prefs_application_quit(GSimpleAction *simple, GVariant *parameter,
48 gpointer user_data)
49 {
50 PuiPrefsApplication *self = user_data;
52 g_application_quit(G_APPLICATION(self));
53 }
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;
67 setting_interval = g_variant_get_uint32(variant);
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);
83 return (TRUE);
84 }
85 }
87 g_debug("mapping refresh-interval %" G_GUINT32_FORMAT " to index -1",
88 setting_interval);
89 g_value_set_int(value, -1);
91 return (TRUE);
92 }
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;
104 index = g_value_get_int(value);
105 if (!gtk_tree_model_iter_nth_child(tree_model, &iter, NULL, index)) {
106 return (NULL);
107 }
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);
116 return (g_variant_new_uint32(model_interval));
117 }
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 GtkWidget *use_mobile_check_button;
132 application_class->startup(application);
134 /* create actions */
135 g_action_map_add_action_entries(G_ACTION_MAP(self),
136 pui_prefs_application_actions,
137 G_N_ELEMENTS(pui_prefs_application_actions), self);
139 /* get widgets from builder */
140 builder = gtk_builder_new_from_resource("/org/guido-berhoerster/code/"
141 "package-update-indicator/preferences/pui-prefs-window.ui");
142 window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
143 gtk_application_add_window(GTK_APPLICATION(self), GTK_WINDOW(window));
144 update_command_entry = GTK_WIDGET(gtk_builder_get_object(builder,
145 "update-command"));
146 refresh_interval_combo_box = GTK_WIDGET(gtk_builder_get_object(builder,
147 "refresh-interval"));
148 tree_model =
149 gtk_combo_box_get_model(GTK_COMBO_BOX(refresh_interval_combo_box));
150 use_mobile_check_button = GTK_WIDGET(gtk_builder_get_object(builder,
151 "use-mobile-connection"));
153 /* bind settings to widgets */
154 self->settings = pui_settings_new();
155 g_settings_bind(self->settings, "update-command",
156 update_command_entry, "text", G_SETTINGS_BIND_DEFAULT);
157 g_settings_bind_with_mapping(self->settings, "refresh-interval",
158 refresh_interval_combo_box, "active", G_SETTINGS_BIND_DEFAULT,
159 map_refresh_interval_to_index, map_index_to_refresh_interval,
160 tree_model, NULL);
161 g_settings_bind(self->settings, "use-mobile-connection",
162 use_mobile_check_button, "active", G_SETTINGS_BIND_DEFAULT);
164 /* show window */
165 gtk_widget_show(window);
166 gtk_window_present(GTK_WINDOW(window));
168 g_object_unref(builder);
169 }
171 static void
172 pui_prefs_application_activate(GApplication *application) {
173 GtkApplication *gtk_application = GTK_APPLICATION(application);
174 GApplicationClass *application_class =
175 G_APPLICATION_CLASS(pui_prefs_application_parent_class);
177 /* raise window when activated */
178 gtk_window_present(gtk_application_get_active_window(gtk_application));
180 application_class->activate(application);
181 }
183 static void
184 pui_prefs_application_dispose(GObject *object)
185 {
186 PuiPrefsApplication *self = PUI_PREFS_APPLICATION(object);
188 if (self->settings != NULL) {
189 g_clear_object(&self->settings);
190 }
192 G_OBJECT_CLASS(pui_prefs_application_parent_class)->dispose(object);
193 }
195 static void
196 pui_prefs_application_class_init(PuiPrefsApplicationClass *klass)
197 {
198 GObjectClass *object_class = G_OBJECT_CLASS(klass);
199 GApplicationClass *application_class = G_APPLICATION_CLASS(klass);
201 object_class->dispose = pui_prefs_application_dispose;
203 application_class->startup = pui_prefs_application_startup;
204 application_class->activate = pui_prefs_application_activate;
205 }
207 static void
208 pui_prefs_application_init(PuiPrefsApplication *self)
209 {
210 /* do nothing, implementation required */
211 }
213 PuiPrefsApplication *
214 pui_prefs_application_new(void)
215 {
216 return (g_object_new(PUI_TYPE_PREFS_APPLICATION,
217 "application-id", APPLICATION_ID, NULL));
218 }