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