mark PurpleImageClass as private
[pidgin-git.git] / finch / gntlog.c
blob071673976d79b3b21e99bc1e7aafdd16ba9d68a2
1 /* finch
3 * Finch is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include <internal.h>
22 #include "finch.h"
24 #include <gnt.h>
25 #include <gntbox.h>
26 #include <gntbutton.h>
27 #include <gntentry.h>
28 #include <gntlabel.h>
29 #include <gnttextview.h>
30 #include <gnttree.h>
31 #include <gntwindow.h>
33 #include "account.h"
34 #include "debug.h"
35 #include "log.h"
36 #include "notify.h"
37 #include "request.h"
38 #include "util.h"
40 #include "gntlog.h"
42 static GHashTable *log_viewers = NULL;
43 static void populate_log_tree(FinchLogViewer *lv);
44 static FinchLogViewer *syslog_viewer = NULL;
46 struct log_viewer_hash {
47 PurpleLogType type;
48 char *username;
49 PurpleAccount *account;
50 PurpleContact *contact;
53 static guint log_viewer_hash(gconstpointer data)
55 const struct log_viewer_hash *viewer = data;
57 if (viewer->contact != NULL)
58 return g_direct_hash(viewer->contact);
60 if (viewer->account) {
61 return g_str_hash(viewer->username) +
62 g_str_hash(purple_account_get_username(viewer->account));
65 return g_direct_hash(viewer);
68 static gboolean log_viewer_equal(gconstpointer y, gconstpointer z)
70 const struct log_viewer_hash *a, *b;
71 int ret;
72 char *normal;
74 a = y;
75 b = z;
77 if (a->contact != NULL) {
78 if (b->contact != NULL)
79 return (a->contact == b->contact);
80 else
81 return FALSE;
82 } else {
83 if (b->contact != NULL)
84 return FALSE;
87 if (a->username && b->username) {
88 normal = g_strdup(purple_normalize(a->account, a->username));
89 ret = (a->account == b->account) &&
90 purple_strequal(normal, purple_normalize(b->account, b->username));
91 g_free(normal);
92 } else {
93 ret = (a == b);
96 return ret;
99 static gchar *log_get_date(PurpleLog *log)
101 GDateTime *dt;
102 gchar *ret;
103 dt = g_date_time_to_local(log->time);
104 ret = g_date_time_format(dt, "%c");
105 g_date_time_unref(dt);
106 return ret;
109 static void search_cb(GntWidget *button, FinchLogViewer *lv)
111 const char *search_term = gnt_entry_get_text(GNT_ENTRY(lv->entry));
112 GList *logs;
114 if (!(*search_term)) {
115 /* reset the tree */
116 gnt_tree_remove_all(GNT_TREE(lv->tree));
117 g_free(lv->search);
118 lv->search = NULL;
119 populate_log_tree(lv);
120 return;
123 if (lv->search != NULL && purple_strequal(lv->search, search_term)) {
124 return;
127 g_free(lv->search);
128 lv->search = g_strdup(search_term);
130 gnt_tree_remove_all(GNT_TREE(lv->tree));
131 gnt_text_view_clear(GNT_TEXT_VIEW(lv->text));
133 for (logs = lv->logs; logs != NULL; logs = logs->next) {
134 char *read = purple_log_read((PurpleLog*)logs->data, NULL);
135 if (read && *read && purple_strcasestr(read, search_term)) {
136 PurpleLog *log = logs->data;
137 gchar *log_date = log_get_date(log);
139 gnt_tree_add_row_last(GNT_TREE(lv->tree),
140 log,
141 gnt_tree_create_row(GNT_TREE(lv->tree), log_date),
142 NULL);
143 g_free(log_date);
145 g_free(read);
150 static void
151 destroy_cb(GntWidget *w, struct log_viewer_hash *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 gchar *log_date = log_get_date(log);
192 char *title;
193 if (log->type == PURPLE_LOG_CHAT)
194 title = g_strdup_printf(_("Conversation in %s on %s"),
195 log->name, log_date);
196 else
197 title = g_strdup_printf(_("Conversation with %s on %s"),
198 log->name, log_date);
200 gnt_label_set_text(GNT_LABEL(viewer->label), title);
201 g_free(log_date);
202 g_free(title);
205 read = purple_log_read(log, &flags);
206 if (flags != PURPLE_LOG_READ_NO_NEWLINE) {
207 newline = purple_strdup_withhtml(read);
208 strip = purple_markup_strip_html(newline);
209 g_free(newline);
210 } else {
211 strip = purple_markup_strip_html(read);
213 viewer->flags = flags;
215 purple_signal_emit(finch_log_get_handle(), "log-displaying", viewer, log);
217 gnt_text_view_clear(GNT_TEXT_VIEW(viewer->text));
218 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(viewer->text), strip, GNT_TEXT_FLAG_NORMAL);
219 g_free(read);
220 g_free(strip);
223 /* I want to make this smarter, but haven't come up with a cool algorithm to do so, yet.
224 * I want the tree to be divided into groups like "Today," "Yesterday," "Last week,"
225 * "August," "2002," etc. based on how many conversation took place in each subdivision.
227 * For now, I'll just make it a flat list.
229 static void populate_log_tree(FinchLogViewer *lv)
230 /* Logs are made from trees in real life.
231 This is a tree made from logs */
233 gchar *pmonth;
234 gchar *month = NULL;
235 char prev_top_month[30] = "";
236 GList *logs = lv->logs;
238 while (logs != NULL) {
239 PurpleLog *log = logs->data;
240 GDateTime *dt;
241 gchar *log_date;
243 dt = g_date_time_to_local(log->time);
244 pmonth = g_date_time_format(dt, _("%B %Y"));
246 if (!purple_strequal(pmonth, prev_top_month)) {
247 month = g_strdup(pmonth);
248 /* top level */
249 gnt_tree_add_row_last(GNT_TREE(lv->tree),
250 month,
251 gnt_tree_create_row(GNT_TREE(lv->tree), month),
252 NULL);
253 gnt_tree_set_expanded(GNT_TREE(lv->tree), month, FALSE);
255 g_strlcpy(prev_top_month, month, sizeof(prev_top_month));
258 /* sub */
259 log_date = g_date_time_format(dt, "%c");
260 gnt_tree_add_row_last(GNT_TREE(lv->tree),
261 log,
262 gnt_tree_create_row(GNT_TREE(lv->tree), log_date),
263 month);
265 g_free(log_date);
266 g_free(pmonth);
267 g_date_time_unref(dt);
268 logs = logs->next;
272 static FinchLogViewer *
273 display_log_viewer(struct log_viewer_hash *ht, GList *logs, const char *title,
274 int log_size)
276 FinchLogViewer *lv;
277 char *text;
278 GntWidget *vbox, *hbox;
279 GntWidget *size_label;
281 if (logs == NULL)
283 /* No logs were found. */
284 const char *log_preferences = NULL;
286 if (ht == NULL) {
287 if (!purple_prefs_get_bool("/purple/logging/log_system"))
288 log_preferences = _("System events will only be logged if the \"Log all status changes to system log\" preference is enabled.");
289 } else {
290 if (ht->type == PURPLE_LOG_IM) {
291 if (!purple_prefs_get_bool("/purple/logging/log_ims"))
292 log_preferences = _("Instant messages will only be logged if the \"Log all instant messages\" preference is enabled.");
293 } else if (ht->type == PURPLE_LOG_CHAT) {
294 if (!purple_prefs_get_bool("/purple/logging/log_chats"))
295 log_preferences = _("Chats will only be logged if the \"Log all chats\" preference is enabled.");
297 g_free(ht->username);
298 g_free(ht);
301 purple_notify_info(NULL, title, _("No logs were found"), log_preferences, NULL);
302 return NULL;
305 lv = g_new0(FinchLogViewer, 1);
306 lv->logs = logs;
308 if (ht != NULL)
309 g_hash_table_insert(log_viewers, ht, lv);
311 /* Window ***********/
312 lv->window = gnt_vwindow_new(FALSE);
313 gnt_box_set_title(GNT_BOX(lv->window), title);
314 gnt_box_set_toplevel(GNT_BOX(lv->window), TRUE);
315 gnt_box_set_pad(GNT_BOX(lv->window), 0);
316 g_signal_connect(G_OBJECT(lv->window), "destroy", G_CALLBACK(destroy_cb), ht);
318 vbox = gnt_vbox_new(FALSE);
319 gnt_box_add_widget(GNT_BOX(lv->window), vbox);
321 /* Label ************/
322 text = g_strdup_printf("%s", title);
323 lv->label = gnt_label_new_with_format(text, GNT_TEXT_FLAG_BOLD);
324 g_free(text);
325 gnt_box_add_widget(GNT_BOX(vbox), lv->label);
327 hbox = gnt_hbox_new(FALSE);
328 gnt_box_add_widget(GNT_BOX(vbox), hbox);
329 /* List *************/
330 lv->tree = gnt_tree_new();
331 gnt_widget_set_size(lv->tree, 30, 0);
332 populate_log_tree(lv);
333 g_signal_connect (G_OBJECT(lv->tree), "selection-changed",
334 G_CALLBACK (log_select_cb),
335 lv);
336 gnt_box_add_widget(GNT_BOX(hbox), lv->tree);
338 /* Viewer ************/
339 lv->text = gnt_text_view_new();
340 gnt_box_add_widget(GNT_BOX(hbox), lv->text);
341 gnt_text_view_set_flag(GNT_TEXT_VIEW(lv->text), GNT_TEXT_VIEW_TOP_ALIGN);
343 hbox = gnt_hbox_new(FALSE);
344 gnt_box_add_widget(GNT_BOX(vbox), hbox);
345 /* Log size ************/
346 if (log_size) {
347 char *sz_txt = g_format_size(log_size);
348 text = g_strdup_printf("%s %s", _("Total log size:"), sz_txt);
349 size_label = gnt_label_new(text);
350 gnt_box_add_widget(GNT_BOX(hbox), size_label);
351 g_free(sz_txt);
352 g_free(text);
355 /* Search box **********/
356 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(_("Scroll/Search: ")));
357 lv->entry = gnt_entry_new("");
358 gnt_box_add_widget(GNT_BOX(hbox), lv->entry);
359 g_signal_connect(GNT_ENTRY(lv->entry), "activate", G_CALLBACK(search_cb), lv);
361 gnt_text_view_attach_scroll_widget(GNT_TEXT_VIEW(lv->text), lv->entry);
362 gnt_text_view_attach_pager_widget(GNT_TEXT_VIEW(lv->text), lv->entry);
364 gnt_widget_show(lv->window);
366 return lv;
369 static void
370 our_logging_blows(PurpleLogSet *set, PurpleLogSet *setagain, GList **list)
372 /* The iteration happens on the first list. So we use the shorter list in front */
373 if (set->type != PURPLE_LOG_IM)
374 return;
375 *list = g_list_concat(purple_log_get_logs(PURPLE_LOG_IM, set->name, set->account), *list);
378 void finch_log_show(PurpleLogType type, const char *username, PurpleAccount *account)
380 struct log_viewer_hash *ht;
381 FinchLogViewer *lv = NULL;
382 const char *name = username;
383 char *title;
384 GList *logs = NULL;
385 int size = 0;
387 if (type != PURPLE_LOG_IM) {
388 g_return_if_fail(account != NULL);
389 g_return_if_fail(username != NULL);
392 ht = g_new0(struct log_viewer_hash, 1);
394 ht->type = type;
395 ht->username = g_strdup(username);
396 ht->account = account;
398 if (log_viewers == NULL) {
399 log_viewers = g_hash_table_new(log_viewer_hash, log_viewer_equal);
400 } else if ((lv = g_hash_table_lookup(log_viewers, ht))) {
401 gnt_window_present(lv->window);
402 g_free(ht->username);
403 g_free(ht);
404 return;
407 if (type == PURPLE_LOG_CHAT) {
408 PurpleChat *chat;
410 chat = purple_blist_find_chat(account, username);
411 if (chat != NULL)
412 name = purple_chat_get_name(chat);
414 title = g_strdup_printf(_("Conversations in %s"), name);
415 } else {
416 PurpleBuddy *buddy;
418 if (username) {
419 buddy = purple_blist_find_buddy(account, username);
420 if (buddy != NULL)
421 name = purple_buddy_get_contact_alias(buddy);
422 title = g_strdup_printf(_("Conversations with %s"), name);
423 } else {
424 title = g_strdup(_("All Conversations"));
428 if (username) {
429 logs = purple_log_get_logs(type, username, account);
430 size = purple_log_get_total_size(type, username, account);
431 } else {
432 /* This will happen only for IMs */
433 GHashTable *table = purple_log_get_log_sets();
434 g_hash_table_foreach(table, (GHFunc)our_logging_blows, &logs);
435 g_hash_table_destroy(table);
436 logs = g_list_sort(logs, purple_log_compare);
437 size = 0;
440 display_log_viewer(ht, logs, title, size);
442 g_free(title);
445 void finch_log_show_contact(PurpleContact *contact)
447 struct log_viewer_hash *ht;
448 PurpleBlistNode *child;
449 FinchLogViewer *lv = NULL;
450 GList *logs = NULL;
451 const char *name = NULL;
452 char *title;
453 int total_log_size = 0;
455 g_return_if_fail(contact != NULL);
457 ht = g_new0(struct log_viewer_hash, 1);
458 ht->type = PURPLE_LOG_IM;
459 ht->contact = contact;
461 if (log_viewers == NULL) {
462 log_viewers = g_hash_table_new(log_viewer_hash, log_viewer_equal);
463 } else if ((lv = g_hash_table_lookup(log_viewers, ht))) {
464 gnt_window_present(lv->window);
465 g_free(ht);
466 return;
469 for (child = purple_blist_node_get_first_child((PurpleBlistNode*)contact); child;
470 child = purple_blist_node_get_sibling_next(child)) {
471 const char *name;
472 PurpleAccount *account;
473 if (!PURPLE_IS_BUDDY(child))
474 continue;
476 name = purple_buddy_get_name((PurpleBuddy *)child);
477 account = purple_buddy_get_account((PurpleBuddy *)child);
478 logs = g_list_concat(purple_log_get_logs(PURPLE_LOG_IM, name,
479 account), logs);
480 total_log_size += purple_log_get_total_size(PURPLE_LOG_IM, name, account);
482 logs = g_list_sort(logs, purple_log_compare);
484 name = purple_contact_get_alias(contact);
485 if (!name)
486 name = purple_buddy_get_contact_alias(purple_contact_get_priority_buddy(contact));
488 /* This will happen if the contact doesn't have an alias,
489 * and none of the contact's buddies are online.
490 * There is probably a better way to deal with this. */
491 if (name == NULL) {
492 child = purple_blist_node_get_first_child((PurpleBlistNode*)contact);
493 if (child != NULL && PURPLE_IS_BUDDY(child))
494 name = purple_buddy_get_contact_alias((PurpleBuddy *)child);
495 if (name == NULL)
496 name = "";
499 title = g_strdup_printf(_("Conversations with %s"), name);
500 display_log_viewer(ht, logs, title, total_log_size);
501 g_free(title);
504 void finch_syslog_show()
506 GList *accounts = NULL;
507 GList *logs = NULL;
509 if (syslog_viewer != NULL) {
510 gnt_window_present(syslog_viewer->window);
511 return;
514 for(accounts = purple_accounts_get_all(); accounts != NULL; accounts = accounts->next) {
516 PurpleAccount *account = (PurpleAccount *)accounts->data;
517 if(purple_protocols_find(purple_account_get_protocol_id(account)) == NULL)
518 continue;
520 logs = g_list_concat(purple_log_get_system_logs(account), logs);
522 logs = g_list_sort(logs, purple_log_compare);
524 syslog_viewer = display_log_viewer(NULL, logs, _("System Log"), 0);
527 /****************************************************************************
528 * GNT LOG SUBSYSTEM *******************************************************
529 ****************************************************************************/
531 void *
532 finch_log_get_handle(void)
534 static int handle;
536 return &handle;
539 void finch_log_init(void)
541 void *handle = finch_log_get_handle();
543 purple_signal_register(handle, "log-displaying",
544 purple_marshal_VOID__POINTER_POINTER,
545 G_TYPE_NONE, 2,
546 G_TYPE_POINTER, /* (FinchLogViewer *) */
547 PURPLE_TYPE_LOG);
550 void
551 finch_log_uninit(void)
553 purple_signals_unregister_by_instance(finch_log_get_handle());