comparison gtk/gtklog.c @ 14191:009db0b357b5

This is a hand-crafted commit to migrate across subversion revisions 16854:16861, due to some vagaries of the way the original renames were done. Witness that monotone can do in one revision what svn had to spread across several.
author Ethan Blanton <elb@pidgin.im>
date Sat, 16 Dec 2006 04:59:55 +0000
parents
children 2ec879353592
comparison
equal deleted inserted replaced
14190:366be2ce35a7 14191:009db0b357b5
1 /**
2 * @file gtklog.c GTK+ Log viewer
3 * @ingroup gtkui
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 #include "internal.h"
26 #include "gtkgaim.h"
27
28 #include "account.h"
29 #include "gtkblist.h"
30 #include "gtkimhtml.h"
31 #include "gtklog.h"
32 #include "gtkutils.h"
33 #include "log.h"
34 #include "notify.h"
35 #include "util.h"
36
37 static GHashTable *log_viewers = NULL;
38 static void populate_log_tree(GaimGtkLogViewer *lv);
39 static GaimGtkLogViewer *syslog_viewer = NULL;
40
41 struct log_viewer_hash_t {
42 GaimLogType type;
43 char *screenname;
44 GaimAccount *account;
45 GaimContact *contact;
46 };
47
48 static guint log_viewer_hash(gconstpointer data)
49 {
50 const struct log_viewer_hash_t *viewer = data;
51
52 if (viewer->contact != NULL)
53 return g_direct_hash(viewer->contact);
54
55 return g_str_hash(viewer->screenname) +
56 g_str_hash(gaim_account_get_username(viewer->account));
57 }
58
59 static gboolean log_viewer_equal(gconstpointer y, gconstpointer z)
60 {
61 const struct log_viewer_hash_t *a, *b;
62 int ret;
63 char *normal;
64
65 a = y;
66 b = z;
67
68 if (a->contact != NULL) {
69 if (b->contact != NULL)
70 return (a->contact == b->contact);
71 else
72 return FALSE;
73 } else {
74 if (b->contact != NULL)
75 return FALSE;
76 }
77
78 normal = g_strdup(gaim_normalize(a->account, a->screenname));
79 ret = (a->account == b->account) &&
80 !strcmp(normal, gaim_normalize(b->account, b->screenname));
81 g_free(normal);
82
83 return ret;
84 }
85
86 static void select_first_log(GaimGtkLogViewer *lv)
87 {
88 GtkTreeModel *model;
89 GtkTreeIter iter, it;
90 GtkTreePath *path;
91
92 model = GTK_TREE_MODEL(lv->treestore);
93
94 if (!gtk_tree_model_get_iter_first(model, &iter))
95 return;
96
97 path = gtk_tree_model_get_path(model, &iter);
98 if (gtk_tree_model_iter_children(model, &it, &iter))
99 {
100 gtk_tree_view_expand_row(GTK_TREE_VIEW(lv->treeview), path, TRUE);
101 path = gtk_tree_model_get_path(model, &it);
102 }
103
104 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(lv->treeview)), path);
105
106 gtk_tree_path_free(path);
107 }
108
109 static void search_cb(GtkWidget *button, GaimGtkLogViewer *lv)
110 {
111 const char *search_term = gtk_entry_get_text(GTK_ENTRY(lv->entry));
112 GList *logs;
113
114 g_free(lv->search);
115
116 gtk_tree_store_clear(lv->treestore);
117 if (!(*search_term)) {
118 /* reset the tree */
119 populate_log_tree(lv);
120 lv->search = NULL;
121 gtk_imhtml_search_clear(GTK_IMHTML(lv->imhtml));
122 select_first_log(lv);
123 return;
124 }
125
126 lv->search = g_strdup(search_term);
127 gtk_imhtml_clear(GTK_IMHTML(lv->imhtml));
128
129 gaim_gtk_set_cursor(lv->window, GDK_WATCH);
130
131 for (logs = lv->logs; logs != NULL; logs = logs->next) {
132 char *read = gaim_log_read((GaimLog*)logs->data, NULL);
133 if (read && *read && gaim_strcasestr(read, search_term)) {
134 GtkTreeIter iter;
135 GaimLog *log = logs->data;
136
137 gtk_tree_store_append (lv->treestore, &iter, NULL);
138 gtk_tree_store_set(lv->treestore, &iter,
139 0, gaim_date_format_full(localtime(&log->time)),
140 1, log, -1);
141 }
142 g_free(read);
143 }
144
145 select_first_log(lv);
146 gaim_gtk_clear_cursor(lv->window);
147 }
148
149 static gboolean destroy_cb(GtkWidget *w, gint resp, struct log_viewer_hash_t *ht) {
150 GaimGtkLogViewer *lv = syslog_viewer;
151
152 if (ht != NULL) {
153 lv = g_hash_table_lookup(log_viewers, ht);
154 g_hash_table_remove(log_viewers, ht);
155
156 g_free(ht->screenname);
157 g_free(ht);
158 } else
159 syslog_viewer = NULL;
160
161 g_list_foreach(lv->logs, (GFunc)gaim_log_free, NULL);
162 g_list_free(lv->logs);
163
164 g_free(lv->search);
165 g_free(lv);
166
167 gtk_widget_destroy(w);
168
169 return TRUE;
170 }
171
172 static void log_row_activated_cb(GtkTreeView *tv, GtkTreePath *path, GtkTreeViewColumn *col, GaimGtkLogViewer *viewer) {
173 if (gtk_tree_view_row_expanded(tv, path))
174 gtk_tree_view_collapse_row(tv, path);
175 else
176 gtk_tree_view_expand_row(tv, path, FALSE);
177 }
178
179 static void log_select_cb(GtkTreeSelection *sel, GaimGtkLogViewer *viewer) {
180 GtkTreeIter iter;
181 GValue val;
182 GtkTreeModel *model = GTK_TREE_MODEL(viewer->treestore);
183 GaimLog *log = NULL;
184 GaimLogReadFlags flags;
185 char *read = NULL;
186
187 if (!gtk_tree_selection_get_selected(sel, &model, &iter))
188 return;
189
190 val.g_type = 0;
191 gtk_tree_model_get_value (model, &iter, 1, &val);
192 log = g_value_get_pointer(&val);
193 g_value_unset(&val);
194
195 if (log == NULL)
196 return;
197
198 gaim_gtk_set_cursor(viewer->window, GDK_WATCH);
199
200 if (log->type != GAIM_LOG_SYSTEM) {
201 char *title;
202 if (log->type == GAIM_LOG_CHAT)
203 title = g_strdup_printf(_("<span size='larger' weight='bold'>Conversation in %s on %s</span>"),
204 log->name,
205 log->tm ? gaim_date_format_full(log->tm) :
206 gaim_date_format_full(localtime(&log->time)));
207 else
208 title = g_strdup_printf(_("<span size='larger' weight='bold'>Conversation with %s on %s</span>"),
209 log->name,
210 log->tm ? gaim_date_format_full(log->tm) :
211 gaim_date_format_full(localtime(&log->time)));
212
213 gtk_label_set_markup(GTK_LABEL(viewer->label), title);
214 g_free(title);
215 }
216
217 read = gaim_log_read(log, &flags);
218 viewer->flags = flags;
219
220 gtk_imhtml_clear(GTK_IMHTML(viewer->imhtml));
221 gtk_imhtml_set_protocol_name(GTK_IMHTML(viewer->imhtml),
222 gaim_account_get_protocol_name(log->account));
223
224 gaim_signal_emit(gaim_gtk_log_get_handle(), "log-displaying", viewer, log);
225
226 gtk_imhtml_append_text(GTK_IMHTML(viewer->imhtml), read,
227 GTK_IMHTML_NO_COMMENTS | GTK_IMHTML_NO_TITLE | GTK_IMHTML_NO_SCROLL |
228 ((flags & GAIM_LOG_READ_NO_NEWLINE) ? GTK_IMHTML_NO_NEWLINE : 0));
229 g_free(read);
230
231 if (viewer->search != NULL) {
232 gtk_imhtml_search_clear(GTK_IMHTML(viewer->imhtml));
233 gtk_imhtml_search_find(GTK_IMHTML(viewer->imhtml), viewer->search);
234 }
235
236 gaim_gtk_clear_cursor(viewer->window);
237 }
238
239 /* I want to make this smarter, but haven't come up with a cool algorithm to do so, yet.
240 * I want the tree to be divided into groups like "Today," "Yesterday," "Last week,"
241 * "August," "2002," etc. based on how many conversation took place in each subdivision.
242 *
243 * For now, I'll just make it a flat list.
244 */
245 static void populate_log_tree(GaimGtkLogViewer *lv)
246 /* Logs are made from trees in real life.
247 This is a tree made from logs */
248 {
249 const char *month;
250 char prev_top_month[30] = "";
251 GtkTreeIter toplevel, child;
252 GList *logs = lv->logs;
253
254 while (logs != NULL) {
255 GaimLog *log = logs->data;
256
257 month = gaim_utf8_strftime(_("%B %Y"),
258 log->tm ? log->tm : localtime(&log->time));
259
260 if (strcmp(month, prev_top_month) != 0)
261 {
262 /* top level */
263 gtk_tree_store_append(lv->treestore, &toplevel, NULL);
264 gtk_tree_store_set(lv->treestore, &toplevel, 0, month, 1, NULL, -1);
265
266 strncpy(prev_top_month, month, sizeof(prev_top_month));
267 }
268
269 /* sub */
270 gtk_tree_store_append(lv->treestore, &child, &toplevel);
271 gtk_tree_store_set(lv->treestore, &child,
272 0, log->tm ? gaim_date_format_full(log->tm) : gaim_date_format_full(localtime(&log->time)),
273 1, log,
274 -1);
275
276 logs = logs->next;
277 }
278 }
279
280 static GaimGtkLogViewer *display_log_viewer(struct log_viewer_hash_t *ht, GList *logs,
281 const char *title, GdkPixbuf *pixbuf, int log_size)
282 {
283 GaimGtkLogViewer *lv;
284 GtkWidget *title_box;
285 char *text;
286 GtkWidget *pane;
287 GtkWidget *sw;
288 GtkCellRenderer *rend;
289 GtkTreeViewColumn *col;
290 GtkTreeSelection *sel;
291 GtkWidget *vbox;
292 GtkWidget *frame;
293 GtkWidget *hbox;
294 GtkWidget *button;
295 GtkWidget *size_label;
296
297 if (logs == NULL)
298 {
299 /* No logs were found. */
300 const char *log_preferences = NULL;
301
302 if (ht == NULL) {
303 if (!gaim_prefs_get_bool("/core/logging/log_system"))
304 log_preferences = _("System events will only be logged if the \"Log all status changes to system log\" preference is enabled.");
305 } else {
306 if (ht->type == GAIM_LOG_IM) {
307 if (!gaim_prefs_get_bool("/core/logging/log_ims"))
308 log_preferences = _("Instant messages will only be logged if the \"Log all instant messages\" preference is enabled.");
309 } else if (ht->type == GAIM_LOG_CHAT) {
310 if (!gaim_prefs_get_bool("/core/logging/log_chats"))
311 log_preferences = _("Chats will only be logged if the \"Log all chats\" preference is enabled.");
312 }
313 }
314
315 gaim_notify_info(NULL, title, _("No logs were found"), log_preferences);
316 return NULL;
317 }
318
319 lv = g_new0(GaimGtkLogViewer, 1);
320 lv->logs = logs;
321
322 if (ht != NULL)
323 g_hash_table_insert(log_viewers, ht, lv);
324
325 /* Window ***********/
326 lv->window = gtk_dialog_new_with_buttons(title, NULL, 0,
327 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL);
328 gtk_container_set_border_width (GTK_CONTAINER(lv->window), GAIM_HIG_BOX_SPACE);
329 gtk_dialog_set_has_separator(GTK_DIALOG(lv->window), FALSE);
330 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(lv->window)->vbox), 0);
331 g_signal_connect(G_OBJECT(lv->window), "response",
332 G_CALLBACK(destroy_cb), ht);
333 gtk_window_set_role(GTK_WINDOW(lv->window), "log_viewer");
334
335 /* Icon *************/
336 if (pixbuf != NULL) {
337 GdkPixbuf *scale;
338 GtkWidget *icon;
339
340 title_box = gtk_hbox_new(FALSE, GAIM_HIG_BOX_SPACE);
341 gtk_container_set_border_width(GTK_CONTAINER(title_box), GAIM_HIG_BOX_SPACE);
342 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(lv->window)->vbox), title_box, FALSE, FALSE, 0);
343
344 scale = gdk_pixbuf_scale_simple(pixbuf, 16, 16, GDK_INTERP_BILINEAR);
345 icon = gtk_image_new_from_pixbuf(scale);
346 gtk_box_pack_start(GTK_BOX(title_box), icon, FALSE, FALSE, 0);
347 g_object_unref(G_OBJECT(pixbuf));
348 g_object_unref(G_OBJECT(scale));
349 } else
350 title_box = GTK_DIALOG(lv->window)->vbox;
351
352 /* Label ************/
353 lv->label = gtk_label_new(NULL);
354
355 text = g_strdup_printf("<span size='larger' weight='bold'>%s</span>", title);
356
357 gtk_label_set_markup(GTK_LABEL(lv->label), text);
358 gtk_misc_set_alignment(GTK_MISC(lv->label), 0, 0);
359 gtk_box_pack_start(GTK_BOX(title_box), lv->label, FALSE, FALSE, 0);
360 g_free(text);
361
362 /* Pane *************/
363 pane = gtk_hpaned_new();
364 gtk_container_set_border_width(GTK_CONTAINER(pane), GAIM_HIG_BOX_SPACE);
365 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(lv->window)->vbox), pane, TRUE, TRUE, 0);
366
367 /* List *************/
368 sw = gtk_scrolled_window_new (NULL, NULL);
369 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
370 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
371 gtk_paned_add1(GTK_PANED(pane), sw);
372 lv->treestore = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
373 lv->treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (lv->treestore));
374 rend = gtk_cell_renderer_text_new();
375 col = gtk_tree_view_column_new_with_attributes ("time", rend, "markup", 0, NULL);
376 gtk_tree_view_append_column (GTK_TREE_VIEW(lv->treeview), col);
377 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (lv->treeview), FALSE);
378 gtk_container_add (GTK_CONTAINER (sw), lv->treeview);
379
380 populate_log_tree(lv);
381
382 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (lv->treeview));
383 g_signal_connect (G_OBJECT (sel), "changed",
384 G_CALLBACK (log_select_cb),
385 lv);
386 g_signal_connect (G_OBJECT(lv->treeview), "row-activated",
387 G_CALLBACK(log_row_activated_cb),
388 lv);
389 gaim_set_accessible_label(lv->treeview, lv->label);
390
391 /* Log size ************/
392 if(log_size) {
393 char *sz_txt = gaim_str_size_to_units(log_size);
394 text = g_strdup_printf("<span weight='bold'>%s</span> %s", _("Total log size:"), sz_txt);
395 size_label = gtk_label_new(NULL);
396 gtk_label_set_markup(GTK_LABEL(size_label), text);
397 /* gtk_paned_add1(GTK_PANED(pane), size_label); */
398 gtk_misc_set_alignment(GTK_MISC(size_label), 0, 0);
399 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(lv->window)->vbox), size_label, FALSE, FALSE, 0);
400 g_free(sz_txt);
401 g_free(text);
402 }
403
404 /* A fancy little box ************/
405 vbox = gtk_vbox_new(FALSE, GAIM_HIG_BOX_SPACE);
406 gtk_paned_add2(GTK_PANED(pane), vbox);
407
408 /* Viewer ************/
409 frame = gaim_gtk_create_imhtml(FALSE, &lv->imhtml, NULL, NULL);
410 gtk_widget_set_name(lv->imhtml, "gaim_gtklog_imhtml");
411 gtk_widget_set_size_request(lv->imhtml, 320, 200);
412 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
413 gtk_widget_show(frame);
414
415 /* Search box **********/
416 hbox = gtk_hbox_new(FALSE, GAIM_HIG_BOX_SPACE);
417 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
418 lv->entry = gtk_entry_new();
419 gtk_box_pack_start(GTK_BOX(hbox), lv->entry, TRUE, TRUE, 0);
420 button = gtk_button_new_from_stock(GTK_STOCK_FIND);
421 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
422 g_signal_connect(GTK_ENTRY(lv->entry), "activate", G_CALLBACK(search_cb), lv);
423 g_signal_connect(GTK_BUTTON(button), "clicked", G_CALLBACK(search_cb), lv);
424
425 select_first_log(lv);
426
427 gtk_widget_show_all(lv->window);
428
429 return lv;
430 }
431
432 void gaim_gtk_log_show(GaimLogType type, const char *screenname, GaimAccount *account) {
433 struct log_viewer_hash_t *ht = g_new0(struct log_viewer_hash_t, 1);
434 GaimGtkLogViewer *lv = NULL;
435 const char *name = screenname;
436 char *title;
437
438 g_return_if_fail(account != NULL);
439 g_return_if_fail(screenname != NULL);
440
441 ht->type = type;
442 ht->screenname = g_strdup(screenname);
443 ht->account = account;
444
445 if (log_viewers == NULL) {
446 log_viewers = g_hash_table_new(log_viewer_hash, log_viewer_equal);
447 } else if ((lv = g_hash_table_lookup(log_viewers, ht))) {
448 gtk_window_present(GTK_WINDOW(lv->window));
449 g_free(ht);
450 return;
451 }
452
453 if (type == GAIM_LOG_CHAT) {
454 GaimChat *chat;
455
456 chat = gaim_blist_find_chat(account, screenname);
457 if (chat != NULL)
458 name = gaim_chat_get_name(chat);
459
460 title = g_strdup_printf(_("Conversations in %s"), name);
461 } else {
462 GaimBuddy *buddy;
463
464 buddy = gaim_find_buddy(account, screenname);
465 if (buddy != NULL)
466 name = gaim_buddy_get_contact_alias(buddy);
467
468 title = g_strdup_printf(_("Conversations with %s"), name);
469 }
470
471 display_log_viewer(ht, gaim_log_get_logs(type, screenname, account),
472 title, gaim_gtk_create_prpl_icon(account, 0.5), gaim_log_get_total_size(type, screenname, account));
473 g_free(title);
474 }
475
476 void gaim_gtk_log_show_contact(GaimContact *contact) {
477 struct log_viewer_hash_t *ht = g_new0(struct log_viewer_hash_t, 1);
478 GaimBlistNode *child;
479 GaimGtkLogViewer *lv = NULL;
480 GList *logs = NULL;
481 char *filename;
482 GdkPixbuf *pixbuf;
483 const char *name = NULL;
484 char *title;
485 int total_log_size = 0;
486
487 g_return_if_fail(contact != NULL);
488
489 ht->type = GAIM_LOG_IM;
490 ht->contact = contact;
491
492 if (log_viewers == NULL) {
493 log_viewers = g_hash_table_new(log_viewer_hash, log_viewer_equal);
494 } else if ((lv = g_hash_table_lookup(log_viewers, ht))) {
495 gtk_window_present(GTK_WINDOW(lv->window));
496 g_free(ht);
497 return;
498 }
499
500 for (child = contact->node.child ; child ; child = child->next) {
501 if (!GAIM_BLIST_NODE_IS_BUDDY(child))
502 continue;
503
504 logs = g_list_concat(gaim_log_get_logs(GAIM_LOG_IM, ((GaimBuddy *)child)->name,
505 ((GaimBuddy *)child)->account), logs);
506 total_log_size += gaim_log_get_total_size(GAIM_LOG_IM, ((GaimBuddy *)child)->name, ((GaimBuddy *)child)->account);
507 }
508 logs = g_list_sort(logs, gaim_log_compare);
509
510 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "icons", "online.png", NULL);
511 pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
512 g_free(filename);
513
514 if (contact->alias != NULL)
515 name = contact->alias;
516 else if (contact->priority != NULL)
517 name = gaim_buddy_get_contact_alias(contact->priority);
518
519 title = g_strdup_printf(_("Conversations with %s"), name);
520 display_log_viewer(ht, logs, title, pixbuf, total_log_size);
521 g_free(title);
522 }
523
524 void gaim_gtk_syslog_show()
525 {
526 GList *accounts = NULL;
527 GList *logs = NULL;
528
529 if (syslog_viewer != NULL) {
530 gtk_window_present(GTK_WINDOW(syslog_viewer->window));
531 return;
532 }
533
534 for(accounts = gaim_accounts_get_all(); accounts != NULL; accounts = accounts->next) {
535
536 GaimAccount *account = (GaimAccount *)accounts->data;
537 if(gaim_find_prpl(gaim_account_get_protocol_id(account)) == NULL)
538 continue;
539
540 logs = g_list_concat(gaim_log_get_system_logs(account), logs);
541 }
542 logs = g_list_sort(logs, gaim_log_compare);
543
544 syslog_viewer = display_log_viewer(NULL, logs, _("System Log"), NULL, 0);
545 }
546
547 /****************************************************************************
548 * GTK+ LOG SUBSYSTEM *******************************************************
549 ****************************************************************************/
550
551 void *
552 gaim_gtk_log_get_handle(void)
553 {
554 static int handle;
555
556 return &handle;
557 }
558
559 void gaim_gtk_log_init(void)
560 {
561 void *handle = gaim_gtk_log_get_handle();
562
563 gaim_signal_register(handle, "log-displaying",
564 gaim_marshal_VOID__POINTER_POINTER,
565 NULL, 2,
566 gaim_value_new(GAIM_TYPE_BOXED,
567 "GaimGtkLogViewer *"),
568 gaim_value_new(GAIM_TYPE_SUBTYPE,
569 GAIM_SUBTYPE_LOG));
570 }
571
572 void
573 gaim_gtk_log_uninit(void)
574 {
575 gaim_signals_unregister_by_instance(gaim_gtk_log_get_handle());
576 }