Merged pidgin/main into default
[pidgin-git.git] / pidgin / gtkdocklet.c
blob8d8b4b050594a6d7062dfca70a5fd07184cbc120
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 (!strcmp(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 (!strcmp(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 (!strcmp(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 = purple_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 purple_timeout_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 && !strcmp(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;
684 GtkMenuPositionFunc pos_func = gtk_status_icon_position_menu;
686 if (menu) {
687 gtk_widget_destroy(menu);
690 menu = gtk_menu_new();
692 menuitem = gtk_check_menu_item_new_with_mnemonic(_("Show Buddy _List"));
693 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/list_visible"));
694 g_signal_connect(G_OBJECT(menuitem), "toggled", G_CALLBACK(docklet_toggle_blist), NULL);
695 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
697 menuitem = gtk_menu_item_new_with_mnemonic(_("_Unread Messages"));
699 if (flags & PIDGIN_DOCKLET_CONV_PENDING) {
700 GtkWidget *submenu = gtk_menu_new();
701 GList *l = get_pending_list(0);
702 if (l == NULL) {
703 gtk_widget_set_sensitive(menuitem, FALSE);
704 purple_debug_warning("docklet",
705 "status indicates messages pending, but no conversations with unseen messages were found.");
706 } else {
707 pidgin_conversations_fill_menu(submenu, l);
708 g_list_free(l);
709 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
711 } else {
712 gtk_widget_set_sensitive(menuitem, FALSE);
714 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
716 pidgin_separator(menu);
718 menuitem = pidgin_new_menu_item(menu, _("New _Message..."),
719 PIDGIN_STOCK_TOOLBAR_MESSAGE_NEW,
720 G_CALLBACK(pidgin_dialogs_im), NULL);
721 if (status == PURPLE_STATUS_OFFLINE)
722 gtk_widget_set_sensitive(menuitem, FALSE);
724 menuitem = pidgin_new_menu_item(menu, _("Join Chat..."),
725 PIDGIN_STOCK_CHAT, G_CALLBACK(pidgin_blist_joinchat_show),
726 NULL);
727 if (status == PURPLE_STATUS_OFFLINE)
728 gtk_widget_set_sensitive(menuitem, FALSE);
730 menuitem = docklet_status_submenu();
731 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
733 pidgin_separator(menu);
735 pidgin_new_menu_item(menu, _("_Accounts"), NULL,
736 G_CALLBACK(pidgin_accounts_window_show), NULL);
737 pidgin_new_menu_item(menu, _("Plu_gins"),
738 PIDGIN_STOCK_TOOLBAR_PLUGINS,
739 G_CALLBACK(pidgin_plugin_dialog_show), NULL);
740 pidgin_new_menu_item(menu, _("Pr_eferences"),
741 GTK_STOCK_PREFERENCES,
742 G_CALLBACK(pidgin_prefs_show), NULL);
744 pidgin_separator(menu);
746 menuitem = gtk_check_menu_item_new_with_mnemonic(_("Mute _Sounds"));
747 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/sound/mute"));
748 if (!strcmp(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/sound/method"), "none"))
749 gtk_widget_set_sensitive(GTK_WIDGET(menuitem), FALSE);
750 g_signal_connect(G_OBJECT(menuitem), "toggled", G_CALLBACK(docklet_toggle_mute), NULL);
751 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
753 pidgin_separator(menu);
755 /* add plugin actions */
756 docklet_plugin_actions(menu);
758 pidgin_new_menu_item(menu, _("_Quit"), GTK_STOCK_QUIT,
759 G_CALLBACK(purple_core_quit), NULL);
761 #ifdef _WIN32
762 g_signal_connect(menu, "leave-notify-event", G_CALLBACK(docklet_menu_leave_enter), NULL);
763 g_signal_connect(menu, "enter-notify-event", G_CALLBACK(docklet_menu_leave_enter), NULL);
764 pos_func = NULL;
765 #endif
766 gtk_widget_show_all(menu);
767 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
768 pos_func,
769 docklet, 0, gtk_get_current_event_time());
772 static void
773 pidgin_docklet_clicked(int button_type)
775 switch (button_type) {
776 case 1:
777 if (flags & PIDGIN_DOCKLET_EMAIL_PENDING) {
778 pidgin_notify_emails_present(NULL);
779 } else if (flags & PIDGIN_DOCKLET_CONV_PENDING) {
780 GList *l = get_pending_list(1);
781 if (l != NULL) {
782 pidgin_conv_present_conversation((PurpleConversation *)l->data);
783 g_list_free(l);
785 } else {
786 pidgin_blist_toggle_visibility();
788 break;
789 case 3:
790 docklet_menu();
791 break;
795 static void
796 pidgin_docklet_embedded(void)
798 if (!visibility_manager
799 && strcmp(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "pending")) {
800 pidgin_blist_visibility_manager_add();
801 visibility_manager = TRUE;
803 visible = TRUE;
804 docklet_update_status();
805 docklet_gtk_status_update_icon(status, flags);
808 static void
809 pidgin_docklet_remove(void)
811 if (visible) {
812 if (visibility_manager) {
813 pidgin_blist_visibility_manager_remove();
814 visibility_manager = FALSE;
816 visible = FALSE;
817 status = PURPLE_STATUS_OFFLINE;
821 #ifndef _WIN32
822 static gboolean
823 docklet_gtk_embed_timeout_cb(gpointer data)
825 /* The docklet was not embedded within the timeout.
826 * Remove it as a visibility manager, but leave the plugin
827 * loaded so that it can embed automatically if/when a notification
828 * area becomes available.
830 purple_debug_info("docklet", "failed to embed within timeout\n");
831 pidgin_docklet_remove();
832 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
834 embed_timeout = 0;
835 return FALSE;
837 #endif
839 static gboolean
840 docklet_gtk_embedded_cb(GtkWidget *widget, gpointer data)
842 if (embed_timeout) {
843 purple_timeout_remove(embed_timeout);
844 embed_timeout = 0;
847 if (gtk_status_icon_is_embedded(docklet)) {
848 purple_debug_info("docklet", "embedded\n");
850 pidgin_docklet_embedded();
851 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", TRUE);
852 } else {
853 purple_debug_info("docklet", "detached\n");
855 pidgin_docklet_remove();
856 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
859 return TRUE;
862 static void
863 docklet_gtk_status_activated_cb(GtkStatusIcon *status_icon, gpointer user_data)
865 pidgin_docklet_clicked(1);
868 static void
869 docklet_gtk_status_clicked_cb(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data)
871 purple_debug_info("docklet", "The button is %u\n", button);
872 #ifdef GDK_WINDOWING_QUARTZ
873 /* You can only click left mouse button on MacOSX native GTK. Let that be the menu */
874 pidgin_docklet_clicked(3);
875 #else
876 pidgin_docklet_clicked(button);
877 #endif
880 static void
881 docklet_gtk_status_destroy(void)
883 g_return_if_fail(docklet != NULL);
885 pidgin_docklet_remove();
887 if (embed_timeout) {
888 purple_timeout_remove(embed_timeout);
889 embed_timeout = 0;
892 gtk_status_icon_set_visible(docklet, FALSE);
893 g_object_unref(G_OBJECT(docklet));
894 docklet = NULL;
896 purple_debug_info("docklet", "GTK+ destroyed\n");
899 static void
900 docklet_gtk_status_create(gboolean recreate)
902 if (docklet) {
903 /* if this is being called when a tray icon exists, it's because
904 something messed up. try destroying it before we proceed,
905 although docklet_refcount may be all hosed. hopefully won't happen. */
906 purple_debug_warning("docklet", "trying to create icon but it already exists?\n");
907 docklet_gtk_status_destroy();
910 docklet = gtk_status_icon_new();
911 g_return_if_fail(docklet != NULL);
913 g_signal_connect(G_OBJECT(docklet), "activate", G_CALLBACK(docklet_gtk_status_activated_cb), NULL);
914 g_signal_connect(G_OBJECT(docklet), "popup-menu", G_CALLBACK(docklet_gtk_status_clicked_cb), NULL);
915 g_signal_connect(G_OBJECT(docklet), "notify::embedded", G_CALLBACK(docklet_gtk_embedded_cb), NULL);
917 gtk_status_icon_set_visible(docklet, TRUE);
919 /* This is a hack to avoid a race condition between the docklet getting
920 * embedded in the notification area and the gtkblist restoring its
921 * previous visibility state. If the docklet does not get embedded within
922 * the timeout, it will be removed as a visibility manager until it does
923 * get embedded. Ideally, we would only call docklet_embedded() when the
924 * icon was actually embedded. This only happens when the docklet is first
925 * created, not when being recreated.
927 * The gtk docklet tracks whether it successfully embedded in a pref and
928 * allows for a longer timeout period if it successfully embedded the last
929 * time it was run. This should hopefully solve problems with the buddy
930 * list not properly starting hidden when Pidgin is started on login.
932 if (!recreate) {
933 pidgin_docklet_embedded();
934 #ifndef _WIN32
935 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded")) {
936 embed_timeout = purple_timeout_add_seconds(LONG_EMBED_TIMEOUT, docklet_gtk_embed_timeout_cb, NULL);
937 } else {
938 embed_timeout = purple_timeout_add_seconds(SHORT_EMBED_TIMEOUT, docklet_gtk_embed_timeout_cb, NULL);
940 #endif
943 purple_debug_info("docklet", "GTK+ created\n");
946 /**************************************************************************
947 * public api
948 **************************************************************************/
950 void*
951 pidgin_docklet_get_handle()
953 static int i;
954 return &i;
957 GtkStatusIcon *
958 pidgin_docklet_get_status_icon(void)
960 return docklet;
963 void
964 pidgin_docklet_init()
966 void *conn_handle = purple_connections_get_handle();
967 void *conv_handle = purple_conversations_get_handle();
968 void *accounts_handle = purple_accounts_get_handle();
969 void *status_handle = purple_savedstatuses_get_handle();
970 void *docklet_handle = pidgin_docklet_get_handle();
971 void *notify_handle = purple_notify_get_handle();
972 gchar *tmp;
974 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/docklet");
975 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/blink", FALSE);
976 purple_prefs_add_string(PIDGIN_PREFS_ROOT "/docklet/show", "always");
977 purple_prefs_connect_callback(docklet_handle, PIDGIN_PREFS_ROOT "/docklet/show",
978 docklet_show_pref_changed_cb, NULL);
979 purple_prefs_add_int(PIDGIN_PREFS_ROOT "/conversations/notification_chat", PIDGIN_UNSEEN_TEXT);
981 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/docklet/gtk");
982 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/docklet/x11/embedded")) {
983 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", TRUE);
984 purple_prefs_remove(PIDGIN_PREFS_ROOT "/docklet/x11/embedded");
985 } else {
986 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
989 tmp = g_build_path(G_DIR_SEPARATOR_S, PURPLE_DATADIR,
990 "pixmaps", "pidgin", "tray", NULL);
991 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(), tmp);
992 g_free(tmp);
994 if (!strcmp(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "always"))
995 docklet_gtk_status_create(FALSE);
997 purple_signal_connect(conn_handle, "signed-on",
998 docklet_handle, PURPLE_CALLBACK(docklet_signed_on_cb), NULL);
999 purple_signal_connect(conn_handle, "signed-off",
1000 docklet_handle, PURPLE_CALLBACK(docklet_signed_off_cb), NULL);
1001 purple_signal_connect(accounts_handle, "account-connecting",
1002 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1003 purple_signal_connect(conv_handle, "received-im-msg",
1004 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1005 purple_signal_connect(conv_handle, "conversation-created",
1006 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1007 purple_signal_connect(conv_handle, "deleting-conversation",
1008 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1009 purple_signal_connect(conv_handle, "conversation-updated",
1010 docklet_handle, PURPLE_CALLBACK(docklet_conv_updated_cb), NULL);
1011 purple_signal_connect(status_handle, "savedstatus-changed",
1012 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1013 purple_signal_connect(notify_handle, "displaying-email-notification",
1014 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1015 purple_signal_connect(notify_handle, "displaying-emails-notification",
1016 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1017 purple_signal_connect(notify_handle, "displaying-emails-clear",
1018 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1019 #if 0
1020 purple_signal_connect(purple_get_core(), "quitting",
1021 docklet_handle, PURPLE_CALLBACK(purple_quit_cb), NULL);
1022 #endif
1024 enable_join_chat = online_account_supports_chat();
1027 void
1028 pidgin_docklet_uninit()
1030 if (visible)
1031 docklet_gtk_status_destroy();