1 /* Puts last 4k of log in new conversations a la Everybuddy (and then
2 * stolen by Trillian "Pro") */
7 #include "conversation.h"
17 #include "gtkplugin.h"
18 #include "gtkwebview.h"
20 #define HISTORY_PLUGIN_ID "gtk-history"
22 #define HISTORY_SIZE (4 * 1024)
24 static gboolean
_scroll_webview_to_end(gpointer data
)
26 PidginWebView
*webview
= data
;
27 pidgin_webview_scroll_to_end(PIDGIN_WEBVIEW(webview
), FALSE
);
28 g_object_unref(G_OBJECT(webview
));
32 static void historize(PurpleConversation
*c
)
34 PurpleAccount
*account
= purple_conversation_get_account(c
);
35 const char *name
= purple_conversation_get_name(c
);
37 const char *alias
= name
;
40 PidginConversation
*gtkconv
;
42 /* FIXME: WebView has no options */
43 GtkIMHtmlOptions options
= GTK_IMHTML_NO_COLOURS
;
47 /* FIXME: WebView has no protocol setting */
54 gtkconv
= PIDGIN_CONVERSATION(c
);
55 g_return_if_fail(gtkconv
!= NULL
);
57 /* An IM which is the first active conversation. */
58 g_return_if_fail(gtkconv
->convs
!= NULL
);
59 if (PURPLE_IS_IM_CONVERSATION(c
) && !gtkconv
->convs
->next
)
64 /* If we're not logging, don't show anything.
65 * Otherwise, we might show a very old log. */
66 if (!purple_prefs_get_bool("/purple/logging/log_ims"))
69 /* Find buddies for this conversation. */
70 buddies
= purple_blist_find_buddies(account
, name
);
72 /* If we found at least one buddy, save the first buddy's alias. */
74 alias
= purple_buddy_get_contact_alias(PURPLE_BUDDY(buddies
->data
));
76 for (cur
= buddies
; cur
!= NULL
; cur
= cur
->next
)
78 PurpleBlistNode
*node
= cur
->data
;
79 PurpleBlistNode
*prev
= purple_blist_node_get_sibling_prev(node
);
80 PurpleBlistNode
*next
= purple_blist_node_get_sibling_next(node
);
81 if ((node
!= NULL
) && ((prev
!= NULL
) || (next
!= NULL
)))
83 PurpleBlistNode
*node2
;
84 PurpleBlistNode
*parent
= purple_blist_node_get_parent(node
);
85 PurpleBlistNode
*child
= purple_blist_node_get_first_child(parent
);
87 alias
= purple_buddy_get_contact_alias(PURPLE_BUDDY(node
));
89 /* We've found a buddy that matches this conversation. It's part of a
90 * PurpleContact with more than one PurpleBuddy. Loop through the PurpleBuddies
91 * in the contact and get all the logs. */
92 for (node2
= child
; node2
!= NULL
; node2
= purple_blist_node_get_sibling_next(node2
))
94 logs
= g_list_concat(purple_log_get_logs(PURPLE_LOG_IM
,
95 purple_buddy_get_name(PURPLE_BUDDY(node2
)),
96 purple_buddy_get_account(PURPLE_BUDDY(node2
))),
102 g_slist_free(buddies
);
105 logs
= purple_log_get_logs(PURPLE_LOG_IM
, name
, account
);
107 logs
= g_list_sort(logs
, purple_log_compare
);
109 else if (PURPLE_IS_CHAT_CONVERSATION(c
))
111 /* If we're not logging, don't show anything.
112 * Otherwise, we might show a very old log. */
113 if (!purple_prefs_get_bool("/purple/logging/log_chats"))
116 logs
= purple_log_get_logs(PURPLE_LOG_CHAT
, name
, account
);
122 history
= purple_log_read((PurpleLog
*)logs
->data
, &flags
);
123 gtkconv
= PIDGIN_CONVERSATION(c
);
125 /* FIXME: WebView has no options */
126 if (flags
& PURPLE_LOG_READ_NO_NEWLINE
)
127 options
|= GTK_IMHTML_NO_NEWLINE
;
131 /* FIXME: WebView has no protocol setting */
132 protocol
= g_strdup(gtk_imhtml_get_protocol_name(GTK_IMHTML(gtkconv
->imhtml
)));
133 gtk_imhtml_set_protocol_name(GTK_IMHTML(gtkconv
->imhtml
),
134 purple_account_get_protocol_name(((PurpleLog
*)logs
->data
)->account
));
138 /* TODO WebKit: Do this properly... */
139 if (!pidgin_webview_is_empty(PIDGIN_WEBVIEW(gtkconv
->webview
)))
140 pidgin_webview_append_html(PIDGIN_WEBVIEW(gtkconv
->webview
), "<BR>");
143 escaped_alias
= g_markup_escape_text(alias
, -1);
145 dt
= g_date_time_to_local(((PurpleLog
*)logs
->data
)->time
);
146 header_date
= g_date_time_format(dt
, "%c");
147 g_date_time_unref(dt
);
149 header
= g_strdup_printf(_("<b>Conversation with %s on %s:</b><br>"), escaped_alias
, header_date
);
150 pidgin_webview_append_html(PIDGIN_WEBVIEW(gtkconv
->webview
), header
);
153 g_free(escaped_alias
);
156 pidgin_webview_append_html(PIDGIN_WEBVIEW(gtkconv
->webview
), history
);
159 pidgin_webview_append_html(PIDGIN_WEBVIEW(gtkconv
->webview
), "<hr>");
162 /* FIXME: WebView has no protocol setting */
163 gtk_imhtml_set_protocol_name(GTK_IMHTML(gtkconv
->imhtml
), protocol
);
167 g_object_ref(G_OBJECT(gtkconv
->webview
));
168 g_idle_add(_scroll_webview_to_end
, gtkconv
->webview
);
170 g_list_foreach(logs
, (GFunc
)purple_log_free
, NULL
);
175 history_prefs_check(PurplePlugin
*plugin
)
177 if (!purple_prefs_get_bool("/purple/logging/log_ims") &&
178 !purple_prefs_get_bool("/purple/logging/log_chats"))
180 /* Translators: Please maintain the use of ⇦ or ⇨ to represent the menu hierarchy */
181 purple_notify_warning(plugin
, NULL
, _("History Plugin Requires Logging"),
182 _("Logging can be enabled from Tools ⇨ Preferences ⇨ Logging.\n\n"
183 "Enabling logs for instant messages and/or chats will activate "
184 "history for the same conversation type(s)."), NULL
);
188 static void history_prefs_cb(const char *name
, PurplePrefType type
,
189 gconstpointer val
, gpointer data
)
191 history_prefs_check((PurplePlugin
*)data
);
194 static PidginPluginInfo
*
195 plugin_query(GError
**error
)
197 const gchar
* const authors
[] = {
198 "Sean Egan <seanegan@gmail.com>",
202 return pidgin_plugin_info_new(
203 "id", HISTORY_PLUGIN_ID
,
204 "name", N_("History"),
205 "version", DISPLAY_VERSION
,
206 "category", N_("User interface"),
207 "summary", N_("Shows recently logged conversations in new "
209 "description", N_("When a new conversation is opened this plugin will "
210 "insert the last conversation into the current "
213 "website", PURPLE_WEBSITE
,
214 "abi-version", PURPLE_ABI_VERSION
,
220 plugin_load(PurplePlugin
*plugin
, GError
**error
)
222 purple_signal_connect(purple_conversations_get_handle(),
223 "conversation-created",
224 plugin
, PURPLE_CALLBACK(historize
), NULL
);
225 /* XXX: Do we want to listen to pidgin's "conversation-displayed" signal? */
227 purple_prefs_connect_callback(plugin
, "/purple/logging/log_ims",
228 history_prefs_cb
, plugin
);
229 purple_prefs_connect_callback(plugin
, "/purple/logging/log_chats",
230 history_prefs_cb
, plugin
);
232 history_prefs_check(plugin
);
238 plugin_unload(PurplePlugin
*plugin
, GError
**error
)
243 PURPLE_PLUGIN_INIT(history
, plugin_query
, plugin_load
, plugin_unload
);