2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto & 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 (fscanf(fp, "%d\t%254s\n", &id, tmp) == 2) {
73 g_hash_table_insert(tags_table,
74 GINT_TO_POINTER(id), g_strdup(tmp));
75 g_hash_table_insert(tags_reverse_table,
76 g_strdup(tmp), GINT_TO_POINTER(id));
82 typedef struct _TagWriteData
88 static void tag_write(gpointer key, gpointer value, gpointer user_data)
90 TagWriteData *data = (TagWriteData *)user_data;
91 const gchar *str = value;
92 gint id = GPOINTER_TO_INT(key);
97 if (fprintf(data->fp, "%d\t%s\n", id, str) <= 0) {
98 FILE_OP_ERROR("tagsrc", "fprintf");
103 void tags_write_tags(void)
105 gchar *file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
106 TAGS_RC, ".tmp", NULL);
107 gchar *file_new = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
111 FILE *fp = fopen(file, "wb");
114 FILE_OP_ERROR(file, "fopen");
123 if (fprintf(data.fp, "max_id %d\n", tag_max_id) <= 0) {
124 FILE_OP_ERROR("tagsrc", "fprintf");
127 g_hash_table_foreach(tags_table, tag_write, &data);
136 if (fclose(fp) == EOF) {
137 FILE_OP_ERROR(file, "fclose");
143 if (g_rename(file, file_new) < 0) {
144 FILE_OP_ERROR(file, "g_rename");
151 gint tags_add_tag(const gchar *tag)
153 if (g_hash_table_lookup(tags_reverse_table, tag))
157 g_hash_table_insert(tags_table, GINT_TO_POINTER(tag_max_id),
159 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
160 GINT_TO_POINTER(tag_max_id));
165 void tags_remove_tag(gint id)
167 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
170 g_hash_table_remove(tags_reverse_table, old_tag);
172 g_hash_table_remove(tags_table, GINT_TO_POINTER(id));
175 void tags_update_tag(gint id, const gchar *tag)
177 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
180 g_hash_table_remove(tags_reverse_table, old_tag);
183 g_hash_table_replace(tags_table, GINT_TO_POINTER(id),
185 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
186 GINT_TO_POINTER(id));
189 const gchar *tags_get_tag(gint id)
191 return (const gchar *)g_hash_table_lookup(tags_table,
192 GINT_TO_POINTER(id));
195 gint tags_get_id_for_str(const gchar *str)
198 if ((id_ptr = g_hash_table_lookup(tags_reverse_table, str)) != NULL)
199 return GPOINTER_TO_INT(id_ptr);
204 typedef struct _TagListData {
208 static void tag_add_list(gpointer key, gpointer value, gpointer user_data)
210 TagListData *data = (TagListData *)user_data;
212 data->list = g_slist_prepend(data->list, GINT_TO_POINTER(key));
215 GSList *tags_get_list(void)
220 g_hash_table_foreach(tags_table, tag_add_list, &data);
222 data.list = g_slist_reverse(data.list);