Move thrasher_write_conv to thconversations.c to imitate other UI code layouts.
[thrasher.git] / thconversations.c
blob44ecaeaca59d623f45cf9cc3b1dd2deac5d57ed9
1 /*
2 * Thrasher Bird - XMPP transport via libpurple
3 * Copyright (C) 2008 Barracuda Networks, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Thrasher Bird; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
20 #include <glib.h>
21 #include "thrasher.h"
22 #include "thperl.h"
24 #include <purple.h>
26 #include "thconversations.h"
28 static void
29 thrasher_destroy_conversation(PurpleConversation *conv) {
30 purple_debug_info("thrasher", "destroy conversation\n");
33 PurpleConversation*
34 thrasher_find_conversation_with_account(PurpleAccount* pa,
35 const char* who) {
36 PurpleConversation *conv;
38 /* For testing and the initial build we're recreating a
39 * conversation *every time*
41 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, pa);
43 /* If not, create a new one */
44 if (conv == NULL) {
45 conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, pa, who);
48 return conv;
52 static void
53 thrasher_write_conv(PurpleConversation *conv,
54 const char *who,
55 const char *alias,
56 const char *message,
57 PurpleMessageFlags flags,
58 time_t mtime) {
59 PurpleAccount *account;
60 account = purple_conversation_get_account(conv);
62 /* libpurple throws write_conv on every recv and send */
63 if (flags != PURPLE_MESSAGE_RECV)
64 return;
66 purple_debug_info("thrasher",
67 "Incoming message:\n");
69 thrasher_wrapper_incoming_msg(thrasher_account_get_jid(account),
70 who,
71 alias,
72 message);
74 #ifdef TH_DEBUG
75 purple_debug_info("thrasher",
76 "(%s) %s %s: %s\n",
77 purple_conversation_get_name(conv),
78 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)),
79 who,
80 message);
81 #endif /* TH_DEBUG */
84 gboolean
85 thrasher_outgoing_chatstate(PurpleAccount* pa,
86 char* recipient,
87 PurpleTypingState chatstate) {
88 g_return_val_if_fail(pa != NULL, FALSE);
89 g_return_val_if_fail(recipient != NULL, FALSE);
91 PurpleConversation *conv
92 = thrasher_find_conversation_with_account(pa, recipient);
93 g_return_val_if_fail(conv != NULL, FALSE);
95 PurpleConnection *gc = purple_conversation_get_gc(conv);
96 if (gc == NULL) {
97 return FALSE;
100 unsigned int timeout = serv_send_typing(gc, recipient, chatstate);
101 if (chatstate == PURPLE_TYPING) {
102 /* TODO: if the prpl requests the typing notification be resent
103 * after timeout, "normal" UIs can check get_type_again() each
104 * keystroke. Should Thrasher add a event loop timeout? (and rm it
105 * if the conv ended or the state was no longer typing...)
107 PurpleConvIm *im = PURPLE_CONV_IM(conv);
108 purple_conv_im_set_type_again(im, timeout);
110 return TRUE;
113 /* incoming_chatstate_cb(pa, who):
115 * Account "pa" got a typing notification state change from the contact "who".
117 static void
118 incoming_chatstate_cb(PurpleAccount *pa,
119 const char *who) {
120 PurpleConversation *conv
121 = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM,
122 who,
123 pa);
124 if (! conv
125 || purple_conversation_get_type(conv) != PURPLE_CONV_TYPE_IM) {
126 return;
129 PurpleConvIm *im = PURPLE_CONV_IM(conv);
130 if (! im) {
131 return;
134 thrasher_wrapper_incoming_chatstate(pa,
135 who,
136 purple_conv_im_get_typing_state(im));
139 static gpointer
140 thrasher_conversations_get_handle() {
141 static int handle;
142 return &handle;
145 static PurpleConversationUiOps thrasher_conv_uiops =
147 NULL, /* create_conversation */
148 thrasher_destroy_conversation, /* destroy_conversation */
149 NULL, /* write_chat */
150 NULL, /* write_im */
151 thrasher_write_conv, /* write_conv */
152 NULL, /* chat_add_users */
153 NULL, /* chat_rename_user */
154 NULL, /* chat_remove_users */
155 NULL, /* chat_update_user */
156 NULL, /* present */
157 NULL, /* has_focus */
158 NULL, /* custom_smiley_add */
159 NULL, /* custom_smiley_write */
160 NULL, /* custom_smiley_close */
161 NULL, /* send_confirm */
162 NULL,
163 NULL,
164 NULL,
165 NULL
168 void
169 thrasher_conversations_init(void) {
170 purple_conversations_set_ui_ops(&thrasher_conv_uiops);
172 purple_signal_connect(purple_conversations_get_handle(),
173 "buddy-typing",
174 thrasher_conversations_get_handle(),
175 PURPLE_CALLBACK(incoming_chatstate_cb),
176 NULL);
177 purple_signal_connect(purple_conversations_get_handle(),
178 "buddy-typing-stopped",
179 thrasher_conversations_get_handle(),
180 PURPLE_CALLBACK(incoming_chatstate_cb),
181 NULL);