2007-07-02 [colin] 2.10.0cvs1
[claws.git] / src / common / tags.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto & 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 = 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 (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));                
77         }
78         
79         fclose(fp);
80 }
81
82 typedef struct _TagWriteData
83 {
84         FILE *fp;
85         gboolean error;
86 } TagWriteData;
87
88 static void tag_write(gpointer key, gpointer value, gpointer user_data)
89 {
90         TagWriteData *data = (TagWriteData *)user_data;
91         const gchar *str = value;
92         gint id = GPOINTER_TO_INT(key);
93
94         if (data->error)
95                 return;
96                 
97         if (fprintf(data->fp, "%d\t%s\n", id, str) <= 0) {
98                 FILE_OP_ERROR("tagsrc", "fprintf");
99                 data->error = TRUE;
100         }
101 }
102
103 void tags_write_tags(void)
104 {
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,
108                         TAGS_RC, NULL);
109         TagWriteData data;
110
111         FILE *fp = fopen(file, "wb");
112                         
113         if (!fp) {
114                 FILE_OP_ERROR(file, "fopen");
115                 g_free(file);
116                 g_free(file_new);
117                 return;
118         }
119         
120         data.fp = fp;
121         data.error = FALSE;
122
123         if (fprintf(data.fp, "max_id %d\n", tag_max_id) <= 0) {
124                 FILE_OP_ERROR("tagsrc", "fprintf");
125                 data.error = TRUE;
126         } else {
127                 g_hash_table_foreach(tags_table, tag_write, &data);
128         }
129
130         if (data.error) {
131                 g_free(file);
132                 g_free(file_new);
133                 return;
134         }
135         
136         if (fclose(fp) == EOF) {
137                 FILE_OP_ERROR(file, "fclose");
138                 g_free(file);
139                 g_free(file_new);
140                 return;
141         }
142         
143         if (g_rename(file, file_new) < 0) {
144                 FILE_OP_ERROR(file, "g_rename");
145         }
146
147         g_free(file);
148         g_free(file_new);
149 }
150
151 gint tags_add_tag(const gchar *tag)
152 {
153         if (g_hash_table_lookup(tags_reverse_table, tag))
154                 return -1;
155
156         tag_max_id++;
157         g_hash_table_insert(tags_table, GINT_TO_POINTER(tag_max_id), 
158                 g_strdup(tag));
159         g_hash_table_insert(tags_reverse_table, g_strdup(tag),
160                 GINT_TO_POINTER(tag_max_id));
161         
162         return tag_max_id;
163 }
164
165 void tags_remove_tag(gint id)
166 {
167         gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
168
169         if (old_tag) {
170                 g_hash_table_remove(tags_reverse_table, old_tag);
171         }
172         g_hash_table_remove(tags_table, GINT_TO_POINTER(id));
173 }
174
175 void tags_update_tag(gint id, const gchar *tag)
176 {
177         gchar *old_tag = g_hash_table_lookup(tags_table, GINT_TO_POINTER(id));
178
179         if (old_tag) {
180                 g_hash_table_remove(tags_reverse_table, old_tag);
181         }
182
183         g_hash_table_replace(tags_table, GINT_TO_POINTER(id), 
184                 g_strdup(tag));
185         g_hash_table_insert(tags_reverse_table, g_strdup(tag),
186                 GINT_TO_POINTER(id));
187 }
188
189 const gchar *tags_get_tag(gint id)
190 {
191         return (const gchar *)g_hash_table_lookup(tags_table,
192                                 GINT_TO_POINTER(id));
193 }
194
195 gint tags_get_id_for_str(const gchar *str)
196 {
197         gpointer id_ptr;
198         if ((id_ptr = g_hash_table_lookup(tags_reverse_table, str)) != NULL)
199                 return GPOINTER_TO_INT(id_ptr);
200         else
201                 return -1;
202 }
203
204 typedef struct _TagListData {
205         GSList *list;
206 } TagListData;
207
208 static void tag_add_list(gpointer key, gpointer value, gpointer user_data)
209 {
210         TagListData *data = (TagListData *)user_data;
211
212         data->list = g_slist_prepend(data->list, GINT_TO_POINTER(key));
213 }
214
215 GSList *tags_get_list(void)
216 {
217         TagListData data;
218         data.list = NULL;
219
220         g_hash_table_foreach(tags_table, tag_add_list, &data);
221
222         data.list = g_slist_reverse(data.list);
223         
224         return data.list;
225 }
226