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