2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2014-2015 Ricardo Mones and 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/>.
23 #include "libravatar_missing.h"
24 #include "libravatar_prefs.h"
26 #include "file-utils.h"
29 * Loads the hash table of md5sum → time from the given filename.
31 * @param filename Name of the hash table filename.
33 * @return A hash table with the entries not expired contained in
34 * the given filename or NULL if failed to load.
36 GHashTable
*missing_load_from_file(const gchar
*filename
)
38 FILE *file
= claws_fopen(filename
, "r");
42 GHashTable
*table
= NULL
;
43 int r
= 0, a
= 0, d
= 0;
46 if (!is_file_exist(filename
)) { /* first run, return an empty table */
47 return g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
49 g_warning("cannot open '%s' for reading", filename
);
53 if (t
== (time_t)-1) {
54 g_warning("cannot get time!");
58 table
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
60 while ((r
= fscanf(file
, "%32s %" CM_TIME_FORMAT
"\n", md5sum
, &seen
)) != EOF
) {
61 if (t
- seen
<= LIBRAVATAR_MISSING_TIME
) {
62 time_t *value
= g_malloc0(sizeof(time_t));
64 g_hash_table_insert(table
, g_strdup(md5sum
), value
);
71 if (claws_fclose(file
) != 0)
72 g_warning("error closing '%s'", filename
);
74 debug_print("Read %d missing avatar entries, %d obsolete entries discarded\n", a
, d
);
79 * Saves a hash table item.
81 * @param key Hash table key, a md5sum string.
82 * @param vakue Hash table value, a time_t.
83 * @param data User data, a pointer to the open FILE being written.
85 static void missing_save_item(gpointer key
, gpointer value
, gpointer data
)
87 FILE *file
= (FILE *)data
;
88 gchar
*line
= g_strdup_printf("%s %"CM_TIME_FORMAT
"\n", (gchar
*)key
, *(time_t *)value
);
89 if (claws_fputs(line
, file
) < 0)
90 g_warning("error saving missing item");
95 * Saves a hash table of md5sum → time to a given file name.
97 * @param table The table to save.
98 * @param filename The name of the file where table data will be saved.
100 * @return 0 on success, -1 if there was some problem saving.
102 gint
missing_save_to_file(GHashTable
*table
, const gchar
*filename
)
104 FILE *file
= claws_fopen(filename
, "w");
107 g_warning("cannot open '%s' for writing", filename
);
111 g_hash_table_foreach(table
, missing_save_item
, (gpointer
)file
);
112 debug_print("Saved %u missing avatar entries\n", g_hash_table_size(table
));
114 if (claws_safe_fclose(file
) != 0) {
115 g_warning("error closing '%s'", filename
);
123 * Adds a md5sum to a md5sum → time hash table.
124 * If the md5sum is already in the table its time is updated.
126 * @param table The table to use.
127 * @param md5 The md5sum to add or update.
129 void missing_add_md5(GHashTable
*table
, const gchar
*md5
)
131 time_t t
= time(NULL
);
133 if (t
== (time_t)-1) {
134 g_warning("cannot get time!");
138 time_t *seen
= g_hash_table_lookup(table
, md5
);
140 seen
= g_malloc0(sizeof(time_t));
142 g_hash_table_insert(table
, g_strdup(md5
), seen
);
143 debug_print("New md5 %s added with time %"CM_TIME_FORMAT
"\n", md5
, t
);
145 *seen
= t
; /* just update */
146 debug_print("Updated md5 %s with time %"CM_TIME_FORMAT
"\n", md5
, t
);
151 * Check if a md5sum is in hash table and not expired.
153 * @param table The table to check against.
154 * @param md5 The md5sum to check.
156 * @return TRUE if the md5sum is in the table and is not expired,
159 gboolean
is_missing_md5(GHashTable
*table
, const gchar
*md5
)
162 time_t *seen
= (time_t *)g_hash_table_lookup(table
, md5
);
168 if (t
!= (time_t)-1) {
169 if (t
- *seen
<= LIBRAVATAR_MISSING_TIME
) {
170 debug_print("Found missing md5 %s\n", md5
);