Convert UI ops to PurpleBuddyListClass methods.
[pidgin-git.git] / pidgin / gtkdocklet.c
blob5adcc2d49cfcdb5f27420a6b63ffd7bdc87b0006
1 /*
2 * System tray icon (aka docklet) plugin for Purple
4 * Copyright (C) 2002-3 Robert McQueen <robot101@debian.org>
5 * Copyright (C) 2003 Herman Bloggs <hermanator12002@yahoo.com>
6 * Inspired by a similar plugin by:
7 * John (J5) Palmieri <johnp@martianrock.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02111-1301, USA.
24 #include "internal.h"
25 #include "pidgin.h"
27 #include "core.h"
28 #include "conversation.h"
29 #include "debug.h"
30 #include "prefs.h"
31 #include "signals.h"
32 #include "sound.h"
33 #include "status.h"
35 #include "gtkaccount.h"
36 #include "gtkblist.h"
37 #include "gtkconv.h"
38 #include "gtkplugin.h"
39 #include "gtkprefs.h"
40 #include "gtksavedstatuses.h"
41 #include "gtksound.h"
42 #include "gtkstatusbox.h"
43 #include "gtkutils.h"
44 #include "pidginstock.h"
45 #include "gtkdocklet.h"
46 #include "gtkdialogs.h"
47 #include "gtknotify.h"
49 #include "gtk3compat.h"
51 #ifndef DOCKLET_TOOLTIP_LINE_LIMIT
52 #define DOCKLET_TOOLTIP_LINE_LIMIT 5
53 #endif
55 #define SHORT_EMBED_TIMEOUT 5
56 #define LONG_EMBED_TIMEOUT 15
58 /* globals */
59 static GtkStatusIcon *docklet = NULL;
60 static guint embed_timeout = 0;
61 static PurpleStatusPrimitive status = PURPLE_STATUS_OFFLINE;
62 static PidginDockletFlag flags = 0;
63 static gboolean enable_join_chat = FALSE;
64 static gboolean visible = FALSE;
65 static gboolean visibility_manager = FALSE;
67 /* protos */
68 static void docklet_gtk_status_create(gboolean);
69 static void docklet_gtk_status_destroy(void);
71 /**************************************************************************
72 * docklet status and utility functions
73 **************************************************************************/
74 static void
75 docklet_gtk_status_update_icon(PurpleStatusPrimitive status, PidginDockletFlag newflag)
77 const gchar *icon_name = NULL;
79 switch (status) {
80 case PURPLE_STATUS_OFFLINE:
81 icon_name = PIDGIN_STOCK_TRAY_OFFLINE;
82 break;
83 case PURPLE_STATUS_AWAY:
84 icon_name = PIDGIN_STOCK_TRAY_AWAY;
85 break;
86 case PURPLE_STATUS_UNAVAILABLE:
87 icon_name = PIDGIN_STOCK_TRAY_BUSY;
88 break;
89 case PURPLE_STATUS_EXTENDED_AWAY:
90 icon_name = PIDGIN_STOCK_TRAY_XA;
91 break;
92 case PURPLE_STATUS_INVISIBLE:
93 icon_name = PIDGIN_STOCK_TRAY_INVISIBLE;
94 break;
95 default:
96 icon_name = PIDGIN_STOCK_TRAY_AVAILABLE;
97 break;
100 if (newflag & PIDGIN_DOCKLET_EMAIL_PENDING)
101 icon_name = PIDGIN_STOCK_TRAY_EMAIL;
102 if (newflag & PIDGIN_DOCKLET_CONV_PENDING)
103 icon_name = PIDGIN_STOCK_TRAY_PENDING;
104 if (newflag & PIDGIN_DOCKLET_CONNECTING)
105 icon_name = PIDGIN_STOCK_TRAY_CONNECT;
107 if (icon_name) {
108 gtk_status_icon_set_from_icon_name(docklet, icon_name);
112 static GList *
113 get_pending_list(guint max)
115 GList *l_im, *l_chat;
117 l_im = pidgin_conversations_get_unseen_ims(PIDGIN_UNSEEN_TEXT, FALSE, max);
119 /* Short circuit if we have our information already */
120 if (max == 1 && l_im != NULL)
121 return l_im;
123 l_chat = pidgin_conversations_get_unseen_chats(
124 purple_prefs_get_int(PIDGIN_PREFS_ROOT "/conversations/notification_chat"),
125 FALSE, max);
127 if (l_im != NULL && l_chat != NULL)
128 return g_list_concat(l_im, l_chat);
129 else if (l_im != NULL)
130 return l_im;
131 else
132 return l_chat;
135 static gboolean
136 docklet_update_status(void)
138 GList *convs, *l;
139 int count;
140 PurpleSavedStatus *saved_status;
141 PurpleStatusPrimitive newstatus = PURPLE_STATUS_OFFLINE;
142 PidginDockletFlag newflags = 0;
144 /* get the current savedstatus */
145 saved_status = purple_savedstatus_get_current();
147 /* determine if any ims have unseen messages */
148 convs = get_pending_list(DOCKLET_TOOLTIP_LINE_LIMIT);
150 if (purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "pending")) {
151 if (convs && !visible) {
152 g_list_free(convs);
153 docklet_gtk_status_create(FALSE);
154 return FALSE;
155 } else if (!convs && visible) {
156 docklet_gtk_status_destroy();
157 return FALSE;
161 if (!visible) {
162 g_list_free(convs);
163 return FALSE;
166 if (convs != NULL) {
167 /* set tooltip if messages are pending */
168 GString *tooltip_text = g_string_new("");
169 newflags |= PIDGIN_DOCKLET_CONV_PENDING;
171 for (l = convs, count = 0 ; l != NULL ; l = l->next, count++) {
172 PurpleConversation *conv = (PurpleConversation *)l->data;
173 PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
175 if (count == DOCKLET_TOOLTIP_LINE_LIMIT - 1) {
176 g_string_append(tooltip_text, _("Right-click for more unread messages...\n"));
177 } else if(gtkconv) {
178 g_string_append_printf(tooltip_text,
179 ngettext("%d unread message from %s\n", "%d unread messages from %s\n", gtkconv->unseen_count),
180 gtkconv->unseen_count,
181 purple_conversation_get_title(conv));
182 } else {
183 g_string_append_printf(tooltip_text,
184 ngettext("%d unread message from %s\n", "%d unread messages from %s\n",
185 GPOINTER_TO_INT(g_object_get_data(G_OBJECT(conv), "unseen-count"))),
186 GPOINTER_TO_INT(g_object_get_data(G_OBJECT(conv), "unseen-count")),
187 purple_conversation_get_title(conv));
191 /* get rid of the last newline */
192 if (tooltip_text->len > 0)
193 tooltip_text = g_string_truncate(tooltip_text, tooltip_text->len - 1);
195 gtk_status_icon_set_tooltip_text(docklet, tooltip_text->str);
197 g_string_free(tooltip_text, TRUE);
198 g_list_free(convs);
200 } else {
201 char *tooltip_text = g_strconcat(PIDGIN_NAME, " - ",
202 purple_savedstatus_get_title(saved_status), NULL);
203 gtk_status_icon_set_tooltip_text(docklet, tooltip_text);
204 g_free(tooltip_text);
207 for(l = purple_accounts_get_all(); l != NULL; l = l->next) {
209 PurpleAccount *account = (PurpleAccount*)l->data;
211 if (!purple_account_get_enabled(account, PIDGIN_UI))
212 continue;
214 if (purple_account_is_disconnected(account))
215 continue;
217 if (purple_account_is_connecting(account))
218 newflags |= PIDGIN_DOCKLET_CONNECTING;
220 if (pidgin_notify_emails_pending())
221 newflags |= PIDGIN_DOCKLET_EMAIL_PENDING;
224 newstatus = purple_savedstatus_get_primitive_type(saved_status);
226 /* update the icon if we changed status */
227 if (status != newstatus || flags != newflags) {
228 status = newstatus;
229 flags = newflags;
231 docklet_gtk_status_update_icon(status, flags);
234 return FALSE; /* for when we're called by the glib idle handler */
237 static gboolean
238 online_account_supports_chat(void)
240 GList *c = NULL;
241 c = purple_connections_get_all();
243 while(c != NULL) {
244 PurpleConnection *gc = c->data;
245 PurpleProtocol *protocol = purple_connection_get_protocol(gc);
246 if (protocol != NULL && PURPLE_PROTOCOL_IMPLEMENTS(protocol, CHAT_IFACE, info))
247 return TRUE;
248 c = c->next;
251 return FALSE;
254 /**************************************************************************
255 * callbacks and signal handlers
256 **************************************************************************/
257 #if 0
258 static void
259 pidgin_quit_cb()
261 /* TODO: confirm quit while pending */
263 #endif
265 static void
266 docklet_update_status_cb(void *data)
268 docklet_update_status();
271 static void
272 docklet_conv_updated_cb(PurpleConversation *conv, PurpleConversationUpdateType type)
274 if (type == PURPLE_CONVERSATION_UPDATE_UNSEEN)
275 docklet_update_status();
278 static void
279 docklet_signed_on_cb(PurpleConnection *gc)
281 if (!enable_join_chat) {
282 if (PURPLE_PROTOCOL_IMPLEMENTS(purple_connection_get_protocol(gc), CHAT_IFACE, info))
283 enable_join_chat = TRUE;
285 docklet_update_status();
288 static void
289 docklet_signed_off_cb(PurpleConnection *gc)
291 if (enable_join_chat) {
292 if (PURPLE_PROTOCOL_IMPLEMENTS(purple_connection_get_protocol(gc), CHAT_IFACE, info))
293 enable_join_chat = online_account_supports_chat();
295 docklet_update_status();
298 static void
299 docklet_show_pref_changed_cb(const char *name, PurplePrefType type,
300 gconstpointer value, gpointer data)
302 const char *val = value;
303 if (purple_strequal(val, "always")) {
304 if (!visible)
305 docklet_gtk_status_create(FALSE);
306 else if (!visibility_manager) {
307 pidgin_blist_visibility_manager_add();
308 visibility_manager = TRUE;
310 } else if (purple_strequal(val, "never")) {
311 if (visible)
312 docklet_gtk_status_destroy();
313 } else {
314 if (visibility_manager) {
315 pidgin_blist_visibility_manager_remove();
316 visibility_manager = FALSE;
318 docklet_update_status();
322 /**************************************************************************
323 * docklet pop-up menu
324 **************************************************************************/
325 static void
326 docklet_toggle_mute(GtkWidget *toggle, void *data)
328 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/sound/mute",
329 gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(toggle)));
332 static void
333 docklet_toggle_blist(GtkWidget *toggle, void *data)
335 purple_blist_set_visible(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(toggle)));
338 #ifdef _WIN32
339 /* This is a workaround for a bug in windows GTK+. Clicking outside of the
340 menu does not get rid of it, so instead we get rid of it as soon as the
341 pointer leaves the menu. */
342 static gboolean
343 hide_docklet_menu(gpointer data)
345 if (data != NULL) {
346 gtk_menu_popdown(GTK_MENU(data));
348 return FALSE;
351 static gboolean
352 docklet_menu_leave_enter(GtkWidget *menu, GdkEventCrossing *event, void *data)
354 static guint hide_docklet_timer = 0;
356 if (event->type == GDK_LEAVE_NOTIFY && (event->detail == GDK_NOTIFY_ANCESTOR ||
357 event->detail == GDK_NOTIFY_UNKNOWN)) {
358 purple_debug(PURPLE_DEBUG_INFO, "docklet", "menu leave-notify-event\n");
359 /* Add some slop so that the menu doesn't annoyingly disappear when mousing around */
360 if (hide_docklet_timer == 0) {
361 hide_docklet_timer = g_timeout_add(500,
362 hide_docklet_menu, menu);
364 } else if (event->type == GDK_ENTER_NOTIFY && event->detail == GDK_NOTIFY_ANCESTOR) {
365 purple_debug(PURPLE_DEBUG_INFO, "docklet", "menu enter-notify-event\n");
366 if (hide_docklet_timer != 0) {
367 /* Cancel the hiding if we reenter */
369 g_source_remove(hide_docklet_timer);
370 hide_docklet_timer = 0;
373 return FALSE;
375 #endif
377 /* There is a lot of code here for handling the status submenu, much of
378 * which is duplicated from the gtkstatusbox. It'd be nice to add API
379 * somewhere to simplify this (either in the statusbox, or in libpurple).
381 static void
382 show_custom_status_editor_cb(GtkMenuItem *menuitem, gpointer user_data)
384 PurpleSavedStatus *saved_status;
385 saved_status = purple_savedstatus_get_current();
387 if (purple_savedstatus_get_primitive_type(saved_status) == PURPLE_STATUS_AVAILABLE)
388 saved_status = purple_savedstatus_new(NULL, PURPLE_STATUS_AWAY);
390 pidgin_status_editor_show(FALSE,
391 purple_savedstatus_is_transient(saved_status) ? saved_status : NULL);
394 static PurpleSavedStatus *
395 create_transient_status(PurpleStatusPrimitive primitive, PurpleStatusType *status_type)
397 PurpleSavedStatus *saved_status = purple_savedstatus_new(NULL, primitive);
399 if(status_type != NULL) {
400 GList *tmp, *active_accts = purple_accounts_get_all_active();
401 for (tmp = active_accts; tmp != NULL; tmp = tmp->next) {
402 purple_savedstatus_set_substatus(saved_status,
403 (PurpleAccount*) tmp->data, status_type, NULL);
405 g_list_free(active_accts);
408 return saved_status;
411 static void
412 activate_status_account_cb(GtkMenuItem *menuitem, gpointer user_data)
414 PurpleStatusType *status_type;
415 PurpleStatusPrimitive primitive;
416 PurpleSavedStatus *saved_status = NULL;
417 GList *iter = purple_savedstatuses_get_all();
418 GList *tmp, *active_accts = purple_accounts_get_all_active();
420 status_type = (PurpleStatusType *)user_data;
421 primitive = purple_status_type_get_primitive(status_type);
423 for (; iter != NULL; iter = iter->next) {
424 PurpleSavedStatus *ss = iter->data;
425 if ((purple_savedstatus_get_primitive_type(ss) == primitive) && purple_savedstatus_is_transient(ss) &&
426 purple_savedstatus_has_substatuses(ss))
428 gboolean found = FALSE;
429 /* The currently enabled accounts must have substatuses for all the active accts */
430 for(tmp = active_accts; tmp != NULL; tmp = tmp->next) {
431 PurpleAccount *acct = tmp->data;
432 PurpleSavedStatusSub *sub = purple_savedstatus_get_substatus(ss, acct);
433 if (sub) {
434 const PurpleStatusType *sub_type = purple_savedstatus_substatus_get_status_type(sub);
435 const char *subtype_status_id = purple_status_type_get_id(sub_type);
436 if (subtype_status_id && purple_strequal(subtype_status_id,
437 purple_status_type_get_id(status_type)))
438 found = TRUE;
441 if (!found)
442 continue;
443 saved_status = ss;
444 break;
448 g_list_free(active_accts);
450 /* Create a new transient saved status if we weren't able to find one */
451 if (saved_status == NULL)
452 saved_status = create_transient_status(primitive, status_type);
454 /* Set the status for each account */
455 purple_savedstatus_activate(saved_status);
458 static void
459 activate_status_primitive_cb(GtkMenuItem *menuitem, gpointer user_data)
461 PurpleStatusPrimitive primitive;
462 PurpleSavedStatus *saved_status;
464 primitive = GPOINTER_TO_INT(user_data);
466 /* Try to lookup an already existing transient saved status */
467 saved_status = purple_savedstatus_find_transient_by_type_and_message(primitive, NULL);
469 /* Create a new transient saved status if we weren't able to find one */
470 if (saved_status == NULL)
471 saved_status = create_transient_status(primitive, NULL);
473 /* Set the status for each account */
474 purple_savedstatus_activate(saved_status);
477 static void
478 activate_saved_status_cb(GtkMenuItem *menuitem, gpointer user_data)
480 time_t creation_time;
481 PurpleSavedStatus *saved_status;
483 creation_time = GPOINTER_TO_INT(user_data);
484 saved_status = purple_savedstatus_find_by_creation_time(creation_time);
485 if (saved_status != NULL)
486 purple_savedstatus_activate(saved_status);
489 static GtkWidget *
490 new_menu_item_with_status_icon(GtkWidget *menu, const char *str, PurpleStatusPrimitive primitive, GCallback cb, gpointer data, guint accel_key, guint accel_mods, char *mod)
492 GtkWidget *menuitem;
493 GdkPixbuf *pixbuf;
494 GtkWidget *image;
496 menuitem = gtk_image_menu_item_new_with_label(str);
498 if (menu)
499 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
501 if (cb)
502 g_signal_connect(G_OBJECT(menuitem), "activate", cb, data);
504 pixbuf = pidgin_create_status_icon(primitive, menu, PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL);
505 image = gtk_image_new_from_pixbuf(pixbuf);
506 g_object_unref(pixbuf);
507 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem), image);
509 gtk_widget_show_all(menuitem);
511 return menuitem;
514 static void
515 add_account_statuses(GtkWidget *menu, PurpleAccount *account)
517 GList *l;
519 for (l = purple_account_get_status_types(account); l != NULL; l = l->next) {
520 PurpleStatusType *status_type = (PurpleStatusType *)l->data;
521 PurpleStatusPrimitive prim;
523 if (!purple_status_type_is_user_settable(status_type))
524 continue;
526 prim = purple_status_type_get_primitive(status_type);
528 new_menu_item_with_status_icon(menu,
529 purple_status_type_get_name(status_type),
530 prim, G_CALLBACK(activate_status_account_cb),
531 GINT_TO_POINTER(status_type), 0, 0, NULL);
535 static GtkWidget *
536 docklet_status_submenu(void)
538 GtkWidget *submenu, *menuitem;
539 GList *popular_statuses, *cur;
540 PidginStatusBox *statusbox = NULL;
542 submenu = gtk_menu_new();
543 menuitem = gtk_menu_item_new_with_mnemonic(_("_Change Status"));
544 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
546 if(pidgin_blist_get_default_gtk_blist() != NULL) {
547 statusbox = PIDGIN_STATUS_BOX(pidgin_blist_get_default_gtk_blist()->statusbox);
550 if(statusbox && statusbox->account != NULL) {
551 add_account_statuses(submenu, statusbox->account);
552 } else if(statusbox && statusbox->token_status_account != NULL) {
553 add_account_statuses(submenu, statusbox->token_status_account);
554 } else {
555 new_menu_item_with_status_icon(submenu, _("Available"),
556 PURPLE_STATUS_AVAILABLE, G_CALLBACK(activate_status_primitive_cb),
557 GINT_TO_POINTER(PURPLE_STATUS_AVAILABLE), 0, 0, NULL);
559 new_menu_item_with_status_icon(submenu, _("Away"),
560 PURPLE_STATUS_AWAY, G_CALLBACK(activate_status_primitive_cb),
561 GINT_TO_POINTER(PURPLE_STATUS_AWAY), 0, 0, NULL);
563 new_menu_item_with_status_icon(submenu, _("Do not disturb"),
564 PURPLE_STATUS_UNAVAILABLE, G_CALLBACK(activate_status_primitive_cb),
565 GINT_TO_POINTER(PURPLE_STATUS_UNAVAILABLE), 0, 0, NULL);
567 new_menu_item_with_status_icon(submenu, _("Invisible"),
568 PURPLE_STATUS_INVISIBLE, G_CALLBACK(activate_status_primitive_cb),
569 GINT_TO_POINTER(PURPLE_STATUS_INVISIBLE), 0, 0, NULL);
571 new_menu_item_with_status_icon(submenu, _("Offline"),
572 PURPLE_STATUS_OFFLINE, G_CALLBACK(activate_status_primitive_cb),
573 GINT_TO_POINTER(PURPLE_STATUS_OFFLINE), 0, 0, NULL);
576 popular_statuses = purple_savedstatuses_get_popular(6);
577 if (popular_statuses != NULL)
578 pidgin_separator(submenu);
579 for (cur = popular_statuses; cur != NULL; cur = cur->next)
581 PurpleSavedStatus *saved_status = cur->data;
582 time_t creation_time = purple_savedstatus_get_creation_time(saved_status);
583 new_menu_item_with_status_icon(submenu,
584 purple_savedstatus_get_title(saved_status),
585 purple_savedstatus_get_primitive_type(saved_status), G_CALLBACK(activate_saved_status_cb),
586 GINT_TO_POINTER(creation_time), 0, 0, NULL);
588 g_list_free(popular_statuses);
590 pidgin_separator(submenu);
592 pidgin_new_menu_item(submenu, _("New..."), NULL,
593 G_CALLBACK(show_custom_status_editor_cb), NULL);
594 pidgin_new_menu_item(submenu, _("Saved..."), NULL,
595 G_CALLBACK(pidgin_status_window_show), NULL);
597 return menuitem;
601 static void
602 plugin_act(GtkWidget *widget, PurplePluginAction *pam)
604 if (pam && pam->callback)
605 pam->callback(pam);
608 static void
609 build_plugin_actions(GtkWidget *menu, PurplePlugin *plugin)
611 GtkWidget *menuitem;
612 PurplePluginActionsCb actions_cb;
613 PurplePluginAction *action = NULL;
614 GList *actions, *l;
616 actions_cb =
617 purple_plugin_info_get_actions_cb(purple_plugin_get_info(plugin));
618 actions = actions_cb(plugin);
620 for (l = actions; l != NULL; l = l->next)
622 if (l->data)
624 action = (PurplePluginAction *) l->data;
625 action->plugin = plugin;
627 menuitem = gtk_menu_item_new_with_label(action->label);
628 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
630 g_signal_connect(G_OBJECT(menuitem), "activate",
631 G_CALLBACK(plugin_act), action);
632 g_object_set_data_full(G_OBJECT(menuitem), "plugin_action",
633 action,
634 (GDestroyNotify)purple_plugin_action_free);
635 gtk_widget_show(menuitem);
637 else
638 pidgin_separator(menu);
641 g_list_free(actions);
645 static void
646 docklet_plugin_actions(GtkWidget *menu)
648 GtkWidget *menuitem, *submenu;
649 PurplePlugin *plugin = NULL;
650 PurplePluginInfo *info;
651 GList *l;
652 int c = 0;
654 g_return_if_fail(menu != NULL);
656 /* Add a submenu for each plugin with custom actions */
657 for (l = purple_plugins_get_loaded(); l; l = l->next) {
658 plugin = PURPLE_PLUGIN(l->data);
659 info = purple_plugin_get_info(plugin);
661 if (!purple_plugin_info_get_actions_cb(info))
662 continue;
664 menuitem =
665 gtk_image_menu_item_new_with_label(_(purple_plugin_info_get_name(info)));
666 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
668 submenu = gtk_menu_new();
669 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
671 build_plugin_actions(submenu, plugin);
673 c++;
675 if(c>0)
676 pidgin_separator(menu);
679 static void
680 docklet_menu(void)
682 static GtkWidget *menu = NULL;
683 GtkWidget *menuitem;
685 if (menu) {
686 gtk_widget_destroy(menu);
689 menu = gtk_menu_new();
691 menuitem = gtk_check_menu_item_new_with_mnemonic(_("Show Buddy _List"));
692 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/list_visible"));
693 g_signal_connect(G_OBJECT(menuitem), "toggled", G_CALLBACK(docklet_toggle_blist), NULL);
694 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
696 menuitem = gtk_menu_item_new_with_mnemonic(_("_Unread Messages"));
698 if (flags & PIDGIN_DOCKLET_CONV_PENDING) {
699 GtkWidget *submenu = gtk_menu_new();
700 GList *l = get_pending_list(0);
701 if (l == NULL) {
702 gtk_widget_set_sensitive(menuitem, FALSE);
703 purple_debug_warning("docklet",
704 "status indicates messages pending, but no conversations with unseen messages were found.");
705 } else {
706 pidgin_conversations_fill_menu(submenu, l);
707 g_list_free(l);
708 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
710 } else {
711 gtk_widget_set_sensitive(menuitem, FALSE);
713 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
715 pidgin_separator(menu);
717 menuitem = pidgin_new_menu_item(menu, _("New _Message..."),
718 PIDGIN_STOCK_TOOLBAR_MESSAGE_NEW,
719 G_CALLBACK(pidgin_dialogs_im), NULL);
720 if (status == PURPLE_STATUS_OFFLINE)
721 gtk_widget_set_sensitive(menuitem, FALSE);
723 menuitem = pidgin_new_menu_item(menu, _("Join Chat..."),
724 PIDGIN_STOCK_CHAT, G_CALLBACK(pidgin_blist_joinchat_show),
725 NULL);
726 if (status == PURPLE_STATUS_OFFLINE)
727 gtk_widget_set_sensitive(menuitem, FALSE);
729 menuitem = docklet_status_submenu();
730 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
732 pidgin_separator(menu);
734 pidgin_new_menu_item(menu, _("_Accounts"), NULL,
735 G_CALLBACK(pidgin_accounts_window_show), NULL);
736 pidgin_new_menu_item(menu, _("Plu_gins"),
737 PIDGIN_STOCK_TOOLBAR_PLUGINS,
738 G_CALLBACK(pidgin_plugin_dialog_show), NULL);
739 pidgin_new_menu_item(menu, _("Pr_eferences"),
740 GTK_STOCK_PREFERENCES,
741 G_CALLBACK(pidgin_prefs_show), NULL);
743 pidgin_separator(menu);
745 menuitem = gtk_check_menu_item_new_with_mnemonic(_("Mute _Sounds"));
746 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/sound/mute"));
747 if (purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/sound/method"), "none"))
748 gtk_widget_set_sensitive(GTK_WIDGET(menuitem), FALSE);
749 g_signal_connect(G_OBJECT(menuitem), "toggled", G_CALLBACK(docklet_toggle_mute), NULL);
750 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
752 pidgin_separator(menu);
754 /* add plugin actions */
755 docklet_plugin_actions(menu);
757 pidgin_new_menu_item(menu, _("_Quit"), GTK_STOCK_QUIT,
758 G_CALLBACK(purple_core_quit), NULL);
760 #ifdef _WIN32
761 g_signal_connect(menu, "leave-notify-event", G_CALLBACK(docklet_menu_leave_enter), NULL);
762 g_signal_connect(menu, "enter-notify-event", G_CALLBACK(docklet_menu_leave_enter), NULL);
763 #endif
764 gtk_widget_show_all(menu);
765 gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
768 static void
769 pidgin_docklet_clicked(int button_type)
771 switch (button_type) {
772 case 1:
773 if (flags & PIDGIN_DOCKLET_EMAIL_PENDING) {
774 pidgin_notify_emails_present(NULL);
775 } else if (flags & PIDGIN_DOCKLET_CONV_PENDING) {
776 GList *l = get_pending_list(1);
777 if (l != NULL) {
778 pidgin_conv_present_conversation((PurpleConversation *)l->data);
779 g_list_free(l);
781 } else {
782 pidgin_blist_toggle_visibility();
784 break;
785 case 3:
786 docklet_menu();
787 break;
791 static void
792 pidgin_docklet_embedded(void)
794 if (!visibility_manager
795 && !purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "pending")) {
796 pidgin_blist_visibility_manager_add();
797 visibility_manager = TRUE;
799 visible = TRUE;
800 docklet_update_status();
801 docklet_gtk_status_update_icon(status, flags);
804 static void
805 pidgin_docklet_remove(void)
807 if (visible) {
808 if (visibility_manager) {
809 pidgin_blist_visibility_manager_remove();
810 visibility_manager = FALSE;
812 visible = FALSE;
813 status = PURPLE_STATUS_OFFLINE;
817 #ifndef _WIN32
818 static gboolean
819 docklet_gtk_embed_timeout_cb(gpointer data)
821 /* The docklet was not embedded within the timeout.
822 * Remove it as a visibility manager, but leave the plugin
823 * loaded so that it can embed automatically if/when a notification
824 * area becomes available.
826 purple_debug_info("docklet", "failed to embed within timeout\n");
827 pidgin_docklet_remove();
828 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
830 embed_timeout = 0;
831 return FALSE;
833 #endif
835 static gboolean
836 docklet_gtk_embedded_cb(GtkWidget *widget, gpointer data)
838 if (embed_timeout) {
839 g_source_remove(embed_timeout);
840 embed_timeout = 0;
843 if (gtk_status_icon_is_embedded(docklet)) {
844 purple_debug_info("docklet", "embedded\n");
846 pidgin_docklet_embedded();
847 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", TRUE);
848 } else {
849 purple_debug_info("docklet", "detached\n");
851 pidgin_docklet_remove();
852 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
855 return TRUE;
858 static void
859 docklet_gtk_status_activated_cb(GtkStatusIcon *status_icon, gpointer user_data)
861 pidgin_docklet_clicked(1);
864 static void
865 docklet_gtk_status_clicked_cb(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data)
867 purple_debug_info("docklet", "The button is %u\n", button);
868 #ifdef GDK_WINDOWING_QUARTZ
869 /* You can only click left mouse button on MacOSX native GTK. Let that be the menu */
870 pidgin_docklet_clicked(3);
871 #else
872 pidgin_docklet_clicked(button);
873 #endif
876 static void
877 docklet_gtk_status_destroy(void)
879 g_return_if_fail(docklet != NULL);
881 pidgin_docklet_remove();
883 if (embed_timeout) {
884 g_source_remove(embed_timeout);
885 embed_timeout = 0;
888 gtk_status_icon_set_visible(docklet, FALSE);
889 g_object_unref(G_OBJECT(docklet));
890 docklet = NULL;
892 purple_debug_info("docklet", "GTK+ destroyed\n");
895 static void
896 docklet_gtk_status_create(gboolean recreate)
898 if (docklet) {
899 /* if this is being called when a tray icon exists, it's because
900 something messed up. try destroying it before we proceed,
901 although docklet_refcount may be all hosed. hopefully won't happen. */
902 purple_debug_warning("docklet", "trying to create icon but it already exists?\n");
903 docklet_gtk_status_destroy();
906 docklet = gtk_status_icon_new();
907 g_return_if_fail(docklet != NULL);
909 g_signal_connect(G_OBJECT(docklet), "activate", G_CALLBACK(docklet_gtk_status_activated_cb), NULL);
910 g_signal_connect(G_OBJECT(docklet), "popup-menu", G_CALLBACK(docklet_gtk_status_clicked_cb), NULL);
911 g_signal_connect(G_OBJECT(docklet), "notify::embedded", G_CALLBACK(docklet_gtk_embedded_cb), NULL);
913 gtk_status_icon_set_visible(docklet, TRUE);
915 /* This is a hack to avoid a race condition between the docklet getting
916 * embedded in the notification area and the gtkblist restoring its
917 * previous visibility state. If the docklet does not get embedded within
918 * the timeout, it will be removed as a visibility manager until it does
919 * get embedded. Ideally, we would only call docklet_embedded() when the
920 * icon was actually embedded. This only happens when the docklet is first
921 * created, not when being recreated.
923 * The gtk docklet tracks whether it successfully embedded in a pref and
924 * allows for a longer timeout period if it successfully embedded the last
925 * time it was run. This should hopefully solve problems with the buddy
926 * list not properly starting hidden when Pidgin is started on login.
928 if (!recreate) {
929 pidgin_docklet_embedded();
930 #ifndef _WIN32
931 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded")) {
932 embed_timeout = g_timeout_add_seconds(LONG_EMBED_TIMEOUT, docklet_gtk_embed_timeout_cb, NULL);
933 } else {
934 embed_timeout = g_timeout_add_seconds(SHORT_EMBED_TIMEOUT, docklet_gtk_embed_timeout_cb, NULL);
936 #endif
939 purple_debug_info("docklet", "GTK+ created\n");
942 /**************************************************************************
943 * public api
944 **************************************************************************/
946 void*
947 pidgin_docklet_get_handle()
949 static int i;
950 return &i;
953 GtkStatusIcon *
954 pidgin_docklet_get_status_icon(void)
956 return docklet;
959 void
960 pidgin_docklet_init()
962 void *conn_handle = purple_connections_get_handle();
963 void *conv_handle = purple_conversations_get_handle();
964 void *accounts_handle = purple_accounts_get_handle();
965 void *status_handle = purple_savedstatuses_get_handle();
966 void *docklet_handle = pidgin_docklet_get_handle();
967 void *notify_handle = purple_notify_get_handle();
969 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/docklet");
970 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/blink", FALSE);
971 purple_prefs_add_string(PIDGIN_PREFS_ROOT "/docklet/show", "always");
972 purple_prefs_connect_callback(docklet_handle, PIDGIN_PREFS_ROOT "/docklet/show",
973 docklet_show_pref_changed_cb, NULL);
974 purple_prefs_add_int(PIDGIN_PREFS_ROOT "/conversations/notification_chat", PIDGIN_UNSEEN_TEXT);
976 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/docklet/gtk");
977 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/docklet/x11/embedded")) {
978 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", TRUE);
979 purple_prefs_remove(PIDGIN_PREFS_ROOT "/docklet/x11/embedded");
980 } else {
981 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
984 if (purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "always"))
985 docklet_gtk_status_create(FALSE);
987 purple_signal_connect(conn_handle, "signed-on",
988 docklet_handle, PURPLE_CALLBACK(docklet_signed_on_cb), NULL);
989 purple_signal_connect(conn_handle, "signed-off",
990 docklet_handle, PURPLE_CALLBACK(docklet_signed_off_cb), NULL);
991 purple_signal_connect(accounts_handle, "account-connecting",
992 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
993 purple_signal_connect(conv_handle, "received-im-msg",
994 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
995 purple_signal_connect(conv_handle, "conversation-created",
996 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
997 purple_signal_connect(conv_handle, "deleting-conversation",
998 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
999 purple_signal_connect(conv_handle, "conversation-updated",
1000 docklet_handle, PURPLE_CALLBACK(docklet_conv_updated_cb), NULL);
1001 purple_signal_connect(status_handle, "savedstatus-changed",
1002 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1003 purple_signal_connect(notify_handle, "displaying-email-notification",
1004 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1005 purple_signal_connect(notify_handle, "displaying-emails-notification",
1006 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1007 purple_signal_connect(notify_handle, "displaying-emails-clear",
1008 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1009 #if 0
1010 purple_signal_connect(purple_get_core(), "quitting",
1011 docklet_handle, PURPLE_CALLBACK(purple_quit_cb), NULL);
1012 #endif
1014 enable_join_chat = online_account_supports_chat();
1017 void
1018 pidgin_docklet_uninit()
1020 if (visible)
1021 docklet_gtk_status_destroy();