Replace functions which called once with their bodies
[pidgin-git.git] / finch / gntconv.c
blobd94b6f132d069477ee431d7bb063c591b587d201
1 /*
2 * finch
4 * Finch is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include <internal.h>
24 #include "finch.h"
26 #include NCURSES_HEADER
28 #include <cmds.h>
29 #include <core.h>
30 #include <idle.h>
31 #include <prefs.h>
32 #include <util.h>
34 #include "gntaccount.h"
35 #include "gntblist.h"
36 #include "gntconv.h"
37 #include "gntdebug.h"
38 #include "gntlog.h"
39 #include "gntplugin.h"
40 #include "gntpounce.h"
41 #include "gntprefs.h"
42 #include "gntrequest.h"
43 #include "gntsound.h"
44 #include "gntstatus.h"
46 #include "gnt.h"
47 #include "gntbox.h"
48 #include "gntentry.h"
49 #include "gntlabel.h"
50 #include "gntmenu.h"
51 #include "gntmenuitem.h"
52 #include "gntmenuitemcheck.h"
53 #include "gntmenuutil.h"
54 #include "gntstyle.h"
55 #include "gnttextview.h"
56 #include "gnttree.h"
57 #include "gntutils.h"
58 #include "gntwindow.h"
60 #define PREF_ROOT "/finch/conversations"
61 #define PREF_CHAT PREF_ROOT "/chats"
62 #define PREF_USERLIST PREF_CHAT "/userlist"
64 #include "config.h"
66 static void generate_send_to_menu(FinchConv *ggc);
67 static void generate_e2ee_menu(FinchConv *ggc);
69 static int color_message_receive;
70 static int color_message_send;
71 static int color_message_highlight;
72 static int color_message_action;
73 static int color_timestamp;
75 static PurpleBuddy *
76 find_buddy_for_conversation(PurpleConversation *conv)
78 return purple_blist_find_buddy(purple_conversation_get_account(conv),
79 purple_conversation_get_name(conv));
82 static PurpleChat *
83 find_chat_for_conversation(PurpleConversation *conv)
85 return purple_blist_find_chat(purple_conversation_get_account(conv),
86 purple_conversation_get_name(conv));
89 static PurpleBlistNode *
90 get_conversation_blist_node(PurpleConversation *conv)
92 PurpleBlistNode *node = NULL;
94 if (PURPLE_IS_IM_CONVERSATION(conv)) {
95 node = (PurpleBlistNode*)find_buddy_for_conversation(conv);
96 node = node ? purple_blist_node_get_parent(node) : NULL;
97 } else if (PURPLE_IS_CHAT_CONVERSATION(conv)) {
98 node = (PurpleBlistNode*)find_chat_for_conversation(conv);
101 return node;
104 static void
105 send_typing_notification(GntWidget *w, FinchConv *ggconv)
107 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry));
108 gboolean empty = (!text || !*text || (*text == '/'));
109 if (purple_prefs_get_bool("/finch/conversations/notify_typing")) {
110 PurpleConversation *conv = ggconv->active_conv;
111 PurpleIMConversation *im = PURPLE_IM_CONVERSATION(conv);
112 if (!empty) {
113 gboolean send = (purple_im_conversation_get_send_typed_timeout(im) == 0);
115 purple_im_conversation_stop_send_typed_timeout(im);
116 purple_im_conversation_start_send_typed_timeout(im);
117 if (send || (purple_im_conversation_get_type_again(im) != 0 &&
118 time(NULL) > purple_im_conversation_get_type_again(im))) {
119 unsigned int timeout;
120 timeout = purple_serv_send_typing(purple_conversation_get_connection(conv),
121 purple_conversation_get_name(conv),
122 PURPLE_IM_TYPING);
123 purple_im_conversation_set_type_again(im, timeout);
125 } else {
126 purple_im_conversation_stop_send_typed_timeout(im);
128 purple_serv_send_typing(purple_conversation_get_connection(conv),
129 purple_conversation_get_name(conv),
130 PURPLE_IM_NOT_TYPING);
135 static void
136 entry_key_pressed(GntWidget *w, FinchConv *ggconv)
138 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry));
139 if (*text == '/' && *(text + 1) != '/')
141 PurpleConversation *conv = ggconv->active_conv;
142 PurpleCmdStatus status;
143 const char *cmdline = text + 1;
144 char *error = NULL, *escape;
146 escape = g_markup_escape_text(cmdline, -1);
147 status = purple_cmd_do_command(conv, cmdline, escape, &error);
148 g_free(escape);
150 switch (status)
152 case PURPLE_CMD_STATUS_OK:
153 break;
154 case PURPLE_CMD_STATUS_NOT_FOUND:
155 purple_conversation_write_system_message(conv,
156 _("No such command."), PURPLE_MESSAGE_NO_LOG);
157 break;
158 case PURPLE_CMD_STATUS_WRONG_ARGS:
159 purple_conversation_write_system_message(conv,
160 _("Syntax Error: You typed the wrong "
161 "number of arguments to that command."),
162 PURPLE_MESSAGE_NO_LOG);
163 break;
164 case PURPLE_CMD_STATUS_FAILED:
165 purple_conversation_write_system_message(conv,
166 error ? error : _("Your command failed for an unknown reason."),
167 PURPLE_MESSAGE_NO_LOG);
168 break;
169 case PURPLE_CMD_STATUS_WRONG_TYPE:
170 if(PURPLE_IS_IM_CONVERSATION(conv))
171 purple_conversation_write_system_message(conv,
172 _("That command only works in chats, not IMs."),
173 PURPLE_MESSAGE_NO_LOG);
174 else
175 purple_conversation_write_system_message(conv,
176 _("That command only works in IMs, not chats."),
177 PURPLE_MESSAGE_NO_LOG);
178 break;
179 case PURPLE_CMD_STATUS_WRONG_PROTOCOL:
180 purple_conversation_write_system_message(conv,
181 _("That command doesn't work on this protocol."),
182 PURPLE_MESSAGE_NO_LOG);
183 break;
185 g_free(error);
187 else if (!purple_account_is_connected(purple_conversation_get_account(ggconv->active_conv)))
189 purple_conversation_write_system_message(ggconv->active_conv,
190 _("Message was not sent, because you are not signed on."),
191 PURPLE_MESSAGE_ERROR | PURPLE_MESSAGE_NO_LOG);
193 else
195 char *escape = purple_markup_escape_text((*text == '/' ? text + 1 : text), -1);
196 purple_conversation_send(ggconv->active_conv, escape);
197 g_free(escape);
198 purple_idle_touch();
200 gnt_entry_add_to_history(GNT_ENTRY(ggconv->entry), text);
201 gnt_entry_clear(GNT_ENTRY(ggconv->entry));
204 static void
205 closing_window(GntWidget *window, FinchConv *ggconv)
207 GList *list = ggconv->list;
208 ggconv->window = NULL;
209 while (list) {
210 PurpleConversation *conv = list->data;
211 list = list->next;
212 g_object_unref(conv);
216 static void
217 size_changed_cb(GntWidget *widget, int width, int height)
219 int w, h;
220 gnt_widget_get_size(widget, &w, &h);
221 purple_prefs_set_int(PREF_ROOT "/size/width", w);
222 purple_prefs_set_int(PREF_ROOT "/size/height", h);
225 static void
226 save_position_cb(GntWidget *w, int x, int y)
228 purple_prefs_set_int(PREF_ROOT "/position/x", x);
229 purple_prefs_set_int(PREF_ROOT "/position/y", y);
232 static PurpleIMConversation *
233 find_im_with_contact(PurpleAccount *account, const char *name)
235 PurpleBlistNode *node;
236 PurpleBuddy *buddy = purple_blist_find_buddy(account, name);
237 PurpleIMConversation *im = NULL;
239 if (!buddy)
240 return NULL;
242 for (node = purple_blist_node_get_first_child(purple_blist_node_get_parent((PurpleBlistNode*)buddy));
243 node; node = purple_blist_node_get_sibling_next(node)) {
244 if (node == (PurpleBlistNode*)buddy)
245 continue;
246 if ((im = purple_conversations_find_im_with_account(
247 purple_buddy_get_name((PurpleBuddy*)node), purple_buddy_get_account((PurpleBuddy*)node))) != NULL)
248 break;
250 return im;
253 static char *
254 get_conversation_title(PurpleConversation *conv, PurpleAccount *account)
256 PurpleE2eeState *e2ee;
258 e2ee = purple_conversation_get_e2ee_state(conv);
260 return g_strdup_printf(_("%s (%s -- %s)%s%s%s%s"),
261 purple_conversation_get_title(conv),
262 purple_account_get_username(account),
263 purple_account_get_protocol_name(account),
264 e2ee ? " | " : "",
265 e2ee ? purple_e2ee_provider_get_name(purple_e2ee_state_get_provider(e2ee)) : "",
266 e2ee ? ": " : "",
267 e2ee ? purple_e2ee_state_get_name(e2ee) : "");
270 static void
271 update_buddy_typing(PurpleAccount *account, const char *who, gpointer null)
273 FinchConv *ggc;
274 PurpleIMConversation *im;
275 PurpleConversation *conv;
276 char *title, *str;
278 im = purple_conversations_find_im_with_account(who, account);
280 if (!im)
281 return;
283 conv = PURPLE_CONVERSATION(im);
284 ggc = FINCH_CONV(conv);
286 if (purple_im_conversation_get_typing_state(im) == PURPLE_IM_TYPING) {
287 int scroll;
288 str = get_conversation_title(conv, account);
289 title = g_strdup_printf(_("%s [%s]"), str,
290 gnt_ascii_only() ? "T" : "\342\243\277");
291 g_free(str);
293 scroll = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggc->tv));
294 str = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_title(conv));
295 /* Updating is a little buggy. So just remove and add a new one */
296 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE);
297 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggc->tv),
298 str, GNT_TEXT_FLAG_DIM, "typing");
299 g_free(str);
300 if (scroll <= 1)
301 gnt_text_view_scroll(GNT_TEXT_VIEW(ggc->tv), 0);
302 } else {
303 title = get_conversation_title(conv, account);
304 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", " ", TRUE);
306 gnt_screen_rename_widget(ggc->window, title);
307 g_free(title);
310 static void
311 chat_left_cb(PurpleConversation *conv, gpointer null)
313 purple_conversation_write_system_message(conv,
314 _("You have left this chat."), 0);
317 static void
318 buddy_signed_on_off(PurpleBuddy *buddy, gpointer null)
320 PurpleIMConversation *im = find_im_with_contact(purple_buddy_get_account(buddy), purple_buddy_get_name(buddy));
321 if (im == NULL)
322 return;
323 generate_send_to_menu(FINCH_CONV(PURPLE_CONVERSATION(im)));
326 static void
327 account_signed_on_off(PurpleConnection *gc, gpointer null)
329 GList *list = purple_conversations_get_ims();
330 while (list) {
331 PurpleConversation *conv = list->data;
332 PurpleIMConversation *cc = find_im_with_contact(
333 purple_conversation_get_account(conv), purple_conversation_get_name(conv));
334 if (cc)
335 generate_send_to_menu(FINCH_CONV(PURPLE_CONVERSATION(cc)));
336 list = list->next;
339 if (PURPLE_CONNECTION_IS_CONNECTED(gc)) {
340 /* We just signed on. Let's see if there's any chat that we have open,
341 * and hadn't left before the disconnect. */
342 list = purple_conversations_get_chats();
343 while (list) {
344 PurpleConversation *conv = list->data;
345 PurpleChat *chat;
346 GHashTable *comps = NULL;
348 list = list->next;
349 if (purple_conversation_get_account(conv) != purple_connection_get_account(gc) ||
350 !g_object_get_data(G_OBJECT(conv), "want-to-rejoin"))
351 continue;
353 chat = find_chat_for_conversation(conv);
354 if (chat == NULL) {
355 PurpleProtocol *protocol = purple_connection_get_protocol(gc);
356 comps = purple_protocol_chat_iface_info_defaults(protocol, gc,
357 purple_conversation_get_name(conv));
358 } else {
359 comps = purple_chat_get_components(chat);
361 purple_serv_join_chat(gc, comps);
362 if (chat == NULL && comps != NULL)
363 g_hash_table_destroy(comps);
368 static void
369 account_signing_off(PurpleConnection *gc)
371 GList *list = purple_conversations_get_chats();
372 PurpleAccount *account = purple_connection_get_account(gc);
374 /* We are about to sign off. See which chats we are currently in, and mark
375 * them for rejoin on reconnect. */
376 while (list) {
377 PurpleConversation *conv = list->data;
378 if (!purple_chat_conversation_has_left(PURPLE_CHAT_CONVERSATION(conv)) &&
379 purple_conversation_get_account(conv) == account) {
380 g_object_set_data(G_OBJECT(conv), "want-to-rejoin", GINT_TO_POINTER(TRUE));
381 purple_conversation_write_system_message(conv,
382 _("The account has disconnected and you are no "
383 "longer in this chat. You will be automatically rejoined in the chat when "
384 "the account reconnects."),
385 PURPLE_MESSAGE_NO_LOG);
387 list = list->next;
391 static gpointer
392 finch_conv_get_handle(void)
394 static int handle;
395 return &handle;
398 static void
399 cleared_message_history_cb(PurpleConversation *conv, gpointer data)
401 FinchConv *ggc = FINCH_CONV(conv);
402 if (ggc)
403 gnt_text_view_clear(GNT_TEXT_VIEW(ggc->tv));
406 static void
407 gg_extended_menu(FinchConv *ggc)
409 GntMenu *sub;
410 GList *list;
411 gboolean is_empty = TRUE;
413 g_return_if_fail(ggc != NULL);
415 sub = GNT_MENU(gnt_menu_new(GNT_MENU_POPUP));
416 gnt_menuitem_set_submenu(ggc->plugins, sub);
418 for (list = purple_conversation_get_extended_menu(ggc->active_conv);
419 list; list = g_list_delete_link(list, list))
421 finch_append_menu_action(sub, list->data, ggc->active_conv);
422 is_empty = FALSE;
424 gnt_menuitem_set_visible(ggc->plugins, !is_empty);
427 static void
428 conv_updated(PurpleConversation *conv, PurpleConversationUpdateType type)
430 if (purple_conversation_get_ui_data(conv) == NULL)
431 return;
433 if (type == PURPLE_CONVERSATION_UPDATE_FEATURES) {
434 gg_extended_menu(purple_conversation_get_ui_data(conv));
435 return;
437 if (type == PURPLE_CONVERSATION_UPDATE_E2EE) {
438 FinchConv *ggconv = FINCH_CONV(conv);
439 gchar *title;
441 title = get_conversation_title(conv,
442 purple_conversation_get_account(conv));
443 gnt_screen_rename_widget(ggconv->window, title);
444 g_free(title);
446 generate_e2ee_menu(ggconv);
448 return;
452 static void
453 clear_scrollback_cb(GntMenuItem *item, gpointer ggconv)
455 FinchConv *ggc = ggconv;
456 purple_conversation_clear_message_history(ggc->active_conv);
459 static void
460 send_file_cb(GntMenuItem *item, gpointer ggconv)
462 FinchConv *ggc = ggconv;
463 purple_serv_send_file(purple_conversation_get_connection(ggc->active_conv),
464 purple_conversation_get_name(ggc->active_conv), NULL);
467 static void
468 add_pounce_cb(GntMenuItem *item, gpointer ggconv)
470 FinchConv *ggc = ggconv;
471 finch_pounce_editor_show(
472 purple_conversation_get_account(ggc->active_conv),
473 purple_conversation_get_name(ggc->active_conv), NULL);
476 static void
477 get_info_cb(GntMenuItem *item, gpointer ggconv)
479 FinchConv *ggc = ggconv;
480 finch_retrieve_user_info(purple_conversation_get_connection(ggc->active_conv),
481 purple_conversation_get_name(ggc->active_conv));
484 static void
485 toggle_timestamps_cb(GntMenuItem *item, gpointer ggconv)
487 purple_prefs_set_bool(PREF_ROOT "/timestamps",
488 !purple_prefs_get_bool(PREF_ROOT "/timestamps"));
491 static void
492 toggle_logging_cb(GntMenuItem *item, gpointer ggconv)
494 FinchConv *fc = ggconv;
495 PurpleConversation *conv = fc->active_conv;
496 gboolean logging = gnt_menuitem_check_get_checked(GNT_MENU_ITEM_CHECK(item));
497 GList *iter;
499 if (logging == purple_conversation_is_logging(conv))
500 return;
502 /* Xerox */
503 if (logging) {
504 /* Enable logging first so the message below can be logged. */
505 purple_conversation_set_logging(conv, TRUE);
507 purple_conversation_write_system_message(conv,
508 _("Logging started. Future messages in this "
509 "conversation will be logged."), 0);
510 } else {
511 purple_conversation_write_system_message(conv,
512 _("Logging stopped. Future messages in this "
513 "conversation will not be logged."), 0);
515 /* Disable the logging second, so that the above message can be logged. */
516 purple_conversation_set_logging(conv, FALSE);
519 /* Each conversation with the same person will have the same logging setting */
520 for (iter = fc->list; iter; iter = iter->next) {
521 if (iter->data == conv)
522 continue;
523 purple_conversation_set_logging(iter->data, logging);
527 static void
528 toggle_sound_cb(GntMenuItem *item, gpointer ggconv)
530 FinchConv *fc = ggconv;
531 PurpleBlistNode *node = get_conversation_blist_node(fc->active_conv);
532 fc->flags ^= FINCH_CONV_NO_SOUND;
533 if (node)
534 purple_blist_node_set_bool(node, "gnt-mute-sound", !!(fc->flags & FINCH_CONV_NO_SOUND));
537 static void
538 send_to_cb(GntMenuItem *m, gpointer n)
540 PurpleAccount *account = g_object_get_data(G_OBJECT(m), "purple_account");
541 gchar *buddy = g_object_get_data(G_OBJECT(m), "purple_buddy_name");
542 PurpleIMConversation *im = purple_im_conversation_new(account, buddy);
543 finch_conversation_set_active(PURPLE_CONVERSATION(im));
546 static void
547 view_log_cb(GntMenuItem *n, gpointer ggc)
549 FinchConv *fc;
550 PurpleConversation *conv;
551 PurpleLogType type;
552 const char *name;
553 PurpleAccount *account;
554 GSList *buddies;
555 GSList *cur;
557 fc = ggc;
558 conv = fc->active_conv;
560 if (PURPLE_IS_IM_CONVERSATION(conv))
561 type = PURPLE_LOG_IM;
562 else if (PURPLE_IS_CHAT_CONVERSATION(conv))
563 type = PURPLE_LOG_CHAT;
564 else
565 return;
567 name = purple_conversation_get_name(conv);
568 account = purple_conversation_get_account(conv);
570 buddies = purple_blist_find_buddies(account, name);
571 for (cur = buddies; cur != NULL; cur = cur->next) {
572 PurpleBlistNode *node = cur->data;
573 if ((node != NULL) &&
574 (purple_blist_node_get_sibling_prev(node) || purple_blist_node_get_sibling_next(node))) {
575 finch_log_show_contact((PurpleContact *)purple_blist_node_get_parent(node));
576 g_slist_free(buddies);
577 return;
580 g_slist_free(buddies);
582 finch_log_show(type, name, account);
585 static void
586 generate_send_to_menu(FinchConv *ggc)
588 GntWidget *sub, *menu = ggc->menu;
589 GntMenuItem *item;
590 GSList *buds;
591 GList *list = NULL;
593 buds = purple_blist_find_buddies(purple_conversation_get_account(ggc->active_conv),
594 purple_conversation_get_name(ggc->active_conv));
595 if (!buds)
596 return;
598 if ((item = ggc->u.im->sendto) == NULL) {
599 item = gnt_menuitem_new(_("Send To"));
600 gnt_menu_add_item(GNT_MENU(menu), item);
601 ggc->u.im->sendto = item;
603 sub = gnt_menu_new(GNT_MENU_POPUP);
604 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
606 for (; buds; buds = g_slist_delete_link(buds, buds)) {
607 PurpleBlistNode *node = PURPLE_BLIST_NODE(purple_buddy_get_contact(PURPLE_BUDDY(buds->data)));
608 for (node = purple_blist_node_get_first_child(node); node != NULL;
609 node = purple_blist_node_get_sibling_next(node)) {
610 PurpleBuddy *buddy = (PurpleBuddy *)node;
611 PurpleAccount *account = purple_buddy_get_account(buddy);
612 if (purple_account_is_connected(account)) {
613 /* Use the PurplePresence to get unique buddies. */
614 PurplePresence *presence = purple_buddy_get_presence(buddy);
615 if (!g_list_find(list, presence))
616 list = g_list_prepend(list, presence);
620 for (list = g_list_reverse(list); list != NULL; list = g_list_delete_link(list, list)) {
621 PurplePresence *pre = list->data;
622 PurpleBuddy *buddy = purple_buddy_presence_get_buddy(PURPLE_BUDDY_PRESENCE(pre));
623 PurpleAccount *account = purple_buddy_get_account(buddy);
624 gchar *name = g_strdup(purple_buddy_get_name(buddy));
625 gchar *text = g_strdup_printf("%s (%s)", purple_buddy_get_name(buddy), purple_account_get_username(account));
626 item = gnt_menuitem_new(text);
627 g_free(text);
628 gnt_menu_add_item(GNT_MENU(sub), item);
629 gnt_menuitem_set_callback(item, send_to_cb, NULL);
630 g_object_set_data(G_OBJECT(item), "purple_account", account);
631 g_object_set_data_full(G_OBJECT(item), "purple_buddy_name", name, g_free);
635 static void
636 generate_e2ee_menu(FinchConv *ggc)
638 GntMenu *sub;
639 GntWidget *menu = ggc->menu;
640 PurpleConversation *conv = ggc->active_conv;
641 GntMenuItem *item;
642 PurpleE2eeProvider *eprov;
643 GList *menu_actions, *it;
645 eprov = purple_e2ee_provider_get_main();
647 item = ggc->u.im->e2ee_menu;
648 if (item == NULL) {
649 item = gnt_menuitem_new(NULL);
650 gnt_menu_add_item(GNT_MENU(menu), item);
651 ggc->u.im->e2ee_menu = item;
653 sub = GNT_MENU(gnt_menu_new(GNT_MENU_POPUP));
654 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
656 gnt_menuitem_set_visible(item, (eprov != NULL));
657 if (eprov == NULL)
658 return;
659 gnt_menuitem_set_text(item, purple_e2ee_provider_get_name(eprov));
661 menu_actions = purple_e2ee_provider_get_conv_menu_actions(eprov, conv);
662 for (it = menu_actions; it; it = g_list_next(it)) {
663 PurpleActionMenu *action = it->data;
665 finch_append_menu_action(sub, action, conv);
667 g_list_free(menu_actions);
670 static void
671 invite_cb(GntMenuItem *item, gpointer ggconv)
673 FinchConv *fc = ggconv;
674 PurpleChatConversation *chat = PURPLE_CHAT_CONVERSATION(fc->active_conv);
675 purple_chat_conversation_invite_user(chat, NULL, NULL, TRUE);
678 static void
679 plugin_changed_cb(PurplePlugin *p, gpointer data)
681 gg_extended_menu(data);
684 static void
685 gg_create_menu(FinchConv *ggc)
687 GntWidget *menu, *sub;
688 GntMenuItem *item;
690 ggc->menu = menu = gnt_menu_new(GNT_MENU_TOPLEVEL);
691 gnt_window_set_menu(GNT_WINDOW(ggc->window), GNT_MENU(menu));
693 item = gnt_menuitem_new(_("Conversation"));
694 gnt_menu_add_item(GNT_MENU(menu), item);
696 sub = gnt_menu_new(GNT_MENU_POPUP);
697 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
699 item = gnt_menuitem_new(_("Clear Scrollback"));
700 gnt_menu_add_item(GNT_MENU(sub), item);
701 gnt_menuitem_set_callback(item, clear_scrollback_cb, ggc);
703 item = gnt_menuitem_check_new(_("Show Timestamps"));
704 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
705 purple_prefs_get_bool(PREF_ROOT "/timestamps"));
706 gnt_menu_add_item(GNT_MENU(sub), item);
707 gnt_menuitem_set_callback(item, toggle_timestamps_cb, ggc);
709 if (PURPLE_IS_IM_CONVERSATION(ggc->active_conv)) {
710 PurpleAccount *account = purple_conversation_get_account(ggc->active_conv);
711 PurpleConnection *gc = purple_account_get_connection(account);
712 PurpleProtocol *protocol =
713 gc ? purple_connection_get_protocol(gc) : NULL;
715 if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, get_info)) {
716 item = gnt_menuitem_new(_("Get Info"));
717 gnt_menu_add_item(GNT_MENU(sub), item);
718 gnt_menuitem_set_callback(item, get_info_cb, ggc);
721 item = gnt_menuitem_new(_("Add Buddy Pounce..."));
722 gnt_menu_add_item(GNT_MENU(sub), item);
723 gnt_menuitem_set_callback(item, add_pounce_cb, ggc);
725 if (PURPLE_IS_PROTOCOL_XFER(protocol) &&
726 purple_protocol_xfer_can_receive(
727 PURPLE_PROTOCOL_XFER(protocol),
729 purple_conversation_get_name(ggc->active_conv)
732 item = gnt_menuitem_new(_("Send File"));
733 gnt_menu_add_item(GNT_MENU(sub), item);
734 gnt_menuitem_set_callback(item, send_file_cb, ggc);
737 generate_send_to_menu(ggc);
738 } else if (PURPLE_IS_CHAT_CONVERSATION(ggc->active_conv)) {
739 item = gnt_menuitem_new(_("Invite..."));
740 gnt_menu_add_item(GNT_MENU(sub), item);
741 gnt_menuitem_set_callback(item, invite_cb, ggc);
744 generate_e2ee_menu(ggc);
746 item = gnt_menuitem_new(_("View Log..."));
747 gnt_menu_add_item(GNT_MENU(sub), item);
748 gnt_menuitem_set_callback(item, view_log_cb, ggc);
750 item = gnt_menuitem_check_new(_("Enable Logging"));
751 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
752 purple_conversation_is_logging(ggc->active_conv));
753 gnt_menu_add_item(GNT_MENU(sub), item);
754 gnt_menuitem_set_callback(item, toggle_logging_cb, ggc);
756 item = gnt_menuitem_check_new(_("Enable Sounds"));
757 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
758 !(ggc->flags & FINCH_CONV_NO_SOUND));
759 gnt_menu_add_item(GNT_MENU(sub), item);
760 gnt_menuitem_set_callback(item, toggle_sound_cb, ggc);
762 item = gnt_menuitem_new(_("Plugins"));
763 gnt_menu_add_item(GNT_MENU(menu), item);
764 ggc->plugins = item;
766 gg_extended_menu(ggc);
769 static void
770 create_conv_from_userlist(GntWidget *widget, FinchConv *fc)
772 PurpleAccount *account = purple_conversation_get_account(fc->active_conv);
773 PurpleConnection *gc = purple_account_get_connection(account);
774 PurpleProtocol *protocol = NULL;
775 char *name, *realname = NULL;
777 if (!gc) {
778 purple_conversation_write_system_message(fc->active_conv,
779 _("You are not connected."), 0);
780 return;
783 name = gnt_tree_get_selection_data(GNT_TREE(widget));
785 protocol = purple_connection_get_protocol(gc);
786 if (protocol)
787 realname = purple_protocol_chat_iface_get_user_real_name(protocol, gc,
788 purple_chat_conversation_get_id(
789 PURPLE_CHAT_CONVERSATION(fc->active_conv)), name);
791 purple_im_conversation_new(account, realname ? realname : name);
792 g_free(realname);
795 static void
796 gained_focus_cb(GntWindow *window, FinchConv *fc)
798 GList *iter;
799 for (iter = fc->list; iter; iter = iter->next) {
800 g_object_set_data(G_OBJECT(iter->data), "unseen-count", 0);
801 purple_conversation_update(iter->data, PURPLE_CONVERSATION_UPDATE_UNSEEN);
805 static void
806 completion_cb(GntEntry *entry, const char *start, G_GNUC_UNUSED const char *end)
808 if (start == gnt_entry_get_text(entry) && *start != '/')
809 gnt_widget_key_pressed(GNT_WIDGET(entry), ": ");
812 static void
813 gg_setup_commands(FinchConv *fconv, gboolean remove_first)
815 GList *commands;
816 char command[256] = "/";
818 if (remove_first) {
819 commands = purple_cmd_list(NULL);
820 for (; commands; commands = g_list_delete_link(commands, commands)) {
821 g_strlcpy(command + 1, commands->data, sizeof(command) - 1);
822 gnt_entry_remove_suggest(GNT_ENTRY(fconv->entry), command);
826 commands = purple_cmd_list(fconv->active_conv);
827 for (; commands; commands = g_list_delete_link(commands, commands)) {
828 g_strlcpy(command + 1, commands->data, sizeof(command) - 1);
829 gnt_entry_add_suggest(GNT_ENTRY(fconv->entry), command);
833 static void
834 cmd_added_cb(const char *cmd, PurpleCmdPriority prior, PurpleCmdFlag flags,
835 FinchConv *fconv)
837 gg_setup_commands(fconv, TRUE);
840 static void
841 cmd_removed_cb(const char *cmd, FinchConv *fconv)
843 char command[256] = "/";
844 g_strlcpy(command + 1, cmd, sizeof(command) - 1);
845 gnt_entry_remove_suggest(GNT_ENTRY(fconv->entry), command);
846 gg_setup_commands(fconv, TRUE);
849 static void
850 finch_create_conversation(PurpleConversation *conv)
852 FinchConv *ggc = FINCH_CONV(conv);
853 char *title;
854 PurpleConversation *cc;
855 PurpleAccount *account;
856 PurpleBlistNode *convnode = NULL;
858 if (ggc) {
859 gnt_window_present(ggc->window);
860 return;
863 account = purple_conversation_get_account(conv);
864 cc = PURPLE_CONVERSATION(find_im_with_contact(account, purple_conversation_get_name(conv)));
865 if (cc && FINCH_CONV(cc))
866 ggc = FINCH_CONV(cc);
867 else
868 ggc = g_new0(FinchConv, 1);
870 /* Each conversation with the same person will have the same logging setting */
871 if (ggc->list) {
872 purple_conversation_set_logging(conv,
873 purple_conversation_is_logging(ggc->list->data));
876 ggc->list = g_list_prepend(ggc->list, conv);
877 ggc->active_conv = conv;
878 purple_conversation_set_ui_data(conv, ggc);
880 if (cc && FINCH_CONV(cc) && cc != conv) {
881 finch_conversation_set_active(conv);
882 return;
885 title = get_conversation_title(conv, account);
887 ggc->window = gnt_vwindow_new(FALSE);
888 gnt_box_set_title(GNT_BOX(ggc->window), title);
889 gnt_box_set_toplevel(GNT_BOX(ggc->window), TRUE);
890 gnt_box_set_pad(GNT_BOX(ggc->window), 0);
892 if (PURPLE_IS_IM_CONVERSATION(conv))
893 gnt_widget_set_name(ggc->window, "conversation-window-im");
894 else if (PURPLE_IS_CHAT_CONVERSATION(conv))
895 gnt_widget_set_name(ggc->window, "conversation-window-chat");
896 else
897 gnt_widget_set_name(ggc->window, "conversation-window-other");
899 ggc->tv = gnt_text_view_new();
900 gnt_widget_set_name(ggc->tv, "conversation-window-textview");
901 gnt_widget_set_size(ggc->tv, purple_prefs_get_int(PREF_ROOT "/size/width"),
902 purple_prefs_get_int(PREF_ROOT "/size/height"));
904 if (PURPLE_IS_CHAT_CONVERSATION(conv)) {
905 GntWidget *hbox, *tree;
906 FinchConvChat *fc = ggc->u.chat = g_new0(FinchConvChat, 1);
907 hbox = gnt_hbox_new(FALSE);
908 gnt_box_set_pad(GNT_BOX(hbox), 0);
909 tree = fc->userlist = gnt_tree_new_with_columns(2);
910 gnt_tree_set_col_width(GNT_TREE(tree), 0, 1); /* The flag column */
911 gnt_tree_set_compare_func(GNT_TREE(tree), (GCompareFunc)g_utf8_collate);
912 gnt_tree_set_hash_fns(GNT_TREE(tree), g_str_hash, g_str_equal, g_free);
913 gnt_tree_set_search_column(GNT_TREE(tree), 1);
914 gnt_widget_set_has_border(tree, FALSE);
915 gnt_box_add_widget(GNT_BOX(hbox), ggc->tv);
916 gnt_box_add_widget(GNT_BOX(hbox), tree);
917 gnt_box_add_widget(GNT_BOX(ggc->window), hbox);
918 g_signal_connect(G_OBJECT(tree), "activate", G_CALLBACK(create_conv_from_userlist), ggc);
919 gnt_widget_set_visible(tree, purple_prefs_get_bool(PREF_USERLIST));
920 } else {
921 ggc->u.im = g_new0(FinchConvIm, 1);
922 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->tv);
925 ggc->info = gnt_vbox_new(FALSE);
926 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->info);
928 ggc->entry = gnt_entry_new(NULL);
929 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->entry);
930 gnt_widget_set_name(ggc->entry, "conversation-window-entry");
931 gnt_entry_set_history_length(GNT_ENTRY(ggc->entry), -1);
932 gnt_entry_set_word_suggest(GNT_ENTRY(ggc->entry), TRUE);
933 gnt_entry_set_always_suggest(GNT_ENTRY(ggc->entry), FALSE);
935 gnt_text_view_attach_scroll_widget(GNT_TEXT_VIEW(ggc->tv), ggc->entry);
936 gnt_text_view_attach_pager_widget(GNT_TEXT_VIEW(ggc->tv), ggc->entry);
938 g_signal_connect_after(G_OBJECT(ggc->entry), "activate", G_CALLBACK(entry_key_pressed), ggc);
939 g_signal_connect(G_OBJECT(ggc->entry), "completion", G_CALLBACK(completion_cb), NULL);
940 g_signal_connect(G_OBJECT(ggc->window), "destroy", G_CALLBACK(closing_window), ggc);
942 gnt_widget_set_position(ggc->window, purple_prefs_get_int(PREF_ROOT "/position/x"),
943 purple_prefs_get_int(PREF_ROOT "/position/y"));
944 gnt_widget_show(ggc->window);
946 g_signal_connect(G_OBJECT(ggc->tv), "size_changed", G_CALLBACK(size_changed_cb), NULL);
947 g_signal_connect(G_OBJECT(ggc->window), "position_set", G_CALLBACK(save_position_cb), NULL);
949 if (PURPLE_IS_IM_CONVERSATION(conv))
950 g_signal_connect(G_OBJECT(ggc->entry), "text_changed", G_CALLBACK(send_typing_notification), ggc);
952 convnode = get_conversation_blist_node(conv);
953 if ((convnode && purple_blist_node_get_bool(convnode, "gnt-mute-sound")) ||
954 !finch_sound_is_enabled())
955 ggc->flags |= FINCH_CONV_NO_SOUND;
957 gg_create_menu(ggc);
958 gg_setup_commands(ggc, FALSE);
960 purple_signal_connect(purple_cmds_get_handle(), "cmd-added", ggc,
961 G_CALLBACK(cmd_added_cb), ggc);
962 purple_signal_connect(purple_cmds_get_handle(), "cmd-removed", ggc,
963 G_CALLBACK(cmd_removed_cb), ggc);
965 purple_signal_connect(purple_plugins_get_handle(), "plugin-load", ggc,
966 PURPLE_CALLBACK(plugin_changed_cb), ggc);
967 purple_signal_connect(purple_plugins_get_handle(), "plugin-unload", ggc,
968 PURPLE_CALLBACK(plugin_changed_cb), ggc);
970 g_free(title);
971 gnt_box_give_focus_to_child(GNT_BOX(ggc->window), ggc->entry);
972 g_signal_connect(G_OBJECT(ggc->window), "gained-focus", G_CALLBACK(gained_focus_cb), ggc);
975 static void
976 finch_destroy_conversation(PurpleConversation *conv)
978 /* do stuff here */
979 FinchConv *ggc = FINCH_CONV(conv);
980 ggc->list = g_list_remove(ggc->list, conv);
981 if (ggc->list && conv == ggc->active_conv)
982 finch_conversation_set_active(ggc->list->data);
984 if (ggc->list == NULL) {
985 g_free(ggc->u.chat);
986 purple_signals_disconnect_by_handle(ggc);
987 if (ggc->window)
988 gnt_widget_destroy(ggc->window);
989 g_free(ggc);
993 static void
994 finch_write_conv(PurpleConversation *conv, PurpleMessage *msg)
996 FinchConv *ggconv = FINCH_CONV(conv);
997 char *strip, *newline;
998 GntTextFormatFlags fl = 0;
999 int pos;
1000 PurpleMessageFlags flags = purple_message_get_flags(msg);
1002 g_return_if_fail(ggconv != NULL);
1004 if ((flags & PURPLE_MESSAGE_SYSTEM) && !(flags & PURPLE_MESSAGE_NOTIFY)) {
1005 flags &= ~(PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV);
1008 if (ggconv->active_conv != conv) {
1009 if (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV))
1010 finch_conversation_set_active(conv);
1011 else
1012 return;
1015 pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggconv->tv));
1017 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggconv->tv), "typing", NULL, TRUE);
1018 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), "\n", GNT_TEXT_FLAG_NORMAL);
1020 /* Unnecessary to print the timestamp for delayed message */
1021 if (purple_prefs_get_bool("/finch/conversations/timestamps")) {
1022 time_t mtime = purple_message_get_time(msg);
1023 if (!mtime)
1024 time(&mtime);
1025 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
1026 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)), gnt_color_pair(color_timestamp));
1029 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), " ", GNT_TEXT_FLAG_NORMAL);
1031 if (flags & PURPLE_MESSAGE_AUTO_RESP)
1032 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
1033 _("<AUTO-REPLY> "), GNT_TEXT_FLAG_BOLD);
1035 if (purple_message_get_author(msg) && (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV)) &&
1036 !(flags & PURPLE_MESSAGE_NOTIFY))
1038 char * name = NULL;
1039 GntTextFormatFlags msgflags = GNT_TEXT_FLAG_NORMAL;
1040 gboolean me = FALSE;
1041 gchar *msg_text = g_strdup(purple_message_get_contents(msg));
1043 if (purple_message_meify(msg_text, -1)) {
1044 name = g_strdup_printf("*** %s", purple_message_get_author_alias(msg));
1045 if (!(flags & PURPLE_MESSAGE_SEND) &&
1046 (flags & PURPLE_MESSAGE_NICK))
1047 msgflags = gnt_color_pair(color_message_highlight);
1048 else
1049 msgflags = gnt_color_pair(color_message_action);
1050 me = TRUE;
1051 } else {
1052 name = g_strdup_printf("%s", purple_message_get_author_alias(msg));
1053 if (flags & PURPLE_MESSAGE_SEND)
1054 msgflags = gnt_color_pair(color_message_send);
1055 else if (flags & PURPLE_MESSAGE_NICK)
1056 msgflags = gnt_color_pair(color_message_highlight);
1057 else
1058 msgflags = gnt_color_pair(color_message_receive);
1060 purple_message_set_contents(msg, msg_text); /* might be "meified" */
1061 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
1062 name, msgflags);
1063 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), me ? " " : ": ", GNT_TEXT_FLAG_NORMAL);
1064 g_free(name);
1065 g_free(msg_text);
1066 } else
1067 fl = GNT_TEXT_FLAG_DIM;
1069 if (flags & PURPLE_MESSAGE_ERROR)
1070 fl |= GNT_TEXT_FLAG_BOLD;
1072 /* XXX: Remove this workaround when textview can parse messages. */
1073 newline = purple_strdup_withhtml(purple_message_get_contents(msg));
1074 strip = purple_markup_strip_html(newline);
1075 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
1076 strip, fl);
1078 g_free(newline);
1079 g_free(strip);
1081 if (PURPLE_IS_IM_CONVERSATION(conv) && purple_im_conversation_get_typing_state(
1082 PURPLE_IM_CONVERSATION(conv)) == PURPLE_IM_TYPING) {
1083 strip = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_title(conv));
1084 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggconv->tv),
1085 strip, GNT_TEXT_FLAG_DIM, "typing");
1086 g_free(strip);
1089 if (pos <= 1)
1090 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 0);
1092 if (flags & (PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_NICK | PURPLE_MESSAGE_ERROR))
1093 gnt_widget_set_urgent(ggconv->tv);
1094 if (flags & PURPLE_MESSAGE_RECV && !gnt_widget_has_focus(ggconv->window)) {
1095 int count = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(conv), "unseen-count"));
1096 g_object_set_data(G_OBJECT(conv), "unseen-count", GINT_TO_POINTER(count + 1));
1097 purple_conversation_update(conv, PURPLE_CONVERSATION_UPDATE_UNSEEN);
1101 static const char *
1102 chat_flag_text(PurpleChatUserFlags flags)
1104 if (flags & PURPLE_CHAT_USER_FOUNDER)
1105 return "~";
1106 if (flags & PURPLE_CHAT_USER_OP)
1107 return "@";
1108 if (flags & PURPLE_CHAT_USER_HALFOP)
1109 return "%";
1110 if (flags & PURPLE_CHAT_USER_VOICE)
1111 return "+";
1112 return " ";
1115 static void
1116 finch_chat_add_users(PurpleChatConversation *chat, GList *users, gboolean new_arrivals)
1118 PurpleConversation *conv = PURPLE_CONVERSATION(chat);
1119 FinchConv *ggc = FINCH_CONV(conv);
1120 GntEntry *entry = GNT_ENTRY(ggc->entry);
1122 if (!new_arrivals)
1124 /* Print the list of users in the room */
1125 GString *string = g_string_new(NULL);
1126 GList *iter;
1127 int count = g_list_length(users);
1129 g_string_printf(string,
1130 ngettext("List of %d user:\n", "List of %d users:\n", count), count);
1131 for (iter = users; iter; iter = iter->next)
1133 PurpleChatUser *chatuser = iter->data;
1134 const char *str;
1136 if ((str = purple_chat_user_get_alias(chatuser)) == NULL)
1137 str = purple_chat_user_get_name(chatuser);
1138 g_string_append_printf(string, "[ %s ]", str);
1141 purple_conversation_write_system_message(
1142 conv, string->str, 0);
1143 g_string_free(string, TRUE);
1146 for (; users; users = users->next)
1148 PurpleChatUser *chatuser = users->data;
1149 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1150 gnt_entry_add_suggest(entry, purple_chat_user_get_name(chatuser));
1151 gnt_entry_add_suggest(entry, purple_chat_user_get_alias(chatuser));
1152 gnt_tree_add_row_after(tree, g_strdup(purple_chat_user_get_name(chatuser)),
1153 gnt_tree_create_row(tree, chat_flag_text(purple_chat_user_get_flags(chatuser)), purple_chat_user_get_alias(chatuser)), NULL, NULL);
1157 static void
1158 finch_chat_rename_user(PurpleChatConversation *chat, const char *old, const char *new_n, const char *new_a)
1160 /* Update the name for string completion */
1161 FinchConv *ggc = FINCH_CONV(PURPLE_CONVERSATION(chat));
1162 GntEntry *entry = GNT_ENTRY(ggc->entry);
1163 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1164 PurpleChatUser *cb = purple_chat_conversation_find_user(chat, new_n);
1166 gnt_entry_remove_suggest(entry, old);
1167 gnt_tree_remove(tree, (gpointer)old);
1169 gnt_entry_add_suggest(entry, new_n);
1170 gnt_entry_add_suggest(entry, new_a);
1171 gnt_tree_add_row_after(tree, g_strdup(new_n),
1172 gnt_tree_create_row(tree, chat_flag_text(purple_chat_user_get_flags(cb)), new_a), NULL, NULL);
1175 static void
1176 finch_chat_remove_users(PurpleChatConversation *chat, GList *list)
1178 /* Remove the name from string completion */
1179 FinchConv *ggc = FINCH_CONV(PURPLE_CONVERSATION(chat));
1180 GntEntry *entry = GNT_ENTRY(ggc->entry);
1181 for (; list; list = list->next) {
1182 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1183 gnt_entry_remove_suggest(entry, list->data);
1184 gnt_tree_remove(tree, list->data);
1188 static void
1189 finch_chat_update_user(PurpleChatUser *cb)
1191 PurpleChatConversation *chat;
1192 FinchConv *ggc;
1193 if (!cb)
1194 return;
1196 chat = purple_chat_user_get_chat(cb);
1197 ggc = FINCH_CONV(PURPLE_CONVERSATION(chat));
1198 gnt_tree_change_text(GNT_TREE(ggc->u.chat->userlist),
1199 (gpointer)purple_chat_user_get_name(cb), 0,
1200 chat_flag_text(purple_chat_user_get_flags(cb)));
1203 static void
1204 finch_conv_present(PurpleConversation *conv)
1206 FinchConv *fc = FINCH_CONV(conv);
1207 if (fc && fc->window)
1208 gnt_window_present(fc->window);
1211 static gboolean
1212 finch_conv_has_focus(PurpleConversation *conv)
1214 FinchConv *fc = FINCH_CONV(conv);
1215 if (fc && fc->window)
1216 return gnt_widget_has_focus(fc->window);
1217 return FALSE;
1220 static PurpleConversationUiOps conv_ui_ops =
1222 finch_create_conversation,
1223 finch_destroy_conversation,
1224 NULL, /* write_chat */
1225 NULL, /* write_im */
1226 finch_write_conv,
1227 finch_chat_add_users,
1228 finch_chat_rename_user,
1229 finch_chat_remove_users,
1230 finch_chat_update_user,
1231 finch_conv_present, /* present */
1232 finch_conv_has_focus, /* has_focus */
1233 NULL, /* send_confirm */
1234 NULL,
1235 NULL,
1236 NULL,
1237 NULL
1240 PurpleConversationUiOps *finch_conv_get_ui_ops()
1242 return &conv_ui_ops;
1245 /* Xerox */
1246 static PurpleCmdRet
1247 say_command_cb(PurpleConversation *conv,
1248 const char *cmd, char **args, char **error, void *data)
1250 purple_conversation_send(conv, args[0]);
1252 return PURPLE_CMD_RET_OK;
1255 /* Xerox */
1256 static PurpleCmdRet
1257 me_command_cb(PurpleConversation *conv,
1258 const char *cmd, char **args, char **error, void *data)
1260 char *tmp;
1262 tmp = g_strdup_printf("/me %s", args[0]);
1263 purple_conversation_send(conv, tmp);
1265 g_free(tmp);
1266 return PURPLE_CMD_RET_OK;
1269 /* Xerox */
1270 static PurpleCmdRet
1271 debug_command_cb(PurpleConversation *conv,
1272 const char *cmd, char **args, char **error, void *data)
1274 char *tmp, *markup;
1276 if (!g_ascii_strcasecmp(args[0], "version")) {
1277 tmp = g_strdup_printf("Using Finch v%s with libpurple v%s.",
1278 DISPLAY_VERSION, purple_core_get_version());
1279 } else if (!g_ascii_strcasecmp(args[0], "plugins")) {
1280 /* Show all the loaded plugins, including plugins marked internal.
1281 * This is intentional, since third party protocols are often sources of bugs, and some
1282 * loaders can also be buggy.
1284 GString *str = g_string_new("Loaded Plugins: ");
1285 const GList *plugins = purple_plugins_get_loaded();
1286 if (plugins) {
1287 for (; plugins; plugins = plugins->next) {
1288 GPluginPluginInfo *plugin_info =
1289 GPLUGIN_PLUGIN_INFO(
1290 purple_plugin_get_info(
1291 plugins->data));
1292 str = g_string_append(
1293 str, gplugin_plugin_info_get_name(
1294 plugin_info));
1296 if (plugins->next)
1297 str = g_string_append(str, ", ");
1299 } else {
1300 str = g_string_append(str, "(none)");
1303 tmp = g_string_free(str, FALSE);
1304 } else {
1305 purple_conversation_write_system_message(conv,
1306 _("Supported debug options are: plugins version"),
1307 PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_ERROR);
1308 return PURPLE_CMD_RET_OK;
1311 markup = g_markup_escape_text(tmp, -1);
1312 purple_conversation_send(conv, markup);
1314 g_free(tmp);
1315 g_free(markup);
1316 return PURPLE_CMD_RET_OK;
1319 /* Xerox */
1320 static PurpleCmdRet
1321 clear_command_cb(PurpleConversation *conv,
1322 const char *cmd, char **args, char **error, void *data)
1324 purple_conversation_clear_message_history(conv);
1325 return PURPLE_CMD_RET_OK;
1328 /* Xerox */
1329 static PurpleCmdRet
1330 help_command_cb(PurpleConversation *conv,
1331 const char *cmd, char **args, char **error, void *data)
1333 GList *l, *text;
1334 GString *s;
1336 if (args[0] != NULL) {
1337 s = g_string_new("");
1338 text = purple_cmd_help(conv, args[0]);
1340 if (text) {
1341 for (l = text; l; l = l->next)
1342 if (l->next)
1343 g_string_append_printf(s, "%s\n", (char *)l->data);
1344 else
1345 g_string_append_printf(s, "%s", (char *)l->data);
1346 } else {
1347 g_string_append(s, _("No such command (in this context)."));
1349 } else {
1350 s = g_string_new(_("Use \"/help &lt;command&gt;\" for help with a "
1351 "specific command.\nThe following commands are available in "
1352 "this context:\n"));
1354 text = purple_cmd_list(conv);
1355 for (l = text; l; l = l->next)
1356 if (l->next)
1357 g_string_append_printf(s, "%s, ", (char *)l->data);
1358 else
1359 g_string_append_printf(s, "%s.", (char *)l->data);
1360 g_list_free(text);
1363 purple_conversation_write_system_message(conv, s->str, PURPLE_MESSAGE_NO_LOG);
1364 g_string_free(s, TRUE);
1366 return PURPLE_CMD_RET_OK;
1369 static PurpleCmdRet
1370 cmd_show_window(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1372 void (*callback)(void) = data;
1373 callback();
1374 return PURPLE_CMD_RET_OK;
1377 static PurpleCmdRet
1378 cmd_message_color(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1380 int *msgclass = NULL;
1381 int fg, bg;
1383 if (purple_strequal(args[0], "receive"))
1384 msgclass = &color_message_receive;
1385 else if (purple_strequal(args[0], "send"))
1386 msgclass = &color_message_send;
1387 else if (purple_strequal(args[0], "highlight"))
1388 msgclass = &color_message_highlight;
1389 else if (purple_strequal(args[0], "action"))
1390 msgclass = &color_message_action;
1391 else if (purple_strequal(args[0], "timestamp"))
1392 msgclass = &color_timestamp;
1393 else {
1394 if (error)
1395 *error = g_strdup_printf(_("%s is not a valid message class. See '/help msgcolor' for valid message classes."), args[0]);
1396 return PURPLE_CMD_RET_FAILED;
1399 fg = gnt_colors_get_color(args[1]);
1400 if (fg == -EINVAL) {
1401 if (error)
1402 *error = g_strdup_printf(_("%s is not a valid color. See '/help msgcolor' for valid colors."), args[1]);
1403 return PURPLE_CMD_RET_FAILED;
1406 bg = gnt_colors_get_color(args[2]);
1407 if (bg == -EINVAL) {
1408 if (error)
1409 *error = g_strdup_printf(_("%s is not a valid color. See '/help msgcolor' for valid colors."), args[2]);
1410 return PURPLE_CMD_RET_FAILED;
1413 init_pair(*msgclass, fg, bg);
1415 return PURPLE_CMD_RET_OK;
1418 static PurpleCmdRet
1419 users_command_cb(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1421 FinchConv *fc = FINCH_CONV(conv);
1422 FinchConvChat *ch;
1423 if (!fc)
1424 return PURPLE_CMD_RET_FAILED;
1426 ch = fc->u.chat;
1427 gnt_widget_set_visible(ch->userlist,
1428 !gnt_widget_get_visible(ch->userlist));
1429 gnt_box_readjust(GNT_BOX(fc->window));
1430 gnt_box_give_focus_to_child(GNT_BOX(fc->window), fc->entry);
1431 purple_prefs_set_bool(PREF_USERLIST, gnt_widget_get_visible(ch->userlist));
1432 return PURPLE_CMD_RET_OK;
1435 void finch_conversation_init()
1437 color_message_send = gnt_style_get_color(NULL, "color-message-sent");
1438 if (!color_message_send)
1439 color_message_send = gnt_color_add_pair(COLOR_CYAN, -1);
1440 color_message_receive = gnt_style_get_color(NULL, "color-message-received");
1441 if (!color_message_receive)
1442 color_message_receive = gnt_color_add_pair(COLOR_RED, -1);
1443 color_message_highlight = gnt_style_get_color(NULL, "color-message-highlight");
1444 if (!color_message_highlight)
1445 color_message_highlight = gnt_color_add_pair(COLOR_GREEN, -1);
1446 color_timestamp = gnt_style_get_color(NULL, "color-timestamp");
1447 if (!color_timestamp)
1448 color_timestamp = gnt_color_add_pair(COLOR_BLUE, -1);
1449 color_message_action = gnt_style_get_color(NULL, "color-message-action");
1450 if (!color_message_action)
1451 color_message_action = gnt_color_add_pair(COLOR_YELLOW, -1);
1452 purple_prefs_add_none(PREF_ROOT);
1453 purple_prefs_add_none(PREF_ROOT "/size");
1454 purple_prefs_add_int(PREF_ROOT "/size/width", 70);
1455 purple_prefs_add_int(PREF_ROOT "/size/height", 20);
1456 purple_prefs_add_none(PREF_ROOT "/position");
1457 purple_prefs_add_int(PREF_ROOT "/position/x", 0);
1458 purple_prefs_add_int(PREF_ROOT "/position/y", 0);
1459 purple_prefs_add_none(PREF_CHAT);
1460 purple_prefs_add_bool(PREF_USERLIST, FALSE);
1462 /* Xerox the commands */
1463 purple_cmd_register("say", "S", PURPLE_CMD_P_DEFAULT,
1464 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1465 say_command_cb, _("say &lt;message&gt;: Send a message normally as if you weren't using a command."), NULL);
1466 purple_cmd_register("me", "S", PURPLE_CMD_P_DEFAULT,
1467 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1468 me_command_cb, _("me &lt;action&gt;: Send an IRC style action to a buddy or chat."), NULL);
1469 purple_cmd_register("debug", "w", PURPLE_CMD_P_DEFAULT,
1470 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1471 debug_command_cb, _("debug &lt;option&gt;: Send various debug information to the current conversation."), NULL);
1472 purple_cmd_register("clear", "", PURPLE_CMD_P_DEFAULT,
1473 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1474 clear_command_cb, _("clear: Clears the conversation scrollback."), NULL);
1475 purple_cmd_register("help", "w", PURPLE_CMD_P_DEFAULT,
1476 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL,
1477 help_command_cb, _("help &lt;command&gt;: Help on a specific command."), NULL);
1478 purple_cmd_register("users", "", PURPLE_CMD_P_DEFAULT,
1479 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL,
1480 users_command_cb, _("users: Show the list of users in the chat."), NULL);
1482 /* Now some commands to bring up some other windows */
1483 purple_cmd_register("plugins", "", PURPLE_CMD_P_DEFAULT,
1484 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1485 cmd_show_window, _("plugins: Show the plugins window."), finch_plugins_show_all);
1486 purple_cmd_register("buddylist", "", PURPLE_CMD_P_DEFAULT,
1487 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1488 cmd_show_window, _("buddylist: Show the buddylist."), finch_blist_show);
1489 purple_cmd_register("accounts", "", PURPLE_CMD_P_DEFAULT,
1490 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1491 cmd_show_window, _("accounts: Show the accounts window."), finch_accounts_show_all);
1492 purple_cmd_register("debugwin", "", PURPLE_CMD_P_DEFAULT,
1493 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1494 cmd_show_window, _("debugwin: Show the debug window."), finch_debug_window_show);
1495 purple_cmd_register("prefs", "", PURPLE_CMD_P_DEFAULT,
1496 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1497 cmd_show_window, _("prefs: Show the preference window."), finch_prefs_show_all);
1498 purple_cmd_register("status", "", PURPLE_CMD_P_DEFAULT,
1499 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1500 cmd_show_window, _("statuses: Show the savedstatuses window."), finch_savedstatus_show_all);
1502 /* Allow customizing the message colors using a command during run-time */
1503 purple_cmd_register("msgcolor", "www", PURPLE_CMD_P_DEFAULT,
1504 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1505 cmd_message_color, _("msgcolor &lt;class&gt; &lt;foreground&gt; &lt;background&gt;: "
1506 "Set the color for different classes of messages in the conversation window.<br>"
1507 " &lt;class&gt;: receive, send, highlight, action, timestamp<br>"
1508 " &lt;foreground/background&gt;: black, red, green, blue, white, gray, darkgray, magenta, cyan, default<br><br>"
1509 "EXAMPLE:<br> msgcolor send cyan default"),
1510 NULL);
1511 purple_cmd_register("msgcolour", "www", PURPLE_CMD_P_DEFAULT,
1512 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1513 cmd_message_color, _("msgcolor &lt;class&gt; &lt;foreground&gt; &lt;background&gt;: "
1514 "Set the color for different classes of messages in the conversation window.<br>"
1515 " &lt;class&gt;: receive, send, highlight, action, timestamp<br>"
1516 " &lt;foreground/background&gt;: black, red, green, blue, white, gray, darkgray, magenta, cyan, default<br><br>"
1517 "EXAMPLE:<br> msgcolor send cyan default"),
1518 NULL);
1520 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", finch_conv_get_handle(),
1521 PURPLE_CALLBACK(update_buddy_typing), NULL);
1522 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", finch_conv_get_handle(),
1523 PURPLE_CALLBACK(update_buddy_typing), NULL);
1524 purple_signal_connect(purple_conversations_get_handle(), "chat-left", finch_conv_get_handle(),
1525 PURPLE_CALLBACK(chat_left_cb), NULL);
1526 purple_signal_connect(purple_conversations_get_handle(), "cleared-message-history", finch_conv_get_handle(),
1527 PURPLE_CALLBACK(cleared_message_history_cb), NULL);
1528 purple_signal_connect(purple_conversations_get_handle(), "conversation-updated", finch_conv_get_handle(),
1529 PURPLE_CALLBACK(conv_updated), NULL);
1530 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", finch_conv_get_handle(),
1531 PURPLE_CALLBACK(buddy_signed_on_off), NULL);
1532 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", finch_conv_get_handle(),
1533 PURPLE_CALLBACK(buddy_signed_on_off), NULL);
1534 purple_signal_connect(purple_connections_get_handle(), "signed-on", finch_conv_get_handle(),
1535 PURPLE_CALLBACK(account_signed_on_off), NULL);
1536 purple_signal_connect(purple_connections_get_handle(), "signed-off", finch_conv_get_handle(),
1537 PURPLE_CALLBACK(account_signed_on_off), NULL);
1538 purple_signal_connect(purple_connections_get_handle(), "signing-off", finch_conv_get_handle(),
1539 PURPLE_CALLBACK(account_signing_off), NULL);
1542 void finch_conversation_uninit()
1544 purple_signals_disconnect_by_handle(finch_conv_get_handle());
1547 void finch_conversation_set_active(PurpleConversation *conv)
1549 FinchConv *ggconv = FINCH_CONV(conv);
1550 PurpleAccount *account;
1551 char *title;
1553 g_return_if_fail(ggconv);
1554 g_return_if_fail(g_list_find(ggconv->list, conv));
1555 if (ggconv->active_conv == conv)
1556 return;
1558 ggconv->active_conv = conv;
1559 gg_setup_commands(ggconv, TRUE);
1560 account = purple_conversation_get_account(conv);
1561 title = get_conversation_title(conv, account);
1562 gnt_screen_rename_widget(ggconv->window, title);
1563 g_free(title);
1565 generate_e2ee_menu(ggconv);
1568 void finch_conversation_set_info_widget(PurpleConversation *conv, GntWidget *widget)
1570 FinchConv *fc = FINCH_CONV(conv);
1571 int height, width;
1573 gnt_box_remove_all(GNT_BOX(fc->info));
1575 if (widget) {
1576 gnt_box_add_widget(GNT_BOX(fc->info), widget);
1577 gnt_box_readjust(GNT_BOX(fc->info));
1580 gnt_widget_get_size(fc->window, &width, &height);
1581 gnt_box_readjust(GNT_BOX(fc->window));
1582 gnt_screen_resize_widget(fc->window, width, height);
1583 gnt_box_give_focus_to_child(GNT_BOX(fc->window), fc->entry);