mark PurpleImageClass as private
[pidgin-git.git] / pidgin / gtkblist.c
blob2b570f6e520c6a54abd478bf481b17ad92a35e2e
1 /* pidgin
3 * Pidgin 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 "pidgin.h"
25 #include "account.h"
26 #include "action.h"
27 #include "connection.h"
28 #include "core.h"
29 #include "debug.h"
30 #include "notify.h"
31 #include "protocol.h"
32 #include "prefs.h"
33 #include "plugins.h"
34 #include "request.h"
35 #include "signals.h"
36 #include "pidginstock.h"
37 #include "theme-loader.h"
38 #include "theme-manager.h"
39 #include "util.h"
41 #include "gtkaccount.h"
42 #include "gtkblist.h"
43 #include "gtkcellrendererexpander.h"
44 #include "gtkconv.h"
45 #include "gtkdialogs.h"
46 #include "gtkxfer.h"
47 #include "gtkmenutray.h"
48 #include "gtkpounce.h"
49 #include "gtkplugin.h"
50 #include "gtkprefs.h"
51 #include "gtkprivacy.h"
52 #include "gtkroomlist.h"
53 #include "gtkstatusbox.h"
54 #include "gtkscrollbook.h"
55 #include "gtksmiley-manager.h"
56 #include "gtkstyle.h"
57 #include "gtkblist-theme.h"
58 #include "gtkblist-theme-loader.h"
59 #include "gtkutils.h"
60 #include "pidgin/minidialog.h"
61 #include "pidgin/pidginabout.h"
62 #include "pidgin/pidginaccountchooser.h"
63 #include "pidgin/pidgindebug.h"
64 #include "pidgin/pidgindebugplugininfo.h"
65 #include "pidgin/pidgingdkpixbuf.h"
66 #include "pidgin/pidginlog.h"
67 #include "pidgin/pidgintooltip.h"
69 #include <gdk/gdkkeysyms.h>
70 #include <gtk/gtk.h>
71 #include <gdk/gdk.h>
73 #include "gtk3compat.h"
75 typedef struct
77 PurpleAccount *account;
78 GtkWidget *window;
79 GtkBox *vbox;
80 GtkWidget *account_menu;
81 GtkSizeGroup *sg;
82 } PidginBlistRequestData;
84 typedef struct
86 PidginBlistRequestData rq_data;
87 GtkWidget *combo;
88 GtkWidget *entry;
89 GtkWidget *entry_for_alias;
90 GtkWidget *entry_for_invite;
92 } PidginAddBuddyData;
94 typedef struct
96 PidginBlistRequestData rq_data;
97 gchar *default_chat_name;
98 GList *entries;
99 } PidginChatData;
101 typedef struct
103 PidginChatData chat_data;
105 GtkWidget *alias_entry;
106 GtkWidget *group_combo;
107 GtkWidget *autojoin;
108 GtkWidget *persistent;
109 } PidginAddChatData;
111 typedef struct
113 /* GBoxed reference count */
114 int box_count;
116 /* Used to hold error minidialogs. Gets packed
117 * inside PidginBuddyList.error_buttons
119 PidginScrollBook *error_scrollbook;
121 /* Pointer to the mini-dialog about having signed on elsewhere, if one
122 * is showing; %NULL otherwise.
124 PidginMiniDialog *signed_on_elsewhere;
126 PidginBlistTheme *current_theme;
128 guint select_notebook_page_timeout;
131 } PidginBuddyListPrivate;
133 G_DEFINE_TYPE_WITH_PRIVATE(PidginBuddyList, pidgin_buddy_list,
134 PURPLE_TYPE_BUDDY_LIST)
136 #define PIDGIN_WINDOW_ICONIFIED(x) \
137 (gdk_window_get_state(gtk_widget_get_window(GTK_WIDGET(x))) & \
138 GDK_WINDOW_STATE_ICONIFIED)
140 #define PIDGIN_WINDOW_MAXIMIZED(x) \
141 (gdk_window_get_state(gtk_widget_get_window(GTK_WIDGET(x))) & \
142 GDK_WINDOW_STATE_MAXIMIZED)
144 static GtkWidget *accountmenu = NULL;
146 static guint visibility_manager_count = 0;
147 static GdkVisibilityState gtk_blist_visibility = GDK_VISIBILITY_UNOBSCURED;
148 static gboolean gtk_blist_focused = FALSE;
149 static gboolean editing_blist = FALSE;
151 static GList *pidgin_blist_sort_methods = NULL;
152 static struct _PidginBlistSortMethod *current_sort_method = NULL;
153 static void sort_method_none(PurpleBlistNode *node, PurpleBuddyList *blist, GtkTreeIter groupiter, GtkTreeIter *cur, GtkTreeIter *iter);
155 static void sort_method_alphabetical(PurpleBlistNode *node, PurpleBuddyList *blist, GtkTreeIter groupiter, GtkTreeIter *cur, GtkTreeIter *iter);
156 static void sort_method_status(PurpleBlistNode *node, PurpleBuddyList *blist, GtkTreeIter groupiter, GtkTreeIter *cur, GtkTreeIter *iter);
157 static void sort_method_log_activity(PurpleBlistNode *node, PurpleBuddyList *blist, GtkTreeIter groupiter, GtkTreeIter *cur, GtkTreeIter *iter);
158 static guint sort_merge_id;
159 static GtkActionGroup *sort_action_group = NULL;
161 static PidginBuddyList *gtkblist = NULL;
163 static GList *groups_tree(void);
164 static gboolean pidgin_blist_refresh_timer(PurpleBuddyList *list);
165 static void pidgin_blist_update_buddy(PurpleBuddyList *list, PurpleBlistNode *node, gboolean status_change);
166 static void pidgin_blist_selection_changed(GtkTreeSelection *selection, gpointer data);
167 static void pidgin_blist_update(PurpleBuddyList *list, PurpleBlistNode *node);
168 static void pidgin_blist_update_group(PurpleBuddyList *list, PurpleBlistNode *node);
169 static void pidgin_blist_update_contact(PurpleBuddyList *list, PurpleBlistNode *node);
170 static char *pidgin_get_tooltip_text(PurpleBlistNode *node, gboolean full);
171 static gboolean get_iter_from_node(PurpleBlistNode *node, GtkTreeIter *iter);
172 static gboolean buddy_is_displayable(PurpleBuddy *buddy);
173 static void redo_buddy_list(PurpleBuddyList *list, gboolean remove, gboolean rerender);
174 static void pidgin_blist_collapse_contact_cb(GtkWidget *w, PurpleBlistNode *node);
175 static char *pidgin_get_group_title(PurpleBlistNode *gnode, gboolean expanded);
176 static void pidgin_blist_expand_contact_cb(GtkWidget *w, PurpleBlistNode *node);
177 static void set_urgent(void);
179 typedef enum {
180 PIDGIN_BLIST_NODE_HAS_PENDING_MESSAGE = 1 << 0, /* Whether there's pending message in a conversation */
181 PIDGIN_BLIST_CHAT_HAS_PENDING_MESSAGE_WITH_NICK = 1 << 1, /* Whether there's a pending message in a chat that mentions our nick */
182 } PidginBlistNodeFlags;
184 typedef struct {
185 GtkTreeRowReference *row;
186 gboolean contact_expanded;
187 gboolean recent_signonoff;
188 gint recent_signonoff_timer;
189 struct {
190 PurpleConversation *conv;
191 PidginBlistNodeFlags flags;
192 } conv;
193 } PidginBlistNode;
195 /***************************************************
196 * Callbacks *
197 ***************************************************/
198 static gboolean gtk_blist_visibility_cb(GtkWidget *w, GdkEventVisibility *event, gpointer data)
200 GdkVisibilityState old_state = gtk_blist_visibility;
201 gtk_blist_visibility = event->state;
203 if (gtk_blist_visibility == GDK_VISIBILITY_FULLY_OBSCURED &&
204 old_state != GDK_VISIBILITY_FULLY_OBSCURED) {
206 /* no longer fully obscured */
207 pidgin_blist_refresh_timer(purple_blist_get_default());
210 /* continue to handle event normally */
211 return FALSE;
214 static gboolean gtk_blist_window_state_cb(GtkWidget *w, GdkEventWindowState *event, gpointer data)
216 if(event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN) {
217 if(event->new_window_state & GDK_WINDOW_STATE_WITHDRAWN)
218 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/blist/list_visible", FALSE);
219 else {
220 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/blist/list_visible", TRUE);
221 pidgin_blist_refresh_timer(purple_blist_get_default());
225 if(event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED) {
226 if(event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED)
227 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/blist/list_maximized", TRUE);
228 else
229 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/blist/list_maximized", FALSE);
232 /* Refresh gtkblist if un-iconifying */
233 if (event->changed_mask & GDK_WINDOW_STATE_ICONIFIED){
234 if (!(event->new_window_state & GDK_WINDOW_STATE_ICONIFIED))
235 pidgin_blist_refresh_timer(purple_blist_get_default());
238 return FALSE;
241 static gboolean gtk_blist_delete_cb(GtkWidget *w, GdkEventAny *event, gpointer data)
243 if(visibility_manager_count)
244 purple_blist_set_visible(FALSE);
245 else
246 purple_core_quit();
248 /* we handle everything, event should not propogate further */
249 return TRUE;
252 static void
253 gtk_blist_hide_cb(GtkWidget *widget, PidginBuddyList *gtkblist)
255 purple_signal_emit(pidgin_blist_get_handle(),
256 "gtkblist-hiding", gtkblist);
259 static void
260 gtk_blist_show_cb(GtkWidget *widget, PidginBuddyList *gtkblist)
262 purple_signal_emit(pidgin_blist_get_handle(),
263 "gtkblist-unhiding", gtkblist);
266 static void
267 gtk_blist_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
268 gpointer data)
270 int new_width;
271 int new_height;
273 /* ignore changes when maximized */
274 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/list_maximized")) {
275 return;
278 gtk_window_get_size(GTK_WINDOW(widget), &new_width, &new_height);
280 /* store the size */
281 purple_prefs_set_int(PIDGIN_PREFS_ROOT "/blist/width", new_width);
282 purple_prefs_set_int(PIDGIN_PREFS_ROOT "/blist/height", new_height);
285 static void gtk_blist_menu_info_cb(GtkWidget *w, PurpleBuddy *b)
287 PurpleAccount *account = purple_buddy_get_account(b);
289 pidgin_retrieve_user_info(purple_account_get_connection(account),
290 purple_buddy_get_name(b));
293 static void gtk_blist_menu_im_cb(GtkWidget *w, PurpleBuddy *b)
295 pidgin_dialogs_im_with_user(purple_buddy_get_account(b),
296 purple_buddy_get_name(b));
299 #ifdef USE_VV
300 static void gtk_blist_menu_audio_call_cb(GtkWidget *w, PurpleBuddy *b)
302 purple_protocol_initiate_media(purple_buddy_get_account(b),
303 purple_buddy_get_name(b), PURPLE_MEDIA_AUDIO);
306 static void gtk_blist_menu_video_call_cb(GtkWidget *w, PurpleBuddy *b)
308 /* if the buddy supports both audio and video, start a combined call,
309 otherwise start a pure video session */
310 if (purple_protocol_get_media_caps(purple_buddy_get_account(b),
311 purple_buddy_get_name(b)) &
312 PURPLE_MEDIA_CAPS_AUDIO_VIDEO) {
313 purple_protocol_initiate_media(purple_buddy_get_account(b),
314 purple_buddy_get_name(b), PURPLE_MEDIA_AUDIO | PURPLE_MEDIA_VIDEO);
315 } else {
316 purple_protocol_initiate_media(purple_buddy_get_account(b),
317 purple_buddy_get_name(b), PURPLE_MEDIA_VIDEO);
321 #endif
323 static void gtk_blist_menu_send_file_cb(GtkWidget *w, PurpleBuddy *b)
325 PurpleAccount *account = purple_buddy_get_account(b);
327 purple_serv_send_file(purple_account_get_connection(account),
328 purple_buddy_get_name(b), NULL);
331 static void gtk_blist_menu_move_to_cb(GtkWidget *w, PurpleBlistNode *node)
333 PurpleGroup *group = g_object_get_data(G_OBJECT(w), "groupnode");
334 purple_blist_add_contact((PurpleContact *)node, group, NULL);
338 static void gtk_blist_menu_autojoin_cb(GtkWidget *w, PurpleChat *chat)
340 purple_blist_node_set_bool(PURPLE_BLIST_NODE(chat), "gtk-autojoin",
341 gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w)));
344 static void gtk_blist_menu_persistent_cb(GtkWidget *w, PurpleChat *chat)
346 purple_blist_node_set_bool(PURPLE_BLIST_NODE(chat), "gtk-persistent",
347 gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w)));
350 static PurpleConversation *
351 find_conversation_with_buddy(PurpleBuddy *buddy)
353 PidginBlistNode *ui = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(buddy));
354 if (ui)
355 return ui->conv.conv;
356 return PURPLE_CONVERSATION(purple_conversations_find_im_with_account(
357 purple_buddy_get_name(buddy), purple_buddy_get_account(buddy)));
360 static void gtk_blist_join_chat(PurpleChat *chat)
362 PurpleAccount *account;
363 PurpleConversation *conv;
364 PurpleProtocol *protocol;
365 GHashTable *components;
366 const char *name;
367 char *chat_name = NULL;
369 account = purple_chat_get_account(chat);
370 protocol = purple_protocols_find(purple_account_get_protocol_id(account));
372 components = purple_chat_get_components(chat);
374 if (protocol)
375 chat_name = purple_protocol_chat_iface_get_name(protocol, components);
377 if (chat_name)
378 name = chat_name;
379 else
380 name = purple_chat_get_name(chat);
382 conv = PURPLE_CONVERSATION(purple_conversations_find_chat_with_account(name,
383 account));
385 if (conv != NULL) {
386 pidgin_conv_attach_to_conversation(conv);
387 purple_conversation_present(conv);
390 purple_serv_join_chat(purple_account_get_connection(account), components);
391 g_free(chat_name);
394 static void gtk_blist_menu_join_cb(GtkWidget *w, PurpleChat *chat)
396 gtk_blist_join_chat(chat);
399 static void gtk_blist_renderer_editing_cancelled_cb(GtkCellRenderer *renderer, PurpleBuddyList *list)
401 editing_blist = FALSE;
402 g_object_set(G_OBJECT(renderer), "editable", FALSE, NULL);
403 pidgin_blist_refresh(list);
406 static void gtk_blist_renderer_editing_started_cb(GtkCellRenderer *renderer,
407 GtkCellEditable *editable,
408 gchar *path_str,
409 gpointer user_data)
411 PidginBuddyList *gtkblist = PIDGIN_BUDDY_LIST(user_data);
412 GtkTreeIter iter;
413 GtkTreePath *path = NULL;
414 PurpleBlistNode *node;
415 const char *text = NULL;
417 path = gtk_tree_path_new_from_string (path_str);
418 gtk_tree_model_get_iter (GTK_TREE_MODEL(gtkblist->treemodel), &iter, path);
419 gtk_tree_path_free (path);
420 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
422 if (PURPLE_IS_CONTACT(node))
423 text = purple_contact_get_alias(PURPLE_CONTACT(node));
424 else if (PURPLE_IS_BUDDY(node))
425 text = purple_buddy_get_alias(PURPLE_BUDDY(node));
426 else if (PURPLE_IS_GROUP(node))
427 text = purple_group_get_name(PURPLE_GROUP(node));
428 else if (PURPLE_IS_CHAT(node))
429 text = purple_chat_get_name(PURPLE_CHAT(node));
430 else
431 g_return_if_reached();
433 if (GTK_IS_ENTRY (editable)) {
434 GtkEntry *entry = GTK_ENTRY (editable);
435 gtk_entry_set_text(entry, text);
437 editing_blist = TRUE;
440 static void
441 gtk_blist_do_personize(GList *merges)
443 PurpleBlistNode *contact = NULL;
444 int max = 0;
445 GList *tmp;
447 /* First, we find the contact to merge the rest of the buddies into.
448 * This will be the contact with the most buddies in it; ties are broken
449 * by which contact is higher in the list
451 for (tmp = merges; tmp; tmp = tmp->next) {
452 PurpleBlistNode *node = tmp->data;
453 PurpleBlistNode *b;
454 int i = 0;
456 if (PURPLE_IS_BUDDY(node))
457 node = purple_blist_node_get_parent(node);
459 if (!PURPLE_IS_CONTACT(node))
460 continue;
462 for (b = purple_blist_node_get_first_child(node);
464 b = purple_blist_node_get_sibling_next(b))
466 i++;
469 if (i > max) {
470 contact = node;
471 max = i;
475 if (contact == NULL)
476 return;
478 /* Merge all those buddies into this contact */
479 for (tmp = merges; tmp; tmp = tmp->next) {
480 PurpleBlistNode *node = tmp->data;
481 if (PURPLE_IS_BUDDY(node))
482 node = purple_blist_node_get_parent(node);
484 if (node == contact)
485 continue;
487 purple_contact_merge((PurpleContact *)node, contact);
490 /* And show the expanded contact, so the people know what's going on */
491 pidgin_blist_expand_contact_cb(NULL, contact);
492 g_list_free(merges);
495 static void
496 gtk_blist_auto_personize(PurpleBlistNode *group, const char *alias)
498 PurpleBlistNode *contact;
499 PurpleBlistNode *buddy;
500 GList *merges = NULL;
501 int i = 0;
502 char *a = g_utf8_casefold(alias, -1);
504 for (contact = purple_blist_node_get_first_child(group);
505 contact != NULL;
506 contact = purple_blist_node_get_sibling_next(contact)) {
507 char *node_alias;
508 if (!PURPLE_IS_CONTACT(contact))
509 continue;
511 node_alias = g_utf8_casefold(purple_contact_get_alias((PurpleContact *)contact), -1);
512 if (node_alias && !g_utf8_collate(node_alias, a)) {
513 merges = g_list_append(merges, contact);
514 i++;
515 g_free(node_alias);
516 continue;
518 g_free(node_alias);
520 for (buddy = purple_blist_node_get_first_child(contact);
521 buddy;
522 buddy = purple_blist_node_get_sibling_next(buddy))
524 if (!PURPLE_IS_BUDDY(buddy))
525 continue;
527 node_alias = g_utf8_casefold(purple_buddy_get_alias(PURPLE_BUDDY(buddy)), -1);
528 if (node_alias && !g_utf8_collate(node_alias, a)) {
529 merges = g_list_append(merges, buddy);
530 i++;
531 g_free(node_alias);
532 break;
534 g_free(node_alias);
537 g_free(a);
539 if (i > 1)
541 char *msg = g_strdup_printf(ngettext("You have %d contact named %s. Would you like to merge them?", "You currently have %d contacts named %s. Would you like to merge them?", i), i, alias);
542 purple_request_action(NULL, NULL, msg, _("Merging these contacts will cause them to share a single entry on the buddy list and use a single conversation window. "
543 "You can separate them again by choosing 'Expand' from the contact's context menu"), 0, NULL,
544 merges, 2, _("_Yes"), PURPLE_CALLBACK(gtk_blist_do_personize), _("_No"), PURPLE_CALLBACK(g_list_free));
545 g_free(msg);
546 } else
547 g_list_free(merges);
550 static void gtk_blist_renderer_edited_cb(GtkCellRendererText *text_rend, char *arg1,
551 char *arg2, PurpleBuddyList *list)
553 PidginBuddyList *gtkblist = PIDGIN_BUDDY_LIST(list);
554 GtkTreeIter iter;
555 GtkTreePath *path;
556 PurpleBlistNode *node;
557 PurpleGroup *dest;
558 gchar *alias = NULL;
560 editing_blist = FALSE;
561 path = gtk_tree_path_new_from_string (arg1);
562 gtk_tree_model_get_iter (GTK_TREE_MODEL(gtkblist->treemodel), &iter, path);
563 gtk_tree_path_free (path);
564 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
565 gtk_tree_view_set_enable_search (GTK_TREE_VIEW(gtkblist->treeview), TRUE);
566 g_object_set(G_OBJECT(gtkblist->text_rend), "editable", FALSE, NULL);
568 if (PURPLE_IS_CONTACT(node)) {
569 PurpleContact *contact = PURPLE_CONTACT(node);
570 PidginBlistNode *gtknode =
571 (PidginBlistNode *)purple_blist_node_get_ui_data(node);
574 * Using purple_contact_get_alias here breaks because we
575 * specifically want to check the contact alias only (i.e. not
576 * the priority buddy, which purple_contact_get_alias does).
577 * The "alias" GObject property gives us just the alias.
579 g_object_get(contact, "alias", &alias, NULL);
581 if (alias || gtknode->contact_expanded) {
582 purple_contact_set_alias(contact, arg2);
583 gtk_blist_auto_personize(purple_blist_node_get_parent(node), arg2);
584 } else {
585 PurpleBuddy *buddy = purple_contact_get_priority_buddy(contact);
586 purple_buddy_set_local_alias(buddy, arg2);
587 purple_serv_alias_buddy(buddy);
588 gtk_blist_auto_personize(purple_blist_node_get_parent(node), arg2);
590 } else if (PURPLE_IS_BUDDY(node)) {
591 PurpleGroup *group = purple_buddy_get_group(PURPLE_BUDDY(node));
593 purple_buddy_set_local_alias(PURPLE_BUDDY(node), arg2);
594 purple_serv_alias_buddy(PURPLE_BUDDY(node));
595 gtk_blist_auto_personize(PURPLE_BLIST_NODE(group), arg2);
596 } else if (PURPLE_IS_GROUP(node)) {
597 dest = purple_blist_find_group(arg2);
598 if (dest != NULL && purple_utf8_strcasecmp(arg2, purple_group_get_name(PURPLE_GROUP(node)))) {
599 pidgin_dialogs_merge_groups(PURPLE_GROUP(node), arg2);
600 } else {
601 purple_group_set_name(PURPLE_GROUP(node), arg2);
603 } else if (PURPLE_IS_CHAT(node)) {
604 purple_chat_set_alias(PURPLE_CHAT(node), arg2);
607 g_free(alias);
608 pidgin_blist_refresh(list);
611 static void
612 chat_components_edit_ok(PurpleChat *chat, PurpleRequestFields *allfields)
614 GList *groups, *fields;
616 for (groups = purple_request_fields_get_groups(allfields); groups; groups = groups->next) {
617 fields = purple_request_field_group_get_fields(groups->data);
618 for (; fields; fields = fields->next) {
619 PurpleRequestField *field = fields->data;
620 const char *id;
621 char *val;
623 id = purple_request_field_get_id(field);
624 if (purple_request_field_get_field_type(field) == PURPLE_REQUEST_FIELD_INTEGER)
625 val = g_strdup_printf("%d", purple_request_field_int_get_value(field));
626 else
627 val = g_strdup(purple_request_field_string_get_value(field));
629 if (!val) {
630 g_hash_table_remove(purple_chat_get_components(chat), id);
631 } else {
632 g_hash_table_replace(purple_chat_get_components(chat), g_strdup(id), val); /* val should not be free'd */
638 static void chat_components_edit(GtkWidget *w, PurpleBlistNode *node)
640 PurpleRequestFields *fields = purple_request_fields_new();
641 PurpleRequestFieldGroup *group = purple_request_field_group_new(NULL);
642 PurpleRequestField *field;
643 GList *parts, *iter;
644 PurpleProtocolChatEntry *pce;
645 PurpleConnection *gc;
646 PurpleChat *chat = (PurpleChat*)node;
648 purple_request_fields_add_group(fields, group);
650 gc = purple_account_get_connection(purple_chat_get_account(chat));
651 parts = purple_protocol_chat_iface_info(purple_connection_get_protocol(gc), gc);
653 for (iter = parts; iter; iter = iter->next) {
654 pce = iter->data;
655 if (pce->is_int) {
656 int val;
657 const char *str = g_hash_table_lookup(purple_chat_get_components(chat), pce->identifier);
658 if (!str || sscanf(str, "%d", &val) != 1)
659 val = pce->min;
660 field = purple_request_field_int_new(pce->identifier, pce->label, val, INT_MIN, INT_MAX);
661 } else {
662 field = purple_request_field_string_new(pce->identifier, pce->label,
663 g_hash_table_lookup(purple_chat_get_components(chat), pce->identifier), FALSE);
664 if (pce->secret)
665 purple_request_field_string_set_masked(field, TRUE);
668 if (pce->required)
669 purple_request_field_set_required(field, TRUE);
671 purple_request_field_group_add_field(group, field);
672 g_free(pce);
675 g_list_free(parts);
677 purple_request_fields(NULL, _("Edit Chat"), NULL, _("Please update the necessary fields."),
678 fields, _("Save"), G_CALLBACK(chat_components_edit_ok), _("Cancel"), NULL,
679 NULL, chat);
682 static void gtk_blist_menu_alias_cb(GtkWidget *w, PurpleBlistNode *node)
684 GtkTreeIter iter;
685 GtkTreePath *path;
687 if (!(get_iter_from_node(node, &iter))) {
688 /* This is either a bug, or the buddy is in a collapsed contact */
689 node = purple_blist_node_get_parent(node);
690 if (!get_iter_from_node(node, &iter))
691 /* Now it's definitely a bug */
692 return;
695 pidgin_blist_tooltip_destroy();
697 path = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel), &iter);
698 g_object_set(G_OBJECT(gtkblist->text_rend), "editable", TRUE, NULL);
699 gtk_tree_view_set_enable_search (GTK_TREE_VIEW(gtkblist->treeview), FALSE);
700 gtk_widget_grab_focus(gtkblist->treeview);
701 gtk_tree_view_set_cursor_on_cell(GTK_TREE_VIEW(gtkblist->treeview), path,
702 gtkblist->text_column, gtkblist->text_rend, TRUE);
703 gtk_tree_path_free(path);
706 static void gtk_blist_menu_bp_cb(GtkWidget *w, PurpleBuddy *b)
708 pidgin_pounce_editor_show(purple_buddy_get_account(b),
709 purple_buddy_get_name(b), NULL);
712 static void gtk_blist_menu_showlog_cb(GtkWidget *w, PurpleBlistNode *node)
714 PurpleLogType type;
715 PurpleAccount *account;
716 char *name = NULL;
718 pidgin_set_cursor(gtkblist->window, GDK_WATCH);
720 if (PURPLE_IS_BUDDY(node)) {
721 PurpleBuddy *b = (PurpleBuddy*) node;
722 type = PURPLE_LOG_IM;
723 name = g_strdup(purple_buddy_get_name(b));
724 account = purple_buddy_get_account(b);
725 } else if (PURPLE_IS_CHAT(node)) {
726 PurpleChat *c = PURPLE_CHAT(node);
727 PurpleProtocol *protocol = NULL;
728 type = PURPLE_LOG_CHAT;
729 account = purple_chat_get_account(c);
730 protocol = purple_protocols_find(purple_account_get_protocol_id(account));
731 if (protocol) {
732 name = purple_protocol_chat_iface_get_name(protocol, purple_chat_get_components(c));
734 } else if (PURPLE_IS_CONTACT(node)) {
735 pidgin_log_show_contact(PURPLE_CONTACT(node));
736 pidgin_clear_cursor(gtkblist->window);
737 return;
738 } else {
739 pidgin_clear_cursor(gtkblist->window);
741 /* This callback should not have been registered for a node
742 * that doesn't match the type of one of the blocks above. */
743 g_return_if_reached();
746 if (name && account) {
747 pidgin_log_show(type, name, account);
748 pidgin_clear_cursor(gtkblist->window);
751 g_free(name);
754 static void gtk_blist_menu_showoffline_cb(GtkWidget *w, PurpleBlistNode *node)
756 if (PURPLE_IS_BUDDY(node))
758 purple_blist_node_set_bool(node, "show_offline",
759 !purple_blist_node_get_bool(node, "show_offline"));
760 pidgin_blist_update(purple_blist_get_default(), node);
762 else if (PURPLE_IS_CONTACT(node))
764 PurpleBlistNode *bnode;
765 gboolean setting = !purple_blist_node_get_bool(node, "show_offline");
767 purple_blist_node_set_bool(node, "show_offline", setting);
768 for (bnode = purple_blist_node_get_first_child(node);
769 bnode != NULL;
770 bnode = purple_blist_node_get_sibling_next(bnode))
772 purple_blist_node_set_bool(bnode, "show_offline", setting);
773 pidgin_blist_update(purple_blist_get_default(), bnode);
775 } else if (PURPLE_IS_GROUP(node)) {
776 PurpleBlistNode *cnode, *bnode;
777 gboolean setting = !purple_blist_node_get_bool(node, "show_offline");
779 purple_blist_node_set_bool(node, "show_offline", setting);
780 for (cnode = purple_blist_node_get_first_child(node);
781 cnode != NULL;
782 cnode = purple_blist_node_get_sibling_next(cnode))
784 purple_blist_node_set_bool(cnode, "show_offline", setting);
785 for (bnode = purple_blist_node_get_first_child(cnode);
786 bnode != NULL;
787 bnode = purple_blist_node_get_sibling_next(bnode))
789 purple_blist_node_set_bool(bnode, "show_offline", setting);
790 pidgin_blist_update(purple_blist_get_default(),
791 bnode);
797 static void gtk_blist_show_systemlog_cb(void)
799 pidgin_syslog_show();
802 static void gtk_blist_show_onlinehelp_cb(void)
804 purple_notify_uri(NULL, PURPLE_WEBSITE "documentation");
807 static void
808 do_join_chat(PidginChatData *data)
810 if (data)
812 GHashTable *components =
813 g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
814 GList *tmp;
815 PurpleChat *chat;
817 for (tmp = data->entries; tmp != NULL; tmp = tmp->next)
819 if (g_object_get_data(tmp->data, "is_spin"))
821 g_hash_table_replace(components,
822 g_strdup(g_object_get_data(tmp->data, "identifier")),
823 g_strdup_printf("%d",
824 gtk_spin_button_get_value_as_int(tmp->data)));
826 else
828 g_hash_table_replace(components,
829 g_strdup(g_object_get_data(tmp->data, "identifier")),
830 g_strdup(gtk_entry_get_text(tmp->data)));
834 chat = purple_chat_new(data->rq_data.account, NULL, components);
835 gtk_blist_join_chat(chat);
836 purple_blist_remove_chat(chat);
840 static void
841 do_joinchat(GtkWidget *dialog, int id, PidginChatData *info)
843 switch(id)
845 case GTK_RESPONSE_OK:
846 do_join_chat(info);
847 break;
849 case 1:
850 pidgin_roomlist_dialog_show_with_account(info->rq_data.account);
851 return;
853 break;
856 gtk_widget_destroy(GTK_WIDGET(dialog));
857 g_list_free(info->entries);
858 g_free(info);
862 * Check the values of all the text entry boxes. If any required input
863 * strings are empty then don't allow the user to click on "OK."
865 static void
866 set_sensitive_if_input_chat_cb(GtkWidget *entry, gpointer user_data)
868 PurpleProtocol *protocol;
869 PurpleConnection *gc;
870 PidginChatData *data;
871 GList *tmp;
872 const char *text;
873 gboolean required;
874 gboolean sensitive = TRUE;
876 data = user_data;
878 for (tmp = data->entries; tmp != NULL; tmp = tmp->next)
880 if (!g_object_get_data(tmp->data, "is_spin"))
882 required = GPOINTER_TO_INT(g_object_get_data(tmp->data, "required"));
883 text = gtk_entry_get_text(tmp->data);
884 if (required && (*text == '\0'))
885 sensitive = FALSE;
889 gtk_dialog_set_response_sensitive(GTK_DIALOG(data->rq_data.window), GTK_RESPONSE_OK, sensitive);
891 gc = purple_account_get_connection(data->rq_data.account);
892 protocol = (gc != NULL) ? purple_connection_get_protocol(gc) : NULL;
893 sensitive = (protocol != NULL && PURPLE_PROTOCOL_IMPLEMENTS(protocol, ROOMLIST, get_list));
895 gtk_dialog_set_response_sensitive(GTK_DIALOG(data->rq_data.window), 1, sensitive);
898 static void
899 set_sensitive_if_input_buddy_cb(GtkWidget *entry, gpointer user_data)
901 PurpleProtocol *protocol;
902 PidginAddBuddyData *data = user_data;
903 const char *text;
905 protocol = purple_protocols_find(purple_account_get_protocol_id(
906 data->rq_data.account));
907 text = gtk_entry_get_text(GTK_ENTRY(entry));
909 gtk_dialog_set_response_sensitive(GTK_DIALOG(data->rq_data.window),
910 GTK_RESPONSE_OK, purple_validate(protocol, text));
913 static void
914 pidgin_blist_update_privacy_cb(PurpleBuddy *buddy)
916 PidginBlistNode *ui_data = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(buddy));
917 if (ui_data == NULL || ui_data->row == NULL)
918 return;
919 pidgin_blist_update_buddy(purple_blist_get_default(),
920 PURPLE_BLIST_NODE(buddy), TRUE);
923 static gboolean
924 add_buddy_account_filter_func(PurpleAccount *account)
926 PurpleConnection *gc = purple_account_get_connection(account);
927 PurpleProtocol *protocol = purple_connection_get_protocol(gc);
929 return PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, add_buddy);
932 static gboolean
933 chat_account_filter_func(PurpleAccount *account)
935 PurpleConnection *gc = purple_account_get_connection(account);
936 PurpleProtocol *protocol = NULL;
938 if (gc == NULL)
939 return FALSE;
941 protocol = purple_connection_get_protocol(gc);
943 return (PURPLE_PROTOCOL_IMPLEMENTS(protocol, CHAT, info));
946 gboolean
947 pidgin_blist_joinchat_is_showable()
949 GList *c;
950 PurpleConnection *gc;
952 for (c = purple_connections_get_all(); c != NULL; c = c->next) {
953 gc = c->data;
955 if (chat_account_filter_func(purple_connection_get_account(gc)))
956 return TRUE;
959 return FALSE;
962 static GtkWidget *
963 make_blist_request_dialog(PidginBlistRequestData *data, PurpleAccount *account,
964 const char *title, const char *window_role, const char *label_text,
965 GCallback callback_func, PurpleFilterAccountFunc filter_func,
966 GCallback response_cb)
968 GtkWidget *label;
969 GtkWidget *img;
970 GtkWidget *hbox;
971 GtkWidget *vbox;
972 GtkWindow *blist_window;
973 PidginBuddyList *gtkblist;
975 data->account = account;
977 img = gtk_image_new_from_icon_name("dialog-question",
978 GTK_ICON_SIZE_DIALOG);
980 gtkblist = PIDGIN_BUDDY_LIST(purple_blist_get_default());
981 blist_window = gtkblist ? GTK_WINDOW(gtkblist->window) : NULL;
983 data->window = gtk_dialog_new();
984 gtk_window_set_title(GTK_WINDOW(data->window), title);
985 gtk_window_set_transient_for(GTK_WINDOW(data->window), blist_window);
986 gtk_dialog_set_default_response(GTK_DIALOG(data->window), GTK_RESPONSE_OK);
987 gtk_container_set_border_width(GTK_CONTAINER(data->window), PIDGIN_HIG_BOX_SPACE);
988 gtk_window_set_resizable(GTK_WINDOW(data->window), FALSE);
989 gtk_box_set_spacing(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(data->window))),
990 PIDGIN_HIG_BORDER);
991 gtk_container_set_border_width(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(data->window))),
992 PIDGIN_HIG_BOX_SPACE);
993 gtk_window_set_role(GTK_WINDOW(data->window), window_role);
995 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PIDGIN_HIG_BORDER);
996 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(data->window))),
997 hbox);
998 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
999 gtk_widget_set_halign(img, GTK_ALIGN_START);
1000 gtk_widget_set_valign(img, GTK_ALIGN_START);
1002 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
1003 gtk_container_add(GTK_CONTAINER(hbox), vbox);
1005 label = gtk_label_new(label_text);
1007 gtk_widget_set_size_request(label, 400, -1);
1008 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
1009 gtk_label_set_xalign(GTK_LABEL(label), 0);
1010 gtk_label_set_yalign(GTK_LABEL(label), 0);
1011 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1013 data->sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1015 data->account_menu = pidgin_account_chooser_new(account, FALSE);
1016 pidgin_account_chooser_set_filter_func(
1017 PIDGIN_ACCOUNT_CHOOSER(data->account_menu), filter_func);
1018 pidgin_add_widget_to_vbox(GTK_BOX(vbox), _("A_ccount"), data->sg, data->account_menu, TRUE, NULL);
1019 g_signal_connect(data->account_menu, "changed",
1020 G_CALLBACK(callback_func), data);
1022 data->vbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 5));
1023 gtk_container_set_border_width(GTK_CONTAINER(data->vbox), 0);
1024 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(data->vbox), FALSE, FALSE, 0);
1026 g_signal_connect(G_OBJECT(data->window), "response", response_cb, data);
1028 g_object_unref(data->sg);
1030 return vbox;
1033 static void
1034 rebuild_chat_entries(PidginChatData *data, const char *default_chat_name)
1036 PurpleConnection *gc;
1037 PurpleProtocol *protocol;
1038 GList *list = NULL, *tmp;
1039 GHashTable *defaults = NULL;
1040 PurpleProtocolChatEntry *pce;
1041 gboolean focus = TRUE;
1043 g_return_if_fail(data->rq_data.account != NULL);
1045 gc = purple_account_get_connection(data->rq_data.account);
1046 protocol = purple_connection_get_protocol(gc);
1048 gtk_container_foreach(GTK_CONTAINER(data->rq_data.vbox), (GtkCallback)gtk_widget_destroy, NULL);
1050 g_list_free(data->entries);
1051 data->entries = NULL;
1053 list = purple_protocol_chat_iface_info(protocol, gc);
1054 defaults = purple_protocol_chat_iface_info_defaults(protocol, gc, default_chat_name);
1056 for (tmp = list; tmp; tmp = tmp->next)
1058 GtkWidget *input;
1060 pce = tmp->data;
1062 if (pce->is_int)
1064 GtkAdjustment *adjust;
1065 adjust = GTK_ADJUSTMENT(gtk_adjustment_new(pce->min,
1066 pce->min, pce->max,
1067 1, 10, 10));
1068 input = gtk_spin_button_new(adjust, 1, 0);
1069 gtk_widget_set_size_request(input, 50, -1);
1070 pidgin_add_widget_to_vbox(GTK_BOX(data->rq_data.vbox), pce->label, data->rq_data.sg, input, FALSE, NULL);
1072 else
1074 char *value;
1075 input = gtk_entry_new();
1076 gtk_entry_set_activates_default(GTK_ENTRY(input), TRUE);
1077 value = g_hash_table_lookup(defaults, pce->identifier);
1078 if (value != NULL)
1079 gtk_entry_set_text(GTK_ENTRY(input), value);
1080 if (pce->secret)
1082 gtk_entry_set_visibility(GTK_ENTRY(input), FALSE);
1084 pidgin_add_widget_to_vbox(data->rq_data.vbox, pce->label, data->rq_data.sg, input, TRUE, NULL);
1085 g_signal_connect(G_OBJECT(input), "changed",
1086 G_CALLBACK(set_sensitive_if_input_chat_cb), data);
1089 /* Do the following for any type of input widget */
1090 if (focus)
1092 gtk_widget_grab_focus(input);
1093 focus = FALSE;
1095 g_object_set_data(G_OBJECT(input), "identifier", (gpointer)pce->identifier);
1096 g_object_set_data(G_OBJECT(input), "is_spin", GINT_TO_POINTER(pce->is_int));
1097 g_object_set_data(G_OBJECT(input), "required", GINT_TO_POINTER(pce->required));
1098 data->entries = g_list_append(data->entries, input);
1100 g_free(pce);
1103 g_list_free(list);
1104 g_hash_table_destroy(defaults);
1106 /* Set whether the "OK" button should be clickable initially */
1107 set_sensitive_if_input_chat_cb(NULL, data);
1109 gtk_widget_show_all(GTK_WIDGET(data->rq_data.vbox));
1112 static void
1113 chat_select_account_cb(GObject *w, PidginChatData *data)
1115 PurpleAccount *account =
1116 pidgin_account_chooser_get_selected(GTK_WIDGET(w));
1118 g_return_if_fail(data != NULL);
1119 g_return_if_fail(account != NULL);
1121 if (purple_strequal(purple_account_get_protocol_id(data->rq_data.account),
1122 purple_account_get_protocol_id(account)))
1124 data->rq_data.account = account;
1126 else
1128 data->rq_data.account = account;
1129 rebuild_chat_entries(data, data->default_chat_name);
1133 void
1134 pidgin_blist_joinchat_show(void)
1136 PidginChatData *data = NULL;
1138 data = g_new0(PidginChatData, 1);
1140 make_blist_request_dialog((PidginBlistRequestData *)data, NULL,
1141 _("Join a Chat"), "join_chat",
1142 _("Please enter the appropriate information about the chat "
1143 "you would like to join.\n"),
1144 G_CALLBACK(chat_select_account_cb),
1145 chat_account_filter_func, (GCallback)do_joinchat);
1146 gtk_dialog_add_buttons(GTK_DIALOG(data->rq_data.window),
1147 _("Room _List"), 1,
1148 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1149 PIDGIN_STOCK_CHAT, GTK_RESPONSE_OK, NULL);
1150 gtk_dialog_set_default_response(GTK_DIALOG(data->rq_data.window),
1151 GTK_RESPONSE_OK);
1152 data->default_chat_name = NULL;
1153 data->rq_data.account =
1154 pidgin_account_chooser_get_selected(data->rq_data.account_menu);
1156 rebuild_chat_entries(data, NULL);
1158 gtk_widget_show_all(data->rq_data.window);
1161 static void gtk_blist_row_expanded_cb(GtkTreeView *tv, GtkTreeIter *iter, GtkTreePath *path, gpointer user_data)
1163 PidginBuddyList *gtkblist = PIDGIN_BUDDY_LIST(user_data);
1164 PurpleBlistNode *node;
1166 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), iter, NODE_COLUMN, &node, -1);
1168 if (PURPLE_IS_GROUP(node)) {
1169 char *title;
1171 title = pidgin_get_group_title(node, TRUE);
1173 gtk_tree_store_set(gtkblist->treemodel, iter,
1174 NAME_COLUMN, title,
1175 -1);
1177 g_free(title);
1179 purple_blist_node_set_bool(node, "collapsed", FALSE);
1180 pidgin_blist_tooltip_destroy();
1184 static void gtk_blist_row_collapsed_cb(GtkTreeView *tv, GtkTreeIter *iter, GtkTreePath *path, gpointer user_data)
1186 PidginBuddyList *gtkblist = PIDGIN_BUDDY_LIST(user_data);
1187 PurpleBlistNode *node;
1189 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), iter, NODE_COLUMN, &node, -1);
1191 if (PURPLE_IS_GROUP(node)) {
1192 char *title;
1193 PidginBlistNode *gtknode;
1194 PurpleBlistNode *cnode;
1196 title = pidgin_get_group_title(node, FALSE);
1198 gtk_tree_store_set(gtkblist->treemodel, iter,
1199 NAME_COLUMN, title,
1200 -1);
1202 g_free(title);
1204 purple_blist_node_set_bool(node, "collapsed", TRUE);
1206 for(cnode = purple_blist_node_get_first_child(node); cnode; cnode = purple_blist_node_get_sibling_next(cnode)) {
1207 if (PURPLE_IS_CONTACT(cnode)) {
1208 gtknode = purple_blist_node_get_ui_data(cnode);
1209 if (!gtknode->contact_expanded)
1210 continue;
1211 gtknode->contact_expanded = FALSE;
1212 pidgin_blist_update_contact(NULL, cnode);
1215 pidgin_blist_tooltip_destroy();
1216 } else if(PURPLE_IS_CONTACT(node)) {
1217 pidgin_blist_collapse_contact_cb(NULL, node);
1221 static void gtk_blist_row_activated_cb(GtkTreeView *tv, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data) {
1222 PidginBuddyList *gtkblist = PIDGIN_BUDDY_LIST(data);
1223 PurpleBlistNode *node;
1224 GtkTreeIter iter;
1226 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), &iter, path);
1227 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
1229 if(PURPLE_IS_CONTACT(node) || PURPLE_IS_BUDDY(node)) {
1230 PurpleBuddy *buddy;
1232 if(PURPLE_IS_CONTACT(node))
1233 buddy = purple_contact_get_priority_buddy((PurpleContact*)node);
1234 else
1235 buddy = (PurpleBuddy*)node;
1237 pidgin_dialogs_im_with_user(purple_buddy_get_account(buddy), purple_buddy_get_name(buddy));
1238 } else if (PURPLE_IS_CHAT(node)) {
1239 gtk_blist_join_chat((PurpleChat *)node);
1240 } else if (PURPLE_IS_GROUP(node)) {
1241 /* if (gtk_tree_view_row_expanded(tv, path))
1242 gtk_tree_view_collapse_row(tv, path);
1243 else
1244 gtk_tree_view_expand_row(tv,path,FALSE);*/
1248 static void pidgin_blist_add_chat_cb(void)
1250 GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(gtkblist->treeview));
1251 GtkTreeIter iter;
1252 PurpleBlistNode *node;
1254 if(gtk_tree_selection_get_selected(sel, NULL, &iter)){
1255 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
1256 if (PURPLE_IS_BUDDY(node))
1257 purple_blist_request_add_chat(NULL, purple_buddy_get_group(PURPLE_BUDDY(node)), NULL, NULL);
1258 if (PURPLE_IS_CONTACT(node) || PURPLE_IS_CHAT(node))
1259 purple_blist_request_add_chat(NULL, purple_contact_get_group(PURPLE_CONTACT(node)), NULL, NULL);
1260 else if (PURPLE_IS_GROUP(node))
1261 purple_blist_request_add_chat(NULL, (PurpleGroup*)node, NULL, NULL);
1263 else {
1264 purple_blist_request_add_chat(NULL, NULL, NULL, NULL);
1268 static void pidgin_blist_add_buddy_cb(void)
1270 GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(gtkblist->treeview));
1271 GtkTreeIter iter;
1272 PurpleBlistNode *node;
1274 if(gtk_tree_selection_get_selected(sel, NULL, &iter)){
1275 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
1276 if (PURPLE_IS_BUDDY(node)) {
1277 PurpleGroup *group = purple_buddy_get_group(PURPLE_BUDDY(node));
1278 purple_blist_request_add_buddy(NULL, NULL, purple_group_get_name(group), NULL);
1279 } else if (PURPLE_IS_CONTACT(node) || PURPLE_IS_CHAT(node)) {
1280 PurpleGroup *group = purple_contact_get_group(PURPLE_CONTACT(node));
1281 purple_blist_request_add_buddy(NULL, NULL, purple_group_get_name(group), NULL);
1282 } else if (PURPLE_IS_GROUP(node)) {
1283 purple_blist_request_add_buddy(NULL, NULL, purple_group_get_name(PURPLE_GROUP(node)), NULL);
1286 else {
1287 purple_blist_request_add_buddy(NULL, NULL, NULL, NULL);
1291 static void
1292 pidgin_blist_remove_cb (GtkWidget *w, PurpleBlistNode *node)
1294 if (PURPLE_IS_BUDDY(node)) {
1295 pidgin_dialogs_remove_buddy((PurpleBuddy*)node);
1296 } else if (PURPLE_IS_CHAT(node)) {
1297 pidgin_dialogs_remove_chat((PurpleChat*)node);
1298 } else if (PURPLE_IS_GROUP(node)) {
1299 pidgin_dialogs_remove_group((PurpleGroup*)node);
1300 } else if (PURPLE_IS_CONTACT(node)) {
1301 pidgin_dialogs_remove_contact((PurpleContact*)node);
1305 struct _expand {
1306 GtkTreeView *treeview;
1307 GtkTreePath *path;
1308 PurpleBlistNode *node;
1311 static gboolean
1312 scroll_to_expanded_cell(gpointer data)
1314 struct _expand *ex = data;
1315 gtk_tree_view_scroll_to_cell(ex->treeview, ex->path, NULL, FALSE, 0, 0);
1316 pidgin_blist_update_contact(NULL, ex->node);
1318 gtk_tree_path_free(ex->path);
1319 g_free(ex);
1321 return FALSE;
1324 static void
1325 pidgin_blist_expand_contact_cb(GtkWidget *w, PurpleBlistNode *node)
1327 PidginBlistNode *gtknode;
1328 GtkTreeIter iter, parent;
1329 PurpleBlistNode *bnode;
1330 GtkTreePath *path;
1332 if(!PURPLE_IS_CONTACT(node))
1333 return;
1335 gtknode = purple_blist_node_get_ui_data(node);
1337 gtknode->contact_expanded = TRUE;
1339 for(bnode = purple_blist_node_get_first_child(node); bnode; bnode = purple_blist_node_get_sibling_next(bnode)) {
1340 pidgin_blist_update(NULL, bnode);
1343 /* This ensures that the bottom buddy is visible, i.e. not scrolled off the alignment */
1344 if (get_iter_from_node(node, &parent)) {
1345 struct _expand *ex = g_new0(struct _expand, 1);
1347 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(gtkblist->treemodel), &iter, &parent,
1348 gtk_tree_model_iter_n_children(GTK_TREE_MODEL(gtkblist->treemodel), &parent) -1);
1349 path = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel), &iter);
1351 /* Let the treeview draw so it knows where to scroll */
1352 ex->treeview = GTK_TREE_VIEW(gtkblist->treeview);
1353 ex->path = path;
1354 ex->node = purple_blist_node_get_first_child(node);
1355 g_idle_add(scroll_to_expanded_cell, ex);
1359 static void
1360 pidgin_blist_collapse_contact_cb(GtkWidget *w, PurpleBlistNode *node)
1362 PurpleBlistNode *bnode;
1363 PidginBlistNode *gtknode;
1365 if(!PURPLE_IS_CONTACT(node))
1366 return;
1368 gtknode = purple_blist_node_get_ui_data(node);
1370 gtknode->contact_expanded = FALSE;
1372 for(bnode = purple_blist_node_get_first_child(node); bnode; bnode = purple_blist_node_get_sibling_next(bnode)) {
1373 pidgin_blist_update(NULL, bnode);
1377 static void
1378 toggle_privacy(GtkWidget *widget, PurpleBlistNode *node)
1380 PurpleBuddy *buddy;
1381 PurpleAccount *account;
1382 gboolean permitted;
1383 const char *name;
1385 if (!PURPLE_IS_BUDDY(node))
1386 return;
1388 buddy = (PurpleBuddy *)node;
1389 account = purple_buddy_get_account(buddy);
1390 name = purple_buddy_get_name(buddy);
1392 permitted = purple_account_privacy_check(account, name);
1394 /* XXX: Perhaps ask whether to restore the previous lists where appropirate? */
1396 if (permitted)
1397 purple_account_privacy_deny(account, name);
1398 else
1399 purple_account_privacy_allow(account, name);
1401 pidgin_blist_update(purple_blist_get_default(), node);
1404 void pidgin_append_blist_node_privacy_menu(GtkWidget *menu, PurpleBlistNode *node)
1406 PurpleBuddy *buddy = (PurpleBuddy *)node;
1407 PurpleAccount *account;
1408 gboolean permitted;
1410 account = purple_buddy_get_account(buddy);
1411 permitted = purple_account_privacy_check(account, purple_buddy_get_name(buddy));
1413 pidgin_new_menu_item(menu, permitted ? _("_Block") : _("Un_block"),
1414 permitted ? PIDGIN_STOCK_TOOLBAR_BLOCK : PIDGIN_STOCK_TOOLBAR_UNBLOCK,
1415 G_CALLBACK(toggle_privacy), node);
1418 void
1419 pidgin_append_blist_node_proto_menu(GtkWidget *menu, PurpleConnection *gc,
1420 PurpleBlistNode *node)
1422 GList *l, *ll;
1423 PurpleProtocol *protocol = purple_connection_get_protocol(gc);
1425 if(!protocol || !PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, blist_node_menu))
1426 return;
1428 for(l = ll = purple_protocol_client_iface_blist_node_menu(protocol, node); l; l = l->next) {
1429 PurpleActionMenu *act = (PurpleActionMenu *) l->data;
1430 pidgin_append_menu_action(menu, act, node);
1432 g_list_free(ll);
1435 void
1436 pidgin_append_blist_node_extended_menu(GtkWidget *menu, PurpleBlistNode *node)
1438 GList *l, *ll;
1440 for(l = ll = purple_blist_node_get_extended_menu(node); l; l = l->next) {
1441 PurpleActionMenu *act = (PurpleActionMenu *) l->data;
1442 pidgin_append_menu_action(menu, act, node);
1444 g_list_free(ll);
1449 static void
1450 pidgin_append_blist_node_move_to_menu(GtkWidget *menu, PurpleBlistNode *node)
1452 GtkWidget *submenu;
1453 GtkWidget *menuitem;
1454 PurpleBlistNode *group;
1456 menuitem = gtk_menu_item_new_with_label(_("Move to"));
1457 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1458 gtk_widget_show(menuitem);
1460 submenu = gtk_menu_new();
1461 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1463 for (group = purple_blist_get_default_root(); group;
1464 group = purple_blist_node_get_sibling_next(group)) {
1465 if (!PURPLE_IS_GROUP(group))
1466 continue;
1467 if (group == purple_blist_node_get_parent(node))
1468 continue;
1469 menuitem = pidgin_new_menu_item(submenu,
1470 purple_group_get_name((PurpleGroup *)group), NULL,
1471 G_CALLBACK(gtk_blist_menu_move_to_cb), node);
1472 g_object_set_data(G_OBJECT(menuitem), "groupnode", group);
1474 gtk_widget_show_all(submenu);
1477 void
1478 pidgin_blist_make_buddy_menu(GtkWidget *menu, PurpleBuddy *buddy, gboolean sub) {
1479 PurpleAccount *account = NULL;
1480 PurpleConnection *pc = NULL;
1481 PurpleProtocol *protocol;
1482 PurpleContact *contact;
1483 PurpleBlistNode *node;
1484 gboolean contact_expanded = FALSE;
1486 g_return_if_fail(menu);
1487 g_return_if_fail(buddy);
1489 account = purple_buddy_get_account(buddy);
1490 pc = purple_account_get_connection(account);
1491 protocol = purple_connection_get_protocol(pc);
1493 node = PURPLE_BLIST_NODE(buddy);
1495 contact = purple_buddy_get_contact(buddy);
1496 if (contact) {
1497 PidginBlistNode *node = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(contact));
1498 contact_expanded = node->contact_expanded;
1501 if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, get_info)) {
1502 pidgin_new_menu_item(menu, _("Get _Info"), PIDGIN_STOCK_TOOLBAR_USER_INFO,
1503 G_CALLBACK(gtk_blist_menu_info_cb), buddy);
1505 pidgin_new_menu_item(menu, _("I_M"), PIDGIN_STOCK_TOOLBAR_MESSAGE_NEW,
1506 G_CALLBACK(gtk_blist_menu_im_cb), buddy);
1508 #ifdef USE_VV
1509 if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, MEDIA, get_caps)) {
1510 PurpleAccount *account = purple_buddy_get_account(buddy);
1511 const gchar *who = purple_buddy_get_name(buddy);
1512 PurpleMediaCaps caps = purple_protocol_get_media_caps(account, who);
1513 if (caps & PURPLE_MEDIA_CAPS_AUDIO) {
1514 pidgin_new_menu_item(menu, _("_Audio Call"),
1515 PIDGIN_STOCK_TOOLBAR_AUDIO_CALL,
1516 G_CALLBACK(gtk_blist_menu_audio_call_cb), buddy);
1518 if (caps & PURPLE_MEDIA_CAPS_AUDIO_VIDEO) {
1519 pidgin_new_menu_item(menu, _("Audio/_Video Call"),
1520 PIDGIN_STOCK_TOOLBAR_VIDEO_CALL,
1521 G_CALLBACK(gtk_blist_menu_video_call_cb), buddy);
1522 } else if (caps & PURPLE_MEDIA_CAPS_VIDEO) {
1523 pidgin_new_menu_item(menu, _("_Video Call"),
1524 PIDGIN_STOCK_TOOLBAR_VIDEO_CALL,
1525 G_CALLBACK(gtk_blist_menu_video_call_cb), buddy);
1529 #endif
1531 if (protocol && PURPLE_IS_PROTOCOL_XFER(protocol)) {
1532 if (purple_protocol_xfer_can_receive(
1533 PURPLE_PROTOCOL_XFER(protocol),
1534 purple_account_get_connection(purple_buddy_get_account(buddy)), purple_buddy_get_name(buddy)
1535 )) {
1536 pidgin_new_menu_item(menu, _("_Send File..."),
1537 PIDGIN_STOCK_TOOLBAR_SEND_FILE,
1538 G_CALLBACK(gtk_blist_menu_send_file_cb),
1539 buddy);
1543 pidgin_new_menu_item(menu, _("Add Buddy _Pounce..."), NULL,
1544 G_CALLBACK(gtk_blist_menu_bp_cb), buddy);
1546 if (node->parent && node->parent->child->next &&
1547 !sub && !contact_expanded) {
1548 pidgin_new_menu_item(menu, _("View _Log"), NULL,
1549 G_CALLBACK(gtk_blist_menu_showlog_cb), contact);
1550 } else if (!sub) {
1551 pidgin_new_menu_item(menu, _("View _Log"), NULL,
1552 G_CALLBACK(gtk_blist_menu_showlog_cb), buddy);
1555 if (!purple_blist_node_is_transient(node)) {
1556 gboolean show_offline = purple_blist_node_get_bool(node, "show_offline");
1557 pidgin_new_menu_item(menu,
1558 show_offline ? _("Hide When Offline") : _("Show When Offline"),
1559 NULL, G_CALLBACK(gtk_blist_menu_showoffline_cb), node);
1562 pidgin_append_blist_node_proto_menu(menu, purple_account_get_connection(purple_buddy_get_account(buddy)), node);
1563 pidgin_append_blist_node_extended_menu(menu, node);
1565 if (!contact_expanded && contact != NULL)
1566 pidgin_append_blist_node_move_to_menu(menu, PURPLE_BLIST_NODE(contact));
1568 if (node->parent && node->parent->child->next &&
1569 !sub && !contact_expanded) {
1570 pidgin_separator(menu);
1571 pidgin_append_blist_node_privacy_menu(menu, node);
1572 pidgin_new_menu_item(menu, _("_Alias..."), PIDGIN_STOCK_ALIAS,
1573 G_CALLBACK(gtk_blist_menu_alias_cb), contact);
1574 pidgin_new_menu_item(menu, _("_Remove"), GTK_STOCK_REMOVE,
1575 G_CALLBACK(pidgin_blist_remove_cb), contact);
1576 } else if (!sub || contact_expanded) {
1577 pidgin_separator(menu);
1578 pidgin_append_blist_node_privacy_menu(menu, node);
1579 pidgin_new_menu_item(menu, _("_Alias..."), PIDGIN_STOCK_ALIAS,
1580 G_CALLBACK(gtk_blist_menu_alias_cb), buddy);
1581 pidgin_new_menu_item(menu, _("_Remove"), GTK_STOCK_REMOVE,
1582 G_CALLBACK(pidgin_blist_remove_cb), buddy);
1586 static gboolean
1587 gtk_blist_key_press_cb(GtkWidget *tv, GdkEventKey *event, gpointer data)
1589 PurpleBlistNode *node;
1590 GtkTreeIter iter, parent;
1591 GtkTreeSelection *sel;
1592 GtkTreePath *path;
1594 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
1595 if(!gtk_tree_selection_get_selected(sel, NULL, &iter))
1596 return FALSE;
1598 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
1600 if(event->state & GDK_CONTROL_MASK &&
1601 (event->keyval == 'o' || event->keyval == 'O')) {
1602 PurpleBuddy *buddy;
1604 if(PURPLE_IS_CONTACT(node)) {
1605 buddy = purple_contact_get_priority_buddy((PurpleContact*)node);
1606 } else if(PURPLE_IS_BUDDY(node)) {
1607 buddy = (PurpleBuddy*)node;
1608 } else {
1609 return FALSE;
1611 if(buddy)
1612 pidgin_retrieve_user_info(purple_account_get_connection(purple_buddy_get_account(buddy)), purple_buddy_get_name(buddy));
1613 } else {
1614 switch (event->keyval) {
1615 case GDK_KEY_F2:
1616 gtk_blist_menu_alias_cb(tv, node);
1617 break;
1619 case GDK_KEY_Left:
1620 path = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel), &iter);
1621 if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(tv), path)) {
1622 /* Collapse the Group */
1623 gtk_tree_view_collapse_row(GTK_TREE_VIEW(tv), path);
1624 gtk_tree_path_free(path);
1625 return TRUE;
1626 } else {
1627 /* Select the Parent */
1628 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), &iter, path)) {
1629 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(gtkblist->treemodel), &parent, &iter)) {
1630 gtk_tree_path_free(path);
1631 path = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel), &parent);
1632 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tv), path, NULL, FALSE);
1633 gtk_tree_path_free(path);
1634 return TRUE;
1638 gtk_tree_path_free(path);
1639 break;
1641 case GDK_KEY_Right:
1642 path = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel), &iter);
1643 if (!gtk_tree_view_row_expanded(GTK_TREE_VIEW(tv), path)) {
1644 /* Expand the Group */
1645 if (PURPLE_IS_CONTACT(node)) {
1646 pidgin_blist_expand_contact_cb(NULL, node);
1647 gtk_tree_path_free(path);
1648 return TRUE;
1649 } else if (!PURPLE_IS_BUDDY(node)) {
1650 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv), path, FALSE);
1651 gtk_tree_path_free(path);
1652 return TRUE;
1654 } else {
1655 /* Select the First Child */
1656 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), &parent, path)) {
1657 if (gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(gtkblist->treemodel), &iter, &parent, 0)) {
1658 gtk_tree_path_free(path);
1659 path = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel), &iter);
1660 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tv), path, NULL, FALSE);
1661 gtk_tree_path_free(path);
1662 return TRUE;
1666 gtk_tree_path_free(path);
1667 break;
1671 return FALSE;
1674 static void
1675 set_node_custom_icon_cb(const gchar *filename, gpointer data)
1677 if (filename) {
1678 PurpleBlistNode *node = (PurpleBlistNode*)data;
1680 purple_buddy_icons_node_set_custom_icon_from_file(node,
1681 filename);
1685 static void
1686 set_node_custom_icon(GtkWidget *w, PurpleBlistNode *node)
1688 /* This doesn't keep track of the returned dialog (so that successive
1689 * calls could be made to re-display that dialog). Do we want that? */
1690 GtkWidget *win = pidgin_buddy_icon_chooser_new(NULL, set_node_custom_icon_cb, node);
1691 gtk_widget_show_all(win);
1694 static void
1695 remove_node_custom_icon(GtkWidget *w, PurpleBlistNode *node)
1697 purple_buddy_icons_node_set_custom_icon(node, NULL, 0);
1700 static void
1701 add_buddy_icon_menu_items(GtkWidget *menu, PurpleBlistNode *node)
1703 GtkWidget *item;
1705 pidgin_new_menu_item(menu, _("Set Custom Icon"), NULL,
1706 G_CALLBACK(set_node_custom_icon), node);
1708 item = pidgin_new_menu_item(menu, _("Remove Custom Icon"), NULL,
1709 G_CALLBACK(remove_node_custom_icon), node);
1710 if (!purple_buddy_icons_node_has_custom_icon(node))
1711 gtk_widget_set_sensitive(item, FALSE);
1714 static GtkWidget *
1715 create_group_menu (PurpleBlistNode *node, PurpleGroup *g)
1717 GtkWidget *menu;
1718 GtkWidget *item;
1720 menu = gtk_menu_new();
1721 item = pidgin_new_menu_item(menu, _("Add _Buddy..."), GTK_STOCK_ADD,
1722 G_CALLBACK(pidgin_blist_add_buddy_cb), node);
1723 gtk_widget_set_sensitive(item, purple_connections_get_all() != NULL);
1724 item = pidgin_new_menu_item(menu, _("Add C_hat..."), GTK_STOCK_ADD,
1725 G_CALLBACK(pidgin_blist_add_chat_cb), node);
1726 gtk_widget_set_sensitive(item, pidgin_blist_joinchat_is_showable());
1727 pidgin_new_menu_item(menu, _("_Delete Group"), GTK_STOCK_REMOVE,
1728 G_CALLBACK(pidgin_blist_remove_cb), node);
1729 pidgin_new_menu_item(menu, _("_Rename"), NULL,
1730 G_CALLBACK(gtk_blist_menu_alias_cb), node);
1731 if (!purple_blist_node_is_transient(node)) {
1732 gboolean show_offline = purple_blist_node_get_bool(node, "show_offline");
1733 pidgin_new_menu_item(menu,
1734 show_offline ? _("Hide When Offline") : _("Show When Offline"),
1735 NULL, G_CALLBACK(gtk_blist_menu_showoffline_cb), node);
1738 add_buddy_icon_menu_items(menu, node);
1740 pidgin_append_blist_node_extended_menu(menu, node);
1742 return menu;
1745 static GtkWidget *
1746 create_chat_menu(PurpleBlistNode *node, PurpleChat *c)
1748 GtkWidget *menu;
1749 gboolean autojoin, persistent;
1751 menu = gtk_menu_new();
1752 autojoin = purple_blist_node_get_bool(node, "gtk-autojoin");
1753 persistent = purple_blist_node_get_bool(node, "gtk-persistent");
1755 pidgin_new_menu_item(menu, _("_Join"), PIDGIN_STOCK_CHAT,
1756 G_CALLBACK(gtk_blist_menu_join_cb), node);
1757 pidgin_new_check_item(menu, _("Auto-Join"),
1758 G_CALLBACK(gtk_blist_menu_autojoin_cb), node, autojoin);
1759 pidgin_new_check_item(menu, _("Persistent"),
1760 G_CALLBACK(gtk_blist_menu_persistent_cb), node, persistent);
1761 pidgin_new_menu_item(menu, _("View _Log"), NULL,
1762 G_CALLBACK(gtk_blist_menu_showlog_cb), node);
1764 pidgin_append_blist_node_proto_menu(menu, purple_account_get_connection(purple_chat_get_account(c)), node);
1765 pidgin_append_blist_node_extended_menu(menu, node);
1767 pidgin_separator(menu);
1769 pidgin_new_menu_item(menu, _("_Edit Settings..."), NULL,
1770 G_CALLBACK(chat_components_edit), node);
1771 pidgin_new_menu_item(menu, _("_Alias..."), PIDGIN_STOCK_ALIAS,
1772 G_CALLBACK(gtk_blist_menu_alias_cb), node);
1773 pidgin_new_menu_item(menu, _("_Remove"), GTK_STOCK_REMOVE,
1774 G_CALLBACK(pidgin_blist_remove_cb), node);
1776 add_buddy_icon_menu_items(menu, node);
1778 return menu;
1781 static GtkWidget *
1782 create_contact_menu (PurpleBlistNode *node)
1784 GtkWidget *menu;
1786 menu = gtk_menu_new();
1788 pidgin_new_menu_item(menu, _("View _Log"), NULL,
1789 G_CALLBACK(gtk_blist_menu_showlog_cb),
1790 node);
1792 pidgin_separator(menu);
1794 pidgin_new_menu_item(menu, _("_Alias..."), PIDGIN_STOCK_ALIAS,
1795 G_CALLBACK(gtk_blist_menu_alias_cb), node);
1796 pidgin_new_menu_item(menu, _("_Remove"), GTK_STOCK_REMOVE,
1797 G_CALLBACK(pidgin_blist_remove_cb), node);
1799 add_buddy_icon_menu_items(menu, node);
1801 pidgin_separator(menu);
1803 pidgin_new_menu_item(menu, _("_Collapse"), GTK_STOCK_ZOOM_OUT,
1804 G_CALLBACK(pidgin_blist_collapse_contact_cb),
1805 node);
1807 pidgin_append_blist_node_extended_menu(menu, node);
1808 return menu;
1811 static GtkWidget *
1812 create_buddy_menu(PurpleBlistNode *node, PurpleBuddy *b)
1814 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(node);
1815 GtkWidget *menu;
1816 GtkWidget *menuitem;
1817 gboolean show_offline = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_offline_buddies");
1819 menu = gtk_menu_new();
1820 pidgin_blist_make_buddy_menu(menu, b, FALSE);
1822 if(PURPLE_IS_CONTACT(node)) {
1823 pidgin_separator(menu);
1825 add_buddy_icon_menu_items(menu, node);
1827 if(gtknode->contact_expanded) {
1828 pidgin_new_menu_item(menu, _("_Collapse"),
1829 GTK_STOCK_ZOOM_OUT,
1830 G_CALLBACK(pidgin_blist_collapse_contact_cb),
1831 node);
1832 } else {
1833 pidgin_new_menu_item(menu, _("_Expand"),
1834 GTK_STOCK_ZOOM_IN,
1835 G_CALLBACK(pidgin_blist_expand_contact_cb),
1836 node);
1838 if(node->child->next) {
1839 PurpleBlistNode *bnode;
1841 for(bnode = node->child; bnode; bnode = bnode->next) {
1842 PurpleBuddy *buddy = (PurpleBuddy*)bnode;
1843 GdkPixbuf *buf;
1844 GtkWidget *submenu;
1845 GtkWidget *image;
1847 if(buddy == b)
1848 continue;
1849 if(!purple_account_get_connection(purple_buddy_get_account(buddy)))
1850 continue;
1851 if(!show_offline && !PURPLE_BUDDY_IS_ONLINE(buddy))
1852 continue;
1854 menuitem = gtk_image_menu_item_new_with_label(purple_buddy_get_name(buddy));
1855 buf = pidgin_create_protocol_icon(purple_buddy_get_account(buddy), PIDGIN_PROTOCOL_ICON_SMALL);
1856 image = gtk_image_new_from_pixbuf(buf);
1857 g_object_unref(G_OBJECT(buf));
1858 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem),
1859 image);
1860 gtk_widget_show(image);
1861 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1862 gtk_widget_show(menuitem);
1864 submenu = gtk_menu_new();
1865 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1866 gtk_widget_show(submenu);
1868 pidgin_blist_make_buddy_menu(submenu, buddy, TRUE);
1872 return menu;
1875 static gboolean
1876 pidgin_blist_show_context_menu(GtkWidget *tv, PurpleBlistNode *node, GdkEvent *event)
1878 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(node);
1879 GtkWidget *menu = NULL;
1880 gboolean handled = FALSE;
1882 /* Create a menu based on the thing we right-clicked on */
1883 if (PURPLE_IS_GROUP(node)) {
1884 PurpleGroup *g = (PurpleGroup *)node;
1886 menu = create_group_menu(node, g);
1887 } else if (PURPLE_IS_CHAT(node)) {
1888 PurpleChat *c = (PurpleChat *)node;
1890 menu = create_chat_menu(node, c);
1891 } else if ((PURPLE_IS_CONTACT(node)) && (gtknode->contact_expanded)) {
1892 menu = create_contact_menu(node);
1893 } else if (PURPLE_IS_CONTACT(node) || PURPLE_IS_BUDDY(node)) {
1894 PurpleBuddy *b;
1896 if (PURPLE_IS_CONTACT(node))
1897 b = purple_contact_get_priority_buddy((PurpleContact*)node);
1898 else
1899 b = (PurpleBuddy *)node;
1901 menu = create_buddy_menu(node, b);
1904 #ifdef _WIN32
1905 pidgin_blist_tooltip_destroy();
1907 /* Unhook the tooltip-timeout since we don't want a tooltip
1908 * to appear and obscure the context menu we are about to show
1909 This is a workaround for GTK+ bug 107320. */
1910 if (gtkblist->timeout) {
1911 g_source_remove(gtkblist->timeout);
1912 gtkblist->timeout = 0;
1914 #endif
1916 /* Now display the menu */
1917 if (menu != NULL) {
1918 gtk_widget_show_all(menu);
1919 if (event != NULL) {
1920 /* Pointer event */
1921 gtk_menu_popup_at_pointer(GTK_MENU(menu), event);
1922 } else {
1923 /* Keyboard event */
1924 pidgin_menu_popup_at_treeview_selection(menu, tv);
1926 handled = TRUE;
1929 return handled;
1932 static gboolean
1933 gtk_blist_button_press_cb(GtkWidget *tv, GdkEventButton *event, gpointer user_data)
1935 GtkTreePath *path;
1936 PurpleBlistNode *node;
1937 GtkTreeIter iter;
1938 GtkTreeSelection *sel;
1939 PurpleProtocol *protocol = NULL;
1940 PidginBlistNode *gtknode;
1941 gboolean handled = FALSE;
1943 /* Here we figure out which node was clicked */
1944 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(tv), event->x, event->y, &path, NULL, NULL, NULL))
1945 return FALSE;
1946 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), &iter, path);
1947 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
1948 gtknode = purple_blist_node_get_ui_data(node);
1950 /* Right click draws a context menu */
1951 if (gdk_event_triggers_context_menu((GdkEvent *)event)) {
1952 handled = pidgin_blist_show_context_menu(tv, node, (GdkEvent *)event);
1954 /* CTRL+middle click expands or collapse a contact */
1955 } else if ((event->button == GDK_BUTTON_MIDDLE) && (event->type == GDK_BUTTON_PRESS) &&
1956 (event->state & GDK_CONTROL_MASK) && (PURPLE_IS_CONTACT(node))) {
1957 if (gtknode->contact_expanded)
1958 pidgin_blist_collapse_contact_cb(NULL, node);
1959 else
1960 pidgin_blist_expand_contact_cb(NULL, node);
1961 handled = TRUE;
1963 /* Double middle click gets info */
1964 } else if ((event->button == GDK_BUTTON_MIDDLE) && (event->type == GDK_2BUTTON_PRESS) &&
1965 ((PURPLE_IS_CONTACT(node)) || (PURPLE_IS_BUDDY(node)))) {
1966 PurpleBuddy *b;
1967 if(PURPLE_IS_CONTACT(node))
1968 b = purple_contact_get_priority_buddy((PurpleContact*)node);
1969 else
1970 b = (PurpleBuddy *)node;
1972 protocol = purple_protocols_find(purple_account_get_protocol_id(purple_buddy_get_account(b)));
1974 if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, get_info))
1975 pidgin_retrieve_user_info(purple_account_get_connection(purple_buddy_get_account(b)), purple_buddy_get_name(b));
1976 handled = TRUE;
1979 #if (1)
1981 * This code only exists because GTK+ doesn't work. If we return
1982 * FALSE here, as would be normal the event propoagates down and
1983 * somehow gets interpreted as the start of a drag event.
1985 * Um, isn't it _normal_ to return TRUE here? Since the event
1986 * was handled? --Mark
1988 if(handled) {
1989 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
1990 gtk_tree_selection_select_path(sel, path);
1991 gtk_tree_path_free(path);
1992 return TRUE;
1994 #endif
1995 gtk_tree_path_free(path);
1997 return FALSE;
2000 static gboolean
2001 pidgin_blist_popup_menu_cb(GtkWidget *tv, void *user_data)
2003 PurpleBlistNode *node;
2004 GtkTreeIter iter;
2005 GtkTreeSelection *sel;
2006 gboolean handled = FALSE;
2008 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
2009 if (!gtk_tree_selection_get_selected(sel, NULL, &iter))
2010 return FALSE;
2012 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
2014 /* Shift+F10 draws a context menu */
2015 handled = pidgin_blist_show_context_menu(tv, node, NULL);
2017 return handled;
2020 static void gtk_blist_show_xfer_dialog_cb(GtkAction *item, gpointer data)
2022 pidgin_xfer_dialog_show(NULL);
2025 static void pidgin_blist_buddy_details_cb(GtkToggleAction *item, gpointer data)
2027 pidgin_set_cursor(gtkblist->window, GDK_WATCH);
2029 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons",
2030 gtk_toggle_action_get_active(item));
2032 pidgin_clear_cursor(gtkblist->window);
2035 static void pidgin_blist_show_idle_time_cb(GtkToggleAction *item, gpointer data)
2037 pidgin_set_cursor(gtkblist->window, GDK_WATCH);
2039 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/blist/show_idle_time",
2040 gtk_toggle_action_get_active(item));
2042 pidgin_clear_cursor(gtkblist->window);
2045 static void pidgin_blist_show_protocol_icons_cb(GtkToggleAction *item, gpointer data)
2047 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/blist/show_protocol_icons",
2048 gtk_toggle_action_get_active(item));
2051 static void pidgin_blist_show_empty_groups_cb(GtkToggleAction *item, gpointer data)
2053 pidgin_set_cursor(gtkblist->window, GDK_WATCH);
2055 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/blist/show_empty_groups",
2056 gtk_toggle_action_get_active(item));
2058 pidgin_clear_cursor(gtkblist->window);
2061 static void pidgin_blist_edit_mode_cb(GtkToggleAction *checkitem, gpointer data)
2063 pidgin_set_cursor(gtkblist->window, GDK_WATCH);
2065 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/blist/show_offline_buddies",
2066 gtk_toggle_action_get_active(checkitem));
2068 pidgin_clear_cursor(gtkblist->window);
2071 static void pidgin_blist_mute_sounds_cb(GtkToggleAction *item, gpointer data)
2073 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/sound/mute",
2074 gtk_toggle_action_get_active(item));
2078 static void
2079 pidgin_blist_mute_pref_cb(const char *name, PurplePrefType type,
2080 gconstpointer value, gpointer data)
2082 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(gtk_ui_manager_get_action(gtkblist->ui,
2083 "/BList/ToolsMenu/MuteSounds")), (gboolean)GPOINTER_TO_INT(value));
2086 static void
2087 pidgin_blist_sound_method_pref_cb(const char *name, PurplePrefType type,
2088 gconstpointer value, gpointer data)
2090 gboolean sensitive = TRUE;
2092 if(purple_strequal(value, "none"))
2093 sensitive = FALSE;
2095 gtk_action_set_sensitive(gtk_ui_manager_get_action(gtkblist->ui, "/BList/ToolsMenu/MuteSounds"), sensitive);
2098 static void
2099 add_buddies_from_vcard(const char *protocol_id, PurpleGroup *group, GList *list,
2100 const char *alias)
2102 GList *l;
2103 PurpleAccount *account = NULL;
2104 PurpleConnection *gc;
2106 if (list == NULL)
2107 return;
2109 for (l = purple_connections_get_all(); l != NULL; l = l->next)
2111 gc = (PurpleConnection *)l->data;
2112 account = purple_connection_get_account(gc);
2114 if (purple_strequal(purple_account_get_protocol_id(account), protocol_id))
2115 break;
2117 account = NULL;
2120 if (account != NULL)
2122 for (l = list; l != NULL; l = l->next)
2124 purple_blist_request_add_buddy(account, l->data,
2125 (group ? purple_group_get_name(group) : NULL),
2126 alias);
2130 g_list_foreach(list, (GFunc)g_free, NULL);
2131 g_list_free(list);
2134 static gboolean
2135 parse_vcard(const char *vcard, PurpleGroup *group)
2137 char *temp_vcard;
2138 char *s, *c;
2139 char *alias = NULL;
2140 GList *aims = NULL;
2141 GList *icqs = NULL;
2142 GList *jabbers = NULL;
2144 s = temp_vcard = g_strdup(vcard);
2146 while (*s != '\0' && strncmp(s, "END:vCard", strlen("END:vCard")))
2148 char *field, *value;
2150 field = s;
2152 /* Grab the field */
2153 while (*s != '\r' && *s != '\n' && *s != '\0' && *s != ':')
2154 s++;
2156 if (*s == '\r') s++;
2157 if (*s == '\n')
2159 s++;
2160 continue;
2163 if (*s != '\0') *s++ = '\0';
2165 if ((c = strchr(field, ';')) != NULL)
2166 *c = '\0';
2168 /* Proceed to the end of the line */
2169 value = s;
2171 while (*s != '\r' && *s != '\n' && *s != '\0')
2172 s++;
2174 if (*s == '\r') *s++ = '\0';
2175 if (*s == '\n') *s++ = '\0';
2177 /* We only want to worry about a few fields here. */
2178 if (purple_strequal(field, "FN"))
2179 alias = g_strdup(value);
2180 else if (purple_strequal(field, "X-AIM") || purple_strequal(field, "X-ICQ") ||
2181 purple_strequal(field, "X-JABBER"))
2183 char **values = g_strsplit(value, ":", 0);
2184 char **im;
2186 for (im = values; *im != NULL; im++)
2188 if (purple_strequal(field, "X-AIM"))
2189 aims = g_list_append(aims, g_strdup(*im));
2190 else if (purple_strequal(field, "X-ICQ"))
2191 icqs = g_list_append(icqs, g_strdup(*im));
2192 else if (purple_strequal(field, "X-JABBER"))
2193 jabbers = g_list_append(jabbers, g_strdup(*im));
2196 g_strfreev(values);
2200 g_free(temp_vcard);
2202 if (aims == NULL && icqs == NULL && jabbers == NULL)
2204 g_free(alias);
2206 return FALSE;
2209 add_buddies_from_vcard("prpl-aim", group, aims, alias);
2210 add_buddies_from_vcard("prpl-icq", group, icqs, alias);
2211 add_buddies_from_vcard("prpl-jabber", group, jabbers, alias);
2213 g_free(alias);
2215 return TRUE;
2218 #ifdef _WIN32
2219 static void pidgin_blist_drag_begin(GtkWidget *widget,
2220 GdkDragContext *drag_context, gpointer user_data)
2222 pidgin_blist_tooltip_destroy();
2225 /* Unhook the tooltip-timeout since we don't want a tooltip
2226 * to appear and obscure the dragging operation.
2227 * This is a workaround for GTK+ bug 107320. */
2228 if (gtkblist->timeout) {
2229 g_source_remove(gtkblist->timeout);
2230 gtkblist->timeout = 0;
2233 #endif
2235 static void pidgin_blist_drag_data_get_cb(GtkWidget *widget,
2236 GdkDragContext *dc,
2237 GtkSelectionData *data,
2238 guint info,
2239 guint time,
2240 gpointer null)
2242 GdkAtom target = gtk_selection_data_get_target(data);
2244 if (target == gdk_atom_intern("PURPLE_BLIST_NODE", FALSE)) {
2245 GtkTreeRowReference *ref = g_object_get_data(G_OBJECT(dc), "gtk-tree-view-source-row");
2246 GtkTreePath *sourcerow = gtk_tree_row_reference_get_path(ref);
2247 GtkTreeIter iter;
2248 PurpleBlistNode *node = NULL;
2249 if(!sourcerow)
2250 return;
2251 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), &iter, sourcerow);
2252 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
2253 gtk_selection_data_set (data,
2254 gdk_atom_intern ("PURPLE_BLIST_NODE", FALSE),
2255 8, /* bits */
2256 (void*)&node,
2257 sizeof (node));
2259 gtk_tree_path_free(sourcerow);
2260 } else if (target == gdk_atom_intern("application/x-im-contact", FALSE)) {
2261 GtkTreeRowReference *ref;
2262 GtkTreePath *sourcerow;
2263 GtkTreeIter iter;
2264 PurpleBlistNode *node = NULL;
2265 PurpleBuddy *buddy;
2266 PurpleConnection *gc;
2267 GString *str;
2268 const char *protocol;
2270 ref = g_object_get_data(G_OBJECT(dc), "gtk-tree-view-source-row");
2271 sourcerow = gtk_tree_row_reference_get_path(ref);
2273 if (!sourcerow)
2274 return;
2276 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), &iter,
2277 sourcerow);
2278 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
2280 if (PURPLE_IS_CONTACT(node))
2282 buddy = purple_contact_get_priority_buddy((PurpleContact *)node);
2284 else if (!PURPLE_IS_BUDDY(node))
2286 gtk_tree_path_free(sourcerow);
2287 return;
2289 else
2291 buddy = (PurpleBuddy *)node;
2294 gc = purple_account_get_connection(purple_buddy_get_account(buddy));
2296 if (gc == NULL)
2298 gtk_tree_path_free(sourcerow);
2299 return;
2302 protocol =
2303 purple_protocol_class_list_icon(purple_connection_get_protocol(gc),
2304 purple_buddy_get_account(buddy), buddy);
2306 str = g_string_new(NULL);
2307 g_string_printf(str,
2308 "MIME-Version: 1.0\r\n"
2309 "Content-Type: application/x-im-contact\r\n"
2310 "X-IM-Protocol: %s\r\n"
2311 "X-IM-Username: %s\r\n",
2312 protocol,
2313 purple_buddy_get_name(buddy));
2315 if (purple_buddy_get_local_alias(buddy) != NULL)
2317 g_string_append_printf(str,
2318 "X-IM-Alias: %s\r\n",
2319 purple_buddy_get_local_alias(buddy));
2322 g_string_append(str, "\r\n");
2324 gtk_selection_data_set(data,
2325 gdk_atom_intern("application/x-im-contact", FALSE),
2326 8, /* bits */
2327 (const guchar *)str->str,
2328 strlen(str->str) + 1);
2330 g_string_free(str, TRUE);
2331 gtk_tree_path_free(sourcerow);
2335 static void pidgin_blist_drag_data_rcv_cb(GtkWidget *widget, GdkDragContext *dc, guint x, guint y,
2336 GtkSelectionData *sd, guint info, guint t)
2338 GdkAtom target = gtk_selection_data_get_target(sd);
2339 const guchar *data = gtk_selection_data_get_data(sd);
2341 if (gtkblist->drag_timeout) {
2342 g_source_remove(gtkblist->drag_timeout);
2343 gtkblist->drag_timeout = 0;
2346 if (target == gdk_atom_intern("PURPLE_BLIST_NODE", FALSE) && data) {
2347 PurpleBlistNode *n = NULL;
2348 GtkTreePath *path = NULL;
2349 GtkTreeViewDropPosition position;
2350 memcpy(&n, data, sizeof(n));
2351 if(gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(widget), x, y, &path, &position)) {
2352 /* if we're here, I think it means the drop is ok */
2353 GtkTreeIter iter;
2354 PurpleBlistNode *node;
2355 PidginBlistNode *gtknode;
2357 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel),
2358 &iter, path);
2359 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel),
2360 &iter, NODE_COLUMN, &node, -1);
2361 gtknode = purple_blist_node_get_ui_data(node);
2363 if (PURPLE_IS_CONTACT(n)) {
2364 PurpleContact *c = (PurpleContact*)n;
2365 if (PURPLE_IS_CONTACT(node) && gtknode->contact_expanded) {
2366 purple_contact_merge(c, node);
2367 } else if (PURPLE_IS_CONTACT(node) ||
2368 PURPLE_IS_CHAT(node)) {
2369 switch(position) {
2370 case GTK_TREE_VIEW_DROP_AFTER:
2371 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
2372 purple_blist_add_contact(c, (PurpleGroup*)node->parent,
2373 node);
2374 break;
2375 case GTK_TREE_VIEW_DROP_BEFORE:
2376 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
2377 purple_blist_add_contact(c, (PurpleGroup*)node->parent,
2378 node->prev);
2379 break;
2381 } else if(PURPLE_IS_GROUP(node)) {
2382 purple_blist_add_contact(c, (PurpleGroup*)node, NULL);
2383 } else if(PURPLE_IS_BUDDY(node)) {
2384 purple_contact_merge(c, node);
2386 } else if (PURPLE_IS_BUDDY(n)) {
2387 PurpleBuddy *b = (PurpleBuddy*)n;
2388 if (PURPLE_IS_BUDDY(node)) {
2389 switch(position) {
2390 case GTK_TREE_VIEW_DROP_AFTER:
2391 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
2392 purple_blist_add_buddy(b, (PurpleContact*)node->parent,
2393 (PurpleGroup*)node->parent->parent, node);
2394 break;
2395 case GTK_TREE_VIEW_DROP_BEFORE:
2396 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
2397 purple_blist_add_buddy(b, (PurpleContact*)node->parent,
2398 (PurpleGroup*)node->parent->parent,
2399 node->prev);
2400 break;
2402 } else if(PURPLE_IS_CHAT(node)) {
2403 purple_blist_add_buddy(b, NULL, (PurpleGroup*)node->parent,
2404 NULL);
2405 } else if (PURPLE_IS_GROUP(node)) {
2406 purple_blist_add_buddy(b, NULL, (PurpleGroup*)node, NULL);
2407 } else if (PURPLE_IS_CONTACT(node)) {
2408 if(gtknode->contact_expanded) {
2409 switch(position) {
2410 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
2411 case GTK_TREE_VIEW_DROP_AFTER:
2412 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
2413 purple_blist_add_buddy(b, (PurpleContact*)node,
2414 (PurpleGroup*)node->parent, NULL);
2415 break;
2416 case GTK_TREE_VIEW_DROP_BEFORE:
2417 purple_blist_add_buddy(b, NULL,
2418 (PurpleGroup*)node->parent, node->prev);
2419 break;
2421 } else {
2422 switch(position) {
2423 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
2424 case GTK_TREE_VIEW_DROP_AFTER:
2425 purple_blist_add_buddy(b, NULL,
2426 (PurpleGroup*)node->parent, NULL);
2427 break;
2428 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
2429 case GTK_TREE_VIEW_DROP_BEFORE:
2430 purple_blist_add_buddy(b, NULL,
2431 (PurpleGroup*)node->parent, node->prev);
2432 break;
2436 } else if (PURPLE_IS_CHAT(n)) {
2437 PurpleChat *chat = (PurpleChat *)n;
2438 if (PURPLE_IS_BUDDY(node)) {
2439 switch(position) {
2440 case GTK_TREE_VIEW_DROP_AFTER:
2441 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
2442 case GTK_TREE_VIEW_DROP_BEFORE:
2443 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
2444 purple_blist_add_chat(chat,
2445 (PurpleGroup*)node->parent->parent,
2446 node->parent);
2447 break;
2449 } else if(PURPLE_IS_CONTACT(node) ||
2450 PURPLE_IS_CHAT(node)) {
2451 switch(position) {
2452 case GTK_TREE_VIEW_DROP_AFTER:
2453 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
2454 purple_blist_add_chat(chat, (PurpleGroup*)node->parent, node);
2455 break;
2456 case GTK_TREE_VIEW_DROP_BEFORE:
2457 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
2458 purple_blist_add_chat(chat, (PurpleGroup*)node->parent, node->prev);
2459 break;
2461 } else if (PURPLE_IS_GROUP(node)) {
2462 purple_blist_add_chat(chat, (PurpleGroup*)node, NULL);
2464 } else if (PURPLE_IS_GROUP(n)) {
2465 PurpleGroup *g = (PurpleGroup*)n;
2466 if (PURPLE_IS_GROUP(node)) {
2467 switch (position) {
2468 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
2469 case GTK_TREE_VIEW_DROP_AFTER:
2470 purple_blist_add_group(g, node);
2471 break;
2472 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
2473 case GTK_TREE_VIEW_DROP_BEFORE:
2474 purple_blist_add_group(g, node->prev);
2475 break;
2477 } else if(PURPLE_IS_BUDDY(node)) {
2478 purple_blist_add_group(g, node->parent->parent);
2479 } else if(PURPLE_IS_CONTACT(node) ||
2480 PURPLE_IS_CHAT(node)) {
2481 purple_blist_add_group(g, node->parent);
2485 gtk_tree_path_free(path);
2486 gtk_drag_finish(dc, TRUE, (gdk_drag_context_get_actions(dc) == GDK_ACTION_MOVE), t);
2488 } else if (target == gdk_atom_intern("application/x-im-contact",
2489 FALSE) && data) {
2490 PurpleGroup *group = NULL;
2491 GtkTreePath *path = NULL;
2492 GtkTreeViewDropPosition position;
2493 PurpleAccount *account;
2494 char *protocol = NULL;
2495 char *username = NULL;
2496 char *alias = NULL;
2498 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(widget),
2499 x, y, &path, &position))
2501 GtkTreeIter iter;
2502 PurpleBlistNode *node;
2504 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel),
2505 &iter, path);
2506 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel),
2507 &iter, NODE_COLUMN, &node, -1);
2509 if (PURPLE_IS_BUDDY(node))
2511 group = (PurpleGroup *)node->parent->parent;
2513 else if (PURPLE_IS_CHAT(node) ||
2514 PURPLE_IS_CONTACT(node))
2516 group = (PurpleGroup *)node->parent;
2518 else if (PURPLE_IS_GROUP(node))
2520 group = (PurpleGroup *)node;
2524 if (pidgin_parse_x_im_contact((const char *)data, FALSE, &account,
2525 &protocol, &username, &alias))
2527 if (account == NULL)
2529 purple_notify_error(NULL, NULL,
2530 _("You are not currently signed on with an account that "
2531 "can add that buddy."), NULL, NULL);
2533 else
2535 purple_blist_request_add_buddy(account, username,
2536 (group ? purple_group_get_name(group) : NULL),
2537 alias);
2541 g_free(username);
2542 g_free(protocol);
2543 g_free(alias);
2545 if (path != NULL)
2546 gtk_tree_path_free(path);
2548 gtk_drag_finish(dc, TRUE,
2549 gdk_drag_context_get_actions(dc) == GDK_ACTION_MOVE, t);
2551 else if (target == gdk_atom_intern("text/x-vcard", FALSE) && data)
2553 gboolean result;
2554 PurpleGroup *group = NULL;
2555 GtkTreePath *path = NULL;
2556 GtkTreeViewDropPosition position;
2558 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(widget),
2559 x, y, &path, &position))
2561 GtkTreeIter iter;
2562 PurpleBlistNode *node;
2564 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel),
2565 &iter, path);
2566 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel),
2567 &iter, NODE_COLUMN, &node, -1);
2569 if (PURPLE_IS_BUDDY(node))
2571 group = (PurpleGroup *)node->parent->parent;
2573 else if (PURPLE_IS_CHAT(node) ||
2574 PURPLE_IS_CONTACT(node))
2576 group = (PurpleGroup *)node->parent;
2578 else if (PURPLE_IS_GROUP(node))
2580 group = (PurpleGroup *)node;
2584 result = parse_vcard((const gchar *)data, group);
2586 gtk_drag_finish(dc, result,
2587 gdk_drag_context_get_actions(dc) == GDK_ACTION_MOVE, t);
2588 } else if (target == gdk_atom_intern("text/uri-list", FALSE) && data) {
2589 GtkTreePath *path = NULL;
2590 GtkTreeViewDropPosition position;
2592 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(widget),
2593 x, y, &path, &position))
2595 GtkTreeIter iter;
2596 PurpleBlistNode *node;
2598 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel),
2599 &iter, path);
2600 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel),
2601 &iter, NODE_COLUMN, &node, -1);
2603 if (PURPLE_IS_BUDDY(node) || PURPLE_IS_CONTACT(node)) {
2604 PurpleBuddy *b = PURPLE_IS_BUDDY(node) ? PURPLE_BUDDY(node) : purple_contact_get_priority_buddy(PURPLE_CONTACT(node));
2605 pidgin_dnd_file_manage(sd, purple_buddy_get_account(b), purple_buddy_get_name(b));
2606 gtk_drag_finish(dc, TRUE,
2607 gdk_drag_context_get_actions(dc) == GDK_ACTION_MOVE, t);
2608 } else {
2609 gtk_drag_finish(dc, FALSE, FALSE, t);
2615 /* Altered from do_colorshift in gnome-panel */
2616 static void
2617 do_alphashift(GdkPixbuf *pixbuf, int shift)
2619 gint i, j;
2620 gint width, height, padding;
2621 guchar *pixels;
2622 int val;
2624 if (!gdk_pixbuf_get_has_alpha(pixbuf))
2625 return;
2627 width = gdk_pixbuf_get_width(pixbuf);
2628 height = gdk_pixbuf_get_height(pixbuf);
2629 padding = gdk_pixbuf_get_rowstride(pixbuf) - width * 4;
2630 pixels = gdk_pixbuf_get_pixels(pixbuf);
2632 for (i = 0; i < height; i++) {
2633 for (j = 0; j < width; j++) {
2634 pixels++;
2635 pixels++;
2636 pixels++;
2637 val = *pixels - shift;
2638 *(pixels++) = CLAMP(val, 0, 255);
2640 pixels += padding;
2645 static GdkPixbuf *pidgin_blist_get_buddy_icon(PurpleBlistNode *node,
2646 gboolean scaled, gboolean greyed)
2648 gsize len;
2649 PurpleBuddy *buddy = NULL;
2650 PurpleGroup *group = NULL;
2651 const guchar *data = NULL;
2652 GdkPixbuf *buf, *ret = NULL;
2653 PurpleBuddyIcon *icon = NULL;
2654 PurpleAccount *account = NULL;
2655 PurpleContact *contact = NULL;
2656 PurpleImage *custom_img;
2657 PurpleProtocol *protocol = NULL;
2658 PurpleBuddyIconSpec *icon_spec = NULL;
2659 gint orig_width, orig_height, scale_width, scale_height;
2661 if (PURPLE_IS_CONTACT(node)) {
2662 buddy = purple_contact_get_priority_buddy((PurpleContact*)node);
2663 contact = (PurpleContact*)node;
2664 } else if (PURPLE_IS_BUDDY(node)) {
2665 buddy = (PurpleBuddy*)node;
2666 contact = purple_buddy_get_contact(buddy);
2667 } else if (PURPLE_IS_GROUP(node)) {
2668 group = (PurpleGroup*)node;
2669 } else if (PURPLE_IS_CHAT(node)) {
2670 /* We don't need to do anything here. We just need to not fall
2671 * into the else block and return. */
2672 } else {
2673 return NULL;
2676 if (buddy) {
2677 account = purple_buddy_get_account(buddy);
2680 if(account && purple_account_get_connection(account)) {
2681 protocol = purple_connection_get_protocol(purple_account_get_connection(account));
2684 #if 0
2685 if (!purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons"))
2686 return NULL;
2687 #endif
2689 /* If we have a contact then this is either a contact or a buddy and
2690 * we want to fetch the custom icon for the contact. If we don't have
2691 * a contact then this is a group or some other type of node and we
2692 * want to use that directly. */
2693 if (contact) {
2694 custom_img = purple_buddy_icons_node_find_custom_icon(PURPLE_BLIST_NODE(contact));
2695 } else {
2696 custom_img = purple_buddy_icons_node_find_custom_icon(node);
2699 if (custom_img) {
2700 data = purple_image_get_data(custom_img);
2701 len = purple_image_get_data_size(custom_img);
2704 if (data == NULL) {
2705 if (buddy) {
2706 /* Not sure I like this...*/
2707 if (!(icon = purple_buddy_icons_find(purple_buddy_get_account(buddy), purple_buddy_get_name(buddy))))
2708 return NULL;
2709 data = purple_buddy_icon_get_data(icon, &len);
2712 if(data == NULL)
2713 return NULL;
2716 buf = pidgin_pixbuf_from_data(data, len);
2717 purple_buddy_icon_unref(icon);
2718 if (!buf) {
2719 purple_debug_warning("gtkblist", "Couldn't load buddy icon on "
2720 "account %s (%s); buddyname=%s; custom_img_size=%" G_GSIZE_FORMAT,
2721 account ? purple_account_get_username(account) : "(no account)",
2722 account ? purple_account_get_protocol_id(account) : "(no account)",
2723 buddy ? purple_buddy_get_name(buddy) : "(no buddy)",
2724 custom_img ? purple_image_get_data_size(custom_img) : 0);
2725 if (custom_img)
2726 g_object_unref(custom_img);
2727 return NULL;
2729 if (custom_img)
2730 g_object_unref(custom_img);
2732 if (greyed) {
2733 gboolean offline = FALSE, idle = FALSE;
2735 if (buddy) {
2736 PurplePresence *presence = purple_buddy_get_presence(buddy);
2737 if (!PURPLE_BUDDY_IS_ONLINE(buddy))
2738 offline = TRUE;
2739 if (purple_presence_is_idle(presence))
2740 idle = TRUE;
2741 } else if (group) {
2742 if (purple_counting_node_get_online_count(PURPLE_COUNTING_NODE(group)) == 0)
2743 offline = TRUE;
2746 if (offline)
2747 gdk_pixbuf_saturate_and_pixelate(buf, buf, 0.0, FALSE);
2749 if (idle)
2750 gdk_pixbuf_saturate_and_pixelate(buf, buf, 0.25, FALSE);
2753 /* I'd use the pidgin_buddy_icon_get_scale_size() thing, but it won't
2754 * tell me the original size, which I need for scaling purposes. */
2755 scale_width = orig_width = gdk_pixbuf_get_width(buf);
2756 scale_height = orig_height = gdk_pixbuf_get_height(buf);
2758 if (protocol)
2759 icon_spec = purple_protocol_get_icon_spec(protocol);
2761 if (icon_spec && icon_spec->scale_rules & PURPLE_ICON_SCALE_DISPLAY)
2762 purple_buddy_icon_spec_get_scaled_size(purple_protocol_get_icon_spec(protocol), &scale_width, &scale_height);
2764 if (scaled || scale_height > 200 || scale_width > 200) {
2765 GdkPixbuf *tmpbuf;
2766 float scale_size = scaled ? 32.0 : 200.0;
2767 if(scale_height > scale_width) {
2768 scale_width = scale_size * (double)scale_width / (double)scale_height;
2769 scale_height = scale_size;
2770 } else {
2771 scale_height = scale_size * (double)scale_height / (double)scale_width;
2772 scale_width = scale_size;
2774 /* Scale & round before making square, so rectangular (but
2775 * non-square) images get rounded corners too. */
2776 tmpbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, scale_width, scale_height);
2777 gdk_pixbuf_fill(tmpbuf, 0x00000000);
2778 gdk_pixbuf_scale(buf, tmpbuf, 0, 0, scale_width, scale_height, 0, 0, (double)scale_width/(double)orig_width, (double)scale_height/(double)orig_height, GDK_INTERP_BILINEAR);
2779 if (pidgin_gdk_pixbuf_is_opaque(tmpbuf))
2780 pidgin_gdk_pixbuf_make_round(tmpbuf);
2781 ret = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, scale_size, scale_size);
2782 gdk_pixbuf_fill(ret, 0x00000000);
2783 gdk_pixbuf_copy_area(tmpbuf, 0, 0, scale_width, scale_height, ret, (scale_size-scale_width)/2, (scale_size-scale_height)/2);
2784 g_object_unref(G_OBJECT(tmpbuf));
2785 } else {
2786 ret = gdk_pixbuf_scale_simple(buf,scale_width,scale_height, GDK_INTERP_BILINEAR);
2788 g_object_unref(G_OBJECT(buf));
2790 return ret;
2793 /* # - Status Icon
2794 * P - Protocol Icon
2795 * A - Buddy Icon
2796 * [ - SMALL_SPACE
2797 * = - LARGE_SPACE
2798 * +--- STATUS_SIZE +--- td->avatar_width
2799 * | +-- td->name_width |
2800 * +----+ +-------+ +---------+
2801 * | | | | | |
2802 * +-------------------------------------------+
2803 * | [ = [ |--- TOOLTIP_BORDER
2804 *name_height --+-| ######[BuddyName = PP [ AAAAAAAAAAA |--+
2805 * | | ######[ = PP [ AAAAAAAAAAA | |
2806 * STATUS SIZE -| | ######[[[[[[[[[[[[[[[[[[[[[ AAAAAAAAAAA | |
2807 * +--+-| ######[Account: So-and-so [ AAAAAAAAAAA | |-- td->avatar_height
2808 * | | [Idle: 4h 15m [ AAAAAAAAAAA | |
2809 * height --+ | [Foo: Bar, Baz [ AAAAAAAAAAA | |
2810 * | | [Status: Awesome [ AAAAAAAAAAA |--+
2811 * +----| [Stop: Hammer Time [ |
2812 * | [ [ |--- TOOLTIP_BORDER
2813 * +-------------------------------------------+
2814 * | | | |
2815 * | +----------------+ |
2816 * | | |
2817 * | +-- td->width |
2818 * | |
2819 * +---- TOOLTIP_BORDER +---- TOOLTIP_BORDER
2823 #define STATUS_SIZE 16
2824 #define TOOLTIP_BORDER 12
2825 #define SMALL_SPACE 6
2826 #define LARGE_SPACE 12
2827 #define PROTOCOL_SIZE 16
2828 struct tooltip_data {
2829 PangoLayout *layout;
2830 PangoLayout *name_layout;
2831 GdkPixbuf *protocol_icon;
2832 GdkPixbuf *status_icon;
2833 GdkPixbuf *avatar;
2834 gboolean avatar_is_protocol_icon;
2835 int avatar_width;
2836 int avatar_height;
2837 int name_height;
2838 int name_width;
2839 int width;
2840 int height;
2841 int padding;
2844 static PangoLayout * create_pango_layout(const char *markup, int *width, int *height)
2846 PangoLayout *layout;
2847 int w, h;
2849 layout = gtk_widget_create_pango_layout(gtkblist->tipwindow, NULL);
2850 pango_layout_set_markup(layout, markup, -1);
2851 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
2852 pango_layout_set_width(layout, 300000);
2854 pango_layout_get_size (layout, &w, &h);
2855 if (width)
2856 *width = PANGO_PIXELS(w);
2857 if (height)
2858 *height = PANGO_PIXELS(h);
2859 return layout;
2862 static struct tooltip_data * create_tip_for_account(PurpleAccount *account)
2864 struct tooltip_data *td = g_new0(struct tooltip_data, 1);
2865 td->status_icon = pidgin_create_protocol_icon(account, PIDGIN_PROTOCOL_ICON_SMALL);
2866 /* Yes, status_icon, not protocol_icon */
2867 if (purple_account_is_disconnected(account))
2868 gdk_pixbuf_saturate_and_pixelate(td->status_icon, td->status_icon, 0.0, FALSE);
2869 td->layout = create_pango_layout(purple_account_get_username(account), &td->width, &td->height);
2870 td->padding = SMALL_SPACE;
2871 return td;
2874 static struct tooltip_data * create_tip_for_node(PurpleBlistNode *node, gboolean full)
2876 struct tooltip_data *td = g_new0(struct tooltip_data, 1);
2877 PurpleAccount *account = NULL;
2878 char *tmp = NULL, *node_name = NULL, *tooltip_text = NULL;
2880 if (PURPLE_IS_BUDDY(node)) {
2881 account = purple_buddy_get_account((PurpleBuddy*)(node));
2882 } else if (PURPLE_IS_CHAT(node)) {
2883 account = purple_chat_get_account((PurpleChat*)(node));
2886 td->padding = TOOLTIP_BORDER;
2887 td->status_icon = pidgin_blist_get_status_icon(node, PIDGIN_STATUS_ICON_LARGE);
2888 td->avatar = pidgin_blist_get_buddy_icon(node, !full, FALSE);
2889 if (account != NULL) {
2890 td->protocol_icon = pidgin_create_protocol_icon(account, PIDGIN_PROTOCOL_ICON_SMALL);
2892 tooltip_text = pidgin_get_tooltip_text(node, full);
2893 if (tooltip_text && *tooltip_text) {
2894 td->layout = create_pango_layout(tooltip_text, &td->width, &td->height);
2897 if (PURPLE_IS_BUDDY(node)) {
2898 tmp = g_markup_escape_text(purple_buddy_get_name((PurpleBuddy*)node), -1);
2899 } else if (PURPLE_IS_CHAT(node)) {
2900 tmp = g_markup_escape_text(purple_chat_get_name((PurpleChat*)node), -1);
2901 } else if (PURPLE_IS_GROUP(node)) {
2902 tmp = g_markup_escape_text(purple_group_get_name((PurpleGroup*)node), -1);
2903 } else {
2904 /* I don't believe this can happen currently, I think
2905 * everything that calls this function checks for one of the
2906 * above node types first. */
2907 tmp = g_strdup(_("Unknown node type"));
2909 node_name = g_strdup_printf("<span size='x-large' weight='bold'>%s</span>",
2910 tmp ? tmp : "");
2911 g_free(tmp);
2913 td->name_layout = create_pango_layout(node_name, &td->name_width, &td->name_height);
2914 td->name_width += SMALL_SPACE + PROTOCOL_SIZE;
2915 td->name_height = MAX(td->name_height, PROTOCOL_SIZE + SMALL_SPACE);
2916 #if 0 /* Protocol Icon as avatar */
2917 if(!td->avatar && full) {
2918 td->avatar = pidgin_create_protocol_icon(account, PIDGIN_PROTOCOL_ICON_LARGE);
2919 td->avatar_is_protocol_icon = TRUE;
2921 #endif
2923 if (td->avatar) {
2924 td->avatar_width = gdk_pixbuf_get_width(td->avatar);
2925 td->avatar_height = gdk_pixbuf_get_height(td->avatar);
2928 g_free(node_name);
2929 g_free(tooltip_text);
2930 return td;
2933 static gboolean
2934 pidgin_blist_paint_tip(GtkWidget *widget, cairo_t *cr, gpointer null)
2936 GtkStyleContext *context;
2937 int current_height, max_width;
2938 int max_text_width;
2939 int max_avatar_width;
2940 GList *l;
2941 int protocol_col = 0;
2942 GtkTextDirection dir = gtk_widget_get_direction(widget);
2943 int status_size = 0;
2945 if(gtkblist->tooltipdata == NULL)
2946 return FALSE;
2948 context = gtk_widget_get_style_context(gtkblist->tipwindow);
2949 gtk_style_context_add_class(context, GTK_STYLE_CLASS_TOOLTIP);
2951 max_text_width = 0;
2952 max_avatar_width = 0;
2954 for(l = gtkblist->tooltipdata; l; l = l->next)
2956 struct tooltip_data *td = l->data;
2958 max_text_width = MAX(max_text_width,
2959 MAX(td->width, td->name_width));
2960 max_avatar_width = MAX(max_avatar_width, td->avatar_width);
2961 if (td->status_icon)
2962 status_size = STATUS_SIZE;
2965 max_width = TOOLTIP_BORDER + status_size + SMALL_SPACE + max_text_width + SMALL_SPACE + max_avatar_width + TOOLTIP_BORDER;
2966 if (dir == GTK_TEXT_DIR_RTL)
2967 protocol_col = TOOLTIP_BORDER + max_avatar_width + SMALL_SPACE;
2968 else
2969 protocol_col = TOOLTIP_BORDER + status_size + SMALL_SPACE + max_text_width - PROTOCOL_SIZE;
2971 current_height = 12;
2972 for(l = gtkblist->tooltipdata; l; l = l->next)
2974 struct tooltip_data *td = l->data;
2976 if (td->avatar && pidgin_gdk_pixbuf_is_opaque(td->avatar))
2978 gtk_style_context_save(context);
2979 gtk_style_context_add_class(context, GTK_STYLE_CLASS_FRAME);
2980 if (dir == GTK_TEXT_DIR_RTL) {
2981 gtk_render_frame(context, cr,
2982 TOOLTIP_BORDER - 1, current_height - 1,
2983 td->avatar_width + 2, td->avatar_height + 2);
2984 } else {
2985 gtk_render_frame(context, cr,
2986 max_width - (td->avatar_width + TOOLTIP_BORDER) - 1,
2987 current_height - 1,
2988 td->avatar_width + 2, td->avatar_height + 2);
2990 gtk_style_context_restore(context);
2993 if (td->status_icon) {
2994 if (dir == GTK_TEXT_DIR_RTL) {
2995 gdk_cairo_set_source_pixbuf(cr, td->status_icon,
2996 max_width - TOOLTIP_BORDER - status_size,
2997 current_height);
2998 cairo_paint(cr);
2999 } else {
3000 gdk_cairo_set_source_pixbuf(cr, td->status_icon,
3001 TOOLTIP_BORDER, current_height);
3002 cairo_paint(cr);
3006 if (td->avatar) {
3007 if (dir == GTK_TEXT_DIR_RTL) {
3008 gdk_cairo_set_source_pixbuf(cr, td->avatar,
3009 TOOLTIP_BORDER, current_height);
3010 cairo_paint(cr);
3011 } else {
3012 gdk_cairo_set_source_pixbuf(cr, td->avatar,
3013 max_width - (td->avatar_width + TOOLTIP_BORDER),
3014 current_height);
3015 cairo_paint(cr);
3019 if (!td->avatar_is_protocol_icon && td->protocol_icon) {
3020 gdk_cairo_set_source_pixbuf(cr, td->protocol_icon, protocol_col,
3021 current_height +
3022 (td->name_height - PROTOCOL_SIZE) / 2);
3023 cairo_paint(cr);
3026 if (td->name_layout) {
3027 if (dir == GTK_TEXT_DIR_RTL) {
3028 gtk_render_layout(context, cr,
3029 max_width - (TOOLTIP_BORDER + status_size + SMALL_SPACE) - PANGO_PIXELS(300000),
3030 current_height, td->name_layout);
3031 } else {
3032 gtk_render_layout(context, cr,
3033 TOOLTIP_BORDER + status_size + SMALL_SPACE,
3034 current_height, td->name_layout);
3038 if (td->layout) {
3039 if (dir != GTK_TEXT_DIR_RTL) {
3040 gtk_render_layout(context, cr,
3041 TOOLTIP_BORDER + status_size + SMALL_SPACE,
3042 current_height + td->name_height,
3043 td->layout);
3044 } else {
3045 gtk_render_layout(context, cr,
3046 max_width - (TOOLTIP_BORDER + status_size + SMALL_SPACE) - PANGO_PIXELS(300000),
3047 current_height + td->name_height,
3048 td->layout);
3052 current_height += MAX(td->name_height + td->height, td->avatar_height) + td->padding;
3055 return FALSE;
3058 static void
3059 pidgin_blist_destroy_tooltip_data(void)
3061 while(gtkblist->tooltipdata) {
3062 struct tooltip_data *td = gtkblist->tooltipdata->data;
3064 if(td->avatar)
3065 g_object_unref(td->avatar);
3066 if(td->status_icon)
3067 g_object_unref(td->status_icon);
3068 if(td->protocol_icon)
3069 g_object_unref(td->protocol_icon);
3070 if (td->layout)
3071 g_object_unref(td->layout);
3072 if (td->name_layout)
3073 g_object_unref(td->name_layout);
3074 g_free(td);
3075 gtkblist->tooltipdata = g_list_delete_link(gtkblist->tooltipdata, gtkblist->tooltipdata);
3079 void pidgin_blist_tooltip_destroy()
3081 pidgin_blist_destroy_tooltip_data();
3082 pidgin_tooltip_destroy();
3085 static void
3086 pidgin_blist_align_tooltip(struct tooltip_data *td, GtkWidget *widget)
3088 GtkTextDirection dir = gtk_widget_get_direction(widget);
3090 if (dir == GTK_TEXT_DIR_RTL)
3092 char* layout_name = purple_markup_strip_html(pango_layout_get_text(td->name_layout));
3093 PangoDirection dir = pango_find_base_dir(layout_name, -1);
3094 if (dir == PANGO_DIRECTION_RTL || dir == PANGO_DIRECTION_NEUTRAL)
3095 pango_layout_set_alignment(td->name_layout, PANGO_ALIGN_RIGHT);
3096 g_free(layout_name);
3097 pango_layout_set_alignment(td->layout, PANGO_ALIGN_RIGHT);
3101 static gboolean
3102 pidgin_blist_create_tooltip_for_node(GtkWidget *widget, gpointer data, int *w, int *h)
3104 PurpleBlistNode *node = data;
3105 int width, height;
3106 GList *list;
3107 int max_text_width = 0;
3108 int max_avatar_width = 0;
3109 int status_size = 0;
3111 if (gtkblist->tooltipdata) {
3112 gtkblist->tipwindow = NULL;
3113 pidgin_blist_destroy_tooltip_data();
3116 gtkblist->tipwindow = widget;
3117 if (PURPLE_IS_CHAT(node) ||
3118 PURPLE_IS_BUDDY(node)) {
3119 struct tooltip_data *td = create_tip_for_node(node, TRUE);
3120 pidgin_blist_align_tooltip(td, gtkblist->tipwindow);
3121 gtkblist->tooltipdata = g_list_append(gtkblist->tooltipdata, td);
3122 } else if (PURPLE_IS_GROUP(node)) {
3123 PurpleGroup *group = (PurpleGroup*)node;
3124 GSList *accounts;
3125 struct tooltip_data *td = create_tip_for_node(node, TRUE);
3126 pidgin_blist_align_tooltip(td, gtkblist->tipwindow);
3127 gtkblist->tooltipdata = g_list_append(gtkblist->tooltipdata, td);
3129 /* Accounts with buddies in group */
3130 accounts = purple_group_get_accounts(group);
3131 for (; accounts != NULL;
3132 accounts = g_slist_delete_link(accounts, accounts)) {
3133 PurpleAccount *account = accounts->data;
3134 td = create_tip_for_account(account);
3135 gtkblist->tooltipdata = g_list_append(gtkblist->tooltipdata, td);
3137 } else if (PURPLE_IS_CONTACT(node)) {
3138 PurpleBlistNode *child;
3139 PurpleBuddy *b = purple_contact_get_priority_buddy((PurpleContact *)node);
3141 for(child = node->child; child; child = child->next)
3143 if(PURPLE_IS_BUDDY(child) && buddy_is_displayable((PurpleBuddy*)child)) {
3144 struct tooltip_data *td = create_tip_for_node(child, (b == (PurpleBuddy*)child));
3145 pidgin_blist_align_tooltip(td, gtkblist->tipwindow);
3146 if (b == (PurpleBuddy *)child) {
3147 gtkblist->tooltipdata = g_list_prepend(gtkblist->tooltipdata, td);
3148 } else {
3149 gtkblist->tooltipdata = g_list_append(gtkblist->tooltipdata, td);
3153 } else {
3154 return FALSE;
3157 height = width = 0;
3158 for (list = gtkblist->tooltipdata; list; list = list->next) {
3159 struct tooltip_data *td = list->data;
3160 max_text_width = MAX(max_text_width, MAX(td->width, td->name_width));
3161 max_avatar_width = MAX(max_avatar_width, td->avatar_width);
3162 height += MAX(MAX(STATUS_SIZE, td->avatar_height), td->height + td->name_height) + td->padding;
3163 if (td->status_icon)
3164 status_size = MAX(status_size, STATUS_SIZE);
3166 height += TOOLTIP_BORDER;
3167 width = TOOLTIP_BORDER + status_size + SMALL_SPACE + max_text_width + SMALL_SPACE + max_avatar_width + TOOLTIP_BORDER;
3169 if (w)
3170 *w = width;
3171 if (h)
3172 *h = height;
3174 return TRUE;
3177 static gboolean pidgin_blist_expand_timeout(GtkWidget *tv)
3179 GtkTreePath *path;
3180 GtkTreeIter iter;
3181 PurpleBlistNode *node;
3182 PidginBlistNode *gtknode;
3184 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(tv), gtkblist->tip_rect.x, gtkblist->tip_rect.y + (gtkblist->tip_rect.height/2),
3185 &path, NULL, NULL, NULL))
3186 return FALSE;
3187 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), &iter, path);
3188 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
3190 if(!PURPLE_IS_CONTACT(node)) {
3191 gtk_tree_path_free(path);
3192 return FALSE;
3195 gtknode = purple_blist_node_get_ui_data(node);
3197 if (!gtknode->contact_expanded) {
3198 GtkTreeIter i;
3200 pidgin_blist_expand_contact_cb(NULL, node);
3202 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(tv), path, NULL, &gtkblist->contact_rect);
3203 gtkblist->contact_rect.width =
3204 gdk_window_get_width(gtk_widget_get_window(tv));
3205 gtkblist->mouseover_contact = node;
3206 gtk_tree_path_down (path);
3207 while (gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), &i, path)) {
3208 GdkRectangle rect;
3209 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(tv), path, NULL, &rect);
3210 gtkblist->contact_rect.height += rect.height;
3211 gtk_tree_path_next(path);
3214 gtk_tree_path_free(path);
3215 return FALSE;
3218 static gboolean buddy_is_displayable(PurpleBuddy *buddy)
3220 PidginBlistNode *gtknode;
3222 if(!buddy)
3223 return FALSE;
3225 gtknode = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(buddy));
3227 return (purple_account_is_connected(purple_buddy_get_account(buddy)) &&
3228 (purple_presence_is_online(purple_buddy_get_presence(buddy)) ||
3229 (gtknode && gtknode->recent_signonoff) ||
3230 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_offline_buddies") ||
3231 purple_blist_node_get_bool(PURPLE_BLIST_NODE(buddy), "show_offline")));
3234 void pidgin_blist_draw_tooltip(PurpleBlistNode *node, GtkWidget *widget)
3236 pidgin_tooltip_show(widget, node, pidgin_blist_create_tooltip_for_node, pidgin_blist_paint_tip);
3239 static gboolean pidgin_blist_drag_motion_cb(GtkWidget *tv, GdkDragContext *drag_context,
3240 gint x, gint y, guint time, gpointer user_data)
3242 GtkTreePath *path;
3243 int delay;
3244 GdkRectangle rect;
3247 * When dragging a buddy into a contact, this is the delay before
3248 * the contact auto-expands.
3250 delay = 900;
3252 if (gtkblist->drag_timeout) {
3253 if ((y > gtkblist->tip_rect.y) && ((y - gtkblist->tip_rect.height) < gtkblist->tip_rect.y))
3254 return FALSE;
3255 /* We've left the cell. Remove the timeout and create a new one below */
3256 g_source_remove(gtkblist->drag_timeout);
3259 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(tv), x, y, &path, NULL, NULL, NULL);
3260 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(tv), path, NULL, &rect);
3262 if (path)
3263 gtk_tree_path_free(path);
3265 /* Only autoexpand when in the middle of the cell to avoid annoying un-intended expands */
3266 if (y < rect.y + (rect.height / 3) ||
3267 y > rect.y + (2 * (rect.height /3)))
3268 return FALSE;
3270 rect.height = rect.height / 3;
3271 rect.y += rect.height;
3273 gtkblist->tip_rect = rect;
3275 gtkblist->drag_timeout = g_timeout_add(delay, (GSourceFunc)pidgin_blist_expand_timeout, tv);
3277 if (gtkblist->mouseover_contact) {
3278 if ((y < gtkblist->contact_rect.y) || ((y - gtkblist->contact_rect.height) > gtkblist->contact_rect.y)) {
3279 pidgin_blist_collapse_contact_cb(NULL, gtkblist->mouseover_contact);
3280 gtkblist->mouseover_contact = NULL;
3284 return FALSE;
3287 static gboolean
3288 pidgin_blist_create_tooltip(GtkWidget *widget, GtkTreePath *path,
3289 gpointer null, int *w, int *h)
3291 GtkTreeIter iter;
3292 PurpleBlistNode *node;
3293 gboolean editable = FALSE;
3295 /* If we're editing a cell (e.g. alias editing), don't show the tooltip */
3296 g_object_get(G_OBJECT(gtkblist->text_rend), "editable", &editable, NULL);
3297 if (editable)
3298 return FALSE;
3300 if (gtkblist->tooltipdata) {
3301 gtkblist->tipwindow = NULL;
3302 pidgin_blist_destroy_tooltip_data();
3305 gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), &iter, path);
3306 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
3308 return pidgin_blist_create_tooltip_for_node(widget, node, w, h);
3311 static gboolean pidgin_blist_motion_cb (GtkWidget *tv, GdkEventMotion *event, gpointer null)
3313 if (gtkblist->mouseover_contact) {
3314 if ((event->y < gtkblist->contact_rect.y) || ((event->y - gtkblist->contact_rect.height) > gtkblist->contact_rect.y)) {
3315 pidgin_blist_collapse_contact_cb(NULL, gtkblist->mouseover_contact);
3316 gtkblist->mouseover_contact = NULL;
3320 return FALSE;
3323 static gboolean pidgin_blist_leave_cb (GtkWidget *w, GdkEventCrossing *e, gpointer n)
3325 if (gtkblist->timeout) {
3326 g_source_remove(gtkblist->timeout);
3327 gtkblist->timeout = 0;
3330 if (gtkblist->drag_timeout) {
3331 g_source_remove(gtkblist->drag_timeout);
3332 gtkblist->drag_timeout = 0;
3335 if (gtkblist->mouseover_contact &&
3336 !((e->x > gtkblist->contact_rect.x) && (e->x < (gtkblist->contact_rect.x + gtkblist->contact_rect.width)) &&
3337 (e->y > gtkblist->contact_rect.y) && (e->y < (gtkblist->contact_rect.y + gtkblist->contact_rect.height)))) {
3338 pidgin_blist_collapse_contact_cb(NULL, gtkblist->mouseover_contact);
3339 gtkblist->mouseover_contact = NULL;
3341 return FALSE;
3344 static void
3345 toggle_debug(void)
3347 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/debug/enabled",
3348 !purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/enabled"));
3351 static char *get_mood_icon_path(const char *mood)
3353 char *path;
3355 if (purple_strequal(mood, "busy")) {
3356 path = g_build_filename(PURPLE_DATADIR, "pidgin", "icons",
3357 "hicolor", "16x16", "status", "user-busy.png", NULL);
3358 } else if (purple_strequal(mood, "hiptop")) {
3359 path = g_build_filename(PURPLE_DATADIR, "pidgin", "icons",
3360 "hicolor", "16x16", "emblems", "emblem-hiptop.png",
3361 NULL);
3362 } else {
3363 char *filename = g_strdup_printf("%s.png", mood);
3364 path = g_build_filename(PURPLE_DATADIR, "pixmaps", "pidgin",
3365 "emotes", "small", filename, NULL);
3366 g_free(filename);
3368 return path;
3371 static void
3372 update_status_with_mood(PurpleAccount *account, const gchar *mood,
3373 const gchar *text)
3375 if (mood && *mood) {
3376 if (text) {
3377 purple_account_set_status(account, "mood", TRUE,
3378 PURPLE_MOOD_NAME, mood,
3379 PURPLE_MOOD_COMMENT, text,
3380 NULL);
3381 } else {
3382 purple_account_set_status(account, "mood", TRUE,
3383 PURPLE_MOOD_NAME, mood,
3384 NULL);
3386 } else {
3387 purple_account_set_status(account, "mood", FALSE, NULL);
3391 static void
3392 edit_mood_cb(PurpleConnection *gc, PurpleRequestFields *fields)
3394 PurpleRequestField *mood_field;
3395 GList *l;
3397 mood_field = purple_request_fields_get_field(fields, "mood");
3398 l = purple_request_field_list_get_selected(mood_field);
3400 if (l) {
3401 const char *mood = purple_request_field_list_get_data(mood_field, l->data);
3403 if (gc) {
3404 const char *text;
3405 PurpleAccount *account = purple_connection_get_account(gc);
3407 if (purple_connection_get_flags(gc) & PURPLE_CONNECTION_FLAG_SUPPORT_MOOD_MESSAGES) {
3408 PurpleRequestField *text_field;
3409 text_field = purple_request_fields_get_field(fields, "text");
3410 text = purple_request_field_string_get_value(text_field);
3411 } else {
3412 text = NULL;
3415 update_status_with_mood(account, mood, text);
3416 } else {
3417 GList *accounts = purple_accounts_get_all_active();
3419 for (; accounts ; accounts = g_list_delete_link(accounts, accounts)) {
3420 PurpleAccount *account = (PurpleAccount *) accounts->data;
3421 PurpleConnection *gc = purple_account_get_connection(account);
3423 if (gc && (purple_connection_get_flags(gc) & PURPLE_CONNECTION_FLAG_SUPPORT_MOODS)) {
3424 update_status_with_mood(account, mood, NULL);
3431 static void
3432 global_moods_for_each(gpointer key, gpointer value, gpointer user_data)
3434 GList **out_moods = (GList **) user_data;
3435 PurpleMood *mood = (PurpleMood *) value;
3437 *out_moods = g_list_append(*out_moods, mood);
3440 static PurpleMood *
3441 get_global_moods(void)
3443 GHashTable *global_moods =
3444 g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
3445 GHashTable *mood_counts =
3446 g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
3447 GList *accounts = purple_accounts_get_all_active();
3448 PurpleMood *result = NULL;
3449 GList *out_moods = NULL;
3450 int i = 0;
3451 int num_accounts = 0;
3453 for (; accounts ; accounts = g_list_delete_link(accounts, accounts)) {
3454 PurpleAccount *account = (PurpleAccount *) accounts->data;
3455 if (purple_account_is_connected(account)) {
3456 PurpleConnection *gc = purple_account_get_connection(account);
3458 if (purple_connection_get_flags(gc) & PURPLE_CONNECTION_FLAG_SUPPORT_MOODS) {
3459 PurpleProtocol *protocol = purple_connection_get_protocol(gc);
3460 PurpleMood *mood = NULL;
3462 for (mood = purple_protocol_client_iface_get_moods(protocol, account) ;
3463 mood->mood != NULL ; mood++) {
3464 int mood_count =
3465 GPOINTER_TO_INT(g_hash_table_lookup(mood_counts, mood->mood));
3467 if (!g_hash_table_lookup(global_moods, mood->mood)) {
3468 g_hash_table_insert(global_moods, (gpointer)mood->mood, mood);
3470 g_hash_table_insert(mood_counts, (gpointer)mood->mood,
3471 GINT_TO_POINTER(mood_count + 1));
3474 num_accounts++;
3479 g_hash_table_foreach(global_moods, global_moods_for_each, &out_moods);
3480 result = g_new0(PurpleMood, g_hash_table_size(global_moods) + 1);
3482 while (out_moods) {
3483 PurpleMood *mood = (PurpleMood *) out_moods->data;
3484 int in_num_accounts =
3485 GPOINTER_TO_INT(g_hash_table_lookup(mood_counts, mood->mood));
3487 if (in_num_accounts == num_accounts) {
3488 /* mood is present in all accounts supporting moods */
3489 result[i].mood = mood->mood;
3490 result[i].description = mood->description;
3491 i++;
3493 out_moods = g_list_delete_link(out_moods, out_moods);
3496 g_hash_table_destroy(global_moods);
3497 g_hash_table_destroy(mood_counts);
3499 return result;
3502 /* get current set mood for all mood-supporting accounts, or NULL if not set
3503 or not set to the same on all */
3504 static const gchar *
3505 get_global_mood_status(void)
3507 GList *accounts = purple_accounts_get_all_active();
3508 const gchar *found_mood = NULL;
3510 for (; accounts ; accounts = g_list_delete_link(accounts, accounts)) {
3511 PurpleAccount *account = (PurpleAccount *) accounts->data;
3513 if (purple_account_is_connected(account) &&
3514 (purple_connection_get_flags(purple_account_get_connection(account)) &
3515 PURPLE_CONNECTION_FLAG_SUPPORT_MOODS)) {
3516 PurplePresence *presence = purple_account_get_presence(account);
3517 PurpleStatus *status = purple_presence_get_status(presence, "mood");
3518 const gchar *curr_mood = purple_status_get_attr_string(status, PURPLE_MOOD_NAME);
3520 if (found_mood != NULL && !purple_strequal(curr_mood, found_mood)) {
3521 /* found a different mood */
3522 found_mood = NULL;
3523 break;
3524 } else {
3525 found_mood = curr_mood;
3530 return found_mood;
3533 static void
3534 set_mood_cb(GtkWidget *widget, PurpleAccount *account)
3536 const char *current_mood;
3537 PurpleRequestFields *fields;
3538 PurpleRequestFieldGroup *g;
3539 PurpleRequestField *f;
3540 PurpleConnection *gc = NULL;
3541 PurpleProtocol *protocol = NULL;
3542 PurpleMood *mood;
3543 PurpleMood *global_moods = get_global_moods();
3545 if (account) {
3546 PurplePresence *presence = purple_account_get_presence(account);
3547 PurpleStatus *status = purple_presence_get_status(presence, "mood");
3548 gc = purple_account_get_connection(account);
3549 g_return_if_fail(purple_connection_get_protocol(gc) != NULL);
3550 protocol = purple_connection_get_protocol(gc);
3551 current_mood = purple_status_get_attr_string(status, PURPLE_MOOD_NAME);
3552 } else {
3553 current_mood = get_global_mood_status();
3556 fields = purple_request_fields_new();
3557 g = purple_request_field_group_new(NULL);
3558 f = purple_request_field_list_new("mood", _("Please select your mood from the list"));
3560 purple_request_field_list_add_icon(f, _("None"), NULL, "");
3561 if (current_mood == NULL)
3562 purple_request_field_list_add_selected(f, _("None"));
3564 /* TODO: rlaager wants this sorted. */
3565 /* TODO: darkrain wants it sorted post-translation */
3566 if (account && PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, get_moods))
3567 mood = purple_protocol_client_iface_get_moods(protocol, account);
3568 else
3569 mood = global_moods;
3570 for ( ; mood->mood != NULL ; mood++) {
3571 char *path;
3573 if (mood->mood == NULL || mood->description == NULL)
3574 continue;
3576 path = get_mood_icon_path(mood->mood);
3577 purple_request_field_list_add_icon(f, _(mood->description),
3578 path, (gpointer)mood->mood);
3579 g_free(path);
3581 if (current_mood && purple_strequal(current_mood, mood->mood))
3582 purple_request_field_list_add_selected(f, _(mood->description));
3584 purple_request_field_group_add_field(g, f);
3586 purple_request_fields_add_group(fields, g);
3588 /* if the connection allows setting a mood message */
3589 if (gc && (purple_connection_get_flags(gc) & PURPLE_CONNECTION_FLAG_SUPPORT_MOOD_MESSAGES)) {
3590 g = purple_request_field_group_new(NULL);
3591 f = purple_request_field_string_new("text",
3592 _("Message (optional)"), NULL, FALSE);
3593 purple_request_field_group_add_field(g, f);
3594 purple_request_fields_add_group(fields, g);
3597 purple_request_fields(gc, _("Edit User Mood"), _("Edit User Mood"),
3598 NULL, fields,
3599 _("OK"), G_CALLBACK(edit_mood_cb),
3600 _("Cancel"), NULL,
3601 purple_request_cpar_from_connection(gc), gc);
3603 g_free(global_moods);
3606 static void
3607 set_mood_show(void)
3609 set_mood_cb(NULL, NULL);
3612 /***************************************************
3613 * Crap *
3614 ***************************************************/
3615 static void
3616 _pidgin_about_cb(GtkAction *action, GtkWidget *window) {
3617 GtkWidget *about = pidgin_about_dialog_new();
3619 gtk_window_set_transient_for(GTK_WINDOW(about), GTK_WINDOW(window));
3621 gtk_widget_show_all(about);
3624 /* TODO: fill out tooltips... */
3625 static const GtkActionEntry blist_menu_entries[] = {
3626 /* NOTE: Do not set any accelerator to Control+O. It is mapped by
3627 gtk_blist_key_press_cb to "Get User Info" on the selected buddy. */
3628 /* Buddies menu */
3629 { "BuddiesMenu", NULL, N_("_Buddies"), NULL, NULL, NULL },
3630 { "NewInstantMessage", PIDGIN_STOCK_TOOLBAR_MESSAGE_NEW, N_("New Instant _Message..."), "<control>M", NULL, pidgin_dialogs_im },
3631 { "JoinAChat", PIDGIN_STOCK_CHAT, N_("Join a _Chat..."), "<control>C", NULL, pidgin_blist_joinchat_show },
3632 { "GetUserInfo", PIDGIN_STOCK_TOOLBAR_USER_INFO, N_("Get User _Info..."), "<control>I", NULL, pidgin_dialogs_info },
3633 { "ViewUserLog", NULL, N_("View User _Log..."), "<control>L", NULL, pidgin_dialogs_log },
3634 { "ShowMenu", NULL, N_("Sh_ow"), NULL, NULL, NULL },
3635 { "SortMenu", NULL, N_("_Sort Buddies"), NULL, NULL, NULL },
3636 { "AddBuddy", GTK_STOCK_ADD, N_("_Add Buddy..."), "<control>B", NULL, pidgin_blist_add_buddy_cb },
3637 { "AddChat", GTK_STOCK_ADD, N_("Add C_hat..."), NULL, NULL, pidgin_blist_add_chat_cb },
3638 { "AddGroup", GTK_STOCK_ADD, N_("Add _Group..."), NULL, NULL, purple_blist_request_add_group },
3639 { "Quit", GTK_STOCK_QUIT, N_("_Quit"), "<control>Q", NULL, purple_core_quit },
3641 /* Accounts menu */
3642 { "AccountsMenu", NULL, N_("_Accounts"), NULL, NULL, NULL },
3643 { "ManageAccounts", NULL, N_("Manage Accounts"), "<control>A", NULL, pidgin_accounts_window_show },
3645 /* Tools */
3646 { "ToolsMenu", NULL, N_("_Tools"), NULL, NULL, NULL },
3647 { "BuddyPounces", NULL, N_("Buddy _Pounces"), NULL, NULL, pidgin_pounces_manager_show },
3648 { "CustomSmileys", PIDGIN_STOCK_TOOLBAR_SMILEY, N_("Custom Smile_ys"), "<control>Y", NULL, pidgin_smiley_manager_show },
3649 { "Plugins", PIDGIN_STOCK_TOOLBAR_PLUGINS, N_("Plu_gins"), "<control>U", NULL, pidgin_plugin_dialog_show },
3650 { "Preferences", GTK_STOCK_PREFERENCES, N_("Pr_eferences"), "<control>P", NULL, pidgin_prefs_show },
3651 { "Privacy", NULL, N_("Pr_ivacy"), NULL, NULL, pidgin_privacy_dialog_show },
3652 { "SetMood", NULL, N_("Set _Mood"), "<control>D", NULL, set_mood_show },
3653 { "FileTransfers", PIDGIN_STOCK_TOOLBAR_TRANSFER, N_("_File Transfers"), "<control>T", NULL, G_CALLBACK(gtk_blist_show_xfer_dialog_cb) },
3654 { "RoomList", NULL, N_("R_oom List"), NULL, NULL, pidgin_roomlist_dialog_show },
3655 { "SystemLog", NULL, N_("System _Log"), NULL, NULL, gtk_blist_show_systemlog_cb },
3657 /* Help */
3658 { "HelpMenu", NULL, N_("_Help"), NULL, NULL, NULL },
3659 { "OnlineHelp", GTK_STOCK_HELP, N_("Online _Help"), "F1", NULL, gtk_blist_show_onlinehelp_cb },
3660 { "DebugWindow", NULL, N_("_Debug Window"), NULL, NULL, toggle_debug },
3661 { "PluginInformation", NULL, N_("_Plugin Information"), NULL, NULL, pidgin_debug_plugin_info_show },
3662 { "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, G_CALLBACK(_pidgin_about_cb) },
3665 /* Toggle items */
3666 static const GtkToggleActionEntry blist_menu_toggle_entries[] = {
3667 /* Buddies->Show menu */
3668 { "ShowOffline", NULL, N_("_Offline Buddies"), NULL, NULL, G_CALLBACK(pidgin_blist_edit_mode_cb), FALSE },
3669 { "ShowEmptyGroups", NULL, N_("_Empty Groups"), NULL, NULL, G_CALLBACK(pidgin_blist_show_empty_groups_cb), FALSE },
3670 { "ShowBuddyDetails", NULL, N_("Buddy _Details"), NULL, NULL, G_CALLBACK(pidgin_blist_buddy_details_cb), FALSE },
3671 { "ShowIdleTimes", NULL, N_("Idle _Times"), NULL, NULL, G_CALLBACK(pidgin_blist_show_idle_time_cb), FALSE },
3672 { "ShowProtocolIcons", NULL, N_("_Protocol Icons"), NULL, NULL, G_CALLBACK(pidgin_blist_show_protocol_icons_cb), FALSE },
3674 /* Tools menu */
3675 { "MuteSounds", NULL, N_("Mute _Sounds"), NULL, NULL, G_CALLBACK(pidgin_blist_mute_sounds_cb), FALSE },
3678 static const char *blist_menu =
3679 "<ui>"
3680 "<menubar name='BList'>"
3681 "<menu action='BuddiesMenu'>"
3682 "<menuitem action='NewInstantMessage'/>"
3683 "<menuitem action='JoinAChat'/>"
3684 "<menuitem action='GetUserInfo'/>"
3685 "<menuitem action='ViewUserLog'/>"
3686 "<separator/>"
3687 "<menu action='ShowMenu'>"
3688 "<menuitem action='ShowOffline'/>"
3689 "<menuitem action='ShowEmptyGroups'/>"
3690 "<menuitem action='ShowBuddyDetails'/>"
3691 "<menuitem action='ShowIdleTimes'/>"
3692 "<menuitem action='ShowProtocolIcons'/>"
3693 "</menu>"
3694 "<menu action='SortMenu'/>"
3695 "<separator/>"
3696 "<menuitem action='AddBuddy'/>"
3697 "<menuitem action='AddChat'/>"
3698 "<menuitem action='AddGroup'/>"
3699 "<separator/>"
3700 "<menuitem action='Quit'/>"
3701 "</menu>"
3702 "<menu action='AccountsMenu'>"
3703 "<menuitem action='ManageAccounts'/>"
3704 "</menu>"
3705 "<menu action='ToolsMenu'>"
3706 "<menuitem action='BuddyPounces'/>"
3707 "<menuitem action='CustomSmileys'/>"
3708 "<menuitem action='Plugins'/>"
3709 "<menuitem action='Preferences'/>"
3710 "<menuitem action='Privacy'/>"
3711 "<menuitem action='SetMood'/>"
3712 "<separator/>"
3713 "<menuitem action='FileTransfers'/>"
3714 "<menuitem action='RoomList'/>"
3715 "<menuitem action='SystemLog'/>"
3716 "<separator/>"
3717 "<menuitem action='MuteSounds'/>"
3718 "<placeholder name='PluginActions'/>"
3719 "</menu>"
3720 "<menu action='HelpMenu'>"
3721 "<menuitem action='OnlineHelp'/>"
3722 "<separator/>"
3723 "<menuitem action='DebugWindow'/>"
3724 "<menuitem action='PluginInformation'/>"
3725 "<separator/>"
3726 "<menuitem action='About'/>"
3727 "</menu>"
3728 "</menubar>"
3729 "</ui>";
3731 /*********************************************************
3732 * Private Utility functions *
3733 *********************************************************/
3735 static char *pidgin_get_tooltip_text(PurpleBlistNode *node, gboolean full)
3737 GString *str = g_string_new("");
3738 PurpleProtocol *protocol = NULL;
3739 char *tmp;
3741 if (PURPLE_IS_CHAT(node))
3743 PurpleChat *chat;
3744 GList *connections;
3745 GList *cur = NULL;
3746 PurpleProtocolChatEntry *pce;
3747 char *name, *value;
3748 PurpleChatConversation *conv;
3749 PidginBlistNode *bnode = purple_blist_node_get_ui_data(node);
3751 chat = (PurpleChat *)node;
3752 protocol = purple_protocols_find(purple_account_get_protocol_id(purple_chat_get_account(chat)));
3754 connections = purple_connections_get_all();
3755 if (connections && connections->next)
3757 tmp = g_markup_escape_text(purple_account_get_username(purple_chat_get_account(chat)), -1);
3758 g_string_append_printf(str, _("<b>Account:</b> %s"), tmp);
3759 g_free(tmp);
3762 if (bnode && bnode->conv.conv) {
3763 conv = PURPLE_CHAT_CONVERSATION(bnode->conv.conv);
3764 } else {
3765 char *chat_name;
3766 if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, CHAT, get_name))
3767 chat_name = purple_protocol_chat_iface_get_name(protocol, purple_chat_get_components(chat));
3768 else
3769 chat_name = g_strdup(purple_chat_get_name(chat));
3771 conv = purple_conversations_find_chat_with_account(chat_name,
3772 purple_chat_get_account(chat));
3773 g_free(chat_name);
3776 if (conv && !purple_chat_conversation_has_left(conv)) {
3777 g_string_append_printf(str, _("\n<b>Occupants:</b> %d"),
3778 purple_chat_conversation_get_users_count(conv));
3780 if (protocol && (purple_protocol_get_options(protocol) & OPT_PROTO_CHAT_TOPIC)) {
3781 const char *chattopic = purple_chat_conversation_get_topic(conv);
3782 char *topic = chattopic ? g_markup_escape_text(chattopic, -1) : NULL;
3783 g_string_append_printf(str, _("\n<b>Topic:</b> %s"), topic ? topic : _("(no topic set)"));
3784 g_free(topic);
3788 if (protocol)
3789 cur = purple_protocol_chat_iface_info(protocol, purple_account_get_connection(purple_chat_get_account(chat)));
3791 while (cur != NULL)
3793 pce = cur->data;
3795 if (!pce->secret)
3797 tmp = purple_text_strip_mnemonic(pce->label);
3798 name = g_markup_escape_text(tmp, -1);
3799 g_free(tmp);
3800 value = g_markup_escape_text(g_hash_table_lookup(
3801 purple_chat_get_components(chat), pce->identifier), -1);
3802 g_string_append_printf(str, "\n<b>%s</b> %s",
3803 name ? name : "",
3804 value ? value : "");
3805 g_free(name);
3806 g_free(value);
3809 g_free(pce);
3810 cur = g_list_delete_link(cur, cur);
3813 else if (PURPLE_IS_CONTACT(node) || PURPLE_IS_BUDDY(node))
3815 /* NOTE: THIS FUNCTION IS NO LONGER CALLED FOR CONTACTS.
3816 * It is only called by create_tip_for_node(), and create_tip_for_node() is never called for a contact.
3818 PurpleContact *c;
3819 PurpleBuddy *b;
3820 PurplePresence *presence;
3821 PurpleNotifyUserInfo *user_info;
3822 GList *connections;
3823 char *tmp;
3824 gchar *alias;
3825 time_t idle_secs, signon;
3827 if (PURPLE_IS_CONTACT(node))
3829 c = (PurpleContact *)node;
3830 b = purple_contact_get_priority_buddy(c);
3832 else
3834 b = (PurpleBuddy *)node;
3835 c = purple_buddy_get_contact(b);
3838 protocol = purple_protocols_find(purple_account_get_protocol_id(purple_buddy_get_account(b)));
3840 presence = purple_buddy_get_presence(b);
3841 user_info = purple_notify_user_info_new();
3843 /* Account */
3844 connections = purple_connections_get_all();
3845 if (full && connections && connections->next)
3847 purple_notify_user_info_add_pair_plaintext(user_info, _("Account"),
3848 purple_account_get_username(purple_buddy_get_account(b)));
3851 /* Alias */
3852 /* If there's not a contact alias, the node is being displayed with
3853 * this alias, so there's no point in showing it in the tooltip. */
3854 g_object_get(c, "alias", &alias, NULL);
3855 if (full && c && purple_buddy_get_local_alias(b) != NULL && purple_buddy_get_local_alias(b)[0] != '\0' &&
3856 (alias != NULL && alias[0] != '\0') &&
3857 !purple_strequal(alias, purple_buddy_get_local_alias(b)))
3859 purple_notify_user_info_add_pair_plaintext(user_info,
3860 _("Buddy Alias"), purple_buddy_get_local_alias(b));
3863 /* Nickname/Server Alias */
3864 /* I'd like to only show this if there's a contact or buddy
3865 * alias, but people often set long nicknames, which
3866 * get ellipsized, so the only way to see the whole thing is
3867 * to look at the tooltip. */
3868 if (full && purple_buddy_get_server_alias(b))
3870 purple_notify_user_info_add_pair_plaintext(user_info,
3871 _("Nickname"), purple_buddy_get_server_alias(b));
3874 /* Logged In */
3875 signon = purple_presence_get_login_time(presence);
3876 if (full && PURPLE_BUDDY_IS_ONLINE(b) && signon > 0)
3878 if (signon > time(NULL)) {
3880 * They signed on in the future?! Our local clock
3881 * must be wrong, show the actual date instead of
3882 * "4 days", etc.
3884 tmp = g_strdup(purple_date_format_long(localtime(&signon)));
3885 } else
3886 tmp = purple_str_seconds_to_string(time(NULL) - signon);
3887 purple_notify_user_info_add_pair_plaintext(user_info, _("Logged In"), tmp);
3888 g_free(tmp);
3891 /* Idle */
3892 if (purple_presence_is_idle(presence))
3894 idle_secs = purple_presence_get_idle_time(presence);
3895 if (idle_secs > 0)
3897 tmp = purple_str_seconds_to_string(time(NULL) - idle_secs);
3898 purple_notify_user_info_add_pair_plaintext(user_info, _("Idle"), tmp);
3899 g_free(tmp);
3903 /* Last Seen */
3904 if (full && c && !PURPLE_BUDDY_IS_ONLINE(b))
3906 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(c));
3907 PurpleBlistNode *bnode;
3908 int lastseen = 0;
3910 if (gtknode && (!gtknode->contact_expanded || PURPLE_IS_CONTACT(node)))
3912 /* We're either looking at a buddy for a collapsed contact or
3913 * an expanded contact itself so we show the most recent
3914 * (largest) last_seen time for any of the buddies under
3915 * the contact. */
3916 for (bnode = ((PurpleBlistNode *)c)->child ; bnode != NULL ; bnode = bnode->next)
3918 int value = purple_blist_node_get_int(bnode, "last_seen");
3919 if (value > lastseen)
3920 lastseen = value;
3923 else
3925 /* We're dealing with a buddy under an expanded contact,
3926 * so we show the last_seen time for the buddy. */
3927 lastseen = purple_blist_node_get_int(&b->node, "last_seen");
3930 if (lastseen > 0)
3932 tmp = purple_str_seconds_to_string(time(NULL) - lastseen);
3933 purple_notify_user_info_add_pair_plaintext(user_info, _("Last Seen"), tmp);
3934 g_free(tmp);
3939 /* Offline? */
3940 /* FIXME: Why is this status special-cased by the core? --rlaager
3941 * FIXME: Alternatively, why not have the core do all of them? --rlaager */
3942 if (!PURPLE_BUDDY_IS_ONLINE(b)) {
3943 purple_notify_user_info_add_pair_plaintext(user_info, _("Status"), _("Offline"));
3946 if (purple_account_is_connected(purple_buddy_get_account(b)) &&
3947 protocol)
3949 /* Additional text from the protocol */
3950 purple_protocol_client_iface_tooltip_text(protocol, b, user_info, full);
3953 /* These are Easter Eggs. Patches to remove them will be rejected. */
3954 if (!g_ascii_strcasecmp(purple_buddy_get_name(b), "robflynn"))
3955 purple_notify_user_info_add_pair_plaintext(user_info, _("Description"), _("Spooky"));
3956 if (!g_ascii_strcasecmp(purple_buddy_get_name(b), "seanegn"))
3957 purple_notify_user_info_add_pair_plaintext(user_info, _("Status"), _("Awesome"));
3958 if (!g_ascii_strcasecmp(purple_buddy_get_name(b), "chipx86"))
3959 purple_notify_user_info_add_pair_plaintext(user_info, _("Status"), _("Rockin'"));
3961 tmp = purple_notify_user_info_get_text_with_newline(user_info, "\n");
3962 g_string_append(str, tmp);
3963 g_free(tmp);
3964 g_free(alias);
3966 purple_notify_user_info_destroy(user_info);
3967 } else if (PURPLE_IS_GROUP(node)) {
3968 gint count;
3969 PurpleGroup *group = (PurpleGroup*)node;
3970 PurpleNotifyUserInfo *user_info;
3972 user_info = purple_notify_user_info_new();
3974 count = purple_counting_node_get_online_count(PURPLE_COUNTING_NODE(group));
3975 if (count != 0) {
3976 /* Online buddies in group */
3977 char tmp2[12];
3978 sprintf(tmp2, "%d", count);
3979 purple_notify_user_info_add_pair_plaintext(user_info,
3980 _("Online Buddies"), tmp2);
3983 count = purple_counting_node_get_current_size(PURPLE_COUNTING_NODE(group));
3984 if (count != 0) {
3985 /* Total buddies (from online accounts) in group */
3986 char tmp2[12];
3987 sprintf(tmp2, "%d", count);
3988 purple_notify_user_info_add_pair_html(user_info,
3989 _("Total Buddies"), tmp2);
3992 tmp = purple_notify_user_info_get_text_with_newline(user_info, "\n");
3993 g_string_append(str, tmp);
3994 g_free(tmp);
3996 purple_notify_user_info_destroy(user_info);
3999 purple_signal_emit(pidgin_blist_get_handle(), "drawing-tooltip",
4000 node, str, full);
4002 return g_string_free(str, FALSE);
4005 static GHashTable *cached_emblems;
4007 static void _cleanup_cached_emblem(gpointer data, GObject *obj) {
4008 g_hash_table_remove(cached_emblems, data);
4011 static GdkPixbuf * _pidgin_blist_get_cached_emblem(gchar *path) {
4012 GdkPixbuf *pb = g_hash_table_lookup(cached_emblems, path);
4014 if (pb != NULL) {
4015 /* The caller gets a reference */
4016 g_object_ref(pb);
4017 g_free(path);
4018 } else {
4019 pb = pidgin_pixbuf_new_from_file(path);
4020 if (pb != NULL) {
4021 /* We don't want to own a ref to the pixbuf, but we need to keep clean up. */
4022 /* I'm not sure if it would be better to just keep our ref and not let the emblem ever be destroyed */
4023 g_object_weak_ref(G_OBJECT(pb), _cleanup_cached_emblem, path);
4024 g_hash_table_insert(cached_emblems, path, pb);
4025 } else
4026 g_free(path);
4029 return pb;
4032 GdkPixbuf *
4033 pidgin_blist_get_emblem(PurpleBlistNode *node)
4035 PurpleBuddy *buddy = NULL;
4036 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(node);
4037 PurpleProtocol *protocol;
4038 const char *name = NULL;
4039 char *filename, *path;
4040 PurplePresence *p = NULL;
4041 PurpleStatus *tune;
4043 if(PURPLE_IS_CONTACT(node)) {
4044 if(!gtknode->contact_expanded) {
4045 buddy = purple_contact_get_priority_buddy((PurpleContact*)node);
4047 } else if(PURPLE_IS_BUDDY(node)) {
4048 buddy = (PurpleBuddy*)node;
4049 p = purple_buddy_get_presence(buddy);
4050 if (purple_presence_is_status_primitive_active(p, PURPLE_STATUS_MOBILE)) {
4051 /* This emblem comes from the small emoticon set now,
4052 * to reduce duplication. */
4053 path = g_build_filename(PURPLE_DATADIR, "pixmaps",
4054 "pidgin", "emotes", "small", "mobile.png", NULL);
4055 return _pidgin_blist_get_cached_emblem(path);
4058 if (((PidginBlistNode*)purple_blist_node_get_ui_data(node->parent))->contact_expanded) {
4059 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_protocol_icons"))
4060 return NULL;
4061 return pidgin_create_protocol_icon(purple_buddy_get_account((PurpleBuddy*)node), PIDGIN_PROTOCOL_ICON_SMALL);
4063 } else {
4064 return NULL;
4067 g_return_val_if_fail(buddy != NULL, NULL);
4069 if (!purple_account_privacy_check(purple_buddy_get_account(buddy), purple_buddy_get_name(buddy))) {
4070 path = g_build_filename(PURPLE_DATADIR, "pidgin", "icons",
4071 "hicolor", "16x16", "emblems", "emblem-blocked.png",
4072 NULL);
4073 return _pidgin_blist_get_cached_emblem(path);
4076 /* If we came through the contact code flow above, we didn't need
4077 * to get the presence until now. */
4078 if (p == NULL)
4079 p = purple_buddy_get_presence(buddy);
4081 if (purple_presence_is_status_primitive_active(p, PURPLE_STATUS_MOBILE)) {
4082 /* This emblem comes from the small emoticon set now, to reduce duplication. */
4083 path = g_build_filename(PURPLE_DATADIR, "pixmaps", "pidgin",
4084 "emotes", "small", "mobile.png", NULL);
4085 return _pidgin_blist_get_cached_emblem(path);
4088 tune = purple_presence_get_status(p, "tune");
4089 if (tune && purple_status_is_active(tune)) {
4090 /* TODO: Replace "Tune" with generalized "Media" in 3.0. */
4091 if (purple_status_get_attr_string(tune, "game") != NULL) {
4092 path = g_build_filename(PURPLE_DATADIR, "pidgin",
4093 "icons", "hicolor", "16x16", "emblems",
4094 "emblem-game.png", NULL);
4095 return _pidgin_blist_get_cached_emblem(path);
4097 /* TODO: Replace "Tune" with generalized "Media" in 3.0. */
4098 if (purple_status_get_attr_string(tune, "office") != NULL) {
4099 path = g_build_filename(PURPLE_DATADIR, "pidgin",
4100 "icons", "hicolor", "16x16", "emblems",
4101 "emblem-office.png", NULL);
4102 return _pidgin_blist_get_cached_emblem(path);
4104 /* Regular old "tune" is the only one in all protocols. */
4105 /* This emblem comes from the small emoticon set now, to reduce duplication. */
4106 path = g_build_filename(PURPLE_DATADIR, "pixmaps", "pidgin",
4107 "emotes", "small", "music.png", NULL);
4108 return _pidgin_blist_get_cached_emblem(path);
4111 protocol = purple_protocols_find(purple_account_get_protocol_id(purple_buddy_get_account(buddy)));
4112 if (!protocol)
4113 return NULL;
4115 name = purple_protocol_client_iface_list_emblem(protocol, buddy);
4117 if (name == NULL) {
4118 PurpleStatus *status;
4120 if (!purple_presence_is_status_primitive_active(p, PURPLE_STATUS_MOOD))
4121 return NULL;
4123 status = purple_presence_get_status(p, "mood");
4124 name = purple_status_get_attr_string(status, PURPLE_MOOD_NAME);
4126 if (!(name && *name))
4127 return NULL;
4129 path = get_mood_icon_path(name);
4130 } else {
4131 filename = g_strdup_printf("emblem-%s.png", name);
4132 path = g_build_filename(PURPLE_DATADIR, "pidgin", "icons",
4133 "hicolor", "16x16", "emblems", filename, NULL);
4134 g_free(filename);
4137 /* _pidgin_blist_get_cached_emblem() assumes ownership of path */
4138 return _pidgin_blist_get_cached_emblem(path);
4142 GdkPixbuf *
4143 pidgin_blist_get_status_icon(PurpleBlistNode *node, PidginStatusIconSize size)
4145 GdkPixbuf *ret;
4146 const char *icon = NULL;
4147 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(node);
4148 PidginBlistNode *gtkbuddynode = NULL;
4149 PurpleBuddy *buddy = NULL;
4150 PurpleChat *chat = NULL;
4151 GtkIconSize icon_size = gtk_icon_size_from_name((size == PIDGIN_STATUS_ICON_LARGE) ? PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL :
4152 PIDGIN_ICON_SIZE_TANGO_MICROSCOPIC);
4154 if(PURPLE_IS_CONTACT(node)) {
4155 if(!gtknode->contact_expanded) {
4156 buddy = purple_contact_get_priority_buddy((PurpleContact*)node);
4157 if (buddy != NULL)
4158 gtkbuddynode = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(buddy));
4160 } else if(PURPLE_IS_BUDDY(node)) {
4161 buddy = (PurpleBuddy*)node;
4162 gtkbuddynode = purple_blist_node_get_ui_data(node);
4163 } else if(PURPLE_IS_CHAT(node)) {
4164 chat = (PurpleChat*)node;
4165 } else {
4166 return NULL;
4169 if(buddy || chat) {
4170 PurpleAccount *account;
4171 PurpleProtocol *protocol;
4173 if(buddy)
4174 account = purple_buddy_get_account(buddy);
4175 else
4176 account = purple_chat_get_account(chat);
4178 protocol = purple_protocols_find(purple_account_get_protocol_id(account));
4179 if(!protocol)
4180 return NULL;
4183 if(buddy) {
4184 PurpleConversation *conv = find_conversation_with_buddy(buddy);
4185 PurplePresence *p;
4186 gboolean trans;
4188 if(conv != NULL) {
4189 PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
4190 if (gtkconv == NULL && size == PIDGIN_STATUS_ICON_SMALL) {
4191 PidginBlistNode *ui = purple_blist_node_get_ui_data(&(buddy->node));
4192 if (ui == NULL || (ui->conv.flags & PIDGIN_BLIST_NODE_HAS_PENDING_MESSAGE))
4193 return gtk_widget_render_icon (GTK_WIDGET(gtkblist->treeview),
4194 PIDGIN_STOCK_STATUS_MESSAGE, icon_size, "GtkTreeView");
4198 p = purple_buddy_get_presence(buddy);
4199 trans = purple_presence_is_idle(p);
4201 if (PURPLE_BUDDY_IS_ONLINE(buddy) && gtkbuddynode && gtkbuddynode->recent_signonoff)
4202 icon = PIDGIN_STOCK_STATUS_LOGIN;
4203 else if (gtkbuddynode && gtkbuddynode->recent_signonoff)
4204 icon = PIDGIN_STOCK_STATUS_LOGOUT;
4205 else if (purple_presence_is_status_primitive_active(p, PURPLE_STATUS_UNAVAILABLE))
4206 if (trans)
4207 icon = PIDGIN_STOCK_STATUS_BUSY_I;
4208 else
4209 icon = PIDGIN_STOCK_STATUS_BUSY;
4210 else if (purple_presence_is_status_primitive_active(p, PURPLE_STATUS_AWAY))
4211 if (trans)
4212 icon = PIDGIN_STOCK_STATUS_AWAY_I;
4213 else
4214 icon = PIDGIN_STOCK_STATUS_AWAY;
4215 else if (purple_presence_is_status_primitive_active(p, PURPLE_STATUS_EXTENDED_AWAY))
4216 if (trans)
4217 icon = PIDGIN_STOCK_STATUS_XA_I;
4218 else
4219 icon = PIDGIN_STOCK_STATUS_XA;
4220 else if (purple_presence_is_status_primitive_active(p, PURPLE_STATUS_OFFLINE))
4221 icon = PIDGIN_STOCK_STATUS_OFFLINE;
4222 else if (trans)
4223 icon = PIDGIN_STOCK_STATUS_AVAILABLE_I;
4224 else if (purple_presence_is_status_primitive_active(p, PURPLE_STATUS_INVISIBLE))
4225 icon = PIDGIN_STOCK_STATUS_INVISIBLE;
4226 else
4227 icon = PIDGIN_STOCK_STATUS_AVAILABLE;
4228 } else if (chat) {
4229 icon = PIDGIN_STOCK_STATUS_CHAT;
4230 } else {
4231 icon = PIDGIN_STOCK_STATUS_PERSON;
4234 ret = gtk_widget_render_icon (GTK_WIDGET(gtkblist->treeview), icon,
4235 icon_size, "GtkTreeView");
4236 return ret;
4239 static const char *
4240 theme_font_get_color_default(PidginThemeFont *font, const char *def)
4242 const char *ret;
4243 if (!font || !(ret = pidgin_theme_font_get_color_describe(font)))
4244 ret = def;
4245 return ret;
4248 static const char *
4249 theme_font_get_face_default(PidginThemeFont *font, const char *def)
4251 const char *ret;
4252 if (!font || !(ret = pidgin_theme_font_get_font_face(font)))
4253 ret = def;
4254 return ret;
4257 gchar *
4258 pidgin_blist_get_name_markup(PurpleBuddy *b, gboolean selected, gboolean aliased)
4260 const char *name, *name_color, *name_font, *status_color, *status_font, *dim_grey;
4261 char *text = NULL;
4262 PurpleProtocol *protocol = NULL;
4263 PurpleContact *contact;
4264 PurplePresence *presence;
4265 PidginBlistNode *gtkcontactnode = NULL;
4266 char *idletime = NULL, *statustext = NULL, *nametext = NULL;
4267 PurpleConversation *conv = find_conversation_with_buddy(b);
4268 gboolean hidden_conv = FALSE;
4269 gboolean biglist = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons");
4270 PidginThemeFont *statusfont = NULL, *namefont = NULL;
4271 PidginBlistTheme *theme;
4272 gchar *contact_alias;
4274 if (conv != NULL) {
4275 PidginBlistNode *ui = purple_blist_node_get_ui_data(&(b->node));
4276 if (ui) {
4277 if (ui->conv.flags & PIDGIN_BLIST_NODE_HAS_PENDING_MESSAGE)
4278 hidden_conv = TRUE;
4279 } else {
4280 if (PIDGIN_CONVERSATION(conv) == NULL)
4281 hidden_conv = TRUE;
4285 /* XXX Good luck cleaning up this crap */
4286 contact = PURPLE_CONTACT(PURPLE_BLIST_NODE(b)->parent);
4287 if(contact)
4288 gtkcontactnode = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(contact));
4290 g_object_get(contact, "alias", &contact_alias, NULL);
4292 /* Name */
4293 if (gtkcontactnode && !gtkcontactnode->contact_expanded && contact_alias)
4294 name = contact_alias;
4295 else
4296 name = purple_buddy_get_alias(b);
4298 /* Raise a contact pre-draw signal here. THe callback will return an
4299 * escaped version of the name. */
4300 nametext = purple_signal_emit_return_1(pidgin_blist_get_handle(), "drawing-buddy", b);
4302 if(!nametext)
4303 nametext = g_markup_escape_text(name, strlen(name));
4305 presence = purple_buddy_get_presence(b);
4307 /* Name is all that is needed */
4308 if (!aliased || biglist) {
4310 /* Status Info */
4311 protocol = purple_protocols_find(purple_account_get_protocol_id(purple_buddy_get_account(b)));
4313 if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, status_text) &&
4314 purple_account_get_connection(purple_buddy_get_account(b))) {
4315 char *tmp = purple_protocol_client_iface_status_text(protocol, b);
4316 const char *end;
4318 if(tmp && !g_utf8_validate(tmp, -1, &end)) {
4319 char *new = g_strndup(tmp,
4320 g_utf8_pointer_to_offset(tmp, end));
4321 g_free(tmp);
4322 tmp = new;
4324 if(tmp) {
4325 g_strdelimit(tmp, "\n", ' ');
4326 purple_str_strip_char(tmp, '\r');
4328 statustext = tmp;
4331 if(!purple_presence_is_online(presence) && !statustext)
4332 statustext = g_strdup(_("Offline"));
4334 /* Idle Text */
4335 if (purple_presence_is_idle(presence) && purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_idle_time")) {
4336 time_t idle_secs = purple_presence_get_idle_time(presence);
4338 if (idle_secs > 0) {
4339 int iday, ihrs, imin;
4340 time_t t;
4342 time(&t);
4343 iday = (t - idle_secs) / (24 * 60 * 60);
4344 ihrs = ((t - idle_secs) / 60 / 60) % 24;
4345 imin = ((t - idle_secs) / 60) % 60;
4347 if (iday)
4348 idletime = g_strdup_printf(_("Idle %dd %dh %02dm"), iday, ihrs, imin);
4349 else if (ihrs)
4350 idletime = g_strdup_printf(_("Idle %dh %02dm"), ihrs, imin);
4351 else
4352 idletime = g_strdup_printf(_("Idle %dm"), imin);
4354 } else
4355 idletime = g_strdup(_("Idle"));
4359 /* choose the colors of the text */
4360 theme = pidgin_blist_get_theme();
4361 name_color = NULL;
4363 dim_grey = pidgin_style_is_dark(NULL) ? "light slate grey" : "dim grey";
4365 if (theme) {
4366 if (purple_presence_is_idle(presence)) {
4367 namefont = statusfont = pidgin_blist_theme_get_idle_text_info(theme);
4368 name_color = dim_grey;
4369 } else if (!purple_presence_is_online(presence)) {
4370 namefont = pidgin_blist_theme_get_offline_text_info(theme);
4371 name_color = dim_grey;
4372 statusfont = pidgin_blist_theme_get_status_text_info(theme);
4373 } else if (purple_presence_is_available(presence)) {
4374 namefont = pidgin_blist_theme_get_online_text_info(theme);
4375 statusfont = pidgin_blist_theme_get_status_text_info(theme);
4376 } else {
4377 namefont = pidgin_blist_theme_get_away_text_info(theme);
4378 statusfont = pidgin_blist_theme_get_status_text_info(theme);
4380 } else {
4381 if (!selected
4382 && (purple_presence_is_idle(presence)
4383 || !purple_presence_is_online(presence)))
4385 name_color = dim_grey;
4389 name_color = theme_font_get_color_default(namefont, name_color);
4390 name_font = theme_font_get_face_default(namefont, "");
4392 status_color = theme_font_get_color_default(statusfont, dim_grey);
4393 status_font = theme_font_get_face_default(statusfont, "");
4395 if (aliased && selected) {
4396 if (theme) {
4397 name_color = "black";
4398 status_color = "black";
4399 } else {
4400 name_color = NULL;
4401 status_color = NULL;
4405 if (hidden_conv) {
4406 char *tmp = nametext;
4407 nametext = g_strdup_printf("<b>%s</b>", tmp);
4408 g_free(tmp);
4411 /* Put it all together */
4412 if ((!aliased || biglist) && (statustext || idletime)) {
4413 /* using <span size='smaller'> breaks the status, so it must be seperated into <small><span>*/
4414 if (name_color) {
4415 text = g_strdup_printf("<span font_desc='%s' foreground='%s'>%s</span>\n"
4416 "<small><span font_desc='%s' foreground='%s'>%s%s%s</span></small>",
4417 name_font, name_color, nametext, status_font, status_color,
4418 idletime != NULL ? idletime : "",
4419 (idletime != NULL && statustext != NULL) ? " - " : "",
4420 statustext != NULL ? statustext : "");
4421 } else if (status_color) {
4422 text = g_strdup_printf("<span font_desc='%s'>%s</span>\n"
4423 "<small><span font_desc='%s' foreground='%s'>%s%s%s</span></small>",
4424 name_font, nametext, status_font, status_color,
4425 idletime != NULL ? idletime : "",
4426 (idletime != NULL && statustext != NULL) ? " - " : "",
4427 statustext != NULL ? statustext : "");
4428 } else {
4429 text = g_strdup_printf("<span font_desc='%s'>%s</span>\n"
4430 "<small><span font_desc='%s'>%s%s%s</span></small>",
4431 name_font, nametext, status_font,
4432 idletime != NULL ? idletime : "",
4433 (idletime != NULL && statustext != NULL) ? " - " : "",
4434 statustext != NULL ? statustext : "");
4436 } else {
4437 if (name_color) {
4438 text = g_strdup_printf("<span font_desc='%s' color='%s'>%s</span>",
4439 name_font, name_color, nametext);
4440 } else {
4441 text = g_strdup_printf("<span font_desc='%s'>%s</span>", name_font,
4442 nametext);
4445 g_free(nametext);
4446 g_free(statustext);
4447 g_free(idletime);
4448 g_free(contact_alias);
4450 if (hidden_conv) {
4451 char *tmp = text;
4452 text = g_strdup_printf("<b>%s</b>", tmp);
4453 g_free(tmp);
4456 return text;
4459 static void pidgin_blist_restore_window_state(void)
4461 int blist_width, blist_height;
4463 blist_width = purple_prefs_get_int(PIDGIN_PREFS_ROOT "/blist/width");
4465 /* if the window exists, is hidden, we're saving sizes, and the
4466 * size is sane... */
4467 if (gtkblist && gtkblist->window &&
4468 !gtk_widget_get_visible(gtkblist->window) && blist_width != 0) {
4470 blist_height = purple_prefs_get_int(PIDGIN_PREFS_ROOT "/blist/height");
4472 gtk_window_set_default_size(GTK_WINDOW(gtkblist->window),
4473 blist_width, blist_height);
4474 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/list_maximized"))
4475 gtk_window_maximize(GTK_WINDOW(gtkblist->window));
4479 static gboolean pidgin_blist_refresh_timer(PurpleBuddyList *list)
4481 PurpleBlistNode *gnode, *cnode;
4483 if (gtk_blist_visibility == GDK_VISIBILITY_FULLY_OBSCURED
4484 || !gtk_widget_get_visible(gtkblist->window))
4485 return TRUE;
4487 for (gnode = purple_blist_get_root(list); gnode; gnode = gnode->next) {
4488 if(!PURPLE_IS_GROUP(gnode))
4489 continue;
4490 for(cnode = gnode->child; cnode; cnode = cnode->next) {
4491 if(PURPLE_IS_CONTACT(cnode)) {
4492 PurpleBuddy *buddy;
4494 buddy = purple_contact_get_priority_buddy((PurpleContact*)cnode);
4496 if (buddy &&
4497 purple_presence_is_idle(purple_buddy_get_presence(buddy)))
4498 pidgin_blist_update_contact(list, PURPLE_BLIST_NODE(buddy));
4503 /* keep on going */
4504 return TRUE;
4507 static void pidgin_blist_hide_node(PurpleBuddyList *list, PurpleBlistNode *node, gboolean update)
4509 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(node);
4510 GtkTreeIter iter;
4512 if (!gtknode || !gtknode->row || !gtkblist)
4513 return;
4515 if(gtkblist->selected_node == node)
4516 gtkblist->selected_node = NULL;
4517 if (get_iter_from_node(node, &iter)) {
4518 gtk_tree_store_remove(gtkblist->treemodel, &iter);
4519 if(update && (PURPLE_IS_CONTACT(node) ||
4520 PURPLE_IS_BUDDY(node) || PURPLE_IS_CHAT(node))) {
4521 pidgin_blist_update(list, node->parent);
4524 gtk_tree_row_reference_free(gtknode->row);
4525 gtknode->row = NULL;
4528 static const char *require_connection[] =
4530 "/BList/BuddiesMenu/NewInstantMessage",
4531 "/BList/BuddiesMenu/JoinAChat",
4532 "/BList/BuddiesMenu/GetUserInfo",
4533 "/BList/BuddiesMenu/AddBuddy",
4534 "/BList/BuddiesMenu/AddChat",
4535 "/BList/BuddiesMenu/AddGroup",
4536 "/BList/ToolsMenu/Privacy",
4539 static const int require_connection_size = sizeof(require_connection)
4540 / sizeof(*require_connection);
4543 * Rebuild dynamic menus and make menu items sensitive/insensitive
4544 * where appropriate.
4546 static void
4547 update_menu_bar(PidginBuddyList *gtkblist)
4549 GtkAction *action;
4550 gboolean sensitive;
4551 int i;
4553 g_return_if_fail(gtkblist != NULL);
4555 pidgin_blist_update_accounts_menu();
4557 sensitive = (purple_connections_get_all() != NULL);
4559 for (i = 0; i < require_connection_size; i++)
4561 action = gtk_ui_manager_get_action(gtkblist->ui, require_connection[i]);
4562 gtk_action_set_sensitive(action, sensitive);
4565 action = gtk_ui_manager_get_action(gtkblist->ui, "/BList/BuddiesMenu/JoinAChat");
4566 gtk_action_set_sensitive(action, pidgin_blist_joinchat_is_showable());
4568 action = gtk_ui_manager_get_action(gtkblist->ui, "/BList/BuddiesMenu/AddChat");
4569 gtk_action_set_sensitive(action, pidgin_blist_joinchat_is_showable());
4571 action = gtk_ui_manager_get_action(gtkblist->ui, "/BList/ToolsMenu/RoomList");
4572 gtk_action_set_sensitive(action, pidgin_roomlist_is_showable());
4575 static void
4576 sign_on_off_cb(PurpleConnection *gc, PurpleBuddyList *blist)
4578 PidginBuddyList *gtkblist = PIDGIN_BUDDY_LIST(blist);
4580 update_menu_bar(gtkblist);
4583 static void
4584 plugin_changed_cb(PurplePlugin *p, gpointer data)
4586 pidgin_blist_update_plugin_actions();
4589 static void
4590 unseen_conv_menu(GdkEvent *event)
4592 static GtkWidget *menu = NULL;
4593 GList *convs = NULL;
4594 GList *chats, *ims;
4596 if (menu) {
4597 gtk_widget_destroy(menu);
4598 menu = NULL;
4601 ims = pidgin_conversations_get_unseen_ims(PIDGIN_UNSEEN_TEXT, FALSE, 0);
4603 chats = pidgin_conversations_get_unseen_chats(PIDGIN_UNSEEN_NICK, FALSE, 0);
4605 if(ims && chats)
4606 convs = g_list_concat(ims, chats);
4607 else if(ims && !chats)
4608 convs = ims;
4609 else if(!ims && chats)
4610 convs = chats;
4612 if (!convs)
4613 /* no conversations added, don't show the menu */
4614 return;
4616 menu = gtk_menu_new();
4618 pidgin_conversations_fill_menu(menu, convs);
4619 g_list_free(convs);
4620 gtk_widget_show_all(menu);
4621 gtk_menu_popup_at_pointer(GTK_MENU(menu), event);
4624 static gboolean
4625 menutray_press_cb(GtkWidget *widget, GdkEventButton *event)
4627 GList *convs;
4629 if (event->button == GDK_BUTTON_PRIMARY) {
4630 convs = pidgin_conversations_get_unseen_ims(PIDGIN_UNSEEN_TEXT, FALSE, 1);
4631 if(!convs)
4632 convs = pidgin_conversations_get_unseen_chats(PIDGIN_UNSEEN_NICK, FALSE, 1);
4634 if (convs) {
4635 pidgin_conv_present_conversation((PurpleConversation*)convs->data);
4636 g_list_free(convs);
4639 } else if (gdk_event_triggers_context_menu((GdkEvent *)event)) {
4640 unseen_conv_menu((GdkEvent *)event);
4643 return TRUE;
4646 static void
4647 conversation_updated_cb(PurpleConversation *conv, PurpleConversationUpdateType type,
4648 PidginBuddyList *gtkblist)
4650 PurpleAccount *account = purple_conversation_get_account(conv);
4651 GList *convs = NULL;
4652 GList *ims, *chats;
4653 GList *l = NULL;
4655 if (type != PURPLE_CONVERSATION_UPDATE_UNSEEN)
4656 return;
4658 if(account != NULL && purple_conversation_get_name(conv) != NULL) {
4659 PurpleBuddy *buddy = purple_blist_find_buddy(account, purple_conversation_get_name(conv));
4660 if(buddy != NULL)
4661 pidgin_blist_update_buddy(NULL, PURPLE_BLIST_NODE(buddy), TRUE);
4664 if (gtkblist->menutrayicon) {
4665 gtk_widget_destroy(gtkblist->menutrayicon);
4666 gtkblist->menutrayicon = NULL;
4669 ims = pidgin_conversations_get_unseen_ims(PIDGIN_UNSEEN_TEXT, FALSE, 0);
4671 chats = pidgin_conversations_get_unseen_chats(PIDGIN_UNSEEN_NICK, FALSE, 0);
4673 if(ims && chats)
4674 convs = g_list_concat(ims, chats);
4675 else if(ims && !chats)
4676 convs = ims;
4677 else if(!ims && chats)
4678 convs = chats;
4680 if (convs) {
4681 GtkWidget *img = NULL;
4682 GString *tooltip_text = NULL;
4684 tooltip_text = g_string_new("");
4685 l = convs;
4686 while (l != NULL) {
4687 int count = 0;
4688 PidginConversation *gtkconv = PIDGIN_CONVERSATION((PurpleConversation *)l->data);
4690 if(gtkconv)
4691 count = gtkconv->unseen_count;
4692 else if(g_object_get_data(G_OBJECT(l->data), "unseen-count"))
4693 count = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(l->data), "unseen-count"));
4695 g_string_append_printf(tooltip_text,
4696 ngettext("%d unread message from %s\n", "%d unread messages from %s\n", count),
4697 count, purple_conversation_get_title(l->data));
4698 l = l->next;
4700 if(tooltip_text->len > 0) {
4701 /* get rid of the last newline */
4702 g_string_truncate(tooltip_text, tooltip_text->len -1);
4703 img = gtk_image_new_from_stock(PIDGIN_STOCK_TOOLBAR_PENDING,
4704 gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL));
4706 gtkblist->menutrayicon = gtk_event_box_new();
4707 gtk_container_add(GTK_CONTAINER(gtkblist->menutrayicon), img);
4708 gtk_widget_show(img);
4709 gtk_widget_show(gtkblist->menutrayicon);
4710 g_signal_connect(G_OBJECT(gtkblist->menutrayicon), "button-press-event", G_CALLBACK(menutray_press_cb), NULL);
4712 pidgin_menu_tray_append(PIDGIN_MENU_TRAY(gtkblist->menutray), gtkblist->menutrayicon, tooltip_text->str);
4714 g_string_free(tooltip_text, TRUE);
4715 g_list_free(convs);
4719 static void
4720 conversation_deleting_cb(PurpleConversation *conv, PidginBuddyList *gtkblist)
4722 conversation_updated_cb(conv, PURPLE_CONVERSATION_UPDATE_UNSEEN, gtkblist);
4725 static void
4726 conversation_deleted_update_ui_cb(PurpleConversation *conv, PidginBlistNode *ui)
4728 if (ui->conv.conv != conv)
4729 return;
4730 ui->conv.conv = NULL;
4731 ui->conv.flags = 0;
4734 static void
4735 written_msg_update_ui_cb(PurpleConversation *conv, PurpleMessage *msg, PurpleBlistNode *node)
4737 PidginBlistNode *ui = purple_blist_node_get_ui_data(node);
4739 if (ui->conv.conv != conv)
4740 return;
4742 if (!pidgin_conv_is_hidden(PIDGIN_CONVERSATION(conv)))
4743 return;
4745 if (!(purple_message_get_flags(msg) & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV)))
4746 return;
4748 ui->conv.flags |= PIDGIN_BLIST_NODE_HAS_PENDING_MESSAGE;
4749 if (PURPLE_IS_CHAT_CONVERSATION(conv) && (purple_message_get_flags(msg) & PURPLE_MESSAGE_NICK))
4750 ui->conv.flags |= PIDGIN_BLIST_CHAT_HAS_PENDING_MESSAGE_WITH_NICK;
4752 pidgin_blist_update(purple_blist_get_default(), node);
4755 static void
4756 displayed_msg_update_ui_cb(PidginConversation *gtkconv, PurpleBlistNode *node)
4758 PidginBlistNode *ui = purple_blist_node_get_ui_data(node);
4759 if (ui->conv.conv != gtkconv->active_conv)
4760 return;
4761 ui->conv.flags &= ~(PIDGIN_BLIST_NODE_HAS_PENDING_MESSAGE |
4762 PIDGIN_BLIST_CHAT_HAS_PENDING_MESSAGE_WITH_NICK);
4763 pidgin_blist_update(purple_blist_get_default(), node);
4766 static void
4767 conversation_created_cb(PurpleConversation *conv, PidginBuddyList *gtkblist)
4769 PurpleAccount *account = purple_conversation_get_account(conv);
4771 if (PURPLE_IS_IM_CONVERSATION(conv)) {
4772 GSList *buddies = purple_blist_find_buddies(account, purple_conversation_get_name(conv));
4773 while (buddies) {
4774 PurpleBlistNode *buddy = buddies->data;
4775 PidginBlistNode *ui = purple_blist_node_get_ui_data(buddy);
4776 buddies = g_slist_delete_link(buddies, buddies);
4777 if (!ui)
4778 continue;
4779 ui->conv.conv = conv;
4780 ui->conv.flags = 0;
4781 purple_signal_connect(purple_conversations_get_handle(), "deleting-conversation",
4782 ui, PURPLE_CALLBACK(conversation_deleted_update_ui_cb), ui);
4783 purple_signal_connect(purple_conversations_get_handle(), "wrote-im-msg",
4784 ui, PURPLE_CALLBACK(written_msg_update_ui_cb), buddy);
4785 purple_signal_connect(pidgin_conversations_get_handle(), "conversation-displayed",
4786 ui, PURPLE_CALLBACK(displayed_msg_update_ui_cb), buddy);
4788 } else if (PURPLE_IS_CHAT_CONVERSATION(conv)) {
4789 PurpleChat *chat = purple_blist_find_chat(account, purple_conversation_get_name(conv));
4790 PidginBlistNode *ui;
4791 if (!chat)
4792 return;
4793 ui = purple_blist_node_get_ui_data(&(chat->node));
4794 if (!ui)
4795 return;
4796 ui->conv.conv = conv;
4797 ui->conv.flags = 0;
4798 purple_signal_connect(purple_conversations_get_handle(), "deleting-conversation",
4799 ui, PURPLE_CALLBACK(conversation_deleted_update_ui_cb), ui);
4800 purple_signal_connect(purple_conversations_get_handle(), "wrote-chat-msg",
4801 ui, PURPLE_CALLBACK(written_msg_update_ui_cb), chat);
4802 purple_signal_connect(pidgin_conversations_get_handle(), "conversation-displayed",
4803 ui, PURPLE_CALLBACK(displayed_msg_update_ui_cb), chat);
4807 /**********************************************************************************
4808 * Public API Functions *
4809 **********************************************************************************/
4811 static void
4812 pidgin_blist_new_node(PurpleBuddyList *list, PurpleBlistNode *node)
4814 purple_blist_node_set_ui_data(node, g_new0(PidginBlistNode, 1));
4817 gboolean pidgin_blist_node_is_contact_expanded(PurpleBlistNode *node)
4819 if (PURPLE_IS_BUDDY(node)) {
4820 node = node->parent;
4821 if (node == NULL)
4822 return FALSE;
4825 g_return_val_if_fail(PURPLE_IS_CONTACT(node), FALSE);
4827 return ((PidginBlistNode *)purple_blist_node_get_ui_data(node))->contact_expanded;
4830 enum {
4831 DRAG_BUDDY,
4832 DRAG_ROW,
4833 DRAG_VCARD,
4834 DRAG_TEXT,
4835 DRAG_URI,
4836 NUM_TARGETS
4839 void pidgin_blist_setup_sort_methods()
4841 const char *id;
4843 pidgin_blist_sort_method_reg("none", _("Manually"), sort_method_none);
4844 pidgin_blist_sort_method_reg("alphabetical", _("Alphabetically"), sort_method_alphabetical);
4845 pidgin_blist_sort_method_reg("status", _("By status"), sort_method_status);
4846 pidgin_blist_sort_method_reg("log_size", _("By recent log activity"), sort_method_log_activity);
4848 id = purple_prefs_get_string(PIDGIN_PREFS_ROOT "/blist/sort_type");
4849 if (id == NULL) {
4850 purple_debug_warning("gtkblist", "Sort method was NULL, resetting to alphabetical\n");
4851 id = "alphabetical";
4853 pidgin_blist_sort_method_set(id);
4856 static void _prefs_change_redo_list(const char *name, PurplePrefType type,
4857 gconstpointer val, gpointer data)
4859 GtkTreeSelection *sel;
4860 GtkTreeIter iter;
4861 PurpleBlistNode *node = NULL;
4863 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(gtkblist->treeview));
4864 if (gtk_tree_selection_get_selected(sel, NULL, &iter))
4866 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter, NODE_COLUMN, &node, -1);
4869 redo_buddy_list(purple_blist_get_default(), FALSE, FALSE);
4870 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(gtkblist->treeview));
4872 if (node)
4874 PidginBlistNode *gtknode;
4875 GtkTreePath *path;
4877 gtknode = purple_blist_node_get_ui_data(node);
4878 if (gtknode && gtknode->row)
4880 path = gtk_tree_row_reference_get_path(gtknode->row);
4881 gtk_tree_selection_select_path(sel, path);
4882 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(gtkblist->treeview), path, NULL, FALSE, 0, 0);
4883 gtk_tree_path_free(path);
4888 static void _prefs_change_sort_method(const char *pref_name, PurplePrefType type,
4889 gconstpointer val, gpointer data)
4891 if(purple_strequal(pref_name, PIDGIN_PREFS_ROOT "/blist/sort_type"))
4892 pidgin_blist_sort_method_set(val);
4895 static gboolean pidgin_blist_select_notebook_page_cb(gpointer user_data)
4897 PidginBuddyList *gtkblist = (PidginBuddyList *)user_data;
4898 int errors = 0;
4899 GList *list = NULL;
4900 PidginBuddyListPrivate *priv;
4902 priv = pidgin_buddy_list_get_instance_private(gtkblist);
4904 priv->select_notebook_page_timeout = 0;
4906 /* this is far too ugly thanks to me not wanting to fix #3989 properly right now */
4907 if (priv->error_scrollbook != NULL) {
4908 errors = gtk_notebook_get_n_pages(GTK_NOTEBOOK(priv->error_scrollbook->notebook));
4910 if ((list = purple_accounts_get_all_active()) != NULL || errors) {
4911 gtk_notebook_set_current_page(GTK_NOTEBOOK(gtkblist->notebook), 1);
4912 g_list_free(list);
4913 } else
4914 gtk_notebook_set_current_page(GTK_NOTEBOOK(gtkblist->notebook), 0);
4916 priv->select_notebook_page_timeout = 0;
4917 return FALSE;
4920 static void pidgin_blist_select_notebook_page(PidginBuddyList *gtkblist)
4922 PidginBuddyListPrivate *priv =
4923 pidgin_buddy_list_get_instance_private(gtkblist);
4924 priv->select_notebook_page_timeout = g_timeout_add(0,
4925 pidgin_blist_select_notebook_page_cb, gtkblist);
4928 static void account_modified(PurpleAccount *account, PidginBuddyList *gtkblist)
4930 if (!gtkblist)
4931 return;
4933 pidgin_blist_select_notebook_page(gtkblist);
4934 update_menu_bar(gtkblist);
4937 static void
4938 account_actions_changed(PurpleAccount *account, gpointer data)
4940 pidgin_blist_update_accounts_menu();
4943 static void
4944 account_status_changed(PurpleAccount *account, PurpleStatus *old,
4945 PurpleStatus *new, PidginBuddyList *gtkblist)
4947 if (!gtkblist)
4948 return;
4950 account_modified(account, gtkblist);
4953 static gboolean
4954 gtk_blist_window_key_press_cb(GtkWidget *w, GdkEventKey *event, PidginBuddyList *gtkblist)
4956 /* clear any tooltips */
4957 pidgin_blist_tooltip_destroy();
4959 return FALSE;
4962 static void
4963 reset_headline(PidginBuddyList *gtkblist)
4965 gtkblist->headline_callback = NULL;
4966 gtkblist->headline_data = NULL;
4967 gtkblist->headline_destroy = NULL;
4968 pidgin_set_urgent(GTK_WINDOW(gtkblist->window), FALSE);
4971 static gboolean
4972 headline_click_callback(gpointer unused)
4974 if (gtkblist->headline_callback)
4975 ((GSourceFunc) gtkblist->headline_callback)(gtkblist->headline_data);
4976 reset_headline(gtkblist);
4977 return FALSE;
4980 static gboolean
4981 headline_response_cb(GtkInfoBar *infobar, int resp, PidginBuddyList *gtkblist)
4983 gtk_widget_hide(gtkblist->headline);
4985 if (resp == GTK_RESPONSE_OK) {
4986 if (gtkblist->headline_callback)
4987 g_idle_add(headline_click_callback, NULL);
4988 else {
4989 if (gtkblist->headline_destroy)
4990 gtkblist->headline_destroy(gtkblist->headline_data);
4991 reset_headline(gtkblist);
4993 } else {
4994 if (gtkblist->headline_destroy)
4995 gtkblist->headline_destroy(gtkblist->headline_data);
4996 reset_headline(gtkblist);
4999 return FALSE;
5002 static void
5003 headline_realize_cb(GtkWidget *widget, gpointer data)
5005 GdkWindow *window = gtk_widget_get_window(widget);
5006 GdkDisplay *display = gdk_window_get_display(window);
5007 GdkCursor *hand_cursor = gdk_cursor_new_for_display(display, GDK_HAND2);
5008 gdk_window_set_cursor(window, hand_cursor);
5009 g_object_unref(hand_cursor);
5012 static gboolean
5013 headline_press_cb(GtkWidget *widget, GdkEventButton *event, GtkInfoBar *infobar)
5015 gtk_info_bar_response(infobar, GTK_RESPONSE_OK);
5016 return TRUE;
5019 /***********************************/
5020 /* Connection error handling stuff */
5021 /***********************************/
5023 #define OBJECT_DATA_KEY_ACCOUNT "account"
5024 #define DO_NOT_CLEAR_ERROR "do-not-clear-error"
5026 static gboolean
5027 find_account_widget(GObject *widget,
5028 PurpleAccount *account)
5030 if (g_object_get_data(widget, OBJECT_DATA_KEY_ACCOUNT) == account)
5031 return 0; /* found */
5032 else
5033 return 1;
5036 static void
5037 pack_protocol_icon_start(GtkWidget *box,
5038 PurpleAccount *account)
5040 GdkPixbuf *pixbuf;
5041 GtkWidget *image;
5043 pixbuf = pidgin_create_protocol_icon(account, PIDGIN_PROTOCOL_ICON_SMALL);
5044 if (pixbuf != NULL) {
5045 image = gtk_image_new_from_pixbuf(pixbuf);
5046 g_object_unref(pixbuf);
5048 gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
5052 static void
5053 add_error_dialog(PidginBuddyList *gtkblist,
5054 GtkWidget *dialog)
5056 PidginBuddyListPrivate *priv =
5057 pidgin_buddy_list_get_instance_private(gtkblist);
5058 gtk_container_add(GTK_CONTAINER(priv->error_scrollbook), dialog);
5061 static GtkWidget *
5062 find_child_widget_by_account(GtkContainer *container,
5063 PurpleAccount *account)
5065 GList *l = NULL;
5066 GList *children = NULL;
5067 GtkWidget *ret = NULL;
5068 /* XXX: Workaround for the currently incomplete implementation of PidginScrollBook */
5069 if (PIDGIN_IS_SCROLL_BOOK(container))
5070 container = GTK_CONTAINER(PIDGIN_SCROLL_BOOK(container)->notebook);
5071 children = gtk_container_get_children(container);
5072 l = g_list_find_custom(children, account, (GCompareFunc) find_account_widget);
5073 if (l)
5074 ret = GTK_WIDGET(l->data);
5075 g_list_free(children);
5076 return ret;
5079 static void
5080 remove_child_widget_by_account(GtkContainer *container,
5081 PurpleAccount *account)
5083 GtkWidget *widget = find_child_widget_by_account(container, account);
5084 if(widget) {
5085 /* Since we are destroying the widget in response to a change in
5086 * error, we should not clear the error.
5088 g_object_set_data(G_OBJECT(widget), DO_NOT_CLEAR_ERROR,
5089 GINT_TO_POINTER(TRUE));
5090 gtk_widget_destroy(widget);
5094 /* Generic error buttons */
5096 static void
5097 generic_error_modify_cb(PurpleAccount *account)
5099 purple_account_clear_current_error(account);
5100 pidgin_account_dialog_show(PIDGIN_MODIFY_ACCOUNT_DIALOG, account);
5103 static void
5104 generic_error_enable_cb(PurpleAccount *account)
5106 purple_account_clear_current_error(account);
5107 purple_account_set_enabled(account, purple_core_get_ui(), TRUE);
5110 static void
5111 generic_error_destroy_cb(GtkWidget *dialog,
5112 PurpleAccount *account)
5114 /* If the error dialog is being destroyed in response to the
5115 * account-error-changed signal, we don't want to clear the current
5116 * error.
5118 if (g_object_get_data(G_OBJECT(dialog), DO_NOT_CLEAR_ERROR) == NULL)
5119 purple_account_clear_current_error(account);
5122 #define SSL_FAQ_URI "https://developer.pidgin.im/wiki/FAQssl"
5124 static void
5125 ssl_faq_clicked_cb(PidginMiniDialog *mini_dialog,
5126 GtkButton *button,
5127 gpointer ignored)
5129 purple_notify_uri(NULL, SSL_FAQ_URI);
5132 static void
5133 add_generic_error_dialog(PurpleAccount *account,
5134 const PurpleConnectionErrorInfo *err)
5136 GtkWidget *mini_dialog;
5137 const char *username = purple_account_get_username(account);
5138 gboolean enabled =
5139 purple_account_get_enabled(account, purple_core_get_ui());
5140 char *primary;
5142 if (enabled)
5143 primary = g_strdup_printf(_("%s disconnected"), username);
5144 else
5145 primary = g_strdup_printf(_("%s disabled"), username);
5147 mini_dialog = pidgin_make_mini_dialog(NULL, "dialog-error",
5148 primary, err->description, account,
5149 (enabled ? _("Reconnect") : _("Re-enable")),
5150 (enabled ? PURPLE_CALLBACK(purple_account_connect)
5151 : PURPLE_CALLBACK(generic_error_enable_cb)),
5152 _("Modify Account"), PURPLE_CALLBACK(generic_error_modify_cb),
5153 NULL);
5155 g_free(primary);
5157 g_object_set_data(G_OBJECT(mini_dialog), OBJECT_DATA_KEY_ACCOUNT,
5158 account);
5160 if(err->type == PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT)
5161 pidgin_mini_dialog_add_non_closing_button(PIDGIN_MINI_DIALOG(mini_dialog),
5162 _("SSL FAQs"), ssl_faq_clicked_cb, NULL);
5164 g_signal_connect_after(mini_dialog, "destroy",
5165 (GCallback)generic_error_destroy_cb,
5166 account);
5168 add_error_dialog(gtkblist, mini_dialog);
5171 static void
5172 remove_generic_error_dialog(PurpleAccount *account)
5174 PidginBuddyListPrivate *priv =
5175 pidgin_buddy_list_get_instance_private(gtkblist);
5176 remove_child_widget_by_account(
5177 GTK_CONTAINER(priv->error_scrollbook), account);
5181 static void
5182 update_generic_error_message(PurpleAccount *account,
5183 const char *description)
5185 PidginBuddyListPrivate *priv =
5186 pidgin_buddy_list_get_instance_private(gtkblist);
5187 GtkWidget *mini_dialog = find_child_widget_by_account(
5188 GTK_CONTAINER(priv->error_scrollbook), account);
5189 pidgin_mini_dialog_set_description(PIDGIN_MINI_DIALOG(mini_dialog),
5190 description);
5194 /* Notifications about accounts which were disconnected with
5195 * PURPLE_CONNECTION_ERROR_NAME_IN_USE
5198 typedef void (*AccountFunction)(PurpleAccount *);
5200 static void
5201 elsewhere_foreach_account(PidginMiniDialog *mini_dialog,
5202 AccountFunction f)
5204 PurpleAccount *account;
5205 GList *labels = gtk_container_get_children(
5206 GTK_CONTAINER(mini_dialog->contents));
5207 GList *l;
5209 for (l = labels; l; l = l->next) {
5210 account = g_object_get_data(G_OBJECT(l->data), OBJECT_DATA_KEY_ACCOUNT);
5211 if (account)
5212 f(account);
5213 else
5214 purple_debug_warning("gtkblist", "mini_dialog's child "
5215 "didn't have an account stored in it!");
5217 g_list_free(labels);
5220 static void
5221 enable_account(PurpleAccount *account)
5223 purple_account_set_enabled(account, purple_core_get_ui(), TRUE);
5226 static void
5227 reconnect_elsewhere_accounts(PidginMiniDialog *mini_dialog,
5228 GtkButton *button,
5229 gpointer unused)
5231 elsewhere_foreach_account(mini_dialog, enable_account);
5234 static void
5235 clear_elsewhere_errors(PidginMiniDialog *mini_dialog,
5236 gpointer unused)
5238 elsewhere_foreach_account(mini_dialog, purple_account_clear_current_error);
5241 static void
5242 ensure_signed_on_elsewhere_minidialog(PidginBuddyList *gtkblist)
5244 PidginBuddyListPrivate *priv =
5245 pidgin_buddy_list_get_instance_private(gtkblist);
5246 PidginMiniDialog *mini_dialog;
5248 if(priv->signed_on_elsewhere)
5249 return;
5251 mini_dialog = priv->signed_on_elsewhere =
5252 pidgin_mini_dialog_new(_("Welcome back!"), NULL, PIDGIN_STOCK_DISCONNECT);
5254 pidgin_mini_dialog_add_button(mini_dialog, _("Re-enable"),
5255 reconnect_elsewhere_accounts, NULL);
5257 /* Make dismissing the dialog clear the errors. The "destroy" signal
5258 * does not appear to fire at quit, which is fortunate!
5260 g_signal_connect(G_OBJECT(mini_dialog), "destroy",
5261 (GCallback) clear_elsewhere_errors, NULL);
5263 add_error_dialog(gtkblist, GTK_WIDGET(mini_dialog));
5265 /* Set priv->signed_on_elsewhere to NULL when the dialog is destroyed */
5266 g_signal_connect(G_OBJECT(mini_dialog), "destroy",
5267 (GCallback) gtk_widget_destroyed, &(priv->signed_on_elsewhere));
5270 static void
5271 update_signed_on_elsewhere_minidialog_title(void)
5273 PidginBuddyListPrivate *priv =
5274 pidgin_buddy_list_get_instance_private(gtkblist);
5275 PidginMiniDialog *mini_dialog = priv->signed_on_elsewhere;
5276 guint accounts;
5277 char *title;
5279 if (mini_dialog == NULL)
5280 return;
5282 accounts = pidgin_mini_dialog_get_num_children(mini_dialog);
5283 if (accounts == 0) {
5284 gtk_widget_destroy(GTK_WIDGET(mini_dialog));
5285 return;
5288 title = g_strdup_printf(
5289 ngettext("%d account was disabled because you signed on from another location:",
5290 "%d accounts were disabled because you signed on from another location:",
5291 accounts),
5292 accounts);
5293 pidgin_mini_dialog_set_description(mini_dialog, title);
5294 g_free(title);
5297 static GtkWidget *
5298 create_account_label(PurpleAccount *account)
5300 GtkWidget *hbox, *label;
5301 const char *username = purple_account_get_username(account);
5302 char *markup;
5303 char *description;
5305 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
5306 g_object_set_data(G_OBJECT(hbox), OBJECT_DATA_KEY_ACCOUNT, account);
5308 pack_protocol_icon_start(hbox, account);
5310 label = gtk_label_new(NULL);
5311 markup = g_strdup_printf("<span size=\"smaller\">%s</span>", username);
5312 gtk_label_set_markup(GTK_LABEL(label), markup);
5313 g_free(markup);
5314 gtk_label_set_xalign(GTK_LABEL(label), 0);
5315 gtk_label_set_yalign(GTK_LABEL(label), 0);
5316 g_object_set(G_OBJECT(label), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
5317 description = purple_account_get_current_error(account)->description;
5318 if (description != NULL && *description != '\0')
5319 gtk_widget_set_tooltip_text(label, description);
5320 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
5322 return hbox;
5325 static void
5326 add_to_signed_on_elsewhere(PurpleAccount *account)
5328 PidginBuddyListPrivate *priv =
5329 pidgin_buddy_list_get_instance_private(gtkblist);
5330 PidginMiniDialog *mini_dialog;
5331 GtkWidget *account_label;
5333 ensure_signed_on_elsewhere_minidialog(gtkblist);
5334 mini_dialog = priv->signed_on_elsewhere;
5336 if(find_child_widget_by_account(GTK_CONTAINER(mini_dialog->contents), account))
5337 return;
5339 account_label = create_account_label(account);
5340 gtk_box_pack_start(mini_dialog->contents, account_label, FALSE, FALSE, 0);
5341 gtk_widget_show_all(account_label);
5343 update_signed_on_elsewhere_minidialog_title();
5346 static void
5347 remove_from_signed_on_elsewhere(PurpleAccount *account)
5349 PidginBuddyListPrivate *priv =
5350 pidgin_buddy_list_get_instance_private(gtkblist);
5351 PidginMiniDialog *mini_dialog = priv->signed_on_elsewhere;
5352 if(mini_dialog == NULL)
5353 return;
5355 remove_child_widget_by_account(GTK_CONTAINER(mini_dialog->contents), account);
5357 update_signed_on_elsewhere_minidialog_title();
5361 static void
5362 update_signed_on_elsewhere_tooltip(PurpleAccount *account,
5363 const char *description)
5365 PidginBuddyListPrivate *priv =
5366 pidgin_buddy_list_get_instance_private(gtkblist);
5367 GtkContainer *c = GTK_CONTAINER(priv->signed_on_elsewhere->contents);
5368 GtkWidget *label = find_child_widget_by_account(c, account);
5369 gtk_widget_set_tooltip_text(label, description);
5373 /* Call appropriate error notification code based on error types */
5374 static void
5375 update_account_error_state(PurpleAccount *account,
5376 const PurpleConnectionErrorInfo *old,
5377 const PurpleConnectionErrorInfo *new,
5378 PidginBuddyList *gtkblist)
5380 gboolean descriptions_differ;
5381 const char *desc;
5383 if (old == NULL && new == NULL)
5384 return;
5386 if (new != NULL)
5387 pidgin_blist_select_notebook_page(gtkblist);
5389 if (old != NULL && new == NULL) {
5390 if(old->type == PURPLE_CONNECTION_ERROR_NAME_IN_USE)
5391 remove_from_signed_on_elsewhere(account);
5392 else
5393 remove_generic_error_dialog(account);
5394 return;
5397 if (old == NULL && new != NULL) {
5398 if(new->type == PURPLE_CONNECTION_ERROR_NAME_IN_USE)
5399 add_to_signed_on_elsewhere(account);
5400 else
5401 add_generic_error_dialog(account, new);
5402 return;
5405 /* else, new and old are both non-NULL */
5407 descriptions_differ = !purple_strequal(old->description, new->description);
5408 desc = new->description;
5410 switch (new->type) {
5411 case PURPLE_CONNECTION_ERROR_NAME_IN_USE:
5412 if (old->type == PURPLE_CONNECTION_ERROR_NAME_IN_USE
5413 && descriptions_differ) {
5414 update_signed_on_elsewhere_tooltip(account, desc);
5415 } else {
5416 remove_generic_error_dialog(account);
5417 add_to_signed_on_elsewhere(account);
5419 break;
5420 default:
5421 if (old->type == PURPLE_CONNECTION_ERROR_NAME_IN_USE) {
5422 remove_from_signed_on_elsewhere(account);
5423 add_generic_error_dialog(account, new);
5424 } else if (descriptions_differ) {
5425 update_generic_error_message(account, desc);
5427 break;
5431 /* In case accounts are loaded before the blist (which they currently are),
5432 * let's call update_account_error_state ourselves on every account's current
5433 * state when the blist starts.
5435 static void
5436 show_initial_account_errors(PidginBuddyList *gtkblist)
5438 GList *l = purple_accounts_get_all();
5439 PurpleAccount *account;
5440 const PurpleConnectionErrorInfo *err;
5442 for (; l; l = l->next)
5444 account = l->data;
5445 err = purple_account_get_current_error(account);
5447 update_account_error_state(account, NULL, err, gtkblist);
5451 /* This assumes there are not things like groupless buddies or multi-leveled groups.
5452 * I'm sure other things in this code assumes that also.
5454 static void
5455 treeview_style_set(GtkWidget *widget,
5456 gpointer data)
5458 PurpleBuddyList *list = data;
5459 PurpleBlistNode *node = purple_blist_get_root(list);
5460 while (node) {
5461 pidgin_blist_update_group(list, node);
5462 node = node->next;
5466 /******************************************/
5467 /* End of connection error handling stuff */
5468 /******************************************/
5470 static int
5471 blist_focus_cb(GtkWidget *widget, GdkEventFocus *event, PidginBuddyList *gtkblist)
5473 if(event->in) {
5474 gtk_blist_focused = TRUE;
5475 pidgin_set_urgent(GTK_WINDOW(gtkblist->window), FALSE);
5476 } else {
5477 gtk_blist_focused = FALSE;
5479 return 0;
5482 #if 0
5483 static GtkWidget *
5484 kiosk_page()
5486 GtkWidget *ret = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BOX_SPACE);
5487 GtkWidget *label;
5488 GtkWidget *entry;
5489 GtkWidget *bbox;
5490 GtkWidget *button;
5492 label = gtk_label_new(NULL);
5493 gtk_box_pack_start(GTK_BOX(ret), label, TRUE, TRUE, 0);
5495 label = gtk_label_new(NULL);
5496 gtk_label_set_markup(GTK_LABEL(label), _("<b>Username:</b>"));
5497 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
5498 gtk_box_pack_start(GTK_BOX(ret), label, FALSE, FALSE, 0);
5499 entry = gtk_entry_new();
5500 gtk_box_pack_start(GTK_BOX(ret), entry, FALSE, FALSE, 0);
5502 label = gtk_label_new(NULL);
5503 gtk_label_set_markup(GTK_LABEL(label), _("<b>Password:</b>"));
5504 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
5505 gtk_box_pack_start(GTK_BOX(ret), label, FALSE, FALSE, 0);
5506 entry = gtk_entry_new();
5507 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
5508 gtk_box_pack_start(GTK_BOX(ret), entry, FALSE, FALSE, 0);
5510 label = gtk_label_new(" ");
5511 gtk_box_pack_start(GTK_BOX(ret), label, FALSE, FALSE, 0);
5513 bbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
5514 button = gtk_button_new_with_mnemonic(_("_Login"));
5515 gtk_box_pack_start(GTK_BOX(ret), bbox, FALSE, FALSE, 0);
5516 gtk_container_add(GTK_CONTAINER(bbox), button);
5519 label = gtk_label_new(NULL);
5520 gtk_box_pack_start(GTK_BOX(ret), label, TRUE, TRUE, 0);
5522 gtk_container_set_border_width(GTK_CONTAINER(ret), PIDGIN_HIG_BORDER);
5524 gtk_widget_show_all(ret);
5525 return ret;
5527 #endif
5529 /* builds the blist layout according to to the current theme */
5530 static void
5531 pidgin_blist_build_layout(PurpleBuddyList *list)
5533 GtkTreeViewColumn *column;
5534 PidginBlistLayout *layout;
5535 PidginBlistTheme *theme;
5536 GtkCellRenderer *rend;
5537 gint i, status_icon = 0, text = 1, emblem = 2, protocol_icon = 3, buddy_icon = 4;
5540 column = gtkblist->text_column;
5542 if ((theme = pidgin_blist_get_theme()) != NULL && (layout = pidgin_blist_theme_get_layout(theme)) != NULL) {
5543 status_icon = layout->status_icon ;
5544 text = layout->text;
5545 emblem = layout->emblem;
5546 protocol_icon = layout->protocol_icon;
5547 buddy_icon = layout->buddy_icon;
5550 gtk_tree_view_column_clear(column);
5552 /* group */
5553 rend = pidgin_cell_renderer_expander_new();
5554 gtk_tree_view_column_pack_start(column, rend, FALSE);
5555 gtk_tree_view_column_set_attributes(column, rend,
5556 "visible", GROUP_EXPANDER_VISIBLE_COLUMN,
5557 "expander-visible", GROUP_EXPANDER_COLUMN,
5558 "sensitive", GROUP_EXPANDER_COLUMN,
5559 "cell-background-rgba", BGCOLOR_COLUMN,
5560 NULL);
5562 /* contact */
5563 rend = pidgin_cell_renderer_expander_new();
5564 gtk_tree_view_column_pack_start(column, rend, FALSE);
5565 gtk_tree_view_column_set_attributes(column, rend,
5566 "visible", CONTACT_EXPANDER_VISIBLE_COLUMN,
5567 "expander-visible", CONTACT_EXPANDER_COLUMN,
5568 "sensitive", CONTACT_EXPANDER_COLUMN,
5569 "cell-background-rgba", BGCOLOR_COLUMN,
5570 NULL);
5572 for (i = 0; i < 5; i++) {
5574 if (status_icon == i) {
5575 /* status icons */
5576 rend = gtk_cell_renderer_pixbuf_new();
5577 gtk_tree_view_column_pack_start(column, rend, FALSE);
5578 gtk_tree_view_column_set_attributes(column, rend,
5579 "pixbuf", STATUS_ICON_COLUMN,
5580 "visible", STATUS_ICON_VISIBLE_COLUMN,
5581 "cell-background-rgba", BGCOLOR_COLUMN,
5582 NULL);
5583 g_object_set(rend, "xalign", 0.0, "xpad", 6, "ypad", 0, NULL);
5585 } else if (text == i) {
5586 /* name */
5587 gtkblist->text_rend = rend = gtk_cell_renderer_text_new();
5588 gtk_tree_view_column_pack_start(column, rend, TRUE);
5589 gtk_tree_view_column_set_attributes(column, rend,
5590 "cell-background-rgba", BGCOLOR_COLUMN,
5591 "markup", NAME_COLUMN,
5592 NULL);
5593 g_signal_connect(
5594 G_OBJECT(rend), "editing-started",
5595 G_CALLBACK(gtk_blist_renderer_editing_started_cb),
5596 list);
5597 g_signal_connect(G_OBJECT(rend), "editing-canceled", G_CALLBACK(gtk_blist_renderer_editing_cancelled_cb), list);
5598 g_signal_connect(G_OBJECT(rend), "edited", G_CALLBACK(gtk_blist_renderer_edited_cb), list);
5599 g_object_set(rend, "ypad", 0, "yalign", 0.5, NULL);
5600 g_object_set(rend, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
5602 /* idle */
5603 rend = gtk_cell_renderer_text_new();
5604 g_object_set(rend, "xalign", 1.0, "ypad", 0, NULL);
5605 gtk_tree_view_column_pack_start(column, rend, FALSE);
5606 gtk_tree_view_column_set_attributes(column, rend,
5607 "markup", IDLE_COLUMN,
5608 "visible", IDLE_VISIBLE_COLUMN,
5609 "cell-background-rgba", BGCOLOR_COLUMN,
5610 NULL);
5611 } else if (emblem == i) {
5612 /* emblem */
5613 rend = gtk_cell_renderer_pixbuf_new();
5614 g_object_set(rend, "xalign", 1.0, "yalign", 0.5, "ypad", 0, "xpad", 3, NULL);
5615 gtk_tree_view_column_pack_start(column, rend, FALSE);
5616 gtk_tree_view_column_set_attributes(column, rend, "pixbuf", EMBLEM_COLUMN,
5617 "cell-background-rgba", BGCOLOR_COLUMN,
5618 "visible", EMBLEM_VISIBLE_COLUMN, NULL);
5620 } else if (protocol_icon == i) {
5621 /* protocol icon */
5622 rend = gtk_cell_renderer_pixbuf_new();
5623 gtk_tree_view_column_pack_start(column, rend, FALSE);
5624 gtk_tree_view_column_set_attributes(column, rend,
5625 "pixbuf", PROTOCOL_ICON_COLUMN,
5626 "visible", PROTOCOL_ICON_VISIBLE_COLUMN,
5627 "cell-background-rgba", BGCOLOR_COLUMN,
5628 NULL);
5629 g_object_set(rend, "xalign", 0.0, "xpad", 3, "ypad", 0, NULL);
5631 } else if (buddy_icon == i) {
5632 /* buddy icon */
5633 rend = gtk_cell_renderer_pixbuf_new();
5634 g_object_set(rend, "xalign", 1.0, "ypad", 0, NULL);
5635 gtk_tree_view_column_pack_start(column, rend, FALSE);
5636 gtk_tree_view_column_set_attributes(column, rend, "pixbuf", BUDDY_ICON_COLUMN,
5637 "cell-background-rgba", BGCOLOR_COLUMN,
5638 "visible", BUDDY_ICON_VISIBLE_COLUMN,
5639 NULL);
5642 }/* end for loop */
5646 static gboolean
5647 pidgin_blist_search_equal_func(GtkTreeModel *model, gint column,
5648 const gchar *key, GtkTreeIter *iter, gpointer data)
5650 PurpleBlistNode *node = NULL;
5651 gboolean res = TRUE;
5652 const char *compare = NULL;
5654 if (!pidgin_tree_view_search_equal_func(model, column, key, iter, data))
5655 return FALSE;
5657 /* If the search string does not match the displayed label, then look
5658 * at the alternate labels for the nodes and search in them. Currently,
5659 * alternate labels that make sense are usernames/email addresses for
5660 * buddies (but only for the ones who don't have a local alias).
5663 gtk_tree_model_get(model, iter, NODE_COLUMN, &node, -1);
5664 if (!node)
5665 return TRUE;
5667 compare = NULL;
5668 if (PURPLE_IS_CONTACT(node)) {
5669 PurpleBuddy *b = purple_contact_get_priority_buddy(PURPLE_CONTACT(node));
5670 if (!purple_buddy_get_local_alias(b))
5671 compare = purple_buddy_get_name(b);
5672 } else if (PURPLE_IS_BUDDY(node)) {
5673 if (!purple_buddy_get_local_alias(PURPLE_BUDDY(node)))
5674 compare = purple_buddy_get_name(PURPLE_BUDDY(node));
5677 if (compare) {
5678 char *tmp, *enteredstring;
5679 tmp = g_utf8_normalize(key, -1, G_NORMALIZE_DEFAULT);
5680 enteredstring = g_utf8_casefold(tmp, -1);
5681 g_free(tmp);
5683 if (purple_str_has_prefix(compare, enteredstring))
5684 res = FALSE;
5685 g_free(enteredstring);
5688 return res;
5691 static void pidgin_blist_show(PurpleBuddyList *list)
5693 PidginBuddyListPrivate *priv;
5694 void *handle;
5695 GtkTreeViewColumn *column;
5696 GtkWidget *menu;
5697 GtkWidget *sep;
5698 GtkWidget *infobar;
5699 GtkWidget *content_area;
5700 GtkWidget *label;
5701 GtkWidget *close;
5702 gchar *text;
5703 const char *theme_name;
5704 GtkActionGroup *action_group;
5705 GError *error;
5706 GtkAccelGroup *accel_group;
5707 GtkTreeSelection *selection;
5708 GtkTargetEntry dte[] = {{"PURPLE_BLIST_NODE", GTK_TARGET_SAME_APP, DRAG_ROW},
5709 {"application/x-im-contact", 0, DRAG_BUDDY},
5710 {"text/x-vcard", 0, DRAG_VCARD },
5711 {"text/uri-list", 0, DRAG_URI},
5712 {"text/plain", 0, DRAG_TEXT}};
5713 GtkTargetEntry ste[] = {{"PURPLE_BLIST_NODE", GTK_TARGET_SAME_APP, DRAG_ROW},
5714 {"application/x-im-contact", 0, DRAG_BUDDY},
5715 {"text/x-vcard", 0, DRAG_VCARD }};
5716 if (gtkblist && gtkblist->window) {
5717 purple_blist_set_visible(purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/list_visible"));
5718 return;
5721 gtkblist = PIDGIN_BUDDY_LIST(list);
5722 priv = pidgin_buddy_list_get_instance_private(gtkblist);
5724 if (priv->current_theme)
5725 g_object_unref(priv->current_theme);
5727 theme_name = purple_prefs_get_string(PIDGIN_PREFS_ROOT "/blist/theme");
5728 if (theme_name && *theme_name)
5729 priv->current_theme = g_object_ref(PIDGIN_BLIST_THEME(purple_theme_manager_find_theme(theme_name, "blist")));
5730 else
5731 priv->current_theme = NULL;
5733 gtkblist->empty_avatar = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 32, 32);
5734 gdk_pixbuf_fill(gtkblist->empty_avatar, 0x00000000);
5736 gtkblist->window = pidgin_create_window(_("Buddy List"), 0, "buddy_list", TRUE);
5737 g_signal_connect(G_OBJECT(gtkblist->window), "focus-in-event",
5738 G_CALLBACK(blist_focus_cb), gtkblist);
5739 g_signal_connect(G_OBJECT(gtkblist->window), "focus-out-event",
5740 G_CALLBACK(blist_focus_cb), gtkblist);
5742 gtkblist->main_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
5743 gtk_widget_show(gtkblist->main_vbox);
5744 gtk_container_add(GTK_CONTAINER(gtkblist->window), gtkblist->main_vbox);
5746 g_signal_connect(G_OBJECT(gtkblist->window), "delete_event", G_CALLBACK(gtk_blist_delete_cb), NULL);
5747 g_signal_connect(G_OBJECT(gtkblist->window), "hide",
5748 G_CALLBACK(gtk_blist_hide_cb), gtkblist);
5749 g_signal_connect(G_OBJECT(gtkblist->window), "show",
5750 G_CALLBACK(gtk_blist_show_cb), gtkblist);
5751 g_signal_connect(G_OBJECT(gtkblist->window), "size-allocate",
5752 G_CALLBACK(gtk_blist_size_allocate_cb), NULL);
5753 g_signal_connect(G_OBJECT(gtkblist->window), "visibility_notify_event", G_CALLBACK(gtk_blist_visibility_cb), NULL);
5754 g_signal_connect(G_OBJECT(gtkblist->window), "window_state_event", G_CALLBACK(gtk_blist_window_state_cb), NULL);
5755 g_signal_connect(G_OBJECT(gtkblist->window), "key_press_event", G_CALLBACK(gtk_blist_window_key_press_cb), gtkblist);
5756 gtk_widget_add_events(gtkblist->window, GDK_VISIBILITY_NOTIFY_MASK);
5758 /******************************* Menu bar *************************************/
5759 action_group = gtk_action_group_new("BListActions");
5760 gtk_action_group_set_translation_domain(action_group, PACKAGE);
5761 gtk_action_group_add_actions(action_group,
5762 blist_menu_entries,
5763 G_N_ELEMENTS(blist_menu_entries),
5764 GTK_WINDOW(gtkblist->window));
5765 gtk_action_group_add_toggle_actions(action_group,
5766 blist_menu_toggle_entries,
5767 G_N_ELEMENTS(blist_menu_toggle_entries),
5768 GTK_WINDOW(gtkblist->window));
5770 gtkblist->ui = gtk_ui_manager_new();
5771 gtk_ui_manager_insert_action_group(gtkblist->ui, action_group, 0);
5773 accel_group = gtk_ui_manager_get_accel_group(gtkblist->ui);
5774 gtk_window_add_accel_group(GTK_WINDOW(gtkblist->window), accel_group);
5775 pidgin_load_accels();
5776 g_signal_connect(G_OBJECT(accel_group), "accel-changed", G_CALLBACK(pidgin_save_accels_cb), NULL);
5778 error = NULL;
5779 if (!gtk_ui_manager_add_ui_from_string(gtkblist->ui, blist_menu, -1, &error))
5781 g_message("building menus failed: %s", error->message);
5782 g_error_free(error);
5783 exit(EXIT_FAILURE);
5786 menu = gtk_ui_manager_get_widget(gtkblist->ui, "/BList");
5787 gtkblist->menutray = pidgin_menu_tray_new();
5788 gtk_menu_shell_append(GTK_MENU_SHELL(menu), gtkblist->menutray);
5789 gtk_widget_show(gtkblist->menutray);
5790 gtk_widget_show(menu);
5791 gtk_box_pack_start(GTK_BOX(gtkblist->main_vbox), menu, FALSE, FALSE, 0);
5793 menu = gtk_ui_manager_get_widget(gtkblist->ui, "/BList/AccountsMenu");
5794 accountmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu));
5796 /****************************** Notebook *************************************/
5797 gtkblist->notebook = gtk_notebook_new();
5798 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gtkblist->notebook), FALSE);
5799 gtk_notebook_set_show_border(GTK_NOTEBOOK(gtkblist->notebook), FALSE);
5800 gtk_box_pack_start(GTK_BOX(gtkblist->main_vbox), gtkblist->notebook, TRUE, TRUE, 0);
5802 #if 0
5803 gtk_notebook_append_page(GTK_NOTEBOOK(gtkblist->notebook), kiosk_page(), NULL);
5804 #endif
5806 /* Translators: Please maintain the use of ⇦ or ⇨ to refer to menu hierarchy */
5807 text = g_strdup_printf(_("<span weight='bold' size='larger'>Welcome to %s!</span>\n\n"
5809 "You have no accounts enabled. Enable your IM accounts from the "
5810 "<b>Accounts</b> window at <b>Accounts⇨Manage Accounts</b>. Once you "
5811 "enable accounts, you'll be able to sign on, set your status, "
5812 "and talk to your friends."), PIDGIN_NAME);
5813 label = gtk_label_new(NULL);
5814 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
5815 gtk_label_set_yalign(GTK_LABEL(label), 0.2);
5816 gtk_label_set_markup(GTK_LABEL(label), text);
5817 g_free(text);
5818 gtk_notebook_append_page(GTK_NOTEBOOK(gtkblist->notebook),label, NULL);
5819 gtkblist->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
5820 gtk_notebook_append_page(GTK_NOTEBOOK(gtkblist->notebook), gtkblist->vbox, NULL);
5821 gtk_widget_show_all(gtkblist->notebook);
5822 pidgin_blist_select_notebook_page(gtkblist);
5824 /****************************** Headline **********************************/
5826 gtkblist->headline = gtk_event_box_new();
5827 gtk_box_pack_start(GTK_BOX(gtkblist->vbox), gtkblist->headline,
5828 FALSE, FALSE, 0);
5829 infobar = gtk_info_bar_new();
5830 gtk_container_add(GTK_CONTAINER(gtkblist->headline), infobar);
5831 gtk_info_bar_set_default_response(GTK_INFO_BAR(infobar), GTK_RESPONSE_OK);
5832 gtk_info_bar_set_message_type(GTK_INFO_BAR(infobar), GTK_MESSAGE_INFO);
5834 content_area = gtk_info_bar_get_content_area(GTK_INFO_BAR(infobar));
5835 gtkblist->headline_image = gtk_image_new_from_pixbuf(NULL);
5836 gtk_widget_set_halign(gtkblist->headline_image, GTK_ALIGN_CENTER);
5837 gtk_widget_set_valign(gtkblist->headline_image, GTK_ALIGN_CENTER);
5838 gtkblist->headline_label = gtk_label_new(NULL);
5839 gtk_label_set_line_wrap(GTK_LABEL(gtkblist->headline_label), TRUE);
5840 gtk_box_pack_start(GTK_BOX(content_area), gtkblist->headline_image,
5841 FALSE, FALSE, 0);
5842 gtk_box_pack_start(GTK_BOX(content_area), gtkblist->headline_label,
5843 TRUE, TRUE, 0);
5845 close = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
5846 close = pidgin_create_small_button(close);
5847 gtk_widget_set_tooltip_text(close, _("Close"));
5848 gtk_info_bar_add_action_widget(GTK_INFO_BAR(infobar), close,
5849 GTK_RESPONSE_CLOSE);
5851 g_signal_connect(infobar, "response", G_CALLBACK(headline_response_cb),
5852 gtkblist);
5853 g_signal_connect(infobar, "close", G_CALLBACK(gtk_info_bar_response),
5854 GINT_TO_POINTER(GTK_RESPONSE_CLOSE));
5855 g_signal_connect(gtkblist->headline, "realize",
5856 G_CALLBACK(headline_realize_cb), NULL);
5857 g_signal_connect(gtkblist->headline, "button-press-event",
5858 G_CALLBACK(headline_press_cb), infobar);
5860 /****************************** GtkTreeView **********************************/
5861 gtkblist->treemodel = gtk_tree_store_new(BLIST_COLUMNS,
5862 GDK_TYPE_PIXBUF, /* Status icon */
5863 G_TYPE_BOOLEAN, /* Status icon visible */
5864 G_TYPE_STRING, /* Name */
5865 G_TYPE_STRING, /* Idle */
5866 G_TYPE_BOOLEAN, /* Idle visible */
5867 GDK_TYPE_PIXBUF, /* Buddy icon */
5868 G_TYPE_BOOLEAN, /* Buddy icon visible */
5869 G_TYPE_POINTER, /* Node */
5870 GDK_TYPE_RGBA, /* bgcolor */
5871 G_TYPE_BOOLEAN, /* Group expander */
5872 G_TYPE_BOOLEAN, /* Group expander visible */
5873 G_TYPE_BOOLEAN, /* Contact expander */
5874 G_TYPE_BOOLEAN, /* Contact expander visible */
5875 GDK_TYPE_PIXBUF, /* Emblem */
5876 G_TYPE_BOOLEAN, /* Emblem visible */
5877 GDK_TYPE_PIXBUF, /* Protocol icon */
5878 G_TYPE_BOOLEAN /* Protocol visible */
5881 gtkblist->treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(gtkblist->treemodel));
5883 gtk_widget_show(gtkblist->treeview);
5884 gtk_widget_set_name(gtkblist->treeview, "pidgin_blist_treeview");
5886 g_signal_connect(gtkblist->treeview,
5887 "style-updated",
5888 G_CALLBACK(treeview_style_set), list);
5889 /* Set up selection stuff */
5890 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(gtkblist->treeview));
5891 g_signal_connect(G_OBJECT(selection), "changed", G_CALLBACK(pidgin_blist_selection_changed), NULL);
5893 /* Set up dnd */
5894 gtk_tree_view_enable_model_drag_source(GTK_TREE_VIEW(gtkblist->treeview),
5895 GDK_BUTTON1_MASK, ste, 3,
5896 GDK_ACTION_COPY);
5897 gtk_tree_view_enable_model_drag_dest(GTK_TREE_VIEW(gtkblist->treeview),
5898 dte, 5,
5899 GDK_ACTION_COPY | GDK_ACTION_MOVE);
5901 g_signal_connect(G_OBJECT(gtkblist->treeview), "drag-data-received", G_CALLBACK(pidgin_blist_drag_data_rcv_cb), NULL);
5902 g_signal_connect(G_OBJECT(gtkblist->treeview), "drag-data-get", G_CALLBACK(pidgin_blist_drag_data_get_cb), NULL);
5903 #ifdef _WIN32
5904 g_signal_connect(G_OBJECT(gtkblist->treeview), "drag-begin", G_CALLBACK(pidgin_blist_drag_begin), NULL);
5905 #endif
5906 g_signal_connect(G_OBJECT(gtkblist->treeview), "drag-motion", G_CALLBACK(pidgin_blist_drag_motion_cb), NULL);
5907 g_signal_connect(G_OBJECT(gtkblist->treeview), "motion-notify-event", G_CALLBACK(pidgin_blist_motion_cb), NULL);
5908 g_signal_connect(G_OBJECT(gtkblist->treeview), "leave-notify-event", G_CALLBACK(pidgin_blist_leave_cb), NULL);
5910 /* Tooltips */
5911 pidgin_tooltip_setup_for_treeview(gtkblist->treeview, NULL,
5912 pidgin_blist_create_tooltip,
5913 pidgin_blist_paint_tip);
5915 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(gtkblist->treeview), FALSE);
5917 /* expander columns */
5918 column = gtk_tree_view_column_new();
5919 gtk_tree_view_append_column(GTK_TREE_VIEW(gtkblist->treeview), column);
5920 gtk_tree_view_column_set_visible(column, FALSE);
5921 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(gtkblist->treeview), column);
5923 /* everything else column */
5924 gtkblist->text_column = gtk_tree_view_column_new ();
5925 gtk_tree_view_append_column(GTK_TREE_VIEW(gtkblist->treeview), gtkblist->text_column);
5926 pidgin_blist_build_layout(list);
5928 g_signal_connect(G_OBJECT(gtkblist->treeview), "row-activated",
5929 G_CALLBACK(gtk_blist_row_activated_cb), gtkblist);
5930 g_signal_connect(G_OBJECT(gtkblist->treeview), "row-expanded",
5931 G_CALLBACK(gtk_blist_row_expanded_cb), gtkblist);
5932 g_signal_connect(G_OBJECT(gtkblist->treeview), "row-collapsed",
5933 G_CALLBACK(gtk_blist_row_collapsed_cb), gtkblist);
5934 g_signal_connect(G_OBJECT(gtkblist->treeview), "button-press-event", G_CALLBACK(gtk_blist_button_press_cb), NULL);
5935 g_signal_connect(G_OBJECT(gtkblist->treeview), "key-press-event", G_CALLBACK(gtk_blist_key_press_cb), NULL);
5936 g_signal_connect(G_OBJECT(gtkblist->treeview), "popup-menu", G_CALLBACK(pidgin_blist_popup_menu_cb), NULL);
5938 /* Enable CTRL+F searching */
5939 gtk_tree_view_set_search_column(GTK_TREE_VIEW(gtkblist->treeview), NAME_COLUMN);
5940 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(gtkblist->treeview),
5941 pidgin_blist_search_equal_func, NULL, NULL);
5943 gtk_box_pack_start(GTK_BOX(gtkblist->vbox),
5944 pidgin_make_scrollable(gtkblist->treeview, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC, GTK_SHADOW_NONE, -1, -1),
5945 TRUE, TRUE, 0);
5947 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
5948 gtk_box_pack_start(GTK_BOX(gtkblist->vbox), sep, FALSE, FALSE, 0);
5950 gtkblist->scrollbook = pidgin_scroll_book_new();
5951 gtk_box_pack_start(GTK_BOX(gtkblist->vbox), gtkblist->scrollbook, FALSE, FALSE, 0);
5953 priv->error_scrollbook = PIDGIN_SCROLL_BOOK(pidgin_scroll_book_new());
5954 gtk_box_pack_start(GTK_BOX(gtkblist->vbox),
5955 GTK_WIDGET(priv->error_scrollbook), FALSE, FALSE, 0);
5957 /* Add the statusbox */
5958 gtkblist->statusbox = pidgin_status_box_new();
5959 gtk_box_pack_start(GTK_BOX(gtkblist->vbox), gtkblist->statusbox, FALSE, TRUE, 0);
5960 gtk_widget_set_name(gtkblist->statusbox, "pidgin_blist_statusbox");
5961 gtk_widget_show(gtkblist->statusbox);
5963 /* set the Show Offline Buddies option. must be done
5964 * after the treeview or faceprint gets mad. -Robot101
5966 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(gtk_ui_manager_get_action(gtkblist->ui, "/BList/BuddiesMenu/ShowMenu/ShowOffline")),
5967 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_offline_buddies"));
5969 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(gtk_ui_manager_get_action(gtkblist->ui, "/BList/BuddiesMenu/ShowMenu/ShowEmptyGroups")),
5970 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_empty_groups"));
5972 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(gtk_ui_manager_get_action(gtkblist->ui, "/BList/ToolsMenu/MuteSounds")),
5973 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/sound/mute"));
5975 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(gtk_ui_manager_get_action(gtkblist->ui, "/BList/BuddiesMenu/ShowMenu/ShowBuddyDetails")),
5976 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons"));
5978 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(gtk_ui_manager_get_action(gtkblist->ui, "/BList/BuddiesMenu/ShowMenu/ShowIdleTimes")),
5979 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_idle_time"));
5981 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(gtk_ui_manager_get_action(gtkblist->ui, "/BList/BuddiesMenu/ShowMenu/ShowProtocolIcons")),
5982 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_protocol_icons"));
5984 if(purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/sound/method"), "none"))
5985 gtk_action_set_sensitive(gtk_ui_manager_get_action(gtkblist->ui, "/BList/ToolsMenu/MuteSounds"), FALSE);
5987 /* Update some dynamic things */
5988 update_menu_bar(gtkblist);
5989 pidgin_blist_update_plugin_actions();
5990 pidgin_blist_update_sort_methods();
5992 /* OK... let's show this bad boy. */
5993 pidgin_blist_refresh(list);
5994 pidgin_blist_restore_window_state();
5995 gtk_widget_show_all(GTK_WIDGET(gtkblist->vbox));
5996 gtk_widget_realize(GTK_WIDGET(gtkblist->window));
5997 purple_blist_set_visible(purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/list_visible"));
5999 /* start the refresh timer */
6000 gtkblist->refresh_timer = g_timeout_add_seconds(30, (GSourceFunc)pidgin_blist_refresh_timer, list);
6002 handle = pidgin_blist_get_handle();
6004 /* things that affect how buddies are displayed */
6005 purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/blist/show_buddy_icons",
6006 _prefs_change_redo_list, NULL);
6007 purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/blist/show_idle_time",
6008 _prefs_change_redo_list, NULL);
6009 purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/blist/show_empty_groups",
6010 _prefs_change_redo_list, NULL);
6011 purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/blist/show_offline_buddies",
6012 _prefs_change_redo_list, NULL);
6013 purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/blist/show_protocol_icons",
6014 _prefs_change_redo_list, NULL);
6016 /* sorting */
6017 purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/blist/sort_type",
6018 _prefs_change_sort_method, NULL);
6020 /* menus */
6021 purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/sound/mute",
6022 pidgin_blist_mute_pref_cb, NULL);
6023 purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/sound/method",
6024 pidgin_blist_sound_method_pref_cb, NULL);
6026 /* Setup some purple signal handlers. */
6028 handle = purple_accounts_get_handle();
6029 purple_signal_connect(handle, "account-enabled", gtkblist,
6030 PURPLE_CALLBACK(account_modified), gtkblist);
6031 purple_signal_connect(handle, "account-disabled", gtkblist,
6032 PURPLE_CALLBACK(account_modified), gtkblist);
6033 purple_signal_connect(handle, "account-removed", gtkblist,
6034 PURPLE_CALLBACK(account_modified), gtkblist);
6035 purple_signal_connect(handle, "account-status-changed", gtkblist,
6036 PURPLE_CALLBACK(account_status_changed),
6037 gtkblist);
6038 purple_signal_connect(handle, "account-error-changed", gtkblist,
6039 PURPLE_CALLBACK(update_account_error_state),
6040 gtkblist);
6041 purple_signal_connect(handle, "account-actions-changed", gtkblist,
6042 PURPLE_CALLBACK(account_actions_changed), NULL);
6044 handle = pidgin_accounts_get_handle();
6045 purple_signal_connect(handle, "account-modified", gtkblist,
6046 PURPLE_CALLBACK(account_modified), gtkblist);
6048 handle = purple_connections_get_handle();
6049 purple_signal_connect(handle, "signed-on", gtkblist,
6050 PURPLE_CALLBACK(sign_on_off_cb), list);
6051 purple_signal_connect(handle, "signed-off", gtkblist,
6052 PURPLE_CALLBACK(sign_on_off_cb), list);
6054 handle = purple_plugins_get_handle();
6055 purple_signal_connect(handle, "plugin-load", gtkblist,
6056 PURPLE_CALLBACK(plugin_changed_cb), NULL);
6057 purple_signal_connect(handle, "plugin-unload", gtkblist,
6058 PURPLE_CALLBACK(plugin_changed_cb), NULL);
6060 handle = purple_conversations_get_handle();
6061 purple_signal_connect(handle, "conversation-updated", gtkblist,
6062 PURPLE_CALLBACK(conversation_updated_cb),
6063 gtkblist);
6064 purple_signal_connect(handle, "deleting-conversation", gtkblist,
6065 PURPLE_CALLBACK(conversation_deleting_cb),
6066 gtkblist);
6067 purple_signal_connect(handle, "conversation-created", gtkblist,
6068 PURPLE_CALLBACK(conversation_created_cb),
6069 gtkblist);
6070 purple_signal_connect(handle, "chat-joined", gtkblist,
6071 PURPLE_CALLBACK(conversation_created_cb),
6072 gtkblist);
6074 gtk_widget_hide(gtkblist->headline);
6076 show_initial_account_errors(gtkblist);
6078 /* emit our created signal */
6079 handle = pidgin_blist_get_handle();
6080 purple_signal_emit(handle, "gtkblist-created", list);
6083 static void redo_buddy_list(PurpleBuddyList *list, gboolean remove, gboolean rerender)
6085 PurpleBlistNode *node;
6087 gtkblist = PIDGIN_BUDDY_LIST(list);
6088 if(!gtkblist || !gtkblist->treeview)
6089 return;
6091 node = purple_blist_get_root(list);
6093 while (node)
6095 /* This is only needed when we're reverting to a non-GTK+ sorted
6096 * status. We shouldn't need to remove otherwise.
6098 if (remove && !PURPLE_IS_GROUP(node))
6099 pidgin_blist_hide_node(list, node, FALSE);
6101 if (PURPLE_IS_BUDDY(node))
6102 pidgin_blist_update_buddy(list, node, rerender);
6103 else if (PURPLE_IS_CHAT(node))
6104 pidgin_blist_update(list, node);
6105 else if (PURPLE_IS_GROUP(node))
6106 pidgin_blist_update(list, node);
6107 node = purple_blist_node_next(node, FALSE);
6112 void pidgin_blist_refresh(PurpleBuddyList *list)
6114 redo_buddy_list(list, FALSE, TRUE);
6117 void
6118 pidgin_blist_update_refresh_timeout()
6120 PurpleBuddyList *blist;
6121 PidginBuddyList *gtkblist;
6123 blist = purple_blist_get_default();
6124 gtkblist = PIDGIN_BUDDY_LIST(blist);
6126 gtkblist->refresh_timer = g_timeout_add_seconds(30,(GSourceFunc)pidgin_blist_refresh_timer, blist);
6129 static gboolean get_iter_from_node(PurpleBlistNode *node, GtkTreeIter *iter) {
6130 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(node);
6131 GtkTreePath *path;
6133 if (!gtknode) {
6134 return FALSE;
6137 if (!gtkblist) {
6138 purple_debug_error("gtkblist", "get_iter_from_node was called, but we don't seem to have a blist\n");
6139 return FALSE;
6142 if (!gtknode->row)
6143 return FALSE;
6146 if ((path = gtk_tree_row_reference_get_path(gtknode->row)) == NULL)
6147 return FALSE;
6149 if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(gtkblist->treemodel), iter, path)) {
6150 gtk_tree_path_free(path);
6151 return FALSE;
6153 gtk_tree_path_free(path);
6154 return TRUE;
6157 static void pidgin_blist_remove(PurpleBuddyList *list, PurpleBlistNode *node)
6159 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(node);
6161 purple_request_close_with_handle(node);
6163 pidgin_blist_hide_node(list, node, TRUE);
6165 if(node->parent)
6166 pidgin_blist_update(list, node->parent);
6168 /* There's something I don't understand here - Ethan */
6169 /* Ethan said that back in 2003, but this g_free has been left commented
6170 * out ever since. I can't find any reason at all why this is bad and
6171 * valgrind found several reasons why it's good. If this causes problems
6172 * comment it out again. Stu */
6173 /* Of course it still causes problems - this breaks dragging buddies into
6174 * contacts, the dragged buddy mysteriously 'disappears'. Stu. */
6175 /* I think it's fixed now. Stu. */
6177 if(gtknode) {
6178 if(gtknode->recent_signonoff_timer > 0)
6179 g_source_remove(gtknode->recent_signonoff_timer);
6181 purple_signals_disconnect_by_handle(gtknode);
6182 g_free(gtknode);
6183 purple_blist_node_set_ui_data(node, NULL);
6187 static gboolean do_selection_changed(PurpleBlistNode *new_selection)
6189 PurpleBlistNode *old_selection = NULL;
6191 /* test for gtkblist because crazy timeout means we can be called after the blist is gone */
6192 if (gtkblist && new_selection != gtkblist->selected_node) {
6193 old_selection = gtkblist->selected_node;
6194 gtkblist->selected_node = new_selection;
6195 if(new_selection)
6196 pidgin_blist_update(NULL, new_selection);
6197 if(old_selection)
6198 pidgin_blist_update(NULL, old_selection);
6201 return FALSE;
6204 static void pidgin_blist_selection_changed(GtkTreeSelection *selection, gpointer data)
6206 PurpleBlistNode *new_selection = NULL;
6207 GtkTreeIter iter;
6209 if(gtk_tree_selection_get_selected(selection, NULL, &iter)){
6210 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter,
6211 NODE_COLUMN, &new_selection, -1);
6214 /* we set this up as a timeout, otherwise the blist flickers ...
6215 * but we don't do it for groups, because it causes total bizarness -
6216 * the previously selected buddy node might rendered at half height.
6218 if ((new_selection != NULL) && PURPLE_IS_GROUP(new_selection)) {
6219 do_selection_changed(new_selection);
6220 } else {
6221 g_timeout_add(0, (GSourceFunc)do_selection_changed, new_selection);
6225 static gboolean insert_node(PurpleBuddyList *list, PurpleBlistNode *node, GtkTreeIter *iter)
6227 GtkTreeIter parent_iter = {0, NULL, NULL, NULL}, cur, *curptr = NULL;
6228 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(node);
6229 GtkTreePath *newpath;
6231 if(!iter)
6232 return FALSE;
6234 /* XXX: it's not necessary, but let's silence a warning*/
6235 memset(&parent_iter, 0, sizeof(parent_iter));
6237 if(node->parent && !get_iter_from_node(node->parent, &parent_iter))
6238 return FALSE;
6240 if(get_iter_from_node(node, &cur))
6241 curptr = &cur;
6243 if(PURPLE_IS_CONTACT(node) || PURPLE_IS_CHAT(node)) {
6244 current_sort_method->func(node, list, parent_iter, curptr, iter);
6245 } else {
6246 sort_method_none(node, list, parent_iter, curptr, iter);
6249 if(gtknode != NULL) {
6250 gtk_tree_row_reference_free(gtknode->row);
6251 } else {
6252 pidgin_blist_new_node(list, node);
6253 gtknode = purple_blist_node_get_ui_data(node);
6256 newpath = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel),
6257 iter);
6258 gtknode->row =
6259 gtk_tree_row_reference_new(GTK_TREE_MODEL(gtkblist->treemodel),
6260 newpath);
6262 gtk_tree_path_free(newpath);
6264 if (!editing_blist)
6265 gtk_tree_store_set(gtkblist->treemodel, iter,
6266 NODE_COLUMN, node,
6267 -1);
6269 if(node->parent) {
6270 GtkTreePath *expand = NULL;
6271 PidginBlistNode *gtkparentnode = purple_blist_node_get_ui_data(node->parent);
6273 if(PURPLE_IS_GROUP(node->parent)) {
6274 if(!purple_blist_node_get_bool(node->parent, "collapsed"))
6275 expand = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel), &parent_iter);
6276 } else if(PURPLE_IS_CONTACT(node->parent) &&
6277 gtkparentnode->contact_expanded) {
6278 expand = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel), &parent_iter);
6280 if(expand) {
6281 gtk_tree_view_expand_row(GTK_TREE_VIEW(gtkblist->treeview), expand, FALSE);
6282 gtk_tree_path_free(expand);
6286 return TRUE;
6289 static gboolean pidgin_blist_group_has_show_offline_buddy(PurpleGroup *group)
6291 PurpleBlistNode *gnode, *cnode, *bnode;
6293 gnode = PURPLE_BLIST_NODE(group);
6294 for(cnode = gnode->child; cnode; cnode = cnode->next) {
6295 if(PURPLE_IS_CONTACT(cnode)) {
6296 for(bnode = cnode->child; bnode; bnode = bnode->next) {
6297 PurpleBuddy *buddy = (PurpleBuddy *)bnode;
6298 if (purple_account_is_connected(purple_buddy_get_account(buddy)) &&
6299 purple_blist_node_get_bool(bnode, "show_offline"))
6300 return TRUE;
6304 return FALSE;
6307 /* This version of pidgin_blist_update_group can take the original buddy or a
6308 * group, but has much better algorithmic performance with a pre-known buddy.
6310 static void pidgin_blist_update_group(PurpleBuddyList *list,
6311 PurpleBlistNode *node)
6313 gint count;
6314 PurpleGroup *group;
6315 PurpleBlistNode* gnode;
6316 gboolean show = FALSE, show_offline = FALSE;
6318 g_return_if_fail(node != NULL);
6320 if (editing_blist)
6321 return;
6323 if (PURPLE_IS_GROUP(node))
6324 gnode = node;
6325 else if (PURPLE_IS_BUDDY(node))
6326 gnode = node->parent->parent;
6327 else if (PURPLE_IS_CONTACT(node) || PURPLE_IS_CHAT(node))
6328 gnode = node->parent;
6329 else
6330 return;
6332 group = (PurpleGroup*)gnode;
6334 show_offline = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_offline_buddies");
6336 if(show_offline)
6337 count = purple_counting_node_get_current_size(PURPLE_COUNTING_NODE(group));
6338 else
6339 count = purple_counting_node_get_online_count(PURPLE_COUNTING_NODE(group));
6341 if (count > 0 || purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_empty_groups"))
6342 show = TRUE;
6343 else if (PURPLE_IS_BUDDY(node) && buddy_is_displayable((PurpleBuddy*)node)) { /* Or chat? */
6344 show = TRUE;
6345 } else if (!show_offline) {
6346 show = pidgin_blist_group_has_show_offline_buddy(group);
6349 if (show) {
6350 gchar *title;
6351 gboolean biglist;
6352 GtkTreeIter iter;
6353 GtkTreePath *path;
6354 gboolean expanded;
6355 GdkRGBA *bgcolor = NULL;
6356 GdkPixbuf *avatar = NULL;
6357 PidginBlistTheme *theme = NULL;
6359 if(!insert_node(list, gnode, &iter))
6360 return;
6362 if ((theme = pidgin_blist_get_theme()) == NULL)
6363 bgcolor = NULL;
6364 else if (purple_blist_node_get_bool(gnode, "collapsed") || count <= 0)
6365 bgcolor = pidgin_blist_theme_get_collapsed_background_color(theme);
6366 else
6367 bgcolor = pidgin_blist_theme_get_expanded_background_color(theme);
6369 path = gtk_tree_model_get_path(GTK_TREE_MODEL(gtkblist->treemodel), &iter);
6370 expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(gtkblist->treeview), path);
6371 gtk_tree_path_free(path);
6373 title = pidgin_get_group_title(gnode, expanded);
6374 biglist = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons");
6376 if (biglist) {
6377 avatar = pidgin_blist_get_buddy_icon(gnode, TRUE, TRUE);
6380 gtk_tree_store_set(gtkblist->treemodel, &iter,
6381 STATUS_ICON_VISIBLE_COLUMN, FALSE,
6382 STATUS_ICON_COLUMN, NULL,
6383 NAME_COLUMN, title,
6384 NODE_COLUMN, gnode,
6385 BGCOLOR_COLUMN, bgcolor,
6386 GROUP_EXPANDER_COLUMN, TRUE,
6387 GROUP_EXPANDER_VISIBLE_COLUMN, TRUE,
6388 CONTACT_EXPANDER_VISIBLE_COLUMN, FALSE,
6389 BUDDY_ICON_COLUMN, avatar,
6390 BUDDY_ICON_VISIBLE_COLUMN, biglist,
6391 IDLE_VISIBLE_COLUMN, FALSE,
6392 EMBLEM_VISIBLE_COLUMN, FALSE,
6393 -1);
6394 g_free(title);
6395 } else {
6396 pidgin_blist_hide_node(list, gnode, TRUE);
6400 static char *pidgin_get_group_title(PurpleBlistNode *gnode, gboolean expanded)
6402 PurpleGroup *group;
6403 gboolean selected;
6404 char group_count[12] = "";
6405 char *mark, *esc;
6406 PurpleBlistNode *selected_node = NULL;
6407 GtkTreeIter iter;
6408 PidginThemeFont *pair;
6409 gchar const *text_color, *text_font;
6410 PidginBlistTheme *theme;
6412 group = (PurpleGroup*)gnode;
6414 if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(gtkblist->treeview)), NULL, &iter)) {
6415 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &iter,
6416 NODE_COLUMN, &selected_node, -1);
6418 selected = (gnode == selected_node);
6420 if (!expanded) {
6421 g_snprintf(group_count, sizeof(group_count), "%d/%d",
6422 purple_counting_node_get_online_count(PURPLE_COUNTING_NODE(group)),
6423 purple_counting_node_get_current_size(PURPLE_COUNTING_NODE(group)));
6426 theme = pidgin_blist_get_theme();
6427 if (theme == NULL)
6428 pair = NULL;
6429 else if (expanded)
6430 pair = pidgin_blist_theme_get_expanded_text_info(theme);
6431 else
6432 pair = pidgin_blist_theme_get_collapsed_text_info(theme);
6435 text_color = selected ? NULL : theme_font_get_color_default(pair, NULL);
6436 text_font = theme_font_get_face_default(pair, "");
6438 esc = g_markup_escape_text(purple_group_get_name(group), -1);
6439 if (text_color) {
6440 mark = g_strdup_printf("<span foreground='%s' font_desc='%s'><b>%s</b>%s%s%s</span>",
6441 text_color, text_font,
6442 esc ? esc : "",
6443 !expanded ? " <span weight='light'>(</span>" : "",
6444 group_count,
6445 !expanded ? "<span weight='light'>)</span>" : "");
6446 } else {
6447 mark = g_strdup_printf("<span font_desc='%s'><b>%s</b>%s%s%s</span>",
6448 text_font, esc ? esc : "",
6449 !expanded ? " <span weight='light'>(</span>" : "",
6450 group_count,
6451 !expanded ? "<span weight='light'>)</span>" : "");
6454 g_free(esc);
6455 return mark;
6458 static void buddy_node(PurpleBuddy *buddy, GtkTreeIter *iter, PurpleBlistNode *node)
6460 PurplePresence *presence = purple_buddy_get_presence(buddy);
6461 GdkPixbuf *status, *avatar, *emblem, *protocol_icon;
6462 GdkRGBA *color = NULL;
6463 char *mark;
6464 char *idle = NULL;
6465 gboolean expanded = ((PidginBlistNode *)purple_blist_node_get_ui_data(node->parent))->contact_expanded;
6466 gboolean selected = (gtkblist->selected_node == node);
6467 gboolean biglist = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons");
6468 PidginBlistTheme *theme;
6470 if (editing_blist)
6471 return;
6473 status = pidgin_blist_get_status_icon(PURPLE_BLIST_NODE(buddy),
6474 biglist ? PIDGIN_STATUS_ICON_LARGE : PIDGIN_STATUS_ICON_SMALL);
6476 /* Speed it up if we don't want buddy icons. */
6477 if(biglist)
6478 avatar = pidgin_blist_get_buddy_icon(PURPLE_BLIST_NODE(buddy), TRUE, TRUE);
6479 else
6480 avatar = NULL;
6482 if (!avatar) {
6483 g_object_ref(G_OBJECT(gtkblist->empty_avatar));
6484 avatar = gtkblist->empty_avatar;
6485 } else if ((!PURPLE_BUDDY_IS_ONLINE(buddy) || purple_presence_is_idle(presence))) {
6486 do_alphashift(avatar, 77);
6489 emblem = pidgin_blist_get_emblem(PURPLE_BLIST_NODE(buddy));
6490 mark = pidgin_blist_get_name_markup(buddy, selected, TRUE);
6492 theme = pidgin_blist_get_theme();
6494 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_idle_time") &&
6495 purple_presence_is_idle(presence) && !biglist)
6497 time_t idle_secs = purple_presence_get_idle_time(presence);
6499 if (idle_secs > 0)
6501 PidginThemeFont *pair = NULL;
6502 const gchar *textcolor;
6503 time_t t;
6504 int ihrs, imin;
6505 time(&t);
6507 ihrs = (t - idle_secs) / 3600;
6508 imin = ((t - idle_secs) / 60) % 60;
6510 if (selected)
6511 textcolor = NULL;
6512 else if (theme != NULL && (pair = pidgin_blist_theme_get_idle_text_info(theme)) != NULL)
6513 textcolor = pidgin_theme_font_get_color_describe(pair);
6514 else
6515 /* If no theme them default to making idle buddy names grey */
6516 textcolor = pidgin_style_is_dark(NULL) ? "light slate grey" : "dim grey";
6518 if (textcolor) {
6519 idle = g_strdup_printf("<span color='%s' font_desc='%s'>%d:%02d</span>",
6520 textcolor, theme_font_get_face_default(pair, ""),
6521 ihrs, imin);
6522 } else {
6523 idle = g_strdup_printf("<span font_desc='%s'>%d:%02d</span>",
6524 theme_font_get_face_default(pair, ""),
6525 ihrs, imin);
6530 protocol_icon = pidgin_create_protocol_icon(purple_buddy_get_account(buddy), PIDGIN_PROTOCOL_ICON_SMALL);
6532 if (theme != NULL)
6533 color = pidgin_blist_theme_get_contact_color(theme);
6535 gtk_tree_store_set(gtkblist->treemodel, iter,
6536 STATUS_ICON_COLUMN, status,
6537 STATUS_ICON_VISIBLE_COLUMN, TRUE,
6538 NAME_COLUMN, mark,
6539 IDLE_COLUMN, idle,
6540 IDLE_VISIBLE_COLUMN, !biglist && idle,
6541 BUDDY_ICON_COLUMN, avatar,
6542 BUDDY_ICON_VISIBLE_COLUMN, biglist,
6543 EMBLEM_COLUMN, emblem,
6544 EMBLEM_VISIBLE_COLUMN, (emblem != NULL),
6545 PROTOCOL_ICON_COLUMN, protocol_icon,
6546 PROTOCOL_ICON_VISIBLE_COLUMN, purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_protocol_icons"),
6547 BGCOLOR_COLUMN, color,
6548 CONTACT_EXPANDER_COLUMN, NULL,
6549 CONTACT_EXPANDER_VISIBLE_COLUMN, expanded,
6550 GROUP_EXPANDER_VISIBLE_COLUMN, FALSE,
6551 -1);
6553 g_free(mark);
6554 g_free(idle);
6555 if(emblem)
6556 g_object_unref(emblem);
6557 if(status)
6558 g_object_unref(status);
6559 if(avatar)
6560 g_object_unref(avatar);
6561 if(protocol_icon)
6562 g_object_unref(protocol_icon);
6565 /* This is a variation on the original gtk_blist_update_contact. Here we
6566 can know in advance which buddy has changed so we can just update that */
6567 static void pidgin_blist_update_contact(PurpleBuddyList *list, PurpleBlistNode *node)
6569 PurpleBlistNode *cnode;
6570 PurpleContact *contact;
6571 PurpleBuddy *buddy;
6572 gboolean biglist = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons");
6573 PidginBlistNode *gtknode;
6575 if (editing_blist)
6576 return;
6578 if (PURPLE_IS_BUDDY(node))
6579 cnode = node->parent;
6580 else
6581 cnode = node;
6583 g_return_if_fail(PURPLE_IS_CONTACT(cnode));
6585 /* First things first, update the group */
6586 if (PURPLE_IS_BUDDY(node))
6587 pidgin_blist_update_group(list, node);
6588 else
6589 pidgin_blist_update_group(list, cnode->parent);
6591 contact = (PurpleContact*)cnode;
6592 buddy = purple_contact_get_priority_buddy(contact);
6594 if (buddy_is_displayable(buddy))
6596 GtkTreeIter iter;
6598 if(!insert_node(list, cnode, &iter))
6599 return;
6601 gtknode = purple_blist_node_get_ui_data(cnode);
6603 if(gtknode->contact_expanded) {
6604 GdkPixbuf *status;
6605 gchar *mark, *tmp;
6606 const gchar *fg_color, *font;
6607 GdkRGBA *color = NULL;
6608 PidginBlistTheme *theme;
6609 PidginThemeFont *pair;
6610 gboolean selected = (gtkblist->selected_node == cnode);
6612 mark = g_markup_escape_text(purple_contact_get_alias(contact), -1);
6614 theme = pidgin_blist_get_theme();
6615 if (theme == NULL)
6616 pair = NULL;
6617 else {
6618 pair = pidgin_blist_theme_get_contact_text_info(theme);
6619 color = pidgin_blist_theme_get_contact_color(theme);
6622 font = theme_font_get_face_default(pair, "");
6623 fg_color = selected ? NULL : theme_font_get_color_default(pair, NULL);
6625 if (fg_color) {
6626 tmp = g_strdup_printf("<span font_desc='%s' color='%s'>%s</span>",
6627 font, fg_color, mark);
6628 } else {
6629 tmp = g_strdup_printf("<span font_desc='%s'>%s</span>", font,
6630 mark);
6632 g_free(mark);
6633 mark = tmp;
6635 status = pidgin_blist_get_status_icon(cnode,
6636 biglist? PIDGIN_STATUS_ICON_LARGE : PIDGIN_STATUS_ICON_SMALL);
6638 gtk_tree_store_set(gtkblist->treemodel, &iter,
6639 STATUS_ICON_COLUMN, status,
6640 STATUS_ICON_VISIBLE_COLUMN, TRUE,
6641 NAME_COLUMN, mark,
6642 IDLE_COLUMN, NULL,
6643 IDLE_VISIBLE_COLUMN, FALSE,
6644 BGCOLOR_COLUMN, color,
6645 BUDDY_ICON_COLUMN, NULL,
6646 CONTACT_EXPANDER_COLUMN, TRUE,
6647 CONTACT_EXPANDER_VISIBLE_COLUMN, TRUE,
6648 GROUP_EXPANDER_VISIBLE_COLUMN, FALSE,
6649 -1);
6650 g_free(mark);
6651 if(status)
6652 g_object_unref(status);
6653 } else {
6654 buddy_node(buddy, &iter, cnode);
6656 } else {
6657 pidgin_blist_hide_node(list, cnode, TRUE);
6663 static void pidgin_blist_update_buddy(PurpleBuddyList *list, PurpleBlistNode *node, gboolean status_change)
6665 PurpleBuddy *buddy;
6666 PidginBlistNode *gtkparentnode;
6668 g_return_if_fail(PURPLE_IS_BUDDY(node));
6670 if (node->parent == NULL)
6671 return;
6673 buddy = (PurpleBuddy*)node;
6675 /* First things first, update the contact */
6676 pidgin_blist_update_contact(list, node);
6678 gtkparentnode = purple_blist_node_get_ui_data(node->parent);
6680 if (gtkparentnode->contact_expanded && buddy_is_displayable(buddy))
6682 GtkTreeIter iter;
6684 if (!insert_node(list, node, &iter))
6685 return;
6687 buddy_node(buddy, &iter, node);
6689 } else {
6690 pidgin_blist_hide_node(list, node, TRUE);
6695 static void pidgin_blist_update_chat(PurpleBuddyList *list, PurpleBlistNode *node)
6697 PurpleChat *chat;
6699 g_return_if_fail(PURPLE_IS_CHAT(node));
6701 if (editing_blist)
6702 return;
6704 /* First things first, update the group */
6705 pidgin_blist_update_group(list, node->parent);
6707 chat = (PurpleChat*)node;
6709 if(purple_account_is_connected(purple_chat_get_account(chat))) {
6710 GtkTreeIter iter;
6711 GdkPixbuf *status, *avatar, *emblem, *protocol_icon;
6712 const gchar *color, *font;
6713 gchar *mark, *tmp;
6714 gboolean showicons = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons");
6715 gboolean biglist = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons");
6716 PidginBlistNode *ui;
6717 PurpleConversation *conv;
6718 gboolean hidden = FALSE;
6719 GdkRGBA *bgcolor = NULL;
6720 PidginThemeFont *pair;
6721 PidginBlistTheme *theme;
6722 gboolean selected = (gtkblist->selected_node == node);
6723 gboolean nick_said = FALSE;
6725 if (!insert_node(list, node, &iter))
6726 return;
6728 ui = purple_blist_node_get_ui_data(node);
6729 conv = ui->conv.conv;
6730 if (conv && pidgin_conv_is_hidden(PIDGIN_CONVERSATION(conv))) {
6731 hidden = (ui->conv.flags & PIDGIN_BLIST_NODE_HAS_PENDING_MESSAGE);
6732 nick_said = (ui->conv.flags & PIDGIN_BLIST_CHAT_HAS_PENDING_MESSAGE_WITH_NICK);
6735 status = pidgin_blist_get_status_icon(node,
6736 biglist ? PIDGIN_STATUS_ICON_LARGE : PIDGIN_STATUS_ICON_SMALL);
6737 emblem = pidgin_blist_get_emblem(node);
6739 /* Speed it up if we don't want buddy icons. */
6740 if(showicons)
6741 avatar = pidgin_blist_get_buddy_icon(node, TRUE, FALSE);
6742 else
6743 avatar = NULL;
6745 mark = g_markup_escape_text(purple_chat_get_name(chat), -1);
6747 theme = pidgin_blist_get_theme();
6749 if (theme == NULL)
6750 pair = NULL;
6751 else if (nick_said)
6752 pair = pidgin_blist_theme_get_unread_message_nick_said_text_info(theme);
6753 else if (hidden)
6754 pair = pidgin_blist_theme_get_unread_message_text_info(theme);
6755 else pair = pidgin_blist_theme_get_online_text_info(theme);
6758 font = theme_font_get_face_default(pair, "");
6759 if (selected || !(color = theme_font_get_color_default(pair, NULL)))
6760 /* nick_said color is the same as gtkconv:tab-label-attention */
6761 color = (nick_said ? "#006aff" : NULL);
6763 if (color) {
6764 tmp = g_strdup_printf("<span font_desc='%s' color='%s' weight='%s'>%s</span>",
6765 font, color, hidden ? "bold" : "normal", mark);
6766 } else {
6767 tmp = g_strdup_printf("<span font_desc='%s' weight='%s'>%s</span>",
6768 font, hidden ? "bold" : "normal", mark);
6770 g_free(mark);
6771 mark = tmp;
6773 protocol_icon = pidgin_create_protocol_icon(purple_chat_get_account(chat), PIDGIN_PROTOCOL_ICON_SMALL);
6775 if (theme != NULL)
6776 bgcolor = pidgin_blist_theme_get_contact_color(theme);
6778 gtk_tree_store_set(gtkblist->treemodel, &iter,
6779 STATUS_ICON_COLUMN, status,
6780 STATUS_ICON_VISIBLE_COLUMN, TRUE,
6781 BUDDY_ICON_COLUMN, avatar ? avatar : gtkblist->empty_avatar,
6782 BUDDY_ICON_VISIBLE_COLUMN, showicons,
6783 EMBLEM_COLUMN, emblem,
6784 EMBLEM_VISIBLE_COLUMN, emblem != NULL,
6785 PROTOCOL_ICON_COLUMN, protocol_icon,
6786 PROTOCOL_ICON_VISIBLE_COLUMN, purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/show_protocol_icons"),
6787 NAME_COLUMN, mark,
6788 BGCOLOR_COLUMN, bgcolor,
6789 GROUP_EXPANDER_VISIBLE_COLUMN, FALSE,
6790 -1);
6792 g_free(mark);
6793 if(emblem)
6794 g_object_unref(emblem);
6795 if(status)
6796 g_object_unref(status);
6797 if(avatar)
6798 g_object_unref(avatar);
6799 if(protocol_icon)
6800 g_object_unref(protocol_icon);
6802 } else {
6803 pidgin_blist_hide_node(list, node, TRUE);
6807 static void pidgin_blist_update(PurpleBuddyList *list, PurpleBlistNode *node)
6809 if (list)
6810 gtkblist = PIDGIN_BUDDY_LIST(list);
6811 if(!gtkblist || !gtkblist->treeview || !node)
6812 return;
6814 if (purple_blist_node_get_ui_data(node) == NULL)
6815 pidgin_blist_new_node(list, node);
6817 if (PURPLE_IS_GROUP(node))
6818 pidgin_blist_update_group(list, node);
6819 else if (PURPLE_IS_CONTACT(node))
6820 pidgin_blist_update_contact(list, node);
6821 else if (PURPLE_IS_BUDDY(node))
6822 pidgin_blist_update_buddy(list, node, TRUE);
6823 else if (PURPLE_IS_CHAT(node))
6824 pidgin_blist_update_chat(list, node);
6827 static void pidgin_blist_set_visible(PurpleBuddyList *list, gboolean show)
6829 if (!(gtkblist && gtkblist->window))
6830 return;
6832 if (show) {
6833 gtk_widget_show(gtkblist->window);
6834 } else {
6835 if(visibility_manager_count) {
6836 gtk_widget_hide(gtkblist->window);
6837 } else {
6838 if (!gtk_widget_get_visible(gtkblist->window))
6839 gtk_widget_show(gtkblist->window);
6840 gtk_window_iconify(GTK_WINDOW(gtkblist->window));
6845 static GList *
6846 groups_tree(void)
6848 static GList *list = NULL;
6849 PurpleGroup *g;
6850 PurpleBlistNode *gnode;
6852 g_list_free(list);
6853 list = NULL;
6855 gnode = purple_blist_get_default_root();
6856 if (gnode == NULL) {
6857 list = g_list_append(list,
6858 (gpointer)PURPLE_BLIST_DEFAULT_GROUP_NAME);
6859 } else {
6860 for (; gnode != NULL; gnode = gnode->next) {
6861 if (PURPLE_IS_GROUP(gnode))
6863 g = (PurpleGroup *)gnode;
6864 list = g_list_append(list, (char *) purple_group_get_name(g));
6869 return list;
6872 static void
6873 add_buddy_select_account_cb(GObject *w, PidginAddBuddyData *data)
6875 PurpleAccount *account =
6876 pidgin_account_chooser_get_selected(GTK_WIDGET(w));
6877 PurpleConnection *pc = NULL;
6878 PurpleProtocol *protocol = NULL;
6879 gboolean invite_enabled = TRUE;
6881 /* Save our account */
6882 data->rq_data.account = account;
6884 if (account)
6885 pc = purple_account_get_connection(account);
6886 if (pc)
6887 protocol = purple_connection_get_protocol(pc);
6888 if (protocol && !(purple_protocol_get_options(protocol) & OPT_PROTO_INVITE_MESSAGE))
6889 invite_enabled = FALSE;
6891 gtk_widget_set_sensitive(data->entry_for_invite, invite_enabled);
6892 set_sensitive_if_input_buddy_cb(data->entry, data);
6895 static void
6896 destroy_add_buddy_dialog_cb(GtkWidget *win, PidginAddBuddyData *data)
6898 g_free(data);
6901 static void
6902 add_buddy_cb(GtkWidget *w, int resp, PidginAddBuddyData *data)
6904 const char *grp, *who, *whoalias, *invite;
6905 PurpleAccount *account;
6906 PurpleGroup *g;
6907 PurpleBuddy *b;
6908 PurpleIMConversation *im;
6909 PurpleBuddyIcon *icon;
6911 if (resp == GTK_RESPONSE_OK)
6913 who = gtk_entry_get_text(GTK_ENTRY(data->entry));
6914 grp = pidgin_text_combo_box_entry_get_text(data->combo);
6915 whoalias = gtk_entry_get_text(GTK_ENTRY(data->entry_for_alias));
6916 if (*whoalias == '\0')
6917 whoalias = NULL;
6918 invite = gtk_entry_get_text(GTK_ENTRY(data->entry_for_invite));
6919 if (*invite == '\0')
6920 invite = NULL;
6922 account = data->rq_data.account;
6924 g = NULL;
6925 if ((grp != NULL) && (*grp != '\0'))
6927 if ((g = purple_blist_find_group(grp)) == NULL)
6929 g = purple_group_new(grp);
6930 purple_blist_add_group(g, NULL);
6933 b = purple_blist_find_buddy_in_group(account, who, g);
6935 else if ((b = purple_blist_find_buddy(account, who)) != NULL)
6937 g = purple_buddy_get_group(b);
6940 if (b == NULL)
6942 b = purple_buddy_new(account, who, whoalias);
6943 purple_blist_add_buddy(b, NULL, g, NULL);
6946 purple_account_add_buddy(account, b, invite);
6948 /* Offer to merge people with the same alias. */
6949 if (whoalias != NULL && g != NULL)
6950 gtk_blist_auto_personize(PURPLE_BLIST_NODE(g), whoalias);
6953 * XXX
6954 * It really seems like it would be better if the call to
6955 * purple_account_add_buddy() and purple_conversation_update() were done in
6956 * blist.c, possibly in the purple_blist_add_buddy() function. Maybe
6957 * purple_account_add_buddy() should be renamed to
6958 * purple_blist_add_new_buddy() or something, and have it call
6959 * purple_blist_add_buddy() after it creates it. --Mark
6961 * No that's not good. blist.c should only deal with adding nodes to the
6962 * local list. We need a new, non-gtk file that calls both
6963 * purple_account_add_buddy and purple_blist_add_buddy().
6964 * Or something. --Mark
6967 im = purple_conversations_find_im_with_account(who, data->rq_data.account);
6968 if (im != NULL) {
6969 icon = purple_im_conversation_get_icon(im);
6970 if (icon != NULL)
6971 purple_buddy_icon_update(icon);
6975 gtk_widget_destroy(data->rq_data.window);
6978 static void
6979 pidgin_blist_request_add_buddy(PurpleBuddyList *list, PurpleAccount *account,
6980 const char *username, const char *group,
6981 const char *alias)
6983 PidginAddBuddyData *data = g_new0(PidginAddBuddyData, 1);
6984 PidginBlistRequestData *blist_req_data = &data->rq_data;
6986 if (account == NULL)
6987 account = purple_connection_get_account(purple_connections_get_all()->data);
6989 make_blist_request_dialog(blist_req_data,
6990 account,
6991 _("Add Buddy"), "add_buddy",
6992 _("Add a buddy.\n"),
6993 G_CALLBACK(add_buddy_select_account_cb), add_buddy_account_filter_func,
6994 G_CALLBACK(add_buddy_cb));
6995 gtk_dialog_add_buttons(GTK_DIALOG(data->rq_data.window),
6996 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
6997 GTK_STOCK_ADD, GTK_RESPONSE_OK,
6998 NULL);
6999 gtk_dialog_set_default_response(GTK_DIALOG(data->rq_data.window),
7000 GTK_RESPONSE_OK);
7002 g_signal_connect(G_OBJECT(data->rq_data.window), "destroy",
7003 G_CALLBACK(destroy_add_buddy_dialog_cb), data);
7005 data->entry = gtk_entry_new();
7007 pidgin_add_widget_to_vbox(data->rq_data.vbox, _("Buddy's _username:"),
7008 data->rq_data.sg, data->entry, TRUE, NULL);
7009 gtk_widget_grab_focus(data->entry);
7011 if (username != NULL)
7012 gtk_entry_set_text(GTK_ENTRY(data->entry), username);
7013 else
7014 gtk_dialog_set_response_sensitive(GTK_DIALOG(data->rq_data.window),
7015 GTK_RESPONSE_OK, FALSE);
7017 gtk_entry_set_activates_default (GTK_ENTRY(data->entry), TRUE);
7019 g_signal_connect(G_OBJECT(data->entry), "changed",
7020 G_CALLBACK(set_sensitive_if_input_buddy_cb),
7021 data);
7023 data->entry_for_alias = gtk_entry_new();
7024 pidgin_add_widget_to_vbox(data->rq_data.vbox, _("(Optional) A_lias:"),
7025 data->rq_data.sg, data->entry_for_alias, TRUE,
7026 NULL);
7028 if (alias != NULL)
7029 gtk_entry_set_text(GTK_ENTRY(data->entry_for_alias), alias);
7031 if (username != NULL)
7032 gtk_widget_grab_focus(GTK_WIDGET(data->entry_for_alias));
7034 data->entry_for_invite = gtk_entry_new();
7035 pidgin_add_widget_to_vbox(data->rq_data.vbox, _("(Optional) _Invite message:"),
7036 data->rq_data.sg, data->entry_for_invite, TRUE,
7037 NULL);
7039 data->combo = pidgin_text_combo_box_entry_new(group, groups_tree());
7040 pidgin_add_widget_to_vbox(data->rq_data.vbox, _("Add buddy to _group:"),
7041 data->rq_data.sg, data->combo, TRUE, NULL);
7043 gtk_widget_show_all(data->rq_data.window);
7045 /* Force update of invite message entry sensitivity */
7046 pidgin_account_chooser_set_selected(blist_req_data->account_menu,
7047 account);
7050 static void
7051 add_chat_cb(GtkWidget *w, PidginAddChatData *data)
7053 GList *tmp;
7054 PurpleChat *chat;
7055 GHashTable *components;
7057 components = g_hash_table_new_full(g_str_hash, g_str_equal,
7058 g_free, g_free);
7060 for (tmp = data->chat_data.entries; tmp; tmp = tmp->next)
7062 if (g_object_get_data(tmp->data, "is_spin"))
7064 g_hash_table_replace(components,
7065 g_strdup(g_object_get_data(tmp->data, "identifier")),
7066 g_strdup_printf("%d",
7067 gtk_spin_button_get_value_as_int(tmp->data)));
7069 else
7071 const char *value = gtk_entry_get_text(tmp->data);
7073 if (*value != '\0')
7074 g_hash_table_replace(components,
7075 g_strdup(g_object_get_data(tmp->data, "identifier")),
7076 g_strdup(value));
7080 chat = purple_chat_new(data->chat_data.rq_data.account,
7081 gtk_entry_get_text(GTK_ENTRY(data->alias_entry)),
7082 components);
7084 if (chat != NULL) {
7085 PurpleGroup *group;
7086 const char *group_name;
7088 group_name = pidgin_text_combo_box_entry_get_text(data->group_combo);
7090 group = NULL;
7091 if ((group_name != NULL) && (*group_name != '\0') &&
7092 ((group = purple_blist_find_group(group_name)) == NULL))
7094 group = purple_group_new(group_name);
7095 purple_blist_add_group(group, NULL);
7098 purple_blist_add_chat(chat, group, NULL);
7100 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->autojoin)))
7101 purple_blist_node_set_bool(PURPLE_BLIST_NODE(chat), "gtk-autojoin", TRUE);
7103 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->persistent)))
7104 purple_blist_node_set_bool(PURPLE_BLIST_NODE(chat), "gtk-persistent", TRUE);
7107 gtk_widget_destroy(data->chat_data.rq_data.window);
7108 g_free(data->chat_data.default_chat_name);
7109 g_list_free(data->chat_data.entries);
7110 g_free(data);
7113 static void
7114 add_chat_resp_cb(GtkWidget *w, int resp, PidginAddChatData *data)
7116 if (resp == GTK_RESPONSE_OK)
7118 add_chat_cb(NULL, data);
7120 else if (resp == 1)
7122 pidgin_roomlist_dialog_show_with_account(data->chat_data.rq_data.account);
7124 else
7126 gtk_widget_destroy(data->chat_data.rq_data.window);
7127 g_free(data->chat_data.default_chat_name);
7128 g_list_free(data->chat_data.entries);
7129 g_free(data);
7133 static void
7134 pidgin_blist_request_add_chat(PurpleBuddyList *list, PurpleAccount *account,
7135 PurpleGroup *group, const char *alias,
7136 const char *name)
7138 PidginAddChatData *data;
7139 GList *l;
7140 PurpleConnection *gc;
7141 GtkBox *vbox;
7142 PurpleProtocol *protocol = NULL;
7144 if (account != NULL) {
7145 gc = purple_account_get_connection(account);
7146 protocol = purple_connection_get_protocol(gc);
7148 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol, CHAT, join)) {
7149 purple_notify_error(gc, NULL, _("This protocol does not"
7150 " support chat rooms."), NULL,
7151 purple_request_cpar_from_account(account));
7152 return;
7154 } else {
7155 /* Find an account with chat capabilities */
7156 for (l = purple_connections_get_all(); l != NULL; l = l->next) {
7157 gc = (PurpleConnection *)l->data;
7158 protocol = purple_connection_get_protocol(gc);
7160 if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, CHAT, join)) {
7161 account = purple_connection_get_account(gc);
7162 break;
7166 if (account == NULL) {
7167 purple_notify_error(NULL, NULL,
7168 _("You are not currently signed on with any "
7169 "protocols that have the ability to chat."), NULL, NULL);
7170 return;
7174 data = g_new0(PidginAddChatData, 1);
7175 vbox = GTK_BOX(make_blist_request_dialog((PidginBlistRequestData *)data, account,
7176 _("Add Chat"), "add_chat",
7177 _("Please enter an alias, and the appropriate information "
7178 "about the chat you would like to add to your buddy list.\n"),
7179 G_CALLBACK(chat_select_account_cb), chat_account_filter_func,
7180 G_CALLBACK(add_chat_resp_cb)));
7181 gtk_dialog_add_buttons(GTK_DIALOG(data->chat_data.rq_data.window),
7182 _("Room List"), 1,
7183 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
7184 GTK_STOCK_ADD, GTK_RESPONSE_OK,
7185 NULL);
7186 gtk_dialog_set_default_response(GTK_DIALOG(data->chat_data.rq_data.window),
7187 GTK_RESPONSE_OK);
7189 data->chat_data.default_chat_name = g_strdup(name);
7191 rebuild_chat_entries((PidginChatData *)data, name);
7193 data->alias_entry = gtk_entry_new();
7194 if (alias != NULL)
7195 gtk_entry_set_text(GTK_ENTRY(data->alias_entry), alias);
7196 gtk_entry_set_activates_default(GTK_ENTRY(data->alias_entry), TRUE);
7198 pidgin_add_widget_to_vbox(GTK_BOX(vbox), _("A_lias:"),
7199 data->chat_data.rq_data.sg, data->alias_entry,
7200 TRUE, NULL);
7201 if (name != NULL)
7202 gtk_widget_grab_focus(data->alias_entry);
7204 data->group_combo = pidgin_text_combo_box_entry_new(group ? purple_group_get_name(group) : NULL, groups_tree());
7205 pidgin_add_widget_to_vbox(GTK_BOX(vbox), _("_Group:"),
7206 data->chat_data.rq_data.sg, data->group_combo,
7207 TRUE, NULL);
7209 data->autojoin = gtk_check_button_new_with_mnemonic(_("Automatically _join when account connects"));
7210 data->persistent = gtk_check_button_new_with_mnemonic(_("_Remain in chat after window is closed"));
7211 gtk_box_pack_start(GTK_BOX(vbox), data->autojoin, FALSE, FALSE, 0);
7212 gtk_box_pack_start(GTK_BOX(vbox), data->persistent, FALSE, FALSE, 0);
7214 gtk_widget_show_all(data->chat_data.rq_data.window);
7217 static void
7218 add_group_cb(PurpleConnection *gc, const char *group_name)
7220 PurpleGroup *group;
7222 if ((group_name == NULL) || (*group_name == '\0'))
7223 return;
7225 group = purple_group_new(group_name);
7226 purple_blist_add_group(group, NULL);
7229 static void
7230 pidgin_blist_request_add_group(PurpleBuddyList *list)
7232 purple_request_input(NULL, _("Add Group"), NULL,
7233 _("Please enter the name of the group to be added."),
7234 NULL, FALSE, FALSE, NULL,
7235 _("Add"), G_CALLBACK(add_group_cb),
7236 _("Cancel"), NULL,
7237 NULL, NULL);
7240 void
7241 pidgin_blist_toggle_visibility()
7243 if (gtkblist && gtkblist->window) {
7244 if (gtk_widget_get_visible(gtkblist->window)) {
7245 /* make the buddy list visible if it is iconified or if it is
7246 * obscured and not currently focused (the focus part ensures
7247 * that we do something reasonable if the buddy list is obscured
7248 * by a window set to always be on top), otherwise hide the
7249 * buddy list
7251 purple_blist_set_visible(PIDGIN_WINDOW_ICONIFIED(gtkblist->window) ||
7252 ((gtk_blist_visibility != GDK_VISIBILITY_UNOBSCURED) &&
7253 !gtk_blist_focused));
7254 } else {
7255 purple_blist_set_visible(TRUE);
7260 void
7261 pidgin_blist_visibility_manager_add()
7263 visibility_manager_count++;
7264 purple_debug_info("gtkblist", "added visibility manager: %d\n", visibility_manager_count);
7267 void
7268 pidgin_blist_visibility_manager_remove()
7270 if (visibility_manager_count)
7271 visibility_manager_count--;
7272 if (!visibility_manager_count)
7273 purple_blist_set_visible(TRUE);
7274 purple_debug_info("gtkblist", "removed visibility manager: %d\n", visibility_manager_count);
7277 void pidgin_blist_add_alert(GtkWidget *widget)
7279 gtk_container_add(GTK_CONTAINER(gtkblist->scrollbook), widget);
7280 set_urgent();
7283 void
7284 pidgin_blist_set_headline(const char *text, const gchar *icon_name,
7285 GCallback callback, gpointer user_data, GDestroyNotify destroy)
7287 /* Destroy any existing headline first */
7288 if (gtkblist->headline_destroy)
7289 gtkblist->headline_destroy(gtkblist->headline_data);
7291 gtk_label_set_markup(GTK_LABEL(gtkblist->headline_label), text);
7292 gtk_image_set_from_icon_name(GTK_IMAGE(gtkblist->headline_image),
7293 icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
7295 gtkblist->headline_callback = callback;
7296 gtkblist->headline_data = user_data;
7297 gtkblist->headline_destroy = destroy;
7298 if (text != NULL || icon_name != NULL) {
7299 set_urgent();
7300 gtk_widget_show_all(gtkblist->headline);
7301 } else {
7302 gtk_widget_hide(gtkblist->headline);
7307 static void
7308 set_urgent(void)
7310 if (gtkblist->window && !gtk_widget_has_focus(gtkblist->window))
7311 pidgin_set_urgent(GTK_WINDOW(gtkblist->window), TRUE);
7314 PidginBuddyList *
7315 pidgin_blist_get_default_gtk_blist(void)
7317 return gtkblist;
7320 static gboolean autojoin_cb(PurpleConnection *gc, gpointer data)
7322 PurpleAccount *account = purple_connection_get_account(gc);
7323 PurpleBlistNode *gnode, *cnode;
7324 for (gnode = purple_blist_get_default_root(); gnode;
7325 gnode = gnode->next) {
7326 if(!PURPLE_IS_GROUP(gnode))
7327 continue;
7328 for(cnode = gnode->child; cnode; cnode = cnode->next)
7330 PurpleChat *chat;
7332 if(!PURPLE_IS_CHAT(cnode))
7333 continue;
7335 chat = (PurpleChat *)cnode;
7337 if(purple_chat_get_account(chat) != account)
7338 continue;
7340 if (purple_blist_node_get_bool(PURPLE_BLIST_NODE(chat), "gtk-autojoin"))
7341 purple_serv_join_chat(gc, purple_chat_get_components(chat));
7345 /* Stop processing; we handled the autojoins. */
7346 return TRUE;
7349 void *
7350 pidgin_blist_get_handle() {
7351 static int handle;
7353 return &handle;
7356 static gboolean buddy_signonoff_timeout_cb(PurpleBuddy *buddy)
7358 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(buddy));
7360 gtknode->recent_signonoff = FALSE;
7361 gtknode->recent_signonoff_timer = 0;
7363 pidgin_blist_update(NULL, PURPLE_BLIST_NODE(buddy));
7365 g_object_unref(buddy);
7366 return FALSE;
7369 static void buddy_signonoff_cb(PurpleBuddy *buddy)
7371 PidginBlistNode *gtknode = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(buddy));
7373 if(!gtknode) {
7374 pidgin_blist_new_node(purple_blist_get_default(),
7375 PURPLE_BLIST_NODE(buddy));
7378 gtknode = purple_blist_node_get_ui_data(PURPLE_BLIST_NODE(buddy));
7380 gtknode->recent_signonoff = TRUE;
7382 if(gtknode->recent_signonoff_timer > 0)
7383 g_source_remove(gtknode->recent_signonoff_timer);
7385 g_object_ref(buddy);
7386 gtknode->recent_signonoff_timer = g_timeout_add_seconds(10,
7387 (GSourceFunc)buddy_signonoff_timeout_cb, buddy);
7390 void
7391 pidgin_blist_set_theme(PidginBlistTheme *theme)
7393 PidginBuddyListPrivate *priv =
7394 pidgin_buddy_list_get_instance_private(gtkblist);
7395 PurpleBuddyList *list = purple_blist_get_default();
7397 if (theme != NULL)
7398 purple_prefs_set_string(PIDGIN_PREFS_ROOT "/blist/theme",
7399 purple_theme_get_name(PURPLE_THEME(theme)));
7400 else
7401 purple_prefs_set_string(PIDGIN_PREFS_ROOT "/blist/theme", "");
7403 if (priv->current_theme)
7404 g_object_unref(priv->current_theme);
7406 priv->current_theme = theme ? g_object_ref(theme) : NULL;
7408 pidgin_blist_build_layout(list);
7410 pidgin_blist_refresh(list);
7414 PidginBlistTheme *
7415 pidgin_blist_get_theme()
7417 PidginBuddyListPrivate *priv =
7418 pidgin_buddy_list_get_instance_private(gtkblist);
7420 return priv->current_theme;
7423 void pidgin_blist_init(void)
7425 void *gtk_blist_handle = pidgin_blist_get_handle();
7427 cached_emblems = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
7429 /* Initialize prefs */
7430 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/blist");
7431 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/blist/show_buddy_icons", TRUE);
7432 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/blist/show_empty_groups", FALSE);
7433 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/blist/show_idle_time", TRUE);
7434 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/blist/show_offline_buddies", FALSE);
7435 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/blist/show_protocol_icons", FALSE);
7436 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/blist/list_visible", FALSE);
7437 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/blist/list_maximized", FALSE);
7438 purple_prefs_add_string(PIDGIN_PREFS_ROOT "/blist/sort_type", "alphabetical");
7439 purple_prefs_add_int(PIDGIN_PREFS_ROOT "/blist/width", 250); /* Golden ratio, baby */
7440 purple_prefs_add_int(PIDGIN_PREFS_ROOT "/blist/height", 405); /* Golden ratio, baby */
7441 purple_prefs_add_string(PIDGIN_PREFS_ROOT "/blist/theme", "");
7443 purple_theme_manager_register_type(g_object_new(PIDGIN_TYPE_BLIST_THEME_LOADER, "type", "blist", NULL));
7445 /* Register our signals */
7446 purple_signal_register(gtk_blist_handle, "gtkblist-hiding",
7447 purple_marshal_VOID__POINTER, G_TYPE_NONE, 1,
7448 PURPLE_TYPE_BUDDY_LIST);
7450 purple_signal_register(gtk_blist_handle, "gtkblist-unhiding",
7451 purple_marshal_VOID__POINTER, G_TYPE_NONE, 1,
7452 PURPLE_TYPE_BUDDY_LIST);
7454 purple_signal_register(gtk_blist_handle, "gtkblist-created",
7455 purple_marshal_VOID__POINTER, G_TYPE_NONE, 1,
7456 PURPLE_TYPE_BUDDY_LIST);
7458 purple_signal_register(gtk_blist_handle, "drawing-tooltip",
7459 purple_marshal_VOID__POINTER_POINTER_UINT, G_TYPE_NONE,
7460 3, PURPLE_TYPE_BLIST_NODE,
7461 G_TYPE_POINTER, /* pointer to a (GString *) */
7462 G_TYPE_BOOLEAN);
7464 purple_signal_register(gtk_blist_handle, "drawing-buddy",
7465 purple_marshal_POINTER__POINTER,
7466 G_TYPE_STRING, 1, PURPLE_TYPE_BUDDY);
7468 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on",
7469 gtk_blist_handle, PURPLE_CALLBACK(buddy_signonoff_cb), NULL);
7470 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off",
7471 gtk_blist_handle, PURPLE_CALLBACK(buddy_signonoff_cb), NULL);
7472 purple_signal_connect(purple_blist_get_handle(), "buddy-privacy-changed",
7473 gtk_blist_handle, PURPLE_CALLBACK(pidgin_blist_update_privacy_cb), NULL);
7475 purple_signal_connect_priority(purple_connections_get_handle(), "autojoin",
7476 gtk_blist_handle, PURPLE_CALLBACK(autojoin_cb),
7477 NULL, PURPLE_SIGNAL_PRIORITY_HIGHEST);
7480 void
7481 pidgin_blist_uninit(void) {
7482 g_hash_table_destroy(cached_emblems);
7484 purple_signals_unregister_by_instance(pidgin_blist_get_handle());
7485 purple_signals_disconnect_by_handle(pidgin_blist_get_handle());
7487 accountmenu = NULL;
7488 gtkblist = NULL;
7491 /**************************************************************************
7492 * GTK Buddy list GObject code
7493 **************************************************************************/
7494 static void
7495 pidgin_buddy_list_init(PidginBuddyList *self)
7499 static void
7500 pidgin_buddy_list_finalize(GObject *obj)
7502 PidginBuddyList *gtkblist = PIDGIN_BUDDY_LIST(obj);
7503 PidginBuddyListPrivate *priv =
7504 pidgin_buddy_list_get_instance_private(gtkblist);
7506 purple_signals_disconnect_by_handle(gtkblist);
7508 gtk_widget_destroy(gtkblist->window);
7510 pidgin_blist_tooltip_destroy();
7512 if (gtkblist->refresh_timer) {
7513 g_source_remove(gtkblist->refresh_timer);
7514 gtkblist->refresh_timer = 0;
7516 if (gtkblist->timeout) {
7517 g_source_remove(gtkblist->timeout);
7518 gtkblist->timeout = 0;
7520 if (gtkblist->drag_timeout) {
7521 g_source_remove(gtkblist->drag_timeout);
7522 gtkblist->drag_timeout = 0;
7525 gtkblist->window = gtkblist->vbox = gtkblist->treeview = NULL;
7526 g_clear_object(&gtkblist->treemodel);
7527 g_object_unref(G_OBJECT(gtkblist->ui));
7528 g_object_unref(G_OBJECT(gtkblist->empty_avatar));
7530 g_clear_object(&priv->current_theme);
7531 if (priv->select_notebook_page_timeout) {
7532 g_source_remove(priv->select_notebook_page_timeout);
7535 purple_prefs_disconnect_by_handle(pidgin_blist_get_handle());
7537 G_OBJECT_CLASS(pidgin_buddy_list_parent_class)->finalize(obj);
7540 static void
7541 pidgin_buddy_list_class_init(PidginBuddyListClass *klass)
7543 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
7544 PurpleBuddyListClass *purple_blist_class;
7546 obj_class->finalize = pidgin_buddy_list_finalize;
7548 purple_blist_class = PURPLE_BUDDY_LIST_CLASS(klass);
7549 purple_blist_class->new_node = pidgin_blist_new_node;
7550 purple_blist_class->show = pidgin_blist_show;
7551 purple_blist_class->update = pidgin_blist_update;
7552 purple_blist_class->remove = pidgin_blist_remove;
7553 purple_blist_class->set_visible = pidgin_blist_set_visible;
7554 purple_blist_class->request_add_buddy = pidgin_blist_request_add_buddy;
7555 purple_blist_class->request_add_chat = pidgin_blist_request_add_chat;
7556 purple_blist_class->request_add_group = pidgin_blist_request_add_group;
7559 /*********************************************************************
7560 * Buddy List sorting functions *
7561 *********************************************************************/
7563 GList *pidgin_blist_get_sort_methods()
7565 return pidgin_blist_sort_methods;
7568 void pidgin_blist_sort_method_reg(const char *id, const char *name, pidgin_blist_sort_function func)
7570 struct _PidginBlistSortMethod *method;
7572 g_return_if_fail(id != NULL);
7573 g_return_if_fail(name != NULL);
7574 g_return_if_fail(func != NULL);
7576 method = g_new0(struct _PidginBlistSortMethod, 1);
7577 method->id = g_strdup(id);
7578 method->name = g_strdup(name);
7579 method->func = func;
7580 pidgin_blist_sort_methods = g_list_append(pidgin_blist_sort_methods, method);
7581 pidgin_blist_update_sort_methods();
7584 void pidgin_blist_sort_method_unreg(const char *id)
7586 GList *l = pidgin_blist_sort_methods;
7588 g_return_if_fail(id != NULL);
7590 while(l) {
7591 struct _PidginBlistSortMethod *method = l->data;
7592 if(purple_strequal(method->id, id)) {
7593 pidgin_blist_sort_methods = g_list_delete_link(pidgin_blist_sort_methods, l);
7594 g_free(method->id);
7595 g_free(method->name);
7596 g_free(method);
7597 break;
7599 l = l->next;
7601 pidgin_blist_update_sort_methods();
7604 void pidgin_blist_sort_method_set(const char *id){
7605 GList *l = pidgin_blist_sort_methods;
7607 if(!id)
7608 id = "none";
7610 while (l && !purple_strequal(((struct _PidginBlistSortMethod*)l->data)->id, id))
7611 l = l->next;
7613 if (l) {
7614 current_sort_method = l->data;
7615 } else if (!current_sort_method) {
7616 pidgin_blist_sort_method_set("none");
7617 return;
7619 if (purple_strequal(id, "none")) {
7620 redo_buddy_list(purple_blist_get_default(), TRUE, FALSE);
7621 } else {
7622 redo_buddy_list(purple_blist_get_default(), FALSE, FALSE);
7626 /******************************************
7627 ** Sort Methods
7628 ******************************************/
7630 static void sort_method_none(PurpleBlistNode *node, PurpleBuddyList *blist, GtkTreeIter parent_iter, GtkTreeIter *cur, GtkTreeIter *iter)
7632 PurpleBlistNode *sibling = node->prev;
7633 GtkTreeIter sibling_iter;
7635 if (cur != NULL) {
7636 *iter = *cur;
7637 return;
7640 while (sibling && !get_iter_from_node(sibling, &sibling_iter)) {
7641 sibling = sibling->prev;
7644 gtk_tree_store_insert_after(gtkblist->treemodel, iter,
7645 node->parent ? &parent_iter : NULL,
7646 sibling ? &sibling_iter : NULL);
7649 static void sort_method_alphabetical(PurpleBlistNode *node, PurpleBuddyList *blist, GtkTreeIter groupiter, GtkTreeIter *cur, GtkTreeIter *iter)
7651 GtkTreeIter more_z;
7653 const char *my_name;
7655 if(PURPLE_IS_CONTACT(node)) {
7656 my_name = purple_contact_get_alias((PurpleContact*)node);
7657 } else if(PURPLE_IS_CHAT(node)) {
7658 my_name = purple_chat_get_name((PurpleChat*)node);
7659 } else {
7660 sort_method_none(node, blist, groupiter, cur, iter);
7661 return;
7664 if (!gtk_tree_model_iter_children(GTK_TREE_MODEL(gtkblist->treemodel), &more_z, &groupiter)) {
7665 gtk_tree_store_insert(gtkblist->treemodel, iter, &groupiter, 0);
7666 return;
7669 do {
7670 PurpleBlistNode *n;
7671 const char *this_name;
7672 int cmp;
7674 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &more_z, NODE_COLUMN, &n, -1);
7676 if(PURPLE_IS_CONTACT(n)) {
7677 this_name = purple_contact_get_alias((PurpleContact*)n);
7678 } else if(PURPLE_IS_CHAT(n)) {
7679 this_name = purple_chat_get_name((PurpleChat*)n);
7680 } else {
7681 this_name = NULL;
7684 cmp = purple_utf8_strcasecmp(my_name, this_name);
7686 if(this_name && (cmp < 0 || (cmp == 0 && node < n))) {
7687 if(cur) {
7688 gtk_tree_store_move_before(gtkblist->treemodel, cur, &more_z);
7689 *iter = *cur;
7690 return;
7691 } else {
7692 gtk_tree_store_insert_before(gtkblist->treemodel, iter,
7693 &groupiter, &more_z);
7694 return;
7697 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL(gtkblist->treemodel), &more_z));
7699 if(cur) {
7700 gtk_tree_store_move_before(gtkblist->treemodel, cur, NULL);
7701 *iter = *cur;
7702 return;
7703 } else {
7704 gtk_tree_store_append(gtkblist->treemodel, iter, &groupiter);
7705 return;
7709 static void sort_method_status(PurpleBlistNode *node, PurpleBuddyList *blist, GtkTreeIter groupiter, GtkTreeIter *cur, GtkTreeIter *iter)
7711 GtkTreeIter more_z;
7713 PurpleBuddy *my_buddy, *this_buddy;
7715 if(PURPLE_IS_CONTACT(node)) {
7716 my_buddy = purple_contact_get_priority_buddy((PurpleContact*)node);
7717 } else if(PURPLE_IS_CHAT(node)) {
7718 if (cur != NULL) {
7719 *iter = *cur;
7720 return;
7723 gtk_tree_store_append(gtkblist->treemodel, iter, &groupiter);
7724 return;
7725 } else {
7726 sort_method_alphabetical(node, blist, groupiter, cur, iter);
7727 return;
7731 if (!gtk_tree_model_iter_children(GTK_TREE_MODEL(gtkblist->treemodel), &more_z, &groupiter)) {
7732 gtk_tree_store_insert(gtkblist->treemodel, iter, &groupiter, 0);
7733 return;
7736 do {
7737 PurpleBlistNode *n;
7738 gint name_cmp;
7739 gint presence_cmp;
7741 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &more_z, NODE_COLUMN, &n, -1);
7743 if(PURPLE_IS_CONTACT(n)) {
7744 this_buddy = purple_contact_get_priority_buddy((PurpleContact*)n);
7745 } else {
7746 this_buddy = NULL;
7749 name_cmp = purple_utf8_strcasecmp(
7750 purple_contact_get_alias(purple_buddy_get_contact(my_buddy)),
7751 (this_buddy
7752 ? purple_contact_get_alias(purple_buddy_get_contact(this_buddy))
7753 : NULL));
7755 presence_cmp = purple_buddy_presence_compare(
7756 PURPLE_BUDDY_PRESENCE(purple_buddy_get_presence(my_buddy)),
7757 this_buddy ? PURPLE_BUDDY_PRESENCE(purple_buddy_get_presence(this_buddy)) : NULL);
7759 if (this_buddy == NULL ||
7760 (presence_cmp < 0 ||
7761 (presence_cmp == 0 &&
7762 (name_cmp < 0 || (name_cmp == 0 && node < n)))))
7764 if (cur != NULL)
7766 gtk_tree_store_move_before(gtkblist->treemodel, cur, &more_z);
7767 *iter = *cur;
7768 return;
7770 else
7772 gtk_tree_store_insert_before(gtkblist->treemodel, iter,
7773 &groupiter, &more_z);
7774 return;
7778 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(gtkblist->treemodel),
7779 &more_z));
7781 if (cur) {
7782 gtk_tree_store_move_before(gtkblist->treemodel, cur, NULL);
7783 *iter = *cur;
7784 return;
7785 } else {
7786 gtk_tree_store_append(gtkblist->treemodel, iter, &groupiter);
7787 return;
7791 static void sort_method_log_activity(PurpleBlistNode *node, PurpleBuddyList *blist, GtkTreeIter groupiter, GtkTreeIter *cur, GtkTreeIter *iter)
7793 GtkTreeIter more_z;
7795 int activity_score = 0, this_log_activity_score = 0;
7796 const char *buddy_name, *this_buddy_name;
7798 if(cur && (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(gtkblist->treemodel), &groupiter) == 1)) {
7799 *iter = *cur;
7800 return;
7803 if(PURPLE_IS_CONTACT(node)) {
7804 PurpleBlistNode *n;
7805 PurpleBuddy *buddy;
7806 for (n = node->child; n; n = n->next) {
7807 buddy = (PurpleBuddy*)n;
7808 activity_score += purple_log_get_activity_score(PURPLE_LOG_IM, purple_buddy_get_name(buddy), purple_buddy_get_account(buddy));
7810 buddy_name = purple_contact_get_alias((PurpleContact*)node);
7811 } else if(PURPLE_IS_CHAT(node)) {
7812 /* we don't have a reliable way of getting the log filename
7813 * from the chat info in the blist, yet */
7814 if (cur != NULL) {
7815 *iter = *cur;
7816 return;
7819 gtk_tree_store_append(gtkblist->treemodel, iter, &groupiter);
7820 return;
7821 } else {
7822 sort_method_none(node, blist, groupiter, cur, iter);
7823 return;
7827 if (!gtk_tree_model_iter_children(GTK_TREE_MODEL(gtkblist->treemodel), &more_z, &groupiter)) {
7828 gtk_tree_store_insert(gtkblist->treemodel, iter, &groupiter, 0);
7829 return;
7832 do {
7833 PurpleBlistNode *n;
7834 PurpleBlistNode *n2;
7835 PurpleBuddy *buddy;
7836 int cmp;
7838 gtk_tree_model_get(GTK_TREE_MODEL(gtkblist->treemodel), &more_z, NODE_COLUMN, &n, -1);
7839 this_log_activity_score = 0;
7841 if(PURPLE_IS_CONTACT(n)) {
7842 for (n2 = n->child; n2; n2 = n2->next) {
7843 buddy = (PurpleBuddy*)n2;
7844 this_log_activity_score += purple_log_get_activity_score(PURPLE_LOG_IM, purple_buddy_get_name(buddy), purple_buddy_get_account(buddy));
7846 this_buddy_name = purple_contact_get_alias((PurpleContact*)n);
7847 } else {
7848 this_buddy_name = NULL;
7851 cmp = purple_utf8_strcasecmp(buddy_name, this_buddy_name);
7853 if (!PURPLE_IS_CONTACT(n) || activity_score > this_log_activity_score ||
7854 ((activity_score == this_log_activity_score) &&
7855 (cmp < 0 || (cmp == 0 && node < n)))) {
7856 if (cur != NULL) {
7857 gtk_tree_store_move_before(gtkblist->treemodel, cur, &more_z);
7858 *iter = *cur;
7859 return;
7860 } else {
7861 gtk_tree_store_insert_before(gtkblist->treemodel, iter,
7862 &groupiter, &more_z);
7863 return;
7866 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL(gtkblist->treemodel), &more_z));
7868 if (cur != NULL) {
7869 gtk_tree_store_move_before(gtkblist->treemodel, cur, NULL);
7870 *iter = *cur;
7871 return;
7872 } else {
7873 gtk_tree_store_append(gtkblist->treemodel, iter, &groupiter);
7874 return;
7878 static void
7879 plugin_act(GSimpleAction *action, GVariant *param, PurplePluginAction *pam)
7881 if (pam && pam->callback)
7882 pam->callback(pam);
7885 static GtkWidget *
7886 build_plugin_actions(GActionMap *action_map, const gchar *parent,
7887 PurplePlugin *plugin)
7889 GMenu *menu = NULL;
7890 GMenu *section = NULL;
7891 PurplePluginActionsCb actions_cb;
7892 PurplePluginAction *action = NULL;
7893 GList *actions, *l;
7894 char *name;
7895 int count = 0;
7896 GtkWidget *ret;
7898 actions_cb =
7899 purple_plugin_info_get_actions_cb(purple_plugin_get_info(plugin));
7901 actions = actions_cb(plugin);
7903 if (actions == NULL) {
7904 return NULL;
7907 menu = g_menu_new();
7909 for (l = actions; l != NULL; l = l->next) {
7910 GAction *menuaction;
7912 action = (PurplePluginAction *)l->data;
7914 if (action == NULL) {
7915 if (section != NULL) {
7916 /* Close and append section if any */
7917 g_menu_append_section(menu, NULL,
7918 G_MENU_MODEL(section));
7919 g_clear_object(&section);
7922 continue;
7925 action->plugin = plugin;
7927 name = g_strdup_printf("plugin.%s-action-%d", parent, count++);
7928 /* +7 to skip "plugin." prefix */
7929 menuaction = G_ACTION(g_simple_action_new(name + 7, NULL));
7930 g_signal_connect_data(G_OBJECT(menuaction), "activate",
7931 G_CALLBACK(plugin_act), action,
7932 (GClosureNotify)purple_plugin_action_free, 0);
7933 g_action_map_add_action(action_map, menuaction);
7934 g_object_unref(menuaction);
7936 if (section == NULL) {
7937 section = g_menu_new();
7940 g_menu_append(section, action->label, name);
7941 g_free(name);
7944 if (section != NULL) {
7945 /* Close and append final section if any */
7946 g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
7947 g_clear_object(&section);
7950 g_list_free(actions);
7952 ret = gtk_menu_new_from_model(G_MENU_MODEL(menu));
7953 g_object_unref(menu);
7954 return ret;
7958 static void
7959 modify_account_cb(GtkWidget *widget, gpointer data)
7961 pidgin_account_dialog_show(PIDGIN_MODIFY_ACCOUNT_DIALOG, data);
7964 static void
7965 enable_account_cb(GtkCheckMenuItem *widget, gpointer data)
7967 PurpleAccount *account = data;
7968 const PurpleSavedStatus *saved_status;
7970 saved_status = purple_savedstatus_get_current();
7971 purple_savedstatus_activate_for_account(saved_status, account);
7973 purple_account_set_enabled(account, PIDGIN_UI, TRUE);
7976 static void
7977 disable_account_cb(GtkCheckMenuItem *widget, gpointer data)
7979 PurpleAccount *account = data;
7981 purple_account_set_enabled(account, PIDGIN_UI, FALSE);
7984 static void
7985 protocol_act(GtkWidget *obj, PurpleProtocolAction *pam)
7987 if (pam && pam->callback)
7988 pam->callback(pam);
7991 void
7992 pidgin_blist_update_accounts_menu(void)
7994 GtkWidget *menuitem, *submenu;
7995 GtkAccelGroup *accel_group;
7996 GList *l, *accounts;
7997 gboolean disabled_accounts = FALSE;
7998 gboolean enabled_accounts = FALSE;
8000 if (accountmenu == NULL)
8001 return;
8003 /* Clear the old Accounts menu */
8004 for (l = gtk_container_get_children(GTK_CONTAINER(accountmenu)); l; l = g_list_delete_link(l, l)) {
8005 menuitem = l->data;
8007 if (menuitem != gtk_ui_manager_get_widget(gtkblist->ui, "/BList/AccountsMenu/ManageAccounts"))
8008 gtk_widget_destroy(menuitem);
8011 accel_group = gtk_menu_get_accel_group(GTK_MENU(accountmenu));
8013 for (accounts = purple_accounts_get_all(); accounts; accounts = accounts->next) {
8014 char *buf = NULL;
8015 GtkWidget *image = NULL;
8016 PurpleAccount *account = NULL;
8017 GdkPixbuf *pixbuf = NULL;
8019 account = accounts->data;
8021 if (!purple_account_get_enabled(account, PIDGIN_UI)) {
8022 if (!disabled_accounts) {
8023 menuitem = gtk_menu_item_new_with_label(_("Enable Account"));
8024 gtk_menu_shell_append(GTK_MENU_SHELL(accountmenu), menuitem);
8026 submenu = gtk_menu_new();
8027 gtk_menu_set_accel_group(GTK_MENU(submenu), accel_group);
8028 gtk_menu_set_accel_path(GTK_MENU(submenu), "<Actions>/BListActions/EnableAccount");
8029 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
8031 disabled_accounts = TRUE;
8034 buf = g_strconcat(purple_account_get_username(account), " (",
8035 purple_account_get_protocol_name(account), ")", NULL);
8036 menuitem = gtk_image_menu_item_new_with_label(buf);
8037 g_free(buf);
8039 pixbuf = pidgin_create_protocol_icon(account, PIDGIN_PROTOCOL_ICON_SMALL);
8040 if (pixbuf != NULL) {
8041 if (!purple_account_is_connected(account))
8042 gdk_pixbuf_saturate_and_pixelate(pixbuf, pixbuf, 0.0, FALSE);
8043 image = gtk_image_new_from_pixbuf(pixbuf);
8044 g_object_unref(G_OBJECT(pixbuf));
8045 gtk_widget_show(image);
8046 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem), image);
8049 g_signal_connect(G_OBJECT(menuitem), "activate",
8050 G_CALLBACK(enable_account_cb), account);
8051 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
8053 } else {
8054 enabled_accounts = TRUE;
8058 if (!enabled_accounts) {
8059 gtk_widget_show_all(accountmenu);
8060 return;
8063 pidgin_separator(accountmenu);
8065 for (accounts = purple_accounts_get_all(); accounts; accounts = accounts->next) {
8066 char *buf = NULL;
8067 char *accel_path_buf = NULL;
8068 GtkWidget *image = NULL;
8069 PurpleConnection *gc = NULL;
8070 PurpleAccount *account = NULL;
8071 GdkPixbuf *pixbuf = NULL;
8072 PurpleProtocol *protocol;
8074 account = accounts->data;
8076 if (!purple_account_get_enabled(account, PIDGIN_UI))
8077 continue;
8079 buf = g_strconcat(purple_account_get_username(account), " (",
8080 purple_account_get_protocol_name(account), ")", NULL);
8081 menuitem = gtk_image_menu_item_new_with_label(buf);
8082 accel_path_buf = g_strconcat("<Actions>/AccountActions/", buf, NULL);
8083 g_free(buf);
8085 pixbuf = pidgin_create_protocol_icon(account, PIDGIN_PROTOCOL_ICON_SMALL);
8086 if (pixbuf != NULL) {
8087 if (!purple_account_is_connected(account))
8088 gdk_pixbuf_saturate_and_pixelate(pixbuf, pixbuf,
8089 0.0, FALSE);
8090 image = gtk_image_new_from_pixbuf(pixbuf);
8091 g_object_unref(G_OBJECT(pixbuf));
8092 gtk_widget_show(image);
8093 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem), image);
8096 gtk_menu_shell_append(GTK_MENU_SHELL(accountmenu), menuitem);
8098 submenu = gtk_menu_new();
8099 gtk_menu_set_accel_group(GTK_MENU(submenu), accel_group);
8100 gtk_menu_set_accel_path(GTK_MENU(submenu), accel_path_buf);
8101 g_free(accel_path_buf);
8102 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
8104 menuitem = gtk_menu_item_new_with_mnemonic(_("_Edit Account"));
8105 g_signal_connect(G_OBJECT(menuitem), "activate",
8106 G_CALLBACK(modify_account_cb), account);
8107 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
8109 pidgin_separator(submenu);
8111 gc = purple_account_get_connection(account);
8112 protocol = gc && PURPLE_CONNECTION_IS_CONNECTED(gc) ?
8113 purple_connection_get_protocol(gc) : NULL;
8115 if (protocol &&
8116 (PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, get_moods) ||
8117 PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, get_actions))) {
8118 if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, get_moods) &&
8119 (purple_connection_get_flags(gc) & PURPLE_CONNECTION_FLAG_SUPPORT_MOODS)) {
8121 if (purple_account_get_status(account, "mood")) {
8122 menuitem = gtk_menu_item_new_with_mnemonic(_("Set _Mood..."));
8123 g_signal_connect(G_OBJECT(menuitem), "activate",
8124 G_CALLBACK(set_mood_cb), account);
8125 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
8129 if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, get_actions)) {
8130 GtkWidget *menuitem;
8131 PurpleProtocolAction *action = NULL;
8132 GList *actions, *l;
8134 actions = purple_protocol_client_iface_get_actions(protocol, gc);
8136 for (l = actions; l != NULL; l = l->next)
8138 if (l->data)
8140 action = (PurpleProtocolAction *) l->data;
8141 action->connection = gc;
8143 menuitem = gtk_menu_item_new_with_label(action->label);
8144 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
8146 g_signal_connect(G_OBJECT(menuitem), "activate",
8147 G_CALLBACK(protocol_act), action);
8148 g_object_set_data_full(G_OBJECT(menuitem), "protocol_action",
8149 action,
8150 (GDestroyNotify)purple_protocol_action_free);
8151 gtk_widget_show(menuitem);
8153 else
8154 pidgin_separator(submenu);
8157 g_list_free(actions);
8159 } else {
8160 menuitem = gtk_menu_item_new_with_label(_("No actions available"));
8161 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
8162 gtk_widget_set_sensitive(menuitem, FALSE);
8165 pidgin_separator(submenu);
8167 menuitem = gtk_menu_item_new_with_mnemonic(_("_Disable"));
8168 g_signal_connect(G_OBJECT(menuitem), "activate",
8169 G_CALLBACK(disable_account_cb), account);
8170 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
8173 gtk_widget_show_all(accountmenu);
8176 static GSList *plugin_menu_items;
8178 void
8179 pidgin_blist_update_plugin_actions(void)
8181 GtkWidget *toolsmenu;
8182 GSimpleActionGroup *action_group;
8183 PurplePlugin *plugin = NULL;
8184 PurplePluginInfo *info;
8185 GList *l;
8187 int count = 0;
8189 if ((gtkblist == NULL) || (gtkblist->ui == NULL))
8190 return;
8192 /* Clear the old menu */
8193 g_slist_free_full(plugin_menu_items,
8194 (GDestroyNotify)gtk_widget_destroy);
8195 plugin_menu_items = NULL;
8197 toolsmenu = gtk_ui_manager_get_widget(gtkblist->ui,
8198 "/BList/ToolsMenu");
8199 toolsmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(toolsmenu));
8201 action_group = g_simple_action_group_new();
8203 /* Add a submenu for each plugin with custom actions */
8204 for (l = purple_plugins_get_loaded(); l; l = l->next) {
8205 char *name;
8206 GtkWidget *submenu;
8207 GtkWidget *menuitem;
8209 plugin = (PurplePlugin *)l->data;
8210 info = purple_plugin_get_info(plugin);
8212 if (!purple_plugin_info_get_actions_cb(info))
8213 continue;
8215 name = g_strdup_printf("plugin%d", count);
8216 submenu = build_plugin_actions(G_ACTION_MAP(action_group),
8217 name, plugin);
8218 g_free(name);
8220 if (submenu == NULL) {
8221 continue;
8224 menuitem = gtk_menu_item_new_with_mnemonic(
8225 _(gplugin_plugin_info_get_name(
8226 GPLUGIN_PLUGIN_INFO(info))));
8227 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
8228 gtk_widget_show(menuitem);
8229 plugin_menu_items = g_slist_prepend(plugin_menu_items,
8230 menuitem);
8231 gtk_menu_shell_append(GTK_MENU_SHELL(toolsmenu), menuitem);
8233 count++;
8236 /* Replaces existing "plugin" group if any */
8237 gtk_widget_insert_action_group(toolsmenu, "plugin",
8238 G_ACTION_GROUP(action_group));
8239 g_object_unref(action_group);
8242 static void
8243 sortmethod_act(GtkRadioAction *action, GtkRadioAction *current, char *id)
8245 if (action == current)
8247 pidgin_set_cursor(gtkblist->window, GDK_WATCH);
8248 /* This is redundant. I think. */
8249 /* pidgin_blist_sort_method_set(id); */
8250 purple_prefs_set_string(PIDGIN_PREFS_ROOT "/blist/sort_type", id);
8252 pidgin_clear_cursor(gtkblist->window);
8256 void
8257 pidgin_blist_update_sort_methods(void)
8259 PidginBlistSortMethod *method = NULL;
8260 GList *l;
8261 GSList *sl = NULL;
8262 const char *m = purple_prefs_get_string(PIDGIN_PREFS_ROOT "/blist/sort_type");
8264 GtkRadioAction *action;
8265 GString *ui_string;
8267 if ((gtkblist == NULL) || (gtkblist->ui == NULL))
8268 return;
8270 /* Clear the old menu */
8271 if (sort_action_group) {
8272 gtk_ui_manager_remove_ui(gtkblist->ui, sort_merge_id);
8273 gtk_ui_manager_remove_action_group(gtkblist->ui, sort_action_group);
8274 g_object_unref(G_OBJECT(sort_action_group));
8277 sort_action_group = gtk_action_group_new("SortMethods");
8278 gtk_action_group_set_translation_domain(sort_action_group, PACKAGE);
8280 ui_string = g_string_new("<ui><menubar name='BList'>"
8281 "<menu action='BuddiesMenu'><menu action='SortMenu'>");
8283 for (l = pidgin_blist_sort_methods; l; l = l->next) {
8284 method = (PidginBlistSortMethod *)l->data;
8286 g_string_append_printf(ui_string, "<menuitem action='%s'/>", method->id);
8287 action = gtk_radio_action_new(method->id,
8288 method->name,
8289 NULL,
8290 NULL,
8292 gtk_action_group_add_action_with_accel(sort_action_group, GTK_ACTION(action), NULL);
8294 gtk_radio_action_set_group(action, sl);
8295 sl = gtk_radio_action_get_group(action);
8297 if (purple_strequal(m, method->id))
8298 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), TRUE);
8299 else
8300 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), FALSE);
8302 g_signal_connect(G_OBJECT(action), "changed",
8303 G_CALLBACK(sortmethod_act), method->id);
8306 g_string_append(ui_string, "</menu></menu></menubar></ui>");
8307 gtk_ui_manager_insert_action_group(gtkblist->ui, sort_action_group, 1);
8308 sort_merge_id = gtk_ui_manager_add_ui_from_string(gtkblist->ui, ui_string->str, -1, NULL);
8310 g_string_free(ui_string, TRUE);