Minor changelog updates
[pidgin-git.git] / pidgin / gtkdocklet-x11.c
blob9d3391619c5283f47d7e7f893df3a91328261cb0
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>
8 *
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.
25 #include "internal.h"
26 #include "pidgin.h"
27 #include "debug.h"
28 #include "prefs.h"
29 #include "pidginstock.h"
31 #include "gtkdialogs.h"
33 #include "eggtrayicon.h"
34 #include "gtkdocklet.h"
36 #define SHORT_EMBED_TIMEOUT 5000
37 #define LONG_EMBED_TIMEOUT 15000
39 /* globals */
40 static EggTrayIcon *docklet = NULL;
41 static GtkWidget *image = NULL;
42 static GtkTooltips *tooltips = NULL;
43 static GdkPixbuf *blank_icon = NULL;
44 static int embed_timeout = 0;
45 static int docklet_height = 0;
47 /* protos */
48 static void docklet_x11_create(gboolean);
50 static gboolean
51 docklet_x11_recreate_cb()
53 docklet_x11_create(TRUE);
55 return FALSE; /* for when we're called by the glib idle handler */
58 static void
59 docklet_x11_embedded_cb(GtkWidget *widget, void *data)
61 purple_debug(PURPLE_DEBUG_INFO, "docklet", "embedded\n");
63 g_source_remove(embed_timeout);
64 embed_timeout = 0;
65 pidgin_docklet_embedded();
66 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/docklet/x11/embedded", FALSE);
69 static void
70 docklet_x11_destroyed_cb(GtkWidget *widget, void *data)
72 purple_debug(PURPLE_DEBUG_INFO, "docklet", "destroyed\n");
74 pidgin_docklet_remove();
76 g_object_unref(G_OBJECT(docklet));
77 docklet = NULL;
79 g_idle_add(docklet_x11_recreate_cb, NULL);
82 static void
83 docklet_x11_clicked_cb(GtkWidget *button, GdkEventButton *event, void *data)
85 if (event->type != GDK_BUTTON_RELEASE)
86 return;
88 pidgin_docklet_clicked(event->button);
91 static void
92 docklet_x11_update_icon(PurpleStatusPrimitive status, gboolean connecting, gboolean pending)
94 const gchar *icon_name = NULL;
96 g_return_if_fail(image != NULL);
98 switch (status) {
99 case PURPLE_STATUS_OFFLINE:
100 icon_name = PIDGIN_STOCK_TRAY_OFFLINE;
101 break;
102 case PURPLE_STATUS_AWAY:
103 icon_name = PIDGIN_STOCK_TRAY_AWAY;
104 break;
105 case PURPLE_STATUS_UNAVAILABLE:
106 icon_name = PIDGIN_STOCK_TRAY_BUSY;
107 break;
108 case PURPLE_STATUS_EXTENDED_AWAY:
109 icon_name = PIDGIN_STOCK_TRAY_XA;
110 break;
111 case PURPLE_STATUS_INVISIBLE:
112 icon_name = PIDGIN_STOCK_TRAY_INVISIBLE;
113 break;
114 default:
115 icon_name = PIDGIN_STOCK_TRAY_AVAILABLE;
116 break;
119 if (pending)
120 icon_name = PIDGIN_STOCK_TRAY_PENDING;
121 if (connecting)
122 icon_name = PIDGIN_STOCK_TRAY_CONNECT;
124 if(icon_name) {
125 int icon_size;
126 if (docklet_height < 22)
127 icon_size = gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL);
128 else if (docklet_height < 32)
129 icon_size = gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_SMALL);
130 else if (docklet_height < 48)
131 icon_size = gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_MEDIUM);
132 else
133 icon_size = gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_LARGE);
135 gtk_image_set_from_stock(GTK_IMAGE(image), icon_name, icon_size);
139 static void
140 docklet_x11_resize_icon(GtkWidget *widget)
142 if (docklet_height == MIN(widget->allocation.height, widget->allocation.width))
143 return;
144 docklet_height = MIN(widget->allocation.height, widget->allocation.width);
145 pidgin_docklet_update_icon();
148 static void
149 docklet_x11_blank_icon()
151 if (!blank_icon) {
152 GtkIconSize size = GTK_ICON_SIZE_LARGE_TOOLBAR;
153 gint width, height;
154 g_object_get(G_OBJECT(image), "icon-size", &size, NULL);
155 gtk_icon_size_lookup(size, &width, &height);
156 blank_icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
157 gdk_pixbuf_fill(blank_icon, 0);
160 gtk_image_set_from_pixbuf(GTK_IMAGE(image), blank_icon);
163 static void
164 docklet_x11_set_tooltip(gchar *tooltip)
166 if (!tooltips)
167 tooltips = gtk_tooltips_new();
169 /* image->parent is a GtkEventBox */
170 if (tooltip) {
171 gtk_tooltips_enable(tooltips);
172 gtk_tooltips_set_tip(tooltips, image->parent, tooltip, NULL);
173 } else {
174 gtk_tooltips_set_tip(tooltips, image->parent, "", NULL);
175 gtk_tooltips_disable(tooltips);
179 #if GTK_CHECK_VERSION(2,2,0)
180 static void
181 docklet_x11_position_menu(GtkMenu *menu, int *x, int *y, gboolean *push_in,
182 gpointer user_data)
184 GtkWidget *widget = GTK_WIDGET(docklet);
185 GtkRequisition req;
186 gint menu_xpos, menu_ypos;
188 gtk_widget_size_request(GTK_WIDGET(menu), &req);
189 gdk_window_get_origin(widget->window, &menu_xpos, &menu_ypos);
191 menu_xpos += widget->allocation.x;
192 menu_ypos += widget->allocation.y;
194 if (menu_ypos > gdk_screen_get_height(gtk_widget_get_screen(widget)) / 2)
195 menu_ypos -= req.height;
196 else
197 menu_ypos += widget->allocation.height;
199 *x = menu_xpos;
200 *y = menu_ypos;
202 *push_in = TRUE;
204 #endif
206 static void
207 docklet_x11_destroy()
209 g_return_if_fail(docklet != NULL);
211 if (embed_timeout)
212 g_source_remove(embed_timeout);
214 pidgin_docklet_remove();
216 g_signal_handlers_disconnect_by_func(G_OBJECT(docklet), G_CALLBACK(docklet_x11_destroyed_cb), NULL);
217 gtk_widget_destroy(GTK_WIDGET(docklet));
219 g_object_unref(G_OBJECT(docklet));
220 docklet = NULL;
222 if (blank_icon)
223 g_object_unref(G_OBJECT(blank_icon));
224 blank_icon = NULL;
226 image = NULL;
228 purple_debug(PURPLE_DEBUG_INFO, "docklet", "destroyed\n");
231 static gboolean
232 docklet_x11_embed_timeout_cb()
234 /* The docklet was not embedded within the timeout.
235 * Remove it as a visibility manager, but leave the plugin
236 * loaded so that it can embed automatically if/when a notification
237 * area becomes available.
239 purple_debug_info("docklet", "failed to embed within timeout\n");
240 pidgin_docklet_remove();
242 return FALSE;
245 static void
246 docklet_x11_create(gboolean recreate)
248 GtkWidget *box;
250 if (docklet) {
251 /* if this is being called when a tray icon exists, it's because
252 something messed up. try destroying it before we proceed,
253 although docklet_refcount may be all hosed. hopefully won't happen. */
254 purple_debug(PURPLE_DEBUG_WARNING, "docklet", "trying to create icon but it already exists?\n");
255 docklet_x11_destroy();
258 docklet = egg_tray_icon_new(PIDGIN_NAME);
259 box = gtk_event_box_new();
260 image = gtk_image_new();
262 g_signal_connect(G_OBJECT(docklet), "embedded", G_CALLBACK(docklet_x11_embedded_cb), NULL);
263 g_signal_connect(G_OBJECT(docklet), "destroy", G_CALLBACK(docklet_x11_destroyed_cb), NULL);
264 g_signal_connect(G_OBJECT(docklet), "size-allocate", G_CALLBACK(docklet_x11_resize_icon), NULL);
265 g_signal_connect(G_OBJECT(box), "button-release-event", G_CALLBACK(docklet_x11_clicked_cb), NULL);
266 gtk_container_add(GTK_CONTAINER(box), image);
267 gtk_container_add(GTK_CONTAINER(docklet), box);
269 if (!gtk_check_version(2,4,0))
270 g_object_set(G_OBJECT(box), "visible-window", FALSE, NULL);
272 gtk_widget_show_all(GTK_WIDGET(docklet));
274 /* ref the docklet before we bandy it about the place */
275 g_object_ref(G_OBJECT(docklet));
277 /* This is a hack to avoid a race condition between the docklet getting
278 * embedded in the notification area and the gtkblist restoring its
279 * previous visibility state. If the docklet does not get embedded within
280 * the timeout, it will be removed as a visibility manager until it does
281 * get embedded. Ideally, we would only call docklet_embedded() when the
282 * icon was actually embedded. This only happens when the docklet is first
283 * created, not when being recreated.
285 * The x11 docklet tracks whether it successfully embedded in a pref and
286 * allows for a longer timeout period if it successfully embedded the last
287 * time it was run. This should hopefully solve problems with the buddy
288 * list not properly starting hidden when Pidgin is started on login.
290 if(!recreate) {
291 pidgin_docklet_embedded();
292 if(purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/docklet/x11/embedded")) {
293 embed_timeout = g_timeout_add(LONG_EMBED_TIMEOUT, docklet_x11_embed_timeout_cb, NULL);
294 } else {
295 embed_timeout = g_timeout_add(SHORT_EMBED_TIMEOUT, docklet_x11_embed_timeout_cb, NULL);
299 purple_debug(PURPLE_DEBUG_INFO, "docklet", "created\n");
302 static void
303 docklet_x11_create_ui_op()
305 docklet_x11_create(FALSE);
308 static struct docklet_ui_ops ui_ops =
310 docklet_x11_create_ui_op,
311 docklet_x11_destroy,
312 docklet_x11_update_icon,
313 docklet_x11_blank_icon,
314 docklet_x11_set_tooltip,
315 #if GTK_CHECK_VERSION(2,2,0)
316 docklet_x11_position_menu
317 #else
318 NULL
319 #endif
322 void
323 docklet_ui_init()
325 pidgin_docklet_set_ui_ops(&ui_ops);
326 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/docklet/x11");
327 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/docklet/x11/embedded", FALSE);