merge of 'c965c78fc4196475e8fd004312be08c074958764'
[pidgin-git.git] / finch / gntconv.c
blob254bcc3fe26e1b23e89157520bb682105c407494
1 /**
2 * @file gntconv.c GNT Conversation API
3 * @ingroup finch
4 */
6 /* finch
8 * Finch is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include <string.h>
28 #include "finch.h"
29 #include <internal.h>
31 #include <cmds.h>
32 #include <core.h>
33 #include <idle.h>
34 #include <prefs.h>
35 #include <util.h>
37 #include "gntaccount.h"
38 #include "gntblist.h"
39 #include "gntconv.h"
40 #include "gntdebug.h"
41 #include "gntlog.h"
42 #include "gntplugin.h"
43 #include "gntpounce.h"
44 #include "gntprefs.h"
45 #include "gntrequest.h"
46 #include "gntsound.h"
47 #include "gntstatus.h"
49 #include "gnt.h"
50 #include "gntbox.h"
51 #include "gntentry.h"
52 #include "gntlabel.h"
53 #include "gntmenu.h"
54 #include "gntmenuitem.h"
55 #include "gntmenuitemcheck.h"
56 #include "gntstyle.h"
57 #include "gnttextview.h"
58 #include "gnttree.h"
59 #include "gntutils.h"
60 #include "gntwindow.h"
62 #define PREF_ROOT "/finch/conversations"
63 #define PREF_CHAT PREF_ROOT "/chats"
64 #define PREF_USERLIST PREF_CHAT "/userlist"
66 #include "config.h"
68 static void finch_write_common(PurpleConversation *conv, const char *who,
69 const char *message, PurpleMessageFlags flags, time_t mtime);
70 static void generate_send_to_menu(FinchConv *ggc);
72 static int color_message_receive;
73 static int color_message_send;
74 static int color_message_highlight;
75 static int color_message_action;
76 static int color_timestamp;
78 static PurpleBuddy *
79 find_buddy_for_conversation(PurpleConversation *conv)
81 return purple_find_buddy(purple_conversation_get_account(conv),
82 purple_conversation_get_name(conv));
85 static PurpleChat *
86 find_chat_for_conversation(PurpleConversation *conv)
88 return purple_blist_find_chat(purple_conversation_get_account(conv),
89 purple_conversation_get_name(conv));
92 static PurpleBlistNode *
93 get_conversation_blist_node(PurpleConversation *conv)
95 PurpleBlistNode *node = NULL;
97 switch (purple_conversation_get_type(conv)) {
98 case PURPLE_CONV_TYPE_IM:
99 node = (PurpleBlistNode*)find_buddy_for_conversation(conv);
100 node = node ? purple_blist_node_get_parent(node) : NULL;
101 break;
102 case PURPLE_CONV_TYPE_CHAT:
103 node = (PurpleBlistNode*)find_chat_for_conversation(conv);
104 break;
105 default:
106 break;
108 return node;
111 static void
112 send_typing_notification(GntWidget *w, FinchConv *ggconv)
114 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry));
115 gboolean empty = (!text || !*text || (*text == '/'));
116 if (purple_prefs_get_bool("/finch/conversations/notify_typing")) {
117 PurpleConversation *conv = ggconv->active_conv;
118 PurpleConvIm *im = PURPLE_CONV_IM(conv);
119 if (!empty) {
120 gboolean send = (purple_conv_im_get_send_typed_timeout(im) == 0);
122 purple_conv_im_stop_send_typed_timeout(im);
123 purple_conv_im_start_send_typed_timeout(im);
124 if (send || (purple_conv_im_get_type_again(im) != 0 &&
125 time(NULL) > purple_conv_im_get_type_again(im))) {
126 unsigned int timeout;
127 timeout = serv_send_typing(purple_conversation_get_gc(conv),
128 purple_conversation_get_name(conv),
129 PURPLE_TYPING);
130 purple_conv_im_set_type_again(im, timeout);
132 } else {
133 purple_conv_im_stop_send_typed_timeout(im);
135 serv_send_typing(purple_conversation_get_gc(conv),
136 purple_conversation_get_name(conv),
137 PURPLE_NOT_TYPING);
142 static void
143 entry_key_pressed(GntWidget *w, FinchConv *ggconv)
145 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry));
146 if (*text == '/' && *(text + 1) != '/')
148 PurpleConversation *conv = ggconv->active_conv;
149 PurpleCmdStatus status;
150 const char *cmdline = text + 1;
151 char *error = NULL, *escape;
153 escape = g_markup_escape_text(cmdline, -1);
154 status = purple_cmd_do_command(conv, cmdline, escape, &error);
155 g_free(escape);
157 switch (status)
159 case PURPLE_CMD_STATUS_OK:
160 break;
161 case PURPLE_CMD_STATUS_NOT_FOUND:
162 purple_conversation_write(conv, "", _("No such command."),
163 PURPLE_MESSAGE_NO_LOG, time(NULL));
164 break;
165 case PURPLE_CMD_STATUS_WRONG_ARGS:
166 purple_conversation_write(conv, "", _("Syntax Error: You typed the wrong number of arguments "
167 "to that command."),
168 PURPLE_MESSAGE_NO_LOG, time(NULL));
169 break;
170 case PURPLE_CMD_STATUS_FAILED:
171 purple_conversation_write(conv, "", error ? error : _("Your command failed for an unknown reason."),
172 PURPLE_MESSAGE_NO_LOG, time(NULL));
173 break;
174 case PURPLE_CMD_STATUS_WRONG_TYPE:
175 if(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
176 purple_conversation_write(conv, "", _("That command only works in chats, not IMs."),
177 PURPLE_MESSAGE_NO_LOG, time(NULL));
178 else
179 purple_conversation_write(conv, "", _("That command only works in IMs, not chats."),
180 PURPLE_MESSAGE_NO_LOG, time(NULL));
181 break;
182 case PURPLE_CMD_STATUS_WRONG_PRPL:
183 purple_conversation_write(conv, "", _("That command doesn't work on this protocol."),
184 PURPLE_MESSAGE_NO_LOG, time(NULL));
185 break;
187 g_free(error);
189 else if (!purple_account_is_connected(purple_conversation_get_account(ggconv->active_conv)))
191 purple_conversation_write(ggconv->active_conv, "", _("Message was not sent, because you are not signed on."),
192 PURPLE_MESSAGE_ERROR | PURPLE_MESSAGE_NO_LOG, time(NULL));
194 else
196 char *escape = purple_markup_escape_text((*text == '/' ? text + 1 : text), -1);
197 switch (purple_conversation_get_type(ggconv->active_conv))
199 case PURPLE_CONV_TYPE_IM:
200 purple_conv_im_send_with_flags(PURPLE_CONV_IM(ggconv->active_conv), escape, PURPLE_MESSAGE_SEND);
201 break;
202 case PURPLE_CONV_TYPE_CHAT:
203 purple_conv_chat_send(PURPLE_CONV_CHAT(ggconv->active_conv), escape);
204 break;
205 default:
206 g_free(escape);
207 g_return_if_reached();
209 g_free(escape);
210 purple_idle_touch();
212 gnt_entry_add_to_history(GNT_ENTRY(ggconv->entry), text);
213 gnt_entry_clear(GNT_ENTRY(ggconv->entry));
216 static void
217 closing_window(GntWidget *window, FinchConv *ggconv)
219 GList *list = ggconv->list;
220 ggconv->window = NULL;
221 while (list) {
222 PurpleConversation *conv = list->data;
223 list = list->next;
224 purple_conversation_destroy(conv);
228 static void
229 size_changed_cb(GntWidget *widget, int width, int height)
231 int w, h;
232 gnt_widget_get_size(widget, &w, &h);
233 purple_prefs_set_int(PREF_ROOT "/size/width", w);
234 purple_prefs_set_int(PREF_ROOT "/size/height", h);
237 static void
238 save_position_cb(GntWidget *w, int x, int y)
240 purple_prefs_set_int(PREF_ROOT "/position/x", x);
241 purple_prefs_set_int(PREF_ROOT "/position/y", y);
244 static PurpleConversation *
245 find_conv_with_contact(PurpleAccount *account, const char *name)
247 PurpleBlistNode *node;
248 PurpleBuddy *buddy = purple_find_buddy(account, name);
249 PurpleConversation *ret = NULL;
251 if (!buddy)
252 return NULL;
254 for (node = purple_blist_node_get_first_child(purple_blist_node_get_parent((PurpleBlistNode*)buddy));
255 node; node = purple_blist_node_get_sibling_next(node)) {
256 if (node == (PurpleBlistNode*)buddy)
257 continue;
258 if ((ret = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM,
259 purple_buddy_get_name((PurpleBuddy*)node), purple_buddy_get_account((PurpleBuddy*)node))) != NULL)
260 break;
262 return ret;
265 static char *
266 get_conversation_title(PurpleConversation *conv, PurpleAccount *account)
268 return g_strdup_printf(_("%s (%s -- %s)"), purple_conversation_get_title(conv),
269 purple_account_get_username(account), purple_account_get_protocol_name(account));
272 static void
273 update_buddy_typing(PurpleAccount *account, const char *who, gpointer null)
275 PurpleConversation *conv;
276 FinchConv *ggc;
277 PurpleConvIm *im = NULL;
278 char *title, *str;
280 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, account);
282 if (!conv)
283 return;
285 im = PURPLE_CONV_IM(conv);
286 ggc = FINCH_GET_DATA(conv);
288 if (purple_conv_im_get_typing_state(im) == PURPLE_TYPING) {
289 int scroll;
290 str = get_conversation_title(conv, account);
291 title = g_strdup_printf(_("%s [%s]"), str,
292 gnt_ascii_only() ? "T" : "\342\243\277");
293 g_free(str);
295 scroll = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggc->tv));
296 str = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_title(conv));
297 /* Updating is a little buggy. So just remove and add a new one */
298 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE);
299 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggc->tv),
300 str, GNT_TEXT_FLAG_DIM, "typing");
301 g_free(str);
302 if (scroll <= 1)
303 gnt_text_view_scroll(GNT_TEXT_VIEW(ggc->tv), 0);
304 } else {
305 title = get_conversation_title(conv, account);
306 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", " ", TRUE);
308 gnt_screen_rename_widget(ggc->window, title);
309 g_free(title);
312 static void
313 chat_left_cb(PurpleConversation *conv, gpointer null)
315 finch_write_common(conv, NULL, _("You have left this chat."),
316 PURPLE_MESSAGE_SYSTEM, time(NULL));
319 static void
320 buddy_signed_on_off(PurpleBuddy *buddy, gpointer null)
322 PurpleConversation *conv = find_conv_with_contact(purple_buddy_get_account(buddy), purple_buddy_get_name(buddy));
323 if (conv == NULL)
324 return;
325 generate_send_to_menu(FINCH_GET_DATA(conv));
328 static void
329 account_signed_on_off(PurpleConnection *gc, gpointer null)
331 GList *list = purple_get_ims();
332 while (list) {
333 PurpleConversation *conv = list->data;
334 PurpleConversation *cc = find_conv_with_contact(
335 purple_conversation_get_account(conv), purple_conversation_get_name(conv));
336 if (cc)
337 generate_send_to_menu(FINCH_GET_DATA(cc));
338 list = list->next;
341 if (PURPLE_CONNECTION_IS_CONNECTED(gc)) {
342 /* We just signed on. Let's see if there's any chat that we have open,
343 * and hadn't left before the disconnect. */
344 list = purple_get_chats();
345 while (list) {
346 PurpleConversation *conv = list->data;
347 PurpleChat *chat;
348 GHashTable *comps = NULL;
350 list = list->next;
351 if (purple_conversation_get_account(conv) != purple_connection_get_account(gc) ||
352 !purple_conversation_get_data(conv, "want-to-rejoin"))
353 continue;
355 chat = find_chat_for_conversation(conv);
356 if (chat == NULL) {
357 PurplePluginProtocolInfo *info = PURPLE_PLUGIN_PROTOCOL_INFO(purple_connection_get_prpl(gc));
358 if (info->chat_info_defaults != NULL)
359 comps = info->chat_info_defaults(gc, purple_conversation_get_name(conv));
360 } else {
361 comps = purple_chat_get_components(chat);
363 serv_join_chat(gc, comps);
364 if (chat == NULL && comps != NULL)
365 g_hash_table_destroy(comps);
370 static void
371 account_signing_off(PurpleConnection *gc)
373 GList *list = purple_get_chats();
374 PurpleAccount *account = purple_connection_get_account(gc);
376 /* We are about to sign off. See which chats we are currently in, and mark
377 * them for rejoin on reconnect. */
378 while (list) {
379 PurpleConversation *conv = list->data;
380 if (!purple_conv_chat_has_left(PURPLE_CONV_CHAT(conv)) &&
381 purple_conversation_get_account(conv) == account) {
382 purple_conversation_set_data(conv, "want-to-rejoin", GINT_TO_POINTER(TRUE));
383 purple_conversation_write(conv, NULL, _("The account has disconnected and you are no "
384 "longer in this chat. You will be automatically rejoined in the chat when "
385 "the account reconnects."),
386 PURPLE_MESSAGE_SYSTEM, time(NULL));
388 list = list->next;
392 static gpointer
393 finch_conv_get_handle(void)
395 static int handle;
396 return &handle;
399 static void
400 clear_scrollback_cb(GntMenuItem *item, gpointer ggconv)
402 FinchConv *ggc = ggconv;
403 gnt_text_view_clear(GNT_TEXT_VIEW(ggc->tv));
406 static void
407 send_file_cb(GntMenuItem *item, gpointer ggconv)
409 FinchConv *ggc = ggconv;
410 serv_send_file(purple_conversation_get_gc(ggc->active_conv),
411 purple_conversation_get_name(ggc->active_conv), NULL);
414 static void
415 add_pounce_cb(GntMenuItem *item, gpointer ggconv)
417 FinchConv *ggc = ggconv;
418 finch_pounce_editor_show(
419 purple_conversation_get_account(ggc->active_conv),
420 purple_conversation_get_name(ggc->active_conv), NULL);
423 static void
424 get_info_cb(GntMenuItem *item, gpointer ggconv)
426 FinchConv *ggc = ggconv;
427 finch_retrieve_user_info(purple_conversation_get_gc(ggc->active_conv),
428 purple_conversation_get_name(ggc->active_conv));
431 static void
432 toggle_timestamps_cb(GntMenuItem *item, gpointer ggconv)
434 purple_prefs_set_bool(PREF_ROOT "/timestamps",
435 !purple_prefs_get_bool(PREF_ROOT "/timestamps"));
438 static void
439 toggle_logging_cb(GntMenuItem *item, gpointer ggconv)
441 FinchConv *fc = ggconv;
442 PurpleConversation *conv = fc->active_conv;
443 gboolean logging = gnt_menuitem_check_get_checked(GNT_MENU_ITEM_CHECK(item));
444 GList *iter;
446 if (logging == purple_conversation_is_logging(conv))
447 return;
449 /* Xerox */
450 if (logging) {
451 /* Enable logging first so the message below can be logged. */
452 purple_conversation_set_logging(conv, TRUE);
454 purple_conversation_write(conv, NULL,
455 _("Logging started. Future messages in this conversation will be logged."),
456 PURPLE_MESSAGE_SYSTEM, time(NULL));
457 } else {
458 purple_conversation_write(conv, NULL,
459 _("Logging stopped. Future messages in this conversation will not be logged."),
460 PURPLE_MESSAGE_SYSTEM, time(NULL));
462 /* Disable the logging second, so that the above message can be logged. */
463 purple_conversation_set_logging(conv, FALSE);
466 /* Each conversation with the same person will have the same logging setting */
467 for (iter = fc->list; iter; iter = iter->next) {
468 if (iter->data == conv)
469 continue;
470 purple_conversation_set_logging(iter->data, logging);
474 static void
475 toggle_sound_cb(GntMenuItem *item, gpointer ggconv)
477 FinchConv *fc = ggconv;
478 PurpleBlistNode *node = get_conversation_blist_node(fc->active_conv);
479 fc->flags ^= FINCH_CONV_NO_SOUND;
480 if (node)
481 purple_blist_node_set_bool(node, "gnt-mute-sound", !!(fc->flags & FINCH_CONV_NO_SOUND));
484 static void
485 send_to_cb(GntMenuItem *m, gpointer n)
487 PurpleAccount *account = g_object_get_data(G_OBJECT(m), "purple_account");
488 gchar *buddy = g_object_get_data(G_OBJECT(m), "purple_buddy_name");
489 PurpleConversation *conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, buddy);
490 finch_conversation_set_active(conv);
493 static void
494 view_log_cb(GntMenuItem *n, gpointer ggc)
496 FinchConv *fc;
497 PurpleConversation *conv;
498 PurpleLogType type;
499 const char *name;
500 PurpleAccount *account;
501 GSList *buddies;
502 GSList *cur;
504 fc = ggc;
505 conv = fc->active_conv;
507 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
508 type = PURPLE_LOG_IM;
509 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
510 type = PURPLE_LOG_CHAT;
511 else
512 return;
514 name = purple_conversation_get_name(conv);
515 account = purple_conversation_get_account(conv);
517 buddies = purple_find_buddies(account, name);
518 for (cur = buddies; cur != NULL; cur = cur->next) {
519 PurpleBlistNode *node = cur->data;
520 if ((node != NULL) &&
521 (purple_blist_node_get_sibling_prev(node) || purple_blist_node_get_sibling_next(node))) {
522 finch_log_show_contact((PurpleContact *)purple_blist_node_get_parent(node));
523 g_slist_free(buddies);
524 return;
527 g_slist_free(buddies);
529 finch_log_show(type, name, account);
532 static void
533 generate_send_to_menu(FinchConv *ggc)
535 GntWidget *sub, *menu = ggc->menu;
536 GntMenuItem *item;
537 GSList *buds;
538 GList *list = NULL;
540 buds = purple_find_buddies(purple_conversation_get_account(ggc->active_conv),
541 purple_conversation_get_name(ggc->active_conv));
542 if (!buds)
543 return;
545 if ((item = ggc->u.im->sendto) == NULL) {
546 item = gnt_menuitem_new(_("Send To"));
547 gnt_menu_add_item(GNT_MENU(menu), item);
548 ggc->u.im->sendto = item;
550 sub = gnt_menu_new(GNT_MENU_POPUP);
551 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
553 for (; buds; buds = g_slist_delete_link(buds, buds)) {
554 PurpleBlistNode *node = PURPLE_BLIST_NODE(purple_buddy_get_contact(PURPLE_BUDDY(buds->data)));
555 for (node = purple_blist_node_get_first_child(node); node != NULL;
556 node = purple_blist_node_get_sibling_next(node)) {
557 PurpleBuddy *buddy = (PurpleBuddy *)node;
558 PurpleAccount *account = purple_buddy_get_account(buddy);
559 if (purple_account_is_connected(account)) {
560 /* Use the PurplePresence to get unique buddies. */
561 PurplePresence *presence = purple_buddy_get_presence(buddy);
562 if (!g_list_find(list, presence))
563 list = g_list_prepend(list, presence);
567 for (list = g_list_reverse(list); list != NULL; list = g_list_delete_link(list, list)) {
568 PurplePresence *pre = list->data;
569 PurpleBuddy *buddy = purple_presence_get_buddy(pre);
570 PurpleAccount *account = purple_buddy_get_account(buddy);
571 gchar *name = g_strdup(purple_buddy_get_name(buddy));
572 gchar *text = g_strdup_printf("%s (%s)", purple_buddy_get_name(buddy), purple_account_get_username(account));
573 item = gnt_menuitem_new(text);
574 g_free(text);
575 gnt_menu_add_item(GNT_MENU(sub), item);
576 gnt_menuitem_set_callback(item, send_to_cb, NULL);
577 g_object_set_data(G_OBJECT(item), "purple_account", account);
578 g_object_set_data_full(G_OBJECT(item), "purple_buddy_name", name, g_free);
582 static void
583 invite_cb(GntMenuItem *item, gpointer ggconv)
585 FinchConv *fc = ggconv;
586 PurpleConversation *conv = fc->active_conv;
587 purple_conv_chat_invite_user(PURPLE_CONV_CHAT(conv), NULL, NULL, TRUE);
590 static void
591 gg_create_menu(FinchConv *ggc)
593 GntWidget *menu, *sub;
594 GntMenuItem *item;
596 ggc->menu = menu = gnt_menu_new(GNT_MENU_TOPLEVEL);
597 gnt_window_set_menu(GNT_WINDOW(ggc->window), GNT_MENU(menu));
599 item = gnt_menuitem_new(_("Conversation"));
600 gnt_menu_add_item(GNT_MENU(menu), item);
602 sub = gnt_menu_new(GNT_MENU_POPUP);
603 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
605 item = gnt_menuitem_new(_("Clear Scrollback"));
606 gnt_menu_add_item(GNT_MENU(sub), item);
607 gnt_menuitem_set_callback(item, clear_scrollback_cb, ggc);
609 item = gnt_menuitem_check_new(_("Show Timestamps"));
610 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
611 purple_prefs_get_bool(PREF_ROOT "/timestamps"));
612 gnt_menu_add_item(GNT_MENU(sub), item);
613 gnt_menuitem_set_callback(item, toggle_timestamps_cb, ggc);
615 if (purple_conversation_get_type(ggc->active_conv) == PURPLE_CONV_TYPE_IM) {
616 PurpleAccount *account = purple_conversation_get_account(ggc->active_conv);
617 PurpleConnection *gc = purple_account_get_connection(account);
618 PurplePluginProtocolInfo *pinfo =
619 gc ? PURPLE_PLUGIN_PROTOCOL_INFO(purple_connection_get_prpl(gc)) : NULL;
621 if (pinfo && pinfo->get_info) {
622 item = gnt_menuitem_new(_("Get Info"));
623 gnt_menu_add_item(GNT_MENU(sub), item);
624 gnt_menuitem_set_callback(item, get_info_cb, ggc);
627 item = gnt_menuitem_new(_("Add Buddy Pounce..."));
628 gnt_menu_add_item(GNT_MENU(sub), item);
629 gnt_menuitem_set_callback(item, add_pounce_cb, ggc);
631 if (pinfo && pinfo->send_file &&
632 (!pinfo->can_receive_file ||
633 pinfo->can_receive_file(gc, purple_conversation_get_name(ggc->active_conv)))) {
634 item = gnt_menuitem_new(_("Send File"));
635 gnt_menu_add_item(GNT_MENU(sub), item);
636 gnt_menuitem_set_callback(item, send_file_cb, ggc);
639 generate_send_to_menu(ggc);
640 } else if (purple_conversation_get_type(ggc->active_conv) == PURPLE_CONV_TYPE_CHAT) {
641 item = gnt_menuitem_new(_("Invite..."));
642 gnt_menu_add_item(GNT_MENU(sub), item);
643 gnt_menuitem_set_callback(item, invite_cb, ggc);
646 item = gnt_menuitem_new(_("View Log..."));
647 gnt_menu_add_item(GNT_MENU(sub), item);
648 gnt_menuitem_set_callback(item, view_log_cb, ggc);
650 item = gnt_menuitem_check_new(_("Enable Logging"));
651 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
652 purple_conversation_is_logging(ggc->active_conv));
653 gnt_menu_add_item(GNT_MENU(sub), item);
654 gnt_menuitem_set_callback(item, toggle_logging_cb, ggc);
656 item = gnt_menuitem_check_new(_("Enable Sounds"));
657 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
658 !(ggc->flags & FINCH_CONV_NO_SOUND));
659 gnt_menu_add_item(GNT_MENU(sub), item);
660 gnt_menuitem_set_callback(item, toggle_sound_cb, ggc);
663 static void
664 create_conv_from_userlist(GntWidget *widget, FinchConv *fc)
666 PurpleAccount *account = purple_conversation_get_account(fc->active_conv);
667 PurpleConnection *gc = purple_account_get_connection(account);
668 PurplePluginProtocolInfo *prpl_info = NULL;
669 char *name, *realname;
671 if (!gc) {
672 purple_conversation_write(fc->active_conv, NULL, _("You are not connected."),
673 PURPLE_MESSAGE_SYSTEM, time(NULL));
674 return;
677 name = gnt_tree_get_selection_data(GNT_TREE(widget));
679 prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl);
680 if (prpl_info && PURPLE_PROTOCOL_PLUGIN_HAS_FUNC(prpl_info, get_cb_real_name))
681 realname = prpl_info->get_cb_real_name(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(fc->active_conv)), name);
682 else
683 realname = NULL;
684 purple_conversation_new(PURPLE_CONV_TYPE_IM, account, realname ? realname : name);
685 g_free(realname);
688 static void
689 gained_focus_cb(GntWindow *window, FinchConv *fc)
691 GList *iter;
692 for (iter = fc->list; iter; iter = iter->next) {
693 purple_conversation_set_data(iter->data, "unseen-count", 0);
694 purple_conversation_update(iter->data, PURPLE_CONV_UPDATE_UNSEEN);
698 static void
699 completion_cb(GntEntry *entry, const char *start, const char *end)
701 if (start == entry->start && *start != '/')
702 gnt_widget_key_pressed(GNT_WIDGET(entry), ": ");
705 static void
706 gg_setup_commands(FinchConv *fconv, gboolean remove_first)
708 GList *commands;
709 char command[256] = "/";
711 if (remove_first) {
712 commands = purple_cmd_list(NULL);
713 for (; commands; commands = g_list_delete_link(commands, commands)) {
714 g_strlcpy(command + 1, commands->data, sizeof(command) - 1);
715 gnt_entry_remove_suggest(GNT_ENTRY(fconv->entry), command);
719 commands = purple_cmd_list(fconv->active_conv);
720 for (; commands; commands = g_list_delete_link(commands, commands)) {
721 g_strlcpy(command + 1, commands->data, sizeof(command) - 1);
722 gnt_entry_add_suggest(GNT_ENTRY(fconv->entry), command);
726 static void
727 cmd_added_cb(const char *cmd, PurpleCmdPriority prior, PurpleCmdFlag flags,
728 FinchConv *fconv)
730 gg_setup_commands(fconv, TRUE);
733 static void
734 cmd_removed_cb(const char *cmd, FinchConv *fconv)
736 char command[256] = "/";
737 g_strlcpy(command + 1, cmd, sizeof(command) - 1);
738 gnt_entry_remove_suggest(GNT_ENTRY(fconv->entry), command);
739 gg_setup_commands(fconv, TRUE);
742 static void
743 finch_create_conversation(PurpleConversation *conv)
745 FinchConv *ggc = FINCH_GET_DATA(conv);
746 char *title;
747 PurpleConversationType type;
748 PurpleConversation *cc;
749 PurpleAccount *account;
750 PurpleBlistNode *convnode = NULL;
752 if (ggc) {
753 gnt_window_present(ggc->window);
754 return;
757 account = purple_conversation_get_account(conv);
758 cc = find_conv_with_contact(account, purple_conversation_get_name(conv));
759 if (cc && FINCH_GET_DATA(cc))
760 ggc = FINCH_GET_DATA(cc);
761 else
762 ggc = g_new0(FinchConv, 1);
764 /* Each conversation with the same person will have the same logging setting */
765 if (ggc->list) {
766 purple_conversation_set_logging(conv,
767 purple_conversation_is_logging(ggc->list->data));
770 ggc->list = g_list_prepend(ggc->list, conv);
771 ggc->active_conv = conv;
772 FINCH_SET_DATA(conv, ggc);
774 if (cc && FINCH_GET_DATA(cc) && cc != conv) {
775 finch_conversation_set_active(conv);
776 return;
779 type = purple_conversation_get_type(conv);
780 title = get_conversation_title(conv, account);
782 ggc->window = gnt_vwindow_new(FALSE);
783 gnt_box_set_title(GNT_BOX(ggc->window), title);
784 gnt_box_set_toplevel(GNT_BOX(ggc->window), TRUE);
785 gnt_box_set_pad(GNT_BOX(ggc->window), 0);
787 switch (purple_conversation_get_type(conv)) {
788 case PURPLE_CONV_TYPE_UNKNOWN:
789 gnt_widget_set_name(ggc->window, "conversation-window-unknown" );
790 break;
791 case PURPLE_CONV_TYPE_IM:
792 gnt_widget_set_name(ggc->window, "conversation-window-im" );
793 break;
794 case PURPLE_CONV_TYPE_CHAT:
795 gnt_widget_set_name(ggc->window, "conversation-window-chat" );
796 break;
797 case PURPLE_CONV_TYPE_MISC:
798 gnt_widget_set_name(ggc->window, "conversation-window-misc" );
799 break;
800 case PURPLE_CONV_TYPE_ANY:
801 gnt_widget_set_name(ggc->window, "conversation-window-any" );
802 break;
805 ggc->tv = gnt_text_view_new();
806 gnt_widget_set_name(ggc->tv, "conversation-window-textview");
807 gnt_widget_set_size(ggc->tv, purple_prefs_get_int(PREF_ROOT "/size/width"),
808 purple_prefs_get_int(PREF_ROOT "/size/height"));
810 if (type == PURPLE_CONV_TYPE_CHAT) {
811 GntWidget *hbox, *tree;
812 FinchConvChat *fc = ggc->u.chat = g_new0(FinchConvChat, 1);
813 hbox = gnt_hbox_new(FALSE);
814 gnt_box_set_pad(GNT_BOX(hbox), 0);
815 tree = fc->userlist = gnt_tree_new_with_columns(2);
816 gnt_tree_set_col_width(GNT_TREE(tree), 0, 1); /* The flag column */
817 gnt_tree_set_compare_func(GNT_TREE(tree), (GCompareFunc)g_utf8_collate);
818 gnt_tree_set_hash_fns(GNT_TREE(tree), g_str_hash, g_str_equal, g_free);
819 gnt_tree_set_search_column(GNT_TREE(tree), 1);
820 GNT_WIDGET_SET_FLAGS(tree, GNT_WIDGET_NO_BORDER);
821 gnt_box_add_widget(GNT_BOX(hbox), ggc->tv);
822 gnt_box_add_widget(GNT_BOX(hbox), tree);
823 gnt_box_add_widget(GNT_BOX(ggc->window), hbox);
824 g_signal_connect(G_OBJECT(tree), "activate", G_CALLBACK(create_conv_from_userlist), ggc);
825 gnt_widget_set_visible(tree, purple_prefs_get_bool(PREF_USERLIST));
826 } else {
827 ggc->u.im = g_new0(FinchConvIm, 1);
828 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->tv);
831 ggc->info = gnt_vbox_new(FALSE);
832 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->info);
834 ggc->entry = gnt_entry_new(NULL);
835 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->entry);
836 gnt_widget_set_name(ggc->entry, "conversation-window-entry");
837 gnt_entry_set_history_length(GNT_ENTRY(ggc->entry), -1);
838 gnt_entry_set_word_suggest(GNT_ENTRY(ggc->entry), TRUE);
839 gnt_entry_set_always_suggest(GNT_ENTRY(ggc->entry), FALSE);
841 gnt_text_view_attach_scroll_widget(GNT_TEXT_VIEW(ggc->tv), ggc->entry);
842 gnt_text_view_attach_pager_widget(GNT_TEXT_VIEW(ggc->tv), ggc->entry);
844 g_signal_connect_after(G_OBJECT(ggc->entry), "activate", G_CALLBACK(entry_key_pressed), ggc);
845 g_signal_connect(G_OBJECT(ggc->entry), "completion", G_CALLBACK(completion_cb), NULL);
846 g_signal_connect(G_OBJECT(ggc->window), "destroy", G_CALLBACK(closing_window), ggc);
848 gnt_widget_set_position(ggc->window, purple_prefs_get_int(PREF_ROOT "/position/x"),
849 purple_prefs_get_int(PREF_ROOT "/position/y"));
850 gnt_widget_show(ggc->window);
852 g_signal_connect(G_OBJECT(ggc->tv), "size_changed", G_CALLBACK(size_changed_cb), NULL);
853 g_signal_connect(G_OBJECT(ggc->window), "position_set", G_CALLBACK(save_position_cb), NULL);
855 if (type == PURPLE_CONV_TYPE_IM) {
856 g_signal_connect(G_OBJECT(ggc->entry), "text_changed", G_CALLBACK(send_typing_notification), ggc);
859 convnode = get_conversation_blist_node(conv);
860 if ((convnode && purple_blist_node_get_bool(convnode, "gnt-mute-sound")) ||
861 !finch_sound_is_enabled())
862 ggc->flags |= FINCH_CONV_NO_SOUND;
864 gg_create_menu(ggc);
865 gg_setup_commands(ggc, FALSE);
867 purple_signal_connect(purple_cmds_get_handle(), "cmd-added", ggc,
868 G_CALLBACK(cmd_added_cb), ggc);
869 purple_signal_connect(purple_cmds_get_handle(), "cmd-removed", ggc,
870 G_CALLBACK(cmd_removed_cb), ggc);
872 g_free(title);
873 gnt_box_give_focus_to_child(GNT_BOX(ggc->window), ggc->entry);
874 g_signal_connect(G_OBJECT(ggc->window), "gained-focus", G_CALLBACK(gained_focus_cb), ggc);
877 static void
878 finch_destroy_conversation(PurpleConversation *conv)
880 /* do stuff here */
881 FinchConv *ggc = FINCH_GET_DATA(conv);
882 ggc->list = g_list_remove(ggc->list, conv);
883 if (ggc->list && conv == ggc->active_conv) {
884 ggc->active_conv = ggc->list->data;
885 gg_setup_commands(ggc, TRUE);
888 if (ggc->list == NULL) {
889 g_free(ggc->u.chat);
890 purple_signals_disconnect_by_handle(ggc);
891 if (ggc->window)
892 gnt_widget_destroy(ggc->window);
893 g_free(ggc);
897 static void
898 finch_write_common(PurpleConversation *conv, const char *who, const char *message,
899 PurpleMessageFlags flags, time_t mtime)
901 FinchConv *ggconv = FINCH_GET_DATA(conv);
902 char *strip, *newline;
903 GntTextFormatFlags fl = 0;
904 int pos;
906 g_return_if_fail(ggconv != NULL);
908 if ((flags & PURPLE_MESSAGE_SYSTEM) && !(flags & PURPLE_MESSAGE_NOTIFY)) {
909 flags &= ~(PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV);
912 if (ggconv->active_conv != conv) {
913 if (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV))
914 finch_conversation_set_active(conv);
915 else
916 return;
919 pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggconv->tv));
921 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggconv->tv), "typing", NULL, TRUE);
922 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), "\n", GNT_TEXT_FLAG_NORMAL);
924 /* Unnecessary to print the timestamp for delayed message */
925 if (purple_prefs_get_bool("/finch/conversations/timestamps")) {
926 if (!mtime)
927 time(&mtime);
928 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
929 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)), gnt_color_pair(color_timestamp));
932 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), " ", GNT_TEXT_FLAG_NORMAL);
934 if (flags & PURPLE_MESSAGE_AUTO_RESP)
935 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
936 _("<AUTO-REPLY> "), GNT_TEXT_FLAG_BOLD);
938 if (who && *who && (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV)) &&
939 !(flags & PURPLE_MESSAGE_NOTIFY))
941 char * name = NULL;
942 GntTextFormatFlags msgflags = GNT_TEXT_FLAG_NORMAL;
943 gboolean me = FALSE;
945 if (purple_message_meify((char*)message, -1)) {
946 name = g_strdup_printf("*** %s", who);
947 if (!(flags & PURPLE_MESSAGE_SEND) &&
948 (flags & PURPLE_MESSAGE_NICK))
949 msgflags = gnt_color_pair(color_message_highlight);
950 else
951 msgflags = gnt_color_pair(color_message_action);
952 me = TRUE;
953 } else {
954 name = g_strdup_printf("%s", who);
955 if (flags & PURPLE_MESSAGE_SEND)
956 msgflags = gnt_color_pair(color_message_send);
957 else if (flags & PURPLE_MESSAGE_NICK)
958 msgflags = gnt_color_pair(color_message_highlight);
959 else
960 msgflags = gnt_color_pair(color_message_receive);
962 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
963 name, msgflags);
964 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), me ? " " : ": ", GNT_TEXT_FLAG_NORMAL);
965 g_free(name);
966 } else
967 fl = GNT_TEXT_FLAG_DIM;
969 if (flags & PURPLE_MESSAGE_ERROR)
970 fl |= GNT_TEXT_FLAG_BOLD;
972 /* XXX: Remove this workaround when textview can parse messages. */
973 newline = purple_strdup_withhtml(message);
974 strip = purple_markup_strip_html(newline);
975 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
976 strip, fl);
978 g_free(newline);
979 g_free(strip);
981 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM &&
982 purple_conv_im_get_typing_state(PURPLE_CONV_IM(conv)) == PURPLE_TYPING) {
983 strip = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_title(conv));
984 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggconv->tv),
985 strip, GNT_TEXT_FLAG_DIM, "typing");
986 g_free(strip);
989 if (pos <= 1)
990 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 0);
992 if (flags & (PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_NICK | PURPLE_MESSAGE_ERROR))
993 gnt_widget_set_urgent(ggconv->tv);
994 if (flags & PURPLE_MESSAGE_RECV && !gnt_widget_has_focus(ggconv->window)) {
995 int count = GPOINTER_TO_INT(purple_conversation_get_data(conv, "unseen-count"));
996 purple_conversation_set_data(conv, "unseen-count", GINT_TO_POINTER(count + 1));
997 purple_conversation_update(conv, PURPLE_CONV_UPDATE_UNSEEN);
1001 static void
1002 finch_write_chat(PurpleConversation *conv, const char *who, const char *message,
1003 PurpleMessageFlags flags, time_t mtime)
1005 purple_conversation_write(conv, who, message, flags, mtime);
1008 static void
1009 finch_write_im(PurpleConversation *conv, const char *who, const char *message,
1010 PurpleMessageFlags flags, time_t mtime)
1012 PurpleAccount *account = purple_conversation_get_account(conv);
1013 if (flags & PURPLE_MESSAGE_SEND)
1015 who = purple_connection_get_display_name(purple_account_get_connection(account));
1016 if (!who)
1017 who = purple_account_get_alias(account);
1018 if (!who)
1019 who = purple_account_get_username(account);
1021 else if (flags & PURPLE_MESSAGE_RECV)
1023 PurpleBuddy *buddy;
1024 who = purple_conversation_get_name(conv);
1025 buddy = purple_find_buddy(account, who);
1026 if (buddy)
1027 who = purple_buddy_get_contact_alias(buddy);
1030 purple_conversation_write(conv, who, message, flags, mtime);
1033 static void
1034 finch_write_conv(PurpleConversation *conv, const char *who, const char *alias,
1035 const char *message, PurpleMessageFlags flags, time_t mtime)
1037 const char *name;
1038 if (alias && *alias)
1039 name = alias;
1040 else if (who && *who)
1041 name = who;
1042 else
1043 name = NULL;
1045 finch_write_common(conv, name, message, flags, mtime);
1048 static const char *
1049 chat_flag_text(PurpleConvChatBuddyFlags flags)
1051 if (flags & PURPLE_CBFLAGS_FOUNDER)
1052 return "~";
1053 if (flags & PURPLE_CBFLAGS_OP)
1054 return "@";
1055 if (flags & PURPLE_CBFLAGS_HALFOP)
1056 return "%";
1057 if (flags & PURPLE_CBFLAGS_VOICE)
1058 return "+";
1059 return " ";
1062 static void
1063 finch_chat_add_users(PurpleConversation *conv, GList *users, gboolean new_arrivals)
1065 FinchConv *ggc = FINCH_GET_DATA(conv);
1066 GntEntry *entry = GNT_ENTRY(ggc->entry);
1068 if (!new_arrivals)
1070 /* Print the list of users in the room */
1071 GString *string = g_string_new(NULL);
1072 GList *iter;
1073 int count = g_list_length(users);
1075 g_string_printf(string,
1076 ngettext("List of %d user:\n", "List of %d users:\n", count), count);
1077 for (iter = users; iter; iter = iter->next)
1079 PurpleConvChatBuddy *cbuddy = iter->data;
1080 char *str;
1082 if ((str = cbuddy->alias) == NULL)
1083 str = cbuddy->name;
1084 g_string_append_printf(string, "[ %s ]", str);
1087 purple_conversation_write(conv, NULL, string->str,
1088 PURPLE_MESSAGE_SYSTEM, time(NULL));
1089 g_string_free(string, TRUE);
1092 for (; users; users = users->next)
1094 PurpleConvChatBuddy *cbuddy = users->data;
1095 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1096 gnt_entry_add_suggest(entry, cbuddy->name);
1097 gnt_entry_add_suggest(entry, cbuddy->alias);
1098 gnt_tree_add_row_after(tree, g_strdup(cbuddy->name),
1099 gnt_tree_create_row(tree, chat_flag_text(cbuddy->flags), cbuddy->alias), NULL, NULL);
1103 static void
1104 finch_chat_rename_user(PurpleConversation *conv, const char *old, const char *new_n, const char *new_a)
1106 /* Update the name for string completion */
1107 FinchConv *ggc = FINCH_GET_DATA(conv);
1108 GntEntry *entry = GNT_ENTRY(ggc->entry);
1109 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1110 PurpleConvChatBuddy *cb = purple_conv_chat_cb_find(PURPLE_CONV_CHAT(conv), new_n);
1112 gnt_entry_remove_suggest(entry, old);
1113 gnt_tree_remove(tree, (gpointer)old);
1115 gnt_entry_add_suggest(entry, new_n);
1116 gnt_entry_add_suggest(entry, new_a);
1117 gnt_tree_add_row_after(tree, g_strdup(new_n),
1118 gnt_tree_create_row(tree, chat_flag_text(cb->flags), new_a), NULL, NULL);
1121 static void
1122 finch_chat_remove_users(PurpleConversation *conv, GList *list)
1124 /* Remove the name from string completion */
1125 FinchConv *ggc = FINCH_GET_DATA(conv);
1126 GntEntry *entry = GNT_ENTRY(ggc->entry);
1127 for (; list; list = list->next) {
1128 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1129 gnt_entry_remove_suggest(entry, list->data);
1130 gnt_tree_remove(tree, list->data);
1134 static void
1135 finch_chat_update_user(PurpleConversation *conv, const char *user)
1137 PurpleConvChatBuddy *cb = purple_conv_chat_cb_find(PURPLE_CONV_CHAT(conv), user);
1138 FinchConv *ggc = FINCH_GET_DATA(conv);
1139 gnt_tree_change_text(GNT_TREE(ggc->u.chat->userlist), (gpointer)user, 0, chat_flag_text(cb->flags));
1142 static void
1143 finch_conv_present(PurpleConversation *conv)
1145 FinchConv *fc = FINCH_CONV(conv);
1146 if (fc && fc->window)
1147 gnt_window_present(fc->window);
1150 static gboolean
1151 finch_conv_has_focus(PurpleConversation *conv)
1153 FinchConv *fc = FINCH_CONV(conv);
1154 if (fc && fc->window)
1155 return gnt_widget_has_focus(fc->window);
1156 return FALSE;
1159 static PurpleConversationUiOps conv_ui_ops =
1161 finch_create_conversation,
1162 finch_destroy_conversation,
1163 finch_write_chat,
1164 finch_write_im,
1165 finch_write_conv,
1166 finch_chat_add_users,
1167 finch_chat_rename_user,
1168 finch_chat_remove_users,
1169 finch_chat_update_user,
1170 finch_conv_present, /* present */
1171 finch_conv_has_focus, /* has_focus */
1172 NULL, /* custom_smiley_add */
1173 NULL, /* custom_smiley_write */
1174 NULL, /* custom_smiley_close */
1175 NULL, /* send_confirm */
1176 NULL,
1177 NULL,
1178 NULL,
1179 NULL
1182 PurpleConversationUiOps *finch_conv_get_ui_ops()
1184 return &conv_ui_ops;
1187 /* Xerox */
1188 static PurpleCmdRet
1189 say_command_cb(PurpleConversation *conv,
1190 const char *cmd, char **args, char **error, void *data)
1192 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
1193 purple_conv_im_send(PURPLE_CONV_IM(conv), args[0]);
1194 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
1195 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), args[0]);
1197 return PURPLE_CMD_RET_OK;
1200 /* Xerox */
1201 static PurpleCmdRet
1202 me_command_cb(PurpleConversation *conv,
1203 const char *cmd, char **args, char **error, void *data)
1205 char *tmp;
1207 tmp = g_strdup_printf("/me %s", args[0]);
1209 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
1210 purple_conv_im_send(PURPLE_CONV_IM(conv), tmp);
1211 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
1212 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), tmp);
1214 g_free(tmp);
1215 return PURPLE_CMD_RET_OK;
1218 /* Xerox */
1219 static PurpleCmdRet
1220 debug_command_cb(PurpleConversation *conv,
1221 const char *cmd, char **args, char **error, void *data)
1223 char *tmp, *markup;
1225 if (!g_ascii_strcasecmp(args[0], "version")) {
1226 tmp = g_strdup_printf("Using Finch v%s with libpurple v%s.",
1227 DISPLAY_VERSION, purple_core_get_version());
1228 } else if (!g_ascii_strcasecmp(args[0], "plugins")) {
1229 /* Show all the loaded plugins, including the protocol plugins and plugin loaders.
1230 * This is intentional, since third party prpls are often sources of bugs, and some
1231 * plugin loaders (e.g. mono) can also be buggy.
1233 GString *str = g_string_new("Loaded Plugins: ");
1234 const GList *plugins = purple_plugins_get_loaded();
1235 if (plugins) {
1236 for (; plugins; plugins = plugins->next) {
1237 str = g_string_append(str, purple_plugin_get_name(plugins->data));
1238 if (plugins->next)
1239 str = g_string_append(str, ", ");
1241 } else {
1242 str = g_string_append(str, "(none)");
1245 tmp = g_string_free(str, FALSE);
1246 } else {
1247 purple_conversation_write(conv, NULL, _("Supported debug options are: plugins version"),
1248 PURPLE_MESSAGE_NO_LOG|PURPLE_MESSAGE_ERROR, time(NULL));
1249 return PURPLE_CMD_RET_OK;
1252 markup = g_markup_escape_text(tmp, -1);
1253 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
1254 purple_conv_im_send(PURPLE_CONV_IM(conv), markup);
1255 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
1256 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), markup);
1258 g_free(tmp);
1259 g_free(markup);
1260 return PURPLE_CMD_RET_OK;
1263 /* Xerox */
1264 static PurpleCmdRet
1265 clear_command_cb(PurpleConversation *conv,
1266 const char *cmd, char **args, char **error, void *data)
1268 FinchConv *ggconv = FINCH_GET_DATA(conv);
1269 gnt_text_view_clear(GNT_TEXT_VIEW(ggconv->tv));
1270 purple_conversation_clear_message_history(conv);
1271 return PURPLE_CMD_RET_OK;
1274 /* Xerox */
1275 static PurpleCmdRet
1276 help_command_cb(PurpleConversation *conv,
1277 const char *cmd, char **args, char **error, void *data)
1279 GList *l, *text;
1280 GString *s;
1282 if (args[0] != NULL) {
1283 s = g_string_new("");
1284 text = purple_cmd_help(conv, args[0]);
1286 if (text) {
1287 for (l = text; l; l = l->next)
1288 if (l->next)
1289 g_string_append_printf(s, "%s\n", (char *)l->data);
1290 else
1291 g_string_append_printf(s, "%s", (char *)l->data);
1292 } else {
1293 g_string_append(s, _("No such command (in this context)."));
1295 } else {
1296 s = g_string_new(_("Use \"/help &lt;command&gt;\" for help on a specific command.\n"
1297 "The following commands are available in this context:\n"));
1299 text = purple_cmd_list(conv);
1300 for (l = text; l; l = l->next)
1301 if (l->next)
1302 g_string_append_printf(s, "%s, ", (char *)l->data);
1303 else
1304 g_string_append_printf(s, "%s.", (char *)l->data);
1305 g_list_free(text);
1308 purple_conversation_write(conv, NULL, s->str, PURPLE_MESSAGE_NO_LOG, time(NULL));
1309 g_string_free(s, TRUE);
1311 return PURPLE_CMD_RET_OK;
1314 static PurpleCmdRet
1315 cmd_show_window(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1317 void (*callback)(void) = data;
1318 callback();
1319 return PURPLE_CMD_RET_OK;
1322 #if GLIB_CHECK_VERSION(2,6,0)
1323 static PurpleCmdRet
1324 cmd_message_color(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1326 int *msgclass = NULL;
1327 int fg, bg;
1329 if (strcmp(args[0], "receive") == 0)
1330 msgclass = &color_message_receive;
1331 else if (strcmp(args[0], "send") == 0)
1332 msgclass = &color_message_send;
1333 else if (strcmp(args[0], "highlight") == 0)
1334 msgclass = &color_message_highlight;
1335 else if (strcmp(args[0], "action") == 0)
1336 msgclass = &color_message_action;
1337 else if (strcmp(args[0], "timestamp") == 0)
1338 msgclass = &color_timestamp;
1339 else {
1340 if (error)
1341 *error = g_strdup_printf(_("%s is not a valid message class. See '/help msgcolor' for valid message classes."), args[0]);
1342 return PURPLE_CMD_RET_FAILED;
1345 fg = gnt_colors_get_color(args[1]);
1346 if (fg == -EINVAL) {
1347 if (error)
1348 *error = g_strdup_printf(_("%s is not a valid color. See '/help msgcolor' for valid colors."), args[1]);
1349 return PURPLE_CMD_RET_FAILED;
1352 bg = gnt_colors_get_color(args[2]);
1353 if (bg == -EINVAL) {
1354 if (error)
1355 *error = g_strdup_printf(_("%s is not a valid color. See '/help msgcolor' for valid colors."), args[2]);
1356 return PURPLE_CMD_RET_FAILED;
1359 init_pair(*msgclass, fg, bg);
1361 return PURPLE_CMD_RET_OK;
1363 #endif
1365 static PurpleCmdRet
1366 users_command_cb(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1368 FinchConv *fc = FINCH_GET_DATA(conv);
1369 FinchConvChat *ch;
1370 if (!fc)
1371 return PURPLE_CMD_RET_FAILED;
1373 ch = fc->u.chat;
1374 gnt_widget_set_visible(ch->userlist,
1375 (GNT_WIDGET_IS_FLAG_SET(ch->userlist, GNT_WIDGET_INVISIBLE)));
1376 gnt_box_readjust(GNT_BOX(fc->window));
1377 gnt_box_give_focus_to_child(GNT_BOX(fc->window), fc->entry);
1378 purple_prefs_set_bool(PREF_USERLIST, !(GNT_WIDGET_IS_FLAG_SET(ch->userlist, GNT_WIDGET_INVISIBLE)));
1379 return PURPLE_CMD_RET_OK;
1382 void finch_conversation_init()
1384 color_message_send = gnt_style_get_color(NULL, "color-message-sent");
1385 if (!color_message_send)
1386 color_message_send = gnt_color_add_pair(COLOR_CYAN, -1);
1387 color_message_receive = gnt_style_get_color(NULL, "color-message-received");
1388 if (!color_message_receive)
1389 color_message_receive = gnt_color_add_pair(COLOR_RED, -1);
1390 color_message_highlight = gnt_style_get_color(NULL, "color-message-highlight");
1391 if (!color_message_highlight)
1392 color_message_highlight = gnt_color_add_pair(COLOR_GREEN, -1);
1393 color_timestamp = gnt_style_get_color(NULL, "color-timestamp");
1394 if (!color_timestamp)
1395 color_timestamp = gnt_color_add_pair(COLOR_BLUE, -1);
1396 color_message_action = gnt_style_get_color(NULL, "color-message-action");
1397 if (!color_message_action)
1398 color_message_action = gnt_color_add_pair(COLOR_YELLOW, -1);
1399 purple_prefs_add_none(PREF_ROOT);
1400 purple_prefs_add_none(PREF_ROOT "/size");
1401 purple_prefs_add_int(PREF_ROOT "/size/width", 70);
1402 purple_prefs_add_int(PREF_ROOT "/size/height", 20);
1403 purple_prefs_add_none(PREF_ROOT "/position");
1404 purple_prefs_add_int(PREF_ROOT "/position/x", 0);
1405 purple_prefs_add_int(PREF_ROOT "/position/y", 0);
1406 purple_prefs_add_none(PREF_CHAT);
1407 purple_prefs_add_bool(PREF_USERLIST, FALSE);
1409 /* Xerox the commands */
1410 purple_cmd_register("say", "S", PURPLE_CMD_P_DEFAULT,
1411 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1412 say_command_cb, _("say &lt;message&gt;: Send a message normally as if you weren't using a command."), NULL);
1413 purple_cmd_register("me", "S", PURPLE_CMD_P_DEFAULT,
1414 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1415 me_command_cb, _("me &lt;action&gt;: Send an IRC style action to a buddy or chat."), NULL);
1416 purple_cmd_register("debug", "w", PURPLE_CMD_P_DEFAULT,
1417 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1418 debug_command_cb, _("debug &lt;option&gt;: Send various debug information to the current conversation."), NULL);
1419 purple_cmd_register("clear", "", PURPLE_CMD_P_DEFAULT,
1420 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1421 clear_command_cb, _("clear: Clears the conversation scrollback."), NULL);
1422 purple_cmd_register("help", "w", PURPLE_CMD_P_DEFAULT,
1423 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL,
1424 help_command_cb, _("help &lt;command&gt;: Help on a specific command."), NULL);
1425 purple_cmd_register("users", "", PURPLE_CMD_P_DEFAULT,
1426 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL,
1427 users_command_cb, _("users: Show the list of users in the chat."), NULL);
1429 /* Now some commands to bring up some other windows */
1430 purple_cmd_register("plugins", "", PURPLE_CMD_P_DEFAULT,
1431 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1432 cmd_show_window, _("plugins: Show the plugins window."), finch_plugins_show_all);
1433 purple_cmd_register("buddylist", "", PURPLE_CMD_P_DEFAULT,
1434 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1435 cmd_show_window, _("buddylist: Show the buddylist."), finch_blist_show);
1436 purple_cmd_register("accounts", "", PURPLE_CMD_P_DEFAULT,
1437 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1438 cmd_show_window, _("accounts: Show the accounts window."), finch_accounts_show_all);
1439 purple_cmd_register("debugwin", "", PURPLE_CMD_P_DEFAULT,
1440 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1441 cmd_show_window, _("debugwin: Show the debug window."), finch_debug_window_show);
1442 purple_cmd_register("prefs", "", PURPLE_CMD_P_DEFAULT,
1443 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1444 cmd_show_window, _("prefs: Show the preference window."), finch_prefs_show_all);
1445 purple_cmd_register("status", "", PURPLE_CMD_P_DEFAULT,
1446 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1447 cmd_show_window, _("statuses: Show the savedstatuses window."), finch_savedstatus_show_all);
1449 #if GLIB_CHECK_VERSION(2,6,0)
1450 /* Allow customizing the message colors using a command during run-time */
1451 purple_cmd_register("msgcolor", "www", PURPLE_CMD_P_DEFAULT,
1452 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1453 cmd_message_color, _("msgcolor &lt;class&gt; &lt;foreground&gt; &lt;background&gt;: "
1454 "Set the color for different classes of messages in the conversation window.<br>"
1455 " &lt;class&gt;: receive, send, highlight, action, timestamp<br>"
1456 " &lt;foreground/background&gt;: black, red, green, blue, white, gray, darkgray, magenta, cyan, default<br><br>"
1457 "EXAMPLE:<br> msgcolor send cyan default"),
1458 NULL);
1459 #endif
1461 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", finch_conv_get_handle(),
1462 PURPLE_CALLBACK(update_buddy_typing), NULL);
1463 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", finch_conv_get_handle(),
1464 PURPLE_CALLBACK(update_buddy_typing), NULL);
1465 purple_signal_connect(purple_conversations_get_handle(), "chat-left", finch_conv_get_handle(),
1466 PURPLE_CALLBACK(chat_left_cb), NULL);
1467 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", finch_conv_get_handle(),
1468 PURPLE_CALLBACK(buddy_signed_on_off), NULL);
1469 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", finch_conv_get_handle(),
1470 PURPLE_CALLBACK(buddy_signed_on_off), NULL);
1471 purple_signal_connect(purple_connections_get_handle(), "signed-on", finch_conv_get_handle(),
1472 PURPLE_CALLBACK(account_signed_on_off), NULL);
1473 purple_signal_connect(purple_connections_get_handle(), "signed-off", finch_conv_get_handle(),
1474 PURPLE_CALLBACK(account_signed_on_off), NULL);
1475 purple_signal_connect(purple_connections_get_handle(), "signing-off", finch_conv_get_handle(),
1476 PURPLE_CALLBACK(account_signing_off), NULL);
1479 void finch_conversation_uninit()
1481 purple_signals_disconnect_by_handle(finch_conv_get_handle());
1484 void finch_conversation_set_active(PurpleConversation *conv)
1486 FinchConv *ggconv = FINCH_GET_DATA(conv);
1487 PurpleAccount *account;
1488 char *title;
1490 g_return_if_fail(ggconv);
1491 g_return_if_fail(g_list_find(ggconv->list, conv));
1492 if (ggconv->active_conv == conv)
1493 return;
1495 ggconv->active_conv = conv;
1496 gg_setup_commands(ggconv, TRUE);
1497 account = purple_conversation_get_account(conv);
1498 title = get_conversation_title(conv, account);
1499 gnt_screen_rename_widget(ggconv->window, title);
1500 g_free(title);
1503 void finch_conversation_set_info_widget(PurpleConversation *conv, GntWidget *widget)
1505 FinchConv *fc = FINCH_GET_DATA(conv);
1506 int height, width;
1508 gnt_box_remove_all(GNT_BOX(fc->info));
1510 if (widget) {
1511 gnt_box_add_widget(GNT_BOX(fc->info), widget);
1512 gnt_box_readjust(GNT_BOX(fc->info));
1515 gnt_widget_get_size(fc->window, &width, &height);
1516 gnt_box_readjust(GNT_BOX(fc->window));
1517 gnt_screen_resize_widget(fc->window, width, height);
1518 gnt_box_give_focus_to_child(GNT_BOX(fc->window), fc->entry);