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