b9b78c2580bd9764eecd3f133800f44b7e66b78a
[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 (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_save_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                         buf[0] = '\0';
305                 }
306
307                 if (buf[0] != '\0') {
308                         if (fputs(buf, fp) == EOF) {
309                                 perror("fputs");
310                                 return -1;
311                         }
312                 }
313         }
314
315         return 0;
316 }
317
318 void prefs_set_default(PrefParam *param)
319 {
320         gint i;
321         GdkColor color;
322
323         g_return_if_fail(param != NULL);
324
325         for (i = 0; param[i].name != NULL; i++) {
326                 if (!param[i].data) continue;
327
328                 switch (param[i].type) {
329                 case P_STRING:
330                 case P_PASSWORD:
331                         if (param[i].defval != NULL) {
332                                 if (!strncasecmp(param[i].defval, "ENV_", 4))
333                                         *((gchar **)param[i].data) =
334                                                 g_strdup(g_getenv(param[i].defval + 4));
335                                 else if (param[i].defval[0] == '~')
336                                         *((gchar **)param[i].data) =
337                                                 g_strconcat(get_home_dir(),
338                                                             param[i].defval + 1,
339                                                             NULL);
340                                 else if (param[i].defval[0] != '\0')
341                                         *((gchar **)param[i].data) =
342                                                 g_strdup(param[i].defval);
343                                 else
344                                         *((gchar **)param[i].data) = NULL;
345                         } else
346                                 *((gchar **)param[i].data) = NULL;
347                         break;
348                 case P_INT:
349                         if (param[i].defval != NULL)
350                                 *((gint *)param[i].data) =
351                                         (gint)atoi(param[i].defval);
352                         else
353                                 *((gint *)param[i].data) = 0;
354                         break;
355                 case P_BOOL:
356                         if (param[i].defval != NULL) {
357                                 if (!strcasecmp(param[i].defval, "TRUE"))
358                                         *((gboolean *)param[i].data) = TRUE;
359                                 else
360                                         *((gboolean *)param[i].data) =
361                                                 atoi(param[i].defval) ? TRUE : FALSE;
362                         } else
363                                 *((gboolean *)param[i].data) = FALSE;
364                         break;
365                 case P_ENUM:
366                         if (param[i].defval != NULL)
367                                 *((DummyEnum*)param[i].data) =
368                                         (DummyEnum)atoi(param[i].defval);
369                         else
370                                 *((DummyEnum *)param[i].data) = 0;
371                         break;
372                 case P_USHORT:
373                         if (param[i].defval != NULL)
374                                 *((gushort *)param[i].data) =
375                                         (gushort)atoi(param[i].defval);
376                         else
377                                 *((gushort *)param[i].data) = 0;
378                         break;
379                 case P_COLOR:
380                         if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
381                                 *((gulong *)param[i].data) =
382                                         RGB_FROM_GDK_COLOR(color);
383                         else if (param[i].defval)
384                                 /* be compatible and accept ints */
385                                 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10); 
386                         else
387                                 *((gulong *)param[i].data) = 0; 
388                         break;
389                 default:
390                         break;
391                 }
392         }
393 }
394
395 void prefs_free(PrefParam *param)
396 {
397         gint i;
398
399         g_return_if_fail(param != NULL);
400
401         for (i = 0; param[i].name != NULL; i++) {
402                 if (!param[i].data) continue;
403
404                 switch (param[i].type) {
405                 case P_STRING:
406                 case P_PASSWORD:
407                         g_free(*((gchar **)param[i].data));
408                         break;
409                 default:
410                         break;
411                 }
412         }
413 }
414
415 void prefs_dialog_create(PrefsDialog *dialog)
416 {
417         GtkWidget *window;
418         GtkWidget *vbox;
419         GtkWidget *notebook;
420
421         GtkWidget *confirm_area;
422         GtkWidget *ok_btn;
423         GtkWidget *cancel_btn;
424         GtkWidget *apply_btn;
425
426         g_return_if_fail(dialog != NULL);
427
428         window = gtk_window_new (GTK_WINDOW_DIALOG);
429         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
430         gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
431         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
432         gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
433
434         vbox = gtk_vbox_new (FALSE, 6);
435         gtk_widget_show(vbox);
436         gtk_container_add (GTK_CONTAINER (window), vbox);
437
438         notebook = gtk_notebook_new ();
439         gtk_widget_show(notebook);
440         gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
441         gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
442         /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
443         gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
444         
445         gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
446         
447         gtkut_button_set_create(&confirm_area,
448                                 &ok_btn,        _("OK"),
449                                 &cancel_btn,    _("Cancel"),
450                                 &apply_btn,     _("Apply"));
451         gtk_widget_show(confirm_area);
452         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
453         gtk_widget_grab_default(ok_btn);
454
455         dialog->window     = window;
456         dialog->notebook   = notebook;
457         dialog->ok_btn     = ok_btn;
458         dialog->cancel_btn = cancel_btn;
459         dialog->apply_btn  = apply_btn;
460 }
461
462 void prefs_dialog_destroy(PrefsDialog *dialog)
463 {
464         gtk_widget_destroy(dialog->window);
465         dialog->window     = NULL;
466         dialog->notebook   = NULL;
467         dialog->ok_btn     = NULL;
468         dialog->cancel_btn = NULL;
469         dialog->apply_btn  = NULL;
470 }
471
472 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
473 {
474         gboolean is_active;
475
476         is_active = gtk_toggle_button_get_active(toggle_btn);
477         gtk_widget_set_sensitive(widget, is_active);
478 }
479
480 void prefs_set_dialog(PrefParam *param)
481 {
482         gint i;
483
484         for (i = 0; param[i].name != NULL; i++) {
485                 if (param[i].widget_set_func)
486                         param[i].widget_set_func(&param[i]);
487         }
488 }
489
490 void prefs_set_data_from_dialog(PrefParam *param)
491 {
492         gint i;
493
494         for (i = 0; param[i].name != NULL; i++) {
495                 if (param[i].data_set_func)
496                         param[i].data_set_func(&param[i]);
497         }
498 }
499
500 void prefs_set_dialog_to_default(PrefParam *param)
501 {
502         gint       i;
503         PrefParam  tmpparam;
504         gchar     *str_data = NULL;
505         gint       int_data;
506         gushort    ushort_data;
507         gboolean   bool_data;
508         DummyEnum  enum_data;
509
510         for (i = 0; param[i].name != NULL; i++) {
511                 if (!param[i].widget_set_func) continue;
512
513                 tmpparam = param[i];
514
515                 switch (tmpparam.type) {
516                 case P_STRING:
517                 case P_PASSWORD:
518                         if (tmpparam.defval) {
519                                 if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
520                                         str_data = g_strdup(g_getenv(param[i].defval + 4));
521                                         tmpparam.data = &str_data;
522                                         break;
523                                 } else if (tmpparam.defval[0] == '~') {
524                                         str_data =
525                                                 g_strconcat(get_home_dir(),
526                                                             param[i].defval + 1,
527                                                             NULL);
528                                         tmpparam.data = &str_data;
529                                         break;
530                                 }
531                         }
532                         tmpparam.data = &tmpparam.defval;
533                         break;
534                 case P_INT:
535                         if (tmpparam.defval)
536                                 int_data = atoi(tmpparam.defval);
537                         else
538                                 int_data = 0;
539                         tmpparam.data = &int_data;
540                         break;
541                 case P_USHORT:
542                         if (tmpparam.defval)
543                                 ushort_data = atoi(tmpparam.defval);
544                         else
545                                 ushort_data = 0;
546                         tmpparam.data = &ushort_data;
547                         break;
548                 case P_BOOL:
549                         if (tmpparam.defval) {
550                                 if (!strcasecmp(tmpparam.defval, "TRUE"))
551                                         bool_data = TRUE;
552                                 else
553                                         bool_data = atoi(tmpparam.defval)
554                                                 ? TRUE : FALSE;
555                         } else
556                                 bool_data = FALSE;
557                         tmpparam.data = &bool_data;
558                         break;
559                 case P_ENUM:
560                         if (tmpparam.defval)
561                                 enum_data = (DummyEnum)atoi(tmpparam.defval);
562                         else
563                                 enum_data = 0;
564                         tmpparam.data = &enum_data;
565                         break;
566                 case P_OTHER:
567                 default:
568                         break;
569                 }
570                 tmpparam.widget_set_func(&tmpparam);
571                 g_free(str_data);
572                 str_data = NULL;
573         }
574 }
575
576 void prefs_set_data_from_entry(PrefParam *pparam)
577 {
578         gchar **str, *entry_str;
579
580         g_return_if_fail(*pparam->widget != NULL);
581
582         entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
583
584         switch (pparam->type) {
585         case P_STRING:
586         case P_PASSWORD:
587                 str = (gchar **)pparam->data;
588                 g_free(*str);
589                 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
590                 break;
591         case P_USHORT:
592                 *((gushort *)pparam->data) = atoi(entry_str);
593                 break;
594         case P_INT:
595                 *((gint *)pparam->data) = atoi(entry_str);
596                 break;
597         default:
598                 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
599                           pparam->type);
600         }
601 }
602
603 void prefs_set_entry(PrefParam *pparam)
604 {
605         gchar **str;
606
607         g_return_if_fail(*pparam->widget != NULL);
608
609         switch (pparam->type) {
610         case P_STRING:
611         case P_PASSWORD:
612                 str = (gchar **)pparam->data;
613                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
614                                    *str ? *str : "");
615                 break;
616         case P_INT:
617                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
618                                    itos(*((gint *)pparam->data)));
619                 break;
620         case P_USHORT:
621                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
622                                    itos(*((gushort *)pparam->data)));
623                 break;
624         default:
625                 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
626                           pparam->type);
627         }
628 }
629
630 void prefs_set_data_from_text(PrefParam *pparam)
631 {
632         gchar **str;
633         gchar *text, *tp;
634         gchar *tmp, *tmpp;
635
636         g_return_if_fail(*pparam->widget != NULL);
637
638         switch (pparam->type) {
639         case P_STRING:
640         case P_PASSWORD:
641                 str = (gchar **)pparam->data;
642                 g_free(*str);
643                 tp = text = gtk_editable_get_chars
644                         (GTK_EDITABLE(*pparam->widget), 0, -1);
645                 if (text[0] == '\0') {
646                         *str = NULL;
647                         g_free(text);
648                         break;
649                 }
650
651                 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
652                         { *str = NULL; break; });
653                 while (*tp) {
654                         if (*tp == '\n') {
655                                 *tmpp++ = '\\';
656                                 *tmpp++ = 'n';
657                                 tp++;
658                         } else
659                                 *tmpp++ = *tp++;
660                 }
661                 *tmpp = '\0';
662                 *str = g_strdup(tmp);
663                 g_free(text);
664                 break;
665         default:
666                 g_warning("Invalid PrefType for GtkText widget: %d\n",
667                           pparam->type);
668         }
669 }
670
671 void prefs_set_text(PrefParam *pparam)
672 {
673         gchar *buf, *sp, *bufp;
674         gchar **str;
675         GtkText *text;
676
677         g_return_if_fail(*pparam->widget != NULL);
678
679         switch (pparam->type) {
680         case P_STRING:
681         case P_PASSWORD:
682                 str = (gchar **)pparam->data;
683                 if (*str) {
684                         bufp = buf = alloca(strlen(*str) + 1);
685                         if (!buf) buf = "";
686                         else {
687                                 sp = *str;
688                                 while (*sp) {
689                                         if (*sp == '\\' && *(sp + 1) == 'n') {
690                                                 *bufp++ = '\n';
691                                                 sp += 2;
692                                         } else
693                                                 *bufp++ = *sp++;
694                                 }
695                                 *bufp = '\0';
696                         }
697                 } else
698                         buf = "";
699
700                 text = GTK_TEXT(*pparam->widget);
701                 gtk_text_set_point(text, 0);
702                 gtk_text_forward_delete(text, gtk_text_get_length(text));
703                 gtk_text_set_point(text, 0);
704                 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
705                 break;
706         default:
707                 g_warning("Invalid PrefType for GtkText widget: %d\n",
708                           pparam->type);
709         }
710 }
711
712 void prefs_set_data_from_toggle(PrefParam *pparam)
713 {
714         g_return_if_fail(pparam->type == P_BOOL);
715         g_return_if_fail(*pparam->widget != NULL);
716         
717         *((gboolean *)pparam->data) =
718                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
719 }
720
721 void prefs_set_toggle(PrefParam *pparam)
722 {
723         g_return_if_fail(pparam->type == P_BOOL);
724         g_return_if_fail(*pparam->widget != NULL);
725
726         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
727                                      *((gboolean *)pparam->data));
728 }
729
730 void prefs_set_data_from_spinbtn(PrefParam *pparam)
731 {
732         g_return_if_fail(*pparam->widget != NULL);
733
734         switch (pparam->type) {
735         case P_INT:
736                 *((gint *)pparam->data) =
737                         gtk_spin_button_get_value_as_int
738                         (GTK_SPIN_BUTTON(*pparam->widget));
739                 break;
740         case P_USHORT:
741                 *((gushort *)pparam->data) =
742                         (gushort)gtk_spin_button_get_value_as_int
743                         (GTK_SPIN_BUTTON(*pparam->widget));
744                 break;
745         default:
746                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
747                           pparam->type);
748         }
749 }
750
751 void prefs_set_spinbtn(PrefParam *pparam)
752 {
753         g_return_if_fail(*pparam->widget != NULL);
754
755         switch (pparam->type) {
756         case P_INT:
757                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
758                                           (gfloat)*((gint *)pparam->data));
759                 break;
760         case P_USHORT:
761                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
762                                           (gfloat)*((gushort *)pparam->data));
763                 break;
764         default:
765                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
766                           pparam->type);
767         }
768 }
769
770 static GSList *prefs_pages = NULL;
771
772 void prefs_gtk_open(void)
773 {
774         prefswindow_open(prefs_pages, NULL);
775 }
776
777 void prefs_gtk_register_page(PrefsPage *page)
778 {
779         prefs_pages = g_slist_append(prefs_pages, page);
780 }
781
782 void prefs_gtk_unregister_page(PrefsPage *page)
783 {
784         prefs_pages = g_slist_remove(prefs_pages, page);
785 }