comparison finch/plugins/gnthistory.c @ 15817:0e3a8505ebbe

renamed gaim-text to finch
author Sean Egan <seanegan@gmail.com>
date Sun, 18 Mar 2007 19:38:15 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15816:317e7613e581 15817:0e3a8505ebbe
1 /**
2 * @file gnthistory.c Show log from previous conversation
3 *
4 * Copyright (C) 2006 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
5 *
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.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* Ripped from gtk/plugins/history.c */
22
23 #include "internal.h"
24
25 #include "conversation.h"
26 #include "debug.h"
27 #include "log.h"
28 #include "notify.h"
29 #include "prefs.h"
30 #include "signals.h"
31 #include "util.h"
32 #include "version.h"
33
34 #include "gntplugin.h"
35
36 #define HISTORY_PLUGIN_ID "gnt-history"
37
38 #define HISTORY_SIZE (4 * 1024)
39
40 static void historize(GaimConversation *c)
41 {
42 GaimAccount *account = gaim_conversation_get_account(c);
43 const char *name = gaim_conversation_get_name(c);
44 GaimConversationType convtype;
45 GList *logs = NULL;
46 const char *alias = name;
47 GaimLogReadFlags flags;
48 char *history;
49 char *header;
50 GaimMessageFlags mflag;
51
52 convtype = gaim_conversation_get_type(c);
53 if (convtype == GAIM_CONV_TYPE_IM)
54 {
55 GSList *buddies;
56 GSList *cur;
57
58 /* If we're not logging, don't show anything.
59 * Otherwise, we might show a very old log. */
60 if (!gaim_prefs_get_bool("/core/logging/log_ims"))
61 return;
62
63 /* Find buddies for this conversation. */
64 buddies = gaim_find_buddies(account, name);
65
66 /* If we found at least one buddy, save the first buddy's alias. */
67 if (buddies != NULL)
68 alias = gaim_buddy_get_contact_alias((GaimBuddy *)buddies->data);
69
70 for (cur = buddies; cur != NULL; cur = cur->next)
71 {
72 GaimBlistNode *node = cur->data;
73 if ((node != NULL) && ((node->prev != NULL) || (node->next != NULL)))
74 {
75 GaimBlistNode *node2;
76
77 alias = gaim_buddy_get_contact_alias((GaimBuddy *)node);
78
79 /* We've found a buddy that matches this conversation. It's part of a
80 * GaimContact with more than one GaimBuddy. Loop through the GaimBuddies
81 * in the contact and get all the logs. */
82 for (node2 = node->parent->child ; node2 != NULL ; node2 = node2->next)
83 {
84 logs = g_list_concat(
85 gaim_log_get_logs(GAIM_LOG_IM,
86 gaim_buddy_get_name((GaimBuddy *)node2),
87 gaim_buddy_get_account((GaimBuddy *)node2)),
88 logs);
89 }
90 break;
91 }
92 }
93 g_slist_free(buddies);
94
95 if (logs == NULL)
96 logs = gaim_log_get_logs(GAIM_LOG_IM, name, account);
97 else
98 logs = g_list_sort(logs, gaim_log_compare);
99 }
100 else if (convtype == GAIM_CONV_TYPE_CHAT)
101 {
102 /* If we're not logging, don't show anything.
103 * Otherwise, we might show a very old log. */
104 if (!gaim_prefs_get_bool("/core/logging/log_chats"))
105 return;
106
107 logs = gaim_log_get_logs(GAIM_LOG_CHAT, name, account);
108 }
109
110 if (logs == NULL)
111 return;
112
113 mflag = GAIM_MESSAGE_NO_LOG | GAIM_MESSAGE_SYSTEM | GAIM_MESSAGE_DELAYED;
114 history = gaim_log_read((GaimLog*)logs->data, &flags);
115
116 header = g_strdup_printf(_("<b>Conversation with %s on %s:</b><br>"), alias,
117 gaim_date_format_full(localtime(&((GaimLog *)logs->data)->time)));
118 gaim_conversation_write(c, "", header, mflag, time(NULL));
119 g_free(header);
120
121 if (flags & GAIM_LOG_READ_NO_NEWLINE)
122 gaim_str_strip_char(history, '\n');
123 gaim_conversation_write(c, "", history, mflag, time(NULL));
124 g_free(history);
125
126 gaim_conversation_write(c, "", "<hr>", mflag, time(NULL));
127
128 g_list_foreach(logs, (GFunc)gaim_log_free, NULL);
129 g_list_free(logs);
130 }
131
132 static void
133 history_prefs_check(GaimPlugin *plugin)
134 {
135 if (!gaim_prefs_get_bool("/core/logging/log_ims") &&
136 !gaim_prefs_get_bool("/core/logging/log_chats"))
137 {
138 gaim_notify_warning(plugin, NULL, _("History Plugin Requires Logging"),
139 _("Logging can be enabled from Tools -> Preferences -> Logging.\n\n"
140 "Enabling logs for instant messages and/or chats will activate "
141 "history for the same conversation type(s)."));
142 }
143 }
144
145 static void history_prefs_cb(const char *name, GaimPrefType type,
146 gconstpointer val, gpointer data)
147 {
148 history_prefs_check((GaimPlugin *)data);
149 }
150
151 static gboolean
152 plugin_load(GaimPlugin *plugin)
153 {
154 gaim_signal_connect(gaim_conversations_get_handle(),
155 "conversation-created",
156 plugin, GAIM_CALLBACK(historize), NULL);
157
158 gaim_prefs_connect_callback(plugin, "/core/logging/log_ims",
159 history_prefs_cb, plugin);
160 gaim_prefs_connect_callback(plugin, "/core/logging/log_chats",
161 history_prefs_cb, plugin);
162
163 history_prefs_check(plugin);
164
165 return TRUE;
166 }
167
168 static GaimPluginInfo info =
169 {
170 GAIM_PLUGIN_MAGIC,
171 GAIM_MAJOR_VERSION,
172 GAIM_MINOR_VERSION,
173 GAIM_PLUGIN_STANDARD,
174 NULL,
175 0,
176 NULL,
177 GAIM_PRIORITY_DEFAULT,
178 HISTORY_PLUGIN_ID,
179 N_("GntHistory"),
180 VERSION,
181 N_("Shows recently logged conversations in new conversations."),
182 N_("When a new conversation is opened this plugin will insert "
183 "the last conversation into the current conversation."),
184 "Sean Egan <seanegan@gmail.com>\n"
185 "Sadrul H Chowdhury <sadrul@users.sourceforge.net>",
186 GAIM_WEBSITE,
187 plugin_load,
188 NULL,
189 NULL,
190 NULL,
191 NULL,
192 NULL,
193 NULL
194 };
195
196 static void
197 init_plugin(GaimPlugin *plugin)
198 {
199 }
200
201 GAIM_INIT_PLUGIN(gnthistory, init_plugin, info)
202