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.
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
;
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
;
79 G_DEFINE_TYPE (LogviewPrefs
, logview_prefs
, G_TYPE_OBJECT
);
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
);
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
),
104 G_STRUCT_OFFSET (LogviewPrefsClass
, system_font_changed
),
106 g_cclosure_marshal_VOID__STRING
,
109 signals
[HAVE_TEAROFF_CHANGED
] = g_signal_new ("have-tearoff-changed",
110 G_OBJECT_CLASS_TYPE (oclass
),
112 G_STRUCT_OFFSET (LogviewPrefsClass
, have_tearoff_changed
),
114 g_cclosure_marshal_VOID__BOOLEAN
,
118 g_type_class_add_private (klass
, sizeof (LogviewPrefsPrivate
));
122 have_tearoff_changed_cb (GSettings
*settings
,
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
);
134 monospace_font_changed_cb (GSettings
*settings
,
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 ":"
150 load_filters (LogviewPrefs
*prefs
)
155 LogviewFilter
*filter
;
160 filters
= g_settings_get_strv (prefs
->priv
->logview_prefs
,
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
++) {
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
]),
193 g_object_ref (filter
);
194 g_object_unref (tag
);
198 g_strfreev (filters
);
202 save_filter_foreach_func (gpointer key
, gpointer value
, gpointer user_data
)
206 LogviewFilter
*filter
;
207 GdkColor
*foreground
;
208 gboolean foreground_set
;
209 GdkColor
*background
;
210 gboolean background_set
;
211 gchar
*regex
, *color
;
214 GString
*prefs_string
;
217 filter
= LOGVIEW_FILTER (value
);
221 prefs_string
= g_string_new (name
);
222 g_string_append (prefs_string
, DELIMITER
);
224 g_object_get (filter
,
229 "foreground-gdk", &foreground
,
230 "paragraph-background-gdk", &background
,
231 "foreground-set", &foreground_set
,
232 "paragraph-background-set", &background_set
,
233 "invisible", &invisible
, NULL
);
236 g_string_append (prefs_string
, "1" DELIMITER
);
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
);
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
);
260 gdk_color_free (background
);
263 g_string_append (prefs_string
, DELIMITER
);
264 g_string_append (prefs_string
, regex
);
267 g_object_unref (tag
);
269 g_ptr_array_add (filters
, g_string_free (prefs_string
, FALSE
));
273 save_filters (LogviewPrefs
*prefs
)
276 gchar
**filters_strv
;
278 filters
= g_ptr_array_new ();
279 g_hash_table_foreach (prefs
->priv
->filters
,
280 save_filter_foreach_func
,
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
,
288 (const gchar
**) filters_strv
);
290 g_strfreev (filters_strv
);
294 get_filters_foreach (gpointer key
, gpointer value
, gpointer user_data
)
298 *list
= g_list_append (*list
, value
);
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
);
325 singleton
= g_object_new (LOGVIEW_TYPE_PREFS
, NULL
);
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
,
338 g_settings_set_int (prefs
->priv
->logview_prefs
,
339 PREF_HEIGHT
, height
);
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
,
350 *height
= g_settings_get_int (prefs
->priv
->logview_prefs
,
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
;
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
));
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 () */
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
,
388 logview_prefs_store_log (LogviewPrefs
*prefs
, GFile
*file
)
392 gboolean found
= FALSE
;
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
)) {
406 g_object_unref (stored
);
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
,
421 (const gchar
**) stored_logs
);
424 g_strfreev (stored_logs
);
428 logview_prefs_remove_stored_log (LogviewPrefs
*prefs
, GFile
*target
)
432 GPtrArray
*new_value
;
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
,
458 (const gchar
**) stored_logs
);
460 g_strfreev (stored_logs
);
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
);
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
);
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
,
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
,
514 logview_prefs_remove_filter (LogviewPrefs
*prefs
,
517 g_assert (LOGVIEW_IS_PREFS (prefs
));
519 g_hash_table_remove (prefs
->priv
->filters
,
522 save_filters (prefs
);
526 logview_prefs_add_filter (LogviewPrefs
*prefs
,
527 LogviewFilter
*filter
)
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
);
541 logview_prefs_get_filter (LogviewPrefs
*prefs
,
544 g_assert (LOGVIEW_IS_PREFS (prefs
));
546 return g_hash_table_lookup (prefs
->priv
->filters
, name
);