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>
44 static GHashTable *tags_table = NULL;
45 static GHashTable *tags_reverse_table = NULL;
47 static int tag_max_id = 0;
49 void tags_read_tags(void)
51 gchar *file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
55 FILE *fp = g_fopen(file, "rb");
59 if (tags_table == NULL)
60 tags_table = g_hash_table_new_full(
61 g_direct_hash, g_direct_equal,
63 if (tags_reverse_table == NULL)
64 tags_reverse_table = g_hash_table_new_full(
65 g_str_hash, g_str_equal,
70 if (fscanf(fp, "max_id %d\n", &tag_max_id) != 1) {
74 while (fgets(tmp, sizeof(tmp), fp) != NULL) {
75 gchar *sep = strchr(tmp, '\t');
76 gchar *tag_name = sep?(sep+1):NULL;
78 if (!tag_name || !sep)
82 if (IS_NOT_RESERVED_TAG(tag_name)) {
84 g_hash_table_insert(tags_table,
85 GINT_TO_POINTER(id), g_strdup(tag_name));
86 g_hash_table_insert(tags_reverse_table,
87 g_strdup(tag_name), GINT_TO_POINTER(id));
94 typedef struct _TagWriteData
100 static void tag_write(gpointer key, gpointer value, gpointer user_data)
102 TagWriteData *data = (TagWriteData *)user_data;
103 const gchar *str = value;
104 gint id = GPOINTER_TO_INT(key);
109 if (fprintf(data->fp, "%d\t%s\n", id, str) <= 0) {
110 FILE_OP_ERROR("tagsrc", "fprintf");
115 void tags_write_tags(void)
117 gchar *file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
118 TAGS_RC, ".tmp", NULL);
119 gchar *file_new = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
123 FILE *fp = g_fopen(file, "wb");
126 FILE_OP_ERROR(file, "g_fopen");
135 if (fprintf(data.fp, "max_id %d\n", tag_max_id) <= 0) {
136 FILE_OP_ERROR("tagsrc", "fprintf");
139 g_hash_table_foreach(tags_table, tag_write, &data);
149 if (fclose(fp) == EOF) {
150 FILE_OP_ERROR(file, "fclose");
156 if (rename_force(file, file_new) < 0) {
157 FILE_OP_ERROR(file, "rename_force");
164 gint tags_add_tag(const gchar *tag)
169 if (g_hash_table_lookup(tags_reverse_table, tag))
172 if (IS_NOT_RESERVED_TAG(tag)) {
174 g_hash_table_insert(tags_table, GINT_TO_POINTER(tag_max_id),
176 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
177 GINT_TO_POINTER(tag_max_id));
185 void tags_remove_tag(gint id)
187 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
190 g_hash_table_remove(tags_reverse_table, old_tag);
192 g_hash_table_remove(tags_table, GINT_TO_POINTER(id));
195 /* extern decl. to avoid including ../prefs_filtering.h */
196 extern void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag);
198 void tags_update_tag(gint id, const gchar *tag)
200 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
202 if (IS_NOT_RESERVED_TAG(tag)) {
204 prefs_filtering_rename_tag(old_tag, tag);
205 g_hash_table_remove(tags_reverse_table, old_tag);
208 g_hash_table_replace(tags_table, GINT_TO_POINTER(id),
210 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
211 GINT_TO_POINTER(id));
215 const gchar *tags_get_tag(gint id)
217 return (const gchar *)g_hash_table_lookup(tags_table,
218 GINT_TO_POINTER(id));
221 gint tags_get_id_for_str(const gchar *str)
224 if ((id_ptr = g_hash_table_lookup(tags_reverse_table, str)) != NULL)
225 return GPOINTER_TO_INT(id_ptr);
230 typedef struct _TagListData {
234 static void tag_add_list(gpointer key, gpointer value, gpointer user_data)
236 TagListData *data = (TagListData *)user_data;
238 data->list = g_slist_prepend(data->list, GINT_TO_POINTER(key));
241 GSList *tags_get_list(void)
246 g_hash_table_foreach(tags_table, tag_add_list, &data);
248 data.list = g_slist_reverse(data.list);
253 guint tags_get_size(void)
255 return g_hash_table_size(tags_table);