Re-arrange GG edisc file to avoid prototypes.
[pidgin-git.git] / pidgin / gtkdocklet.c
blob013ad96455ad08b7b4a65aba3f5c3bdd9b3d4a9a
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 gtk_status_icon_set_from_icon_name(docklet, icon_name);
110 static GList *
111 get_pending_list(guint max)
113 GList *l_im, *l_chat;
115 l_im = pidgin_conversations_get_unseen_ims(PIDGIN_UNSEEN_TEXT, FALSE, max);
117 /* Short circuit if we have our information already */
118 if (max == 1 && l_im != NULL)
119 return l_im;
121 l_chat = pidgin_conversations_get_unseen_chats(
122 purple_prefs_get_int(PIDGIN_PREFS_ROOT "/conversations/notification_chat"),
123 FALSE, max);
125 if (l_im != NULL && l_chat != NULL)
126 return g_list_concat(l_im, l_chat);
127 else if (l_im != NULL)
128 return l_im;
129 else
130 return l_chat;
133 static gboolean
134 docklet_update_status(void)
136 GList *convs, *l;
137 int count;
138 PurpleSavedStatus *saved_status;
139 PurpleStatusPrimitive newstatus = PURPLE_STATUS_OFFLINE;
140 PidginDockletFlag newflags = 0;
142 /* get the current savedstatus */
143 saved_status = purple_savedstatus_get_current();
145 /* determine if any ims have unseen messages */
146 convs = get_pending_list(DOCKLET_TOOLTIP_LINE_LIMIT);
148 if (purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "pending")) {
149 if (convs && !visible) {
150 g_list_free(convs);
151 docklet_gtk_status_create(FALSE);
152 return FALSE;
153 } else if (!convs && visible) {
154 docklet_gtk_status_destroy();
155 return FALSE;
159 if (!visible) {
160 g_list_free(convs);
161 return FALSE;
164 if (convs != NULL) {
165 /* set tooltip if messages are pending */
166 GString *tooltip_text = g_string_new("");
167 newflags |= PIDGIN_DOCKLET_CONV_PENDING;
169 for (l = convs, count = 0 ; l != NULL ; l = l->next, count++) {
170 PurpleConversation *conv = (PurpleConversation *)l->data;
171 PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
173 if (count == DOCKLET_TOOLTIP_LINE_LIMIT - 1) {
174 g_string_append(tooltip_text, _("Right-click for more unread messages...\n"));
175 } else if(gtkconv) {
176 g_string_append_printf(tooltip_text,
177 ngettext("%d unread message from %s\n", "%d unread messages from %s\n", gtkconv->unseen_count),
178 gtkconv->unseen_count,
179 purple_conversation_get_title(conv));
180 } else {
181 g_string_append_printf(tooltip_text,
182 ngettext("%d unread message from %s\n", "%d unread messages from %s\n",
183 GPOINTER_TO_INT(g_object_get_data(G_OBJECT(conv), "unseen-count"))),
184 GPOINTER_TO_INT(g_object_get_data(G_OBJECT(conv), "unseen-count")),
185 purple_conversation_get_title(conv));
189 /* get rid of the last newline */
190 if (tooltip_text->len > 0)
191 tooltip_text = g_string_truncate(tooltip_text, tooltip_text->len - 1);
193 gtk_status_icon_set_tooltip_text(docklet, tooltip_text->str);
195 g_string_free(tooltip_text, TRUE);
196 g_list_free(convs);
198 } else {
199 char *tooltip_text = g_strconcat(PIDGIN_NAME, " - ",
200 purple_savedstatus_get_title(saved_status), NULL);
201 gtk_status_icon_set_tooltip_text(docklet, tooltip_text);
202 g_free(tooltip_text);
205 for(l = purple_accounts_get_all(); l != NULL; l = l->next) {
207 PurpleAccount *account = (PurpleAccount*)l->data;
209 if (!purple_account_get_enabled(account, PIDGIN_UI))
210 continue;
212 if (purple_account_is_disconnected(account))
213 continue;
215 if (purple_account_is_connecting(account))
216 newflags |= PIDGIN_DOCKLET_CONNECTING;
218 if (pidgin_notify_emails_pending())
219 newflags |= PIDGIN_DOCKLET_EMAIL_PENDING;
222 newstatus = purple_savedstatus_get_primitive_type(saved_status);
224 /* update the icon if we changed status */
225 if (status != newstatus || flags != newflags) {
226 status = newstatus;
227 flags = newflags;
229 docklet_gtk_status_update_icon(status, flags);
232 return FALSE; /* for when we're called by the glib idle handler */
235 static gboolean
236 online_account_supports_chat(void)
238 GList *c = NULL;
239 c = purple_connections_get_all();
241 while(c != NULL) {
242 PurpleConnection *gc = c->data;
243 PurpleProtocol *protocol = purple_connection_get_protocol(gc);
244 if (protocol != NULL && PURPLE_PROTOCOL_IMPLEMENTS(protocol, CHAT, info))
245 return TRUE;
246 c = c->next;
249 return FALSE;
252 /**************************************************************************
253 * callbacks and signal handlers
254 **************************************************************************/
255 #if 0
256 static void
257 pidgin_quit_cb()
259 /* TODO: confirm quit while pending */
261 #endif
263 static void
264 docklet_update_status_cb(void *data)
266 docklet_update_status();
269 static void
270 docklet_conv_updated_cb(PurpleConversation *conv, PurpleConversationUpdateType type)
272 if (type == PURPLE_CONVERSATION_UPDATE_UNSEEN)
273 docklet_update_status();
276 static void
277 docklet_signed_on_cb(PurpleConnection *gc)
279 if (!enable_join_chat) {
280 if (PURPLE_PROTOCOL_IMPLEMENTS(purple_connection_get_protocol(gc), CHAT, info))
281 enable_join_chat = TRUE;
283 docklet_update_status();
286 static void
287 docklet_signed_off_cb(PurpleConnection *gc)
289 if (enable_join_chat) {
290 if (PURPLE_PROTOCOL_IMPLEMENTS(purple_connection_get_protocol(gc), CHAT, info))
291 enable_join_chat = online_account_supports_chat();
293 docklet_update_status();
296 static void
297 docklet_show_pref_changed_cb(const char *name, PurplePrefType type,
298 gconstpointer value, gpointer data)
300 const char *val = value;
301 if (purple_strequal(val, "always")) {
302 if (!visible)
303 docklet_gtk_status_create(FALSE);
304 else if (!visibility_manager) {
305 pidgin_blist_visibility_manager_add();
306 visibility_manager = TRUE;
308 } else if (purple_strequal(val, "never")) {
309 if (visible)
310 docklet_gtk_status_destroy();
311 } else {
312 if (visibility_manager) {
313 pidgin_blist_visibility_manager_remove();
314 visibility_manager = FALSE;
316 docklet_update_status();
320 /**************************************************************************
321 * docklet pop-up menu
322 **************************************************************************/
323 static void
324 docklet_toggle_mute(GtkWidget *toggle, void *data)
326 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/sound/mute",
327 gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(toggle)));
330 static void
331 docklet_toggle_blist(GtkWidget *toggle, void *data)
333 purple_blist_set_visible(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(toggle)));
336 #ifdef _WIN32
337 /* This is a workaround for a bug in windows GTK+. Clicking outside of the
338 menu does not get rid of it, so instead we get rid of it as soon as the
339 pointer leaves the menu. */
340 static gboolean
341 hide_docklet_menu(gpointer data)
343 if (data != NULL) {
344 gtk_menu_popdown(GTK_MENU(data));
346 return FALSE;
349 static gboolean
350 docklet_menu_leave_enter(GtkWidget *menu, GdkEventCrossing *event, void *data)
352 static guint hide_docklet_timer = 0;
354 if (event->type == GDK_LEAVE_NOTIFY && (event->detail == GDK_NOTIFY_ANCESTOR ||
355 event->detail == GDK_NOTIFY_UNKNOWN)) {
356 purple_debug(PURPLE_DEBUG_INFO, "docklet", "menu leave-notify-event\n");
357 /* Add some slop so that the menu doesn't annoyingly disappear when mousing around */
358 if (hide_docklet_timer == 0) {
359 hide_docklet_timer = g_timeout_add(500,
360 hide_docklet_menu, menu);
362 } else if (event->type == GDK_ENTER_NOTIFY && event->detail == GDK_NOTIFY_ANCESTOR) {
363 purple_debug(PURPLE_DEBUG_INFO, "docklet", "menu enter-notify-event\n");
364 if (hide_docklet_timer != 0) {
365 /* Cancel the hiding if we reenter */
367 g_source_remove(hide_docklet_timer);
368 hide_docklet_timer = 0;
371 return FALSE;
373 #endif
375 /* There is a lot of code here for handling the status submenu, much of
376 * which is duplicated from the gtkstatusbox. It'd be nice to add API
377 * somewhere to simplify this (either in the statusbox, or in libpurple).
379 static void
380 show_custom_status_editor_cb(GtkMenuItem *menuitem, gpointer user_data)
382 PurpleSavedStatus *saved_status;
383 saved_status = purple_savedstatus_get_current();
385 if (purple_savedstatus_get_primitive_type(saved_status) == PURPLE_STATUS_AVAILABLE)
386 saved_status = purple_savedstatus_new(NULL, PURPLE_STATUS_AWAY);
388 pidgin_status_editor_show(FALSE,
389 purple_savedstatus_is_transient(saved_status) ? saved_status : NULL);
392 static PurpleSavedStatus *
393 create_transient_status(PurpleStatusPrimitive primitive, PurpleStatusType *status_type)
395 PurpleSavedStatus *saved_status = purple_savedstatus_new(NULL, primitive);
397 if(status_type != NULL) {
398 GList *tmp, *active_accts = purple_accounts_get_all_active();
399 for (tmp = active_accts; tmp != NULL; tmp = tmp->next) {
400 purple_savedstatus_set_substatus(saved_status,
401 (PurpleAccount*) tmp->data, status_type, NULL);
403 g_list_free(active_accts);
406 return saved_status;
409 static void
410 activate_status_account_cb(GtkMenuItem *menuitem, gpointer user_data)
412 PurpleStatusType *status_type;
413 PurpleStatusPrimitive primitive;
414 PurpleSavedStatus *saved_status = NULL;
415 GList *iter = purple_savedstatuses_get_all();
416 GList *tmp, *active_accts = purple_accounts_get_all_active();
418 status_type = (PurpleStatusType *)user_data;
419 primitive = purple_status_type_get_primitive(status_type);
421 for (; iter != NULL; iter = iter->next) {
422 PurpleSavedStatus *ss = iter->data;
423 if ((purple_savedstatus_get_primitive_type(ss) == primitive) && purple_savedstatus_is_transient(ss) &&
424 purple_savedstatus_has_substatuses(ss))
426 gboolean found = FALSE;
427 /* The currently enabled accounts must have substatuses for all the active accts */
428 for(tmp = active_accts; tmp != NULL; tmp = tmp->next) {
429 PurpleAccount *acct = tmp->data;
430 PurpleSavedStatusSub *sub = purple_savedstatus_get_substatus(ss, acct);
431 if (sub) {
432 const PurpleStatusType *sub_type = purple_savedstatus_substatus_get_status_type(sub);
433 const char *subtype_status_id = purple_status_type_get_id(sub_type);
434 if (subtype_status_id && purple_strequal(subtype_status_id,
435 purple_status_type_get_id(status_type)))
436 found = TRUE;
439 if (!found)
440 continue;
441 saved_status = ss;
442 break;
446 g_list_free(active_accts);
448 /* Create a new transient saved status if we weren't able to find one */
449 if (saved_status == NULL)
450 saved_status = create_transient_status(primitive, status_type);
452 /* Set the status for each account */
453 purple_savedstatus_activate(saved_status);
456 static void
457 activate_status_primitive_cb(GtkMenuItem *menuitem, gpointer user_data)
459 PurpleStatusPrimitive primitive;
460 PurpleSavedStatus *saved_status;
462 primitive = GPOINTER_TO_INT(user_data);
464 /* Try to lookup an already existing transient saved status */
465 saved_status = purple_savedstatus_find_transient_by_type_and_message(primitive, NULL);
467 /* Create a new transient saved status if we weren't able to find one */
468 if (saved_status == NULL)
469 saved_status = create_transient_status(primitive, NULL);
471 /* Set the status for each account */
472 purple_savedstatus_activate(saved_status);
475 static void
476 activate_saved_status_cb(GtkMenuItem *menuitem, gpointer user_data)
478 time_t creation_time;
479 PurpleSavedStatus *saved_status;
481 creation_time = GPOINTER_TO_INT(user_data);
482 saved_status = purple_savedstatus_find_by_creation_time(creation_time);
483 if (saved_status != NULL)
484 purple_savedstatus_activate(saved_status);
487 static GtkWidget *
488 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)
490 GtkWidget *menuitem;
491 GdkPixbuf *pixbuf;
492 GtkWidget *image;
494 menuitem = gtk_image_menu_item_new_with_label(str);
496 if (menu)
497 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
499 if (cb)
500 g_signal_connect(G_OBJECT(menuitem), "activate", cb, data);
502 pixbuf = pidgin_create_status_icon(primitive, menu, PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL);
503 image = gtk_image_new_from_pixbuf(pixbuf);
504 g_object_unref(pixbuf);
505 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem), image);
507 gtk_widget_show_all(menuitem);
509 return menuitem;
512 static void
513 add_account_statuses(GtkWidget *menu, PurpleAccount *account)
515 GList *l;
517 for (l = purple_account_get_status_types(account); l != NULL; l = l->next) {
518 PurpleStatusType *status_type = (PurpleStatusType *)l->data;
519 PurpleStatusPrimitive prim;
521 if (!purple_status_type_is_user_settable(status_type))
522 continue;
524 prim = purple_status_type_get_primitive(status_type);
526 new_menu_item_with_status_icon(menu,
527 purple_status_type_get_name(status_type),
528 prim, G_CALLBACK(activate_status_account_cb),
529 GINT_TO_POINTER(status_type), 0, 0, NULL);
533 static GtkWidget *
534 docklet_status_submenu(void)
536 GtkWidget *submenu, *menuitem;
537 GList *popular_statuses, *cur;
538 PidginStatusBox *statusbox = NULL;
540 submenu = gtk_menu_new();
541 menuitem = gtk_menu_item_new_with_mnemonic(_("_Change Status"));
542 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
544 if(pidgin_blist_get_default_gtk_blist() != NULL) {
545 statusbox = PIDGIN_STATUS_BOX(pidgin_blist_get_default_gtk_blist()->statusbox);
548 if(statusbox && statusbox->account != NULL) {
549 add_account_statuses(submenu, statusbox->account);
550 } else if(statusbox && statusbox->token_status_account != NULL) {
551 add_account_statuses(submenu, statusbox->token_status_account);
552 } else {
553 new_menu_item_with_status_icon(submenu, _("Available"),
554 PURPLE_STATUS_AVAILABLE, G_CALLBACK(activate_status_primitive_cb),
555 GINT_TO_POINTER(PURPLE_STATUS_AVAILABLE), 0, 0, NULL);
557 new_menu_item_with_status_icon(submenu, _("Away"),
558 PURPLE_STATUS_AWAY, G_CALLBACK(activate_status_primitive_cb),
559 GINT_TO_POINTER(PURPLE_STATUS_AWAY), 0, 0, NULL);
561 new_menu_item_with_status_icon(submenu, _("Do not disturb"),
562 PURPLE_STATUS_UNAVAILABLE, G_CALLBACK(activate_status_primitive_cb),
563 GINT_TO_POINTER(PURPLE_STATUS_UNAVAILABLE), 0, 0, NULL);
565 new_menu_item_with_status_icon(submenu, _("Invisible"),
566 PURPLE_STATUS_INVISIBLE, G_CALLBACK(activate_status_primitive_cb),
567 GINT_TO_POINTER(PURPLE_STATUS_INVISIBLE), 0, 0, NULL);
569 new_menu_item_with_status_icon(submenu, _("Offline"),
570 PURPLE_STATUS_OFFLINE, G_CALLBACK(activate_status_primitive_cb),
571 GINT_TO_POINTER(PURPLE_STATUS_OFFLINE), 0, 0, NULL);
574 popular_statuses = purple_savedstatuses_get_popular(6);
575 if (popular_statuses != NULL)
576 pidgin_separator(submenu);
577 for (cur = popular_statuses; cur != NULL; cur = cur->next)
579 PurpleSavedStatus *saved_status = cur->data;
580 time_t creation_time = purple_savedstatus_get_creation_time(saved_status);
581 new_menu_item_with_status_icon(submenu,
582 purple_savedstatus_get_title(saved_status),
583 purple_savedstatus_get_primitive_type(saved_status), G_CALLBACK(activate_saved_status_cb),
584 GINT_TO_POINTER(creation_time), 0, 0, NULL);
586 g_list_free(popular_statuses);
588 pidgin_separator(submenu);
590 pidgin_new_menu_item(submenu, _("New..."), NULL,
591 G_CALLBACK(show_custom_status_editor_cb), NULL);
592 pidgin_new_menu_item(submenu, _("Saved..."), NULL,
593 G_CALLBACK(pidgin_status_window_show), NULL);
595 return menuitem;
599 static void
600 plugin_act(GtkWidget *widget, PurplePluginAction *pam)
602 if (pam && pam->callback)
603 pam->callback(pam);
606 static void
607 build_plugin_actions(GtkWidget *menu, PurplePlugin *plugin)
609 GtkWidget *menuitem;
610 PurplePluginActionsCb actions_cb;
611 PurplePluginAction *action = NULL;
612 GList *actions, *l;
614 actions_cb =
615 purple_plugin_info_get_actions_cb(purple_plugin_get_info(plugin));
616 actions = actions_cb(plugin);
618 for (l = actions; l != NULL; l = l->next)
620 if (l->data)
622 action = (PurplePluginAction *) l->data;
623 action->plugin = plugin;
625 menuitem = gtk_menu_item_new_with_label(action->label);
626 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
628 g_signal_connect(G_OBJECT(menuitem), "activate",
629 G_CALLBACK(plugin_act), action);
630 g_object_set_data_full(G_OBJECT(menuitem), "plugin_action",
631 action,
632 (GDestroyNotify)purple_plugin_action_free);
633 gtk_widget_show(menuitem);
635 else
636 pidgin_separator(menu);
639 g_list_free(actions);
643 static void
644 docklet_plugin_actions(GtkWidget *menu)
646 GtkWidget *menuitem, *submenu;
647 PurplePlugin *plugin = NULL;
648 PurplePluginInfo *info;
649 GList *l;
650 int c = 0;
652 g_return_if_fail(menu != NULL);
654 /* Add a submenu for each plugin with custom actions */
655 for (l = purple_plugins_get_loaded(); l; l = l->next) {
656 plugin = PURPLE_PLUGIN(l->data);
657 info = purple_plugin_get_info(plugin);
659 if (!purple_plugin_info_get_actions_cb(info))
660 continue;
662 menuitem = gtk_image_menu_item_new_with_label(
663 _(gplugin_plugin_info_get_name(
664 GPLUGIN_PLUGIN_INFO(info))));
665 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
667 submenu = gtk_menu_new();
668 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
670 build_plugin_actions(submenu, plugin);
672 c++;
674 if(c>0)
675 pidgin_separator(menu);
678 static void
679 docklet_menu(void)
681 static GtkWidget *menu = NULL;
682 GtkWidget *menuitem;
684 if (menu) {
685 gtk_widget_destroy(menu);
688 menu = gtk_menu_new();
690 menuitem = gtk_check_menu_item_new_with_mnemonic(_("Show Buddy _List"));
691 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/blist/list_visible"));
692 g_signal_connect(G_OBJECT(menuitem), "toggled", G_CALLBACK(docklet_toggle_blist), NULL);
693 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
695 menuitem = gtk_menu_item_new_with_mnemonic(_("_Unread Messages"));
697 if (flags & PIDGIN_DOCKLET_CONV_PENDING) {
698 GtkWidget *submenu = gtk_menu_new();
699 GList *l = get_pending_list(0);
700 if (l == NULL) {
701 gtk_widget_set_sensitive(menuitem, FALSE);
702 purple_debug_warning("docklet",
703 "status indicates messages pending, but no conversations with unseen messages were found.");
704 } else {
705 pidgin_conversations_fill_menu(submenu, l);
706 g_list_free(l);
707 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
709 } else {
710 gtk_widget_set_sensitive(menuitem, FALSE);
712 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
714 pidgin_separator(menu);
716 menuitem = pidgin_new_menu_item(menu, _("New _Message..."),
717 PIDGIN_STOCK_TOOLBAR_MESSAGE_NEW,
718 G_CALLBACK(pidgin_dialogs_im), NULL);
719 if (status == PURPLE_STATUS_OFFLINE)
720 gtk_widget_set_sensitive(menuitem, FALSE);
722 menuitem = pidgin_new_menu_item(menu, _("Join Chat..."),
723 PIDGIN_STOCK_CHAT, G_CALLBACK(pidgin_blist_joinchat_show),
724 NULL);
725 if (status == PURPLE_STATUS_OFFLINE)
726 gtk_widget_set_sensitive(menuitem, FALSE);
728 menuitem = docklet_status_submenu();
729 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
731 pidgin_separator(menu);
733 pidgin_new_menu_item(menu, _("_Accounts"), NULL,
734 G_CALLBACK(pidgin_accounts_window_show), NULL);
735 pidgin_new_menu_item(menu, _("Plu_gins"),
736 PIDGIN_STOCK_TOOLBAR_PLUGINS,
737 G_CALLBACK(pidgin_plugin_dialog_show), NULL);
738 pidgin_new_menu_item(menu, _("Pr_eferences"),
739 GTK_STOCK_PREFERENCES,
740 G_CALLBACK(pidgin_prefs_show), NULL);
742 pidgin_separator(menu);
744 menuitem = gtk_check_menu_item_new_with_mnemonic(_("Mute _Sounds"));
745 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/sound/mute"));
746 if (purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/sound/method"), "none"))
747 gtk_widget_set_sensitive(GTK_WIDGET(menuitem), FALSE);
748 g_signal_connect(G_OBJECT(menuitem), "toggled", G_CALLBACK(docklet_toggle_mute), NULL);
749 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
751 pidgin_separator(menu);
753 /* add plugin actions */
754 docklet_plugin_actions(menu);
756 pidgin_new_menu_item(menu, _("_Quit"), GTK_STOCK_QUIT,
757 G_CALLBACK(purple_core_quit), NULL);
759 #ifdef _WIN32
760 g_signal_connect(menu, "leave-notify-event", G_CALLBACK(docklet_menu_leave_enter), NULL);
761 g_signal_connect(menu, "enter-notify-event", G_CALLBACK(docklet_menu_leave_enter), NULL);
762 #endif
763 gtk_widget_show_all(menu);
764 gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
767 static void
768 pidgin_docklet_clicked(int button_type)
770 switch (button_type) {
771 case 1:
772 if (flags & PIDGIN_DOCKLET_EMAIL_PENDING) {
773 pidgin_notify_emails_present(NULL);
774 } else if (flags & PIDGIN_DOCKLET_CONV_PENDING) {
775 GList *l = get_pending_list(1);
776 if (l != NULL) {
777 pidgin_conv_present_conversation((PurpleConversation *)l->data);
778 g_list_free(l);
780 } else {
781 pidgin_blist_toggle_visibility();
783 break;
784 case 3:
785 docklet_menu();
786 break;
790 static void
791 pidgin_docklet_embedded(void)
793 if (!visibility_manager
794 && !purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "pending")) {
795 pidgin_blist_visibility_manager_add();
796 visibility_manager = TRUE;
798 visible = TRUE;
799 docklet_update_status();
800 docklet_gtk_status_update_icon(status, flags);
803 static void
804 pidgin_docklet_remove(void)
806 if (visible) {
807 if (visibility_manager) {
808 pidgin_blist_visibility_manager_remove();
809 visibility_manager = FALSE;
811 visible = FALSE;
812 status = PURPLE_STATUS_OFFLINE;
816 #ifndef _WIN32
817 static gboolean
818 docklet_gtk_embed_timeout_cb(gpointer data)
820 /* The docklet was not embedded within the timeout.
821 * Remove it as a visibility manager, but leave the plugin
822 * loaded so that it can embed automatically if/when a notification
823 * area becomes available.
825 purple_debug_info("docklet", "failed to embed within timeout\n");
826 pidgin_docklet_remove();
827 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
829 embed_timeout = 0;
830 return FALSE;
832 #endif
834 static gboolean
835 docklet_gtk_embedded_cb(GtkWidget *widget, gpointer data)
837 if (embed_timeout) {
838 g_source_remove(embed_timeout);
839 embed_timeout = 0;
842 if (gtk_status_icon_is_embedded(docklet)) {
843 purple_debug_info("docklet", "embedded\n");
845 pidgin_docklet_embedded();
846 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", TRUE);
847 } else {
848 purple_debug_info("docklet", "detached\n");
850 pidgin_docklet_remove();
851 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
854 return TRUE;
857 static void
858 docklet_gtk_status_activated_cb(GtkStatusIcon *status_icon, gpointer user_data)
860 pidgin_docklet_clicked(1);
863 static void
864 docklet_gtk_status_clicked_cb(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data)
866 purple_debug_info("docklet", "The button is %u\n", button);
867 #ifdef GDK_WINDOWING_QUARTZ
868 /* You can only click left mouse button on MacOSX native GTK. Let that be the menu */
869 pidgin_docklet_clicked(3);
870 #else
871 pidgin_docklet_clicked(button);
872 #endif
875 static void
876 docklet_gtk_status_destroy(void)
878 g_return_if_fail(docklet != NULL);
880 pidgin_docklet_remove();
882 if (embed_timeout) {
883 g_source_remove(embed_timeout);
884 embed_timeout = 0;
887 gtk_status_icon_set_visible(docklet, FALSE);
888 g_object_unref(G_OBJECT(docklet));
889 docklet = NULL;
891 purple_debug_info("docklet", "GTK+ destroyed\n");
894 static void
895 docklet_gtk_status_create(gboolean recreate)
897 if (docklet) {
898 /* if this is being called when a tray icon exists, it's because
899 something messed up. try destroying it before we proceed,
900 although docklet_refcount may be all hosed. hopefully won't happen. */
901 purple_debug_warning("docklet", "trying to create icon but it already exists?\n");
902 docklet_gtk_status_destroy();
905 docklet = gtk_status_icon_new();
906 g_return_if_fail(docklet != NULL);
908 g_signal_connect(G_OBJECT(docklet), "activate", G_CALLBACK(docklet_gtk_status_activated_cb), NULL);
909 g_signal_connect(G_OBJECT(docklet), "popup-menu", G_CALLBACK(docklet_gtk_status_clicked_cb), NULL);
910 g_signal_connect(G_OBJECT(docklet), "notify::embedded", G_CALLBACK(docklet_gtk_embedded_cb), NULL);
912 gtk_status_icon_set_visible(docklet, TRUE);
914 /* This is a hack to avoid a race condition between the docklet getting
915 * embedded in the notification area and the gtkblist restoring its
916 * previous visibility state. If the docklet does not get embedded within
917 * the timeout, it will be removed as a visibility manager until it does
918 * get embedded. Ideally, we would only call docklet_embedded() when the
919 * icon was actually embedded. This only happens when the docklet is first
920 * created, not when being recreated.
922 * The gtk docklet tracks whether it successfully embedded in a pref and
923 * allows for a longer timeout period if it successfully embedded the last
924 * time it was run. This should hopefully solve problems with the buddy
925 * list not properly starting hidden when Pidgin is started on login.
927 if (!recreate) {
928 pidgin_docklet_embedded();
929 #ifndef _WIN32
930 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded")) {
931 embed_timeout = g_timeout_add_seconds(LONG_EMBED_TIMEOUT, docklet_gtk_embed_timeout_cb, NULL);
932 } else {
933 embed_timeout = g_timeout_add_seconds(SHORT_EMBED_TIMEOUT, docklet_gtk_embed_timeout_cb, NULL);
935 #endif
938 purple_debug_info("docklet", "GTK+ created\n");
941 /**************************************************************************
942 * public api
943 **************************************************************************/
945 void*
946 pidgin_docklet_get_handle()
948 static int i;
949 return &i;
952 GtkStatusIcon *
953 pidgin_docklet_get_status_icon(void)
955 return docklet;
958 void
959 pidgin_docklet_init()
961 void *conn_handle = purple_connections_get_handle();
962 void *conv_handle = purple_conversations_get_handle();
963 void *accounts_handle = purple_accounts_get_handle();
964 void *status_handle = purple_savedstatuses_get_handle();
965 void *docklet_handle = pidgin_docklet_get_handle();
966 void *notify_handle = purple_notify_get_handle();
968 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/docklet");
969 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/blink", FALSE);
970 purple_prefs_add_string(PIDGIN_PREFS_ROOT "/docklet/show", "always");
971 purple_prefs_connect_callback(docklet_handle, PIDGIN_PREFS_ROOT "/docklet/show",
972 docklet_show_pref_changed_cb, NULL);
973 purple_prefs_add_int(PIDGIN_PREFS_ROOT "/conversations/notification_chat", PIDGIN_UNSEEN_TEXT);
975 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/docklet/gtk");
976 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/docklet/x11/embedded")) {
977 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", TRUE);
978 purple_prefs_remove(PIDGIN_PREFS_ROOT "/docklet/x11/embedded");
979 } else {
980 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/gtk/embedded", FALSE);
983 if (purple_strequal(purple_prefs_get_string(PIDGIN_PREFS_ROOT "/docklet/show"), "always"))
984 docklet_gtk_status_create(FALSE);
986 purple_signal_connect(conn_handle, "signed-on",
987 docklet_handle, PURPLE_CALLBACK(docklet_signed_on_cb), NULL);
988 purple_signal_connect(conn_handle, "signed-off",
989 docklet_handle, PURPLE_CALLBACK(docklet_signed_off_cb), NULL);
990 purple_signal_connect(accounts_handle, "account-connecting",
991 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
992 purple_signal_connect(conv_handle, "received-im-msg",
993 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
994 purple_signal_connect(conv_handle, "conversation-created",
995 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
996 purple_signal_connect(conv_handle, "deleting-conversation",
997 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
998 purple_signal_connect(conv_handle, "conversation-updated",
999 docklet_handle, PURPLE_CALLBACK(docklet_conv_updated_cb), NULL);
1000 purple_signal_connect(status_handle, "savedstatus-changed",
1001 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1002 purple_signal_connect(notify_handle, "displaying-email-notification",
1003 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1004 purple_signal_connect(notify_handle, "displaying-emails-notification",
1005 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1006 purple_signal_connect(notify_handle, "displaying-emails-clear",
1007 docklet_handle, PURPLE_CALLBACK(docklet_update_status_cb), NULL);
1008 #if 0
1009 purple_signal_connect(purple_get_core(), "quitting",
1010 docklet_handle, PURPLE_CALLBACK(purple_quit_cb), NULL);
1011 #endif
1013 enable_join_chat = online_account_supports_chat();
1016 void
1017 pidgin_docklet_uninit()
1019 if (visible)
1020 docklet_gtk_status_destroy();