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 */
25 #include "conversation.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
;
48 const char *alias
= name
;
49 PurpleLogReadFlags flags
;
52 PurpleMessageFlags mflag
;
54 convtype
= purple_conversation_get_type(c
);
55 if (convtype
== PURPLE_CONV_TYPE_IM
) {
58 FinchConv
*fc
= FINCH_CONV(c
);
59 if (fc
->list
&& fc
->list
->next
) /* We were already in the middle of a conversation. */
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"))
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. */
72 alias
= purple_buddy_get_contact_alias((PurpleBuddy
*)buddies
->data
);
74 for (cur
= buddies
; cur
!= NULL
; cur
= cur
->next
) {
75 PurpleBlistNode
*node
= cur
->data
;
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
)) {
89 purple_log_get_logs(PURPLE_LOG_IM
,
90 purple_buddy_get_name((PurpleBuddy
*)node2
),
91 purple_buddy_get_account((PurpleBuddy
*)node2
)),
97 g_slist_free(buddies
);
100 logs
= purple_log_get_logs(PURPLE_LOG_IM
, name
, account
);
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"))
109 logs
= purple_log_get_logs(PURPLE_LOG_CHAT
, name
, account
);
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
));
123 if (flags
& PURPLE_LOG_READ_NO_NEWLINE
)
124 purple_str_strip_char(history
, '\n');
125 purple_conversation_write(c
, "", history
, mflag
, time(NULL
));
128 purple_conversation_write(c
, "", "<hr>", mflag
, time(NULL
));
130 g_list_foreach(logs
, (GFunc
)purple_log_free
, NULL
);
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
;
147 {"/purple/logging/log_ims", N_("Log IMs")},
148 {"/purple/logging/log_chats", N_("Log chats")},
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"));
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)."),
181 _("OK"), G_CALLBACK(finch_request_save_in_prefs
),
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
);
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
);
210 static PurplePluginInfo info
=
213 PURPLE_MAJOR_VERSION
,
214 PURPLE_MINOR_VERSION
,
215 PURPLE_PLUGIN_STANDARD
,
219 PURPLE_PRIORITY_DEFAULT
,
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>",
245 init_plugin(PurplePlugin
*plugin
)
249 PURPLE_INIT_PLUGIN(gnthistory
, init_plugin
, info
)