Updated Spanish translation
[gnome-utils.git] / logview / logview-prefs.c
blobd411f2cd3afc0e2b1fbe3b741972b5e03bad9bd2
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /* logview-prefs.c - logview user preferences handling
4 * Copyright (C) 1998 Cesar Miquel <miquel@df.uba.ar>
5 * Copyright (C) 2004 Vincent Noel
6 * Copyright (C) 2006 Emmanuele Bassi
7 * Copyright (C) 2008 Cosimo Cecchi <cosimoc@gnome.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include <sys/stat.h>
29 #include <string.h>
30 #include <gtk/gtk.h>
32 #include "logview-prefs.h"
34 #define LOGVIEW_DEFAULT_HEIGHT 400
35 #define LOGVIEW_DEFAULT_WIDTH 600
37 /* logview settings */
38 #define LOGVIEW_SCHEMA "org.gnome.gnome-system-log"
39 #define PREF_WIDTH "width"
40 #define PREF_HEIGHT "height"
41 #define PREF_LOGFILE "logfile"
42 #define PREF_LOGFILES "logfiles"
43 #define PREF_FONTSIZE "fontsize"
44 #define PREF_FILTERS "filters"
46 /* desktop-wide settings */
47 #define GNOME_MONOSPACE_FONT_NAME "monospace-font-name"
48 #define GNOME_MENUS_HAVE_TEAROFF "menus-have-tearoff"
50 static LogviewPrefs *singleton = NULL;
52 enum {
53 SYSTEM_FONT_CHANGED,
54 HAVE_TEAROFF_CHANGED,
55 LAST_SIGNAL
58 enum {
59 FILTER_NAME,
60 FILTER_INVISIBLE,
61 FILTER_FOREGROUND,
62 FILTER_BACKGROUND,
63 FILTER_REGEX,
64 MAX_TOKENS
67 static guint signals[LAST_SIGNAL] = { 0 };
69 #define GET_PRIVATE(o) \
70 (G_TYPE_INSTANCE_GET_PRIVATE ((o), LOGVIEW_TYPE_PREFS, LogviewPrefsPrivate))
72 struct _LogviewPrefsPrivate {
73 GSettings *logview_prefs;
74 GSettings *interface_prefs;
76 GHashTable *filters;
79 G_DEFINE_TYPE (LogviewPrefs, logview_prefs, G_TYPE_OBJECT);
81 static void
82 do_finalize (GObject *obj)
84 LogviewPrefs *prefs = LOGVIEW_PREFS (obj);
86 g_hash_table_destroy (prefs->priv->filters);
88 g_object_unref (prefs->priv->logview_prefs);
89 g_object_unref (prefs->priv->interface_prefs);
91 G_OBJECT_CLASS (logview_prefs_parent_class)->finalize (obj);
94 static void
95 logview_prefs_class_init (LogviewPrefsClass *klass)
97 GObjectClass *oclass = G_OBJECT_CLASS (klass);
99 oclass->finalize = do_finalize;
101 signals[SYSTEM_FONT_CHANGED] = g_signal_new ("system-font-changed",
102 G_OBJECT_CLASS_TYPE (oclass),
103 G_SIGNAL_RUN_LAST,
104 G_STRUCT_OFFSET (LogviewPrefsClass, system_font_changed),
105 NULL, NULL,
106 g_cclosure_marshal_VOID__STRING,
107 G_TYPE_NONE, 1,
108 G_TYPE_STRING);
109 signals[HAVE_TEAROFF_CHANGED] = g_signal_new ("have-tearoff-changed",
110 G_OBJECT_CLASS_TYPE (oclass),
111 G_SIGNAL_RUN_LAST,
112 G_STRUCT_OFFSET (LogviewPrefsClass, have_tearoff_changed),
113 NULL, NULL,
114 g_cclosure_marshal_VOID__BOOLEAN,
115 G_TYPE_NONE, 1,
116 G_TYPE_BOOLEAN);
118 g_type_class_add_private (klass, sizeof (LogviewPrefsPrivate));
121 static void
122 have_tearoff_changed_cb (GSettings *settings,
123 gchar *key,
124 gpointer data)
126 LogviewPrefs *prefs = data;
127 gboolean add_tearoffs;
129 add_tearoffs = g_settings_get_boolean (settings, key);
130 g_signal_emit (prefs, signals[HAVE_TEAROFF_CHANGED], 0, add_tearoffs, NULL);
133 static void
134 monospace_font_changed_cb (GSettings *settings,
135 gchar *key,
136 gpointer data)
138 LogviewPrefs *prefs = data;
139 gchar *monospace_font_name;
141 monospace_font_name = g_settings_get_string (settings, key);
142 g_signal_emit (prefs, signals[SYSTEM_FONT_CHANGED], 0, monospace_font_name, NULL);
144 g_free (monospace_font_name);
147 #define DELIMITER ":"
149 static void
150 load_filters (LogviewPrefs *prefs)
152 gchar **filters;
153 gchar **tokens;
154 const gchar *str;
155 LogviewFilter *filter;
156 GtkTextTag *tag;
157 GdkColor color;
158 gint idx;
160 filters = g_settings_get_strv (prefs->priv->logview_prefs,
161 PREF_FILTERS);
163 prefs->priv->filters = g_hash_table_new_full (g_str_hash, g_str_equal,
164 (GDestroyNotify) g_free,
165 (GDestroyNotify) g_object_unref);
167 for (idx = 0; filters[idx] != NULL; idx++) {
168 str = filters[idx];
169 tokens = g_strsplit (str, DELIMITER, MAX_TOKENS);
170 filter = logview_filter_new (tokens[FILTER_NAME], tokens[FILTER_REGEX]);
171 tag = gtk_text_tag_new (tokens[FILTER_NAME]);
173 g_object_set (tag, "invisible",
174 g_str_equal (tokens[FILTER_INVISIBLE], "1"), NULL);
176 if (strlen (tokens[FILTER_FOREGROUND])) {
177 gdk_color_parse (tokens[FILTER_FOREGROUND], &color);
178 g_object_set (tag, "foreground-gdk", &color,
179 "foreground-set", TRUE, NULL);
182 if (strlen (tokens[FILTER_BACKGROUND])) {
183 gdk_color_parse (tokens[FILTER_BACKGROUND], &color);
184 g_object_set (tag, "paragraph-background-gdk", &color,
185 "paragraph-background-set", TRUE, NULL);
188 g_object_set (filter, "texttag", tag, NULL);
189 g_hash_table_insert (prefs->priv->filters,
190 g_strdup(tokens[FILTER_NAME]),
191 filter);
193 g_object_ref (filter);
194 g_object_unref (tag);
195 g_strfreev (tokens);
198 g_strfreev (filters);
201 static void
202 save_filter_foreach_func (gpointer key, gpointer value, gpointer user_data)
204 GPtrArray *filters;
205 const gchar *name;
206 LogviewFilter *filter;
207 GdkColor *foreground;
208 gboolean foreground_set;
209 GdkColor *background;
210 gboolean background_set;
211 gchar *regex, *color;
212 gboolean invisible;
213 GtkTextTag *tag;
214 GString *prefs_string;
216 filters = user_data;
217 filter = LOGVIEW_FILTER (value);
218 name = key;
219 color = NULL;
221 prefs_string = g_string_new (name);
222 g_string_append (prefs_string, DELIMITER);
224 g_object_get (filter,
225 "regex", &regex,
226 "texttag", &tag,
227 NULL);
228 g_object_get (tag,
229 "foreground-gdk", &foreground,
230 "paragraph-background-gdk", &background,
231 "foreground-set", &foreground_set,
232 "paragraph-background-set", &background_set,
233 "invisible", &invisible, NULL);
235 if (invisible) {
236 g_string_append (prefs_string, "1" DELIMITER);
237 } else {
238 g_string_append (prefs_string, "0" DELIMITER);
241 if (foreground_set) {
242 color = gdk_color_to_string (foreground);
243 g_string_append (prefs_string, color);
244 g_free (color);
247 if (foreground) {
248 gdk_color_free (foreground);
251 g_string_append (prefs_string, DELIMITER);
253 if (background_set) {
254 color = gdk_color_to_string (background);
255 g_string_append (prefs_string, color);
256 g_free (color);
259 if (background) {
260 gdk_color_free (background);
263 g_string_append (prefs_string, DELIMITER);
264 g_string_append (prefs_string, regex);
266 g_free (regex);
267 g_object_unref (tag);
269 g_ptr_array_add (filters, g_string_free (prefs_string, FALSE));
272 static void
273 save_filters (LogviewPrefs *prefs)
275 GPtrArray *filters;
276 gchar **filters_strv;
278 filters = g_ptr_array_new ();
279 g_hash_table_foreach (prefs->priv->filters,
280 save_filter_foreach_func,
281 filters);
282 g_ptr_array_add (filters, NULL);
284 filters_strv = (gchar **) g_ptr_array_free (filters, FALSE);
286 g_settings_set_strv (prefs->priv->logview_prefs,
287 PREF_FILTERS,
288 (const gchar **) filters_strv);
290 g_strfreev (filters_strv);
293 static void
294 get_filters_foreach (gpointer key, gpointer value, gpointer user_data)
296 GList **list;
297 list = user_data;
298 *list = g_list_append (*list, value);
301 static void
302 logview_prefs_init (LogviewPrefs *self)
304 LogviewPrefsPrivate *priv;
306 priv = self->priv = GET_PRIVATE (self);
308 priv->logview_prefs = g_settings_new (LOGVIEW_SCHEMA);
309 priv->interface_prefs = g_settings_new ("org.gnome.desktop.interface");
311 g_signal_connect (priv->interface_prefs, "changed::" GNOME_MONOSPACE_FONT_NAME,
312 G_CALLBACK (monospace_font_changed_cb), self);
313 g_signal_connect (priv->interface_prefs, "changed::" GNOME_MENUS_HAVE_TEAROFF,
314 G_CALLBACK (have_tearoff_changed_cb), self);
316 load_filters (self);
319 /* public methods */
321 LogviewPrefs *
322 logview_prefs_get ()
324 if (!singleton)
325 singleton = g_object_new (LOGVIEW_TYPE_PREFS, NULL);
327 return singleton;
330 void
331 logview_prefs_store_window_size (LogviewPrefs *prefs,
332 int width, int height)
334 g_assert (LOGVIEW_IS_PREFS (prefs));
336 g_settings_set_int (prefs->priv->logview_prefs,
337 PREF_WIDTH, width);
338 g_settings_set_int (prefs->priv->logview_prefs,
339 PREF_HEIGHT, height);
342 void
343 logview_prefs_get_stored_window_size (LogviewPrefs *prefs,
344 int *width, int *height)
346 g_assert (LOGVIEW_IS_PREFS (prefs));
348 *width = g_settings_get_int (prefs->priv->logview_prefs,
349 PREF_WIDTH);
350 *height = g_settings_get_int (prefs->priv->logview_prefs,
351 PREF_HEIGHT);
353 if ((*width == 0) ^ (*height == 0)) {
354 /* if one of the two failed, return default for both */
355 *width = LOGVIEW_DEFAULT_WIDTH;
356 *height = LOGVIEW_DEFAULT_HEIGHT;
360 char *
361 logview_prefs_get_monospace_font_name (LogviewPrefs *prefs)
363 g_assert (LOGVIEW_IS_PREFS (prefs));
365 return (g_settings_get_string (prefs->priv->interface_prefs, GNOME_MONOSPACE_FONT_NAME));
368 gboolean
369 logview_prefs_get_have_tearoff (LogviewPrefs *prefs)
371 g_assert (LOGVIEW_IS_PREFS (prefs));
373 return (g_settings_get_boolean (prefs->priv->interface_prefs, GNOME_MENUS_HAVE_TEAROFF));
376 /* the elements should be freed with g_free () */
378 gchar **
379 logview_prefs_get_stored_logfiles (LogviewPrefs *prefs)
381 g_assert (LOGVIEW_IS_PREFS (prefs));
383 return g_settings_get_strv (prefs->priv->logview_prefs,
384 PREF_LOGFILES);
387 void
388 logview_prefs_store_log (LogviewPrefs *prefs, GFile *file)
390 gchar **stored_logs;
391 GFile *stored;
392 gboolean found = FALSE;
393 gint idx, old_size;
395 g_assert (LOGVIEW_IS_PREFS (prefs));
396 g_assert (G_IS_FILE (file));
398 stored_logs = logview_prefs_get_stored_logfiles (prefs);
400 for (idx = 0; stored_logs[idx] != NULL; idx++) {
401 stored = g_file_parse_name (stored_logs[idx]);
402 if (g_file_equal (file, stored)) {
403 found = TRUE;
406 g_object_unref (stored);
408 if (found) {
409 break;
413 if (!found) {
414 old_size = g_strv_length (stored_logs);
415 stored_logs = g_realloc (stored_logs, (old_size + 1) * sizeof (gchar *));
416 stored_logs[old_size] = g_file_get_parse_name (file);
417 stored_logs[old_size + 1] = NULL;
419 g_settings_set_strv (prefs->priv->logview_prefs,
420 PREF_LOGFILES,
421 (const gchar **) stored_logs);
424 g_strfreev (stored_logs);
427 void
428 logview_prefs_remove_stored_log (LogviewPrefs *prefs, GFile *target)
430 gchar **stored_logs;
431 GFile *stored;
432 GPtrArray *new_value;
433 gint idx;
434 gboolean removed = FALSE;
436 g_assert (LOGVIEW_IS_PREFS (prefs));
437 g_assert (G_IS_FILE (target));
439 stored_logs = logview_prefs_get_stored_logfiles (prefs);
440 new_value = g_ptr_array_new ();
442 for (idx = 0; stored_logs[idx] != NULL; idx++) {
443 stored = g_file_parse_name (stored_logs[idx]);
444 if (!g_file_equal (stored, target)) {
445 g_ptr_array_add (new_value, g_strdup (stored_logs[idx]));
448 g_object_unref (stored);
451 g_ptr_array_add (new_value, NULL);
452 g_strfreev (stored_logs);
454 stored_logs = (gchar **) g_ptr_array_free (new_value, FALSE);
456 g_settings_set_strv (prefs->priv->logview_prefs,
457 PREF_LOGFILES,
458 (const gchar **) stored_logs);
460 g_strfreev (stored_logs);
463 void
464 logview_prefs_store_fontsize (LogviewPrefs *prefs, int fontsize)
466 g_assert (LOGVIEW_IS_PREFS (prefs));
467 g_assert (fontsize > 0);
469 g_settings_set_int (prefs->priv->logview_prefs, PREF_FONTSIZE, fontsize);
473 logview_prefs_get_stored_fontsize (LogviewPrefs *prefs)
475 g_assert (LOGVIEW_IS_PREFS (prefs));
477 return g_settings_get_int (prefs->priv->logview_prefs, PREF_FONTSIZE);
480 void
481 logview_prefs_store_active_logfile (LogviewPrefs *prefs,
482 const char *filename)
484 g_assert (LOGVIEW_IS_PREFS (prefs));
486 g_settings_set_string (prefs->priv->logview_prefs,
487 PREF_LOGFILE, filename);
490 char *
491 logview_prefs_get_active_logfile (LogviewPrefs *prefs)
493 g_assert (LOGVIEW_IS_PREFS (prefs));
495 return g_settings_get_string (prefs->priv->logview_prefs,
496 PREF_LOGFILE);
499 GList *
500 logview_prefs_get_filters (LogviewPrefs *prefs)
502 GList *filters = NULL;
504 g_assert (LOGVIEW_IS_PREFS (prefs));
506 g_hash_table_foreach (prefs->priv->filters,
507 get_filters_foreach,
508 &filters);
510 return filters;
513 void
514 logview_prefs_remove_filter (LogviewPrefs *prefs,
515 const gchar *name)
517 g_assert (LOGVIEW_IS_PREFS (prefs));
519 g_hash_table_remove (prefs->priv->filters,
520 name);
522 save_filters (prefs);
525 void
526 logview_prefs_add_filter (LogviewPrefs *prefs,
527 LogviewFilter *filter)
529 gchar* name;
531 g_assert (LOGVIEW_IS_PREFS (prefs));
532 g_assert (LOGVIEW_IS_FILTER (filter));
534 g_object_get (filter, "name", &name, NULL);
535 g_hash_table_insert (prefs->priv->filters, name, g_object_ref (filter));
537 save_filters (prefs);
540 LogviewFilter *
541 logview_prefs_get_filter (LogviewPrefs *prefs,
542 const gchar *name)
544 g_assert (LOGVIEW_IS_PREFS (prefs));
546 return g_hash_table_lookup (prefs->priv->filters, name);