2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2007-2009 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/>.
27 #include <glib/gi18n.h>
32 #include <sys/types.h>
34 # include <sys/wait.h>
42 static GHashTable *tags_table = NULL;
43 static GHashTable *tags_reverse_table = NULL;
45 static int tag_max_id = 0;
47 void tags_read_tags(void)
49 gchar *file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
53 FILE *fp = fopen(file, "rb");
57 if (tags_table == NULL)
58 tags_table = g_hash_table_new_full(
59 g_direct_hash, g_direct_equal,
61 if (tags_reverse_table == NULL)
62 tags_reverse_table = g_hash_table_new_full(
63 g_str_hash, g_str_equal,
68 if (fscanf(fp, "max_id %d\n", &tag_max_id) != 1) {
72 while (fgets(tmp, sizeof(tmp), fp) != NULL) {
73 gchar *sep = strchr(tmp, '\t');
74 gchar *tag_name = sep?(sep+1):NULL;
76 if (!tag_name || !sep)
80 if (strcmp(tag_name, "NonJunk") &&
81 strcmp(tag_name, "NotJunk") &&
82 strcmp(tag_name, "NoJunk") &&
83 strcmp(tag_name, "$Forwarded") &&
84 strcmp(tag_name, "Junk")) {
86 g_hash_table_insert(tags_table,
87 GINT_TO_POINTER(id), g_strdup(tag_name));
88 g_hash_table_insert(tags_reverse_table,
89 g_strdup(tag_name), GINT_TO_POINTER(id));
96 typedef struct _TagWriteData
102 static void tag_write(gpointer key, gpointer value, gpointer user_data)
104 TagWriteData *data = (TagWriteData *)user_data;
105 const gchar *str = value;
106 gint id = GPOINTER_TO_INT(key);
111 if (fprintf(data->fp, "%d\t%s\n", id, str) <= 0) {
112 FILE_OP_ERROR("tagsrc", "fprintf");
117 void tags_write_tags(void)
119 gchar *file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
120 TAGS_RC, ".tmp", NULL);
121 gchar *file_new = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
125 FILE *fp = fopen(file, "wb");
128 FILE_OP_ERROR(file, "fopen");
137 if (fprintf(data.fp, "max_id %d\n", tag_max_id) <= 0) {
138 FILE_OP_ERROR("tagsrc", "fprintf");
141 g_hash_table_foreach(tags_table, tag_write, &data);
151 if (fclose(fp) == EOF) {
152 FILE_OP_ERROR(file, "fclose");
158 if (rename_force(file, file_new) < 0) {
159 FILE_OP_ERROR(file, "rename_force");
166 gint tags_add_tag(const gchar *tag)
171 if (g_hash_table_lookup(tags_reverse_table, tag))
174 if (strcmp(tag, "NonJunk") &&
175 strcmp(tag, "NotJunk") &&
176 strcmp(tag, "NoJunk") &&
177 strcmp(tag, "$Forwarded") &&
178 strcmp(tag, "Junk")) {
180 g_hash_table_insert(tags_table, GINT_TO_POINTER(tag_max_id),
182 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
183 GINT_TO_POINTER(tag_max_id));
191 void tags_remove_tag(gint id)
193 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
196 g_hash_table_remove(tags_reverse_table, old_tag);
198 g_hash_table_remove(tags_table, GINT_TO_POINTER(id));
201 /* extern decl. to avoid including ../prefs_filtering.h */
202 extern void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag);
204 void tags_update_tag(gint id, const gchar *tag)
206 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
208 if (strcmp(tag, "NonJunk") &&
209 strcmp(tag, "NotJunk") &&
210 strcmp(tag, "NoJunk") &&
211 strcmp(tag, "$Forwarded") &&
212 strcmp(tag, "Junk")) {
214 prefs_filtering_rename_tag(old_tag, tag);
215 g_hash_table_remove(tags_reverse_table, old_tag);
218 g_hash_table_replace(tags_table, GINT_TO_POINTER(id),
220 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
221 GINT_TO_POINTER(id));
225 const gchar *tags_get_tag(gint id)
227 return (const gchar *)g_hash_table_lookup(tags_table,
228 GINT_TO_POINTER(id));
231 gint tags_get_id_for_str(const gchar *str)
234 if ((id_ptr = g_hash_table_lookup(tags_reverse_table, str)) != NULL)
235 return GPOINTER_TO_INT(id_ptr);
240 typedef struct _TagListData {
244 static void tag_add_list(gpointer key, gpointer value, gpointer user_data)
246 TagListData *data = (TagListData *)user_data;
248 data->list = g_slist_prepend(data->list, GINT_TO_POINTER(key));
251 GSList *tags_get_list(void)
256 g_hash_table_foreach(tags_table, tag_add_list, &data);
258 data.list = g_slist_reverse(data.list);
263 guint tags_get_size(void)
265 return g_hash_table_size(tags_table);