/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
-#include <gtk/gtk.h>
+#define _GNU_SOURCE
#include <stdio.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
-#include "intl.h"
+#include "defs.h"
#include "main.h"
#include "prefs.h"
#include "prefs_gtk.h"
+#include "prefs_common.h"
#include "utils.h"
#include "gtkutils.h"
+#include "passcrypt.h"
+#include "base64.h"
+#include "codeconv.h"
+
+#define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
+#define RGB_FROM_GDK_COLOR(c) \
+ ((CL(c.red) << (gulong) 16) | \
+ (CL(c.green) << (gulong) 8) | \
+ (CL(c.blue)))
+
+#ifdef HAVE_FGETS_UNLOCKED
+#define SC_FGETS fgets_unlocked
+#else
+#define SC_FGETS fgets
+#endif
typedef enum
{
DUMMY_PARAM
} DummyEnum;
+static GHashTable *whole_cache = NULL;
+
+static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
+ const gchar *rcfile);
+
void prefs_read_config(PrefParam *param, const gchar *label,
- const gchar *rcfile)
+ const gchar *rcfile, const gchar *encoding)
{
FILE *fp;
gchar buf[PREFSBUFSIZE];
- gchar *rcpath;
gchar *block_label;
g_return_if_fail(param != NULL);
g_return_if_fail(label != NULL);
g_return_if_fail(rcfile != NULL);
+ if (encoding != NULL)
+ g_warning("Encoding is ignored\n");
+
debug_print("Reading configuration...\n");
prefs_set_default(param);
- rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
- if ((fp = fopen(rcpath, "rb")) == NULL) {
- if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
- g_free(rcpath);
+ if (whole_cache != NULL) {
+ if (prefs_read_config_from_cache(param, label, rcfile) == TRUE)
+ return;
+ }
+
+ if ((fp = g_fopen(rcfile, "rb")) == NULL) {
+ if (ENOENT != errno) FILE_OP_ERROR(rcfile, "fopen");
return;
}
- g_free(rcpath);
block_label = g_strdup_printf("[%s]", label);
+#ifdef HAVE_FGETS_UNLOCKED
+ flockfile(fp);
+#endif
+
/* search aiming block */
- while (fgets(buf, sizeof(buf), fp) != NULL) {
+ while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
gint val;
- val = strncmp(buf, block_label, strlen(block_label));
+ if (encoding) {
+ gchar *conv_str;
+
+ conv_str = conv_codeset_strdup
+ (buf, encoding, CS_INTERNAL);
+ if (!conv_str)
+ conv_str = g_strdup(buf);
+ val = strncmp
+ (conv_str, block_label, strlen(block_label));
+ g_free(conv_str);
+ } else
+ val = strncmp(buf, block_label, strlen(block_label));
if (val == 0) {
debug_print("Found %s\n", block_label);
break;
}
g_free(block_label);
- while (fgets(buf, sizeof(buf), fp) != NULL) {
+ while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
strretchomp(buf);
/* reached next block */
if (buf[0] == '[') break;
+ if (buf[0] == '#') continue;
- prefs_config_parse_one_line(param, buf);
+ if (encoding) {
+ gchar *conv_str;
+
+ conv_str = conv_codeset_strdup
+ (buf, encoding, CS_INTERNAL);
+ if (!conv_str)
+ conv_str = g_strdup(buf);
+ prefs_config_parse_one_line(param, conv_str);
+ g_free(conv_str);
+ } else
+ prefs_config_parse_one_line(param, buf);
}
debug_print("Finished reading configuration.\n");
+#ifdef HAVE_FGETS_UNLOCKED
+ funlockfile(fp);
+#endif
fclose(fp);
}
gint i;
gint name_len;
const gchar *value;
+ GdkColor color;
for (i = 0; param[i].name != NULL; i++) {
name_len = strlen(param[i].name);
- if (strncasecmp(buf, param[i].name, name_len))
+ if (g_ascii_strncasecmp(buf, param[i].name, name_len))
continue;
if (buf[name_len] != '=')
continue;
switch (param[i].type) {
case P_STRING:
+ {
+ gchar *tmp = NULL;
+
+ if (*value) {
+ if (g_utf8_validate(value, -1, NULL))
+ tmp = g_strdup(value);
+ else {
+ tmp = conv_codeset_strdup(value,
+ conv_get_locale_charset_str_no_utf8(),
+ CS_INTERNAL);
+ }
+ } else {
+ tmp = g_strdup("");
+ }
+ if (!tmp) {
+ g_warning("failed to convert character set.");
+ tmp = g_strdup(value);
+ }
g_free(*((gchar **)param[i].data));
- *((gchar **)param[i].data) =
- *value ? g_strdup(value) : NULL;
+ *((gchar **)param[i].data) = tmp;
break;
+ }
case P_INT:
*((gint *)param[i].data) =
(gint)atoi(value);
*((gushort *)param[i].data) =
(gushort)atoi(value);
break;
+ case P_COLOR:
+ if (gdk_color_parse(value, &color))
+ *((gulong *)param[i].data) = RGB_FROM_GDK_COLOR(color);
+ else
+ /* be compatible and accept ints */
+ *((gulong *)param[i].data) = strtoul(value, 0, 10);
+ break;
+ case P_PASSWORD:
+ g_free(*((gchar **)param[i].data));
+ if (value[0] == '!') {
+ gchar tmp[1024];
+ gint len;
+
+ len = base64_decode(tmp, &value[1], strlen(value) - 1);
+ passcrypt_decrypt(tmp, len);
+ tmp[len] = '\0';
+ *((gchar **)param[i].data) =
+ *tmp ? g_strdup(tmp) : NULL;
+ } else {
+ *((gchar **)param[i].data) =
+ *value ? g_strdup(value) : NULL;
+ }
+ break;
default:
break;
}
return; \
} \
-void prefs_save_config(PrefParam *param, const gchar *label,
- const gchar *rcfile)
+void prefs_write_config(PrefParam *param, const gchar *label,
+ const gchar *rcfile)
{
FILE *orig_fp;
PrefFile *pfile;
g_return_if_fail(rcfile != NULL);
rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
- if ((orig_fp = fopen(rcpath, "rb")) == NULL) {
+ if ((orig_fp = g_fopen(rcpath, "rb")) == NULL) {
if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
}
}
TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
- g_free(block_label);
- block_label = NULL;
/* write all param data to file */
TRY(prefs_write_param(param, pfile->fp) == 0);
if (block_matched) {
+ gboolean in_dup_block = FALSE;
while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
/* next block */
if (buf[0] == '[') {
break;
}
}
- while (fgets(buf, sizeof(buf), orig_fp) != NULL)
- TRY(fputs(buf, pfile->fp) != EOF);
+ while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
+ if (buf[0] == '[') {
+ if (!strncmp(buf, block_label,
+ strlen(block_label)))
+ in_dup_block = TRUE;
+ else
+ in_dup_block = FALSE;
+ }
+ if (!in_dup_block)
+ TRY(fputs(buf, pfile->fp) != EOF);
+ }
}
+ g_free(block_label);
+ block_label = NULL;
+
if (orig_fp) fclose(orig_fp);
if (prefs_file_close(pfile) < 0)
g_warning("failed to write configuration to file\n");
for (i = 0; param[i].name != NULL; i++) {
switch (param[i].type) {
case P_STRING:
+ {
+ gchar *tmp = NULL;
+
+ if (*((gchar **)param[i].data)) {
+ if (g_utf8_validate(*((gchar **)param[i].data), -1, NULL))
+ tmp = g_strdup(*((gchar **)param[i].data));
+ else {
+ tmp = conv_codeset_strdup(*((gchar **)param[i].data),
+ conv_get_locale_charset_str_no_utf8(),
+ CS_INTERNAL);
+ if (!tmp)
+ tmp = g_strdup(*((gchar **)param[i].data));
+ }
+ }
+
g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
- *((gchar **)param[i].data) ?
- *((gchar **)param[i].data) : "");
+ tmp ? tmp : "");
+
+ g_free(tmp);
break;
+ }
case P_INT:
g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
*((gint *)param[i].data));
g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
*((gushort *)param[i].data));
break;
+ case P_COLOR:
+ g_snprintf(buf, sizeof buf, "%s=#%6.6lx\n", param[i].name,
+ *((gulong *) param[i].data));
+ break;
+ case P_PASSWORD:
+ {
+ gchar *tmp = NULL, tmp2[1024] = {0};
+
+ tmp = *((gchar **)param[i].data);
+ if (tmp) {
+ gint len;
+
+ tmp = g_strdup(tmp);
+ len = strlen(tmp);
+ passcrypt_encrypt(tmp, len);
+ base64_encode(tmp2, tmp, len);
+ g_free(tmp);
+ tmp = tmp2;
+ }
+ g_snprintf(buf, sizeof(buf), "%s=!%s\n", param[i].name,
+ tmp ?
+ tmp : "");
+ }
+ break;
default:
- buf[0] = '\0';
+ /* unrecognized, fail */
+ debug_print("Unrecognized parameter type\n");
+ return -1;
}
if (buf[0] != '\0') {
void prefs_set_default(PrefParam *param)
{
gint i;
+ GdkColor color;
g_return_if_fail(param != NULL);
switch (param[i].type) {
case P_STRING:
+ case P_PASSWORD:
+ g_free(*((gchar **)param[i].data));
if (param[i].defval != NULL) {
- if (!strncasecmp(param[i].defval, "ENV_", 4))
- *((gchar **)param[i].data) =
- g_strdup(g_getenv(param[i].defval + 4));
- else if (param[i].defval[0] == '~')
+ if (!strncasecmp(param[i].defval, "ENV_", 4)) {
+ const gchar *envstr;
+ gchar *tmp;
+
+ envstr = g_getenv(param[i].defval + 4);
+ tmp = envstr && *envstr ?
+ conv_codeset_strdup(envstr,
+ conv_get_locale_charset_str(),
+ CS_INTERNAL)
+ : g_strdup("");
+ if (!tmp) {
+ g_warning("faild to convert character set.");
+ tmp = g_strdup(envstr);
+ }
+ *((gchar **)param[i].data) = tmp;
+ } else if (param[i].defval[0] == '~')
*((gchar **)param[i].data) =
g_strconcat(get_home_dir(),
param[i].defval + 1,
break;
case P_BOOL:
if (param[i].defval != NULL) {
- if (!strcasecmp(param[i].defval, "TRUE"))
+ if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
*((gboolean *)param[i].data) = TRUE;
else
*((gboolean *)param[i].data) =
else
*((gushort *)param[i].data) = 0;
break;
+ case P_COLOR:
+ if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
+ *((gulong *)param[i].data) =
+ RGB_FROM_GDK_COLOR(color);
+ else if (param[i].defval)
+ /* be compatible and accept ints */
+ *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10);
+ else
+ *((gulong *)param[i].data) = 0;
+ break;
default:
break;
}
switch (param[i].type) {
case P_STRING:
+ case P_PASSWORD:
g_free(*((gchar **)param[i].data));
break;
default:
g_return_if_fail(dialog != NULL);
- window = gtk_window_new (GTK_WINDOW_DIALOG);
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
- gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
+ gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_modal (GTK_WINDOW (window), TRUE);
- gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
+ gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
vbox = gtk_vbox_new (FALSE, 6);
gtk_widget_show(vbox);
gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
-
- gtkut_button_set_create(&confirm_area,
- &ok_btn, _("OK"),
- &cancel_btn, _("Cancel"),
- &apply_btn, _("Apply"));
+
+ gtkut_stock_button_set_create(&confirm_area,
+ &ok_btn, GTK_STOCK_OK,
+ &cancel_btn, GTK_STOCK_CANCEL,
+ &apply_btn, GTK_STOCK_APPLY);
gtk_widget_show(confirm_area);
gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
gtk_widget_grab_default(ok_btn);
gtk_widget_set_sensitive(widget, is_active);
}
+void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
+{
+ gboolean is_active;
+
+ is_active = gtk_toggle_button_get_active(toggle_btn);
+ gtk_widget_set_sensitive(widget, !is_active);
+}
+
void prefs_set_dialog(PrefParam *param)
{
gint i;
switch (tmpparam.type) {
case P_STRING:
+ case P_PASSWORD:
if (tmpparam.defval) {
- if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
+ if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
str_data = g_strdup(g_getenv(param[i].defval + 4));
tmpparam.data = &str_data;
break;
break;
case P_BOOL:
if (tmpparam.defval) {
- if (!strcasecmp(tmpparam.defval, "TRUE"))
+ if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
bool_data = TRUE;
else
bool_data = atoi(tmpparam.defval)
tmpparam.data = &enum_data;
break;
case P_OTHER:
+ default:
break;
}
tmpparam.widget_set_func(&tmpparam);
void prefs_set_data_from_entry(PrefParam *pparam)
{
- gchar **str, *entry_str;
+ gchar **str;
+ const gchar *entry_str;
g_return_if_fail(*pparam->widget != NULL);
switch (pparam->type) {
case P_STRING:
+ case P_PASSWORD:
str = (gchar **)pparam->data;
g_free(*str);
*str = entry_str[0] ? g_strdup(entry_str) : NULL;
switch (pparam->type) {
case P_STRING:
+ case P_PASSWORD:
str = (gchar **)pparam->data;
gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
*str ? *str : "");
void prefs_set_data_from_text(PrefParam *pparam)
{
gchar **str;
- gchar *text, *tp;
+ gchar *text = NULL, *tp = NULL;
gchar *tmp, *tmpp;
g_return_if_fail(*pparam->widget != NULL);
switch (pparam->type) {
case P_STRING:
+ case P_PASSWORD:
str = (gchar **)pparam->data;
g_free(*str);
- tp = text = gtk_editable_get_chars
- (GTK_EDITABLE(*pparam->widget), 0, -1);
+ if (GTK_IS_EDITABLE(*pparam->widget)) { /* need? */
+ tp = text = gtk_editable_get_chars
+ (GTK_EDITABLE(*pparam->widget), 0, -1);
+ } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
+ GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
+ GtkTextIter start, end;
+ gtk_text_buffer_get_start_iter(buffer, &start);
+ gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
+ tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
+ }
+
+ g_return_if_fail (tp && text);
+
if (text[0] == '\0') {
*str = NULL;
g_free(text);
{
gchar *buf, *sp, *bufp;
gchar **str;
- GtkText *text;
+ GtkTextView *text;
+ GtkTextBuffer *buffer;
+ GtkTextIter iter;
g_return_if_fail(*pparam->widget != NULL);
switch (pparam->type) {
case P_STRING:
+ case P_PASSWORD:
str = (gchar **)pparam->data;
if (*str) {
bufp = buf = alloca(strlen(*str) + 1);
} else
buf = "";
- text = GTK_TEXT(*pparam->widget);
- gtk_text_set_point(text, 0);
- gtk_text_forward_delete(text, gtk_text_get_length(text));
- gtk_text_set_point(text, 0);
- gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
+ text = GTK_TEXT_VIEW(*pparam->widget);
+ buffer = gtk_text_view_get_buffer(text);
+ gtk_text_buffer_set_text(buffer, "", -1);
+ gtk_text_buffer_get_start_iter(buffer, &iter);
+ gtk_text_buffer_insert(buffer, &iter, buf, -1);
break;
default:
- g_warning("Invalid PrefType for GtkText widget: %d\n",
+ g_warning("Invalid PrefType for GtkTextView widget: %d\n",
pparam->type);
}
}
pparam->type);
}
}
+
+static GSList *prefs_pages = NULL;
+
+void prefs_gtk_open(void)
+{
+ prefswindow_open(_("Preferences"), prefs_pages, NULL,
+ &prefs_common.prefswin_width, &prefs_common.prefswin_height);
+}
+
+void prefs_gtk_register_page(PrefsPage *page)
+{
+ prefs_pages = g_slist_append(prefs_pages, page);
+}
+
+void prefs_gtk_unregister_page(PrefsPage *page)
+{
+ prefs_pages = g_slist_remove(prefs_pages, page);
+}
+
+static void prefs_destroy_whole_cache(gpointer to_free)
+{
+ GHashTable *table = (GHashTable *)to_free;
+ g_hash_table_destroy(table);
+}
+
+static void prefs_destroy_file_cache(gpointer to_free)
+{
+ GHashTable *table = (GHashTable *)to_free;
+ g_hash_table_destroy(table);
+}
+
+static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
+{
+ FILE *fp = g_fopen(rcfile, "rb");
+ gchar buf[PREFSBUFSIZE];
+ GHashTable *section_cache = NULL;
+
+ if (!fp) {
+ debug_print("cache: %s: %s", rcfile, strerror(errno));
+ return -1;
+ }
+
+#ifdef HAVE_FGETS_UNLOCKED
+ flockfile(fp);
+#endif
+
+ while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
+ strretchomp(buf);
+ if (buf[0] == '\0')
+ continue;
+ if (buf[0] == '#')
+ continue; /* comment */
+ if (buf[0] == '[') { /* new section */
+ gchar *blockname = g_strdup(buf+1);
+
+ if (strrchr(blockname, ']'))
+ *strrchr(blockname, ']') = '\0';
+
+ if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
+ debug_print("new section '%s'\n", blockname);
+ section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, NULL);
+ g_hash_table_insert(file_cache,
+ blockname, section_cache);
+ } else {
+ debug_print("section '%s' already done\n", blockname);
+ g_free(blockname);
+ section_cache = NULL;
+ continue;
+ }
+ } else {
+ if (!section_cache) {
+ debug_print("skipping stuff %s with no section\n", buf);
+ continue;
+ } else {
+ gchar *pref;
+
+ if (!strchr(buf, '=')) {
+ /* plugins do differently */
+ continue;
+ }
+ pref = g_strdup(buf);
+
+ //debug_print("new pref '%s'\n", pref);
+ g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
+ }
+ }
+ }
+#ifdef HAVE_FGETS_UNLOCKED
+ funlockfile(fp);
+#endif
+ fclose(fp);
+ return 0;
+}
+
+static int prefs_cache(const gchar *rcfile)
+{
+ GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, prefs_destroy_file_cache);
+
+ debug_print("new file '%s'\n", rcfile);
+ g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
+
+ return prefs_cache_sections(file_cache, rcfile);
+}
+
+void prefs_prepare_cache(void)
+{
+ gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
+ gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
+ gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
+
+ if (whole_cache == NULL) {
+ whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, prefs_destroy_whole_cache);
+ } else {
+ debug_print("already cached\n");
+ g_free(clawsrc);
+ g_free(folderitemrc);
+ g_free(accountrc);
+ return;
+ }
+ if (prefs_cache(clawsrc) < 0 ||
+ prefs_cache(folderitemrc) < 0 ||
+ prefs_cache(accountrc) < 0)
+ prefs_destroy_cache();
+
+ g_free(clawsrc);
+ g_free(folderitemrc);
+ g_free(accountrc);
+}
+
+void prefs_destroy_cache(void)
+{
+ if (!whole_cache) {
+ debug_print("no cache\n");
+ return;
+ }
+ debug_print("destroying cache\n");
+ g_hash_table_destroy(whole_cache);
+ whole_cache = NULL;
+ return;
+}
+
+static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
+{
+ gchar *pref = (gchar *)key;
+
+ PrefParam *param = (PrefParam *)user_data;
+
+ prefs_config_parse_one_line(param, pref);
+}
+
+static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
+ const gchar *rcfile)
+{
+ GHashTable *sections_table = NULL;
+ GHashTable *values_table = NULL;
+ sections_table = g_hash_table_lookup(whole_cache, rcfile);
+
+ if (sections_table == NULL) {
+ g_warning("can't find %s in the whole cache\n", rcfile);
+ return FALSE;
+ }
+ values_table = g_hash_table_lookup(sections_table, label);
+
+ if (values_table == NULL) {
+ debug_print("no '%s' section in '%s' cache\n", label, rcfile);
+ return TRUE;
+ }
+ g_hash_table_foreach(values_table, prefs_parse_cache, param);
+ return TRUE;
+}