2012-07-07 [colin] 3.8.1cvs7
[claws.git] / src / common / tags.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2007-2012 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 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #if HAVE_SYS_WAIT_H
35 #  include <sys/wait.h>
36 #endif
37 #include <signal.h>
38 #include <unistd.h>
39
40 #include "defs.h"
41 #include "utils.h"
42 #include "tags.h"
43
44 static GHashTable *tags_table = NULL;
45 static GHashTable *tags_reverse_table = NULL;
46
47 static int tag_max_id = 0;
48
49 void tags_read_tags(void)
50 {
51         gchar *file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
52                         TAGS_RC, NULL);
53         gchar tmp[255];
54         gint id;
55         FILE *fp = g_fopen(file, "rb");
56         
57         g_free(file);
58
59         if (tags_table == NULL)
60                 tags_table = g_hash_table_new_full(
61                                 g_direct_hash, g_direct_equal,
62                                 NULL, g_free);
63         if (tags_reverse_table == NULL)
64                 tags_reverse_table = g_hash_table_new_full(
65                                 g_str_hash, g_str_equal,
66                                 g_free, NULL);
67                 
68         if (!fp) 
69                 return;
70         if (fscanf(fp, "max_id %d\n", &tag_max_id) != 1) {
71                 fclose(fp);
72                 return;
73         }
74         while (fgets(tmp, sizeof(tmp), fp) != NULL) {
75                 gchar *sep = strchr(tmp, '\t');
76                 gchar *tag_name = sep?(sep+1):NULL;
77                 
78                 if (!tag_name || !sep)
79                         continue;
80                 g_strstrip(tag_name);
81                 *(sep) = '\0';
82                 if (IS_NOT_RESERVED_TAG(tag_name)) {
83                         id = atoi(tmp);
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));
88                 }
89         }
90         
91         fclose(fp);
92 }
93
94 typedef struct _TagWriteData
95 {
96         FILE *fp;
97         gboolean error;
98 } TagWriteData;
99
100 static void tag_write(gpointer key, gpointer value, gpointer user_data)
101 {
102         TagWriteData *data = (TagWriteData *)user_data;
103         const gchar *str = value;
104         gint id = GPOINTER_TO_INT(key);
105
106         if (data->error)
107                 return;
108                 
109         if (fprintf(data->fp, "%d\t%s\n", id, str) <= 0) {
110                 FILE_OP_ERROR("tagsrc", "fprintf");
111                 data->error = TRUE;
112         }
113 }
114
115 void tags_write_tags(void)
116 {
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,
120                         TAGS_RC, NULL);
121         TagWriteData data;
122
123         FILE *fp = g_fopen(file, "wb");
124                         
125         if (!fp) {
126                 FILE_OP_ERROR(file, "g_fopen");
127                 g_free(file);
128                 g_free(file_new);
129                 return;
130         }
131         
132         data.fp = fp;
133         data.error = FALSE;
134
135         if (fprintf(data.fp, "max_id %d\n", tag_max_id) <= 0) {
136                 FILE_OP_ERROR("tagsrc", "fprintf");
137                 data.error = TRUE;
138         } else {
139                 g_hash_table_foreach(tags_table, tag_write, &data);
140         }
141
142         if (data.error) {
143                 fclose(fp);
144                 g_free(file);
145                 g_free(file_new);
146                 return;
147         }
148         
149         if (fclose(fp) == EOF) {
150                 FILE_OP_ERROR(file, "fclose");
151                 g_free(file);
152                 g_free(file_new);
153                 return;
154         }
155
156         if (rename_force(file, file_new) < 0) {
157                 FILE_OP_ERROR(file, "rename_force");
158         }
159
160         g_free(file);
161         g_free(file_new);
162 }
163
164 gint tags_add_tag(const gchar *tag)
165 {
166         if (!tag || !(*tag))
167                 return -1;
168
169         if (g_hash_table_lookup(tags_reverse_table, tag))
170                 return -1;
171
172         if (IS_NOT_RESERVED_TAG(tag)) {
173                 tag_max_id++;
174                 g_hash_table_insert(tags_table, GINT_TO_POINTER(tag_max_id), 
175                         g_strdup(tag));
176                 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
177                         GINT_TO_POINTER(tag_max_id));
178
179                 return tag_max_id;
180         } else {
181                 return -1;
182         }
183 }
184
185 void tags_remove_tag(gint id)
186 {
187         gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
188
189         if (old_tag) {
190                 g_hash_table_remove(tags_reverse_table, old_tag);
191         }
192         g_hash_table_remove(tags_table, GINT_TO_POINTER(id));
193 }
194
195 /* extern decl. to avoid including ../prefs_filtering.h */
196 extern void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag);
197
198 void tags_update_tag(gint id, const gchar *tag)
199 {
200         gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
201
202         if (IS_NOT_RESERVED_TAG(tag)) {
203                 if (old_tag) {
204                         prefs_filtering_rename_tag(old_tag, tag);
205                         g_hash_table_remove(tags_reverse_table, old_tag);
206                 }
207
208                 g_hash_table_replace(tags_table, GINT_TO_POINTER(id), 
209                         g_strdup(tag));
210                 g_hash_table_insert(tags_reverse_table, g_strdup(tag),
211                         GINT_TO_POINTER(id));
212         }
213 }
214
215 const gchar *tags_get_tag(gint id)
216 {
217         return (const gchar *)g_hash_table_lookup(tags_table,
218                                 GINT_TO_POINTER(id));
219 }
220
221 gint tags_get_id_for_str(const gchar *str)
222 {
223         gpointer id_ptr;
224         if ((id_ptr = g_hash_table_lookup(tags_reverse_table, str)) != NULL)
225                 return GPOINTER_TO_INT(id_ptr);
226         else
227                 return -1;
228 }
229
230 typedef struct _TagListData {
231         GSList *list;
232 } TagListData;
233
234 static void tag_add_list(gpointer key, gpointer value, gpointer user_data)
235 {
236         TagListData *data = (TagListData *)user_data;
237
238         data->list = g_slist_prepend(data->list, GINT_TO_POINTER(key));
239 }
240
241 GSList *tags_get_list(void)
242 {
243         TagListData data;
244         data.list = NULL;
245
246         g_hash_table_foreach(tags_table, tag_add_list, &data);
247
248         data.list = g_slist_reverse(data.list);
249         
250         return data.list;
251 }
252
253 guint tags_get_size(void)
254 {
255         return g_hash_table_size(tags_table);
256 }