/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2012 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
+#include "claws-features.h"
#endif
+#define _GNU_SOURCE
+#include <stdio.h>
+
#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
+#include "defs.h"
#include "main.h"
#include "prefs.h"
#include "prefs_gtk.h"
#include "utils.h"
#include "gtkutils.h"
#include "passcrypt.h"
-#include "base64.h"
#include "codeconv.h"
#define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
(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);
+
+static void prefs_config_parse_one_line(PrefParam *param,
+ const gchar *buf);
+
void prefs_read_config(PrefParam *param, const gchar *label,
const gchar *rcfile, const gchar *encoding)
{
gchar buf[PREFSBUFSIZE];
gchar *block_label;
- g_return_if_fail(param != NULL);
- g_return_if_fail(label != NULL);
- g_return_if_fail(rcfile != NULL);
+ cm_return_if_fail(param != NULL);
+ cm_return_if_fail(label != NULL);
+ cm_return_if_fail(rcfile != NULL);
if (encoding != NULL)
g_warning("Encoding is ignored\n");
prefs_set_default(param);
+ 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;
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;
if (encoding) {
}
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;
}
debug_print("Finished reading configuration.\n");
+#ifdef HAVE_FGETS_UNLOCKED
+ funlockfile(fp);
+#endif
fclose(fp);
}
-void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
+static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
{
gint i;
gint name_len;
tmp = g_strdup("");
}
if (!tmp) {
- g_warning("failed to convert character set.");
+ g_warning("Failed to convert character set.");
tmp = g_strdup(value);
}
g_free(*((gchar **)param[i].data));
(gushort)atoi(value);
break;
case P_COLOR:
- if (gdk_color_parse(value, &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);
case P_PASSWORD:
g_free(*((gchar **)param[i].data));
if (value[0] == '!') {
- gchar tmp[1024];
- gint len;
+ gchar *tmp;
+ gsize len;
- len = base64_decode(tmp, &value[1], strlen(value) - 1);
+ tmp = g_base64_decode_zero(&value[1], &len);
passcrypt_decrypt(tmp, len);
- tmp[len] = '\0';
+
*((gchar **)param[i].data) =
*tmp ? g_strdup(tmp) : NULL;
+ g_free(tmp);
} else {
*((gchar **)param[i].data) =
*value ? g_strdup(value) : NULL;
#define TRY(func) \
if (!(func)) \
{ \
- g_warning("failed to write configuration to file\n"); \
+ g_warning("Failed to write configuration to file\n"); \
if (orig_fp) fclose(orig_fp); \
prefs_file_close_revert(pfile); \
g_free(rcpath); \
gchar *block_label = NULL;
gboolean block_matched = FALSE;
- g_return_if_fail(param != NULL);
- g_return_if_fail(label != NULL);
- g_return_if_fail(rcfile != NULL);
+ cm_return_if_fail(param != NULL);
+ cm_return_if_fail(label != NULL);
+ cm_return_if_fail(rcfile != NULL);
rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
if ((orig_fp = g_fopen(rcpath, "rb")) == NULL) {
}
if ((pfile = prefs_write_open(rcpath)) == NULL) {
- g_warning("failed to write configuration to file\n");
+ g_warning("Failed to write configuration to file\n");
if (orig_fp) fclose(orig_fp);
g_free(rcpath);
return;
}
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");
+ g_warning("Failed to write configuration to file\n");
g_free(rcpath);
debug_print("Configuration is saved.\n");
break;
case P_PASSWORD:
{
- gchar *tmp = NULL, tmp2[1024] = {0};
+ gchar *tmp = NULL, *tmp2 = NULL;
tmp = *((gchar **)param[i].data);
if (tmp) {
tmp = g_strdup(tmp);
len = strlen(tmp);
passcrypt_encrypt(tmp, len);
- base64_encode(tmp2, tmp, len);
+ tmp2 = g_base64_encode(tmp, len);
g_free(tmp);
tmp = tmp2;
}
g_snprintf(buf, sizeof(buf), "%s=!%s\n", param[i].name,
tmp ?
tmp : "");
+ g_free(tmp);
}
break;
default:
gint i;
GdkColor color;
- g_return_if_fail(param != NULL);
+ cm_return_if_fail(param != NULL);
for (i = 0; param[i].name != NULL; i++) {
if (!param[i].data) continue;
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)) {
CS_INTERNAL)
: g_strdup("");
if (!tmp) {
- g_warning("faild to convert character set.");
+ g_warning("Failed to convert character set.");
tmp = g_strdup(envstr);
}
*((gchar **)param[i].data) = tmp;
g_strconcat(get_home_dir(),
param[i].defval + 1,
NULL);
- else if (param[i].defval[0] != '\0')
+ else
+ *((gchar **)param[i].data) =
+ g_strdup(param[i].defval);
+ } else
+ *((gchar **)param[i].data) = NULL;
+ break;
+ case P_PASSWORD:
+ g_free(*((gchar **)param[i].data));
+ if (param[i].defval != NULL) {
+ if (param[i].defval[0] != '\0')
*((gchar **)param[i].data) =
g_strdup(param[i].defval);
else
{
gint i;
- g_return_if_fail(param != NULL);
+ cm_return_if_fail(param != NULL);
for (i = 0; param[i].name != NULL; i++) {
if (!param[i].data) continue;
}
}
-void prefs_dialog_create(PrefsDialog *dialog)
-{
- GtkWidget *window;
- GtkWidget *vbox;
- GtkWidget *notebook;
-
- GtkWidget *confirm_area;
- GtkWidget *ok_btn;
- GtkWidget *cancel_btn;
- GtkWidget *apply_btn;
-
- g_return_if_fail(dialog != NULL);
-
- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- gtk_container_set_border_width (GTK_CONTAINER (window), 8);
- gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
- gtk_window_set_modal (GTK_WINDOW (window), TRUE);
- gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
-
- vbox = gtk_vbox_new (FALSE, 6);
- gtk_widget_show(vbox);
- gtk_container_add (GTK_CONTAINER (window), vbox);
-
- notebook = gtk_notebook_new ();
- gtk_widget_show(notebook);
- gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
- gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
- /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
- gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
-
- gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
-
- 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);
-
- dialog->window = window;
- dialog->notebook = notebook;
- dialog->ok_btn = ok_btn;
- dialog->cancel_btn = cancel_btn;
- dialog->apply_btn = apply_btn;
-}
-
-void prefs_dialog_destroy(PrefsDialog *dialog)
-{
- gtk_widget_destroy(dialog->window);
- dialog->window = NULL;
- dialog->notebook = NULL;
- dialog->ok_btn = NULL;
- dialog->cancel_btn = NULL;
- dialog->apply_btn = NULL;
-}
-
void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
{
gboolean is_active;
switch (tmpparam.type) {
case P_STRING:
- case P_PASSWORD:
if (tmpparam.defval) {
if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
str_data = g_strdup(g_getenv(param[i].defval + 4));
}
tmpparam.data = &tmpparam.defval;
break;
+ case P_PASSWORD:
+ tmpparam.data = &tmpparam.defval;
+ break;
case P_INT:
if (tmpparam.defval)
int_data = atoi(tmpparam.defval);
gchar **str;
const gchar *entry_str;
- g_return_if_fail(*pparam->widget != NULL);
+ cm_return_if_fail(*pparam->widget != NULL);
entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
}
}
+void prefs_set_escaped_data_from_entry(PrefParam *pparam)
+{
+ gchar **str;
+
+ cm_return_if_fail(*pparam->widget != NULL);
+
+ switch (pparam->type) {
+ case P_STRING:
+ str = (gchar **)pparam->data;
+ g_free(*str);
+ *str = pref_get_pref_from_entry(GTK_ENTRY(*pparam->widget));
+ break;
+ default:
+ g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
+ pparam->type);
+ }
+}
+
void prefs_set_entry(PrefParam *pparam)
{
gchar **str;
- g_return_if_fail(*pparam->widget != NULL);
+ cm_return_if_fail(*pparam->widget != NULL);
switch (pparam->type) {
case P_STRING:
}
}
+void prefs_set_entry_from_escaped(PrefParam *pparam)
+{
+ gchar **str;
+
+ cm_return_if_fail(*pparam->widget != NULL);
+
+ switch (pparam->type) {
+ case P_STRING:
+ str = (gchar **)pparam->data;
+ pref_set_entry_from_pref(GTK_ENTRY(*pparam->widget),
+ *str ? *str : "");
+ break;
+ default:
+ g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
+ pparam->type);
+ }
+}
+
void prefs_set_data_from_text(PrefParam *pparam)
{
gchar **str;
gchar *text = NULL, *tp = NULL;
gchar *tmp, *tmpp;
- g_return_if_fail(*pparam->widget != NULL);
+ cm_return_if_fail(*pparam->widget != NULL);
switch (pparam->type) {
case P_STRING:
tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
}
- g_return_if_fail (tp && text);
+ cm_return_if_fail (tp && text);
if (text[0] == '\0') {
*str = NULL;
}
}
+void prefs_set_escaped_data_from_text(PrefParam *pparam)
+{
+ gchar **str;
+
+ cm_return_if_fail(*pparam->widget != NULL);
+
+ switch (pparam->type) {
+ case P_STRING:
+ str = (gchar **)pparam->data;
+ g_free(*str);
+ *str = pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam->widget));
+ break;
+ default:
+ g_warning("Invalid escaped PrefType for GtkText widget: %d\n",
+ pparam->type);
+ }
+}
+
void prefs_set_text(PrefParam *pparam)
{
gchar *buf, *sp, *bufp;
GtkTextBuffer *buffer;
GtkTextIter iter;
- g_return_if_fail(*pparam->widget != NULL);
+ cm_return_if_fail(*pparam->widget != NULL);
switch (pparam->type) {
case P_STRING:
}
}
+void prefs_set_text_from_escaped(PrefParam *pparam)
+{
+ gchar **str;
+
+ cm_return_if_fail(*pparam->widget != NULL);
+
+ switch (pparam->type) {
+ case P_STRING:
+ str = (gchar **)pparam->data;
+ pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam->widget),
+ *str ? *str : "");
+ break;
+ default:
+ g_warning("Invalid escaped PrefType for GtkTextView widget: %d\n",
+ pparam->type);
+ }
+}
+
void prefs_set_data_from_toggle(PrefParam *pparam)
{
- g_return_if_fail(pparam->type == P_BOOL);
- g_return_if_fail(*pparam->widget != NULL);
+ cm_return_if_fail(pparam->type == P_BOOL);
+ cm_return_if_fail(*pparam->widget != NULL);
*((gboolean *)pparam->data) =
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
void prefs_set_toggle(PrefParam *pparam)
{
- g_return_if_fail(pparam->type == P_BOOL);
- g_return_if_fail(*pparam->widget != NULL);
+ cm_return_if_fail(pparam->type == P_BOOL);
+ cm_return_if_fail(*pparam->widget != NULL);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
*((gboolean *)pparam->data));
void prefs_set_data_from_spinbtn(PrefParam *pparam)
{
- g_return_if_fail(*pparam->widget != NULL);
+ cm_return_if_fail(*pparam->widget != NULL);
switch (pparam->type) {
case P_INT:
void prefs_set_spinbtn(PrefParam *pparam)
{
- g_return_if_fail(*pparam->widget != NULL);
+ cm_return_if_fail(*pparam->widget != NULL);
switch (pparam->type) {
case P_INT:
void prefs_gtk_open(void)
{
prefswindow_open(_("Preferences"), prefs_pages, NULL,
- &prefs_common.prefswin_width, &prefs_common.prefswin_height);
+ &prefs_common.prefswin_width, &prefs_common.prefswin_height,
+ NULL, NULL);
}
void prefs_gtk_register_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 = NULL;
+ gchar buf[PREFSBUFSIZE];
+ GHashTable *section_cache = NULL;
+
+ if (rcfile)
+ fp = g_fopen(rcfile, "rb");
+ if (!fp) {
+ debug_print("cache: %s: %s\n", rcfile?rcfile:"(null)", 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?rcfile:"(null)");
+ 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?rcfile:"(null)");
+ 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?label:"(null)", rcfile?rcfile:"(null)");
+ return TRUE;
+ }
+ g_hash_table_foreach(values_table, prefs_parse_cache, param);
+ return TRUE;
+}