Mercurial > pidgin
annotate finch/gntconv.c @ 16124:ab3f93232a2d
Add a utility function to assist scrolling in a textview.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sun, 15 Apr 2007 05:42:14 +0000 |
parents | 66dff3dfdea6 |
children | 7a2ffa981c1a |
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 | |
163 return FALSE; | |
164 } | |
165 | |
166 static void | |
167 closing_window(GntWidget *window, FinchConv *ggconv) | |
168 { | |
169 GList *list = ggconv->list; | |
170 ggconv->window = NULL; | |
171 while (list) { | |
15822 | 172 PurpleConversation *conv = list->data; |
15817 | 173 list = list->next; |
15822 | 174 purple_conversation_destroy(conv); |
15817 | 175 } |
176 } | |
177 | |
178 static void | |
179 size_changed_cb(GntWidget *widget, int width, int height) | |
180 { | |
181 int w, h; | |
182 gnt_widget_get_size(widget, &w, &h); | |
15822 | 183 purple_prefs_set_int(PREF_ROOT "/size/width", w); |
184 purple_prefs_set_int(PREF_ROOT "/size/height", h); | |
15817 | 185 } |
186 | |
187 static void | |
188 save_position_cb(GntWidget *w, int x, int y) | |
189 { | |
15822 | 190 purple_prefs_set_int(PREF_ROOT "/position/x", x); |
191 purple_prefs_set_int(PREF_ROOT "/position/y", y); | |
15817 | 192 } |
193 | |
15822 | 194 static PurpleConversation * |
195 find_conv_with_contact(PurpleConversation *conv) | |
15817 | 196 { |
15822 | 197 PurpleBlistNode *node; |
198 PurpleBuddy *buddy = purple_find_buddy(conv->account, conv->name); | |
199 PurpleConversation *ret = NULL; | |
15817 | 200 |
201 if (!buddy) | |
202 return NULL; | |
203 | |
15822 | 204 for (node = ((PurpleBlistNode*)buddy)->parent->child; node; node = node->next) { |
205 if (node == (PurpleBlistNode*)buddy) | |
15817 | 206 continue; |
15822 | 207 if ((ret = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, |
208 ((PurpleBuddy*)node)->name, ((PurpleBuddy*)node)->account)) != NULL) | |
15817 | 209 break; |
210 } | |
211 return ret; | |
212 } | |
213 | |
214 static char * | |
15822 | 215 get_conversation_title(PurpleConversation *conv, PurpleAccount *account) |
15817 | 216 { |
15822 | 217 return g_strdup_printf(_("%s (%s -- %s)"), purple_conversation_get_title(conv), |
218 purple_account_get_username(account), purple_account_get_protocol_name(account)); | |
15817 | 219 } |
220 | |
221 static void | |
15822 | 222 update_buddy_typing(PurpleAccount *account, const char *who, gpointer null) |
15817 | 223 { |
15822 | 224 PurpleConversation *conv; |
15817 | 225 FinchConv *ggc; |
15822 | 226 PurpleConvIm *im = NULL; |
15817 | 227 char *title, *str; |
228 | |
15822 | 229 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, account); |
15817 | 230 |
231 if (!conv) | |
232 return; | |
233 | |
15822 | 234 im = PURPLE_CONV_IM(conv); |
15817 | 235 ggc = conv->ui_data; |
236 | |
15822 | 237 if (purple_conv_im_get_typing_state(im) == PURPLE_TYPING) { |
15817 | 238 int scroll; |
239 str = get_conversation_title(conv, account); | |
240 title = g_strdup_printf(_("%s [%s]"), str, | |
241 gnt_ascii_only() ? "T" : "\342\243\277"); | |
242 g_free(str); | |
243 | |
244 scroll = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggc->tv)); | |
15822 | 245 str = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_name(conv)); |
15817 | 246 /* Updating is a little buggy. So just remove and add a new one */ |
247 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE); | |
248 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggc->tv), | |
249 str, GNT_TEXT_FLAG_DIM, "typing"); | |
250 g_free(str); | |
251 if (scroll <= 1) | |
252 gnt_text_view_scroll(GNT_TEXT_VIEW(ggc->tv), 0); | |
253 } else { | |
254 title = get_conversation_title(conv, account); | |
255 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE); | |
256 } | |
257 gnt_screen_rename_widget(ggc->window, title); | |
258 g_free(title); | |
259 } | |
260 | |
261 static gpointer | |
262 finch_conv_get_handle() | |
263 { | |
264 static int handle; | |
265 return &handle; | |
266 } | |
267 | |
268 static void | |
15822 | 269 finch_create_conversation(PurpleConversation *conv) |
15817 | 270 { |
271 FinchConv *ggc = conv->ui_data; | |
272 char *title; | |
15822 | 273 PurpleConversationType type; |
274 PurpleConversation *cc; | |
275 PurpleAccount *account; | |
15817 | 276 |
277 if (ggc) | |
278 return; | |
279 | |
280 cc = find_conv_with_contact(conv); | |
281 if (cc && cc->ui_data) | |
282 ggc = cc->ui_data; | |
283 else | |
284 ggc = g_new0(FinchConv, 1); | |
285 | |
286 ggc->list = g_list_prepend(ggc->list, conv); | |
287 ggc->active_conv = conv; | |
288 conv->ui_data = ggc; | |
289 | |
290 if (cc && cc->ui_data) { | |
291 finch_conversation_set_active(conv); | |
292 return; | |
293 } | |
294 | |
15822 | 295 account = purple_conversation_get_account(conv); |
296 type = purple_conversation_get_type(conv); | |
15817 | 297 title = get_conversation_title(conv, account); |
298 | |
299 ggc->window = gnt_box_new(FALSE, TRUE); | |
300 gnt_box_set_title(GNT_BOX(ggc->window), title); | |
301 gnt_box_set_toplevel(GNT_BOX(ggc->window), TRUE); | |
302 gnt_box_set_pad(GNT_BOX(ggc->window), 0); | |
303 gnt_widget_set_name(ggc->window, "conversation-window"); | |
304 | |
305 ggc->tv = gnt_text_view_new(); | |
306 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->tv); | |
307 gnt_widget_set_name(ggc->tv, "conversation-window-textview"); | |
15822 | 308 gnt_widget_set_size(ggc->tv, purple_prefs_get_int(PREF_ROOT "/size/width"), |
309 purple_prefs_get_int(PREF_ROOT "/size/height")); | |
15817 | 310 |
311 ggc->entry = gnt_entry_new(NULL); | |
312 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->entry); | |
313 gnt_widget_set_name(ggc->entry, "conversation-window-entry"); | |
314 gnt_entry_set_history_length(GNT_ENTRY(ggc->entry), -1); | |
315 gnt_entry_set_word_suggest(GNT_ENTRY(ggc->entry), TRUE); | |
316 gnt_entry_set_always_suggest(GNT_ENTRY(ggc->entry), FALSE); | |
317 | |
16124
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15870
diff
changeset
|
318 gnt_text_view_attach_scroll_widget(GNT_TEXT_VIEW(ggc->tv), ggc->entry); |
15817 | 319 g_signal_connect_after(G_OBJECT(ggc->entry), "key_pressed", G_CALLBACK(entry_key_pressed), ggc); |
320 g_signal_connect(G_OBJECT(ggc->window), "destroy", G_CALLBACK(closing_window), ggc); | |
321 | |
15822 | 322 gnt_widget_set_position(ggc->window, purple_prefs_get_int(PREF_ROOT "/position/x"), |
323 purple_prefs_get_int(PREF_ROOT "/position/y")); | |
15817 | 324 gnt_widget_show(ggc->window); |
325 | |
326 g_signal_connect(G_OBJECT(ggc->tv), "size_changed", G_CALLBACK(size_changed_cb), NULL); | |
327 g_signal_connect(G_OBJECT(ggc->window), "position_set", G_CALLBACK(save_position_cb), NULL); | |
328 | |
15822 | 329 if (type == PURPLE_CONV_TYPE_IM) { |
15817 | 330 g_signal_connect(G_OBJECT(ggc->entry), "text_changed", G_CALLBACK(send_typing_notification), ggc); |
15822 | 331 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", finch_conv_get_handle(), |
332 PURPLE_CALLBACK(update_buddy_typing), NULL); | |
333 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", finch_conv_get_handle(), | |
334 PURPLE_CALLBACK(update_buddy_typing), NULL); | |
15817 | 335 } |
336 | |
337 g_free(title); | |
338 } | |
339 | |
340 static void | |
15822 | 341 finch_destroy_conversation(PurpleConversation *conv) |
15817 | 342 { |
343 /* do stuff here */ | |
344 FinchConv *ggc = conv->ui_data; | |
345 ggc->list = g_list_remove(ggc->list, conv); | |
346 if (ggc->list && conv == ggc->active_conv) | |
347 ggc->active_conv = ggc->list->data; | |
348 | |
349 if (ggc->list == NULL) { | |
350 gnt_widget_destroy(ggc->window); | |
351 g_free(ggc); | |
352 } | |
353 } | |
354 | |
355 static void | |
15822 | 356 finch_write_common(PurpleConversation *conv, const char *who, const char *message, |
357 PurpleMessageFlags flags, time_t mtime) | |
15817 | 358 { |
359 FinchConv *ggconv = conv->ui_data; | |
360 char *strip, *newline; | |
361 GntTextFormatFlags fl = 0; | |
362 int pos; | |
363 gboolean notify; | |
364 | |
365 g_return_if_fail(ggconv != NULL); | |
366 | |
367 if (ggconv->active_conv != conv) { | |
15822 | 368 if (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV)) |
15817 | 369 finch_conversation_set_active(conv); |
370 else | |
371 return; | |
372 } | |
373 | |
374 pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggconv->tv)); | |
375 | |
376 notify = !!gnt_text_view_tag_change(GNT_TEXT_VIEW(ggconv->tv), "typing", NULL, TRUE); | |
377 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), "\n", GNT_TEXT_FLAG_NORMAL); | |
378 | |
379 /* Unnecessary to print the timestamp for delayed message */ | |
15822 | 380 if (!(flags & PURPLE_MESSAGE_DELAYED) && |
381 purple_prefs_get_bool("/purple/gnt/conversations/timestamps")) | |
15817 | 382 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
15822 | 383 purple_utf8_strftime("(%H:%M:%S) ", localtime(&mtime)), GNT_TEXT_FLAG_DIM); |
15817 | 384 |
15822 | 385 if (flags & PURPLE_MESSAGE_AUTO_RESP) |
15817 | 386 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
387 _("<AUTO-REPLY> "), GNT_TEXT_FLAG_BOLD); | |
388 | |
15822 | 389 if (who && *who && (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV))) |
15817 | 390 { |
391 char * name = NULL; | |
392 | |
15822 | 393 if (purple_message_meify((char*)message, -1)) |
15817 | 394 name = g_strdup_printf("*** %s ", who); |
395 else | |
396 name = g_strdup_printf("%s: ", who); | |
397 | |
398 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), | |
399 name, GNT_TEXT_FLAG_BOLD); | |
400 g_free(name); | |
401 } | |
402 else | |
403 fl = GNT_TEXT_FLAG_DIM; | |
404 | |
15822 | 405 if (flags & PURPLE_MESSAGE_ERROR) |
15817 | 406 fl |= GNT_TEXT_FLAG_BOLD; |
15822 | 407 if (flags & PURPLE_MESSAGE_NICK) |
15817 | 408 fl |= GNT_TEXT_FLAG_UNDERLINE; |
409 | |
410 /* XXX: Remove this workaround when textview can parse messages. */ | |
15822 | 411 newline = purple_strdup_withhtml(message); |
412 strip = purple_markup_strip_html(newline); | |
15817 | 413 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
414 strip, fl); | |
415 | |
416 g_free(newline); | |
417 g_free(strip); | |
418 | |
419 if (notify) { | |
15822 | 420 strip = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_name(conv)); |
15817 | 421 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggconv->tv), |
422 strip, GNT_TEXT_FLAG_DIM, "typing"); | |
423 g_free(strip); | |
424 } | |
425 | |
426 if (pos <= 1) | |
427 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 0); | |
428 | |
15822 | 429 if (flags & (PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_NICK | PURPLE_MESSAGE_ERROR)) |
15817 | 430 gnt_widget_set_urgent(ggconv->tv); |
431 } | |
432 | |
433 static void | |
15822 | 434 finch_write_chat(PurpleConversation *conv, const char *who, const char *message, |
435 PurpleMessageFlags flags, time_t mtime) | |
15817 | 436 { |
15822 | 437 purple_conversation_write(conv, who, message, flags, mtime); |
15817 | 438 } |
439 | |
440 static void | |
15822 | 441 finch_write_im(PurpleConversation *conv, const char *who, const char *message, |
442 PurpleMessageFlags flags, time_t mtime) | |
15817 | 443 { |
15822 | 444 PurpleAccount *account = purple_conversation_get_account(conv); |
445 if (flags & PURPLE_MESSAGE_SEND) | |
15817 | 446 { |
15822 | 447 who = purple_connection_get_display_name(purple_account_get_connection(account)); |
15817 | 448 if (!who) |
15822 | 449 who = purple_account_get_alias(account); |
15817 | 450 if (!who) |
15822 | 451 who = purple_account_get_username(account); |
15817 | 452 } |
15822 | 453 else if (flags & PURPLE_MESSAGE_RECV) |
15817 | 454 { |
15822 | 455 PurpleBuddy *buddy; |
456 who = purple_conversation_get_name(conv); | |
457 buddy = purple_find_buddy(account, who); | |
15817 | 458 if (buddy) |
15822 | 459 who = purple_buddy_get_contact_alias(buddy); |
15817 | 460 } |
461 | |
15822 | 462 purple_conversation_write(conv, who, message, flags, mtime); |
15817 | 463 } |
464 | |
465 static void | |
15822 | 466 finch_write_conv(PurpleConversation *conv, const char *who, const char *alias, |
467 const char *message, PurpleMessageFlags flags, time_t mtime) | |
15817 | 468 { |
469 const char *name; | |
470 if (alias && *alias) | |
471 name = alias; | |
472 else if (who && *who) | |
473 name = who; | |
474 else | |
475 name = NULL; | |
476 | |
477 finch_write_common(conv, name, message, flags, mtime); | |
478 } | |
479 | |
480 static void | |
15822 | 481 finch_chat_add_users(PurpleConversation *conv, GList *users, gboolean new_arrivals) |
15817 | 482 { |
483 FinchConv *ggc = conv->ui_data; | |
484 GntEntry *entry = GNT_ENTRY(ggc->entry); | |
485 | |
486 if (!new_arrivals) | |
487 { | |
488 /* Print the list of users in the room */ | |
489 GString *string = g_string_new(_("List of users:\n")); | |
490 GList *iter; | |
491 | |
492 for (iter = users; iter; iter = iter->next) | |
493 { | |
15822 | 494 PurpleConvChatBuddy *cbuddy = iter->data; |
15817 | 495 char *str; |
496 | |
497 if ((str = cbuddy->alias) == NULL) | |
498 str = cbuddy->name; | |
499 g_string_append_printf(string, "[ %s ]", str); | |
500 } | |
501 | |
15822 | 502 purple_conversation_write(conv, NULL, string->str, |
503 PURPLE_MESSAGE_SYSTEM, time(NULL)); | |
15817 | 504 g_string_free(string, TRUE); |
505 } | |
506 | |
507 for (; users; users = users->next) | |
508 { | |
15822 | 509 PurpleConvChatBuddy *cbuddy = users->data; |
15817 | 510 gnt_entry_add_suggest(entry, cbuddy->name); |
511 gnt_entry_add_suggest(entry, cbuddy->alias); | |
512 } | |
513 } | |
514 | |
515 static void | |
15822 | 516 finch_chat_rename_user(PurpleConversation *conv, const char *old, const char *new_n, const char *new_a) |
15817 | 517 { |
518 /* Update the name for string completion */ | |
519 FinchConv *ggc = conv->ui_data; | |
520 GntEntry *entry = GNT_ENTRY(ggc->entry); | |
521 gnt_entry_remove_suggest(entry, old); | |
522 gnt_entry_add_suggest(entry, new_n); | |
523 gnt_entry_add_suggest(entry, new_a); | |
524 } | |
525 | |
526 static void | |
15822 | 527 finch_chat_remove_user(PurpleConversation *conv, GList *list) |
15817 | 528 { |
529 /* Remove the name from string completion */ | |
530 FinchConv *ggc = conv->ui_data; | |
531 GntEntry *entry = GNT_ENTRY(ggc->entry); | |
532 for (; list; list = list->next) | |
533 gnt_entry_remove_suggest(entry, list->data); | |
534 } | |
535 | |
536 static void | |
15822 | 537 finch_chat_update_user(PurpleConversation *conv, const char *user) |
15817 | 538 { |
539 } | |
540 | |
15822 | 541 static PurpleConversationUiOps conv_ui_ops = |
15817 | 542 { |
543 .create_conversation = finch_create_conversation, | |
544 .destroy_conversation = finch_destroy_conversation, | |
545 .write_chat = finch_write_chat, | |
546 .write_im = finch_write_im, | |
547 .write_conv = finch_write_conv, | |
548 .chat_add_users = finch_chat_add_users, | |
549 .chat_rename_user = finch_chat_rename_user, | |
550 .chat_remove_users = finch_chat_remove_user, | |
551 .chat_update_user = finch_chat_update_user, | |
552 .present = NULL, | |
553 .has_focus = NULL, | |
554 .custom_smiley_add = NULL, | |
555 .custom_smiley_write = NULL, | |
556 .custom_smiley_close = NULL | |
557 }; | |
558 | |
15822 | 559 PurpleConversationUiOps *finch_conv_get_ui_ops() |
15817 | 560 { |
561 return &conv_ui_ops; | |
562 } | |
563 | |
564 /* Xerox */ | |
15822 | 565 static PurpleCmdRet |
566 say_command_cb(PurpleConversation *conv, | |
15817 | 567 const char *cmd, char **args, char **error, void *data) |
568 { | |
15822 | 569 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) |
570 purple_conv_im_send(PURPLE_CONV_IM(conv), args[0]); | |
571 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) | |
572 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), args[0]); | |
15817 | 573 |
15822 | 574 return PURPLE_CMD_RET_OK; |
15817 | 575 } |
576 | |
577 /* Xerox */ | |
15822 | 578 static PurpleCmdRet |
579 me_command_cb(PurpleConversation *conv, | |
15817 | 580 const char *cmd, char **args, char **error, void *data) |
581 { | |
582 char *tmp; | |
583 | |
584 tmp = g_strdup_printf("/me %s", args[0]); | |
585 | |
15822 | 586 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) |
587 purple_conv_im_send(PURPLE_CONV_IM(conv), tmp); | |
588 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) | |
589 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), tmp); | |
15817 | 590 |
591 g_free(tmp); | |
15822 | 592 return PURPLE_CMD_RET_OK; |
15817 | 593 } |
594 | |
595 /* Xerox */ | |
15822 | 596 static PurpleCmdRet |
597 debug_command_cb(PurpleConversation *conv, | |
15817 | 598 const char *cmd, char **args, char **error, void *data) |
599 { | |
600 char *tmp, *markup; | |
15822 | 601 PurpleCmdStatus status; |
15817 | 602 |
603 if (!g_ascii_strcasecmp(args[0], "version")) { | |
604 tmp = g_strdup_printf("me is using %s.", VERSION); | |
605 markup = g_markup_escape_text(tmp, -1); | |
606 | |
15822 | 607 status = purple_cmd_do_command(conv, tmp, markup, error); |
15817 | 608 |
609 g_free(tmp); | |
610 g_free(markup); | |
611 return status; | |
612 } else { | |
15822 | 613 purple_conversation_write(conv, NULL, _("Supported debug options are: version"), |
614 PURPLE_MESSAGE_NO_LOG|PURPLE_MESSAGE_ERROR, time(NULL)); | |
615 return PURPLE_CMD_STATUS_OK; | |
15817 | 616 } |
617 } | |
618 | |
619 /* Xerox */ | |
15822 | 620 static PurpleCmdRet |
621 clear_command_cb(PurpleConversation *conv, | |
15817 | 622 const char *cmd, char **args, char **error, void *data) |
623 { | |
624 FinchConv *ggconv = conv->ui_data; | |
625 gnt_text_view_clear(GNT_TEXT_VIEW(ggconv->tv)); | |
15822 | 626 return PURPLE_CMD_STATUS_OK; |
15817 | 627 } |
628 | |
629 /* Xerox */ | |
15822 | 630 static PurpleCmdRet |
631 help_command_cb(PurpleConversation *conv, | |
15817 | 632 const char *cmd, char **args, char **error, void *data) |
633 { | |
634 GList *l, *text; | |
635 GString *s; | |
636 | |
637 if (args[0] != NULL) { | |
638 s = g_string_new(""); | |
15822 | 639 text = purple_cmd_help(conv, args[0]); |
15817 | 640 |
641 if (text) { | |
642 for (l = text; l; l = l->next) | |
643 if (l->next) | |
644 g_string_append_printf(s, "%s\n", (char *)l->data); | |
645 else | |
646 g_string_append_printf(s, "%s", (char *)l->data); | |
647 } else { | |
648 g_string_append(s, _("No such command (in this context).")); | |
649 } | |
650 } else { | |
651 s = g_string_new(_("Use \"/help <command>\" for help on a specific command.\n" | |
652 "The following commands are available in this context:\n")); | |
653 | |
15822 | 654 text = purple_cmd_list(conv); |
15817 | 655 for (l = text; l; l = l->next) |
656 if (l->next) | |
657 g_string_append_printf(s, "%s, ", (char *)l->data); | |
658 else | |
659 g_string_append_printf(s, "%s.", (char *)l->data); | |
660 g_list_free(text); | |
661 } | |
662 | |
15822 | 663 purple_conversation_write(conv, NULL, s->str, PURPLE_MESSAGE_NO_LOG, time(NULL)); |
15817 | 664 g_string_free(s, TRUE); |
665 | |
15822 | 666 return PURPLE_CMD_STATUS_OK; |
15817 | 667 } |
668 | |
15822 | 669 static PurpleCmdRet |
670 cmd_show_window(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data) | |
15817 | 671 { |
672 void (*callback)() = data; | |
673 callback(); | |
15822 | 674 return PURPLE_CMD_STATUS_OK; |
15817 | 675 } |
676 | |
677 void finch_conversation_init() | |
678 { | |
15822 | 679 purple_prefs_add_none(PREF_ROOT); |
680 purple_prefs_add_none(PREF_ROOT "/size"); | |
681 purple_prefs_add_int(PREF_ROOT "/size/width", 70); | |
682 purple_prefs_add_int(PREF_ROOT "/size/height", 20); | |
683 purple_prefs_add_none(PREF_ROOT "/position"); | |
684 purple_prefs_add_int(PREF_ROOT "/position/x", 0); | |
685 purple_prefs_add_int(PREF_ROOT "/position/y", 0); | |
15817 | 686 |
687 /* Xerox the commands */ | |
15822 | 688 purple_cmd_register("say", "S", PURPLE_CMD_P_DEFAULT, |
689 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 690 say_command_cb, _("say <message>: Send a message normally as if you weren't using a command."), NULL); |
15822 | 691 purple_cmd_register("me", "S", PURPLE_CMD_P_DEFAULT, |
692 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 693 me_command_cb, _("me <action>: Send an IRC style action to a buddy or chat."), NULL); |
15822 | 694 purple_cmd_register("debug", "w", PURPLE_CMD_P_DEFAULT, |
695 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 696 debug_command_cb, _("debug <option>: Send various debug information to the current conversation."), NULL); |
15822 | 697 purple_cmd_register("clear", "", PURPLE_CMD_P_DEFAULT, |
698 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 699 clear_command_cb, _("clear: Clears the conversation scrollback."), NULL); |
15822 | 700 purple_cmd_register("help", "w", PURPLE_CMD_P_DEFAULT, |
701 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL, | |
15817 | 702 help_command_cb, _("help <command>: Help on a specific command."), NULL); |
703 | |
704 /* Now some commands to bring up some other windows */ | |
15822 | 705 purple_cmd_register("plugins", "", PURPLE_CMD_P_DEFAULT, |
706 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 707 cmd_show_window, _("plugins: Show the plugins window."), finch_plugins_show_all); |
15822 | 708 purple_cmd_register("buddylist", "", PURPLE_CMD_P_DEFAULT, |
709 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 710 cmd_show_window, _("buddylist: Show the buddylist."), finch_blist_show); |
15822 | 711 purple_cmd_register("accounts", "", PURPLE_CMD_P_DEFAULT, |
712 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 713 cmd_show_window, _("accounts: Show the accounts window."), finch_accounts_show_all); |
15822 | 714 purple_cmd_register("debugwin", "", PURPLE_CMD_P_DEFAULT, |
715 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 716 cmd_show_window, _("debugwin: Show the debug window."), finch_debug_window_show); |
15822 | 717 purple_cmd_register("prefs", "", PURPLE_CMD_P_DEFAULT, |
718 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 719 cmd_show_window, _("prefs: Show the preference window."), finch_prefs_show_all); |
15822 | 720 purple_cmd_register("status", "", PURPLE_CMD_P_DEFAULT, |
721 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, | |
15817 | 722 cmd_show_window, _("statuses: Show the savedstatuses window."), finch_savedstatus_show_all); |
723 } | |
724 | |
725 void finch_conversation_uninit() | |
726 { | |
727 } | |
728 | |
15822 | 729 void finch_conversation_set_active(PurpleConversation *conv) |
15817 | 730 { |
731 FinchConv *ggconv = conv->ui_data; | |
15822 | 732 PurpleAccount *account; |
15817 | 733 char *title; |
734 | |
735 g_return_if_fail(ggconv); | |
736 g_return_if_fail(g_list_find(ggconv->list, conv)); | |
737 | |
738 ggconv->active_conv = conv; | |
15822 | 739 account = purple_conversation_get_account(conv); |
15817 | 740 title = get_conversation_title(conv, account); |
741 gnt_screen_rename_widget(ggconv->window, title); | |
742 g_free(title); | |
743 } | |
744 |