merge of '6696045322d43e1d7c9634a33cf10ca2323c061c'
[pidgin-git.git] / finch / gntlog.c
blobf938655097e2ba8c3c86480352fa262c2aff4ae2
1 /**
2 * @file gntlog.c GNT Log viewer
3 * @ingroup finch
4 */
6 /* finch
8 * Finch is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "finch.h"
27 #include <internal.h>
29 #include <gnt.h>
30 #include <gntbox.h>
31 #include <gntbutton.h>
32 #include <gntentry.h>
33 #include <gntlabel.h>
34 #include <gnttextview.h>
35 #include <gnttree.h>
36 #include <gntwindow.h>
38 #include "account.h"
39 #include "debug.h"
40 #include "log.h"
41 #include "notify.h"
42 #include "request.h"
43 #include "util.h"
45 #include "gntlog.h"
47 static GHashTable *log_viewers = NULL;
48 static void populate_log_tree(FinchLogViewer *lv);
49 static FinchLogViewer *syslog_viewer = NULL;
51 struct log_viewer_hash_t {
52 PurpleLogType type;
53 char *username;
54 PurpleAccount *account;
55 PurpleContact *contact;
58 static guint log_viewer_hash(gconstpointer data)
60 const struct log_viewer_hash_t *viewer = data;
62 if (viewer->contact != NULL)
63 return g_direct_hash(viewer->contact);
65 if (viewer->account) {
66 return g_str_hash(viewer->username) +
67 g_str_hash(purple_account_get_username(viewer->account));
70 return g_direct_hash(viewer);
73 static gboolean log_viewer_equal(gconstpointer y, gconstpointer z)
75 const struct log_viewer_hash_t *a, *b;
76 int ret;
77 char *normal;
79 a = y;
80 b = z;
82 if (a->contact != NULL) {
83 if (b->contact != NULL)
84 return (a->contact == b->contact);
85 else
86 return FALSE;
87 } else {
88 if (b->contact != NULL)
89 return FALSE;
92 if (a->username && b->username) {
93 normal = g_strdup(purple_normalize(a->account, a->username));
94 ret = (a->account == b->account) &&
95 !strcmp(normal, purple_normalize(b->account, b->username));
96 g_free(normal);
97 } else {
98 ret = (a == b);
101 return ret;
104 static const char *log_get_date(PurpleLog *log)
106 if (log->tm)
107 return purple_date_format_full(log->tm);
108 else
109 return purple_date_format_full(localtime(&log->time));
112 static void search_cb(GntWidget *button, FinchLogViewer *lv)
114 const char *search_term = gnt_entry_get_text(GNT_ENTRY(lv->entry));
115 GList *logs;
117 if (!(*search_term)) {
118 /* reset the tree */
119 gnt_tree_remove_all(GNT_TREE(lv->tree));
120 g_free(lv->search);
121 lv->search = NULL;
122 populate_log_tree(lv);
123 return;
126 if (lv->search != NULL && !strcmp(lv->search, search_term)) {
127 return;
130 g_free(lv->search);
131 lv->search = g_strdup(search_term);
133 gnt_tree_remove_all(GNT_TREE(lv->tree));
134 gnt_text_view_clear(GNT_TEXT_VIEW(lv->text));
136 for (logs = lv->logs; logs != NULL; logs = logs->next) {
137 char *read = purple_log_read((PurpleLog*)logs->data, NULL);
138 if (read && *read && purple_strcasestr(read, search_term)) {
139 PurpleLog *log = logs->data;
141 gnt_tree_add_row_last(GNT_TREE(lv->tree),
142 log,
143 gnt_tree_create_row(GNT_TREE(lv->tree), log_get_date(log)),
144 NULL);
146 g_free(read);
151 static void destroy_cb(GntWidget *w, struct log_viewer_hash_t *ht)
153 FinchLogViewer *lv = syslog_viewer;
155 if (ht != NULL) {
156 lv = g_hash_table_lookup(log_viewers, ht);
157 g_hash_table_remove(log_viewers, ht);
159 g_free(ht->username);
160 g_free(ht);
161 } else
162 syslog_viewer = NULL;
164 purple_request_close_with_handle(lv);
166 g_list_foreach(lv->logs, (GFunc)purple_log_free, NULL);
167 g_list_free(lv->logs);
169 g_free(lv->search);
170 g_free(lv);
172 gnt_widget_destroy(w);
175 static void log_select_cb(GntWidget *w, gpointer old, gpointer new, FinchLogViewer *viewer)
177 GntTree *tree = GNT_TREE(w);
178 PurpleLog *log = NULL;
179 PurpleLogReadFlags flags;
180 char *read = NULL, *strip, *newline;
182 if (!viewer->search && !gnt_tree_get_parent_key(tree, new))
183 return;
185 log = (PurpleLog *)new;
187 if (log == NULL)
188 return;
190 if (log->type != PURPLE_LOG_SYSTEM) {
191 char *title;
192 if (log->type == PURPLE_LOG_CHAT)
193 title = g_strdup_printf(_("Conversation in %s on %s"),
194 log->name, log_get_date(log));
195 else
196 title = g_strdup_printf(_("Conversation with %s on %s"),
197 log->name, log_get_date(log));
199 gnt_label_set_text(GNT_LABEL(viewer->label), title);
200 g_free(title);
203 read = purple_log_read(log, &flags);
204 if (flags != PURPLE_LOG_READ_NO_NEWLINE) {
205 newline = purple_strdup_withhtml(read);
206 strip = purple_markup_strip_html(newline);
207 g_free(newline);
208 } else {
209 strip = purple_markup_strip_html(read);
211 viewer->flags = flags;
213 purple_signal_emit(finch_log_get_handle(), "log-displaying", viewer, log);
215 gnt_text_view_clear(GNT_TEXT_VIEW(viewer->text));
216 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(viewer->text), strip, GNT_TEXT_FLAG_NORMAL);
217 g_free(read);
218 g_free(strip);
221 /* I want to make this smarter, but haven't come up with a cool algorithm to do so, yet.
222 * I want the tree to be divided into groups like "Today," "Yesterday," "Last week,"
223 * "August," "2002," etc. based on how many conversation took place in each subdivision.
225 * For now, I'll just make it a flat list.
227 static void populate_log_tree(FinchLogViewer *lv)
228 /* Logs are made from trees in real life.
229 This is a tree made from logs */
231 const char *pmonth;
232 char *month = NULL;
233 char prev_top_month[30] = "";
234 GList *logs = lv->logs;
236 while (logs != NULL) {
237 PurpleLog *log = logs->data;
239 pmonth = purple_utf8_strftime(_("%B %Y"),
240 log->tm ? log->tm : localtime(&log->time));
242 if (strcmp(pmonth, prev_top_month) != 0) {
243 month = g_strdup(pmonth);
244 /* top level */
245 gnt_tree_add_row_last(GNT_TREE(lv->tree),
246 month,
247 gnt_tree_create_row(GNT_TREE(lv->tree), month),
248 NULL);
249 gnt_tree_set_expanded(GNT_TREE(lv->tree), month, FALSE);
251 strncpy(prev_top_month, month, sizeof(prev_top_month));
254 /* sub */
255 gnt_tree_add_row_last(GNT_TREE(lv->tree),
256 log,
257 gnt_tree_create_row(GNT_TREE(lv->tree), log_get_date(log)),
258 month);
260 logs = logs->next;
264 static FinchLogViewer *display_log_viewer(struct log_viewer_hash_t *ht, GList *logs,
265 const char *title, int log_size)
267 FinchLogViewer *lv;
268 char *text;
269 GntWidget *vbox, *hbox;
270 GntWidget *size_label;
272 if (logs == NULL)
274 /* No logs were found. */
275 const char *log_preferences = NULL;
277 if (ht == NULL) {
278 if (!purple_prefs_get_bool("/purple/logging/log_system"))
279 log_preferences = _("System events will only be logged if the \"Log all status changes to system log\" preference is enabled.");
280 } else {
281 if (ht->type == PURPLE_LOG_IM) {
282 if (!purple_prefs_get_bool("/purple/logging/log_ims"))
283 log_preferences = _("Instant messages will only be logged if the \"Log all instant messages\" preference is enabled.");
284 } else if (ht->type == PURPLE_LOG_CHAT) {
285 if (!purple_prefs_get_bool("/purple/logging/log_chats"))
286 log_preferences = _("Chats will only be logged if the \"Log all chats\" preference is enabled.");
288 g_free(ht->username);
289 g_free(ht);
292 purple_notify_info(NULL, title, _("No logs were found"), log_preferences);
293 return NULL;
296 lv = g_new0(FinchLogViewer, 1);
297 lv->logs = logs;
299 if (ht != NULL)
300 g_hash_table_insert(log_viewers, ht, lv);
302 /* Window ***********/
303 lv->window = gnt_vwindow_new(FALSE);
304 gnt_box_set_title(GNT_BOX(lv->window), title);
305 gnt_box_set_toplevel(GNT_BOX(lv->window), TRUE);
306 gnt_box_set_pad(GNT_BOX(lv->window), 0);
307 g_signal_connect(G_OBJECT(lv->window), "destroy", G_CALLBACK(destroy_cb), ht);
309 vbox = gnt_vbox_new(FALSE);
310 gnt_box_add_widget(GNT_BOX(lv->window), vbox);
312 /* Label ************/
313 text = g_strdup_printf("%s", title);
314 lv->label = gnt_label_new_with_format(text, GNT_TEXT_FLAG_BOLD);
315 g_free(text);
316 gnt_box_add_widget(GNT_BOX(vbox), lv->label);
318 hbox = gnt_hbox_new(FALSE);
319 gnt_box_add_widget(GNT_BOX(vbox), hbox);
320 /* List *************/
321 lv->tree = gnt_tree_new();
322 gnt_widget_set_size(lv->tree, 30, 0);
323 populate_log_tree(lv);
324 g_signal_connect (G_OBJECT(lv->tree), "selection-changed",
325 G_CALLBACK (log_select_cb),
326 lv);
327 gnt_box_add_widget(GNT_BOX(hbox), lv->tree);
329 /* Viewer ************/
330 lv->text = gnt_text_view_new();
331 gnt_box_add_widget(GNT_BOX(hbox), lv->text);
332 gnt_text_view_set_flag(GNT_TEXT_VIEW(lv->text), GNT_TEXT_VIEW_TOP_ALIGN);
334 hbox = gnt_hbox_new(FALSE);
335 gnt_box_add_widget(GNT_BOX(vbox), hbox);
336 /* Log size ************/
337 if (log_size) {
338 char *sz_txt = purple_str_size_to_units(log_size);
339 text = g_strdup_printf("%s %s", _("Total log size:"), sz_txt);
340 size_label = gnt_label_new(text);
341 gnt_box_add_widget(GNT_BOX(hbox), size_label);
342 g_free(sz_txt);
343 g_free(text);
346 /* Search box **********/
347 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(_("Scroll/Search: ")));
348 lv->entry = gnt_entry_new("");
349 gnt_box_add_widget(GNT_BOX(hbox), lv->entry);
350 g_signal_connect(GNT_ENTRY(lv->entry), "activate", G_CALLBACK(search_cb), lv);
352 gnt_text_view_attach_scroll_widget(GNT_TEXT_VIEW(lv->text), lv->entry);
353 gnt_text_view_attach_pager_widget(GNT_TEXT_VIEW(lv->text), lv->entry);
355 gnt_widget_show(lv->window);
357 return lv;
360 static void
361 our_logging_blows(PurpleLogSet *set, PurpleLogSet *setagain, GList **list)
363 /* The iteration happens on the first list. So we use the shorter list in front */
364 if (set->type != PURPLE_LOG_IM)
365 return;
366 *list = g_list_concat(purple_log_get_logs(PURPLE_LOG_IM, set->name, set->account), *list);
369 void finch_log_show(PurpleLogType type, const char *username, PurpleAccount *account)
371 struct log_viewer_hash_t *ht;
372 FinchLogViewer *lv = NULL;
373 const char *name = username;
374 char *title;
375 GList *logs = NULL;
376 int size = 0;
378 if (type != PURPLE_LOG_IM) {
379 g_return_if_fail(account != NULL);
380 g_return_if_fail(username != NULL);
383 ht = g_new0(struct log_viewer_hash_t, 1);
385 ht->type = type;
386 ht->username = g_strdup(username);
387 ht->account = account;
389 if (log_viewers == NULL) {
390 log_viewers = g_hash_table_new(log_viewer_hash, log_viewer_equal);
391 } else if ((lv = g_hash_table_lookup(log_viewers, ht))) {
392 gnt_window_present(lv->window);
393 g_free(ht->username);
394 g_free(ht);
395 return;
398 if (type == PURPLE_LOG_CHAT) {
399 PurpleChat *chat;
401 chat = purple_blist_find_chat(account, username);
402 if (chat != NULL)
403 name = purple_chat_get_name(chat);
405 title = g_strdup_printf(_("Conversations in %s"), name);
406 } else {
407 PurpleBuddy *buddy;
409 if (username) {
410 buddy = purple_find_buddy(account, username);
411 if (buddy != NULL)
412 name = purple_buddy_get_contact_alias(buddy);
413 title = g_strdup_printf(_("Conversations with %s"), name);
414 } else {
415 title = g_strdup(_("All Conversations"));
419 if (username) {
420 logs = purple_log_get_logs(type, username, account);
421 size = purple_log_get_total_size(type, username, account);
422 } else {
423 /* This will happen only for IMs */
424 GHashTable *table = purple_log_get_log_sets();
425 g_hash_table_foreach(table, (GHFunc)our_logging_blows, &logs);
426 g_hash_table_destroy(table);
427 logs = g_list_sort(logs, purple_log_compare);
428 size = 0;
431 display_log_viewer(ht, logs, title, size);
433 g_free(title);
436 void finch_log_show_contact(PurpleContact *contact)
438 struct log_viewer_hash_t *ht;
439 PurpleBlistNode *child;
440 FinchLogViewer *lv = NULL;
441 GList *logs = NULL;
442 const char *name = NULL;
443 char *title;
444 int total_log_size = 0;
446 g_return_if_fail(contact != NULL);
448 ht = g_new0(struct log_viewer_hash_t, 1);
449 ht->type = PURPLE_LOG_IM;
450 ht->contact = contact;
452 if (log_viewers == NULL) {
453 log_viewers = g_hash_table_new(log_viewer_hash, log_viewer_equal);
454 } else if ((lv = g_hash_table_lookup(log_viewers, ht))) {
455 gnt_window_present(lv->window);
456 g_free(ht);
457 return;
460 for (child = purple_blist_node_get_first_child((PurpleBlistNode*)contact); child;
461 child = purple_blist_node_get_sibling_next(child)) {
462 const char *name;
463 PurpleAccount *account;
464 if (!PURPLE_BLIST_NODE_IS_BUDDY(child))
465 continue;
467 name = purple_buddy_get_name((PurpleBuddy *)child);
468 account = purple_buddy_get_account((PurpleBuddy *)child);
469 logs = g_list_concat(purple_log_get_logs(PURPLE_LOG_IM, name,
470 account), logs);
471 total_log_size += purple_log_get_total_size(PURPLE_LOG_IM, name, account);
473 logs = g_list_sort(logs, purple_log_compare);
475 name = purple_contact_get_alias(contact);
476 if (!name)
477 name = purple_buddy_get_contact_alias(purple_contact_get_priority_buddy(contact));
479 /* This will happen if the contact doesn't have an alias,
480 * and none of the contact's buddies are online.
481 * There is probably a better way to deal with this. */
482 if (name == NULL) {
483 child = purple_blist_node_get_first_child((PurpleBlistNode*)contact);
484 if (child != NULL && PURPLE_BLIST_NODE_IS_BUDDY(child))
485 name = purple_buddy_get_contact_alias((PurpleBuddy *)child);
486 if (name == NULL)
487 name = "";
490 title = g_strdup_printf(_("Conversations with %s"), name);
491 display_log_viewer(ht, logs, title, total_log_size);
492 g_free(title);
495 void finch_syslog_show()
497 GList *accounts = NULL;
498 GList *logs = NULL;
500 if (syslog_viewer != NULL) {
501 gnt_window_present(syslog_viewer->window);
502 return;
505 for(accounts = purple_accounts_get_all(); accounts != NULL; accounts = accounts->next) {
507 PurpleAccount *account = (PurpleAccount *)accounts->data;
508 if(purple_find_prpl(purple_account_get_protocol_id(account)) == NULL)
509 continue;
511 logs = g_list_concat(purple_log_get_system_logs(account), logs);
513 logs = g_list_sort(logs, purple_log_compare);
515 syslog_viewer = display_log_viewer(NULL, logs, _("System Log"), 0);
518 /****************************************************************************
519 * GNT LOG SUBSYSTEM *******************************************************
520 ****************************************************************************/
522 void *
523 finch_log_get_handle(void)
525 static int handle;
527 return &handle;
530 void finch_log_init(void)
532 void *handle = finch_log_get_handle();
534 purple_signal_register(handle, "log-displaying",
535 purple_marshal_VOID__POINTER_POINTER,
536 NULL, 2,
537 purple_value_new(PURPLE_TYPE_BOXED,
538 "FinchLogViewer *"),
539 purple_value_new(PURPLE_TYPE_SUBTYPE,
540 PURPLE_SUBTYPE_LOG));
543 void
544 finch_log_uninit(void)
546 purple_signals_unregister_by_instance(finch_log_get_handle());