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