add info about tbird2syl.py
[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_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
541 {
542         gboolean is_active;
543
544         is_active = gtk_toggle_button_get_active(toggle_btn);
545         gtk_widget_set_sensitive(widget, !is_active);
546 }
547
548 void prefs_set_dialog(PrefParam *param)
549 {
550         gint i;
551
552         for (i = 0; param[i].name != NULL; i++) {
553                 if (param[i].widget_set_func)
554                         param[i].widget_set_func(&param[i]);
555         }
556 }
557
558 void prefs_set_data_from_dialog(PrefParam *param)
559 {
560         gint i;
561
562         for (i = 0; param[i].name != NULL; i++) {
563                 if (param[i].data_set_func)
564                         param[i].data_set_func(&param[i]);
565         }
566 }
567
568 void prefs_set_dialog_to_default(PrefParam *param)
569 {
570         gint       i;
571         PrefParam  tmpparam;
572         gchar     *str_data = NULL;
573         gint       int_data;
574         gushort    ushort_data;
575         gboolean   bool_data;
576         DummyEnum  enum_data;
577
578         for (i = 0; param[i].name != NULL; i++) {
579                 if (!param[i].widget_set_func) continue;
580
581                 tmpparam = param[i];
582
583                 switch (tmpparam.type) {
584                 case P_STRING:
585                 case P_PASSWORD:
586                         if (tmpparam.defval) {
587                                 if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
588                                         str_data = g_strdup(g_getenv(param[i].defval + 4));
589                                         tmpparam.data = &str_data;
590                                         break;
591                                 } else if (tmpparam.defval[0] == '~') {
592                                         str_data =
593                                                 g_strconcat(get_home_dir(),
594                                                             param[i].defval + 1,
595                                                             NULL);
596                                         tmpparam.data = &str_data;
597                                         break;
598                                 }
599                         }
600                         tmpparam.data = &tmpparam.defval;
601                         break;
602                 case P_INT:
603                         if (tmpparam.defval)
604                                 int_data = atoi(tmpparam.defval);
605                         else
606                                 int_data = 0;
607                         tmpparam.data = &int_data;
608                         break;
609                 case P_USHORT:
610                         if (tmpparam.defval)
611                                 ushort_data = atoi(tmpparam.defval);
612                         else
613                                 ushort_data = 0;
614                         tmpparam.data = &ushort_data;
615                         break;
616                 case P_BOOL:
617                         if (tmpparam.defval) {
618                                 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
619                                         bool_data = TRUE;
620                                 else
621                                         bool_data = atoi(tmpparam.defval)
622                                                 ? TRUE : FALSE;
623                         } else
624                                 bool_data = FALSE;
625                         tmpparam.data = &bool_data;
626                         break;
627                 case P_ENUM:
628                         if (tmpparam.defval)
629                                 enum_data = (DummyEnum)atoi(tmpparam.defval);
630                         else
631                                 enum_data = 0;
632                         tmpparam.data = &enum_data;
633                         break;
634                 case P_OTHER:
635                 default:
636                         break;
637                 }
638                 tmpparam.widget_set_func(&tmpparam);
639                 g_free(str_data);
640                 str_data = NULL;
641         }
642 }
643
644 void prefs_set_data_from_entry(PrefParam *pparam)
645 {
646         gchar **str;
647         const gchar *entry_str;
648
649         g_return_if_fail(*pparam->widget != NULL);
650
651         entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
652
653         switch (pparam->type) {
654         case P_STRING:
655         case P_PASSWORD:
656                 str = (gchar **)pparam->data;
657                 g_free(*str);
658                 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
659                 break;
660         case P_USHORT:
661                 *((gushort *)pparam->data) = atoi(entry_str);
662                 break;
663         case P_INT:
664                 *((gint *)pparam->data) = atoi(entry_str);
665                 break;
666         default:
667                 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
668                           pparam->type);
669         }
670 }
671
672 void prefs_set_entry(PrefParam *pparam)
673 {
674         gchar **str;
675
676         g_return_if_fail(*pparam->widget != NULL);
677
678         switch (pparam->type) {
679         case P_STRING:
680         case P_PASSWORD:
681                 str = (gchar **)pparam->data;
682                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
683                                    *str ? *str : "");
684                 break;
685         case P_INT:
686                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
687                                    itos(*((gint *)pparam->data)));
688                 break;
689         case P_USHORT:
690                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
691                                    itos(*((gushort *)pparam->data)));
692                 break;
693         default:
694                 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
695                           pparam->type);
696         }
697 }
698
699 void prefs_set_data_from_text(PrefParam *pparam)
700 {
701         gchar **str;
702         gchar *text = NULL, *tp = NULL;
703         gchar *tmp, *tmpp;
704
705         g_return_if_fail(*pparam->widget != NULL);
706
707         switch (pparam->type) {
708         case P_STRING:
709         case P_PASSWORD:
710                 str = (gchar **)pparam->data;
711                 g_free(*str);
712                 if (GTK_IS_EDITABLE(*pparam->widget)) {   /* need? */
713                         tp = text = gtk_editable_get_chars
714                                         (GTK_EDITABLE(*pparam->widget), 0, -1);
715                 } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
716                         GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
717                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
718                         GtkTextIter start, end;
719                         gtk_text_buffer_get_start_iter(buffer, &start);
720                         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
721                         tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
722                 }
723
724                 g_return_if_fail (tp && text);
725
726                 if (text[0] == '\0') {
727                         *str = NULL;
728                         g_free(text);
729                         break;
730                 }
731
732                 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
733                         { *str = NULL; break; });
734                 while (*tp) {
735                         if (*tp == '\n') {
736                                 *tmpp++ = '\\';
737                                 *tmpp++ = 'n';
738                                 tp++;
739                         } else
740                                 *tmpp++ = *tp++;
741                 }
742                 *tmpp = '\0';
743                 *str = g_strdup(tmp);
744                 g_free(text);
745                 break;
746         default:
747                 g_warning("Invalid PrefType for GtkText widget: %d\n",
748                           pparam->type);
749         }
750 }
751
752 void prefs_set_text(PrefParam *pparam)
753 {
754         gchar *buf, *sp, *bufp;
755         gchar **str;
756         GtkTextView *text;
757         GtkTextBuffer *buffer;
758         GtkTextIter iter;
759
760         g_return_if_fail(*pparam->widget != NULL);
761
762         switch (pparam->type) {
763         case P_STRING:
764         case P_PASSWORD:
765                 str = (gchar **)pparam->data;
766                 if (*str) {
767                         bufp = buf = alloca(strlen(*str) + 1);
768                         if (!buf) buf = "";
769                         else {
770                                 sp = *str;
771                                 while (*sp) {
772                                         if (*sp == '\\' && *(sp + 1) == 'n') {
773                                                 *bufp++ = '\n';
774                                                 sp += 2;
775                                         } else
776                                                 *bufp++ = *sp++;
777                                 }
778                                 *bufp = '\0';
779                         }
780                 } else
781                         buf = "";
782
783                 text = GTK_TEXT_VIEW(*pparam->widget);
784                 buffer = gtk_text_view_get_buffer(text);
785                 gtk_text_buffer_set_text(buffer, "", -1);
786                 gtk_text_buffer_get_start_iter(buffer, &iter);
787                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
788                 break;
789         default:
790                 g_warning("Invalid PrefType for GtkTextView widget: %d\n",
791                           pparam->type);
792         }
793 }
794
795 void prefs_set_data_from_toggle(PrefParam *pparam)
796 {
797         g_return_if_fail(pparam->type == P_BOOL);
798         g_return_if_fail(*pparam->widget != NULL);
799         
800         *((gboolean *)pparam->data) =
801                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
802 }
803
804 void prefs_set_toggle(PrefParam *pparam)
805 {
806         g_return_if_fail(pparam->type == P_BOOL);
807         g_return_if_fail(*pparam->widget != NULL);
808
809         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
810                                      *((gboolean *)pparam->data));
811 }
812
813 void prefs_set_data_from_spinbtn(PrefParam *pparam)
814 {
815         g_return_if_fail(*pparam->widget != NULL);
816
817         switch (pparam->type) {
818         case P_INT:
819                 *((gint *)pparam->data) =
820                         gtk_spin_button_get_value_as_int
821                         (GTK_SPIN_BUTTON(*pparam->widget));
822                 break;
823         case P_USHORT:
824                 *((gushort *)pparam->data) =
825                         (gushort)gtk_spin_button_get_value_as_int
826                         (GTK_SPIN_BUTTON(*pparam->widget));
827                 break;
828         default:
829                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
830                           pparam->type);
831         }
832 }
833
834 void prefs_set_spinbtn(PrefParam *pparam)
835 {
836         g_return_if_fail(*pparam->widget != NULL);
837
838         switch (pparam->type) {
839         case P_INT:
840                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
841                                           (gfloat)*((gint *)pparam->data));
842                 break;
843         case P_USHORT:
844                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
845                                           (gfloat)*((gushort *)pparam->data));
846                 break;
847         default:
848                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
849                           pparam->type);
850         }
851 }
852
853 static GSList *prefs_pages = NULL;
854
855 void prefs_gtk_open(void)
856 {
857         prefswindow_open(_("Preferences"), prefs_pages, NULL,
858                         &prefs_common.prefswin_width, &prefs_common.prefswin_height);
859 }
860
861 void prefs_gtk_register_page(PrefsPage *page)
862 {
863         prefs_pages = g_slist_append(prefs_pages, page);
864 }
865
866 void prefs_gtk_unregister_page(PrefsPage *page)
867 {
868         prefs_pages = g_slist_remove(prefs_pages, page);
869 }