| |
1 /* Puts last 4k of log in new conversations a la Everybuddy (and then |
| |
2 * stolen by Trillian "Pro") */ |
| |
3 |
| |
4 #include "internal.h" |
| |
5 #include "gtkgaim.h" |
| |
6 |
| |
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" |
| |
15 |
| |
16 #include "gtkconv.h" |
| |
17 #include "gtkimhtml.h" |
| |
18 #include "gtkplugin.h" |
| |
19 |
| |
20 #define HISTORY_PLUGIN_ID "gtk-history" |
| |
21 |
| |
22 #define HISTORY_SIZE (4 * 1024) |
| |
23 |
| |
24 static gboolean _scroll_imhtml_to_end(gpointer data) |
| |
25 { |
| |
26 GtkIMHtml *imhtml = data; |
| |
27 gtk_imhtml_scroll_to_end(GTK_IMHTML(imhtml), FALSE); |
| |
28 g_object_unref(G_OBJECT(imhtml)); |
| |
29 return FALSE; |
| |
30 } |
| |
31 |
| |
32 static void historize(GaimConversation *c) |
| |
33 { |
| |
34 GaimAccount *account = gaim_conversation_get_account(c); |
| |
35 const char *name = gaim_conversation_get_name(c); |
| |
36 GaimConversationType convtype; |
| |
37 GList *logs = NULL; |
| |
38 const char *alias = name; |
| |
39 guint flags; |
| |
40 char *history; |
| |
41 GaimGtkConversation *gtkconv; |
| |
42 GtkIMHtmlOptions options = GTK_IMHTML_NO_COLOURS; |
| |
43 char *header; |
| |
44 char *protocol; |
| |
45 |
| |
46 convtype = gaim_conversation_get_type(c); |
| |
47 gtkconv = GAIM_GTK_CONVERSATION(c); |
| |
48 if (convtype == GAIM_CONV_TYPE_IM && g_list_length(gtkconv->convs) < 2) |
| |
49 { |
| |
50 GSList *buddies; |
| |
51 GSList *cur; |
| |
52 |
| |
53 /* If we're not logging, don't show anything. |
| |
54 * Otherwise, we might show a very old log. */ |
| |
55 if (!gaim_prefs_get_bool("/core/logging/log_ims")) |
| |
56 return; |
| |
57 |
| |
58 /* Find buddies for this conversation. */ |
| |
59 buddies = gaim_find_buddies(account, name); |
| |
60 |
| |
61 /* If we found at least one buddy, save the first buddy's alias. */ |
| |
62 if (buddies != NULL) |
| |
63 alias = gaim_buddy_get_contact_alias((GaimBuddy *)buddies->data); |
| |
64 |
| |
65 for (cur = buddies; cur != NULL; cur = cur->next) |
| |
66 { |
| |
67 GaimBlistNode *node = cur->data; |
| |
68 if ((node != NULL) && ((node->prev != NULL) || (node->next != NULL))) |
| |
69 { |
| |
70 GaimBlistNode *node2; |
| |
71 |
| |
72 alias = gaim_buddy_get_contact_alias((GaimBuddy *)node); |
| |
73 |
| |
74 /* We've found a buddy that matches this conversation. It's part of a |
| |
75 * GaimContact with more than one GaimBuddy. Loop through the GaimBuddies |
| |
76 * in the contact and get all the logs. */ |
| |
77 for (node2 = node->parent->child ; node2 != NULL ; node2 = node2->next) |
| |
78 { |
| |
79 logs = g_list_concat( |
| |
80 gaim_log_get_logs(GAIM_LOG_IM, |
| |
81 gaim_buddy_get_name((GaimBuddy *)node2), |
| |
82 gaim_buddy_get_account((GaimBuddy *)node2)), |
| |
83 logs); |
| |
84 } |
| |
85 break; |
| |
86 } |
| |
87 } |
| |
88 g_slist_free(buddies); |
| |
89 |
| |
90 if (logs == NULL) |
| |
91 logs = gaim_log_get_logs(GAIM_LOG_IM, name, account); |
| |
92 else |
| |
93 logs = g_list_sort(logs, gaim_log_compare); |
| |
94 } |
| |
95 else if (convtype == GAIM_CONV_TYPE_CHAT) |
| |
96 { |
| |
97 /* If we're not logging, don't show anything. |
| |
98 * Otherwise, we might show a very old log. */ |
| |
99 if (!gaim_prefs_get_bool("/core/logging/log_chats")) |
| |
100 return; |
| |
101 |
| |
102 logs = gaim_log_get_logs(GAIM_LOG_CHAT, name, account); |
| |
103 } |
| |
104 |
| |
105 if (logs == NULL) |
| |
106 return; |
| |
107 |
| |
108 history = gaim_log_read((GaimLog*)logs->data, &flags); |
| |
109 gtkconv = GAIM_GTK_CONVERSATION(c); |
| |
110 if (flags & GAIM_LOG_READ_NO_NEWLINE) |
| |
111 options |= GTK_IMHTML_NO_NEWLINE; |
| |
112 |
| |
113 protocol = g_strdup(gtk_imhtml_get_protocol_name(GTK_IMHTML(gtkconv->imhtml))); |
| |
114 gtk_imhtml_set_protocol_name(GTK_IMHTML(gtkconv->imhtml), |
| |
115 gaim_account_get_protocol_name(((GaimLog*)logs->data)->account)); |
| |
116 |
| |
117 if (gtk_text_buffer_get_char_count(gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtkconv->imhtml)))) |
| |
118 gtk_imhtml_append_text(GTK_IMHTML(gtkconv->imhtml), "<BR>", options); |
| |
119 |
| |
120 header = g_strdup_printf(_("<b>Conversation with %s on %s:</b><br>"), alias, |
| |
121 gaim_date_format_full(localtime(&((GaimLog *)logs->data)->time))); |
| |
122 gtk_imhtml_append_text(GTK_IMHTML(gtkconv->imhtml), header, options); |
| |
123 g_free(header); |
| |
124 |
| |
125 g_strchomp(history); |
| |
126 gtk_imhtml_append_text(GTK_IMHTML(gtkconv->imhtml), history, options); |
| |
127 g_free(history); |
| |
128 |
| |
129 gtk_imhtml_append_text(GTK_IMHTML(gtkconv->imhtml), "<hr>", options); |
| |
130 |
| |
131 gtk_imhtml_set_protocol_name(GTK_IMHTML(gtkconv->imhtml), protocol); |
| |
132 g_free(protocol); |
| |
133 |
| |
134 g_object_ref(G_OBJECT(gtkconv->imhtml)); |
| |
135 g_idle_add(_scroll_imhtml_to_end, gtkconv->imhtml); |
| |
136 |
| |
137 g_list_foreach(logs, (GFunc)gaim_log_free, NULL); |
| |
138 g_list_free(logs); |
| |
139 } |
| |
140 |
| |
141 static void |
| |
142 history_prefs_check(GaimPlugin *plugin) |
| |
143 { |
| |
144 if (!gaim_prefs_get_bool("/core/logging/log_ims") && |
| |
145 !gaim_prefs_get_bool("/core/logging/log_chats")) |
| |
146 { |
| |
147 gaim_notify_warning(plugin, NULL, _("History Plugin Requires Logging"), |
| |
148 _("Logging can be enabled from Tools -> Preferences -> Logging.\n\n" |
| |
149 "Enabling logs for instant messages and/or chats will activate " |
| |
150 "history for the same conversation type(s).")); |
| |
151 } |
| |
152 } |
| |
153 |
| |
154 static void history_prefs_cb(const char *name, GaimPrefType type, |
| |
155 gconstpointer val, gpointer data) |
| |
156 { |
| |
157 history_prefs_check((GaimPlugin *)data); |
| |
158 } |
| |
159 |
| |
160 static gboolean |
| |
161 plugin_load(GaimPlugin *plugin) |
| |
162 { |
| |
163 gaim_signal_connect(gaim_conversations_get_handle(), |
| |
164 "conversation-created", |
| |
165 plugin, GAIM_CALLBACK(historize), NULL); |
| |
166 |
| |
167 gaim_prefs_connect_callback(plugin, "/core/logging/log_ims", |
| |
168 history_prefs_cb, plugin); |
| |
169 gaim_prefs_connect_callback(plugin, "/core/logging/log_chats", |
| |
170 history_prefs_cb, plugin); |
| |
171 |
| |
172 history_prefs_check(plugin); |
| |
173 |
| |
174 return TRUE; |
| |
175 } |
| |
176 |
| |
177 static GaimPluginInfo info = |
| |
178 { |
| |
179 GAIM_PLUGIN_MAGIC, |
| |
180 GAIM_MAJOR_VERSION, |
| |
181 GAIM_MINOR_VERSION, |
| |
182 GAIM_PLUGIN_STANDARD, |
| |
183 GAIM_GTK_PLUGIN_TYPE, |
| |
184 0, |
| |
185 NULL, |
| |
186 GAIM_PRIORITY_DEFAULT, |
| |
187 HISTORY_PLUGIN_ID, |
| |
188 N_("History"), |
| |
189 VERSION, |
| |
190 N_("Shows recently logged conversations in new conversations."), |
| |
191 N_("When a new conversation is opened this plugin will insert " |
| |
192 "the last conversation into the current conversation."), |
| |
193 "Sean Egan <seanegan@gmail.com>", |
| |
194 GAIM_WEBSITE, |
| |
195 plugin_load, |
| |
196 NULL, |
| |
197 NULL, |
| |
198 NULL, |
| |
199 NULL, |
| |
200 NULL, |
| |
201 NULL |
| |
202 }; |
| |
203 |
| |
204 static void |
| |
205 init_plugin(GaimPlugin *plugin) |
| |
206 { |
| |
207 } |
| |
208 |
| |
209 GAIM_INIT_PLUGIN(history, init_plugin, info) |