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);
81 /* search aiming block */
82 while (fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
88 conv_str = conv_codeset_strdup
89 (buf, encoding, CS_INTERNAL);
91 conv_str = g_strdup(buf);
93 (conv_str, block_label, strlen(block_label));
96 val = strncmp(buf, block_label, strlen(block_label));
98 debug_print("Found %s\n", block_label);
104 while (fgets(buf, sizeof(buf), fp) != NULL) {
106 /* reached next block */
107 if (buf[0] == '[') break;
108 if (buf[0] == '#') continue;
113 conv_str = conv_codeset_strdup
114 (buf, encoding, CS_INTERNAL);
116 conv_str = g_strdup(buf);
117 prefs_config_parse_one_line(param, conv_str);
120 prefs_config_parse_one_line(param, buf);
123 debug_print("Finished reading configuration.\n");
128 void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
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))
139 if (buf[name_len] != '=')
141 value = buf + name_len + 1;
142 /* debug_print("%s = %s\n", param[i].name, value); */
144 switch (param[i].type) {
150 if (g_utf8_validate(value, -1, NULL))
151 tmp = g_strdup(value);
153 tmp = conv_codeset_strdup(value,
154 conv_get_locale_charset_str_no_utf8(),
161 g_warning("failed to convert character set.");
162 tmp = g_strdup(value);
164 g_free(*((gchar **)param[i].data));
165 *((gchar **)param[i].data) = tmp;
169 *((gint *)param[i].data) =
173 *((gboolean *)param[i].data) =
174 (*value == '0' || *value == '\0')
178 *((DummyEnum *)param[i].data) =
179 (DummyEnum)atoi(value);
182 *((gushort *)param[i].data) =
183 (gushort)atoi(value);
186 if (gdk_color_parse(value, &color))
187 *((gulong *)param[i].data) = RGB_FROM_GDK_COLOR(color);
189 /* be compatible and accept ints */
190 *((gulong *)param[i].data) = strtoul(value, 0, 10);
193 g_free(*((gchar **)param[i].data));
194 if (value[0] == '!') {
198 len = base64_decode(tmp, &value[1], strlen(value) - 1);
199 passcrypt_decrypt(tmp, len);
201 *((gchar **)param[i].data) =
202 *tmp ? g_strdup(tmp) : NULL;
204 *((gchar **)param[i].data) =
205 *value ? g_strdup(value) : NULL;
217 g_warning("failed to write configuration to file\n"); \
218 if (orig_fp) fclose(orig_fp); \
219 prefs_file_close_revert(pfile); \
221 g_free(block_label); \
225 void prefs_write_config(PrefParam *param, const gchar *label,
231 gchar buf[PREFSBUFSIZE];
232 gchar *block_label = NULL;
233 gboolean block_matched = FALSE;
235 g_return_if_fail(param != NULL);
236 g_return_if_fail(label != NULL);
237 g_return_if_fail(rcfile != NULL);
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");
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);
251 block_label = g_strdup_printf("[%s]", label);
253 /* search aiming block */
255 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
258 val = strncmp(buf, block_label, strlen(block_label));
260 debug_print("Found %s\n", block_label);
261 block_matched = TRUE;
264 TRY(fputs(buf, pfile->fp) != EOF);
268 TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
272 /* write all param data to file */
273 TRY(prefs_write_param(param, pfile->fp) == 0);
276 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
279 TRY(fputc('\n', pfile->fp) != EOF &&
280 fputs(buf, pfile->fp) != EOF);
284 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
285 TRY(fputs(buf, pfile->fp) != EOF);
288 if (orig_fp) fclose(orig_fp);
289 if (prefs_file_close(pfile) < 0)
290 g_warning("failed to write configuration to file\n");
293 debug_print("Configuration is saved.\n");
296 gint prefs_write_param(PrefParam *param, FILE *fp)
299 gchar buf[PREFSBUFSIZE];
301 for (i = 0; param[i].name != NULL; i++) {
302 switch (param[i].type) {
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));
311 tmp = conv_codeset_strdup(*((gchar **)param[i].data),
312 conv_get_locale_charset_str_no_utf8(),
315 tmp = g_strdup(*((gchar **)param[i].data));
319 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
326 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
327 *((gint *)param[i].data));
330 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
331 *((gboolean *)param[i].data));
334 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
335 *((DummyEnum *)param[i].data));
338 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
339 *((gushort *)param[i].data));
342 g_snprintf(buf, sizeof buf, "%s=#%6.6lx\n", param[i].name,
343 *((gulong *) param[i].data));
347 gchar *tmp = NULL, tmp2[1024] = {0};
349 tmp = *((gchar **)param[i].data);
355 passcrypt_encrypt(tmp, len);
356 base64_encode(tmp2, tmp, len);
360 g_snprintf(buf, sizeof(buf), "%s=!%s\n", param[i].name,
366 /* unrecognized, fail */
367 debug_print("Unrecognized parameter type\n");
371 if (buf[0] != '\0') {
372 if (fputs(buf, fp) == EOF) {
382 void prefs_set_default(PrefParam *param)
387 g_return_if_fail(param != NULL);
389 for (i = 0; param[i].name != NULL; i++) {
390 if (!param[i].data) continue;
392 switch (param[i].type) {
395 g_free(*((gchar **)param[i].data));
396 if (param[i].defval != NULL) {
397 if (!strncasecmp(param[i].defval, "ENV_", 4)) {
401 envstr = g_getenv(param[i].defval + 4);
402 tmp = envstr && *envstr ?
403 conv_codeset_strdup(envstr,
404 conv_get_locale_charset_str(),
408 g_warning("faild to convert character set.");
409 tmp = g_strdup(envstr);
411 *((gchar **)param[i].data) = tmp;
412 } else if (param[i].defval[0] == '~')
413 *((gchar **)param[i].data) =
414 g_strconcat(get_home_dir(),
417 else if (param[i].defval[0] != '\0')
418 *((gchar **)param[i].data) =
419 g_strdup(param[i].defval);
421 *((gchar **)param[i].data) = NULL;
423 *((gchar **)param[i].data) = NULL;
426 if (param[i].defval != NULL)
427 *((gint *)param[i].data) =
428 (gint)atoi(param[i].defval);
430 *((gint *)param[i].data) = 0;
433 if (param[i].defval != NULL) {
434 if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
435 *((gboolean *)param[i].data) = TRUE;
437 *((gboolean *)param[i].data) =
438 atoi(param[i].defval) ? TRUE : FALSE;
440 *((gboolean *)param[i].data) = FALSE;
443 if (param[i].defval != NULL)
444 *((DummyEnum*)param[i].data) =
445 (DummyEnum)atoi(param[i].defval);
447 *((DummyEnum *)param[i].data) = 0;
450 if (param[i].defval != NULL)
451 *((gushort *)param[i].data) =
452 (gushort)atoi(param[i].defval);
454 *((gushort *)param[i].data) = 0;
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);
464 *((gulong *)param[i].data) = 0;
472 void prefs_free(PrefParam *param)
476 g_return_if_fail(param != NULL);
478 for (i = 0; param[i].name != NULL; i++) {
479 if (!param[i].data) continue;
481 switch (param[i].type) {
484 g_free(*((gchar **)param[i].data));
492 void prefs_dialog_create(PrefsDialog *dialog)
498 GtkWidget *confirm_area;
500 GtkWidget *cancel_btn;
501 GtkWidget *apply_btn;
503 g_return_if_fail(dialog != NULL);
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);
511 vbox = gtk_vbox_new (FALSE, 6);
512 gtk_widget_show(vbox);
513 gtk_container_add (GTK_CONTAINER (window), vbox);
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);
522 gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
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);
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;
539 void prefs_dialog_destroy(PrefsDialog *dialog)
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;
549 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
553 is_active = gtk_toggle_button_get_active(toggle_btn);
554 gtk_widget_set_sensitive(widget, is_active);
557 void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
561 is_active = gtk_toggle_button_get_active(toggle_btn);
562 gtk_widget_set_sensitive(widget, !is_active);
565 void prefs_set_dialog(PrefParam *param)
569 for (i = 0; param[i].name != NULL; i++) {
570 if (param[i].widget_set_func)
571 param[i].widget_set_func(¶m[i]);
575 void prefs_set_data_from_dialog(PrefParam *param)
579 for (i = 0; param[i].name != NULL; i++) {
580 if (param[i].data_set_func)
581 param[i].data_set_func(¶m[i]);
585 void prefs_set_dialog_to_default(PrefParam *param)
589 gchar *str_data = NULL;
595 for (i = 0; param[i].name != NULL; i++) {
596 if (!param[i].widget_set_func) continue;
600 switch (tmpparam.type) {
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;
608 } else if (tmpparam.defval[0] == '~') {
610 g_strconcat(get_home_dir(),
613 tmpparam.data = &str_data;
617 tmpparam.data = &tmpparam.defval;
621 int_data = atoi(tmpparam.defval);
624 tmpparam.data = &int_data;
628 ushort_data = atoi(tmpparam.defval);
631 tmpparam.data = &ushort_data;
634 if (tmpparam.defval) {
635 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
638 bool_data = atoi(tmpparam.defval)
642 tmpparam.data = &bool_data;
646 enum_data = (DummyEnum)atoi(tmpparam.defval);
649 tmpparam.data = &enum_data;
655 tmpparam.widget_set_func(&tmpparam);
661 void prefs_set_data_from_entry(PrefParam *pparam)
664 const gchar *entry_str;
666 g_return_if_fail(*pparam->widget != NULL);
668 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
670 switch (pparam->type) {
673 str = (gchar **)pparam->data;
675 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
678 *((gushort *)pparam->data) = atoi(entry_str);
681 *((gint *)pparam->data) = atoi(entry_str);
684 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
689 void prefs_set_entry(PrefParam *pparam)
693 g_return_if_fail(*pparam->widget != NULL);
695 switch (pparam->type) {
698 str = (gchar **)pparam->data;
699 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
703 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
704 itos(*((gint *)pparam->data)));
707 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
708 itos(*((gushort *)pparam->data)));
711 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
716 void prefs_set_data_from_text(PrefParam *pparam)
719 gchar *text = NULL, *tp = NULL;
722 g_return_if_fail(*pparam->widget != NULL);
724 switch (pparam->type) {
727 str = (gchar **)pparam->data;
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);
741 g_return_if_fail (tp && text);
743 if (text[0] == '\0') {
749 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
750 { *str = NULL; break; });
760 *str = g_strdup(tmp);
764 g_warning("Invalid PrefType for GtkText widget: %d\n",
769 void prefs_set_text(PrefParam *pparam)
771 gchar *buf, *sp, *bufp;
774 GtkTextBuffer *buffer;
777 g_return_if_fail(*pparam->widget != NULL);
779 switch (pparam->type) {
782 str = (gchar **)pparam->data;
784 bufp = buf = alloca(strlen(*str) + 1);
789 if (*sp == '\\' && *(sp + 1) == 'n') {
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);
807 g_warning("Invalid PrefType for GtkTextView widget: %d\n",
812 void prefs_set_data_from_toggle(PrefParam *pparam)
814 g_return_if_fail(pparam->type == P_BOOL);
815 g_return_if_fail(*pparam->widget != NULL);
817 *((gboolean *)pparam->data) =
818 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
821 void prefs_set_toggle(PrefParam *pparam)
823 g_return_if_fail(pparam->type == P_BOOL);
824 g_return_if_fail(*pparam->widget != NULL);
826 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
827 *((gboolean *)pparam->data));
830 void prefs_set_data_from_spinbtn(PrefParam *pparam)
832 g_return_if_fail(*pparam->widget != NULL);
834 switch (pparam->type) {
836 *((gint *)pparam->data) =
837 gtk_spin_button_get_value_as_int
838 (GTK_SPIN_BUTTON(*pparam->widget));
841 *((gushort *)pparam->data) =
842 (gushort)gtk_spin_button_get_value_as_int
843 (GTK_SPIN_BUTTON(*pparam->widget));
846 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
851 void prefs_set_spinbtn(PrefParam *pparam)
853 g_return_if_fail(*pparam->widget != NULL);
855 switch (pparam->type) {
857 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
858 (gfloat)*((gint *)pparam->data));
861 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
862 (gfloat)*((gushort *)pparam->data));
865 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
870 static GSList *prefs_pages = NULL;
872 void prefs_gtk_open(void)
874 prefswindow_open(_("Preferences"), prefs_pages, NULL,
875 &prefs_common.prefswin_width, &prefs_common.prefswin_height);
878 void prefs_gtk_register_page(PrefsPage *page)
880 prefs_pages = g_slist_append(prefs_pages, page);
883 void prefs_gtk_unregister_page(PrefsPage *page)
885 prefs_pages = g_slist_remove(prefs_pages, page);