Mercurial > pidgin.yaz
comparison libpurple/conversation.c @ 19413:deb9471d7142
Store message history in a conversation, and provide API to access the history.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sat, 25 Aug 2007 03:59:05 +0000 |
parents | e1062ac14080 |
children | 3489182dc187 |
comparison
equal
deleted
inserted
replaced
19412:1096aea98217 | 19413:deb9471d7142 |
---|---|
37 | 37 |
38 static GList *conversations = NULL; | 38 static GList *conversations = NULL; |
39 static GList *ims = NULL; | 39 static GList *ims = NULL; |
40 static GList *chats = NULL; | 40 static GList *chats = NULL; |
41 static PurpleConversationUiOps *default_ops = NULL; | 41 static PurpleConversationUiOps *default_ops = NULL; |
42 static GHashTable *histories = NULL; | |
42 | 43 |
43 void | 44 void |
44 purple_conversations_set_ui_ops(PurpleConversationUiOps *ops) | 45 purple_conversations_set_ui_ops(PurpleConversationUiOps *ops) |
45 { | 46 { |
46 default_ops = ops; | 47 default_ops = ops; |
200 conv->logs = g_list_append(NULL, purple_log_new(conv->type == PURPLE_CONV_TYPE_CHAT ? PURPLE_LOG_CHAT : | 201 conv->logs = g_list_append(NULL, purple_log_new(conv->type == PURPLE_CONV_TYPE_CHAT ? PURPLE_LOG_CHAT : |
201 PURPLE_LOG_IM, conv->name, conv->account, | 202 PURPLE_LOG_IM, conv->name, conv->account, |
202 conv, time(NULL), NULL)); | 203 conv, time(NULL), NULL)); |
203 } | 204 } |
204 | 205 |
206 /* Functions that deal with PurpleConvMessage */ | |
207 | |
208 static void | |
209 add_message_to_history(PurpleConversation *conv, const char *who, const char *message, | |
210 PurpleMessageFlags flags, time_t when) | |
211 { | |
212 GList *list; | |
213 PurpleConvMessage *msg; | |
214 | |
215 msg = g_new0(PurpleConvMessage, 1); | |
216 msg->who = g_strdup(who); | |
217 msg->flags = flags; | |
218 msg->what = g_strdup(message); | |
219 msg->when = when; | |
220 | |
221 list = g_hash_table_lookup(histories, conv); | |
222 list = g_list_prepend(list, msg); | |
223 g_hash_table_insert(histories, conv, list); | |
224 } | |
225 | |
226 static void | |
227 free_conv_message(PurpleConvMessage *msg) | |
228 { | |
229 g_free(msg->who); | |
230 g_free(msg->what); | |
231 g_free(msg); | |
232 } | |
233 | |
234 static void | |
235 message_history_free(GList *list) | |
236 { | |
237 g_list_foreach(list, (GFunc)free_conv_message, NULL); | |
238 g_list_free(list); | |
239 } | |
205 | 240 |
206 /************************************************************************** | 241 /************************************************************************** |
207 * Conversation API | 242 * Conversation API |
208 **************************************************************************/ | 243 **************************************************************************/ |
209 static void | 244 static void |
471 if (ops != NULL && ops->destroy_conversation != NULL) | 506 if (ops != NULL && ops->destroy_conversation != NULL) |
472 ops->destroy_conversation(conv); | 507 ops->destroy_conversation(conv); |
473 | 508 |
474 purple_conversation_close_logs(conv); | 509 purple_conversation_close_logs(conv); |
475 | 510 |
511 purple_conversation_clear_message_history(conv); | |
512 | |
476 PURPLE_DBUS_UNREGISTER_POINTER(conv); | 513 PURPLE_DBUS_UNREGISTER_POINTER(conv); |
477 g_free(conv); | 514 g_free(conv); |
478 conv = NULL; | 515 conv = NULL; |
479 } | 516 } |
480 | 517 |
890 purple_conv_im_set_typing_state(PURPLE_CONV_IM(conv), PURPLE_NOT_TYPING); | 927 purple_conv_im_set_typing_state(PURPLE_CONV_IM(conv), PURPLE_NOT_TYPING); |
891 } | 928 } |
892 } | 929 } |
893 | 930 |
894 ops->write_conv(conv, who, alias, displayed, flags, mtime); | 931 ops->write_conv(conv, who, alias, displayed, flags, mtime); |
932 add_message_to_history(conv, who, message, flags, mtime); | |
895 | 933 |
896 purple_signal_emit(purple_conversations_get_handle(), | 934 purple_signal_emit(purple_conversations_get_handle(), |
897 (type == PURPLE_CONV_TYPE_IM ? "wrote-im-msg" : "wrote-chat-msg"), | 935 (type == PURPLE_CONV_TYPE_IM ? "wrote-im-msg" : "wrote-chat-msg"), |
898 account, who, displayed, conv, flags); | 936 account, who, displayed, conv, flags); |
899 | 937 |
1105 g_return_if_fail(message != NULL); | 1143 g_return_if_fail(message != NULL); |
1106 | 1144 |
1107 c = purple_conv_im_get_conversation(im); | 1145 c = purple_conv_im_get_conversation(im); |
1108 | 1146 |
1109 /* Pass this on to either the ops structure or the default write func. */ | 1147 /* Pass this on to either the ops structure or the default write func. */ |
1110 if (c->ui_ops != NULL && c->ui_ops->write_im != NULL) | 1148 if (c->ui_ops != NULL && c->ui_ops->write_im != NULL) { |
1111 c->ui_ops->write_im(c, who, message, flags, mtime); | 1149 c->ui_ops->write_im(c, who, message, flags, mtime); |
1112 else | 1150 add_message_to_history(c, who, message, flags, mtime); |
1151 } else | |
1113 purple_conversation_write(c, who, message, flags, mtime); | 1152 purple_conversation_write(c, who, message, flags, mtime); |
1114 } | 1153 } |
1115 | 1154 |
1116 gboolean purple_conv_present_error(const char *who, PurpleAccount *account, const char *what) | 1155 gboolean purple_conv_present_error(const char *who, PurpleAccount *account, const char *what) |
1117 { | 1156 { |
1431 | 1470 |
1432 g_free(str); | 1471 g_free(str); |
1433 } | 1472 } |
1434 | 1473 |
1435 /* Pass this on to either the ops structure or the default write func. */ | 1474 /* Pass this on to either the ops structure or the default write func. */ |
1436 if (conv->ui_ops != NULL && conv->ui_ops->write_chat != NULL) | 1475 if (conv->ui_ops != NULL && conv->ui_ops->write_chat != NULL) { |
1437 conv->ui_ops->write_chat(conv, who, message, flags, mtime); | 1476 conv->ui_ops->write_chat(conv, who, message, flags, mtime); |
1438 else | 1477 add_message_to_history(conv, who, message, flags, mtime); |
1478 } else | |
1439 purple_conversation_write(conv, who, message, flags, mtime); | 1479 purple_conversation_write(conv, who, message, flags, mtime); |
1440 } | 1480 } |
1441 | 1481 |
1442 void | 1482 void |
1443 purple_conv_chat_send(PurpleConvChat *chat, const char *message) | 1483 purple_conv_chat_send(PurpleConvChat *chat, const char *message) |
2018 purple_signal_emit(purple_conversations_get_handle(), | 2058 purple_signal_emit(purple_conversations_get_handle(), |
2019 "conversation-extended-menu", conv, &menu); | 2059 "conversation-extended-menu", conv, &menu); |
2020 return menu; | 2060 return menu; |
2021 } | 2061 } |
2022 | 2062 |
2063 void purple_conversation_clear_message_history(PurpleConversation *conv) | |
2064 { | |
2065 GList *list = g_hash_table_lookup(histories, conv); | |
2066 message_history_free(list); | |
2067 g_hash_table_remove(histories, conv); | |
2068 } | |
2069 | |
2070 GList *purple_conversation_get_message_history(PurpleConversation *conv) | |
2071 { | |
2072 return g_hash_table_lookup(histories, conv); | |
2073 } | |
2074 | |
2075 const char *purple_conversation_message_get_sender(PurpleConvMessage *msg) | |
2076 { | |
2077 g_return_val_if_fail(msg, NULL); | |
2078 return msg->who; | |
2079 } | |
2080 | |
2081 const char *purple_conversation_message_get_message(PurpleConvMessage *msg) | |
2082 { | |
2083 g_return_val_if_fail(msg, NULL); | |
2084 return msg->what; | |
2085 } | |
2086 | |
2087 PurpleMessageFlags purple_conversation_message_get_flags(PurpleConvMessage *msg) | |
2088 { | |
2089 g_return_val_if_fail(msg, 0); | |
2090 return msg->flags; | |
2091 } | |
2092 | |
2093 time_t purple_conversation_message_get_timestamp(PurpleConvMessage *msg) | |
2094 { | |
2095 g_return_val_if_fail(msg, 0); | |
2096 return msg->when; | |
2097 } | |
2098 | |
2023 gboolean | 2099 gboolean |
2024 purple_conversation_do_command(PurpleConversation *conv, const gchar *cmdline, | 2100 purple_conversation_do_command(PurpleConversation *conv, const gchar *cmdline, |
2025 const gchar *markup, gchar **error) | 2101 const gchar *markup, gchar **error) |
2026 { | 2102 { |
2027 char *mark = (markup && *markup) ? NULL : g_markup_escape_text(cmdline, -1), *err = NULL; | 2103 char *mark = (markup && *markup) ? NULL : g_markup_escape_text(cmdline, -1), *err = NULL; |
2298 purple_signal_register(handle, "conversation-extended-menu", | 2374 purple_signal_register(handle, "conversation-extended-menu", |
2299 purple_marshal_VOID__POINTER_POINTER, NULL, 2, | 2375 purple_marshal_VOID__POINTER_POINTER, NULL, 2, |
2300 purple_value_new(PURPLE_TYPE_SUBTYPE, | 2376 purple_value_new(PURPLE_TYPE_SUBTYPE, |
2301 PURPLE_SUBTYPE_CONVERSATION), | 2377 PURPLE_SUBTYPE_CONVERSATION), |
2302 purple_value_new(PURPLE_TYPE_BOXED, "GList **")); | 2378 purple_value_new(PURPLE_TYPE_BOXED, "GList **")); |
2379 | |
2380 /* Initialize the history */ | |
2381 histories = g_hash_table_new(g_direct_hash, g_direct_equal); | |
2303 } | 2382 } |
2304 | 2383 |
2305 void | 2384 void |
2306 purple_conversations_uninit(void) | 2385 purple_conversations_uninit(void) |
2307 { | 2386 { |
2308 while (conversations) | 2387 while (conversations) |
2309 purple_conversation_destroy((PurpleConversation*)conversations->data); | 2388 purple_conversation_destroy((PurpleConversation*)conversations->data); |
2310 purple_signals_unregister_by_instance(purple_conversations_get_handle()); | 2389 purple_signals_unregister_by_instance(purple_conversations_get_handle()); |
2311 } | 2390 g_hash_table_destroy(histories); |
2391 histories = NULL; | |
2392 } |