Replace functions which called once with their bodies
[pidgin-git.git] / pidgin / plugins / history.c
blob116b31dfcb5bab89aded20ebc892806f6839e053
1 /* Puts last 4k of log in new conversations a la Everybuddy (and then
2 * stolen by Trillian "Pro") */
4 #include "internal.h"
5 #include "pidgin.h"
7 #include "conversation.h"
8 #include "debug.h"
9 #include "log.h"
10 #include "notify.h"
11 #include "prefs.h"
12 #include "signals.h"
13 #include "util.h"
14 #include "version.h"
16 #include "gtkconv.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));
29 return FALSE;
32 static void historize(PurpleConversation *c)
34 PurpleAccount *account = purple_conversation_get_account(c);
35 const char *name = purple_conversation_get_name(c);
36 GList *logs = NULL;
37 const char *alias = name;
38 guint flags;
39 char *history;
40 PidginConversation *gtkconv;
41 #if 0
42 /* FIXME: WebView has no options */
43 GtkIMHtmlOptions options = GTK_IMHTML_NO_COLOURS;
44 #endif
45 char *header;
46 #if 0
47 /* FIXME: WebView has no protocol setting */
48 char *protocol;
49 #endif
50 char *escaped_alias;
51 GDateTime *dt;
52 gchar *header_date;
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)
61 GSList *buddies;
62 GSList *cur;
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"))
67 return;
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. */
73 if (buddies != NULL)
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))),
97 logs);
99 break;
102 g_slist_free(buddies);
104 if (logs == NULL)
105 logs = purple_log_get_logs(PURPLE_LOG_IM, name, account);
106 else
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"))
114 return;
116 logs = purple_log_get_logs(PURPLE_LOG_CHAT, name, account);
119 if (logs == NULL)
120 return;
122 history = purple_log_read((PurpleLog*)logs->data, &flags);
123 gtkconv = PIDGIN_CONVERSATION(c);
124 #if 0
125 /* FIXME: WebView has no options */
126 if (flags & PURPLE_LOG_READ_NO_NEWLINE)
127 options |= GTK_IMHTML_NO_NEWLINE;
128 #endif
130 #if 0
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));
135 #endif
137 #if 0
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>");
141 #endif
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);
151 g_free(header_date);
152 g_free(header);
153 g_free(escaped_alias);
155 g_strchomp(history);
156 pidgin_webview_append_html(PIDGIN_WEBVIEW(gtkconv->webview), history);
157 g_free(history);
159 pidgin_webview_append_html(PIDGIN_WEBVIEW(gtkconv->webview), "<hr>");
161 #if 0
162 /* FIXME: WebView has no protocol setting */
163 gtk_imhtml_set_protocol_name(GTK_IMHTML(gtkconv->imhtml), protocol);
164 g_free(protocol);
165 #endif
167 g_object_ref(G_OBJECT(gtkconv->webview));
168 g_idle_add(_scroll_webview_to_end, gtkconv->webview);
170 g_list_free_full(logs, (GDestroyNotify)purple_log_free);
173 static void
174 history_prefs_check(PurplePlugin *plugin)
176 if (!purple_prefs_get_bool("/purple/logging/log_ims") &&
177 !purple_prefs_get_bool("/purple/logging/log_chats"))
179 /* Translators: Please maintain the use of ⇦ or ⇨ to represent the menu hierarchy */
180 purple_notify_warning(plugin, NULL, _("History Plugin Requires Logging"),
181 _("Logging can be enabled from Tools ⇨ Preferences ⇨ Logging.\n\n"
182 "Enabling logs for instant messages and/or chats will activate "
183 "history for the same conversation type(s)."), NULL);
187 static void history_prefs_cb(const char *name, PurplePrefType type,
188 gconstpointer val, gpointer data)
190 history_prefs_check((PurplePlugin *)data);
193 static PidginPluginInfo *
194 plugin_query(GError **error)
196 const gchar * const authors[] = {
197 "Sean Egan <seanegan@gmail.com>",
198 NULL
201 return pidgin_plugin_info_new(
202 "id", HISTORY_PLUGIN_ID,
203 "name", N_("History"),
204 "version", DISPLAY_VERSION,
205 "category", N_("User interface"),
206 "summary", N_("Shows recently logged conversations in new "
207 "conversations."),
208 "description", N_("When a new conversation is opened this plugin will "
209 "insert the last conversation into the current "
210 "conversation."),
211 "authors", authors,
212 "website", PURPLE_WEBSITE,
213 "abi-version", PURPLE_ABI_VERSION,
214 NULL
218 static gboolean
219 plugin_load(PurplePlugin *plugin, GError **error)
221 purple_signal_connect(purple_conversations_get_handle(),
222 "conversation-created",
223 plugin, PURPLE_CALLBACK(historize), NULL);
224 /* XXX: Do we want to listen to pidgin's "conversation-displayed" signal? */
226 purple_prefs_connect_callback(plugin, "/purple/logging/log_ims",
227 history_prefs_cb, plugin);
228 purple_prefs_connect_callback(plugin, "/purple/logging/log_chats",
229 history_prefs_cb, plugin);
231 history_prefs_check(plugin);
233 return TRUE;
236 static gboolean
237 plugin_unload(PurplePlugin *plugin, GError **error)
239 return TRUE;
242 PURPLE_PLUGIN_INIT(history, plugin_query, plugin_load, plugin_unload);