2009-01-17 [colin] 3.7.0cvs37
[claws.git] / src / common / tags.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2007-2009 The Claws Mail Team
4  *
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.
9  *
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.
14  *
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/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
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>
38
39 #include "defs.h"
40 #include "utils.h"
41
42 static GHashTable *tags_table = NULL;
43 static GHashTable *tags_reverse_table = NULL;
44
45 static int tag_max_id = 0;
46
47 void tags_read_tags(void)
48 {
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 = g_fopen(file, "rb");
54         
55         g_free(file);
56
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);
65                 
66         if (!fp) 
67                 return;
68         if (fscanf(fp, "max_id %d\n", &tag_max_id) != 1) {
69                 fclose(fp);
70                 return;
71         }
72         while (fgets(tmp, sizeof(tmp), fp) != NULL) {
73                 gchar *sep = strchr(tmp, '\t');
74                 gchar *tag_name = sep?(sep+1):NULL;
75                 
76                 if (!tag_name || !sep)
77                         continue;
78                 g_strstrip(tag_name);
79                 *(sep) = '\0';
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")) {
85                         id = atoi(tmp);
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));
90                 }
91         }
92         
93         fclose(fp);
94 }
95
96 typedef struct _TagWriteData
97 {
98         FILE *fp;
99         gboolean error;
100 } TagWriteData;
101
102 static void tag_write(gpointer key, gpointer value, gpointer user_data)
103 {
104         TagWriteData *data = (TagWriteData *)user_data;
105         const gchar *str = value;
106         gint id = GPOINTER_TO_INT(key);
107
108         if (data->error)
109                 return;
110                 
111         if (fprintf(data->fp, "%d\t%s\n", id, str) <= 0) {
112                 FILE_OP_ERROR("tagsrc", "fprintf");
113                 data->error = TRUE;
114         }
115 }
116
117 void tags_write_tags(void)
118 {
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,
122                         TAGS_RC, NULL);
123         TagWriteData data;
124
125         FILE *fp = g_fopen(file, "wb");
126                         
127         if (!fp) {
128                 FILE_OP_ERROR(file, "g_fopen");
129                 g_free(file);
130                 g_free(file_new);
131                 return;
132         }
133         
134         data.fp = fp;
135         data.error = FALSE;
136
137         if (fprintf(data.fp, "max_id %d\n", tag_max_id) <= 0) {
138                 FILE_OP_ERROR("tagsrc", "fprintf");
139                 data.error = TRUE;
140         } else {
141                 g_hash_table_foreach(tags_table, tag_write, &data);
142         }
143
144         if (data.error) {
145                 fclose(fp);
146                 g_free(file);
147                 g_free(file_new);
148                 return;
149         }
150         
151         if (fclose(fp) == EOF) {
152                 FILE_OP_ERROR(file, "fclose");
153                 g_free(file);
154                 g_free(file_new);
155                 return;
156         }
157
158         if (rename_force(file, file_new) < 0) {
159                 FILE_OP_ERROR(file, "rename_force");
160         }
161
162         g_free(file);
163         g_free(file_new);
164 }
165
166 gint tags_add_tag(const gchar *tag)
167 {
168         if (!tag || !(*tag))
169                 return -1;
170
171         if (g_hash_table_lookup(tags_reverse_table, tag))
172                 return -1;
173
174         if (strcmp(tag, "NonJunk") && 
175             strcmp(tag, "NotJunk") && 
176             strcmp(tag, "NoJunk") && 
177             strcmp(tag, "$Forwarded") && 
178             strcmp(tag, "Junk")) {
179                 tag_max_id++;
180                 g_hash_table_insert(tags_table, GINT_TO_POINTER(tag_max_id), 
181                         g_strdup(tag));
182                 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
183                         GINT_TO_POINTER(tag_max_id));
184
185                 return tag_max_id;
186         } else {
187                 return -1;
188         }
189 }
190
191 void tags_remove_tag(gint id)
192 {
193         gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
194
195         if (old_tag) {
196                 g_hash_table_remove(tags_reverse_table, old_tag);
197         }
198         g_hash_table_remove(tags_table, GINT_TO_POINTER(id));
199 }
200
201 /* extern decl. to avoid including ../prefs_filtering.h */
202 extern void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag);
203
204 void tags_update_tag(gint id, const gchar *tag)
205 {
206         gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
207
208         if (strcmp(tag, "NonJunk") && 
209             strcmp(tag, "NotJunk") && 
210             strcmp(tag, "NoJunk") && 
211             strcmp(tag, "$Forwarded") && 
212             strcmp(tag, "Junk")) {
213                 if (old_tag) {
214                         prefs_filtering_rename_tag(old_tag, tag);
215                         g_hash_table_remove(tags_reverse_table, old_tag);
216                 }
217
218                 g_hash_table_replace(tags_table, GINT_TO_POINTER(id), 
219                         g_strdup(tag));
220                 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
221                         GINT_TO_POINTER(id));
222         }
223 }
224
225 const gchar *tags_get_tag(gint id)
226 {
227         return (const gchar *)g_hash_table_lookup(tags_table,
228                                 GINT_TO_POINTER(id));
229 }
230
231 gint tags_get_id_for_str(const gchar *str)
232 {
233         gpointer id_ptr;
234         if ((id_ptr = g_hash_table_lookup(tags_reverse_table, str)) != NULL)
235                 return GPOINTER_TO_INT(id_ptr);
236         else
237                 return -1;
238 }
239
240 typedef struct _TagListData {
241         GSList *list;
242 } TagListData;
243
244 static void tag_add_list(gpointer key, gpointer value, gpointer user_data)
245 {
246         TagListData *data = (TagListData *)user_data;
247
248         data->list = g_slist_prepend(data->list, GINT_TO_POINTER(key));
249 }
250
251 GSList *tags_get_list(void)
252 {
253         TagListData data;
254         data.list = NULL;
255
256         g_hash_table_foreach(tags_table, tag_add_list, &data);
257
258         data.list = g_slist_reverse(data.list);
259         
260         return data.list;
261 }
262
263 guint tags_get_size(void)
264 {
265         return g_hash_table_size(tags_table);
266 }