Mercurial > pidgin
annotate finch/gntconv.c @ 20400:ea9a5566a156
propagate from branch 'im.pidgin.rlaager.merging.msnp13-and-sf-1621854-4' (head 36b8a3e05397b5918f311a046fa580c5bb8846e0)
to branch 'im.pidgin.cpw.khc.msnp14' (head 39ac2e1b7754245b292605b0e3055ebb1b954c5d)
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Sun, 15 Apr 2007 05:00:56 +0000 |
parents | 66dff3dfdea6 |
children | ab3f93232a2d |
rev | line source |
---|---|
15817 | 1 /** |
2 * @file gntconv.c GNT Conversation API | |
3 * @ingroup gntui | |
4 * | |
15870
66dff3dfdea6
Re-sed the copyright notices so they don't all talk about Purple.
Richard Laager <rlaager@wiktel.com>
parents:
15822
diff
changeset
|
5 * finch |
15817 | 6 * |
15870
66dff3dfdea6
Re-sed the copyright notices so they don't all talk about Purple.
Richard Laager <rlaager@wiktel.com>
parents:
15822
diff
changeset
|
7 * Finch is the legal property of its developers, whose names are too numerous |
15817 | 8 * to list here. Please refer to the COPYRIGHT file distributed with this |
9 * source distribution. | |
10 * | |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 */ | |
25 #include <string.h> | |
26 | |
27 #include <cmds.h> | |
28 #include <idle.h> | |
29 #include <prefs.h> | |
30 #include <util.h> | |
31 | |
15822 | 32 #include "finch.h" |
15817 | 33 #include "gntaccount.h" |
34 #include "gntblist.h" | |
35 #include "gntconv.h" | |
36 #include "gntdebug.h" | |
37 #include "gntplugin.h" | |
38 #include "gntprefs.h" | |
39 #include "gntstatus.h" | |
40 | |
41 #include "gnt.h" | |
42 #include "gntbox.h" | |
43 #include "gntentry.h" | |
44 #include "gnttextview.h" | |
45 | |
15822 | 46 #define PREF_ROOT "/purple/gnt/conversations" |
15817 | 47 |
48 #include "config.h" | |
49 | |
50 static void | |
51 send_typing_notification(GntWidget *w, FinchConv *ggconv) | |
52 { | |
53 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry)); | |
54 gboolean empty = (!text || !*text); | |
15822 | 55 if (purple_prefs_get_bool("/purple/gnt/conversations/notify_typing")) { |
56 PurpleConversation *conv = ggconv->active_conv; | |
57 PurpleConvIm *im = PURPLE_CONV_IM(conv); | |
15817 | 58 if (!empty) { |
15822 | 59 gboolean send = (purple_conv_im_get_send_typed_timeout(im) == 0); |
15817 | 60 |
15822 | 61 purple_conv_im_stop_send_typed_timeout(im); |
62 purple_conv_im_start_send_typed_timeout(im); | |
63 if (send || (purple_conv_im_get_type_again(im) != 0 && | |
64 time(NULL) > purple_conv_im_get_type_again(im))) { | |
15817 | 65 unsigned int timeout; |
15822 | 66 timeout = serv_send_typing(purple_conversation_get_gc(conv), |
67 purple_conversation_get_name(conv), | |
68 PURPLE_TYPING); | |
69 purple_conv_im_set_type_again(im, timeout); | |
15817 | 70 } |
71 } else { | |
15822 | 72 purple_conv_im_stop_send_typed_timeout(im); |
15817 | 73 |
15822 | 74 serv_send_typing(purple_conversation_get_gc(conv), |
75 purple_conversation_get_name(conv), | |
76 PURPLE_NOT_TYPING); | |
15817 | 77 } |
78 } | |
79 } | |
80 | |
81 static gboolean | |
82 entry_key_pressed(GntWidget *w, const char *key, FinchConv *ggconv) | |
83 { | |
84 if (key[0] == '\r' && key[1] == 0) | |
85 { | |
86 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry)); | |
87 if (*text == '/') | |
88 { | |
15822 | 89 PurpleConversation *conv = ggconv->active_conv; |
90 PurpleCmdStatus status; | |
15817 | 91 const char *cmdline = text + 1; |
92 char *error = NULL, *escape; | |
93 | |
94 escape = g_markup_escape_text(cmdline, -1); | |
15822 | 95 status = purple_cmd_do_command(conv, cmdline, escape, &error); |
15817 | 96 g_free(escape); |
97 | |
98 switch (status) | |
99 { | |
15822 | 100 case PURPLE_CMD_STATUS_OK: |
15817 | 101 break; |
15822 | 102 case PURPLE_CMD_STATUS_NOT_FOUND: |
103 purple_conversation_write(conv, "", _("No such command."), | |
104 PURPLE_MESSAGE_NO_LOG, time(NULL)); | |
15817 | 105 break; |
15822 | 106 case PURPLE_CMD_STATUS_WRONG_ARGS: |
107 purple_conversation_write(conv, "", _("Syntax Error: You typed the wrong number of arguments " | |
15817 | 108 "to that command."), |
15822 | 109 PURPLE_MESSAGE_NO_LOG, time(NULL)); |
15817 | 110 break; |
15822 | 111 case PURPLE_CMD_STATUS_FAILED: |
112 purple_conversation_write(conv, "", error ? error : _("Your command failed for an unknown reason."), | |
113 PURPLE_MESSAGE_NO_LOG, time(NULL)); | |
15817 | 114 break; |
15822 | 115 case PURPLE_CMD_STATUS_WRONG_TYPE: |
116 if(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) | |
117 purple_conversation_write(conv, "", _("That command only works in chats, not IMs."), | |
118 PURPLE_MESSAGE_NO_LOG, time(NULL)); | |
15817 | 119 else |
15822 | 120 purple_conversation_write(conv, "", _("That command only works in IMs, not chats."), |
121 PURPLE_MESSAGE_NO_LOG, time(NULL)); | |
15817 | 122 break; |
15822 | 123 case PURPLE_CMD_STATUS_WRONG_PRPL: |
124 purple_conversation_write(conv, "", _("That command doesn't work on this protocol."), | |
125 PURPLE_MESSAGE_NO_LOG, time(NULL)); | |
15817 | 126 break; |
127 } | |
128 g_free(error); | |
129 #if 0 | |
130 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), | |
131 _("Commands are not supported yet. Message was NOT sent."), | |
132 GNT_TEXT_FLAG_DIM | GNT_TEXT_FLAG_UNDERLINE); | |
133 gnt_text_view_next_line(GNT_TEXT_VIEW(ggconv->tv)); | |
134 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 0); | |
135 #endif | |
136 } | |
137 else | |
138 { | |
139 char *escape = g_markup_escape_text(text, -1); | |
15822 | 140 char *apos = purple_strreplace(escape, "'", "'"); |
15817 | 141 g_free(escape); |
142 escape = apos; | |
15822 | 143 switch (purple_conversation_get_type(ggconv->active_conv)) |
15817 | 144 { |
15822 | 145 case PURPLE_CONV_TYPE_IM: |
146 purple_conv_im_send_with_flags(PURPLE_CONV_IM(ggconv->active_conv), escape, PURPLE_MESSAGE_SEND); | |
15817 | 147 break; |
15822 | 148 case PURPLE_CONV_TYPE_CHAT: |
149 purple_conv_chat_send(PURPLE_CONV_CHAT(ggconv->active_conv), escape); | |
15817 | 150 break; |
151 default: | |
152 g_free(escape); | |
153 g_return_val_if_reached(FALSE); | |
154 } | |
155 g_free(escape); | |
15822 | 156 purple_idle_touch(); |
15817 | 157 } |
158 gnt_entry_add_to_history(GNT_ENTRY(ggconv->entry), text); | |
159 gnt_entry_clear(GNT_ENTRY(ggconv->entry)); | |
160 return TRUE; | |
161 } | |
162 else if (key[0] == 27) | |
163 { | |
164 if (strcmp(key, GNT_KEY_DOWN) == 0) | |
165 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 1); | |
166 else if (strcmp(key, GNT_KEY_UP) == 0) | |
167 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), -1); | |
168 else if (strcmp(key, GNT_KEY_PGDOWN) == 0) | |
169 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), ggconv->tv->priv.height - 2); | |
170 else if (strcmp(key, GNT_KEY_PGUP) == 0) | |
171 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), -(ggconv->tv->priv.height - 2)); | |
172 else | |
173 return FALSE; | |
174 return TRUE; | |
175 } | |
176 else | |
177 { | |
178 } | |
179 | |
180 return FALSE; | |
181 } | |
182 | |
183 static void | |
184 closing_window(GntWidget *window, FinchConv *ggconv) | |
185 { | |
186 GList *list = ggconv->list; | |
187 ggconv->window = NULL; | |
188 while (list) { | |
15822 | 189 PurpleConversation *conv = list->data; |
15817 | 190 list = list->next; |
15822 | 191 purple_conversation_destroy(conv); |
15817 | 192 } |
193 } | |
194 | |
195 static void | |
196 size_changed_cb(GntWidget *widget, int width, int height) | |
197 { | |
198 int w, h; | |
199 gnt_widget_get_size(widget, &w, &h); | |
15822 | 200 purple_prefs_set_int(PREF_ROOT "/size/width", w); |
201 purple_prefs_set_int(PREF_ROOT "/size/height", h); | |
15817 | 202 } |
203 | |
204 static void | |
205 save_position_cb(GntWidget *w, int x, int y) | |
206 { | |
15822 | 207 purple_prefs_set_int(PREF_ROOT "/position/x", x); |
208 purple_prefs_set_int(PREF_ROOT "/position/y", y); | |
15817 | 209 } |
210 | |
15822 | 211 static PurpleConversation * |
212 find_conv_with_contact(PurpleConversation *conv) | |
15817 | 213 { |
15822 | 214 PurpleBlistNode *node; |
215 PurpleBuddy *buddy = purple_find_buddy(conv->account, conv->name); | |
216 PurpleConversation *ret = NULL; | |
15817 | 217 |
218 if (!buddy) | |
219 return NULL; | |
220 | |
15822 | 221 for (node = ((PurpleBlistNode*)buddy)->parent->child; node; node = node->next) { |
222 if (node == (PurpleBlistNode*)buddy) | |
15817 | 223 continue; |
15822 | 224 if ((ret = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, |
225 ((PurpleBuddy*)node)->name, ((PurpleBuddy*)node)->account)) != NULL) | |
15817 | 226 break; |
227 } | |
228 return ret; | |
229 } | |
230 | |
231 static char * | |
15822 | 232 get_conversation_title(PurpleConversation *conv, PurpleAccount *account) |
15817 | 233 { |
15822 | 234 return g_strdup_printf(_("%s (%s -- %s)"), purple_conversation_get_title(conv), |
235 purple_account_get_username(account), purple_account_get_protocol_name(account)); | |
15817 | 236 } |
237 | |
238 static void | |
15822 | 239 update_buddy_typing(PurpleAccount *account, const char *who, gpointer null) |
15817 | 240 { |
15822 | 241 PurpleConversation *conv; |
15817 | 242 FinchConv *ggc; |
15822 | 243 PurpleConvIm *im = NULL; |
15817 | 244 char *title, *str; |
245 | |
15822 | 246 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, account); |
15817 | 247 |
248 if (!conv) | |
249 return; | |
250 | |
15822 | 251 im = PURPLE_CONV_IM(conv); |
15817 | 252 ggc = conv->ui_data; |
253 | |
15822 | 254 if (purple_conv_im_get_typing_state(im) == PURPLE_TYPING) { |
15817 | 255 int scroll; |
256 str = get_conversation_title(conv, account); | |
257 title = g_strdup_printf(_("%s [%s]"), str, | |
258 gnt_ascii_only() ? "T" : "\342\243\277"); | |
259 g_free(str); | |
260 | |
261 scroll = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggc->tv)); | |
15822 | 262 str = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_name(conv)); |
15817 | 263 /* Updating is a little buggy. So just remove and add a new one */ |
264 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE); | |
265 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggc->tv), | |
266 str, GNT_TEXT_FLAG_DIM, "typing"); | |
267 g_free(str); | |
268 if (scroll <= 1) | |
269 gnt_text_view_scroll(GNT_TEXT_VIEW(ggc->tv), 0); | |
270 } else { | |
271 title = get_conversation_title(conv, account); | |
272 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE); | |
273 } | |
274 gnt_screen_rename_widget(ggc->window, title); | |
275 g_free(title); | |
276 } | |
277 | |
278 static gpointer | |
279 finch_conv_get_handle() | |
280 { | |
281 static int handle; | |
282 return &handle; | |
283 } | |
284 | |
285 static void | |
15822 | 286 finch_create_conversation(PurpleConversation *conv) |
15817 | 287 { |
288 FinchConv *ggc = conv->ui_data; | |
289 char *title; | |
15822 | 290 PurpleConversationType type; |
291 PurpleConversation *cc; | |
292 PurpleAccount *account; | |
15817 | 293 |
294 if (ggc) | |
295 return; | |
296 | |
297 cc = find_conv_with_contact(conv); | |
298 if (cc && cc->ui_data) | |
299 ggc = cc->ui_data; | |
300 else | |
301 ggc = g_new0(FinchConv, 1); | |
302 | |
303 ggc->list = g_list_prepend(ggc->list, conv); | |
304 ggc->active_conv = conv; | |
305 conv->ui_data = ggc; | |
306 | |
307 if (cc && cc->ui_data) { | |
308 finch_conversation_set_active(conv); | |
309 return; | |
310 } | |
311 | |
15822 | 312 account = purple_conversation_get_account(conv); |
313 type = purple_conversation_get_type(conv); | |
15817 | 314 title = get_conversation_title(conv, account); |
315 | |
316 ggc->window = gnt_box_new(FALSE, TRUE); | |
317 gnt_box_set_title(GNT_BOX(ggc->window), title); | |
318 gnt_box_set_toplevel(GNT_BOX(ggc->window), TRUE); | |
319 gnt_box_set_pad(GNT_BOX(ggc->window), 0); | |
320 gnt_widget_set_name(ggc->window, "conversation-window"); | |
321 | |
322 ggc->tv = gnt_text_view_new(); | |
323 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->tv); | |
324 gnt_widget_set_name(ggc->tv, "conversation-window-textview"); | |
15822 | 325 gnt_widget_set_size(ggc->tv, purple_prefs_get_int(PREF_ROOT "/size/width"), |
326 purple_prefs_get_int(PREF_ROOT "/size/height")); | |
15817 | 327 |
328 ggc->entry = gnt_entry_new(NULL); | |
329 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->entry); | |
330 gnt_widget_set_name(ggc->entry, "conversation-window-entry"); | |
331 gnt_entry_set_history_length(GNT_ENTRY(ggc->entry), -1); | |
332 gnt_entry_set_word_suggest(GNT_ENTRY(ggc->entry), TRUE); | |
333 gnt_entry_set_always_suggest(GNT_ENTRY(ggc->entry), FALSE); | |
334 | |
335 g_signal_connect_after(G_OBJECT(ggc->entry), "key_pressed", G_CALLBACK(entry_key_pressed), ggc); | |
336 g_signal_connect(G_OBJECT(ggc->window), "destroy", G_CALLBACK(closing_window), ggc); | |
337 | |
15822 | 338 gnt_widget_set_position(ggc->window, purple_prefs_get_int(PREF_ROOT "/position/x"), |
339 purple_prefs_get_int(PREF_ROOT "/position/y")); | |
15817 | 340 gnt_widget_show(ggc->window); |
341 | |
342 g_signal_connect(G_OBJECT(ggc->tv), "size_changed", G_CALLBACK(size_changed_cb), NULL); | |
343 g_signal_connect(G_OBJECT(ggc->window), "position_set", G_CALLBACK(save_position_cb), NULL); | |
344 | |
15822 | 345 if (type == PURPLE_CONV_TYPE_IM) { |
15817 | 346 g_signal_connect(G_OBJECT(ggc->entry), "text_changed", G_CALLBACK(send_typing_notification), ggc); |
15822 | 347 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", finch_conv_get_handle(), |
348 PURPLE_CALLBACK(update_buddy_typing), NULL); | |
349 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", finch_conv_get_handle(), | |
350 PURPLE_CALLBACK(update_buddy_typing), NULL); | |
15817 | 351 } |
352 | |
353 g_free(title); | |
354 } | |
355 | |
356 static void | |
15822 | 357 finch_destroy_conversation(PurpleConversation *conv) |
15817 | 358 { |
359 /* do stuff here */ | |
360 FinchConv *ggc = conv->ui_data; | |
361 ggc->list = g_list_remove(ggc->list, conv); | |
362 if (ggc->list && conv == ggc->active_conv) | |
363 ggc->active_conv = ggc->list->data; | |
364 | |
365 if (ggc->list == NULL) { | |
366 gnt_widget_destroy(ggc->window); | |
367 g_free(ggc); | |
368 } | |
369 } | |
370 | |
371 static void | |
15822 | 372 finch_write_common(PurpleConversation *conv, const char *who, const char *message, |
373 PurpleMessageFlags flags, time_t mtime) | |
15817 | 374 { |
375 FinchConv *ggconv = conv->ui_data; | |
376 char *strip, *newline; | |
377 GntTextFormatFlags fl = 0; | |
378 int pos; | |
379 gboolean notify; | |
380 | |
381 g_return_if_fail(ggconv != NULL); | |
382 | |
383 if (ggconv->active_conv != conv) { | |
15822 | 384 if (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV)) |
15817 | 385 finch_conversation_set_active(conv); |
386 else | |
387 return; | |
388 } | |
389 | |
390 pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggconv->tv)); | |
391 | |
392 notify = !!gnt_text_view_tag_change(GNT_TEXT_VIEW(ggconv->tv), "typing", NULL, TRUE); | |
393 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), "\n", GNT_TEXT_FLAG_NORMAL); | |
394 | |
395 /* Unnecessary to print the timestamp for delayed message */ | |
15822 | 396 if (!(flags & PURPLE_MESSAGE_DELAYED) && |
397 purple_prefs_get_bool("/purple/gnt/conversations/timestamps")) | |
15817 | 398 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
15822 | 399 purple_utf8_strftime("(%H:%M:%S) ", localtime(&mtime)), GNT_TEXT_FLAG_DIM); |
15817 | 400 |
15822 | 401 if (flags & PURPLE_MESSAGE_AUTO_RESP) |
15817 | 402 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
403 _("<AUTO-REPLY> "), GNT_TEXT_FLAG_BOLD); | |
404 | |
15822 | 405 if (who && *who && (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV))) |
15817 | 406 { |
407 char * name = NULL; | |
408 | |
15822 | 409 if (purple_message_meify((char*)message, -1)) |
15817 | 410 name = g_strdup_printf("*** %s ", who); |
411 else | |
412 name = g_strdup_printf("%s: ", who); | |
413 | |
414 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), | |
415 name, GNT_TEXT_FLAG_BOLD); | |
416 g_free(name); | |
417 } | |
418 else | |
419 fl = GNT_TEXT_FLAG_DIM; | |
420 | |
15822 | 421 if (flags & PURPLE_MESSAGE_ERROR) |
15817 | 422 fl |= GNT_TEXT_FLAG_BOLD; |
15822 | 423 if (flags & PURPLE_MESSAGE_NICK) |
15817 | 424 fl |= GNT_TEXT_FLAG_UNDERLINE; |
425 | |
426 /* XXX: Remove this workaround when textview can parse messages. */ | |
15822 | 427 newline = purple_strdup_withhtml(message); |
428 strip = purple_markup_strip_html(newline); | |
15817 | 429 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
430 strip, fl); | |
431 | |
432 g_free(newline); | |
433 g_free(strip); | |
434 | |
435 if (notify) { | |
15822 | 436 strip = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_name(conv)); |
15817 | 437 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggconv->tv), |
438 strip, GNT_TEXT_FLAG_DIM, "typing"); | |
439 g_free(strip); | |
440 } | |
441 | |
442 if (pos <= 1) | |
443 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 0); | |
444 | |
15822 | 445 if (flags & (PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_NICK | PURPLE_MESSAGE_ERROR)) |
15817 | 446 gnt_widget_set_urgent(ggconv->tv); |
447 } | |
448 | |
449 static void | |
15822 | 450 finch_write_chat(PurpleConversation *conv, const char *who, const char *message, |
451 PurpleMessageFlags flags, time_t mtime) | |
15817 | 452 { |
15822 | 453 purple_conversation_write(conv, who, message, flags, mtime); |
15817 | 454 } |
455 | |
456 static void | |
15822 | 457 finch_write_im(PurpleConversation *conv, const char *who, const char *message, |
458 PurpleMessageFlags flags, time_t mtime) | |
15817 | 459 { |
15822 | 460 PurpleAccount *account = purple_conversation_get_account(conv); |
461 if (flags & PURPLE_MESSAGE_SEND) | |
15817 | 462 { |
15822 | 463 who = purple_connection_get_display_name(purple_account_get_connection(account)); |
15817 | 464 if (!who) |
15822 | 465 who = purple_account_get_alias(account); |
15817 | 466 if (!who) |
15822 | 467 who = purple_account_get_username(account); |
15817 | 468 } |
15822 | 469 else if (flags & PURPLE_MESSAGE_RECV) |
15817 | 470 { |
15822 | 471 PurpleBuddy *buddy; |
472 who = purple_conversation_get_name(conv); | |
473 buddy = purple_find_buddy(account, who); | |
15817 | 474 if (buddy) |
15822 | 475 who = purple_buddy_get_contact_alias(buddy); |
15817 | 476 } |
477 | |
15822 | 478 purple_conversation_write(conv, who, message, flags, mtime); |
15817 | 479 } |
480 | |
481 static void | |
15822 | 482 finch_write_conv(PurpleConversation *conv, const char *who, const char *alias, |
483 const char *message, PurpleMessageFlags flags, time_t mtime) | |
15817 | 484 { |
485 const char *name; | |
486 if (alias && *alias) | |
487 name = alias; | |
488 else if (who && *who) | |
489 name = who; | |
490 else | |
491 name = NULL; | |
492 | |
493 finch_write_common(conv, name, message, flags, mtime); | |
494 } | |
495 | |
496 static void | |
15822 | 497 finch_chat_add_users(PurpleConversation *conv, GList *users, gboolean new_arrivals) |
15817 | 498 { |
499 FinchConv *ggc = conv->ui_data; | |
500 GntEntry *entry = GNT_ENTRY(ggc->entry); | |
501 | |
502 if (!new_arrivals) | |
503 { | |
504 /* Print the list of users in the room */ | |
505 GString *string = g_string_new(_("List of users:\n")); | |
506 GList *iter; | |
507 | |
508 for (iter = users; iter; iter = iter->next) | |
509 { | |
15822 | 510 PurpleConvChatBuddy *cbuddy = iter->data; |
15817 | 511 char *str; |
512 | |
513 if ((str = cbuddy->alias) == NULL) | |
514 str = cbuddy->name; | |
515 g_string_append_printf(string, "[ %s ]", str); | |
516 } | |
517 | |
15822 | 518 purple_conversation_write(conv, NULL, string->str, |
519 PURPLE_MESSAGE_SYSTEM, time(NULL)); | |
15817 | 520 g_string_free(string, TRUE); |
521 } | |
522 | |
523 for (; users; users = users->next) | |
524 { | |
15822 | 525 PurpleConvChatBuddy *cbuddy = users->data; |
15817 | 526 gnt_entry_add_suggest(entry, cbuddy->name); |
527 gnt_entry_add_suggest(entry, cbuddy->alias); | |
528 } | |
529 } | |
530 | |
531 static void | |
15822 | 532 finch_chat_rename_user(PurpleConversation *conv, const char *old, const char *new_n, const char *new_a) |
15817 | 533 { |
534 /* Update the name for string completion */ | |
535 FinchConv *ggc = conv->ui_data; | |
536 GntEntry *entry = GNT_ENTRY(ggc->entry); | |
537 gnt_entry_remove_suggest(entry, old); | |
538 gnt_entry_add_suggest(entry, new_n); | |
539 gnt_entry_add_suggest(entry, new_a); | |
540 } | |
541 | |
542 static void | |
15822 | 543 finch_chat_remove_user(PurpleConversation *conv, GList *list) |
15817 | 544 { |
545 /* Remove the name from string completion */ | |
546 FinchConv *ggc = conv->ui_data; | |
547 GntEntry *entry = GNT_ENTRY(ggc->entry); | |
548 for (; list; list = list->next) | |
549 gnt_entry_remove_suggest(entry, list->data); | |
550 } | |
551 | |
552 static void | |
15822 | 553 finch_chat_update_user(PurpleConversation *conv, const char *user) |
15817 | 554 { |
555 } | |
556 | |
15822 | 557 static PurpleConversationUiOps conv_ui_ops = |
15817 | 558 { |
559 .create_conversation = finch_create_conversation, | |
560 .destroy_conversation = finch_destroy_conversation, | |
561 .write_chat = finch_write_chat, | |
562 .write_im = finch_write_im, | |
563 .write_conv = finch_write_conv, | |
564 .chat_add_users = finch_chat_add_users, | |
565 .chat_rename_user = finch_chat_rename_user, | |
566 .chat_remove_users = finch_chat_remove_user, | |
567 .chat_update_user = finch_chat_update_user, | |
568 .present = NULL, | |
569 .has_focus = NULL, | |
570 .custom_smiley_add = NULL, | |
571 .custom_smiley_write = NULL, | |
572 .custom_smiley_close = NULL | |
573 }; | |
574 | |
15822 | 575 PurpleConversationUiOps *finch_conv_get_ui_ops() |
15817 | 576 { |
577 return &conv_ui_ops; | |
578 } | |
579 | |
580 /* Xerox */ | |
15822 | 581 static PurpleCmdRet |
582 say_command_cb(PurpleConversation *conv, | |
15817 | 583 const char *cmd, char **args, char **error, void *data) |
584 { | |
15822 | 585 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) |
586 purple_conv_im_send(PURPLE_CONV_IM(conv), args[0]); | |
587 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) | |
588 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), args[0]); | |
15817 | 589 |
15822 | 590 return PURPLE_CMD_RET_OK; |
15817 | 591 } |
592 | |
593 /* Xerox */ | |
15822 | 594 static PurpleCmdRet |
595 me_command_cb(PurpleConversation *conv, | |
15817 | 596 const char *cmd, char **args, char **error, void *data) |
597 { | |
598 char *tmp; | |
599 | |
600 tmp = g_strdup_printf("/me %s", args[0]); | |
601 | |
15822 | 602 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) |
603 purple_conv_im_send(PURPLE_CONV_IM(conv), tmp); | |
604 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) | |
605 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), tmp); | |
15817 | 606 |
607 g_free(tmp); | |
15822 | 608 return PURPLE_CMD_RET_OK; |
15817 | 609 } |
610 | |
611 /* Xerox */ | |
15822 | 612 static PurpleCmdRet |
613 debug_command_cb(PurpleConversation *conv, | |
15817 | 614 const char *cmd, char **args, char **error, void *data) |
615 { | |
616 char *tmp, *markup; | |
15822 | 617 PurpleCmdStatus status; |
15817 | 618 |
619 if (!g_ascii_strcasecmp(args[0], "version")) { | |
620 tmp = g_strdup_printf("me is using %s.", VERSION); | |
621 markup = g_markup_escape_text(tmp, -1); | |
622 | |
15822 | 623 status = purple_cmd_do_command(conv, tmp, markup, error); |
15817 | 624 |
625 g_free(tmp); | |
626 g_free(markup); | |
627 return status; | |
628 } else { | |
15822 | 629 purple_conversation_write(conv, NULL, _("Supported debug options are: version"), |
630 PURPLE_MESSAGE_NO_LOG|PURPLE_MESSAGE_ERROR, time(NULL)); | |
631 return PURPLE_CMD_STATUS_OK; | |
15817 | 632 } |
633 } | |
634 | |
635 /* Xerox */ | |
15822 | 636 static PurpleCmdRet |
637 clear_command_cb(PurpleConversation *conv, | |
15817 | 638 const char *cmd, char **args, char **error, void *data) |
639 { | |
640 FinchConv *ggconv = conv->ui_data; | |
641 gnt_text_view_clear(GNT_TEXT_VIEW(ggconv->tv)); | |
15822 | 642 return PURPLE_CMD_STATUS_OK; |
15817 | 643 } |
644 | |
645 /* Xerox */ | |
15822 | 646 static PurpleCmdRet |
647 help_command_cb(PurpleConversation *conv, | |
15817 | 648 const char *cmd, char **args, char **error, void *data) |
649 { | |
650 GList *l, *text; | |
651 GString *s; | |
652 | |
653 if (args[0] != NULL) { | |
654 s = g_string_new(""); | |
15822 | 655 text = purple_cmd_help(conv, args[0]); |
15817 | 656 |
657 if (text) { | |
658 for (l = text; l; l = l->next) | |
659 if (l->next) | |
660 g_string_append_printf(s, "%s\n", (char *)l->data); | |
661 else | |
662 g_string_append_printf(s, "%s", (char *)l->data); | |
663 } else { | |
664 g_string_append(s, _("No such command (in this context).")); | |
665 } | |
666 } else { | |
667 s = g_string_new(_("Use \"/help <command>\" for help on a specific command.\n" | |
668 "The following commands are available in this context:\n")); | |
669 | |
15822 | 670 text = purple_cmd_list(conv); |
15817 | 671 for (l = text; l; l = l->next) |
672 if (l->next) | |
673 g_string_append_printf(s, "%s, ", (char *)l->data); | |
674 else | |
675 g_string_append_printf(s, "%s.", (char *)l->data); | |
676 g_list_free(text); | |
677 } | |
678 | |
15822 | 679 purple_conversation_write(conv, NULL, s->str, PURPLE_MESSAGE_NO_LOG, time(NULL)); |
15817 | 680 g_string_free(s, TRUE); |
681 | |
15822 | 682 return PURPLE_CMD_STATUS_OK; |
15817 | 683 } |
684 | |
15822 | 685 static PurpleCmdRet |
686 cmd_show_window(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data) | |
15817 | 687 { |
688 void (*callback)() = data; | |
689 callback(); | |
15822 | 690 return PURPLE_CMD_STATUS_OK; |
15817 | 691 } |
692 | |
693 void finch_conversation_init() | |
694 { | |
15822 | 695 purple_prefs_add_none(PREF_ROOT); |
696 purple_prefs_add_none(PREF_ROOT "/size"); | |
697 purple_prefs_add_int(PREF_ROOT "/size/width", 70); | |
698 purple_prefs_add_int(PREF_ROOT "/size/height", 20); | |
699 purple_prefs_add_none(PREF_ROOT "/position"); | |
700 purple_prefs_add_int(PREF_ROOT "/position/x", 0); | |
701 purple_prefs_add_int(PREF_ROOT "/position/y", 0); | |
15817 | 702 |
703 /* Xerox the commands */ | |
15822 | 704 purple_cmd_register("say", "S", PURPLE_CMD_P_DEFAULT, |
705 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 706 say_command_cb, _("say <message>: Send a message normally as if you weren't using a command."), NULL); |
15822 | 707 purple_cmd_register("me", "S", PURPLE_CMD_P_DEFAULT, |
708 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 709 me_command_cb, _("me <action>: Send an IRC style action to a buddy or chat."), NULL); |
15822 | 710 purple_cmd_register("debug", "w", PURPLE_CMD_P_DEFAULT, |
711 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 712 debug_command_cb, _("debug <option>: Send various debug information to the current conversation."), NULL); |
15822 | 713 purple_cmd_register("clear", "", PURPLE_CMD_P_DEFAULT, |
714 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 715 clear_command_cb, _("clear: Clears the conversation scrollback."), NULL); |
15822 | 716 purple_cmd_register("help", "w", PURPLE_CMD_P_DEFAULT, |
717 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL, | |
15817 | 718 help_command_cb, _("help <command>: Help on a specific command."), NULL); |
719 | |
720 /* Now some commands to bring up some other windows */ | |
15822 | 721 purple_cmd_register("plugins", "", PURPLE_CMD_P_DEFAULT, |
722 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 723 cmd_show_window, _("plugins: Show the plugins window."), finch_plugins_show_all); |
15822 | 724 purple_cmd_register("buddylist", "", PURPLE_CMD_P_DEFAULT, |
725 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 726 cmd_show_window, _("buddylist: Show the buddylist."), finch_blist_show); |
15822 | 727 purple_cmd_register("accounts", "", PURPLE_CMD_P_DEFAULT, |
728 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 729 cmd_show_window, _("accounts: Show the accounts window."), finch_accounts_show_all); |
15822 | 730 purple_cmd_register("debugwin", "", PURPLE_CMD_P_DEFAULT, |
731 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 732 cmd_show_window, _("debugwin: Show the debug window."), finch_debug_window_show); |
15822 | 733 purple_cmd_register("prefs", "", PURPLE_CMD_P_DEFAULT, |
734 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 735 cmd_show_window, _("prefs: Show the preference window."), finch_prefs_show_all); |
15822 | 736 purple_cmd_register("status", "", PURPLE_CMD_P_DEFAULT, |
737 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 738 cmd_show_window, _("statuses: Show the savedstatuses window."), finch_savedstatus_show_all); |
739 } | |
740 | |
741 void finch_conversation_uninit() | |
742 { | |
743 } | |
744 | |
15822 | 745 void finch_conversation_set_active(PurpleConversation *conv) |
15817 | 746 { |
747 FinchConv *ggconv = conv->ui_data; | |
15822 | 748 PurpleAccount *account; |
15817 | 749 char *title; |
750 | |
751 g_return_if_fail(ggconv); | |
752 g_return_if_fail(g_list_find(ggconv->list, conv)); | |
753 | |
754 ggconv->active_conv = conv; | |
15822 | 755 account = purple_conversation_get_account(conv); |
15817 | 756 title = get_conversation_title(conv, account); |
757 gnt_screen_rename_widget(ggconv->window, title); | |
758 g_free(title); | |
759 } | |
760 |