2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
38 #include "prefs_gtk.h"
39 #include "prefs_common.h"
44 #include "file-utils.h"
46 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
47 #define RGB_FROM_GDK_COLOR(c) \
48 ((CL(c.red) << (gulong) 16) | \
49 (CL(c.green) << (gulong) 8) | \
57 static GHashTable *whole_cache = NULL;
59 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
62 static void prefs_config_parse_one_line(PrefParam *param,
65 void prefs_read_config(PrefParam *param, const gchar *label,
66 const gchar *rcfile, const gchar *encoding)
69 gchar buf[PREFSBUFSIZE];
72 cm_return_if_fail(param != NULL);
73 cm_return_if_fail(label != NULL);
74 cm_return_if_fail(rcfile != NULL);
77 g_warning("Encoding is ignored");
79 debug_print("Reading configuration...\n");
81 prefs_set_default(param);
83 if (whole_cache != NULL) {
84 if (prefs_read_config_from_cache(param, label, rcfile) == TRUE)
88 if ((fp = claws_fopen(rcfile, "rb")) == NULL) {
89 if (ENOENT != errno) FILE_OP_ERROR(rcfile, "claws_fopen");
93 block_label = g_strdup_printf("[%s]", label);
95 /* search aiming block */
96 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
102 conv_str = conv_codeset_strdup
103 (buf, encoding, CS_INTERNAL);
105 conv_str = g_strdup(buf);
107 (conv_str, block_label, strlen(block_label));
110 val = strncmp(buf, block_label, strlen(block_label));
112 debug_print("Found %s\n", block_label);
118 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
120 /* reached next block */
121 if (buf[0] == '[') break;
122 if (buf[0] == '#') continue;
127 conv_str = conv_codeset_strdup
128 (buf, encoding, CS_INTERNAL);
130 conv_str = g_strdup(buf);
131 prefs_config_parse_one_line(param, conv_str);
134 prefs_config_parse_one_line(param, buf);
137 debug_print("Finished reading configuration.\n");
141 static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
148 for (i = 0; param[i].name != NULL; i++) {
149 name_len = strlen(param[i].name);
150 if (g_ascii_strncasecmp(buf, param[i].name, name_len))
152 if (buf[name_len] != '=')
154 value = buf + name_len + 1;
155 /* debug_print("%s = %s\n", param[i].name, value); */
157 switch (param[i].type) {
164 if (g_utf8_validate(value, -1, NULL))
165 tmp = g_strdup(value);
167 tmp = conv_codeset_strdup(value,
168 conv_get_locale_charset_str_no_utf8(),
175 g_warning("Failed to convert character set.");
176 tmp = g_strdup(value);
178 g_free(*((gchar **)param[i].data));
179 *((gchar **)param[i].data) = tmp;
183 *((gint *)param[i].data) =
187 *((gboolean *)param[i].data) =
188 (*value == '0' || *value == '\0')
192 *((DummyEnum *)param[i].data) =
193 (DummyEnum)atoi(value);
196 *((gushort *)param[i].data) =
197 (gushort)atoi(value);
200 if (gdk_color_parse(value, &color)) {
201 *((gulong *)param[i].data) = RGB_FROM_GDK_COLOR(color);
204 /* be compatible and accept ints */
205 *((gulong *)param[i].data) = strtoul(value, 0, 10);
216 g_warning("Failed to write configuration to file"); \
217 if (orig_fp) claws_fclose(orig_fp); \
218 prefs_file_close_revert(pfile); \
220 g_free(block_label); \
224 void prefs_write_config(PrefParam *param, const gchar *label,
230 gchar buf[PREFSBUFSIZE];
231 gchar *block_label = NULL;
232 gboolean block_matched = FALSE;
234 cm_return_if_fail(param != NULL);
235 cm_return_if_fail(label != NULL);
236 cm_return_if_fail(rcfile != NULL);
238 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
239 if ((orig_fp = claws_fopen(rcpath, "rb")) == NULL) {
240 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
243 if ((pfile = prefs_write_open(rcpath)) == NULL) {
244 g_warning("Failed to write configuration to file");
245 if (orig_fp) claws_fclose(orig_fp);
250 block_label = g_strdup_printf("[%s]", label);
252 /* search aiming block */
254 while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
257 val = strncmp(buf, block_label, strlen(block_label));
259 debug_print("Found %s\n", block_label);
260 block_matched = TRUE;
263 TRY(claws_fputs(buf, pfile->fp) != EOF);
267 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 gboolean in_dup_block = FALSE;
274 while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
277 TRY(claws_fputc('\n', pfile->fp) != EOF &&
278 claws_fputs(buf, pfile->fp) != EOF);
282 while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
284 if (!strncmp(buf, block_label,
285 strlen(block_label)))
288 in_dup_block = FALSE;
291 TRY(claws_fputs(buf, pfile->fp) != EOF);
298 if (orig_fp) claws_fclose(orig_fp);
299 if (prefs_file_close(pfile) < 0)
300 g_warning("Failed to write configuration to file");
303 debug_print("Configuration is saved.\n");
306 gint prefs_write_param(PrefParam *param, FILE *fp)
309 gchar buf[PREFSBUFSIZE] = "";
311 for (i = 0; param[i].name != NULL; i++) {
312 switch (param[i].type) {
317 if (*((gchar **)param[i].data)) {
318 if (g_utf8_validate(*((gchar **)param[i].data), -1, NULL))
319 tmp = g_strdup(*((gchar **)param[i].data));
321 tmp = conv_codeset_strdup(*((gchar **)param[i].data),
322 conv_get_locale_charset_str_no_utf8(),
325 tmp = g_strdup(*((gchar **)param[i].data));
329 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
336 buf[0] = '\0'; /* Passwords are written to password store. */
339 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
340 *((gint *)param[i].data));
343 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
344 *((gboolean *)param[i].data));
347 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
348 *((DummyEnum *)param[i].data));
351 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
352 *((gushort *)param[i].data));
355 g_snprintf(buf, sizeof buf, "%s=#%6.6lx\n", param[i].name,
356 *((gulong *) param[i].data));
359 /* unrecognized, fail */
360 debug_print("Unrecognized parameter type\n");
364 if (buf[0] != '\0') {
365 if (claws_fputs(buf, fp) == EOF) {
366 perror("claws_fputs");
375 void prefs_set_default(PrefParam *param)
380 cm_return_if_fail(param != NULL);
382 for (i = 0; param[i].name != NULL; i++) {
383 if (!param[i].data) continue;
385 switch (param[i].type) {
387 g_free(*((gchar **)param[i].data));
388 if (param[i].defval != NULL) {
389 if (!strncasecmp(param[i].defval, "ENV_", 4)) {
393 envstr = g_getenv(param[i].defval + 4);
394 tmp = envstr && *envstr ?
395 conv_codeset_strdup(envstr,
396 conv_get_locale_charset_str(),
400 g_warning("Failed to convert character set.");
401 tmp = g_strdup(envstr);
403 *((gchar **)param[i].data) = tmp;
404 } else if (param[i].defval[0] == '~')
405 *((gchar **)param[i].data) =
406 g_strconcat(get_home_dir(),
410 *((gchar **)param[i].data) =
411 g_strdup(param[i].defval);
413 *((gchar **)param[i].data) = NULL;
416 g_free(*((gchar **)param[i].data));
417 if (param[i].defval != NULL) {
418 if (param[i].defval[0] != '\0')
419 *((gchar **)param[i].data) =
420 g_strdup(param[i].defval);
422 *((gchar **)param[i].data) = NULL;
424 *((gchar **)param[i].data) = NULL;
427 if (param[i].defval != NULL)
428 *((gint *)param[i].data) =
429 (gint)atoi(param[i].defval);
430 else if (!strcmp(param[i].name, "config_version"))
431 *((gint *)param[i].data) = CLAWS_CONFIG_VERSION;
433 *((gint *)param[i].data) = 0;
436 if (param[i].defval != NULL) {
437 if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
438 *((gboolean *)param[i].data) = TRUE;
440 *((gboolean *)param[i].data) = atoi(param[i].defval) ? TRUE : FALSE;
442 *((gboolean *)param[i].data) = FALSE;
445 if (param[i].defval != NULL)
446 *((DummyEnum*)param[i].data) =
447 (DummyEnum)atoi(param[i].defval);
449 *((DummyEnum *)param[i].data) = 0;
452 if (param[i].defval != NULL)
453 *((gushort *)param[i].data) = (gushort)atoi(param[i].defval);
455 *((gushort *)param[i].data) = 0;
458 if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
459 *((gulong *)param[i].data) = 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 cm_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_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
496 is_active = gtk_toggle_button_get_active(toggle_btn);
497 gtk_widget_set_sensitive(widget, is_active);
500 void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
504 is_active = gtk_toggle_button_get_active(toggle_btn);
505 gtk_widget_set_sensitive(widget, !is_active);
508 void prefs_set_dialog(PrefParam *param)
512 for (i = 0; param[i].name != NULL; i++) {
513 if (param[i].widget_set_func)
514 param[i].widget_set_func(¶m[i]);
518 void prefs_set_data_from_dialog(PrefParam *param)
522 for (i = 0; param[i].name != NULL; i++) {
523 if (param[i].data_set_func)
524 param[i].data_set_func(¶m[i]);
528 void prefs_set_dialog_to_default(PrefParam *param)
532 gchar *str_data = NULL;
538 for (i = 0; param[i].name != NULL; i++) {
539 if (!param[i].widget_set_func) continue;
543 switch (tmpparam.type) {
545 if (tmpparam.defval) {
546 if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
547 str_data = g_strdup(g_getenv(param[i].defval + 4));
548 tmpparam.data = &str_data;
550 } else if (tmpparam.defval[0] == '~') {
552 g_strconcat(get_home_dir(),
555 tmpparam.data = &str_data;
559 tmpparam.data = &tmpparam.defval;
562 tmpparam.data = &tmpparam.defval;
566 int_data = atoi(tmpparam.defval);
569 tmpparam.data = &int_data;
573 ushort_data = atoi(tmpparam.defval);
576 tmpparam.data = &ushort_data;
579 if (tmpparam.defval) {
580 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
583 bool_data = atoi(tmpparam.defval)
587 tmpparam.data = &bool_data;
591 enum_data = (DummyEnum)atoi(tmpparam.defval);
594 tmpparam.data = &enum_data;
600 tmpparam.widget_set_func(&tmpparam);
606 void prefs_set_data_from_entry(PrefParam *pparam)
609 const gchar *entry_str;
611 cm_return_if_fail(*pparam->widget != NULL);
613 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
615 switch (pparam->type) {
617 str = (gchar **)pparam->data;
619 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
622 *((gushort *)pparam->data) = atoi(entry_str);
625 *((gint *)pparam->data) = atoi(entry_str);
628 g_warning("Invalid PrefType for GtkEntry widget: %d",
633 void prefs_set_escaped_data_from_entry(PrefParam *pparam)
637 cm_return_if_fail(*pparam->widget != NULL);
639 switch (pparam->type) {
641 str = (gchar **)pparam->data;
643 *str = pref_get_pref_from_entry(GTK_ENTRY(*pparam->widget));
646 g_warning("Invalid escaped PrefType for GtkEntry widget: %d",
651 void prefs_set_entry(PrefParam *pparam)
654 cm_return_if_fail(*pparam->widget != NULL);
656 switch (pparam->type) {
658 str = (gchar **)pparam->data;
659 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
663 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
664 itos(*((gint *)pparam->data)));
667 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
668 itos(*((gushort *)pparam->data)));
671 g_warning("Invalid PrefType for GtkEntry widget: %d",
676 void prefs_set_entry_from_escaped(PrefParam *pparam)
680 cm_return_if_fail(*pparam->widget != NULL);
682 switch (pparam->type) {
684 str = (gchar **)pparam->data;
685 pref_set_entry_from_pref(GTK_ENTRY(*pparam->widget),
689 g_warning("Invalid escaped PrefType for GtkEntry widget: %d",
694 void prefs_set_data_from_text(PrefParam *pparam)
697 gchar *text = NULL, *tp = NULL;
700 cm_return_if_fail(*pparam->widget != NULL);
702 switch (pparam->type) {
704 str = (gchar **)pparam->data;
706 if (GTK_IS_EDITABLE(*pparam->widget)) { /* need? */
707 tp = text = gtk_editable_get_chars
708 (GTK_EDITABLE(*pparam->widget), 0, -1);
709 } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
710 GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
711 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
712 GtkTextIter start, end;
713 gtk_text_buffer_get_start_iter(buffer, &start);
714 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
715 tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
718 cm_return_if_fail (tp && text);
720 if (text[0] == '\0') {
726 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
727 { *str = NULL; break; });
737 *str = g_strdup(tmp);
741 g_warning("Invalid PrefType for GtkText widget: %d",
746 void prefs_set_escaped_data_from_text(PrefParam *pparam)
750 cm_return_if_fail(*pparam->widget != NULL);
752 switch (pparam->type) {
754 str = (gchar **)pparam->data;
756 *str = pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam->widget));
759 g_warning("Invalid escaped PrefType for GtkText widget: %d",
764 void prefs_set_text(PrefParam *pparam)
766 gchar *buf, *sp, *bufp;
769 GtkTextBuffer *buffer;
772 cm_return_if_fail(*pparam->widget != NULL);
774 switch (pparam->type) {
776 str = (gchar **)pparam->data;
778 bufp = buf = alloca(strlen(*str) + 1);
783 if (*sp == '\\' && *(sp + 1) == 'n') {
794 text = GTK_TEXT_VIEW(*pparam->widget);
795 buffer = gtk_text_view_get_buffer(text);
796 gtk_text_buffer_set_text(buffer, "", -1);
797 gtk_text_buffer_get_start_iter(buffer, &iter);
798 gtk_text_buffer_insert(buffer, &iter, buf, -1);
801 g_warning("Invalid PrefType for GtkTextView widget: %d",
806 void prefs_set_text_from_escaped(PrefParam *pparam)
810 cm_return_if_fail(*pparam->widget != NULL);
812 switch (pparam->type) {
814 str = (gchar **)pparam->data;
815 pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam->widget),
819 g_warning("Invalid escaped PrefType for GtkTextView widget: %d",
824 void prefs_set_data_from_toggle(PrefParam *pparam)
826 cm_return_if_fail(pparam->type == P_BOOL);
827 cm_return_if_fail(*pparam->widget != NULL);
829 *((gboolean *)pparam->data) =
830 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
833 void prefs_set_toggle(PrefParam *pparam)
835 cm_return_if_fail(pparam->type == P_BOOL);
836 cm_return_if_fail(*pparam->widget != NULL);
838 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
839 *((gboolean *)pparam->data));
842 void prefs_set_data_from_spinbtn(PrefParam *pparam)
844 cm_return_if_fail(*pparam->widget != NULL);
846 switch (pparam->type) {
848 *((gint *)pparam->data) =
849 gtk_spin_button_get_value_as_int
850 (GTK_SPIN_BUTTON(*pparam->widget));
853 *((gushort *)pparam->data) =
854 (gushort)gtk_spin_button_get_value_as_int
855 (GTK_SPIN_BUTTON(*pparam->widget));
858 g_warning("Invalid PrefType for GtkSpinButton widget: %d",
863 void prefs_set_spinbtn(PrefParam *pparam)
865 cm_return_if_fail(*pparam->widget != NULL);
867 switch (pparam->type) {
869 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
870 (gfloat)*((gint *)pparam->data));
873 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
874 (gfloat)*((gushort *)pparam->data));
877 g_warning("Invalid PrefType for GtkSpinButton widget: %d",
882 static GSList *prefs_pages = NULL;
884 static void prefs_gtk_window_closed_cb(PrefsWindow *prefswindow)
886 if (prefswindow == NULL)
889 if (prefswindow->dialog_response > PREFSWINDOW_RESPONSE_CANCEL)
890 prefs_common_write_config();
893 void prefs_gtk_open(void)
895 prefswindow_open(_("Preferences"), prefs_pages, NULL,
896 &prefs_common.prefswin_width, &prefs_common.prefswin_height,
897 NULL, prefs_gtk_window_closed_cb, prefs_gtk_window_closed_cb);
900 void prefs_gtk_register_page(PrefsPage *page)
902 prefs_pages = g_slist_append(prefs_pages, page);
905 void prefs_gtk_unregister_page(PrefsPage *page)
907 prefs_pages = g_slist_remove(prefs_pages, page);
910 static void prefs_destroy_whole_cache(gpointer to_free)
912 GHashTable *table = (GHashTable *)to_free;
913 g_hash_table_destroy(table);
916 static void prefs_destroy_file_cache(gpointer to_free)
918 GHashTable *table = (GHashTable *)to_free;
919 g_hash_table_destroy(table);
922 static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
925 gchar buf[PREFSBUFSIZE];
926 GHashTable *section_cache = NULL;
929 fp = claws_fopen(rcfile, "rb");
931 debug_print("cache: %s: %s\n", rcfile?rcfile:"(null)", g_strerror(errno));
935 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
940 continue; /* comment */
941 if (buf[0] == '[') { /* new section */
942 gchar *blockname = g_strdup(buf+1);
944 if (strrchr(blockname, ']'))
945 *strrchr(blockname, ']') = '\0';
947 if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
948 debug_print("new section '%s'\n", blockname);
949 section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
951 g_hash_table_insert(file_cache,
952 blockname, section_cache);
954 debug_print("section '%s' already done\n", blockname);
956 section_cache = NULL;
960 if (!section_cache) {
961 debug_print("skipping stuff %s with no section\n", buf);
966 if (!strchr(buf, '=')) {
967 /* plugins do differently */
970 pref = g_strdup(buf);
972 //debug_print("new pref '%s'\n", pref);
973 g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
981 static int prefs_cache(const gchar *rcfile)
983 GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
984 g_free, prefs_destroy_file_cache);
986 debug_print("new file '%s'\n", rcfile?rcfile:"(null)");
987 g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
989 return prefs_cache_sections(file_cache, rcfile);
992 void prefs_prepare_cache(void)
994 gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
995 gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
996 gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
998 if (whole_cache == NULL) {
999 whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1000 g_free, prefs_destroy_whole_cache);
1002 debug_print("already cached\n");
1004 g_free(folderitemrc);
1008 if (prefs_cache(clawsrc) < 0 ||
1009 prefs_cache(folderitemrc) < 0 ||
1010 prefs_cache(accountrc) < 0)
1011 prefs_destroy_cache();
1014 g_free(folderitemrc);
1018 void prefs_destroy_cache(void)
1021 debug_print("no cache\n");
1024 debug_print("destroying cache\n");
1025 g_hash_table_destroy(whole_cache);
1030 static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
1032 gchar *pref = (gchar *)key;
1034 PrefParam *param = (PrefParam *)user_data;
1036 prefs_config_parse_one_line(param, pref);
1039 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
1040 const gchar *rcfile)
1042 GHashTable *sections_table = NULL;
1043 GHashTable *values_table = NULL;
1044 sections_table = g_hash_table_lookup(whole_cache, rcfile);
1046 if (sections_table == NULL) {
1047 g_warning("Can't find %s in the whole cache", rcfile?rcfile:"(null)");
1050 values_table = g_hash_table_lookup(sections_table, label);
1052 if (values_table == NULL) {
1053 debug_print("no '%s' section in '%s' cache\n", label?label:"(null)", rcfile?rcfile:"(null)");
1056 g_hash_table_foreach(values_table, prefs_parse_cache, param);