2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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.
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.
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.
25 #include <glib/gi18n.h>
35 #include "prefs_gtk.h"
36 #include "prefs_common.h"
39 #include "passcrypt.h"
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) | \
54 void prefs_read_config(PrefParam *param, const gchar *label,
55 const gchar *rcfile, const gchar *encoding)
58 gchar buf[PREFSBUFSIZE];
61 g_return_if_fail(param != NULL);
62 g_return_if_fail(label != NULL);
63 g_return_if_fail(rcfile != NULL);
66 g_warning("Encoding is ignored\n");
68 debug_print("Reading configuration...\n");
70 prefs_set_default(param);
72 if ((fp = g_fopen(rcfile, "rb")) == NULL) {
73 if (ENOENT != errno) FILE_OP_ERROR(rcfile, "fopen");
77 block_label = g_strdup_printf("[%s]", label);
79 /* search aiming block */
80 while (fgets(buf, sizeof(buf), fp) != NULL) {
86 conv_str = conv_codeset_strdup
87 (buf, encoding, CS_INTERNAL);
89 conv_str = g_strdup(buf);
91 (conv_str, block_label, strlen(block_label));
94 val = strncmp(buf, block_label, strlen(block_label));
96 debug_print("Found %s\n", block_label);
102 while (fgets(buf, sizeof(buf), fp) != NULL) {
104 /* reached next block */
105 if (buf[0] == '[') break;
106 if (buf[0] == '#') continue;
111 conv_str = conv_codeset_strdup
112 (buf, encoding, CS_INTERNAL);
114 conv_str = g_strdup(buf);
115 prefs_config_parse_one_line(param, conv_str);
118 prefs_config_parse_one_line(param, buf);
121 debug_print("Finished reading configuration.\n");
125 void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
132 for (i = 0; param[i].name != NULL; i++) {
133 name_len = strlen(param[i].name);
134 if (g_ascii_strncasecmp(buf, param[i].name, name_len))
136 if (buf[name_len] != '=')
138 value = buf + name_len + 1;
139 /* debug_print("%s = %s\n", param[i].name, value); */
141 switch (param[i].type) {
147 if (g_utf8_validate(value, -1, NULL))
148 tmp = g_strdup(value);
150 tmp = conv_codeset_strdup(value,
151 conv_get_locale_charset_str_no_utf8(),
158 g_warning("failed to convert character set.");
159 tmp = g_strdup(value);
161 g_free(*((gchar **)param[i].data));
162 *((gchar **)param[i].data) = tmp;
166 *((gint *)param[i].data) =
170 *((gboolean *)param[i].data) =
171 (*value == '0' || *value == '\0')
175 *((DummyEnum *)param[i].data) =
176 (DummyEnum)atoi(value);
179 *((gushort *)param[i].data) =
180 (gushort)atoi(value);
183 if (gdk_color_parse(value, &color))
184 *((gulong *)param[i].data) = RGB_FROM_GDK_COLOR(color);
186 /* be compatible and accept ints */
187 *((gulong *)param[i].data) = strtoul(value, 0, 10);
190 g_free(*((gchar **)param[i].data));
191 if (value[0] == '!') {
195 len = base64_decode(tmp, &value[1], strlen(value) - 1);
196 passcrypt_decrypt(tmp, len);
198 *((gchar **)param[i].data) =
199 *tmp ? g_strdup(tmp) : NULL;
201 *((gchar **)param[i].data) =
202 *value ? g_strdup(value) : NULL;
214 g_warning("failed to write configuration to file\n"); \
215 if (orig_fp) fclose(orig_fp); \
216 prefs_file_close_revert(pfile); \
218 g_free(block_label); \
222 void prefs_write_config(PrefParam *param, const gchar *label,
228 gchar buf[PREFSBUFSIZE];
229 gchar *block_label = NULL;
230 gboolean block_matched = FALSE;
232 g_return_if_fail(param != NULL);
233 g_return_if_fail(label != NULL);
234 g_return_if_fail(rcfile != NULL);
236 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
237 if ((orig_fp = g_fopen(rcpath, "rb")) == NULL) {
238 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
241 if ((pfile = prefs_write_open(rcpath)) == NULL) {
242 g_warning("failed to write configuration to file\n");
243 if (orig_fp) fclose(orig_fp);
248 block_label = g_strdup_printf("[%s]", label);
250 /* search aiming block */
252 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
255 val = strncmp(buf, block_label, strlen(block_label));
257 debug_print("Found %s\n", block_label);
258 block_matched = TRUE;
261 TRY(fputs(buf, pfile->fp) != EOF);
265 TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
269 /* write all param data to file */
270 TRY(prefs_write_param(param, pfile->fp) == 0);
273 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
276 TRY(fputc('\n', pfile->fp) != EOF &&
277 fputs(buf, pfile->fp) != EOF);
281 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
282 TRY(fputs(buf, pfile->fp) != EOF);
285 if (orig_fp) fclose(orig_fp);
286 if (prefs_file_close(pfile) < 0)
287 g_warning("failed to write configuration to file\n");
290 debug_print("Configuration is saved.\n");
293 gint prefs_write_param(PrefParam *param, FILE *fp)
296 gchar buf[PREFSBUFSIZE];
298 for (i = 0; param[i].name != NULL; i++) {
299 switch (param[i].type) {
304 if (*((gchar **)param[i].data)) {
305 if (g_utf8_validate(*((gchar **)param[i].data), -1, NULL))
306 tmp = g_strdup(*((gchar **)param[i].data));
308 tmp = conv_codeset_strdup(*((gchar **)param[i].data),
309 conv_get_locale_charset_str_no_utf8(),
312 tmp = g_strdup(*((gchar **)param[i].data));
316 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
323 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
324 *((gint *)param[i].data));
327 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
328 *((gboolean *)param[i].data));
331 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
332 *((DummyEnum *)param[i].data));
335 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
336 *((gushort *)param[i].data));
339 g_snprintf(buf, sizeof buf, "%s=#%6.6lx\n", param[i].name,
340 *((gulong *) param[i].data));
344 gchar *tmp = NULL, tmp2[1024] = {0};
346 tmp = *((gchar **)param[i].data);
352 passcrypt_encrypt(tmp, len);
353 base64_encode(tmp2, tmp, len);
357 g_snprintf(buf, sizeof(buf), "%s=!%s\n", param[i].name,
363 /* unrecognized, fail */
364 debug_print("Unrecognized parameter type\n");
368 if (buf[0] != '\0') {
369 if (fputs(buf, fp) == EOF) {
379 void prefs_set_default(PrefParam *param)
384 g_return_if_fail(param != NULL);
386 for (i = 0; param[i].name != NULL; i++) {
387 if (!param[i].data) continue;
389 switch (param[i].type) {
392 g_free(*((gchar **)param[i].data));
393 if (param[i].defval != NULL) {
394 if (!strncasecmp(param[i].defval, "ENV_", 4)) {
398 envstr = g_getenv(param[i].defval + 4);
399 tmp = envstr && *envstr ?
400 conv_codeset_strdup(envstr,
401 conv_get_locale_charset_str(),
405 g_warning("faild to convert character set.");
406 tmp = g_strdup(envstr);
408 *((gchar **)param[i].data) = tmp;
409 } else if (param[i].defval[0] == '~')
410 *((gchar **)param[i].data) =
411 g_strconcat(get_home_dir(),
414 else if (param[i].defval[0] != '\0')
415 *((gchar **)param[i].data) =
416 g_strdup(param[i].defval);
418 *((gchar **)param[i].data) = NULL;
420 *((gchar **)param[i].data) = NULL;
423 if (param[i].defval != NULL)
424 *((gint *)param[i].data) =
425 (gint)atoi(param[i].defval);
427 *((gint *)param[i].data) = 0;
430 if (param[i].defval != NULL) {
431 if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
432 *((gboolean *)param[i].data) = TRUE;
434 *((gboolean *)param[i].data) =
435 atoi(param[i].defval) ? TRUE : FALSE;
437 *((gboolean *)param[i].data) = FALSE;
440 if (param[i].defval != NULL)
441 *((DummyEnum*)param[i].data) =
442 (DummyEnum)atoi(param[i].defval);
444 *((DummyEnum *)param[i].data) = 0;
447 if (param[i].defval != NULL)
448 *((gushort *)param[i].data) =
449 (gushort)atoi(param[i].defval);
451 *((gushort *)param[i].data) = 0;
454 if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
455 *((gulong *)param[i].data) =
456 RGB_FROM_GDK_COLOR(color);
457 else if (param[i].defval)
458 /* be compatible and accept ints */
459 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10);
461 *((gulong *)param[i].data) = 0;
469 void prefs_free(PrefParam *param)
473 g_return_if_fail(param != NULL);
475 for (i = 0; param[i].name != NULL; i++) {
476 if (!param[i].data) continue;
478 switch (param[i].type) {
481 g_free(*((gchar **)param[i].data));
489 void prefs_dialog_create(PrefsDialog *dialog)
495 GtkWidget *confirm_area;
497 GtkWidget *cancel_btn;
498 GtkWidget *apply_btn;
500 g_return_if_fail(dialog != NULL);
502 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
503 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
504 gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
505 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
506 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
508 vbox = gtk_vbox_new (FALSE, 6);
509 gtk_widget_show(vbox);
510 gtk_container_add (GTK_CONTAINER (window), vbox);
512 notebook = gtk_notebook_new ();
513 gtk_widget_show(notebook);
514 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
515 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
516 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
517 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
519 gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
521 gtkut_stock_button_set_create(&confirm_area,
522 &ok_btn, GTK_STOCK_OK,
523 &cancel_btn, GTK_STOCK_CANCEL,
524 &apply_btn, GTK_STOCK_APPLY);
525 gtk_widget_show(confirm_area);
526 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
527 gtk_widget_grab_default(ok_btn);
529 dialog->window = window;
530 dialog->notebook = notebook;
531 dialog->ok_btn = ok_btn;
532 dialog->cancel_btn = cancel_btn;
533 dialog->apply_btn = apply_btn;
536 void prefs_dialog_destroy(PrefsDialog *dialog)
538 gtk_widget_destroy(dialog->window);
539 dialog->window = NULL;
540 dialog->notebook = NULL;
541 dialog->ok_btn = NULL;
542 dialog->cancel_btn = NULL;
543 dialog->apply_btn = NULL;
546 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
550 is_active = gtk_toggle_button_get_active(toggle_btn);
551 gtk_widget_set_sensitive(widget, is_active);
554 void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
558 is_active = gtk_toggle_button_get_active(toggle_btn);
559 gtk_widget_set_sensitive(widget, !is_active);
562 void prefs_set_dialog(PrefParam *param)
566 for (i = 0; param[i].name != NULL; i++) {
567 if (param[i].widget_set_func)
568 param[i].widget_set_func(¶m[i]);
572 void prefs_set_data_from_dialog(PrefParam *param)
576 for (i = 0; param[i].name != NULL; i++) {
577 if (param[i].data_set_func)
578 param[i].data_set_func(¶m[i]);
582 void prefs_set_dialog_to_default(PrefParam *param)
586 gchar *str_data = NULL;
592 for (i = 0; param[i].name != NULL; i++) {
593 if (!param[i].widget_set_func) continue;
597 switch (tmpparam.type) {
600 if (tmpparam.defval) {
601 if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
602 str_data = g_strdup(g_getenv(param[i].defval + 4));
603 tmpparam.data = &str_data;
605 } else if (tmpparam.defval[0] == '~') {
607 g_strconcat(get_home_dir(),
610 tmpparam.data = &str_data;
614 tmpparam.data = &tmpparam.defval;
618 int_data = atoi(tmpparam.defval);
621 tmpparam.data = &int_data;
625 ushort_data = atoi(tmpparam.defval);
628 tmpparam.data = &ushort_data;
631 if (tmpparam.defval) {
632 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
635 bool_data = atoi(tmpparam.defval)
639 tmpparam.data = &bool_data;
643 enum_data = (DummyEnum)atoi(tmpparam.defval);
646 tmpparam.data = &enum_data;
652 tmpparam.widget_set_func(&tmpparam);
658 void prefs_set_data_from_entry(PrefParam *pparam)
661 const gchar *entry_str;
663 g_return_if_fail(*pparam->widget != NULL);
665 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
667 switch (pparam->type) {
670 str = (gchar **)pparam->data;
672 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
675 *((gushort *)pparam->data) = atoi(entry_str);
678 *((gint *)pparam->data) = atoi(entry_str);
681 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
686 void prefs_set_entry(PrefParam *pparam)
690 g_return_if_fail(*pparam->widget != NULL);
692 switch (pparam->type) {
695 str = (gchar **)pparam->data;
696 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
700 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
701 itos(*((gint *)pparam->data)));
704 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
705 itos(*((gushort *)pparam->data)));
708 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
713 void prefs_set_data_from_text(PrefParam *pparam)
716 gchar *text = NULL, *tp = NULL;
719 g_return_if_fail(*pparam->widget != NULL);
721 switch (pparam->type) {
724 str = (gchar **)pparam->data;
726 if (GTK_IS_EDITABLE(*pparam->widget)) { /* need? */
727 tp = text = gtk_editable_get_chars
728 (GTK_EDITABLE(*pparam->widget), 0, -1);
729 } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
730 GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
731 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
732 GtkTextIter start, end;
733 gtk_text_buffer_get_start_iter(buffer, &start);
734 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
735 tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
738 g_return_if_fail (tp && text);
740 if (text[0] == '\0') {
746 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
747 { *str = NULL; break; });
757 *str = g_strdup(tmp);
761 g_warning("Invalid PrefType for GtkText widget: %d\n",
766 void prefs_set_text(PrefParam *pparam)
768 gchar *buf, *sp, *bufp;
771 GtkTextBuffer *buffer;
774 g_return_if_fail(*pparam->widget != NULL);
776 switch (pparam->type) {
779 str = (gchar **)pparam->data;
781 bufp = buf = alloca(strlen(*str) + 1);
786 if (*sp == '\\' && *(sp + 1) == 'n') {
797 text = GTK_TEXT_VIEW(*pparam->widget);
798 buffer = gtk_text_view_get_buffer(text);
799 gtk_text_buffer_set_text(buffer, "", -1);
800 gtk_text_buffer_get_start_iter(buffer, &iter);
801 gtk_text_buffer_insert(buffer, &iter, buf, -1);
804 g_warning("Invalid PrefType for GtkTextView widget: %d\n",
809 void prefs_set_data_from_toggle(PrefParam *pparam)
811 g_return_if_fail(pparam->type == P_BOOL);
812 g_return_if_fail(*pparam->widget != NULL);
814 *((gboolean *)pparam->data) =
815 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
818 void prefs_set_toggle(PrefParam *pparam)
820 g_return_if_fail(pparam->type == P_BOOL);
821 g_return_if_fail(*pparam->widget != NULL);
823 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
824 *((gboolean *)pparam->data));
827 void prefs_set_data_from_spinbtn(PrefParam *pparam)
829 g_return_if_fail(*pparam->widget != NULL);
831 switch (pparam->type) {
833 *((gint *)pparam->data) =
834 gtk_spin_button_get_value_as_int
835 (GTK_SPIN_BUTTON(*pparam->widget));
838 *((gushort *)pparam->data) =
839 (gushort)gtk_spin_button_get_value_as_int
840 (GTK_SPIN_BUTTON(*pparam->widget));
843 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
848 void prefs_set_spinbtn(PrefParam *pparam)
850 g_return_if_fail(*pparam->widget != NULL);
852 switch (pparam->type) {
854 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
855 (gfloat)*((gint *)pparam->data));
858 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
859 (gfloat)*((gushort *)pparam->data));
862 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
867 static GSList *prefs_pages = NULL;
869 void prefs_gtk_open(void)
871 prefswindow_open(_("Preferences"), prefs_pages, NULL,
872 &prefs_common.prefswin_width, &prefs_common.prefswin_height);
875 void prefs_gtk_register_page(PrefsPage *page)
877 prefs_pages = g_slist_append(prefs_pages, page);
880 void prefs_gtk_unregister_page(PrefsPage *page)
882 prefs_pages = g_slist_remove(prefs_pages, page);