mark PurpleImageClass as private
[pidgin-git.git] / pidgin / gtkdocklet.c
blobe3353e744d718455f31ecbb77ad39e62c29b1701
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, 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, 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, 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 = gtk_image_menu_item_new_with_label(
665 _(gplugin_plugin_info_get_name(
666 GPLUGIN_PLUGIN_INFO(info))));
667 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
669 submenu = gtk_menu_new();
670 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
672 build_plugin_actions(submenu, plugin);
674 c++;
676 if(c>0)
677 pidgin_separator(menu);
680 static void
681 docklet_menu(void)
683 static GtkWidget *menu = NULL;
684 GtkWidget *menuitem;
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 (purple_strequal(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 #endif
765 gtk_widget_show_all(menu);
766 gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
769 static void
770 pidgin_docklet_clicked(int button_type)
772 switch (button_type) {
773 case 1:
774 if (flags & PIDGIN_DOCKLET_EMAIL_PENDING) {
775 pidgin_notify_emails_present(NULL);
776 } else if (flags & PIDGIN_DOCKLET_CONV_PENDING) {
777 GList *l = get_pending_list(1);
778 if (l != NULL) {
779 pidgin_conv_present_conversation((PurpleConversation *)l->data);
780 g_list_free(l);
782 } else {
783 pidgin_blist_toggle_visibility();
785 break;
786 case 3:
787 docklet_menu();
788 break;
792 static void
793 pidgin_docklet_embedded(void)
795 if (!visibility_manager
796 && !purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "pending")) {
797 pidgin_blist_visibility_manager_add();
798 visibility_manager = TRUE;
800 visible = TRUE;
801 docklet_update_status();
802 docklet_gtk_status_update_icon(status, flags);
805 static void
806 pidgin_docklet_remove(void)
808 if (visible) {
809 if (visibility_manager) {
810 pidgin_blist_visibility_manager_remove();
811 visibility_manager = FALSE;
813 visible = FALSE;
814 status = PURPLE_STATUS_OFFLINE;
818 #ifndef _WIN32
819 static gboolean
820 docklet_gtk_embed_timeout_cb(gpointer data)
822 /* The docklet was not embedded within the timeout.
823 * Remove it as a visibility manager, but leave the plugin
824 * loaded so that it can embed automatically if/when a notification
825 * area becomes available.
827 purple_debug_info("docklet", "failed to embed within timeout\n");
828 pidgin_docklet_remove();
829 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
831 embed_timeout = 0;
832 return FALSE;
834 #endif
836 static gboolean
837 docklet_gtk_embedded_cb(GtkWidget *widget, gpointer data)
839 if (embed_timeout) {
840 g_source_remove(embed_timeout);
841 embed_timeout = 0;
844 if (gtk_status_icon_is_embedded(docklet)) {
845 purple_debug_info("docklet", "embedded\n");
847 pidgin_docklet_embedded();
848 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", TRUE);
849 } else {
850 purple_debug_info("docklet", "detached\n");
852 pidgin_docklet_remove();
853 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
856 return TRUE;
859 static void
860 docklet_gtk_status_activated_cb(GtkStatusIcon *status_icon, gpointer user_data)
862 pidgin_docklet_clicked(1);
865 static void
866 docklet_gtk_status_clicked_cb(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data)
868 purple_debug_info("docklet", "The button is %u\n", button);
869 #ifdef GDK_WINDOWING_QUARTZ
870 /* You can only click left mouse button on MacOSX native GTK. Let that be the menu */
871 pidgin_docklet_clicked(3);
872 #else
873 pidgin_docklet_clicked(button);
874 #endif
877 static void
878 docklet_gtk_status_destroy(void)
880 g_return_if_fail(docklet != NULL);
882 pidgin_docklet_remove();
884 if (embed_timeout) {
885 g_source_remove(embed_timeout);
886 embed_timeout = 0;
889 gtk_status_icon_set_visible(docklet, FALSE);
890 g_object_unref(G_OBJECT(docklet));
891 docklet = NULL;
893 purple_debug_info("docklet", "GTK+ destroyed\n");
896 static void
897 docklet_gtk_status_create(gboolean recreate)
899 if (docklet) {
900 /* if this is being called when a tray icon exists, it's because
901 something messed up. try destroying it before we proceed,
902 although docklet_refcount may be all hosed. hopefully won't happen. */
903 purple_debug_warning("docklet", "trying to create icon but it already exists?\n");
904 docklet_gtk_status_destroy();
907 docklet = gtk_status_icon_new();
908 g_return_if_fail(docklet != NULL);
910 g_signal_connect(G_OBJECT(docklet), "activate", G_CALLBACK(docklet_gtk_status_activated_cb), NULL);
911 g_signal_connect(G_OBJECT(docklet), "popup-menu", G_CALLBACK(docklet_gtk_status_clicked_cb), NULL);
912 g_signal_connect(G_OBJECT(docklet), "notify::embedded", G_CALLBACK(docklet_gtk_embedded_cb), NULL);
914 gtk_status_icon_set_visible(docklet, TRUE);
916 /* This is a hack to avoid a race condition between the docklet getting
917 * embedded in the notification area and the gtkblist restoring its
918 * previous visibility state. If the docklet does not get embedded within
919 * the timeout, it will be removed as a visibility manager until it does
920 * get embedded. Ideally, we would only call docklet_embedded() when the
921 * icon was actually embedded. This only happens when the docklet is first
922 * created, not when being recreated.
924 * The gtk docklet tracks whether it successfully embedded in a pref and
925 * allows for a longer timeout period if it successfully embedded the last
926 * time it was run. This should hopefully solve problems with the buddy
927 * list not properly starting hidden when Pidgin is started on login.
929 if (!recreate) {
930 pidgin_docklet_embedded();
931 #ifndef _WIN32
932 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded")) {
933 embed_timeout = g_timeout_add_seconds(LONG_EMBED_TIMEOUT, docklet_gtk_embed_timeout_cb, NULL);
934 } else {
935 embed_timeout = g_timeout_add_seconds(SHORT_EMBED_TIMEOUT, docklet_gtk_embed_timeout_cb, NULL);
937 #endif
940 purple_debug_info("docklet", "GTK+ created\n");
943 /**************************************************************************
944 * public api
945 **************************************************************************/
947 void*
948 pidgin_docklet_get_handle()
950 static int i;
951 return &i;
954 GtkStatusIcon *
955 pidgin_docklet_get_status_icon(void)
957 return docklet;
960 void
961 pidgin_docklet_init()
963 void *conn_handle = purple_connections_get_handle();
964 void *conv_handle = purple_conversations_get_handle();
965 void *accounts_handle = purple_accounts_get_handle();
966 void *status_handle = purple_savedstatuses_get_handle();
967 void *docklet_handle = pidgin_docklet_get_handle();
968 void *notify_handle = purple_notify_get_handle();
970 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/docklet");
971 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/blink", FALSE);
972 purple_prefs_add_string(PIDGIN_PREFS_ROOT "/docklet/show", "always");
973 purple_prefs_connect_callback(docklet_handle, PIDGIN_PREFS_ROOT "/docklet/show",
974 docklet_show_pref_changed_cb, NULL);
975 purple_prefs_add_int(PIDGIN_PREFS_ROOT "/conversations/notification_chat", PIDGIN_UNSEEN_TEXT);
977 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/docklet/gtk");
978 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/docklet/x11/embedded")) {
979 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", TRUE);
980 purple_prefs_remove(PIDGIN_PREFS_ROOT "/docklet/x11/embedded");
981 } else {
982 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
985 if (purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "always"))
986 docklet_gtk_status_create(FALSE);
988 purple_signal_connect(conn_handle, "signed-on",
989 docklet_handle, PURPLE_CALLBACK(docklet_signed_on_cb), NULL);
990 purple_signal_connect(conn_handle, "signed-off",
991 docklet_handle, PURPLE_CALLBACK(docklet_signed_off_cb), NULL);
992 purple_signal_connect(accounts_handle, "account-connecting",
993 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
994 purple_signal_connect(conv_handle, "received-im-msg",
995 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
996 purple_signal_connect(conv_handle, "conversation-created",
997 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
998 purple_signal_connect(conv_handle, "deleting-conversation",
999 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1000 purple_signal_connect(conv_handle, "conversation-updated",
1001 docklet_handle, PURPLE_CALLBACK(docklet_conv_updated_cb), NULL);
1002 purple_signal_connect(status_handle, "savedstatus-changed",
1003 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1004 purple_signal_connect(notify_handle, "displaying-email-notification",
1005 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1006 purple_signal_connect(notify_handle, "displaying-emails-notification",
1007 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1008 purple_signal_connect(notify_handle, "displaying-emails-clear",
1009 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1010 #if 0
1011 purple_signal_connect(purple_get_core(), "quitting",
1012 docklet_handle, PURPLE_CALLBACK(purple_quit_cb), NULL);
1013 #endif
1015 enable_join_chat = online_account_supports_chat();
1018 void
1019 pidgin_docklet_uninit()
1021 if (visible)
1022 docklet_gtk_status_destroy();