mark PurpleImageClass as private
[pidgin-git.git] / finch / gntconv.c
blob3912022e9b2bcd4c738a85b8a028d20f07dbcc25
1 /* finch
3 * Finch is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include <internal.h>
23 #include "finch.h"
25 #include NCURSES_HEADER
27 #include <cmds.h>
28 #include <core.h>
29 #include <idle.h>
30 #include <prefs.h>
31 #include <util.h>
33 #include "gntaccount.h"
34 #include "gntblist.h"
35 #include "gntconv.h"
36 #include "gntdebug.h"
37 #include "gntlog.h"
38 #include "gntplugin.h"
39 #include "gntpounce.h"
40 #include "gntprefs.h"
41 #include "gntrequest.h"
42 #include "gntsound.h"
43 #include "gntstatus.h"
45 #include "gnt.h"
46 #include "gntbox.h"
47 #include "gntentry.h"
48 #include "gntlabel.h"
49 #include "gntmenu.h"
50 #include "gntmenuitem.h"
51 #include "gntmenuitemcheck.h"
52 #include "gntmenuutil.h"
53 #include "gntstyle.h"
54 #include "gnttextview.h"
55 #include "gnttree.h"
56 #include "gntutils.h"
57 #include "gntwindow.h"
59 #define PREF_ROOT "/finch/conversations"
60 #define PREF_CHAT PREF_ROOT "/chats"
61 #define PREF_USERLIST PREF_CHAT "/userlist"
63 #include "config.h"
65 static void generate_send_to_menu(FinchConv *ggc);
66 static void generate_e2ee_menu(FinchConv *ggc);
68 static int color_message_receive;
69 static int color_message_send;
70 static int color_message_highlight;
71 static int color_message_action;
72 static int color_timestamp;
74 static PurpleBuddy *
75 find_buddy_for_conversation(PurpleConversation *conv)
77 return purple_blist_find_buddy(purple_conversation_get_account(conv),
78 purple_conversation_get_name(conv));
81 static PurpleChat *
82 find_chat_for_conversation(PurpleConversation *conv)
84 return purple_blist_find_chat(purple_conversation_get_account(conv),
85 purple_conversation_get_name(conv));
88 static PurpleBlistNode *
89 get_conversation_blist_node(PurpleConversation *conv)
91 PurpleBlistNode *node = NULL;
93 if (PURPLE_IS_IM_CONVERSATION(conv)) {
94 node = (PurpleBlistNode*)find_buddy_for_conversation(conv);
95 node = node ? purple_blist_node_get_parent(node) : NULL;
96 } else if (PURPLE_IS_CHAT_CONVERSATION(conv)) {
97 node = (PurpleBlistNode*)find_chat_for_conversation(conv);
100 return node;
103 static void
104 send_typing_notification(GntWidget *w, FinchConv *ggconv)
106 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry));
107 gboolean empty = (!text || !*text || (*text == '/'));
108 if (purple_prefs_get_bool("/finch/conversations/notify_typing")) {
109 PurpleConversation *conv = ggconv->active_conv;
110 PurpleIMConversation *im = PURPLE_IM_CONVERSATION(conv);
111 if (!empty) {
112 gboolean send = (purple_im_conversation_get_send_typed_timeout(im) == 0);
114 purple_im_conversation_stop_send_typed_timeout(im);
115 purple_im_conversation_start_send_typed_timeout(im);
116 if (send || (purple_im_conversation_get_type_again(im) != 0 &&
117 time(NULL) > purple_im_conversation_get_type_again(im))) {
118 unsigned int timeout;
119 timeout = purple_serv_send_typing(purple_conversation_get_connection(conv),
120 purple_conversation_get_name(conv),
121 PURPLE_IM_TYPING);
122 purple_im_conversation_set_type_again(im, timeout);
124 } else {
125 purple_im_conversation_stop_send_typed_timeout(im);
127 purple_serv_send_typing(purple_conversation_get_connection(conv),
128 purple_conversation_get_name(conv),
129 PURPLE_IM_NOT_TYPING);
134 static void
135 entry_key_pressed(GntWidget *w, FinchConv *ggconv)
137 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry));
138 if (*text == '/' && *(text + 1) != '/')
140 PurpleConversation *conv = ggconv->active_conv;
141 PurpleCmdStatus status;
142 const char *cmdline = text + 1;
143 char *error = NULL, *escape;
145 escape = g_markup_escape_text(cmdline, -1);
146 status = purple_cmd_do_command(conv, cmdline, escape, &error);
147 g_free(escape);
149 switch (status)
151 case PURPLE_CMD_STATUS_OK:
152 break;
153 case PURPLE_CMD_STATUS_NOT_FOUND:
154 purple_conversation_write_system_message(conv,
155 _("No such command."), PURPLE_MESSAGE_NO_LOG);
156 break;
157 case PURPLE_CMD_STATUS_WRONG_ARGS:
158 purple_conversation_write_system_message(conv,
159 _("Syntax Error: You typed the wrong "
160 "number of arguments to that command."),
161 PURPLE_MESSAGE_NO_LOG);
162 break;
163 case PURPLE_CMD_STATUS_FAILED:
164 purple_conversation_write_system_message(conv,
165 error ? error : _("Your command failed for an unknown reason."),
166 PURPLE_MESSAGE_NO_LOG);
167 break;
168 case PURPLE_CMD_STATUS_WRONG_TYPE:
169 if(PURPLE_IS_IM_CONVERSATION(conv))
170 purple_conversation_write_system_message(conv,
171 _("That command only works in chats, not IMs."),
172 PURPLE_MESSAGE_NO_LOG);
173 else
174 purple_conversation_write_system_message(conv,
175 _("That command only works in IMs, not chats."),
176 PURPLE_MESSAGE_NO_LOG);
177 break;
178 case PURPLE_CMD_STATUS_WRONG_PROTOCOL:
179 purple_conversation_write_system_message(conv,
180 _("That command doesn't work on this protocol."),
181 PURPLE_MESSAGE_NO_LOG);
182 break;
184 g_free(error);
186 else if (!purple_account_is_connected(purple_conversation_get_account(ggconv->active_conv)))
188 purple_conversation_write_system_message(ggconv->active_conv,
189 _("Message was not sent, because you are not signed on."),
190 PURPLE_MESSAGE_ERROR | PURPLE_MESSAGE_NO_LOG);
192 else
194 char *escape = purple_markup_escape_text((*text == '/' ? text + 1 : text), -1);
195 purple_conversation_send(ggconv->active_conv, escape);
196 g_free(escape);
197 purple_idle_touch();
199 gnt_entry_add_to_history(GNT_ENTRY(ggconv->entry), text);
200 gnt_entry_clear(GNT_ENTRY(ggconv->entry));
203 static void
204 closing_window(GntWidget *window, FinchConv *ggconv)
206 GList *list = ggconv->list;
207 ggconv->window = NULL;
208 while (list) {
209 PurpleConversation *conv = list->data;
210 list = list->next;
211 g_object_unref(conv);
215 static void
216 size_changed_cb(GntWidget *widget, int width, int height)
218 int w, h;
219 gnt_widget_get_size(widget, &w, &h);
220 purple_prefs_set_int(PREF_ROOT "/size/width", w);
221 purple_prefs_set_int(PREF_ROOT "/size/height", h);
224 static void
225 save_position_cb(GntWidget *w, int x, int y)
227 purple_prefs_set_int(PREF_ROOT "/position/x", x);
228 purple_prefs_set_int(PREF_ROOT "/position/y", y);
231 static PurpleIMConversation *
232 find_im_with_contact(PurpleAccount *account, const char *name)
234 PurpleBlistNode *node;
235 PurpleBuddy *buddy = purple_blist_find_buddy(account, name);
236 PurpleIMConversation *im = NULL;
238 if (!buddy)
239 return NULL;
241 for (node = purple_blist_node_get_first_child(purple_blist_node_get_parent((PurpleBlistNode*)buddy));
242 node; node = purple_blist_node_get_sibling_next(node)) {
243 if (node == (PurpleBlistNode*)buddy)
244 continue;
245 if ((im = purple_conversations_find_im_with_account(
246 purple_buddy_get_name((PurpleBuddy*)node), purple_buddy_get_account((PurpleBuddy*)node))) != NULL)
247 break;
249 return im;
252 static char *
253 get_conversation_title(PurpleConversation *conv, PurpleAccount *account)
255 PurpleE2eeState *e2ee;
257 e2ee = purple_conversation_get_e2ee_state(conv);
259 return g_strdup_printf(_("%s (%s -- %s)%s%s%s%s"),
260 purple_conversation_get_title(conv),
261 purple_account_get_username(account),
262 purple_account_get_protocol_name(account),
263 e2ee ? " | " : "",
264 e2ee ? purple_e2ee_provider_get_name(purple_e2ee_state_get_provider(e2ee)) : "",
265 e2ee ? ": " : "",
266 e2ee ? purple_e2ee_state_get_name(e2ee) : "");
269 static void
270 update_buddy_typing(PurpleAccount *account, const char *who, gpointer null)
272 FinchConv *ggc;
273 PurpleIMConversation *im;
274 PurpleConversation *conv;
275 char *title, *str;
277 im = purple_conversations_find_im_with_account(who, account);
279 if (!im)
280 return;
282 conv = PURPLE_CONVERSATION(im);
283 ggc = FINCH_CONV(conv);
285 if (purple_im_conversation_get_typing_state(im) == PURPLE_IM_TYPING) {
286 int scroll;
287 str = get_conversation_title(conv, account);
288 title = g_strdup_printf(_("%s [%s]"), str,
289 gnt_ascii_only() ? "T" : "\342\243\277");
290 g_free(str);
292 scroll = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggc->tv));
293 str = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_title(conv));
294 /* Updating is a little buggy. So just remove and add a new one */
295 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE);
296 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggc->tv),
297 str, GNT_TEXT_FLAG_DIM, "typing");
298 g_free(str);
299 if (scroll <= 1)
300 gnt_text_view_scroll(GNT_TEXT_VIEW(ggc->tv), 0);
301 } else {
302 title = get_conversation_title(conv, account);
303 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", " ", TRUE);
305 gnt_screen_rename_widget(ggc->window, title);
306 g_free(title);
309 static void
310 chat_left_cb(PurpleConversation *conv, gpointer null)
312 purple_conversation_write_system_message(conv,
313 _("You have left this chat."), 0);
316 static void
317 buddy_signed_on_off(PurpleBuddy *buddy, gpointer null)
319 PurpleIMConversation *im = find_im_with_contact(purple_buddy_get_account(buddy), purple_buddy_get_name(buddy));
320 if (im == NULL)
321 return;
322 generate_send_to_menu(FINCH_CONV(PURPLE_CONVERSATION(im)));
325 static void
326 account_signed_on_off(PurpleConnection *gc, gpointer null)
328 GList *list = purple_conversations_get_ims();
329 while (list) {
330 PurpleConversation *conv = list->data;
331 PurpleIMConversation *cc = find_im_with_contact(
332 purple_conversation_get_account(conv), purple_conversation_get_name(conv));
333 if (cc)
334 generate_send_to_menu(FINCH_CONV(PURPLE_CONVERSATION(cc)));
335 list = list->next;
338 if (PURPLE_CONNECTION_IS_CONNECTED(gc)) {
339 /* We just signed on. Let's see if there's any chat that we have open,
340 * and hadn't left before the disconnect. */
341 list = purple_conversations_get_chats();
342 while (list) {
343 PurpleConversation *conv = list->data;
344 PurpleChat *chat;
345 GHashTable *comps = NULL;
347 list = list->next;
348 if (purple_conversation_get_account(conv) != purple_connection_get_account(gc) ||
349 !g_object_get_data(G_OBJECT(conv), "want-to-rejoin"))
350 continue;
352 chat = find_chat_for_conversation(conv);
353 if (chat == NULL) {
354 PurpleProtocol *protocol = purple_connection_get_protocol(gc);
355 comps = purple_protocol_chat_iface_info_defaults(protocol, gc,
356 purple_conversation_get_name(conv));
357 } else {
358 comps = purple_chat_get_components(chat);
360 purple_serv_join_chat(gc, comps);
361 if (chat == NULL && comps != NULL)
362 g_hash_table_destroy(comps);
367 static void
368 account_signing_off(PurpleConnection *gc)
370 GList *list = purple_conversations_get_chats();
371 PurpleAccount *account = purple_connection_get_account(gc);
373 /* We are about to sign off. See which chats we are currently in, and mark
374 * them for rejoin on reconnect. */
375 while (list) {
376 PurpleConversation *conv = list->data;
377 if (!purple_chat_conversation_has_left(PURPLE_CHAT_CONVERSATION(conv)) &&
378 purple_conversation_get_account(conv) == account) {
379 g_object_set_data(G_OBJECT(conv), "want-to-rejoin", GINT_TO_POINTER(TRUE));
380 purple_conversation_write_system_message(conv,
381 _("The account has disconnected and you are no "
382 "longer in this chat. You will be automatically rejoined in the chat when "
383 "the account reconnects."),
384 PURPLE_MESSAGE_NO_LOG);
386 list = list->next;
390 static gpointer
391 finch_conv_get_handle(void)
393 static int handle;
394 return &handle;
397 static void
398 cleared_message_history_cb(PurpleConversation *conv, gpointer data)
400 FinchConv *ggc = FINCH_CONV(conv);
401 if (ggc)
402 gnt_text_view_clear(GNT_TEXT_VIEW(ggc->tv));
405 static void
406 gg_extended_menu(FinchConv *ggc)
408 GntMenu *sub;
409 GList *list;
410 gboolean is_empty = TRUE;
412 g_return_if_fail(ggc != NULL);
414 sub = GNT_MENU(gnt_menu_new(GNT_MENU_POPUP));
415 gnt_menuitem_set_submenu(ggc->plugins, sub);
417 for (list = purple_conversation_get_extended_menu(ggc->active_conv);
418 list; list = g_list_delete_link(list, list))
420 finch_append_menu_action(sub, list->data, ggc->active_conv);
421 is_empty = FALSE;
423 gnt_menuitem_set_visible(ggc->plugins, !is_empty);
426 static void
427 conv_updated(PurpleConversation *conv, PurpleConversationUpdateType type)
429 if (purple_conversation_get_ui_data(conv) == NULL)
430 return;
432 if (type == PURPLE_CONVERSATION_UPDATE_FEATURES) {
433 gg_extended_menu(purple_conversation_get_ui_data(conv));
434 return;
436 if (type == PURPLE_CONVERSATION_UPDATE_E2EE) {
437 FinchConv *ggconv = FINCH_CONV(conv);
438 gchar *title;
440 title = get_conversation_title(conv,
441 purple_conversation_get_account(conv));
442 gnt_screen_rename_widget(ggconv->window, title);
443 g_free(title);
445 generate_e2ee_menu(ggconv);
447 return;
451 static void
452 clear_scrollback_cb(GntMenuItem *item, gpointer ggconv)
454 FinchConv *ggc = ggconv;
455 purple_conversation_clear_message_history(ggc->active_conv);
458 static void
459 send_file_cb(GntMenuItem *item, gpointer ggconv)
461 FinchConv *ggc = ggconv;
462 purple_serv_send_file(purple_conversation_get_connection(ggc->active_conv),
463 purple_conversation_get_name(ggc->active_conv), NULL);
466 static void
467 add_pounce_cb(GntMenuItem *item, gpointer ggconv)
469 FinchConv *ggc = ggconv;
470 finch_pounce_editor_show(
471 purple_conversation_get_account(ggc->active_conv),
472 purple_conversation_get_name(ggc->active_conv), NULL);
475 static void
476 get_info_cb(GntMenuItem *item, gpointer ggconv)
478 FinchConv *ggc = ggconv;
479 finch_retrieve_user_info(purple_conversation_get_connection(ggc->active_conv),
480 purple_conversation_get_name(ggc->active_conv));
483 static void
484 toggle_timestamps_cb(GntMenuItem *item, gpointer ggconv)
486 purple_prefs_set_bool(PREF_ROOT "/timestamps",
487 !purple_prefs_get_bool(PREF_ROOT "/timestamps"));
490 static void
491 toggle_logging_cb(GntMenuItem *item, gpointer ggconv)
493 FinchConv *fc = ggconv;
494 PurpleConversation *conv = fc->active_conv;
495 gboolean logging = gnt_menuitem_check_get_checked(GNT_MENU_ITEM_CHECK(item));
496 GList *iter;
498 if (logging == purple_conversation_is_logging(conv))
499 return;
501 /* Xerox */
502 if (logging) {
503 /* Enable logging first so the message below can be logged. */
504 purple_conversation_set_logging(conv, TRUE);
506 purple_conversation_write_system_message(conv,
507 _("Logging started. Future messages in this "
508 "conversation will be logged."), 0);
509 } else {
510 purple_conversation_write_system_message(conv,
511 _("Logging stopped. Future messages in this "
512 "conversation will not be logged."), 0);
514 /* Disable the logging second, so that the above message can be logged. */
515 purple_conversation_set_logging(conv, FALSE);
518 /* Each conversation with the same person will have the same logging setting */
519 for (iter = fc->list; iter; iter = iter->next) {
520 if (iter->data == conv)
521 continue;
522 purple_conversation_set_logging(iter->data, logging);
526 static void
527 toggle_sound_cb(GntMenuItem *item, gpointer ggconv)
529 FinchConv *fc = ggconv;
530 PurpleBlistNode *node = get_conversation_blist_node(fc->active_conv);
531 fc->flags ^= FINCH_CONV_NO_SOUND;
532 if (node)
533 purple_blist_node_set_bool(node, "gnt-mute-sound", !!(fc->flags & FINCH_CONV_NO_SOUND));
536 static void
537 send_to_cb(GntMenuItem *m, gpointer n)
539 PurpleAccount *account = g_object_get_data(G_OBJECT(m), "purple_account");
540 gchar *buddy = g_object_get_data(G_OBJECT(m), "purple_buddy_name");
541 PurpleIMConversation *im = purple_im_conversation_new(account, buddy);
542 finch_conversation_set_active(PURPLE_CONVERSATION(im));
545 static void
546 view_log_cb(GntMenuItem *n, gpointer ggc)
548 FinchConv *fc;
549 PurpleConversation *conv;
550 PurpleLogType type;
551 const char *name;
552 PurpleAccount *account;
553 GSList *buddies;
554 GSList *cur;
556 fc = ggc;
557 conv = fc->active_conv;
559 if (PURPLE_IS_IM_CONVERSATION(conv))
560 type = PURPLE_LOG_IM;
561 else if (PURPLE_IS_CHAT_CONVERSATION(conv))
562 type = PURPLE_LOG_CHAT;
563 else
564 return;
566 name = purple_conversation_get_name(conv);
567 account = purple_conversation_get_account(conv);
569 buddies = purple_blist_find_buddies(account, name);
570 for (cur = buddies; cur != NULL; cur = cur->next) {
571 PurpleBlistNode *node = cur->data;
572 if ((node != NULL) &&
573 (purple_blist_node_get_sibling_prev(node) || purple_blist_node_get_sibling_next(node))) {
574 finch_log_show_contact((PurpleContact *)purple_blist_node_get_parent(node));
575 g_slist_free(buddies);
576 return;
579 g_slist_free(buddies);
581 finch_log_show(type, name, account);
584 static void
585 generate_send_to_menu(FinchConv *ggc)
587 GntWidget *sub, *menu = ggc->menu;
588 GntMenuItem *item;
589 GSList *buds;
590 GList *list = NULL;
592 buds = purple_blist_find_buddies(purple_conversation_get_account(ggc->active_conv),
593 purple_conversation_get_name(ggc->active_conv));
594 if (!buds)
595 return;
597 if ((item = ggc->u.im->sendto) == NULL) {
598 item = gnt_menuitem_new(_("Send To"));
599 gnt_menu_add_item(GNT_MENU(menu), item);
600 ggc->u.im->sendto = item;
602 sub = gnt_menu_new(GNT_MENU_POPUP);
603 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
605 for (; buds; buds = g_slist_delete_link(buds, buds)) {
606 PurpleBlistNode *node = PURPLE_BLIST_NODE(purple_buddy_get_contact(PURPLE_BUDDY(buds->data)));
607 for (node = purple_blist_node_get_first_child(node); node != NULL;
608 node = purple_blist_node_get_sibling_next(node)) {
609 PurpleBuddy *buddy = (PurpleBuddy *)node;
610 PurpleAccount *account = purple_buddy_get_account(buddy);
611 if (purple_account_is_connected(account)) {
612 /* Use the PurplePresence to get unique buddies. */
613 PurplePresence *presence = purple_buddy_get_presence(buddy);
614 if (!g_list_find(list, presence))
615 list = g_list_prepend(list, presence);
619 for (list = g_list_reverse(list); list != NULL; list = g_list_delete_link(list, list)) {
620 PurplePresence *pre = list->data;
621 PurpleBuddy *buddy = purple_buddy_presence_get_buddy(PURPLE_BUDDY_PRESENCE(pre));
622 PurpleAccount *account = purple_buddy_get_account(buddy);
623 gchar *name = g_strdup(purple_buddy_get_name(buddy));
624 gchar *text = g_strdup_printf("%s (%s)", purple_buddy_get_name(buddy), purple_account_get_username(account));
625 item = gnt_menuitem_new(text);
626 g_free(text);
627 gnt_menu_add_item(GNT_MENU(sub), item);
628 gnt_menuitem_set_callback(item, send_to_cb, NULL);
629 g_object_set_data(G_OBJECT(item), "purple_account", account);
630 g_object_set_data_full(G_OBJECT(item), "purple_buddy_name", name, g_free);
634 static void
635 generate_e2ee_menu(FinchConv *ggc)
637 GntMenu *sub;
638 GntWidget *menu = ggc->menu;
639 PurpleConversation *conv = ggc->active_conv;
640 GntMenuItem *item;
641 PurpleE2eeProvider *eprov;
642 GList *menu_actions, *it;
644 eprov = purple_e2ee_provider_get_main();
646 item = ggc->u.im->e2ee_menu;
647 if (item == NULL) {
648 item = gnt_menuitem_new(NULL);
649 gnt_menu_add_item(GNT_MENU(menu), item);
650 ggc->u.im->e2ee_menu = item;
652 sub = GNT_MENU(gnt_menu_new(GNT_MENU_POPUP));
653 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
655 gnt_menuitem_set_visible(item, (eprov != NULL));
656 if (eprov == NULL)
657 return;
658 gnt_menuitem_set_text(item, purple_e2ee_provider_get_name(eprov));
660 menu_actions = purple_e2ee_provider_get_conv_menu_actions(eprov, conv);
661 for (it = menu_actions; it; it = g_list_next(it)) {
662 PurpleActionMenu *action = it->data;
664 finch_append_menu_action(sub, action, conv);
666 g_list_free(menu_actions);
669 static void
670 invite_cb(GntMenuItem *item, gpointer ggconv)
672 FinchConv *fc = ggconv;
673 PurpleChatConversation *chat = PURPLE_CHAT_CONVERSATION(fc->active_conv);
674 purple_chat_conversation_invite_user(chat, NULL, NULL, TRUE);
677 static void
678 plugin_changed_cb(PurplePlugin *p, gpointer data)
680 gg_extended_menu(data);
683 static void
684 gg_create_menu(FinchConv *ggc)
686 GntWidget *menu, *sub;
687 GntMenuItem *item;
689 ggc->menu = menu = gnt_menu_new(GNT_MENU_TOPLEVEL);
690 gnt_window_set_menu(GNT_WINDOW(ggc->window), GNT_MENU(menu));
692 item = gnt_menuitem_new(_("Conversation"));
693 gnt_menu_add_item(GNT_MENU(menu), item);
695 sub = gnt_menu_new(GNT_MENU_POPUP);
696 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
698 item = gnt_menuitem_new(_("Clear Scrollback"));
699 gnt_menu_add_item(GNT_MENU(sub), item);
700 gnt_menuitem_set_callback(item, clear_scrollback_cb, ggc);
702 item = gnt_menuitem_check_new(_("Show Timestamps"));
703 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
704 purple_prefs_get_bool(PREF_ROOT "/timestamps"));
705 gnt_menu_add_item(GNT_MENU(sub), item);
706 gnt_menuitem_set_callback(item, toggle_timestamps_cb, ggc);
708 if (PURPLE_IS_IM_CONVERSATION(ggc->active_conv)) {
709 PurpleAccount *account = purple_conversation_get_account(ggc->active_conv);
710 PurpleConnection *gc = purple_account_get_connection(account);
711 PurpleProtocol *protocol =
712 gc ? purple_connection_get_protocol(gc) : NULL;
714 if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, get_info)) {
715 item = gnt_menuitem_new(_("Get Info"));
716 gnt_menu_add_item(GNT_MENU(sub), item);
717 gnt_menuitem_set_callback(item, get_info_cb, ggc);
720 item = gnt_menuitem_new(_("Add Buddy Pounce..."));
721 gnt_menu_add_item(GNT_MENU(sub), item);
722 gnt_menuitem_set_callback(item, add_pounce_cb, ggc);
724 if (PURPLE_IS_PROTOCOL_XFER(protocol) &&
725 purple_protocol_xfer_can_receive(
726 PURPLE_PROTOCOL_XFER(protocol),
728 purple_conversation_get_name(ggc->active_conv)
731 item = gnt_menuitem_new(_("Send File"));
732 gnt_menu_add_item(GNT_MENU(sub), item);
733 gnt_menuitem_set_callback(item, send_file_cb, ggc);
736 generate_send_to_menu(ggc);
737 } else if (PURPLE_IS_CHAT_CONVERSATION(ggc->active_conv)) {
738 item = gnt_menuitem_new(_("Invite..."));
739 gnt_menu_add_item(GNT_MENU(sub), item);
740 gnt_menuitem_set_callback(item, invite_cb, ggc);
743 generate_e2ee_menu(ggc);
745 item = gnt_menuitem_new(_("View Log..."));
746 gnt_menu_add_item(GNT_MENU(sub), item);
747 gnt_menuitem_set_callback(item, view_log_cb, ggc);
749 item = gnt_menuitem_check_new(_("Enable Logging"));
750 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
751 purple_conversation_is_logging(ggc->active_conv));
752 gnt_menu_add_item(GNT_MENU(sub), item);
753 gnt_menuitem_set_callback(item, toggle_logging_cb, ggc);
755 item = gnt_menuitem_check_new(_("Enable Sounds"));
756 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
757 !(ggc->flags & FINCH_CONV_NO_SOUND));
758 gnt_menu_add_item(GNT_MENU(sub), item);
759 gnt_menuitem_set_callback(item, toggle_sound_cb, ggc);
761 item = gnt_menuitem_new(_("Plugins"));
762 gnt_menu_add_item(GNT_MENU(menu), item);
763 ggc->plugins = item;
765 gg_extended_menu(ggc);
768 static void
769 create_conv_from_userlist(GntWidget *widget, FinchConv *fc)
771 PurpleAccount *account = purple_conversation_get_account(fc->active_conv);
772 PurpleConnection *gc = purple_account_get_connection(account);
773 PurpleProtocol *protocol = NULL;
774 char *name, *realname = NULL;
776 if (!gc) {
777 purple_conversation_write_system_message(fc->active_conv,
778 _("You are not connected."), 0);
779 return;
782 name = gnt_tree_get_selection_data(GNT_TREE(widget));
784 protocol = purple_connection_get_protocol(gc);
785 if (protocol)
786 realname = purple_protocol_chat_iface_get_user_real_name(protocol, gc,
787 purple_chat_conversation_get_id(
788 PURPLE_CHAT_CONVERSATION(fc->active_conv)), name);
790 purple_im_conversation_new(account, realname ? realname : name);
791 g_free(realname);
794 static void
795 gained_focus_cb(GntWindow *window, FinchConv *fc)
797 GList *iter;
798 for (iter = fc->list; iter; iter = iter->next) {
799 g_object_set_data(G_OBJECT(iter->data), "unseen-count", 0);
800 purple_conversation_update(iter->data, PURPLE_CONVERSATION_UPDATE_UNSEEN);
804 static void
805 completion_cb(GntEntry *entry, const char *start, const char *end)
807 if (start == gnt_entry_get_text(entry) && *start != '/')
808 gnt_widget_key_pressed(GNT_WIDGET(entry), ": ");
811 static void
812 gg_setup_commands(FinchConv *fconv, gboolean remove_first)
814 GList *commands;
815 char command[256] = "/";
817 if (remove_first) {
818 commands = purple_cmd_list(NULL);
819 for (; commands; commands = g_list_delete_link(commands, commands)) {
820 g_strlcpy(command + 1, commands->data, sizeof(command) - 1);
821 gnt_entry_remove_suggest(GNT_ENTRY(fconv->entry), command);
825 commands = purple_cmd_list(fconv->active_conv);
826 for (; commands; commands = g_list_delete_link(commands, commands)) {
827 g_strlcpy(command + 1, commands->data, sizeof(command) - 1);
828 gnt_entry_add_suggest(GNT_ENTRY(fconv->entry), command);
832 static void
833 cmd_added_cb(const char *cmd, PurpleCmdPriority prior, PurpleCmdFlag flags,
834 FinchConv *fconv)
836 gg_setup_commands(fconv, TRUE);
839 static void
840 cmd_removed_cb(const char *cmd, FinchConv *fconv)
842 char command[256] = "/";
843 g_strlcpy(command + 1, cmd, sizeof(command) - 1);
844 gnt_entry_remove_suggest(GNT_ENTRY(fconv->entry), command);
845 gg_setup_commands(fconv, TRUE);
848 static void
849 finch_create_conversation(PurpleConversation *conv)
851 FinchConv *ggc = FINCH_CONV(conv);
852 char *title;
853 PurpleConversation *cc;
854 PurpleAccount *account;
855 PurpleBlistNode *convnode = NULL;
857 if (ggc) {
858 gnt_window_present(ggc->window);
859 return;
862 account = purple_conversation_get_account(conv);
863 cc = PURPLE_CONVERSATION(find_im_with_contact(account, purple_conversation_get_name(conv)));
864 if (cc && FINCH_CONV(cc))
865 ggc = FINCH_CONV(cc);
866 else
867 ggc = g_new0(FinchConv, 1);
869 /* Each conversation with the same person will have the same logging setting */
870 if (ggc->list) {
871 purple_conversation_set_logging(conv,
872 purple_conversation_is_logging(ggc->list->data));
875 ggc->list = g_list_prepend(ggc->list, conv);
876 ggc->active_conv = conv;
877 purple_conversation_set_ui_data(conv, ggc);
879 if (cc && FINCH_CONV(cc) && cc != conv) {
880 finch_conversation_set_active(conv);
881 return;
884 title = get_conversation_title(conv, account);
886 ggc->window = gnt_vwindow_new(FALSE);
887 gnt_box_set_title(GNT_BOX(ggc->window), title);
888 gnt_box_set_toplevel(GNT_BOX(ggc->window), TRUE);
889 gnt_box_set_pad(GNT_BOX(ggc->window), 0);
891 if (PURPLE_IS_IM_CONVERSATION(conv))
892 gnt_widget_set_name(ggc->window, "conversation-window-im");
893 else if (PURPLE_IS_CHAT_CONVERSATION(conv))
894 gnt_widget_set_name(ggc->window, "conversation-window-chat");
895 else
896 gnt_widget_set_name(ggc->window, "conversation-window-other");
898 ggc->tv = gnt_text_view_new();
899 gnt_widget_set_name(ggc->tv, "conversation-window-textview");
900 gnt_widget_set_size(ggc->tv, purple_prefs_get_int(PREF_ROOT "/size/width"),
901 purple_prefs_get_int(PREF_ROOT "/size/height"));
903 if (PURPLE_IS_CHAT_CONVERSATION(conv)) {
904 GntWidget *hbox, *tree;
905 FinchConvChat *fc = ggc->u.chat = g_new0(FinchConvChat, 1);
906 hbox = gnt_hbox_new(FALSE);
907 gnt_box_set_pad(GNT_BOX(hbox), 0);
908 tree = fc->userlist = gnt_tree_new_with_columns(2);
909 gnt_tree_set_col_width(GNT_TREE(tree), 0, 1); /* The flag column */
910 gnt_tree_set_compare_func(GNT_TREE(tree), (GCompareFunc)g_utf8_collate);
911 gnt_tree_set_hash_fns(GNT_TREE(tree), g_str_hash, g_str_equal, g_free);
912 gnt_tree_set_search_column(GNT_TREE(tree), 1);
913 gnt_widget_set_has_border(tree, FALSE);
914 gnt_box_add_widget(GNT_BOX(hbox), ggc->tv);
915 gnt_box_add_widget(GNT_BOX(hbox), tree);
916 gnt_box_add_widget(GNT_BOX(ggc->window), hbox);
917 g_signal_connect(G_OBJECT(tree), "activate", G_CALLBACK(create_conv_from_userlist), ggc);
918 gnt_widget_set_visible(tree, purple_prefs_get_bool(PREF_USERLIST));
919 } else {
920 ggc->u.im = g_new0(FinchConvIm, 1);
921 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->tv);
924 ggc->info = gnt_vbox_new(FALSE);
925 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->info);
927 ggc->entry = gnt_entry_new(NULL);
928 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->entry);
929 gnt_widget_set_name(ggc->entry, "conversation-window-entry");
930 gnt_entry_set_history_length(GNT_ENTRY(ggc->entry), -1);
931 gnt_entry_set_word_suggest(GNT_ENTRY(ggc->entry), TRUE);
932 gnt_entry_set_always_suggest(GNT_ENTRY(ggc->entry), FALSE);
934 gnt_text_view_attach_scroll_widget(GNT_TEXT_VIEW(ggc->tv), ggc->entry);
935 gnt_text_view_attach_pager_widget(GNT_TEXT_VIEW(ggc->tv), ggc->entry);
937 g_signal_connect_after(G_OBJECT(ggc->entry), "activate", G_CALLBACK(entry_key_pressed), ggc);
938 g_signal_connect(G_OBJECT(ggc->entry), "completion", G_CALLBACK(completion_cb), NULL);
939 g_signal_connect(G_OBJECT(ggc->window), "destroy", G_CALLBACK(closing_window), ggc);
941 gnt_widget_set_position(ggc->window, purple_prefs_get_int(PREF_ROOT "/position/x"),
942 purple_prefs_get_int(PREF_ROOT "/position/y"));
943 gnt_widget_show(ggc->window);
945 g_signal_connect(G_OBJECT(ggc->tv), "size_changed", G_CALLBACK(size_changed_cb), NULL);
946 g_signal_connect(G_OBJECT(ggc->window), "position_set", G_CALLBACK(save_position_cb), NULL);
948 if (PURPLE_IS_IM_CONVERSATION(conv))
949 g_signal_connect(G_OBJECT(ggc->entry), "text_changed", G_CALLBACK(send_typing_notification), ggc);
951 convnode = get_conversation_blist_node(conv);
952 if ((convnode && purple_blist_node_get_bool(convnode, "gnt-mute-sound")) ||
953 !finch_sound_is_enabled())
954 ggc->flags |= FINCH_CONV_NO_SOUND;
956 gg_create_menu(ggc);
957 gg_setup_commands(ggc, FALSE);
959 purple_signal_connect(purple_cmds_get_handle(), "cmd-added", ggc,
960 G_CALLBACK(cmd_added_cb), ggc);
961 purple_signal_connect(purple_cmds_get_handle(), "cmd-removed", ggc,
962 G_CALLBACK(cmd_removed_cb), ggc);
964 purple_signal_connect(purple_plugins_get_handle(), "plugin-load", ggc,
965 PURPLE_CALLBACK(plugin_changed_cb), ggc);
966 purple_signal_connect(purple_plugins_get_handle(), "plugin-unload", ggc,
967 PURPLE_CALLBACK(plugin_changed_cb), ggc);
969 g_free(title);
970 gnt_box_give_focus_to_child(GNT_BOX(ggc->window), ggc->entry);
971 g_signal_connect(G_OBJECT(ggc->window), "gained-focus", G_CALLBACK(gained_focus_cb), ggc);
974 static void
975 finch_destroy_conversation(PurpleConversation *conv)
977 /* do stuff here */
978 FinchConv *ggc = FINCH_CONV(conv);
979 ggc->list = g_list_remove(ggc->list, conv);
980 if (ggc->list && conv == ggc->active_conv)
981 finch_conversation_set_active(ggc->list->data);
983 if (ggc->list == NULL) {
984 g_free(ggc->u.chat);
985 purple_signals_disconnect_by_handle(ggc);
986 if (ggc->window)
987 gnt_widget_destroy(ggc->window);
988 g_free(ggc);
992 static void
993 finch_write_conv(PurpleConversation *conv, PurpleMessage *msg)
995 FinchConv *ggconv = FINCH_CONV(conv);
996 char *strip, *newline;
997 GntTextFormatFlags fl = 0;
998 int pos;
999 PurpleMessageFlags flags = purple_message_get_flags(msg);
1001 g_return_if_fail(ggconv != NULL);
1003 if ((flags & PURPLE_MESSAGE_SYSTEM) && !(flags & PURPLE_MESSAGE_NOTIFY)) {
1004 flags &= ~(PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV);
1007 if (ggconv->active_conv != conv) {
1008 if (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV))
1009 finch_conversation_set_active(conv);
1010 else
1011 return;
1014 pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggconv->tv));
1016 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggconv->tv), "typing", NULL, TRUE);
1017 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), "\n", GNT_TEXT_FLAG_NORMAL);
1019 /* Unnecessary to print the timestamp for delayed message */
1020 if (purple_prefs_get_bool("/finch/conversations/timestamps")) {
1021 time_t mtime = purple_message_get_time(msg);
1022 if (!mtime)
1023 time(&mtime);
1024 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
1025 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)), gnt_color_pair(color_timestamp));
1028 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), " ", GNT_TEXT_FLAG_NORMAL);
1030 if (flags & PURPLE_MESSAGE_AUTO_RESP)
1031 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
1032 _("<AUTO-REPLY> "), GNT_TEXT_FLAG_BOLD);
1034 if (purple_message_get_author(msg) && (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV)) &&
1035 !(flags & PURPLE_MESSAGE_NOTIFY))
1037 char * name = NULL;
1038 GntTextFormatFlags msgflags = GNT_TEXT_FLAG_NORMAL;
1039 gboolean me = FALSE;
1040 gchar *msg_text = g_strdup(purple_message_get_contents(msg));
1042 if (purple_message_meify(msg_text, -1)) {
1043 name = g_strdup_printf("*** %s", purple_message_get_author_alias(msg));
1044 if (!(flags & PURPLE_MESSAGE_SEND) &&
1045 (flags & PURPLE_MESSAGE_NICK))
1046 msgflags = gnt_color_pair(color_message_highlight);
1047 else
1048 msgflags = gnt_color_pair(color_message_action);
1049 me = TRUE;
1050 } else {
1051 name = g_strdup_printf("%s", purple_message_get_author_alias(msg));
1052 if (flags & PURPLE_MESSAGE_SEND)
1053 msgflags = gnt_color_pair(color_message_send);
1054 else if (flags & PURPLE_MESSAGE_NICK)
1055 msgflags = gnt_color_pair(color_message_highlight);
1056 else
1057 msgflags = gnt_color_pair(color_message_receive);
1059 purple_message_set_contents(msg, msg_text); /* might be "meified" */
1060 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
1061 name, msgflags);
1062 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), me ? " " : ": ", GNT_TEXT_FLAG_NORMAL);
1063 g_free(name);
1064 g_free(msg_text);
1065 } else
1066 fl = GNT_TEXT_FLAG_DIM;
1068 if (flags & PURPLE_MESSAGE_ERROR)
1069 fl |= GNT_TEXT_FLAG_BOLD;
1071 /* XXX: Remove this workaround when textview can parse messages. */
1072 newline = purple_strdup_withhtml(purple_message_get_contents(msg));
1073 strip = purple_markup_strip_html(newline);
1074 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
1075 strip, fl);
1077 g_free(newline);
1078 g_free(strip);
1080 if (PURPLE_IS_IM_CONVERSATION(conv) && purple_im_conversation_get_typing_state(
1081 PURPLE_IM_CONVERSATION(conv)) == PURPLE_IM_TYPING) {
1082 strip = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_title(conv));
1083 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggconv->tv),
1084 strip, GNT_TEXT_FLAG_DIM, "typing");
1085 g_free(strip);
1088 if (pos <= 1)
1089 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 0);
1091 if (flags & (PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_NICK | PURPLE_MESSAGE_ERROR))
1092 gnt_widget_set_urgent(ggconv->tv);
1093 if (flags & PURPLE_MESSAGE_RECV && !gnt_widget_has_focus(ggconv->window)) {
1094 int count = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(conv), "unseen-count"));
1095 g_object_set_data(G_OBJECT(conv), "unseen-count", GINT_TO_POINTER(count + 1));
1096 purple_conversation_update(conv, PURPLE_CONVERSATION_UPDATE_UNSEEN);
1100 static const char *
1101 chat_flag_text(PurpleChatUserFlags flags)
1103 if (flags & PURPLE_CHAT_USER_FOUNDER)
1104 return "~";
1105 if (flags & PURPLE_CHAT_USER_OP)
1106 return "@";
1107 if (flags & PURPLE_CHAT_USER_HALFOP)
1108 return "%";
1109 if (flags & PURPLE_CHAT_USER_VOICE)
1110 return "+";
1111 return " ";
1114 static void
1115 finch_chat_add_users(PurpleChatConversation *chat, GList *users, gboolean new_arrivals)
1117 PurpleConversation *conv = PURPLE_CONVERSATION(chat);
1118 FinchConv *ggc = FINCH_CONV(conv);
1119 GntEntry *entry = GNT_ENTRY(ggc->entry);
1121 if (!new_arrivals)
1123 /* Print the list of users in the room */
1124 GString *string = g_string_new(NULL);
1125 GList *iter;
1126 int count = g_list_length(users);
1128 g_string_printf(string,
1129 ngettext("List of %d user:\n", "List of %d users:\n", count), count);
1130 for (iter = users; iter; iter = iter->next)
1132 PurpleChatUser *chatuser = iter->data;
1133 const char *str;
1135 if ((str = purple_chat_user_get_alias(chatuser)) == NULL)
1136 str = purple_chat_user_get_name(chatuser);
1137 g_string_append_printf(string, "[ %s ]", str);
1140 purple_conversation_write_system_message(
1141 conv, string->str, 0);
1142 g_string_free(string, TRUE);
1145 for (; users; users = users->next)
1147 PurpleChatUser *chatuser = users->data;
1148 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1149 gnt_entry_add_suggest(entry, purple_chat_user_get_name(chatuser));
1150 gnt_entry_add_suggest(entry, purple_chat_user_get_alias(chatuser));
1151 gnt_tree_add_row_after(tree, g_strdup(purple_chat_user_get_name(chatuser)),
1152 gnt_tree_create_row(tree, chat_flag_text(purple_chat_user_get_flags(chatuser)), purple_chat_user_get_alias(chatuser)), NULL, NULL);
1156 static void
1157 finch_chat_rename_user(PurpleChatConversation *chat, const char *old, const char *new_n, const char *new_a)
1159 /* Update the name for string completion */
1160 FinchConv *ggc = FINCH_CONV(PURPLE_CONVERSATION(chat));
1161 GntEntry *entry = GNT_ENTRY(ggc->entry);
1162 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1163 PurpleChatUser *cb = purple_chat_conversation_find_user(chat, new_n);
1165 gnt_entry_remove_suggest(entry, old);
1166 gnt_tree_remove(tree, (gpointer)old);
1168 gnt_entry_add_suggest(entry, new_n);
1169 gnt_entry_add_suggest(entry, new_a);
1170 gnt_tree_add_row_after(tree, g_strdup(new_n),
1171 gnt_tree_create_row(tree, chat_flag_text(purple_chat_user_get_flags(cb)), new_a), NULL, NULL);
1174 static void
1175 finch_chat_remove_users(PurpleChatConversation *chat, GList *list)
1177 /* Remove the name from string completion */
1178 FinchConv *ggc = FINCH_CONV(PURPLE_CONVERSATION(chat));
1179 GntEntry *entry = GNT_ENTRY(ggc->entry);
1180 for (; list; list = list->next) {
1181 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1182 gnt_entry_remove_suggest(entry, list->data);
1183 gnt_tree_remove(tree, list->data);
1187 static void
1188 finch_chat_update_user(PurpleChatUser *cb)
1190 PurpleChatConversation *chat;
1191 FinchConv *ggc;
1192 if (!cb)
1193 return;
1195 chat = purple_chat_user_get_chat(cb);
1196 ggc = FINCH_CONV(PURPLE_CONVERSATION(chat));
1197 gnt_tree_change_text(GNT_TREE(ggc->u.chat->userlist),
1198 (gpointer)purple_chat_user_get_name(cb), 0,
1199 chat_flag_text(purple_chat_user_get_flags(cb)));
1202 static void
1203 finch_conv_present(PurpleConversation *conv)
1205 FinchConv *fc = FINCH_CONV(conv);
1206 if (fc && fc->window)
1207 gnt_window_present(fc->window);
1210 static gboolean
1211 finch_conv_has_focus(PurpleConversation *conv)
1213 FinchConv *fc = FINCH_CONV(conv);
1214 if (fc && fc->window)
1215 return gnt_widget_has_focus(fc->window);
1216 return FALSE;
1219 static PurpleConversationUiOps conv_ui_ops =
1221 finch_create_conversation,
1222 finch_destroy_conversation,
1223 NULL, /* write_chat */
1224 NULL, /* write_im */
1225 finch_write_conv,
1226 finch_chat_add_users,
1227 finch_chat_rename_user,
1228 finch_chat_remove_users,
1229 finch_chat_update_user,
1230 finch_conv_present, /* present */
1231 finch_conv_has_focus, /* has_focus */
1232 NULL, /* send_confirm */
1233 NULL,
1234 NULL,
1235 NULL,
1236 NULL
1239 PurpleConversationUiOps *finch_conv_get_ui_ops()
1241 return &conv_ui_ops;
1244 /* Xerox */
1245 static PurpleCmdRet
1246 say_command_cb(PurpleConversation *conv,
1247 const char *cmd, char **args, char **error, void *data)
1249 purple_conversation_send(conv, args[0]);
1251 return PURPLE_CMD_RET_OK;
1254 /* Xerox */
1255 static PurpleCmdRet
1256 me_command_cb(PurpleConversation *conv,
1257 const char *cmd, char **args, char **error, void *data)
1259 char *tmp;
1261 tmp = g_strdup_printf("/me %s", args[0]);
1262 purple_conversation_send(conv, tmp);
1264 g_free(tmp);
1265 return PURPLE_CMD_RET_OK;
1268 /* Xerox */
1269 static PurpleCmdRet
1270 debug_command_cb(PurpleConversation *conv,
1271 const char *cmd, char **args, char **error, void *data)
1273 char *tmp, *markup;
1275 if (!g_ascii_strcasecmp(args[0], "version")) {
1276 tmp = g_strdup_printf("Using Finch v%s with libpurple v%s.",
1277 DISPLAY_VERSION, purple_core_get_version());
1278 } else if (!g_ascii_strcasecmp(args[0], "plugins")) {
1279 /* Show all the loaded plugins, including plugins marked internal.
1280 * This is intentional, since third party protocols are often sources of bugs, and some
1281 * loaders can also be buggy.
1283 GString *str = g_string_new("Loaded Plugins: ");
1284 const GList *plugins = purple_plugins_get_loaded();
1285 if (plugins) {
1286 for (; plugins; plugins = plugins->next) {
1287 GPluginPluginInfo *plugin_info =
1288 GPLUGIN_PLUGIN_INFO(
1289 purple_plugin_get_info(
1290 plugins->data));
1291 str = g_string_append(
1292 str, gplugin_plugin_info_get_name(
1293 plugin_info));
1295 if (plugins->next)
1296 str = g_string_append(str, ", ");
1298 } else {
1299 str = g_string_append(str, "(none)");
1302 tmp = g_string_free(str, FALSE);
1303 } else {
1304 purple_conversation_write_system_message(conv,
1305 _("Supported debug options are: plugins version"),
1306 PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_ERROR);
1307 return PURPLE_CMD_RET_OK;
1310 markup = g_markup_escape_text(tmp, -1);
1311 purple_conversation_send(conv, markup);
1313 g_free(tmp);
1314 g_free(markup);
1315 return PURPLE_CMD_RET_OK;
1318 /* Xerox */
1319 static PurpleCmdRet
1320 clear_command_cb(PurpleConversation *conv,
1321 const char *cmd, char **args, char **error, void *data)
1323 purple_conversation_clear_message_history(conv);
1324 return PURPLE_CMD_RET_OK;
1327 /* Xerox */
1328 static PurpleCmdRet
1329 help_command_cb(PurpleConversation *conv,
1330 const char *cmd, char **args, char **error, void *data)
1332 GList *l, *text;
1333 GString *s;
1335 if (args[0] != NULL) {
1336 s = g_string_new("");
1337 text = purple_cmd_help(conv, args[0]);
1339 if (text) {
1340 for (l = text; l; l = l->next)
1341 if (l->next)
1342 g_string_append_printf(s, "%s\n", (char *)l->data);
1343 else
1344 g_string_append_printf(s, "%s", (char *)l->data);
1345 } else {
1346 g_string_append(s, _("No such command (in this context)."));
1348 } else {
1349 s = g_string_new(_("Use \"/help &lt;command&gt;\" for help with a "
1350 "specific command.\nThe following commands are available in "
1351 "this context:\n"));
1353 text = purple_cmd_list(conv);
1354 for (l = text; l; l = l->next)
1355 if (l->next)
1356 g_string_append_printf(s, "%s, ", (char *)l->data);
1357 else
1358 g_string_append_printf(s, "%s.", (char *)l->data);
1359 g_list_free(text);
1362 purple_conversation_write_system_message(conv, s->str, PURPLE_MESSAGE_NO_LOG);
1363 g_string_free(s, TRUE);
1365 return PURPLE_CMD_RET_OK;
1368 static PurpleCmdRet
1369 cmd_show_window(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1371 void (*callback)(void) = data;
1372 callback();
1373 return PURPLE_CMD_RET_OK;
1376 static PurpleCmdRet
1377 cmd_message_color(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1379 int *msgclass = NULL;
1380 int fg, bg;
1382 if (purple_strequal(args[0], "receive"))
1383 msgclass = &color_message_receive;
1384 else if (purple_strequal(args[0], "send"))
1385 msgclass = &color_message_send;
1386 else if (purple_strequal(args[0], "highlight"))
1387 msgclass = &color_message_highlight;
1388 else if (purple_strequal(args[0], "action"))
1389 msgclass = &color_message_action;
1390 else if (purple_strequal(args[0], "timestamp"))
1391 msgclass = &color_timestamp;
1392 else {
1393 if (error)
1394 *error = g_strdup_printf(_("%s is not a valid message class. See '/help msgcolor' for valid message classes."), args[0]);
1395 return PURPLE_CMD_RET_FAILED;
1398 fg = gnt_colors_get_color(args[1]);
1399 if (fg == -EINVAL) {
1400 if (error)
1401 *error = g_strdup_printf(_("%s is not a valid color. See '/help msgcolor' for valid colors."), args[1]);
1402 return PURPLE_CMD_RET_FAILED;
1405 bg = gnt_colors_get_color(args[2]);
1406 if (bg == -EINVAL) {
1407 if (error)
1408 *error = g_strdup_printf(_("%s is not a valid color. See '/help msgcolor' for valid colors."), args[2]);
1409 return PURPLE_CMD_RET_FAILED;
1412 init_pair(*msgclass, fg, bg);
1414 return PURPLE_CMD_RET_OK;
1417 static PurpleCmdRet
1418 users_command_cb(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1420 FinchConv *fc = FINCH_CONV(conv);
1421 FinchConvChat *ch;
1422 if (!fc)
1423 return PURPLE_CMD_RET_FAILED;
1425 ch = fc->u.chat;
1426 gnt_widget_set_visible(ch->userlist,
1427 !gnt_widget_get_visible(ch->userlist));
1428 gnt_box_readjust(GNT_BOX(fc->window));
1429 gnt_box_give_focus_to_child(GNT_BOX(fc->window), fc->entry);
1430 purple_prefs_set_bool(PREF_USERLIST, gnt_widget_get_visible(ch->userlist));
1431 return PURPLE_CMD_RET_OK;
1434 void finch_conversation_init()
1436 color_message_send = gnt_style_get_color(NULL, "color-message-sent");
1437 if (!color_message_send)
1438 color_message_send = gnt_color_add_pair(COLOR_CYAN, -1);
1439 color_message_receive = gnt_style_get_color(NULL, "color-message-received");
1440 if (!color_message_receive)
1441 color_message_receive = gnt_color_add_pair(COLOR_RED, -1);
1442 color_message_highlight = gnt_style_get_color(NULL, "color-message-highlight");
1443 if (!color_message_highlight)
1444 color_message_highlight = gnt_color_add_pair(COLOR_GREEN, -1);
1445 color_timestamp = gnt_style_get_color(NULL, "color-timestamp");
1446 if (!color_timestamp)
1447 color_timestamp = gnt_color_add_pair(COLOR_BLUE, -1);
1448 color_message_action = gnt_style_get_color(NULL, "color-message-action");
1449 if (!color_message_action)
1450 color_message_action = gnt_color_add_pair(COLOR_YELLOW, -1);
1451 purple_prefs_add_none(PREF_ROOT);
1452 purple_prefs_add_none(PREF_ROOT "/size");
1453 purple_prefs_add_int(PREF_ROOT "/size/width", 70);
1454 purple_prefs_add_int(PREF_ROOT "/size/height", 20);
1455 purple_prefs_add_none(PREF_ROOT "/position");
1456 purple_prefs_add_int(PREF_ROOT "/position/x", 0);
1457 purple_prefs_add_int(PREF_ROOT "/position/y", 0);
1458 purple_prefs_add_none(PREF_CHAT);
1459 purple_prefs_add_bool(PREF_USERLIST, FALSE);
1461 /* Xerox the commands */
1462 purple_cmd_register("say", "S", PURPLE_CMD_P_DEFAULT,
1463 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1464 say_command_cb, _("say &lt;message&gt;: Send a message normally as if you weren't using a command."), NULL);
1465 purple_cmd_register("me", "S", PURPLE_CMD_P_DEFAULT,
1466 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1467 me_command_cb, _("me &lt;action&gt;: Send an IRC style action to a buddy or chat."), NULL);
1468 purple_cmd_register("debug", "w", PURPLE_CMD_P_DEFAULT,
1469 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1470 debug_command_cb, _("debug &lt;option&gt;: Send various debug information to the current conversation."), NULL);
1471 purple_cmd_register("clear", "", PURPLE_CMD_P_DEFAULT,
1472 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1473 clear_command_cb, _("clear: Clears the conversation scrollback."), NULL);
1474 purple_cmd_register("help", "w", PURPLE_CMD_P_DEFAULT,
1475 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL,
1476 help_command_cb, _("help &lt;command&gt;: Help on a specific command."), NULL);
1477 purple_cmd_register("users", "", PURPLE_CMD_P_DEFAULT,
1478 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL,
1479 users_command_cb, _("users: Show the list of users in the chat."), NULL);
1481 /* Now some commands to bring up some other windows */
1482 purple_cmd_register("plugins", "", PURPLE_CMD_P_DEFAULT,
1483 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1484 cmd_show_window, _("plugins: Show the plugins window."), finch_plugins_show_all);
1485 purple_cmd_register("buddylist", "", PURPLE_CMD_P_DEFAULT,
1486 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1487 cmd_show_window, _("buddylist: Show the buddylist."), finch_blist_show);
1488 purple_cmd_register("accounts", "", PURPLE_CMD_P_DEFAULT,
1489 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1490 cmd_show_window, _("accounts: Show the accounts window."), finch_accounts_show_all);
1491 purple_cmd_register("debugwin", "", PURPLE_CMD_P_DEFAULT,
1492 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1493 cmd_show_window, _("debugwin: Show the debug window."), finch_debug_window_show);
1494 purple_cmd_register("prefs", "", PURPLE_CMD_P_DEFAULT,
1495 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1496 cmd_show_window, _("prefs: Show the preference window."), finch_prefs_show_all);
1497 purple_cmd_register("status", "", PURPLE_CMD_P_DEFAULT,
1498 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1499 cmd_show_window, _("statuses: Show the savedstatuses window."), finch_savedstatus_show_all);
1501 /* Allow customizing the message colors using a command during run-time */
1502 purple_cmd_register("msgcolor", "www", PURPLE_CMD_P_DEFAULT,
1503 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1504 cmd_message_color, _("msgcolor &lt;class&gt; &lt;foreground&gt; &lt;background&gt;: "
1505 "Set the color for different classes of messages in the conversation window.<br>"
1506 " &lt;class&gt;: receive, send, highlight, action, timestamp<br>"
1507 " &lt;foreground/background&gt;: black, red, green, blue, white, gray, darkgray, magenta, cyan, default<br><br>"
1508 "EXAMPLE:<br> msgcolor send cyan default"),
1509 NULL);
1510 purple_cmd_register("msgcolour", "www", PURPLE_CMD_P_DEFAULT,
1511 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1512 cmd_message_color, _("msgcolor &lt;class&gt; &lt;foreground&gt; &lt;background&gt;: "
1513 "Set the color for different classes of messages in the conversation window.<br>"
1514 " &lt;class&gt;: receive, send, highlight, action, timestamp<br>"
1515 " &lt;foreground/background&gt;: black, red, green, blue, white, gray, darkgray, magenta, cyan, default<br><br>"
1516 "EXAMPLE:<br> msgcolor send cyan default"),
1517 NULL);
1519 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", finch_conv_get_handle(),
1520 PURPLE_CALLBACK(update_buddy_typing), NULL);
1521 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", finch_conv_get_handle(),
1522 PURPLE_CALLBACK(update_buddy_typing), NULL);
1523 purple_signal_connect(purple_conversations_get_handle(), "chat-left", finch_conv_get_handle(),
1524 PURPLE_CALLBACK(chat_left_cb), NULL);
1525 purple_signal_connect(purple_conversations_get_handle(), "cleared-message-history", finch_conv_get_handle(),
1526 PURPLE_CALLBACK(cleared_message_history_cb), NULL);
1527 purple_signal_connect(purple_conversations_get_handle(), "conversation-updated", finch_conv_get_handle(),
1528 PURPLE_CALLBACK(conv_updated), NULL);
1529 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", finch_conv_get_handle(),
1530 PURPLE_CALLBACK(buddy_signed_on_off), NULL);
1531 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", finch_conv_get_handle(),
1532 PURPLE_CALLBACK(buddy_signed_on_off), NULL);
1533 purple_signal_connect(purple_connections_get_handle(), "signed-on", finch_conv_get_handle(),
1534 PURPLE_CALLBACK(account_signed_on_off), NULL);
1535 purple_signal_connect(purple_connections_get_handle(), "signed-off", finch_conv_get_handle(),
1536 PURPLE_CALLBACK(account_signed_on_off), NULL);
1537 purple_signal_connect(purple_connections_get_handle(), "signing-off", finch_conv_get_handle(),
1538 PURPLE_CALLBACK(account_signing_off), NULL);
1541 void finch_conversation_uninit()
1543 purple_signals_disconnect_by_handle(finch_conv_get_handle());
1546 void finch_conversation_set_active(PurpleConversation *conv)
1548 FinchConv *ggconv = FINCH_CONV(conv);
1549 PurpleAccount *account;
1550 char *title;
1552 g_return_if_fail(ggconv);
1553 g_return_if_fail(g_list_find(ggconv->list, conv));
1554 if (ggconv->active_conv == conv)
1555 return;
1557 ggconv->active_conv = conv;
1558 gg_setup_commands(ggconv, TRUE);
1559 account = purple_conversation_get_account(conv);
1560 title = get_conversation_title(conv, account);
1561 gnt_screen_rename_widget(ggconv->window, title);
1562 g_free(title);
1564 generate_e2ee_menu(ggconv);
1567 void finch_conversation_set_info_widget(PurpleConversation *conv, GntWidget *widget)
1569 FinchConv *fc = FINCH_CONV(conv);
1570 int height, width;
1572 gnt_box_remove_all(GNT_BOX(fc->info));
1574 if (widget) {
1575 gnt_box_add_widget(GNT_BOX(fc->info), widget);
1576 gnt_box_readjust(GNT_BOX(fc->info));
1579 gnt_widget_get_size(fc->window, &width, &height);
1580 gnt_box_readjust(GNT_BOX(fc->window));
1581 gnt_screen_resize_widget(fc->window, width, height);
1582 gnt_box_give_focus_to_child(GNT_BOX(fc->window), fc->entry);