merge of '6696045322d43e1d7c9634a33cf10ca2323c061c'
[pidgin-git.git] / finch / plugins / gnthistory.c
blob68303be145d7229a3e7ba255b78a7930399dad01
1 /**
2 * @file gnthistory.c Show log from previous conversation
4 * Copyright (C) 2006 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 /* Ripped from gtk/plugins/history.c */
23 #include "internal.h"
25 #include "conversation.h"
26 #include "debug.h"
27 #include "log.h"
28 #include "request.h"
29 #include "prefs.h"
30 #include "signals.h"
31 #include "util.h"
32 #include "version.h"
34 #include "gntconv.h"
35 #include "gntplugin.h"
36 #include "gntrequest.h"
38 #define HISTORY_PLUGIN_ID "gnt-history"
40 #define HISTORY_SIZE (4 * 1024)
42 static void historize(PurpleConversation *c)
44 PurpleAccount *account = purple_conversation_get_account(c);
45 const char *name = purple_conversation_get_name(c);
46 PurpleConversationType convtype;
47 GList *logs = NULL;
48 const char *alias = name;
49 PurpleLogReadFlags flags;
50 char *history;
51 char *header;
52 PurpleMessageFlags mflag;
54 convtype = purple_conversation_get_type(c);
55 if (convtype == PURPLE_CONV_TYPE_IM) {
56 GSList *buddies;
57 GSList *cur;
58 FinchConv *fc = FINCH_CONV(c);
59 if (fc->list && fc->list->next) /* We were already in the middle of a conversation. */
60 return;
62 /* If we're not logging, don't show anything.
63 * Otherwise, we might show a very old log. */
64 if (!purple_prefs_get_bool("/purple/logging/log_ims"))
65 return;
67 /* Find buddies for this conversation. */
68 buddies = purple_find_buddies(account, name);
70 /* If we found at least one buddy, save the first buddy's alias. */
71 if (buddies != NULL)
72 alias = purple_buddy_get_contact_alias((PurpleBuddy *)buddies->data);
74 for (cur = buddies; cur != NULL; cur = cur->next) {
75 PurpleBlistNode *node = cur->data;
76 if ((node != NULL) &&
77 ((purple_blist_node_get_sibling_prev(node) != NULL) ||
78 (purple_blist_node_get_sibling_next(node) != NULL))) {
79 PurpleBlistNode *node2;
81 alias = purple_buddy_get_contact_alias((PurpleBuddy *)node);
83 /* We've found a buddy that matches this conversation. It's part of a
84 * PurpleContact with more than one PurpleBuddy. Loop through the PurpleBuddies
85 * in the contact and get all the logs. */
86 for (node2 = purple_blist_node_get_first_child(purple_blist_node_get_parent(node));
87 node2 != NULL ; node2 = purple_blist_node_get_sibling_next(node2)) {
88 logs = g_list_concat(
89 purple_log_get_logs(PURPLE_LOG_IM,
90 purple_buddy_get_name((PurpleBuddy *)node2),
91 purple_buddy_get_account((PurpleBuddy *)node2)),
92 logs);
94 break;
97 g_slist_free(buddies);
99 if (logs == NULL)
100 logs = purple_log_get_logs(PURPLE_LOG_IM, name, account);
101 else
102 logs = g_list_sort(logs, purple_log_compare);
103 } else if (convtype == PURPLE_CONV_TYPE_CHAT) {
104 /* If we're not logging, don't show anything.
105 * Otherwise, we might show a very old log. */
106 if (!purple_prefs_get_bool("/purple/logging/log_chats"))
107 return;
109 logs = purple_log_get_logs(PURPLE_LOG_CHAT, name, account);
112 if (logs == NULL)
113 return;
115 mflag = PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_DELAYED;
116 history = purple_log_read((PurpleLog*)logs->data, &flags);
118 header = g_strdup_printf(_("<b>Conversation with %s on %s:</b><br>"), alias,
119 purple_date_format_full(localtime(&((PurpleLog *)logs->data)->time)));
120 purple_conversation_write(c, "", header, mflag, time(NULL));
121 g_free(header);
123 if (flags & PURPLE_LOG_READ_NO_NEWLINE)
124 purple_str_strip_char(history, '\n');
125 purple_conversation_write(c, "", history, mflag, time(NULL));
126 g_free(history);
128 purple_conversation_write(c, "", "<hr>", mflag, time(NULL));
130 g_list_foreach(logs, (GFunc)purple_log_free, NULL);
131 g_list_free(logs);
134 static void
135 history_prefs_check(PurplePlugin *plugin)
137 if (!purple_prefs_get_bool("/purple/logging/log_ims") &&
138 !purple_prefs_get_bool("/purple/logging/log_chats"))
140 PurpleRequestFields *fields = purple_request_fields_new();
141 PurpleRequestFieldGroup *group;
142 PurpleRequestField *field;
143 struct {
144 const char *pref;
145 const char *label;
146 } prefs[] = {
147 {"/purple/logging/log_ims", N_("Log IMs")},
148 {"/purple/logging/log_chats", N_("Log chats")},
149 {NULL, NULL}
151 int iter;
152 GList *list = purple_log_logger_get_options();
153 const char *system = purple_prefs_get_string("/purple/logging/format");
155 group = purple_request_field_group_new(_("Logging"));
157 field = purple_request_field_list_new("/purple/logging/format", _("Log format"));
158 while (list) {
159 const char *label = _(list->data);
160 list = g_list_delete_link(list, list);
161 purple_request_field_list_add(field, label, list->data);
162 if (system && strcmp(system, list->data) == 0)
163 purple_request_field_list_add_selected(field, label);
164 list = g_list_delete_link(list, list);
166 purple_request_field_group_add_field(group, field);
168 for (iter = 0; prefs[iter].pref; iter++) {
169 field = purple_request_field_bool_new(prefs[iter].pref, _(prefs[iter].label),
170 purple_prefs_get_bool(prefs[iter].pref));
171 purple_request_field_group_add_field(group, field);
174 purple_request_fields_add_group(fields, group);
176 purple_request_fields(plugin, NULL, _("History Plugin Requires Logging"),
177 _("Logging can be enabled from Tools -> Preferences -> Logging.\n\n"
178 "Enabling logs for instant messages and/or chats will activate "
179 "history for the same conversation type(s)."),
180 fields,
181 _("OK"), G_CALLBACK(finch_request_save_in_prefs),
182 _("Cancel"), NULL,
183 NULL, NULL, NULL, plugin);
187 static void history_prefs_cb(const char *name, PurplePrefType type,
188 gconstpointer val, gpointer data)
190 history_prefs_check((PurplePlugin *)data);
193 static gboolean
194 plugin_load(PurplePlugin *plugin)
196 purple_signal_connect(purple_conversations_get_handle(),
197 "conversation-created",
198 plugin, PURPLE_CALLBACK(historize), NULL);
200 purple_prefs_connect_callback(plugin, "/purple/logging/log_ims",
201 history_prefs_cb, plugin);
202 purple_prefs_connect_callback(plugin, "/purple/logging/log_chats",
203 history_prefs_cb, plugin);
205 history_prefs_check(plugin);
207 return TRUE;
210 static PurplePluginInfo info =
212 PURPLE_PLUGIN_MAGIC,
213 PURPLE_MAJOR_VERSION,
214 PURPLE_MINOR_VERSION,
215 PURPLE_PLUGIN_STANDARD,
216 NULL,
218 NULL,
219 PURPLE_PRIORITY_DEFAULT,
220 HISTORY_PLUGIN_ID,
221 N_("GntHistory"),
222 DISPLAY_VERSION,
223 N_("Shows recently logged conversations in new conversations."),
224 N_("When a new conversation is opened this plugin will insert "
225 "the last conversation into the current conversation."),
226 "Sean Egan <seanegan@gmail.com>\n"
227 "Sadrul H Chowdhury <sadrul@users.sourceforge.net>",
228 PURPLE_WEBSITE,
229 plugin_load,
230 NULL,
231 NULL,
232 NULL,
233 NULL,
234 NULL,
235 NULL,
237 /* padding */
238 NULL,
239 NULL,
240 NULL,
241 NULL
244 static void
245 init_plugin(PurplePlugin *plugin)
249 PURPLE_INIT_PLUGIN(gnthistory, init_plugin, info)