2005-01-04 [christoph] 0.9.13cvs27
[claws.git] / src / prefs_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <gtk/gtk.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include "intl.h"
32 #include "main.h"
33 #include "prefs.h"
34 #include "prefs_gtk.h"
35 #include "utils.h"
36 #include "gtkutils.h"
37 #include "passcrypt.h"
38 #include "base64.h"
39
40 #define CL(x)   (((gulong) (x) >> (gulong) 8) & 0xFFUL)
41 #define RGB_FROM_GDK_COLOR(c) \
42         ((CL(c.red)   << (gulong) 16) | \
43          (CL(c.green) << (gulong)  8) | \
44          (CL(c.blue)))
45
46 typedef enum
47 {
48         DUMMY_PARAM
49 } DummyEnum;
50
51 void prefs_read_config(PrefParam *param, const gchar *label,
52                        const gchar *rcfile)
53 {
54         FILE *fp;
55         gchar buf[PREFSBUFSIZE];
56         gchar *rcpath;
57         gchar *block_label;
58
59         g_return_if_fail(param != NULL);
60         g_return_if_fail(label != NULL);
61         g_return_if_fail(rcfile != NULL);
62
63         debug_print("Reading configuration...\n");
64
65         prefs_set_default(param);
66
67         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
68         if ((fp = fopen(rcpath, "rb")) == NULL) {
69                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
70                 g_free(rcpath);
71                 return;
72         }
73         g_free(rcpath);
74
75         block_label = g_strdup_printf("[%s]", label);
76
77         /* search aiming block */
78         while (fgets(buf, sizeof(buf), fp) != NULL) {
79                 gint val;
80
81                 val = strncmp(buf, block_label, strlen(block_label));
82                 if (val == 0) {
83                         debug_print("Found %s\n", block_label);
84                         break;
85                 }
86         }
87         g_free(block_label);
88
89         while (fgets(buf, sizeof(buf), fp) != NULL) {
90                 strretchomp(buf);
91                 /* reached next block */
92                 if (buf[0] == '[') break;
93
94                 prefs_config_parse_one_line(param, buf);
95         }
96
97         debug_print("Finished reading configuration.\n");
98         fclose(fp);
99 }
100
101 void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
102 {
103         gint i;
104         gint name_len;
105         const gchar *value;
106         GdkColor color;
107
108         for (i = 0; param[i].name != NULL; i++) {
109                 name_len = strlen(param[i].name);
110                 if (g_strncasecmp(buf, param[i].name, name_len))
111                         continue;
112                 if (buf[name_len] != '=')
113                         continue;
114                 value = buf + name_len + 1;
115                 /* debug_print("%s = %s\n", param[i].name, value); */
116
117                 switch (param[i].type) {
118                 case P_STRING:
119                         g_free(*((gchar **)param[i].data));
120                         *((gchar **)param[i].data) =
121                                 *value ? g_strdup(value) : NULL;
122                         break;
123                 case P_INT:
124                         *((gint *)param[i].data) =
125                                 (gint)atoi(value);
126                         break;
127                 case P_BOOL:
128                         *((gboolean *)param[i].data) =
129                                 (*value == '0' || *value == '\0')
130                                         ? FALSE : TRUE;
131                         break;
132                 case P_ENUM:
133                         *((DummyEnum *)param[i].data) =
134                                 (DummyEnum)atoi(value);
135                         break;
136                 case P_USHORT:
137                         *((gushort *)param[i].data) =
138                                 (gushort)atoi(value);
139                         break;
140                 case P_COLOR:
141                         if (gdk_color_parse(value, &color)) 
142                                 *((gulong *)param[i].data) = RGB_FROM_GDK_COLOR(color); 
143                         else 
144                                 /* be compatible and accept ints */
145                                 *((gulong *)param[i].data) = strtoul(value, 0, 10); 
146                         break;
147                 case P_PASSWORD:
148                         g_free(*((gchar **)param[i].data));
149                         if (value[0] == '!') {
150                                 gchar tmp[1024];
151                                 gint len;
152
153                                 len = base64_decode(tmp, &value[1], strlen(value) - 1);
154                                 passcrypt_decrypt(tmp, len);
155                                 tmp[len] = '\0';
156                                 *((gchar **)param[i].data) =
157                                         *tmp ? g_strdup(tmp) : NULL;
158                         } else {
159                                 *((gchar **)param[i].data) =
160                                         *value ? g_strdup(value) : NULL;
161                         }
162                         break;
163                 default:
164                         break;
165                 }
166         }
167 }
168
169 #define TRY(func) \
170 if (!(func)) \
171 { \
172         g_warning("failed to write configuration to file\n"); \
173         if (orig_fp) fclose(orig_fp); \
174         prefs_file_close_revert(pfile); \
175         g_free(rcpath); \
176         g_free(block_label); \
177         return; \
178 } \
179
180 void prefs_write_config(PrefParam *param, const gchar *label,
181                         const gchar *rcfile)
182 {
183         FILE *orig_fp;
184         PrefFile *pfile;
185         gchar *rcpath;
186         gchar buf[PREFSBUFSIZE];
187         gchar *block_label = NULL;
188         gboolean block_matched = FALSE;
189
190         g_return_if_fail(param != NULL);
191         g_return_if_fail(label != NULL);
192         g_return_if_fail(rcfile != NULL);
193
194         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
195         if ((orig_fp = fopen(rcpath, "rb")) == NULL) {
196                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
197         }
198
199         if ((pfile = prefs_write_open(rcpath)) == NULL) {
200                 g_warning("failed to write configuration to file\n");
201                 if (orig_fp) fclose(orig_fp);
202                 g_free(rcpath);
203                 return;
204         }
205
206         block_label = g_strdup_printf("[%s]", label);
207
208         /* search aiming block */
209         if (orig_fp) {
210                 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
211                         gint val;
212
213                         val = strncmp(buf, block_label, strlen(block_label));
214                         if (val == 0) {
215                                 debug_print("Found %s\n", block_label);
216                                 block_matched = TRUE;
217                                 break;
218                         } else
219                                 TRY(fputs(buf, pfile->fp) != EOF);
220                 }
221         }
222
223         TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
224         g_free(block_label);
225         block_label = NULL;
226
227         /* write all param data to file */
228         TRY(prefs_write_param(param, pfile->fp) == 0);
229
230         if (block_matched) {
231                 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
232                         /* next block */
233                         if (buf[0] == '[') {
234                                 TRY(fputc('\n', pfile->fp) != EOF &&
235                                     fputs(buf, pfile->fp)  != EOF);
236                                 break;
237                         }
238                 }
239                 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
240                         TRY(fputs(buf, pfile->fp) != EOF);
241         }
242
243         if (orig_fp) fclose(orig_fp);
244         if (prefs_file_close(pfile) < 0)
245                 g_warning("failed to write configuration to file\n");
246         g_free(rcpath);
247
248         debug_print("Configuration is saved.\n");
249 }
250
251 gint prefs_write_param(PrefParam *param, FILE *fp)
252 {
253         gint i;
254         gchar buf[PREFSBUFSIZE];
255
256         for (i = 0; param[i].name != NULL; i++) {
257                 switch (param[i].type) {
258                 case P_STRING:
259                         g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
260                                    *((gchar **)param[i].data) ?
261                                    *((gchar **)param[i].data) : "");
262                         break;
263                 case P_INT:
264                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
265                                    *((gint *)param[i].data));
266                         break;
267                 case P_BOOL:
268                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
269                                    *((gboolean *)param[i].data));
270                         break;
271                 case P_ENUM:
272                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
273                                    *((DummyEnum *)param[i].data));
274                         break;
275                 case P_USHORT:
276                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
277                                    *((gushort *)param[i].data));
278                         break;
279                 case P_COLOR:
280                         g_snprintf(buf, sizeof buf,  "%s=#%6.6lx\n", param[i].name,
281                                    *((gulong *) param[i].data));
282                         break;
283                 case P_PASSWORD:
284                         {
285                                 gchar *tmp = NULL, tmp2[1024] = {0};
286
287                                 tmp = *((gchar **)param[i].data);
288                                 if (tmp) {
289                                         gint len;
290
291                                         tmp = g_strdup(tmp);
292                                         len = strlen(tmp);
293                                         passcrypt_encrypt(tmp, len);
294                                         base64_encode(tmp2, tmp, len);
295                                         g_free(tmp);
296                                         tmp = tmp2;
297                                 }
298                                 g_snprintf(buf, sizeof(buf), "%s=!%s\n", param[i].name,
299                                            tmp ?
300                                            tmp : "");
301                         }
302                         break;
303                 default:
304                         /* unrecognized, fail */
305                         debug_print("Unrecognized parameter type\n");
306                         return -1;
307                 }
308
309                 if (buf[0] != '\0') {
310                         if (fputs(buf, fp) == EOF) {
311                                 perror("fputs");
312                                 return -1;
313                         }
314                 }
315         }
316
317         return 0;
318 }
319
320 void prefs_set_default(PrefParam *param)
321 {
322         gint i;
323         GdkColor color;
324
325         g_return_if_fail(param != NULL);
326
327         for (i = 0; param[i].name != NULL; i++) {
328                 if (!param[i].data) continue;
329
330                 switch (param[i].type) {
331                 case P_STRING:
332                 case P_PASSWORD:
333                         g_free(*((gchar **)param[i].data));
334                         if (param[i].defval != NULL) {
335                                 if (!g_strncasecmp(param[i].defval, "ENV_", 4))
336                                         *((gchar **)param[i].data) =
337                                                 g_strdup(g_getenv(param[i].defval + 4));
338                                 else if (param[i].defval[0] == '~')
339                                         *((gchar **)param[i].data) =
340                                                 g_strconcat(get_home_dir(),
341                                                             param[i].defval + 1,
342                                                             NULL);
343                                 else if (param[i].defval[0] != '\0')
344                                         *((gchar **)param[i].data) =
345                                                 g_strdup(param[i].defval);
346                                 else
347                                         *((gchar **)param[i].data) = NULL;
348                         } else
349                                 *((gchar **)param[i].data) = NULL;
350                         break;
351                 case P_INT:
352                         if (param[i].defval != NULL)
353                                 *((gint *)param[i].data) =
354                                         (gint)atoi(param[i].defval);
355                         else
356                                 *((gint *)param[i].data) = 0;
357                         break;
358                 case P_BOOL:
359                         if (param[i].defval != NULL) {
360                                 if (!g_strcasecmp(param[i].defval, "TRUE"))
361                                         *((gboolean *)param[i].data) = TRUE;
362                                 else
363                                         *((gboolean *)param[i].data) =
364                                                 atoi(param[i].defval) ? TRUE : FALSE;
365                         } else
366                                 *((gboolean *)param[i].data) = FALSE;
367                         break;
368                 case P_ENUM:
369                         if (param[i].defval != NULL)
370                                 *((DummyEnum*)param[i].data) =
371                                         (DummyEnum)atoi(param[i].defval);
372                         else
373                                 *((DummyEnum *)param[i].data) = 0;
374                         break;
375                 case P_USHORT:
376                         if (param[i].defval != NULL)
377                                 *((gushort *)param[i].data) =
378                                         (gushort)atoi(param[i].defval);
379                         else
380                                 *((gushort *)param[i].data) = 0;
381                         break;
382                 case P_COLOR:
383                         if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
384                                 *((gulong *)param[i].data) =
385                                         RGB_FROM_GDK_COLOR(color);
386                         else if (param[i].defval)
387                                 /* be compatible and accept ints */
388                                 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10); 
389                         else
390                                 *((gulong *)param[i].data) = 0; 
391                         break;
392                 default:
393                         break;
394                 }
395         }
396 }
397
398 void prefs_free(PrefParam *param)
399 {
400         gint i;
401
402         g_return_if_fail(param != NULL);
403
404         for (i = 0; param[i].name != NULL; i++) {
405                 if (!param[i].data) continue;
406
407                 switch (param[i].type) {
408                 case P_STRING:
409                 case P_PASSWORD:
410                         g_free(*((gchar **)param[i].data));
411                         break;
412                 default:
413                         break;
414                 }
415         }
416 }
417
418 void prefs_dialog_create(PrefsDialog *dialog)
419 {
420         GtkWidget *window;
421         GtkWidget *vbox;
422         GtkWidget *notebook;
423
424         GtkWidget *confirm_area;
425         GtkWidget *ok_btn;
426         GtkWidget *cancel_btn;
427         GtkWidget *apply_btn;
428
429         g_return_if_fail(dialog != NULL);
430
431         window = gtk_window_new (GTK_WINDOW_DIALOG);
432         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
433         gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
434         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
435         gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
436
437         vbox = gtk_vbox_new (FALSE, 6);
438         gtk_widget_show(vbox);
439         gtk_container_add (GTK_CONTAINER (window), vbox);
440
441         notebook = gtk_notebook_new ();
442         gtk_widget_show(notebook);
443         gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
444         gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
445         /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
446         gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
447         
448         gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
449         
450         gtkut_button_set_create(&confirm_area,
451                                 &ok_btn,        _("OK"),
452                                 &cancel_btn,    _("Cancel"),
453                                 &apply_btn,     _("Apply"));
454         gtk_widget_show(confirm_area);
455         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
456         gtk_widget_grab_default(ok_btn);
457
458         dialog->window     = window;
459         dialog->notebook   = notebook;
460         dialog->ok_btn     = ok_btn;
461         dialog->cancel_btn = cancel_btn;
462         dialog->apply_btn  = apply_btn;
463 }
464
465 void prefs_dialog_destroy(PrefsDialog *dialog)
466 {
467         gtk_widget_destroy(dialog->window);
468         dialog->window     = NULL;
469         dialog->notebook   = NULL;
470         dialog->ok_btn     = NULL;
471         dialog->cancel_btn = NULL;
472         dialog->apply_btn  = NULL;
473 }
474
475 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
476 {
477         gboolean is_active;
478
479         is_active = gtk_toggle_button_get_active(toggle_btn);
480         gtk_widget_set_sensitive(widget, is_active);
481 }
482
483 void prefs_set_dialog(PrefParam *param)
484 {
485         gint i;
486
487         for (i = 0; param[i].name != NULL; i++) {
488                 if (param[i].widget_set_func)
489                         param[i].widget_set_func(&param[i]);
490         }
491 }
492
493 void prefs_set_data_from_dialog(PrefParam *param)
494 {
495         gint i;
496
497         for (i = 0; param[i].name != NULL; i++) {
498                 if (param[i].data_set_func)
499                         param[i].data_set_func(&param[i]);
500         }
501 }
502
503 void prefs_set_dialog_to_default(PrefParam *param)
504 {
505         gint       i;
506         PrefParam  tmpparam;
507         gchar     *str_data = NULL;
508         gint       int_data;
509         gushort    ushort_data;
510         gboolean   bool_data;
511         DummyEnum  enum_data;
512
513         for (i = 0; param[i].name != NULL; i++) {
514                 if (!param[i].widget_set_func) continue;
515
516                 tmpparam = param[i];
517
518                 switch (tmpparam.type) {
519                 case P_STRING:
520                 case P_PASSWORD:
521                         if (tmpparam.defval) {
522                                 if (!g_strncasecmp(tmpparam.defval, "ENV_", 4)) {
523                                         str_data = g_strdup(g_getenv(param[i].defval + 4));
524                                         tmpparam.data = &str_data;
525                                         break;
526                                 } else if (tmpparam.defval[0] == '~') {
527                                         str_data =
528                                                 g_strconcat(get_home_dir(),
529                                                             param[i].defval + 1,
530                                                             NULL);
531                                         tmpparam.data = &str_data;
532                                         break;
533                                 }
534                         }
535                         tmpparam.data = &tmpparam.defval;
536                         break;
537                 case P_INT:
538                         if (tmpparam.defval)
539                                 int_data = atoi(tmpparam.defval);
540                         else
541                                 int_data = 0;
542                         tmpparam.data = &int_data;
543                         break;
544                 case P_USHORT:
545                         if (tmpparam.defval)
546                                 ushort_data = atoi(tmpparam.defval);
547                         else
548                                 ushort_data = 0;
549                         tmpparam.data = &ushort_data;
550                         break;
551                 case P_BOOL:
552                         if (tmpparam.defval) {
553                                 if (!g_strcasecmp(tmpparam.defval, "TRUE"))
554                                         bool_data = TRUE;
555                                 else
556                                         bool_data = atoi(tmpparam.defval)
557                                                 ? TRUE : FALSE;
558                         } else
559                                 bool_data = FALSE;
560                         tmpparam.data = &bool_data;
561                         break;
562                 case P_ENUM:
563                         if (tmpparam.defval)
564                                 enum_data = (DummyEnum)atoi(tmpparam.defval);
565                         else
566                                 enum_data = 0;
567                         tmpparam.data = &enum_data;
568                         break;
569                 case P_OTHER:
570                 default:
571                         break;
572                 }
573                 tmpparam.widget_set_func(&tmpparam);
574                 g_free(str_data);
575                 str_data = NULL;
576         }
577 }
578
579 void prefs_set_data_from_entry(PrefParam *pparam)
580 {
581         gchar **str, *entry_str;
582
583         g_return_if_fail(*pparam->widget != NULL);
584
585         entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
586
587         switch (pparam->type) {
588         case P_STRING:
589         case P_PASSWORD:
590                 str = (gchar **)pparam->data;
591                 g_free(*str);
592                 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
593                 break;
594         case P_USHORT:
595                 *((gushort *)pparam->data) = atoi(entry_str);
596                 break;
597         case P_INT:
598                 *((gint *)pparam->data) = atoi(entry_str);
599                 break;
600         default:
601                 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
602                           pparam->type);
603         }
604 }
605
606 void prefs_set_entry(PrefParam *pparam)
607 {
608         gchar **str;
609
610         g_return_if_fail(*pparam->widget != NULL);
611
612         switch (pparam->type) {
613         case P_STRING:
614         case P_PASSWORD:
615                 str = (gchar **)pparam->data;
616                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
617                                    *str ? *str : "");
618                 break;
619         case P_INT:
620                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
621                                    itos(*((gint *)pparam->data)));
622                 break;
623         case P_USHORT:
624                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
625                                    itos(*((gushort *)pparam->data)));
626                 break;
627         default:
628                 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
629                           pparam->type);
630         }
631 }
632
633 void prefs_set_data_from_text(PrefParam *pparam)
634 {
635         gchar **str;
636         gchar *text, *tp;
637         gchar *tmp, *tmpp;
638
639         g_return_if_fail(*pparam->widget != NULL);
640
641         switch (pparam->type) {
642         case P_STRING:
643         case P_PASSWORD:
644                 str = (gchar **)pparam->data;
645                 g_free(*str);
646                 tp = text = gtk_editable_get_chars
647                         (GTK_EDITABLE(*pparam->widget), 0, -1);
648                 if (text[0] == '\0') {
649                         *str = NULL;
650                         g_free(text);
651                         break;
652                 }
653
654                 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
655                         { *str = NULL; break; });
656                 while (*tp) {
657                         if (*tp == '\n') {
658                                 *tmpp++ = '\\';
659                                 *tmpp++ = 'n';
660                                 tp++;
661                         } else
662                                 *tmpp++ = *tp++;
663                 }
664                 *tmpp = '\0';
665                 *str = g_strdup(tmp);
666                 g_free(text);
667                 break;
668         default:
669                 g_warning("Invalid PrefType for GtkText widget: %d\n",
670                           pparam->type);
671         }
672 }
673
674 void prefs_set_text(PrefParam *pparam)
675 {
676         gchar *buf, *sp, *bufp;
677         gchar **str;
678         GtkText *text;
679
680         g_return_if_fail(*pparam->widget != NULL);
681
682         switch (pparam->type) {
683         case P_STRING:
684         case P_PASSWORD:
685                 str = (gchar **)pparam->data;
686                 if (*str) {
687                         bufp = buf = alloca(strlen(*str) + 1);
688                         if (!buf) buf = "";
689                         else {
690                                 sp = *str;
691                                 while (*sp) {
692                                         if (*sp == '\\' && *(sp + 1) == 'n') {
693                                                 *bufp++ = '\n';
694                                                 sp += 2;
695                                         } else
696                                                 *bufp++ = *sp++;
697                                 }
698                                 *bufp = '\0';
699                         }
700                 } else
701                         buf = "";
702
703                 text = GTK_TEXT(*pparam->widget);
704                 gtk_text_set_point(text, 0);
705                 gtk_text_forward_delete(text, gtk_text_get_length(text));
706                 gtk_text_set_point(text, 0);
707                 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
708                 break;
709         default:
710                 g_warning("Invalid PrefType for GtkText widget: %d\n",
711                           pparam->type);
712         }
713 }
714
715 void prefs_set_data_from_toggle(PrefParam *pparam)
716 {
717         g_return_if_fail(pparam->type == P_BOOL);
718         g_return_if_fail(*pparam->widget != NULL);
719         
720         *((gboolean *)pparam->data) =
721                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
722 }
723
724 void prefs_set_toggle(PrefParam *pparam)
725 {
726         g_return_if_fail(pparam->type == P_BOOL);
727         g_return_if_fail(*pparam->widget != NULL);
728
729         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
730                                      *((gboolean *)pparam->data));
731 }
732
733 void prefs_set_data_from_spinbtn(PrefParam *pparam)
734 {
735         g_return_if_fail(*pparam->widget != NULL);
736
737         switch (pparam->type) {
738         case P_INT:
739                 *((gint *)pparam->data) =
740                         gtk_spin_button_get_value_as_int
741                         (GTK_SPIN_BUTTON(*pparam->widget));
742                 break;
743         case P_USHORT:
744                 *((gushort *)pparam->data) =
745                         (gushort)gtk_spin_button_get_value_as_int
746                         (GTK_SPIN_BUTTON(*pparam->widget));
747                 break;
748         default:
749                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
750                           pparam->type);
751         }
752 }
753
754 void prefs_set_spinbtn(PrefParam *pparam)
755 {
756         g_return_if_fail(*pparam->widget != NULL);
757
758         switch (pparam->type) {
759         case P_INT:
760                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
761                                           (gfloat)*((gint *)pparam->data));
762                 break;
763         case P_USHORT:
764                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
765                                           (gfloat)*((gushort *)pparam->data));
766                 break;
767         default:
768                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
769                           pparam->type);
770         }
771 }
772
773 static GSList *prefs_pages = NULL;
774
775 void prefs_gtk_open(void)
776 {
777         prefswindow_open(_("Preferences"), prefs_pages, NULL);
778 }
779
780 void prefs_gtk_register_page(PrefsPage *page)
781 {
782         prefs_pages = g_slist_append(prefs_pages, page);
783 }
784
785 void prefs_gtk_unregister_page(PrefsPage *page)
786 {
787         prefs_pages = g_slist_remove(prefs_pages, page);
788 }