2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2007-2012 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>
33 #include <sys/types.h>
35 # include <sys/wait.h>
43 #include "file-utils.h"
45 static GHashTable *tags_table = NULL;
46 static GHashTable *tags_reverse_table = NULL;
48 static int tag_max_id = 0;
50 void tags_read_tags(void)
52 gchar *file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
56 FILE *fp = claws_fopen(file, "rb");
60 if (tags_table == NULL)
61 tags_table = g_hash_table_new_full(
62 g_direct_hash, g_direct_equal,
64 if (tags_reverse_table == NULL)
65 tags_reverse_table = g_hash_table_new_full(
66 g_str_hash, g_str_equal,
71 if (fscanf(fp, "max_id %d\n", &tag_max_id) != 1) {
75 while (claws_fgets(tmp, sizeof(tmp), fp) != NULL) {
76 gchar *sep = strchr(tmp, '\t');
77 gchar *tag_name = sep?(sep+1):NULL;
79 if (!tag_name || !sep)
83 if (IS_NOT_RESERVED_TAG(tag_name)) {
85 g_hash_table_insert(tags_table,
86 GINT_TO_POINTER(id), g_strdup(tag_name));
87 g_hash_table_insert(tags_reverse_table,
88 g_strdup(tag_name), GINT_TO_POINTER(id));
95 typedef struct _TagWriteData
101 static void tag_write(gpointer key, gpointer value, gpointer user_data)
103 TagWriteData *data = (TagWriteData *)user_data;
104 const gchar *str = value;
105 gint id = GPOINTER_TO_INT(key);
110 if (fprintf(data->fp, "%d\t%s\n", id, str) <= 0) {
111 FILE_OP_ERROR("tagsrc", "fprintf");
116 void tags_write_tags(void)
118 gchar *file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
119 TAGS_RC, ".tmp", NULL);
120 gchar *file_new = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
124 FILE *fp = claws_fopen(file, "wb");
127 FILE_OP_ERROR(file, "claws_fopen");
136 if (fprintf(data.fp, "max_id %d\n", tag_max_id) <= 0) {
137 FILE_OP_ERROR("tagsrc", "fprintf");
140 g_hash_table_foreach(tags_table, tag_write, &data);
150 if (claws_safe_fclose(fp) == EOF) {
151 FILE_OP_ERROR(file, "claws_fclose");
157 if (rename_force(file, file_new) < 0) {
158 FILE_OP_ERROR(file, "rename_force");
165 gint tags_add_tag(const gchar *tag)
170 if (g_hash_table_lookup(tags_reverse_table, tag))
173 if (IS_NOT_RESERVED_TAG(tag)) {
175 g_hash_table_insert(tags_table, GINT_TO_POINTER(tag_max_id),
177 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
178 GINT_TO_POINTER(tag_max_id));
186 void tags_remove_tag(gint id)
188 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
191 g_hash_table_remove(tags_reverse_table, old_tag);
193 g_hash_table_remove(tags_table, GINT_TO_POINTER(id));
196 /* extern decl. to avoid including ../prefs_filtering.h */
197 extern void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag);
199 void tags_update_tag(gint id, const gchar *tag)
201 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
203 if (IS_NOT_RESERVED_TAG(tag)) {
205 prefs_filtering_rename_tag(old_tag, tag);
206 g_hash_table_remove(tags_reverse_table, old_tag);
209 g_hash_table_replace(tags_table, GINT_TO_POINTER(id),
211 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
212 GINT_TO_POINTER(id));
216 const gchar *tags_get_tag(gint id)
218 return (const gchar *)g_hash_table_lookup(tags_table,
219 GINT_TO_POINTER(id));
222 gint tags_get_id_for_str(const gchar *str)
225 if ((id_ptr = g_hash_table_lookup(tags_reverse_table, str)) != NULL)
226 return GPOINTER_TO_INT(id_ptr);
231 typedef struct _TagListData {
235 static void tag_add_list(gpointer key, gpointer value, gpointer user_data)
237 TagListData *data = (TagListData *)user_data;
239 data->list = g_slist_prepend(data->list, GINT_TO_POINTER(key));
242 GSList *tags_get_list(void)
247 g_hash_table_foreach(tags_table, tag_add_list, &data);
249 data.list = g_slist_reverse(data.list);
254 guint tags_get_size(void)
256 return g_hash_table_size(tags_table);