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