Mercurial > pidgin
comparison libpurple/log.c @ 25919:52b2e7260340
A patch from Chris Connett to change the log size sorting method to weight
logs by date. In other words, to sort them by *recent* activity as opposed
to total log size.
Fixes #5447
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Mon, 26 Jan 2009 04:19:00 +0000 |
parents | a6e3cb32cdd2 |
children | c24032f5bfd7 |
comparison
equal
deleted
inserted
replaced
25918:bc8d1607f9b8 | 25919:52b2e7260340 |
---|---|
32 #include "log.h" | 32 #include "log.h" |
33 #include "prefs.h" | 33 #include "prefs.h" |
34 #include "util.h" | 34 #include "util.h" |
35 #include "stringref.h" | 35 #include "stringref.h" |
36 #include "imgstore.h" | 36 #include "imgstore.h" |
37 #include "time.h" | |
37 | 38 |
38 static GSList *loggers = NULL; | 39 static GSList *loggers = NULL; |
39 | 40 |
40 static PurpleLogLogger *html_logger; | 41 static PurpleLogLogger *html_logger; |
41 static PurpleLogLogger *txt_logger; | 42 static PurpleLogLogger *txt_logger; |
44 struct _purple_logsize_user { | 45 struct _purple_logsize_user { |
45 char *name; | 46 char *name; |
46 PurpleAccount *account; | 47 PurpleAccount *account; |
47 }; | 48 }; |
48 static GHashTable *logsize_users = NULL; | 49 static GHashTable *logsize_users = NULL; |
50 static GHashTable *logsize_users_decayed = NULL; | |
49 | 51 |
50 static void log_get_log_sets_common(GHashTable *sets); | 52 static void log_get_log_sets_common(GHashTable *sets); |
51 | 53 |
52 static gsize html_logger_write(PurpleLog *log, PurpleMessageFlags type, | 54 static gsize html_logger_write(PurpleLog *log, PurpleMessageFlags type, |
53 const char *from, time_t time, const char *message); | 55 const char *from, time_t time, const char *message); |
159 | 161 |
160 lu->name = g_strdup(purple_normalize(log->account, log->name)); | 162 lu->name = g_strdup(purple_normalize(log->account, log->name)); |
161 lu->account = log->account; | 163 lu->account = log->account; |
162 | 164 |
163 if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) { | 165 if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) { |
166 char *tmp = lu->name; | |
167 | |
164 total = GPOINTER_TO_INT(ptrsize); | 168 total = GPOINTER_TO_INT(ptrsize); |
165 total += written; | 169 total += written; |
166 g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(total)); | 170 g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(total)); |
171 | |
172 /* The hash table takes ownership of lu, so create a new one | |
173 * for the logsize_users_decayed check below. */ | |
174 lu = g_new(struct _purple_logsize_user, 1); | |
175 lu->name = g_strdup(tmp); | |
176 lu->account = log->account; | |
177 } | |
178 | |
179 if(g_hash_table_lookup_extended(logsize_users_decayed, lu, NULL, &ptrsize)) { | |
180 total = GPOINTER_TO_INT(ptrsize); | |
181 total += written; | |
182 g_hash_table_replace(logsize_users_decayed, lu, GINT_TO_POINTER(total)); | |
167 } else { | 183 } else { |
168 g_free(lu->name); | 184 g_free(lu->name); |
169 g_free(lu); | 185 g_free(lu); |
170 } | 186 } |
171 | |
172 } | 187 } |
173 | 188 |
174 char *purple_log_read(PurpleLog *log, PurpleLogReadFlags *flags) | 189 char *purple_log_read(PurpleLog *log, PurpleLogReadFlags *flags) |
175 { | 190 { |
176 PurpleLogReadFlags mflags; | 191 PurpleLogReadFlags mflags; |
246 } | 261 } |
247 | 262 |
248 g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(size)); | 263 g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(size)); |
249 } | 264 } |
250 return size; | 265 return size; |
266 } | |
267 | |
268 gint purple_log_get_activity_score(PurpleLogType type, const char *name, PurpleAccount *account) | |
269 { | |
270 gpointer ptrscore; | |
271 int score; | |
272 GSList *n; | |
273 struct _purple_logsize_user *lu; | |
274 time_t now; | |
275 time(&now); | |
276 | |
277 lu = g_new(struct _purple_logsize_user, 1); | |
278 lu->name = g_strdup(purple_normalize(account, name)); | |
279 lu->account = account; | |
280 | |
281 if(g_hash_table_lookup_extended(logsize_users_decayed, lu, NULL, &ptrscore)) { | |
282 score = GPOINTER_TO_INT(ptrscore); | |
283 g_free(lu->name); | |
284 g_free(lu); | |
285 } else { | |
286 double score_double = 0.0; | |
287 for (n = loggers; n; n = n->next) { | |
288 PurpleLogLogger *logger = n->data; | |
289 | |
290 if(logger->list) { | |
291 GList *logs = (logger->list)(type, name, account); | |
292 | |
293 while (logs) { | |
294 PurpleLog *log = (PurpleLog*)(logs->data); | |
295 /* Activity score counts bytes in the log, exponentially | |
296 decayed with a half-life of 14 days. */ | |
297 score_double += purple_log_get_size(log) * | |
298 pow(0.5, difftime(now, log->time)/1209600.0); | |
299 purple_log_free(log); | |
300 logs = g_list_delete_link(logs, logs); | |
301 } | |
302 } | |
303 } | |
304 | |
305 score = (gint)score_double; | |
306 g_hash_table_replace(logsize_users_decayed, lu, GINT_TO_POINTER(score)); | |
307 } | |
308 return score; | |
251 } | 309 } |
252 | 310 |
253 gboolean purple_log_is_deletable(PurpleLog *log) | 311 gboolean purple_log_is_deletable(PurpleLog *log) |
254 { | 312 { |
255 g_return_val_if_fail(log != NULL, FALSE); | 313 g_return_val_if_fail(log != NULL, FALSE); |
659 purple_prefs_trigger_callback("/purple/logging/format"); | 717 purple_prefs_trigger_callback("/purple/logging/format"); |
660 | 718 |
661 logsize_users = g_hash_table_new_full((GHashFunc)_purple_logsize_user_hash, | 719 logsize_users = g_hash_table_new_full((GHashFunc)_purple_logsize_user_hash, |
662 (GEqualFunc)_purple_logsize_user_equal, | 720 (GEqualFunc)_purple_logsize_user_equal, |
663 (GDestroyNotify)_purple_logsize_user_free_key, NULL); | 721 (GDestroyNotify)_purple_logsize_user_free_key, NULL); |
722 logsize_users_decayed = g_hash_table_new_full((GHashFunc)_purple_logsize_user_hash, | |
723 (GEqualFunc)_purple_logsize_user_equal, | |
724 (GDestroyNotify)_purple_logsize_user_free_key, NULL); | |
664 } | 725 } |
665 | 726 |
666 void | 727 void |
667 purple_log_uninit(void) | 728 purple_log_uninit(void) |
668 { | 729 { |
677 txt_logger = NULL; | 738 txt_logger = NULL; |
678 | 739 |
679 purple_log_logger_remove(old_logger); | 740 purple_log_logger_remove(old_logger); |
680 purple_log_logger_free(old_logger); | 741 purple_log_logger_free(old_logger); |
681 old_logger = NULL; | 742 old_logger = NULL; |
743 | |
744 g_hash_table_destroy(logsize_users); | |
745 g_hash_table_destroy(logsize_users_decayed); | |
682 } | 746 } |
683 | 747 |
684 /**************************************************************************** | 748 /**************************************************************************** |
685 * LOGGERS ****************************************************************** | 749 * LOGGERS ****************************************************************** |
686 ****************************************************************************/ | 750 ****************************************************************************/ |