Debian Codegraph @

src/modules/frontend/gtk/di.c

raw
1 /*****************************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * cdebconf is (c) 2000-2007 Randolph Chung and others under the following
7 * license.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *****************************************************************************/
33
34 /** @file di.c
35 * specific debian-installer bits of the GTK+ frontend
36 */
37
38 #include "di.h"
39
40 #include <string.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <syslog.h>
45
46 #include <gtk/gtk.h>
47 #include <gdk/gdkkeysyms.h>
48
49 #include "question.h"
50 #include "database.h"
51
52 #include "cdebconf_gtk.h"
53 #include "fe_data.h"
54 #include "ui.h"
55
56 /** Private data for d-i specific bits. */
57 struct di_data {
58 /** Previously known kepmap, taken from debian-installer/keymap. */
59 char * previous_keymap;
60 /** Previously known language, taken from debconf/language. */
61 char * previous_language;
62 };
63
64 /** Get the current value for a given template.
65 *
66 * The caller needs to free the returned string.
67 *
68 * @param fe cdebconf frontend
69 * @param template name of the template
70 * @return a newly allocated string containing the value, or an empty string
71 * if the template was not found
72 */
73 static gchar * get_question_value(struct frontend * fe, const char * template)
74 {
75 struct question * question = fe->qdb->methods.get(fe->qdb, template);
76 const char * result = NULL;
77
78 if (NULL != question) {
79 result = question_getvalue(question, "" /* no language */);
80 }
81 if (NULL == result) {
82 result = "";
83 }
84 return g_strdup(result);
85 }
86
87 /** Print the given string to the "syslog".
88 *
89 * @param string string to print
90 */
91 static void print_to_syslog(const gchar * string)
92 {
93 syslog(LOG_USER | LOG_DEBUG, "%s", string);
94 }
95
96 /** Get a statically allocated string corresponding to the given GTK+
97 * log level.
98 *
99 * @param log_level the log level
100 * @return a statically allocated string corresponding to the log level
101 */
102 static const char * get_prefix(GLogLevelFlags log_level)
103 {
104 switch (log_level & G_LOG_LEVEL_MASK) {
105 case G_LOG_LEVEL_ERROR: return "ERROR";
106 case G_LOG_LEVEL_CRITICAL: return "CRITICAL";
107 case G_LOG_LEVEL_WARNING: return "WARNING";
108 case G_LOG_LEVEL_MESSAGE: return "Message";
109 case G_LOG_LEVEL_INFO: return "INFO";
110 case G_LOG_LEVEL_DEBUG: return "DEBUG";
111 default: return "(unknown)";
112 }
113 }
114
115 /** Implementation of GLogFunc for the GTK+ frontend.
116 *
117 * This will log messages going through the glib log system to the standard
118 * syslog.
119 *
120 * @param log_domain the log domain of the message
121 * @param log_level the log level of the message
122 * @param message the message to process
123 * @param user_data user data, set in g_log_set_handler()
124 */
125 static void log_glib_to_syslog(const gchar * log_domain,
126 GLogLevelFlags log_level, gchar const * message,
127 gpointer user_data)
128 {
129 GString * gstring;
130 gchar * string;
131
132 gstring = g_string_new(NULL);
133 g_string_append_printf(gstring, "cdebconf_gtk ");
134 g_string_append_printf(gstring, "(process:%lu): ", (gulong) getpid());
135 if (NULL != log_domain) {
136 g_string_append_printf(gstring, "%s - ", log_domain);
137 }
138 g_string_append_printf(gstring, "%s: ", get_prefix(log_level));
139 g_string_append(gstring, message);
140 string = g_string_free(gstring, FALSE);
141 print_to_syslog(string);
142 g_free(string);
143 }
144
145 /** Implementation of GLogFunc for the GTK+ frontend.
146 *
147 * This will log messages going through the glib log system to the standard
148 * syslog.
149 *
150 * @param log_domain the log domain of the message
151 * @param log_level the log level of the message
152 * @param message the message to process
153 * @param user_data user data, set in g_log_set_handler()
154 */
155 static GLogWriterOutput log_glib_structured_to_syslog(GLogLevelFlags log_level,
156 const GLogField *fields,
157 gsize n_fields,
158 gpointer user_data)
159 {
160 const char *domain = NULL;
161 const char *message = "(no message)";
162 gsize i;
163
164 for (i = 0; i < n_fields; i++) {
165 const GLogField *field = &fields[i];
166
167 if (g_strcmp0(field->key, "MESSAGE") == 0) {
168 message = field->value;
169 }
170 else if (g_strcmp0(field->key, "GLIB_DOMAIN") == 0) {
171 domain = field->value;
172 }
173 }
174
175 log_glib_to_syslog(domain, log_level, message, NULL);
176 return G_LOG_WRITER_HANDLED;
177 }
178
179 /** Make the given window fullscreen.
180 *
181 * @param window main window
182 */
183 static void make_fullscreen(GtkWidget * window)
184 {
185 GdkScreen * screen;
186
187 screen = gtk_window_get_screen(GTK_WINDOW(window));
188 gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
189 gdk_screen_get_height(screen));
190 gtk_window_fullscreen(GTK_WINDOW(window));
191 }
192
193 /** Increase or decrease font size
194 * @param factor (> 1 to increase, < 1 to decrease)
195 */
196 static void di_change_font_size(struct frontend *fe, float factor)
197 {
198 GtkSettings *gsettings;
199 char *font_name;
200 char *size_s, *end;
201 char *sed;
202 long size, newsize;
203 FILE *zoom;
204 const char *zoom_path = "/var/lib/cdebconf/zoom";
205 unsigned old_factor;
206
207 gsettings = gtk_settings_get_default ();
208 g_object_get(gsettings, "gtk-font-name", &font_name, NULL);
209 if (!font_name)
210 return;
211
212 size_s = strpbrk(font_name, "0123456789");
213 if (!size_s) {
214 g_free(font_name);
215 return;
216 }
217 size = strtol(size_s, &end, 10);
218 if (end == size_s) {
219 g_free(font_name);
220 return;
221 }
222
223 newsize = size * factor;
224 if (newsize == size) {
225 if (factor < 1) {
226 newsize = size - 1;
227 } else
228 newsize = size + 1;
229 }
230 if (newsize <= 0)
231 newsize = 1;
232
233 zoom = fopen(zoom_path, "r");
234 if (!zoom)
235 old_factor = 100;
236 else {
237 if (fscanf(zoom, "%u", &old_factor) != 1)
238 old_factor = 100;
239 fclose(zoom);
240 }
241
242 zoom = fopen(zoom_path, "w");
243 if (!zoom)
244 return;
245 fprintf(zoom, "%u\n", (unsigned) (old_factor * factor));
246 fclose(zoom);
247
248 asprintf(&sed, "sed -i 's/^gtk-font-name.*$/gtk-font-name = \"%.*s%d%s\"/' "
249 "/etc/gtk-2.0/gtkrc",
250 (int) (size_s - font_name), font_name, (int) newsize, end);
251 system(sed);
252 free(sed);
253 g_free(font_name);
254
255 gtk_rc_reparse_all_for_settings(gsettings, TRUE);
256 cdebconf_gtk_set_answer_notok(fe);
257 }
258
259 /** Key event handler implementing global key shortcuts.
260 *
261 * @param widget main window
262 * @param key the pressed key
263 * @param fe cdebconf frontend
264 * @return TRUE if "Cancel" was handled, FALSE otherwise
265 */
266 static gboolean di_shortcuts(GtkWidget * widget, GdkEventKey * key,
267 struct frontend * fe)
268 {
269 if (GDK_KEY_ZoomIn == key->keyval ||
270 ((GDK_KEY_plus == key->keyval
271 || GDK_KEY_KP_Add == key->keyval)
272 && GDK_CONTROL_MASK & key->state)) {
273 di_change_font_size(fe, 1.25);
274 return TRUE;
275 }
276 if (GDK_KEY_ZoomOut == key->keyval ||
277 ((GDK_KEY_minus == key->keyval
278 || GDK_KEY_KP_Subtract == key->keyval)
279 && GDK_CONTROL_MASK & key->state)) {
280 di_change_font_size(fe, 0.8);
281 return TRUE;
282 }
283 return FALSE;
284 }
285
286 /** Add global keyboard shortcuts
287 *
288 * @param fe cdebconf frontend
289 */
290 static void set_shortcuts(struct frontend *fe)
291 {
292 struct frontend_data * fe_data = fe->data;
293 cdebconf_gtk_add_global_key_handler(fe, fe_data->window, G_CALLBACK(di_shortcuts));
294 }
295
296 /** Setup d-i specific bits.
297 *
298 * This will create and initialize the relevant data structure.
299 *
300 * This will allow setup the glib log handler to log through standard
301 * syslog.
302 *
303 * @param fe cdebconf frontend
304 * @return FALSE if initialization failed
305 * @see log_glib_to_syslog()
306 */
307 gboolean cdebconf_gtk_di_setup(struct frontend * fe)
308 {
309 struct frontend_data * fe_data = fe->data;
310 struct di_data * di_data;
311 GdkCursor * cursor;
312
313 g_assert(NULL == fe_data->di_data);
314 if (NULL == (di_data = g_malloc0(sizeof (struct di_data)))) {
315 return FALSE;
316 }
317 di_data->previous_keymap = get_question_value(
318 fe, "debian-installer/keymap");
319 di_data->previous_language = get_question_value(fe, "debconf/language");
320
321 fe_data->di_data = di_data;
322
323 (void) g_set_printerr_handler(print_to_syslog);
324 g_log_set_writer_func(log_glib_structured_to_syslog, NULL, NULL);
325
326 make_fullscreen(fe_data->window);
327 set_shortcuts(fe);
328
329 cursor = gdk_cursor_new_for_display(gdk_display_get_default(), GDK_LEFT_PTR);
330 gdk_window_set_cursor(gdk_get_default_root_window(), cursor);
331 gdk_cursor_unref(cursor);
332
333 return TRUE;
334 }
335
336 /** Returns the current text direction.
337 *
338 * @param fe cdebconf frontend
339 * @return the current text direction
340 */
341 static GtkTextDirection get_text_direction(struct frontend * fe)
342 {
343 char * dirstr;
344 GtkTextDirection direction;
345
346 dirstr = cdebconf_gtk_get_text(fe, "debconf/text-direction",
347 "LTR - default");
348
349 if ('R' == dirstr[0]) {
350 direction = GTK_TEXT_DIR_RTL;
351 } else {
352 direction = GTK_TEXT_DIR_LTR;
353 }
354 g_free(dirstr);
355 return direction;
356 }
357
358 /** Update various settings with the current language settings.
359 *
360 * @param fe cdebconf frontend
361 */
362 static void refresh_language(struct frontend * fe)
363 {
364 /* This will enable different fonts to be used for different language. */
365 gtk_rc_reparse_all();
366 /* Adapt text direction. */
367 gtk_widget_set_default_direction(get_text_direction(fe));
368 }
369
370 /** Update what needs to be updated on a new user interaction.
371 *
372 * This must be called after gdk_threads_enter().
373 *
374 * @param fe cdebconf frontend
375 */
376 void cdebconf_gtk_di_run_dialog(struct frontend * fe)
377 {
378 struct frontend_data * fe_data = fe->data;
379 struct di_data * di_data = fe_data->di_data;
380 char * keymap;
381 char * language;
382
383 g_assert(NULL != di_data);
384
385 cdebconf_gtk_update_frontend_title(fe);
386 keymap = get_question_value(fe, "debian-installer/keymap");
387 if (0 != strcmp(keymap, di_data->previous_keymap)) {
388 g_free(di_data->previous_keymap);
389 di_data->previous_keymap = keymap;
390 } else {
391 g_free(keymap);
392 }
393
394 language = get_question_value(fe, "debconf/language");
395 if (0 != strcmp(language, di_data->previous_language)) {
396 refresh_language(fe);
397 g_free(di_data->previous_language);
398 di_data->previous_language = language;
399 } else {
400 g_free(language);
401 }
402 }
403
404 /** Clean up d-i specific data structures.
405 *
406 * @param fe cdebconf frontend
407 */
408 void cdebconf_gtk_di_shutdown(struct frontend * fe)
409 {
410 struct frontend_data * fe_data = fe->data;
411 struct di_data * di_data = fe_data->di_data;
412
413 if (NULL != di_data) {
414 fe_data->di_data = NULL;
415 if (NULL != di_data->previous_keymap) {
416 g_free(di_data->previous_keymap);
417 }
418 if (NULL != di_data->previous_language) {
419 g_free(di_data->previous_language);
420 }
421 g_free(di_data);
422 }
423 }
424
425 /* vim: et sw=4 si
426 */