1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2004-2008 Imendio AB
4  * Copyright (C) 2010 Lanedo GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 #include "config.h"
23 #include <gtk/gtk.h>
24 #include <string.h>
25 #include "dh-util.h"
26 #include "dh-preferences.h"
27 #include "ige-conf.h"
28 #include "dh-base.h"
29 
30 typedef struct {
31 	GtkWidget *dialog;
32 
33         /* Fonts tab */
34 	GtkWidget *system_fonts_button;
35 	GtkWidget *fonts_table;
36 	GtkWidget *variable_font_button;
37 	GtkWidget *fixed_font_button;
38 	guint      use_system_fonts_id;
39 	guint      system_var_id;
40 	guint      system_fixed_id;
41 	guint      var_id;
42 	guint      fixed_id;
43 
44         /* Book Shelf tab */
45         DhBookManager *book_manager;
46         GtkTreeView   *booklist_treeview;
47         GtkListStore  *booklist_store;
48 } DhPreferences;
49 
50 /* Fonts-tab related */
51 static void     preferences_fonts_font_set_cb               (GtkFontButton    *button,
52                                                              gpointer          user_data);
53 static void     preferences_fonts_system_fonts_toggled_cb   (GtkToggleButton  *button,
54                                                              gpointer          user_data);
55 #if 0
56 static void     preferences_fonts_var_font_notify_cb        (IgeConf          *client,
57                                                              const gchar      *path,
58                                                              gpointer          user_data);
59 static void     preferences_fonts_fixed_font_notify_cb      (IgeConf          *client,
60                                                              const gchar      *path,
61                                                              gpointer          user_data);
62 static void     preferences_fonts_use_system_font_notify_cb (IgeConf          *client,
63                                                              const gchar      *path,
64                                                              gpointer          user_data);
65 static void     preferences_connect_conf_listeners          (void);
66 #endif
67 static void     preferences_fonts_get_font_names            (gboolean          use_system_fonts,
68                                                              gchar           **variable,
69                                                              gchar           **fixed);
70 
71 /* Bookshelf-tab related */
72 static void     preferences_bookshelf_tree_selection_toggled_cb (GtkCellRendererToggle *cell_renderer,
73                                                                  gchar                 *path,
74                                                                  gpointer               user_data);
75 static void     preferences_bookshelf_populate_store            (void);
76 
77 /* Common */
78 static void     preferences_close_cb                        (GtkButton        *button,
79                                                              gpointer          user_data);
80 
81 #define DH_CONF_PATH                  "/apps/devhelp"
82 #define DH_CONF_USE_SYSTEM_FONTS      DH_CONF_PATH "/ui/use_system_fonts"
83 #define DH_CONF_VARIABLE_FONT         DH_CONF_PATH "/ui/variable_font"
84 #define DH_CONF_FIXED_FONT            DH_CONF_PATH "/ui/fixed_font"
85 #define DH_CONF_SYSTEM_VARIABLE_FONT  "/desktop/gnome/interface/font_name"
86 #define DH_CONF_SYSTEM_FIXED_FONT     "/desktop/gnome/interface/monospace_font_name"
87 
88 /* Book list store columns... */
89 #define LTCOLUMN_ENABLED  0
90 #define LTCOLUMN_TITLE    1
91 #define LTCOLUMN_BOOK     2
92 
93 static DhPreferences *prefs;
94 
95 static void
preferences_init(void)96 preferences_init (void)
97 {
98 	if (!prefs) {
99                 prefs = g_new0 (DhPreferences, 1);
100                 prefs->book_manager  = dh_base_get_book_manager (dh_base_get ());
101         }
102 }
103 
104 static void
preferences_close_cb(GtkButton * button,gpointer user_data)105 preferences_close_cb (GtkButton *button, gpointer user_data)
106 {
107 	DhPreferences *prefs = user_data;
108 
109 	gtk_widget_destroy (GTK_WIDGET (prefs->dialog));
110 	prefs->dialog = NULL;
111 
112 	prefs->system_fonts_button = NULL;
113 	prefs->fonts_table = NULL;
114 	prefs->variable_font_button = NULL;
115 	prefs->fixed_font_button = NULL;
116 
117         prefs->booklist_treeview = NULL;
118         prefs->booklist_store = NULL;
119 }
120 
121 static void
preferences_fonts_font_set_cb(GtkFontButton * button,gpointer user_data)122 preferences_fonts_font_set_cb (GtkFontButton *button,
123                                gpointer       user_data)
124 {
125 	DhPreferences *prefs = user_data;
126 	const gchar   *font_name;
127 	const gchar   *key;
128 
129 	font_name = gtk_font_button_get_font_name (button);
130 
131 	if (GTK_WIDGET (button) == prefs->variable_font_button) {
132 		key = DH_CONF_VARIABLE_FONT;
133 	} else {
134 		key = DH_CONF_FIXED_FONT;
135 	}
136 
137 	ige_conf_set_string (ige_conf_get (), key, font_name);
138 }
139 
140 static void
preferences_fonts_system_fonts_toggled_cb(GtkToggleButton * button,gpointer user_data)141 preferences_fonts_system_fonts_toggled_cb (GtkToggleButton *button,
142                                            gpointer         user_data)
143 {
144 	DhPreferences *prefs = user_data;
145 	gboolean       active;
146 
147 	active = gtk_toggle_button_get_active (button);
148 
149 	ige_conf_set_bool (ige_conf_get (),
150                            DH_CONF_USE_SYSTEM_FONTS,
151                            active);
152 
153 	gtk_widget_set_sensitive (prefs->fonts_table, !active);
154 }
155 
156 #if 0
157 static void
158 preferences_fonts_var_font_notify_cb (IgeConf     *client,
159                                       const gchar *path,
160                                       gpointer     user_data)
161 {
162 	DhPreferences *prefs = user_data;
163 	gboolean       use_system_fonts;
164 	gchar         *font_name;
165 
166         ige_conf_get_bool (ige_conf_get (),
167                            DH_CONF_USE_SYSTEM_FONTS,
168                            &use_system_fonts);
169 
170 	if (prefs->variable_font_button) {
171 		ige_conf_get_string (ige_conf_get (), path, &font_name);
172 		gtk_font_button_set_font_name (GTK_FONT_BUTTON (prefs->variable_font_button),
173 					       font_name);
174                 g_free (font_name);
175 	}
176 }
177 
178 static void
179 preferences_fonts_fixed_font_notify_cb (IgeConf     *client,
180                                         const gchar *path,
181                                         gpointer     user_data)
182 {
183 	DhPreferences *prefs = user_data;
184 	gboolean       use_system_fonts;
185 	gchar         *font_name;
186 
187 	ige_conf_get_bool (ige_conf_get (),
188                            DH_CONF_USE_SYSTEM_FONTS,
189                            &use_system_fonts);
190 
191 	if (prefs->fixed_font_button) {
192                 ige_conf_get_string (ige_conf_get (), path, &font_name);
193 		gtk_font_button_set_font_name (GTK_FONT_BUTTON (prefs->fixed_font_button),
194 					       font_name);
195                 g_free (font_name);
196 	}
197 }
198 
199 static void
200 preferences_fonts_use_system_font_notify_cb (IgeConf     *client,
201                                              const gchar *path,
202                                              gpointer     user_data)
203 {
204 	DhPreferences *prefs = user_data;
205 	gboolean       use_system_fonts;
206 
207 	ige_conf_get_bool (ige_conf_get (), path, &use_system_fonts);
208 
209 	if (prefs->system_fonts_button) {
210 		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (prefs->system_fonts_button),
211 					      use_system_fonts);
212 	}
213 
214 	if (prefs->fonts_table) {
215 		gtk_widget_set_sensitive (prefs->fonts_table, !use_system_fonts);
216 	}
217 }
218 
219 /* FIXME: This is not hooked up yet (to update the dialog if the values are
220  * changed outside of devhelp).
221  */
222 static void
223 preferences_connect_conf_listeners (void)
224 {
225 	IgeConf *conf;
226 
227 	conf = ige_conf_get ();
228 
229 	prefs->use_system_fonts_id =
230 		ige_conf_notify_add (conf,
231                                      DH_CONF_USE_SYSTEM_FONTS,
232                                      preferences_use_system_font_notify_cb,
233                                      prefs);
234 	prefs->system_var_id =
235 		ige_conf_notify_add (conf,
236                                      DH_CONF_SYSTEM_VARIABLE_FONT,
237                                      preferences_var_font_notify_cb,
238                                      prefs);
239 	prefs->system_fixed_id =
240 		ige_conf_notify_add (conf,
241                                      DH_CONF_SYSTEM_FIXED_FONT,
242                                      preferences_fixed_font_notify_cb,
243                                      prefs);
244 	prefs->var_id =
245 		ige_conf_notify_add (conf,
246                                      DH_CONF_VARIABLE_FONT,
247                                      preferences_var_font_notify_cb,
248                                      prefs);
249 	prefs->fixed_id =
250 		ige_conf_notify_add (conf,
251                                      DH_CONF_FIXED_FONT,
252                                      preferences_fixed_font_notify_cb,
253                                      prefs);
254 }
255 #endif
256 
257 /* FIXME: Use the functions in dh-util.c for this. */
258 static void
preferences_fonts_get_font_names(gboolean use_system_fonts,gchar ** variable,gchar ** fixed)259 preferences_fonts_get_font_names (gboolean   use_system_fonts,
260                                   gchar    **variable,
261                                   gchar    **fixed)
262 {
263 	gchar   *var_font_name, *fixed_font_name;
264 	IgeConf *conf;
265 
266 	conf = ige_conf_get ();
267 
268 	if (use_system_fonts) {
269 #ifdef GDK_WINDOWING_QUARTZ
270                 var_font_name = g_strdup ("Lucida Grande 14");
271                 fixed_font_name = g_strdup ("Monaco 14");
272 #else
273 		ige_conf_get_string (conf,
274                                      DH_CONF_SYSTEM_VARIABLE_FONT,
275                                      &var_font_name);
276 		ige_conf_get_string (conf,
277                                      DH_CONF_SYSTEM_FIXED_FONT,
278                                      &fixed_font_name);
279 #endif
280 	} else {
281 		ige_conf_get_string (conf,
282                                      DH_CONF_VARIABLE_FONT,
283                                      &var_font_name);
284                 ige_conf_get_string (conf,
285                                      DH_CONF_FIXED_FONT,
286                                      &fixed_font_name);
287 	}
288 
289 	*variable = var_font_name;
290 	*fixed = fixed_font_name;
291 }
292 
293 static void
preferences_bookshelf_tree_selection_toggled_cb(GtkCellRendererToggle * cell_renderer,gchar * path,gpointer user_data)294 preferences_bookshelf_tree_selection_toggled_cb (GtkCellRendererToggle *cell_renderer,
295                                                  gchar                 *path,
296                                                  gpointer               user_data)
297 {
298         GtkTreeIter iter;
299 
300         if (gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (prefs->booklist_store),
301                                                  &iter,
302                                                  path))
303         {
304                 gpointer book = NULL;
305                 gboolean enabled;
306 
307                 gtk_tree_model_get (GTK_TREE_MODEL (prefs->booklist_store),
308                                     &iter,
309                                     LTCOLUMN_BOOK,       &book,
310                                     LTCOLUMN_ENABLED,    &enabled,
311                                     -1);
312 
313                 if (book) {
314                         /* Update book conf */
315                         dh_book_set_enabled (book, !enabled);
316 
317                         gtk_list_store_set (prefs->booklist_store, &iter,
318                                             LTCOLUMN_ENABLED, !enabled,
319                                             -1);
320 
321                         dh_book_manager_update (prefs->book_manager);
322                 }
323         }
324 }
325 
326 static void
preferences_bookshelf_populate_store(void)327 preferences_bookshelf_populate_store (void)
328 {
329         GList         *l;
330 
331         for (l = dh_book_manager_get_books (prefs->book_manager);
332              l;
333              l = g_list_next (l)) {
334                 GtkTreeIter  iter;
335                 DhBook      *book;
336 
337                 book = DH_BOOK (l->data);
338 
339                 gtk_list_store_append (prefs->booklist_store, &iter);
340                 gtk_list_store_set (prefs->booklist_store, &iter,
341                                     LTCOLUMN_ENABLED,  dh_book_get_enabled (book),
342                                     LTCOLUMN_TITLE,    dh_book_get_title (book),
343                                     LTCOLUMN_BOOK,     book,
344                                     -1);
345         }
346 }
347 
348 void
dh_preferences_show_dialog(GtkWindow * parent)349 dh_preferences_show_dialog (GtkWindow *parent)
350 {
351         gchar      *path;
352 	GtkBuilder *builder;
353 	gboolean    use_system_fonts;
354 	gchar      *var_font_name, *fixed_font_name;
355 
356         preferences_init ();
357 
358 	if (prefs->dialog != NULL) {
359 		gtk_window_present (GTK_WINDOW (prefs->dialog));
360 		return;
361 	}
362 
363         path = dh_util_build_data_filename ("devhelp", "ui",
364                                             "devhelp.builder",
365                                             NULL);
366 	builder = dh_util_builder_get_file (
367                 path,
368                 "preferences_dialog",
369                 NULL,
370                 "preferences_dialog", &prefs->dialog,
371                 "fonts_table", &prefs->fonts_table,
372                 "system_fonts_button", &prefs->system_fonts_button,
373                 "variable_font_button", &prefs->variable_font_button,
374                 "fixed_font_button", &prefs->fixed_font_button,
375                 "book_manager_store", &prefs->booklist_store,
376                 "book_manager_treeview", &prefs->booklist_treeview,
377                 NULL);
378         g_free (path);
379 
380 	dh_util_builder_connect (
381                 builder,
382                 prefs,
383                 "variable_font_button", "font_set", preferences_fonts_font_set_cb,
384                 "fixed_font_button", "font_set", preferences_fonts_font_set_cb,
385                 "system_fonts_button", "toggled", preferences_fonts_system_fonts_toggled_cb,
386                 "book_manager_toggle", "toggled", preferences_bookshelf_tree_selection_toggled_cb,
387                 "preferences_close_button", "clicked", preferences_close_cb,
388                 NULL);
389 
390 	ige_conf_get_bool (ige_conf_get (),
391                            DH_CONF_USE_SYSTEM_FONTS,
392                            &use_system_fonts);
393 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (prefs->system_fonts_button),
394 				      use_system_fonts);
395 	gtk_widget_set_sensitive (prefs->fonts_table, !use_system_fonts);
396 
397 	preferences_fonts_get_font_names (FALSE, &var_font_name, &fixed_font_name);
398 
399 	if (var_font_name) {
400 		gtk_font_button_set_font_name (GTK_FONT_BUTTON (prefs->variable_font_button),
401 					       var_font_name);
402 		g_free (var_font_name);
403 	}
404 
405 	if (fixed_font_name) {
406 		gtk_font_button_set_font_name (GTK_FONT_BUTTON (prefs->fixed_font_button),
407 					       fixed_font_name);
408 		g_free (fixed_font_name);
409 	}
410 
411         preferences_bookshelf_populate_store ();
412 
413 	g_object_unref (builder);
414 
415 	gtk_window_set_transient_for (GTK_WINDOW (prefs->dialog), parent);
416 	gtk_widget_show_all (prefs->dialog);
417 }
418