| |
1 /* |
| |
2 * System tray icon (aka docklet) plugin for Purple |
| |
3 * |
| |
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. |
| |
13 * |
| |
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. |
| |
18 * |
| |
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., 59 Temple Place - Suite 330, Boston, MA |
| |
22 * 02111-1307, USA. |
| |
23 */ |
| |
24 |
| |
25 #include "internal.h" |
| |
26 #include "pidgin.h" |
| |
27 #include "debug.h" |
| |
28 #include "pidginstock.h" |
| |
29 |
| |
30 #include "gtkdialogs.h" |
| |
31 |
| |
32 #include "eggtrayicon.h" |
| |
33 #include "gtkdocklet.h" |
| |
34 |
| |
35 #define EMBED_TIMEOUT 5000 |
| |
36 |
| |
37 /* globals */ |
| |
38 static EggTrayIcon *docklet = NULL; |
| |
39 static GtkWidget *image = NULL; |
| |
40 static GtkTooltips *tooltips = NULL; |
| |
41 static GdkPixbuf *blank_icon = NULL; |
| |
42 static int embed_timeout = 0; |
| |
43 static DockletStatus icon_status = 0; |
| |
44 static int docklet_height = 0; |
| |
45 |
| |
46 /* protos */ |
| |
47 static void docklet_x11_create(void); |
| |
48 |
| |
49 static gboolean |
| |
50 docklet_x11_create_cb() |
| |
51 { |
| |
52 docklet_x11_create(); |
| |
53 |
| |
54 return FALSE; /* for when we're called by the glib idle handler */ |
| |
55 } |
| |
56 |
| |
57 static void |
| |
58 docklet_x11_embedded_cb(GtkWidget *widget, void *data) |
| |
59 { |
| |
60 purple_debug(PURPLE_DEBUG_INFO, "docklet", "embedded\n"); |
| |
61 |
| |
62 g_source_remove(embed_timeout); |
| |
63 embed_timeout = 0; |
| |
64 pidgin_docklet_embedded(); |
| |
65 } |
| |
66 |
| |
67 static void |
| |
68 docklet_x11_destroyed_cb(GtkWidget *widget, void *data) |
| |
69 { |
| |
70 purple_debug(PURPLE_DEBUG_INFO, "docklet", "destroyed\n"); |
| |
71 |
| |
72 pidgin_docklet_remove(); |
| |
73 |
| |
74 g_object_unref(G_OBJECT(docklet)); |
| |
75 docklet = NULL; |
| |
76 |
| |
77 g_idle_add(docklet_x11_create_cb, NULL); |
| |
78 } |
| |
79 |
| |
80 static void |
| |
81 docklet_x11_clicked_cb(GtkWidget *button, GdkEventButton *event, void *data) |
| |
82 { |
| |
83 if (event->type != GDK_BUTTON_RELEASE) |
| |
84 return; |
| |
85 |
| |
86 pidgin_docklet_clicked(event->button); |
| |
87 } |
| |
88 |
| |
89 static void |
| |
90 docklet_x11_update_icon(DockletStatus icon) |
| |
91 { |
| |
92 const gchar *icon_name = NULL; |
| |
93 |
| |
94 g_return_if_fail(image != NULL); |
| |
95 |
| |
96 switch (icon) { |
| |
97 case DOCKLET_STATUS_OFFLINE: |
| |
98 icon_name = PIDGIN_STOCK_TRAY_OFFLINE; |
| |
99 break; |
| |
100 case DOCKLET_STATUS_CONNECTING: |
| |
101 icon_name = PIDGIN_STOCK_TRAY_CONNECT; |
| |
102 break; |
| |
103 case DOCKLET_STATUS_AVAILABLE: |
| |
104 icon_name = PIDGIN_STOCK_TRAY_AVAILABLE; |
| |
105 break; |
| |
106 case DOCKLET_STATUS_PENDING: |
| |
107 icon_name = PIDGIN_STOCK_TRAY_PENDING; |
| |
108 break; |
| |
109 case DOCKLET_STATUS_AWAY: |
| |
110 icon_name = PIDGIN_STOCK_TRAY_AWAY; |
| |
111 break; |
| |
112 case DOCKLET_STATUS_BUSY: |
| |
113 icon_name = PIDGIN_STOCK_TRAY_BUSY; |
| |
114 break; |
| |
115 case DOCKLET_STATUS_XA: |
| |
116 icon_name = PIDGIN_STOCK_TRAY_XA; |
| |
117 break; |
| |
118 } |
| |
119 |
| |
120 if(icon_name) { |
| |
121 int icon_size; |
| |
122 if (docklet_height < 22) |
| |
123 icon_size = gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL); |
| |
124 else |
| |
125 icon_size = gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_SMALL); |
| |
126 |
| |
127 gtk_image_set_from_stock(GTK_IMAGE(image), icon_name, icon_size); |
| |
128 } |
| |
129 icon_status = icon; |
| |
130 } |
| |
131 |
| |
132 static void |
| |
133 docklet_x11_resize_icon(GtkWidget *widget) |
| |
134 { |
| |
135 if (docklet_height == widget->allocation.height) |
| |
136 return; |
| |
137 docklet_height = widget->allocation.height; |
| |
138 docklet_x11_update_icon(icon_status); |
| |
139 } |
| |
140 |
| |
141 static void |
| |
142 docklet_x11_blank_icon() |
| |
143 { |
| |
144 if (!blank_icon) { |
| |
145 gint width, height; |
| |
146 |
| |
147 gtk_icon_size_lookup(GTK_ICON_SIZE_LARGE_TOOLBAR, &width, &height); |
| |
148 blank_icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height); |
| |
149 gdk_pixbuf_fill(blank_icon, 0); |
| |
150 } |
| |
151 |
| |
152 gtk_image_set_from_pixbuf(GTK_IMAGE(image), blank_icon); |
| |
153 } |
| |
154 |
| |
155 static void |
| |
156 docklet_x11_set_tooltip(gchar *tooltip) |
| |
157 { |
| |
158 if (!tooltips) |
| |
159 tooltips = gtk_tooltips_new(); |
| |
160 |
| |
161 /* image->parent is a GtkEventBox */ |
| |
162 if (tooltip) { |
| |
163 gtk_tooltips_enable(tooltips); |
| |
164 gtk_tooltips_set_tip(tooltips, image->parent, tooltip, NULL); |
| |
165 } else { |
| |
166 gtk_tooltips_set_tip(tooltips, image->parent, "", NULL); |
| |
167 gtk_tooltips_disable(tooltips); |
| |
168 } |
| |
169 } |
| |
170 |
| |
171 #if GTK_CHECK_VERSION(2,2,0) |
| |
172 static void |
| |
173 docklet_x11_position_menu(GtkMenu *menu, int *x, int *y, gboolean *push_in, |
| |
174 gpointer user_data) |
| |
175 { |
| |
176 GtkWidget *widget = GTK_WIDGET(docklet); |
| |
177 GtkRequisition req; |
| |
178 gint menu_xpos, menu_ypos; |
| |
179 |
| |
180 gtk_widget_size_request(GTK_WIDGET(menu), &req); |
| |
181 gdk_window_get_origin(widget->window, &menu_xpos, &menu_ypos); |
| |
182 |
| |
183 menu_xpos += widget->allocation.x; |
| |
184 menu_ypos += widget->allocation.y; |
| |
185 |
| |
186 if (menu_ypos > gdk_screen_get_height(gtk_widget_get_screen(widget)) / 2) |
| |
187 menu_ypos -= req.height; |
| |
188 else |
| |
189 menu_ypos += widget->allocation.height; |
| |
190 |
| |
191 *x = menu_xpos; |
| |
192 *y = menu_ypos; |
| |
193 |
| |
194 *push_in = TRUE; |
| |
195 } |
| |
196 #endif |
| |
197 |
| |
198 static void |
| |
199 docklet_x11_destroy() |
| |
200 { |
| |
201 g_return_if_fail(docklet != NULL); |
| |
202 |
| |
203 if (embed_timeout) |
| |
204 g_source_remove(embed_timeout); |
| |
205 |
| |
206 pidgin_docklet_remove(); |
| |
207 |
| |
208 g_signal_handlers_disconnect_by_func(G_OBJECT(docklet), G_CALLBACK(docklet_x11_destroyed_cb), NULL); |
| |
209 gtk_widget_destroy(GTK_WIDGET(docklet)); |
| |
210 |
| |
211 g_object_unref(G_OBJECT(docklet)); |
| |
212 docklet = NULL; |
| |
213 |
| |
214 if (blank_icon) |
| |
215 g_object_unref(G_OBJECT(blank_icon)); |
| |
216 blank_icon = NULL; |
| |
217 |
| |
218 image = NULL; |
| |
219 |
| |
220 purple_debug(PURPLE_DEBUG_INFO, "docklet", "destroyed\n"); |
| |
221 } |
| |
222 |
| |
223 static gboolean |
| |
224 docklet_x11_embed_timeout_cb() |
| |
225 { |
| |
226 /* The docklet was not embedded within the timeout. |
| |
227 * Remove it as a visibility manager, but leave the plugin |
| |
228 * loaded so that it can embed automatically if/when a notification |
| |
229 * area becomes available. |
| |
230 */ |
| |
231 purple_debug_info("docklet", "failed to embed within timeout\n"); |
| |
232 pidgin_docklet_remove(); |
| |
233 |
| |
234 return FALSE; |
| |
235 } |
| |
236 |
| |
237 static void |
| |
238 docklet_x11_create() |
| |
239 { |
| |
240 GtkWidget *box; |
| |
241 |
| |
242 if (docklet) { |
| |
243 /* if this is being called when a tray icon exists, it's because |
| |
244 something messed up. try destroying it before we proceed, |
| |
245 although docklet_refcount may be all hosed. hopefully won't happen. */ |
| |
246 purple_debug(PURPLE_DEBUG_WARNING, "docklet", "trying to create icon but it already exists?\n"); |
| |
247 docklet_x11_destroy(); |
| |
248 } |
| |
249 |
| |
250 docklet = egg_tray_icon_new(PIDGIN_NAME); |
| |
251 box = gtk_event_box_new(); |
| |
252 image = gtk_image_new(); |
| |
253 |
| |
254 g_signal_connect(G_OBJECT(docklet), "embedded", G_CALLBACK(docklet_x11_embedded_cb), NULL); |
| |
255 g_signal_connect(G_OBJECT(docklet), "destroy", G_CALLBACK(docklet_x11_destroyed_cb), NULL); |
| |
256 g_signal_connect(G_OBJECT(docklet), "size-allocate", G_CALLBACK(docklet_x11_resize_icon), NULL); |
| |
257 g_signal_connect(G_OBJECT(box), "button-release-event", G_CALLBACK(docklet_x11_clicked_cb), NULL); |
| |
258 gtk_container_add(GTK_CONTAINER(box), image); |
| |
259 gtk_container_add(GTK_CONTAINER(docklet), box); |
| |
260 |
| |
261 if (!gtk_check_version(2,4,0)) |
| |
262 g_object_set(G_OBJECT(box), "visible-window", FALSE, NULL); |
| |
263 |
| |
264 gtk_widget_show_all(GTK_WIDGET(docklet)); |
| |
265 |
| |
266 /* ref the docklet before we bandy it about the place */ |
| |
267 g_object_ref(G_OBJECT(docklet)); |
| |
268 |
| |
269 /* This is a hack to avoid a race condition between the docklet getting |
| |
270 * embedded in the notification area and the gtkblist restoring its |
| |
271 * previous visibility state. If the docklet does not get embedded within |
| |
272 * the timeout, it will be removed as a visibility manager until it does |
| |
273 * get embedded. Ideally, we would only call docklet_embedded() when the |
| |
274 * icon was actually embedded. |
| |
275 */ |
| |
276 pidgin_docklet_embedded(); |
| |
277 embed_timeout = g_timeout_add(EMBED_TIMEOUT, docklet_x11_embed_timeout_cb, NULL); |
| |
278 |
| |
279 purple_debug(PURPLE_DEBUG_INFO, "docklet", "created\n"); |
| |
280 } |
| |
281 |
| |
282 static struct docklet_ui_ops ui_ops = |
| |
283 { |
| |
284 docklet_x11_create, |
| |
285 docklet_x11_destroy, |
| |
286 docklet_x11_update_icon, |
| |
287 docklet_x11_blank_icon, |
| |
288 docklet_x11_set_tooltip, |
| |
289 #if GTK_CHECK_VERSION(2,2,0) |
| |
290 docklet_x11_position_menu |
| |
291 #else |
| |
292 NULL |
| |
293 #endif |
| |
294 }; |
| |
295 |
| |
296 void |
| |
297 docklet_ui_init() |
| |
298 { |
| |
299 pidgin_docklet_set_ui_ops(&ui_ops); |
| |
300 } |