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