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