finch: Mark unused variable.
[pidgin-git.git] / finch / plugins / gnthistory.c
blobd2471ac1bff0616c53cb653a41f5d5c1cc2ac184
1 /**
2 * Copyright (C) 2006 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 /* Ripped from gtk/plugins/history.c */
21 #include "internal.h"
23 #include "conversation.h"
24 #include "debug.h"
25 #include "log.h"
26 #include "request.h"
27 #include "prefs.h"
28 #include "signals.h"
29 #include "util.h"
30 #include "version.h"
32 #include "gntconv.h"
33 #include "gntplugin.h"
34 #include "gntrequest.h"
36 #define HISTORY_PLUGIN_ID "gnt-history"
38 #define HISTORY_SIZE (4 * 1024)
40 static void historize(PurpleConversation *c)
42 PurpleAccount *account = purple_conversation_get_account(c);
43 const char *name = purple_conversation_get_name(c);
44 GList *logs = NULL;
45 const char *alias = name;
46 PurpleLogReadFlags flags;
47 GDateTime *dt;
48 char *date;
49 char *history;
50 char *header;
51 PurpleMessageFlags mflag;
53 if (PURPLE_IS_IM_CONVERSATION(c)) {
54 GSList *buddies;
55 GSList *cur;
56 FinchConv *fc = FINCH_CONV(c);
57 if (fc->list && fc->list->next) /* We were already in the middle of a conversation. */
58 return;
60 /* If we're not logging, don't show anything.
61 * Otherwise, we might show a very old log. */
62 if (!purple_prefs_get_bool("/purple/logging/log_ims"))
63 return;
65 /* Find buddies for this conversation. */
66 buddies = purple_blist_find_buddies(account, name);
68 /* If we found at least one buddy, save the first buddy's alias. */
69 if (buddies != NULL)
70 alias = purple_buddy_get_contact_alias(PURPLE_BUDDY(buddies->data));
72 for (cur = buddies; cur != NULL; cur = cur->next) {
73 PurpleBlistNode *node = cur->data;
74 if ((node != NULL) &&
75 ((purple_blist_node_get_sibling_prev(node) != NULL) ||
76 (purple_blist_node_get_sibling_next(node) != NULL))) {
77 PurpleBlistNode *node2;
79 alias = purple_buddy_get_contact_alias(PURPLE_BUDDY(node));
81 /* We've found a buddy that matches this conversation. It's part of a
82 * PurpleContact with more than one PurpleBuddy. Loop through the PurpleBuddies
83 * in the contact and get all the logs. */
84 for (node2 = purple_blist_node_get_first_child(purple_blist_node_get_parent(node));
85 node2 != NULL ; node2 = purple_blist_node_get_sibling_next(node2)) {
86 logs = g_list_concat(
87 purple_log_get_logs(PURPLE_LOG_IM,
88 purple_buddy_get_name(PURPLE_BUDDY(node2)),
89 purple_buddy_get_account(PURPLE_BUDDY(node2))),
90 logs);
92 break;
95 g_slist_free(buddies);
97 if (logs == NULL)
98 logs = purple_log_get_logs(PURPLE_LOG_IM, name, account);
99 else
100 logs = g_list_sort(logs, purple_log_compare);
101 } else if (PURPLE_IS_CHAT_CONVERSATION(c)) {
102 /* If we're not logging, don't show anything.
103 * Otherwise, we might show a very old log. */
104 if (!purple_prefs_get_bool("/purple/logging/log_chats"))
105 return;
107 logs = purple_log_get_logs(PURPLE_LOG_CHAT, name, account);
110 if (logs == NULL)
111 return;
113 mflag = PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_DELAYED;
114 history = purple_log_read((PurpleLog*)logs->data, &flags);
116 dt = g_date_time_to_local(((PurpleLog *)logs->data)->time);
117 date = g_date_time_format(dt, "%c");
118 header = g_strdup_printf(_("<b>Conversation with %s on %s:</b><br>"), alias, date);
119 purple_conversation_write_system_message(c, header, mflag);
120 g_free(date);
121 g_free(header);
123 if (flags & PURPLE_LOG_READ_NO_NEWLINE)
124 purple_str_strip_char(history, '\n');
125 purple_conversation_write_system_message(c, history, mflag);
126 g_free(history);
128 purple_conversation_write_system_message(c, "<hr>", mflag);
130 g_list_free_full(logs, (GDestroyNotify)purple_log_free);
133 static void
134 history_prefs_check(PurplePlugin *plugin)
136 if (!purple_prefs_get_bool("/purple/logging/log_ims") &&
137 !purple_prefs_get_bool("/purple/logging/log_chats"))
139 PurpleRequestFields *fields = purple_request_fields_new();
140 PurpleRequestFieldGroup *group;
141 PurpleRequestField *field;
142 struct {
143 const char *pref;
144 const char *label;
145 } prefs[] = {
146 {"/purple/logging/log_ims", N_("Log IMs")},
147 {"/purple/logging/log_chats", N_("Log chats")},
148 {NULL, NULL}
150 int iter;
151 GList *list = purple_log_logger_get_options();
152 const char *system = purple_prefs_get_string("/purple/logging/format");
154 group = purple_request_field_group_new(_("Logging"));
156 field = purple_request_field_list_new("/purple/logging/format", _("Log format"));
157 while (list) {
158 const char *label = _(list->data);
159 list = g_list_delete_link(list, list);
160 purple_request_field_list_add_icon(field, label, NULL, list->data);
161 if (purple_strequal(system, list->data))
162 purple_request_field_list_add_selected(field, label);
163 list = g_list_delete_link(list, list);
165 purple_request_field_group_add_field(group, field);
167 for (iter = 0; prefs[iter].pref; iter++) {
168 field = purple_request_field_bool_new(prefs[iter].pref, _(prefs[iter].label),
169 purple_prefs_get_bool(prefs[iter].pref));
170 purple_request_field_group_add_field(group, field);
173 purple_request_fields_add_group(fields, group);
175 /* Translators: Please maintain the use of ⇦ or ⇨ to represent the menu hierarchy */
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, 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 FinchPluginInfo *
194 plugin_query(GError **error)
196 const gchar * const authors[] = {
197 "Sean Egan <seanegan@gmail.com>",
198 "Sadrul H Chowdhury <sadrul@users.sourceforge.net>",
199 NULL
202 return finch_plugin_info_new(
203 "id", HISTORY_PLUGIN_ID,
204 "name", N_("GntHistory"),
205 "version", DISPLAY_VERSION,
206 "category", N_("User interface"),
207 "summary", N_("Shows recently logged conversations in new "
208 "conversations."),
209 "description", N_("When a new conversation is opened this plugin will "
210 "insert the last conversation into the current "
211 "conversation."),
212 "authors", authors,
213 "website", PURPLE_WEBSITE,
214 "abi-version", PURPLE_ABI_VERSION,
215 NULL
219 static gboolean
220 plugin_load(PurplePlugin *plugin, GError **error)
222 purple_signal_connect(purple_conversations_get_handle(),
223 "conversation-created",
224 plugin, PURPLE_CALLBACK(historize), NULL);
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(gnthistory, plugin_query, plugin_load, plugin_unload);