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