2 * @file gntlog.c GNT Log viewer
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
31 #include <gntbutton.h>
34 #include <gnttextview.h>
36 #include <gntwindow.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
{
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
;
82 if (a
->contact
!= NULL
) {
83 if (b
->contact
!= NULL
)
84 return (a
->contact
== b
->contact
);
88 if (b
->contact
!= NULL
)
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
));
104 static const char *log_get_date(PurpleLog
*log
)
107 return purple_date_format_full(log
->tm
);
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
));
117 if (!(*search_term
)) {
119 gnt_tree_remove_all(GNT_TREE(lv
->tree
));
122 populate_log_tree(lv
);
126 if (lv
->search
!= NULL
&& !strcmp(lv
->search
, search_term
)) {
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
),
143 gnt_tree_create_row(GNT_TREE(lv
->tree
), log_get_date(log
)),
151 static void destroy_cb(GntWidget
*w
, struct log_viewer_hash_t
*ht
)
153 FinchLogViewer
*lv
= syslog_viewer
;
156 lv
= g_hash_table_lookup(log_viewers
, ht
);
157 g_hash_table_remove(log_viewers
, ht
);
159 g_free(ht
->username
);
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
);
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))
185 log
= (PurpleLog
*)new;
190 if (log
->type
!= PURPLE_LOG_SYSTEM
) {
192 if (log
->type
== PURPLE_LOG_CHAT
)
193 title
= g_strdup_printf(_("Conversation in %s on %s"),
194 log
->name
, log_get_date(log
));
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
);
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
);
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
);
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 */
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
);
245 gnt_tree_add_row_last(GNT_TREE(lv
->tree
),
247 gnt_tree_create_row(GNT_TREE(lv
->tree
), month
),
249 gnt_tree_set_expanded(GNT_TREE(lv
->tree
), month
, FALSE
);
251 strncpy(prev_top_month
, month
, sizeof(prev_top_month
));
255 gnt_tree_add_row_last(GNT_TREE(lv
->tree
),
257 gnt_tree_create_row(GNT_TREE(lv
->tree
), log_get_date(log
)),
264 static FinchLogViewer
*display_log_viewer(struct log_viewer_hash_t
*ht
, GList
*logs
,
265 const char *title
, int log_size
)
269 GntWidget
*vbox
, *hbox
;
270 GntWidget
*size_label
;
274 /* No logs were found. */
275 const char *log_preferences
= 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.");
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
);
292 purple_notify_info(NULL
, title
, _("No logs were found"), log_preferences
);
296 lv
= g_new0(FinchLogViewer
, 1);
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
);
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
),
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 ************/
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
);
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
);
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
)
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
;
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);
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
);
398 if (type
== PURPLE_LOG_CHAT
) {
401 chat
= purple_blist_find_chat(account
, username
);
403 name
= purple_chat_get_name(chat
);
405 title
= g_strdup_printf(_("Conversations in %s"), name
);
410 buddy
= purple_find_buddy(account
, username
);
412 name
= purple_buddy_get_contact_alias(buddy
);
413 title
= g_strdup_printf(_("Conversations with %s"), name
);
415 title
= g_strdup(_("All Conversations"));
420 logs
= purple_log_get_logs(type
, username
, account
);
421 size
= purple_log_get_total_size(type
, username
, account
);
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
);
431 display_log_viewer(ht
, logs
, title
, size
);
436 void finch_log_show_contact(PurpleContact
*contact
)
438 struct log_viewer_hash_t
*ht
;
439 PurpleBlistNode
*child
;
440 FinchLogViewer
*lv
= NULL
;
442 const char *name
= NULL
;
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
);
460 for (child
= purple_blist_node_get_first_child((PurpleBlistNode
*)contact
); child
;
461 child
= purple_blist_node_get_sibling_next(child
)) {
463 PurpleAccount
*account
;
464 if (!PURPLE_BLIST_NODE_IS_BUDDY(child
))
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
,
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
);
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. */
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
);
490 title
= g_strdup_printf(_("Conversations with %s"), name
);
491 display_log_viewer(ht
, logs
, title
, total_log_size
);
495 void finch_syslog_show()
497 GList
*accounts
= NULL
;
500 if (syslog_viewer
!= NULL
) {
501 gnt_window_present(syslog_viewer
->window
);
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
)
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 ****************************************************************************/
523 finch_log_get_handle(void)
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
,
537 purple_value_new(PURPLE_TYPE_BOXED
,
539 purple_value_new(PURPLE_TYPE_SUBTYPE
,
540 PURPLE_SUBTYPE_LOG
));
544 finch_log_uninit(void)
546 purple_signals_unregister_by_instance(finch_log_get_handle());