2007-10-01 [colin] 3.0.1cvs2-stable
[claws.git] / src / common / tags.c
blob970b37a28c0e268cc8f28d7530a4aa72b0a0ccb6
1 /*
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 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/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #if HAVE_SYS_WAIT_H
34 # include <sys/wait.h>
35 #endif
36 #include <signal.h>
37 #include <unistd.h>
39 #include "defs.h"
40 #include "utils.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,
50 TAGS_RC, NULL);
51 gchar tmp[255];
52 gint id;
53 FILE *fp = fopen(file, "rb");
55 g_free(file);
57 if (tags_table == NULL)
58 tags_table = g_hash_table_new_full(
59 g_direct_hash, g_direct_equal,
60 NULL, g_free);
61 if (tags_reverse_table == NULL)
62 tags_reverse_table = g_hash_table_new_full(
63 g_str_hash, g_str_equal,
64 g_free, NULL);
66 if (!fp)
67 return;
68 if (fscanf(fp, "max_id %d\n", &tag_max_id) != 1) {
69 fclose(fp);
70 return;
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)
77 continue;
78 g_strstrip(tag_name);
79 *(sep) = '\0';
80 id = atoi(tmp);
81 g_hash_table_insert(tags_table,
82 GINT_TO_POINTER(id), g_strdup(tag_name));
83 g_hash_table_insert(tags_reverse_table,
84 g_strdup(tag_name), GINT_TO_POINTER(id));
87 fclose(fp);
90 typedef struct _TagWriteData
92 FILE *fp;
93 gboolean error;
94 } TagWriteData;
96 static void tag_write(gpointer key, gpointer value, gpointer user_data)
98 TagWriteData *data = (TagWriteData *)user_data;
99 const gchar *str = value;
100 gint id = GPOINTER_TO_INT(key);
102 if (data->error)
103 return;
105 if (fprintf(data->fp, "%d\t%s\n", id, str) <= 0) {
106 FILE_OP_ERROR("tagsrc", "fprintf");
107 data->error = TRUE;
111 void tags_write_tags(void)
113 gchar *file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
114 TAGS_RC, ".tmp", NULL);
115 gchar *file_new = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
116 TAGS_RC, NULL);
117 TagWriteData data;
119 FILE *fp = fopen(file, "wb");
121 if (!fp) {
122 FILE_OP_ERROR(file, "fopen");
123 g_free(file);
124 g_free(file_new);
125 return;
128 data.fp = fp;
129 data.error = FALSE;
131 if (fprintf(data.fp, "max_id %d\n", tag_max_id) <= 0) {
132 FILE_OP_ERROR("tagsrc", "fprintf");
133 data.error = TRUE;
134 } else {
135 g_hash_table_foreach(tags_table, tag_write, &data);
138 if (data.error) {
139 fclose(fp);
140 g_free(file);
141 g_free(file_new);
142 return;
145 if (fclose(fp) == EOF) {
146 FILE_OP_ERROR(file, "fclose");
147 g_free(file);
148 g_free(file_new);
149 return;
152 if (rename_force(file, file_new) < 0) {
153 FILE_OP_ERROR(file, "rename_force");
156 g_free(file);
157 g_free(file_new);
160 gint tags_add_tag(const gchar *tag)
162 if (!tag || !(*tag))
163 return -1;
165 if (g_hash_table_lookup(tags_reverse_table, tag))
166 return -1;
168 tag_max_id++;
169 g_hash_table_insert(tags_table, GINT_TO_POINTER(tag_max_id),
170 g_strdup(tag));
171 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
172 GINT_TO_POINTER(tag_max_id));
174 return tag_max_id;
177 void tags_remove_tag(gint id)
179 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
181 if (old_tag) {
182 g_hash_table_remove(tags_reverse_table, old_tag);
184 g_hash_table_remove(tags_table, GINT_TO_POINTER(id));
187 /* extern decl. to avoid including ../prefs_filtering.h */
188 extern void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag);
190 void tags_update_tag(gint id, const gchar *tag)
192 gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
194 if (old_tag) {
195 prefs_filtering_rename_tag(old_tag, tag);
196 g_hash_table_remove(tags_reverse_table, old_tag);
199 g_hash_table_replace(tags_table, GINT_TO_POINTER(id),
200 g_strdup(tag));
201 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
202 GINT_TO_POINTER(id));
205 const gchar *tags_get_tag(gint id)
207 return (const gchar *)g_hash_table_lookup(tags_table,
208 GINT_TO_POINTER(id));
211 gint tags_get_id_for_str(const gchar *str)
213 gpointer id_ptr;
214 if ((id_ptr = g_hash_table_lookup(tags_reverse_table, str)) != NULL)
215 return GPOINTER_TO_INT(id_ptr);
216 else
217 return -1;
220 typedef struct _TagListData {
221 GSList *list;
222 } TagListData;
224 static void tag_add_list(gpointer key, gpointer value, gpointer user_data)
226 TagListData *data = (TagListData *)user_data;
228 data->list = g_slist_prepend(data->list, GINT_TO_POINTER(key));
231 GSList *tags_get_list(void)
233 TagListData data;
234 data.list = NULL;
236 g_hash_table_foreach(tags_table, tag_add_list, &data);
238 data.list = g_slist_reverse(data.list);
240 return data.list;