|
0 |
|
|
1 |
#include <config.h>
|
|
2 |
|
|
3 |
#include <stdio.h>
|
|
4 |
#include <stdlib.h>
|
|
5 |
#include <string.h>
|
|
6 |
|
|
7 |
#include <sys/types.h>
|
|
8 |
#include <unistd.h>
|
|
9 |
|
|
10 |
#include <gdk/gdkx.h>
|
|
11 |
#include <gtk/gtk.h>
|
|
12 |
|
|
13 |
#include <glib/gi18n-lib.h>
|
|
14 |
|
|
15 |
#include "xapp-status-icon.h"
|
|
16 |
|
|
17 |
static const gchar * DBUS_PATH = "/org/x/StatusIcon";
|
|
18 |
static const gchar * DBUS_NAME = "org.x.StatusIcon";
|
|
19 |
|
|
20 |
static const gchar introspection_xml[] =
|
|
21 |
"<node>"
|
|
22 |
" <interface name='org.x.StatusIcon'>"
|
|
23 |
" <method name='ButtonPress'>"
|
|
24 |
" <arg name='x' direction='in' type='i'/>"
|
|
25 |
" <arg name='y' direction='in' type='i'/>"
|
|
26 |
" <arg name='button' direction='in' type='i'/>"
|
|
27 |
" <arg name='time' direction='in' type='i'/>"
|
|
28 |
" <arg name='panel_position' direction='in' type='i'/>"
|
|
29 |
" </method>"
|
|
30 |
" <method name='ButtonRelease'>"
|
|
31 |
" <arg name='x' direction='in' type='i'/>"
|
|
32 |
" <arg name='y' direction='in' type='i'/>"
|
|
33 |
" <arg name='button' direction='in' type='i'/>"
|
|
34 |
" <arg name='time' direction='in' type='i'/>"
|
|
35 |
" <arg name='panel_position' direction='in' type='i'/>"
|
|
36 |
" </method>"
|
|
37 |
" <property type='s' name='Name' access='read'/>"
|
|
38 |
" <property type='s' name='IconName' access='read'/>"
|
|
39 |
" <property type='s' name='TooltipText' access='read'/>"
|
|
40 |
" <property type='s' name='Label' access='read'/>"
|
|
41 |
" <property type='b' name='Visible' access='read'/>"
|
|
42 |
" </interface>"
|
|
43 |
"</node>";
|
|
44 |
|
|
45 |
enum
|
|
46 |
{
|
|
47 |
BUTTON_PRESS,
|
|
48 |
BUTTON_RELEASE,
|
|
49 |
LAST_SIGNAL
|
|
50 |
};
|
|
51 |
|
|
52 |
static guint signals[LAST_SIGNAL] = {0, };
|
|
53 |
|
|
54 |
/**
|
|
55 |
* SECTION:xapp-status-icon
|
|
56 |
* @Short_description: Broadcasts status information over DBUS
|
|
57 |
* @Title: XAppStatusIcon
|
|
58 |
*
|
|
59 |
* The XAppStatusIcon allows applications to share status info
|
|
60 |
* about themselves. It replaces the obsolete and very similar
|
|
61 |
* Gtk.StatusIcon widget.
|
|
62 |
*
|
|
63 |
* If used in an environment where no applet is handling XAppStatusIcons,
|
|
64 |
* The XAppStatusIcon delegates its calls to a Gtk.StatusIcon.
|
|
65 |
*/
|
|
66 |
struct _XAppStatusIconPrivate
|
|
67 |
{
|
|
68 |
gchar *name;
|
|
69 |
gchar *icon_name;
|
|
70 |
gchar *tooltip_text;
|
|
71 |
gchar *label;
|
|
72 |
gboolean visible;
|
|
73 |
GDBusConnection *connection;
|
|
74 |
guint owner_id;
|
|
75 |
guint registration_id;
|
|
76 |
GtkStatusIcon *gtk_status_icon;
|
|
77 |
};
|
|
78 |
|
|
79 |
G_DEFINE_TYPE (XAppStatusIcon, xapp_status_icon, G_TYPE_OBJECT);
|
|
80 |
|
|
81 |
void
|
|
82 |
emit_changed_properties_signal (XAppStatusIcon *self)
|
|
83 |
{
|
|
84 |
GVariantBuilder *builder;
|
|
85 |
GError *local_error;
|
|
86 |
|
|
87 |
if (self->priv->connection)
|
|
88 |
{
|
|
89 |
local_error = NULL;
|
|
90 |
builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
|
|
91 |
if (self->priv->name)
|
|
92 |
{
|
|
93 |
g_variant_builder_add (builder, "{sv}", "Name", g_variant_new_string (self->priv->name));
|
|
94 |
}
|
|
95 |
if (self->priv->icon_name)
|
|
96 |
{
|
|
97 |
g_variant_builder_add (builder, "{sv}", "IconName", g_variant_new_string (self->priv->icon_name));
|
|
98 |
}
|
|
99 |
if (self->priv->tooltip_text)
|
|
100 |
{
|
|
101 |
g_variant_builder_add (builder, "{sv}", "TooltipText", g_variant_new_string (self->priv->tooltip_text));
|
|
102 |
}
|
|
103 |
if (self->priv->label)
|
|
104 |
{
|
|
105 |
g_variant_builder_add (builder, "{sv}", "Label", g_variant_new_string (self->priv->label));
|
|
106 |
}
|
|
107 |
if (self->priv->visible)
|
|
108 |
{
|
|
109 |
g_variant_builder_add (builder, "{sv}", "Visible", g_variant_new_boolean (self->priv->visible));
|
|
110 |
}
|
|
111 |
g_dbus_connection_emit_signal (self->priv->connection,
|
|
112 |
NULL,
|
|
113 |
DBUS_PATH,
|
|
114 |
"org.freedesktop.DBus.Properties",
|
|
115 |
"PropertiesChanged",
|
|
116 |
g_variant_new ("(sa{sv}as)", DBUS_NAME, builder, NULL),
|
|
117 |
&local_error);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
void
|
|
122 |
handle_method_call (GDBusConnection *connection,
|
|
123 |
const gchar *sender,
|
|
124 |
const gchar *object_path,
|
|
125 |
const gchar *interface_name,
|
|
126 |
const gchar *method_name,
|
|
127 |
GVariant *parameters,
|
|
128 |
GDBusMethodInvocation *invocation,
|
|
129 |
gpointer user_data)
|
|
130 |
{
|
|
131 |
int x, y, time, button, position;
|
|
132 |
XAppStatusIcon *icon = user_data;
|
|
133 |
|
|
134 |
if (g_strcmp0 (method_name, "ButtonPress") == 0)
|
|
135 |
{
|
|
136 |
g_variant_get (parameters, "(iiiii)", &x, &y, &button, &time, &position);
|
|
137 |
g_dbus_method_invocation_return_value (invocation, NULL);
|
|
138 |
g_signal_emit (icon, signals[BUTTON_PRESS], 0, x, y, button, time, position);
|
|
139 |
}
|
|
140 |
else if (g_strcmp0 (method_name, "ButtonRelease") == 0)
|
|
141 |
{
|
|
142 |
g_variant_get (parameters, "(iiiii)", &x, &y, &button, &time, &position);
|
|
143 |
g_dbus_method_invocation_return_value (invocation, NULL);
|
|
144 |
g_signal_emit (icon, signals[BUTTON_RELEASE], 0, x, y, button, time, position);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
GVariant *
|
|
149 |
get_property (GDBusConnection *connection,
|
|
150 |
const gchar *sender,
|
|
151 |
const gchar *object_path,
|
|
152 |
const gchar *interface_name,
|
|
153 |
const gchar *property_name,
|
|
154 |
GError **error,
|
|
155 |
gpointer user_data)
|
|
156 |
{
|
|
157 |
GVariant *ret;
|
|
158 |
XAppStatusIcon *icon = user_data;
|
|
159 |
ret = NULL;
|
|
160 |
|
|
161 |
if (icon->priv->name && g_strcmp0 (property_name, "Name") == 0)
|
|
162 |
{
|
|
163 |
ret = g_variant_new_string (icon->priv->name);
|
|
164 |
}
|
|
165 |
else if (icon->priv->icon_name && g_strcmp0 (property_name, "IconName") == 0)
|
|
166 |
{
|
|
167 |
ret = g_variant_new_string (icon->priv->icon_name);
|
|
168 |
}
|
|
169 |
else if (icon->priv->tooltip_text && g_strcmp0 (property_name, "TooltipText") == 0)
|
|
170 |
{
|
|
171 |
ret = g_variant_new_string (icon->priv->tooltip_text);
|
|
172 |
}
|
|
173 |
else if (icon->priv->label && g_strcmp0 (property_name, "Label") == 0)
|
|
174 |
{
|
|
175 |
ret = g_variant_new_string (icon->priv->label);
|
|
176 |
}
|
|
177 |
else if (g_strcmp0 (property_name, "Visible") == 0)
|
|
178 |
{
|
|
179 |
ret = g_variant_new_boolean (icon->priv->visible);
|
|
180 |
}
|
|
181 |
|
|
182 |
return ret;
|
|
183 |
}
|
|
184 |
|
|
185 |
static const GDBusInterfaceVTable interface_vtable =
|
|
186 |
{
|
|
187 |
handle_method_call,
|
|
188 |
get_property,
|
|
189 |
NULL
|
|
190 |
};
|
|
191 |
|
|
192 |
void
|
|
193 |
on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
|
|
194 |
{
|
|
195 |
XAppStatusIcon *icon = user_data;
|
|
196 |
icon->priv->connection = connection;
|
|
197 |
GDBusNodeInfo *introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
|
|
198 |
icon->priv->registration_id = g_dbus_connection_register_object (connection,
|
|
199 |
DBUS_PATH,
|
|
200 |
introspection_data->interfaces[0],
|
|
201 |
&interface_vtable,
|
|
202 |
icon, /* user_data */
|
|
203 |
NULL, /* user_data_free_func */
|
|
204 |
NULL); /* GError** */
|
|
205 |
}
|
|
206 |
|
|
207 |
void on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) {}
|
|
208 |
void on_name_lost (GDBusConnection *connection, const gchar *name, gpointer user_data) {}
|
|
209 |
|
|
210 |
static gboolean
|
|
211 |
check_at_least_one_status_applet_is_running ()
|
|
212 |
{
|
|
213 |
// Check that there is at least one applet on DBUS
|
|
214 |
GDBusConnection *connection;
|
|
215 |
GVariant *result;
|
|
216 |
GError *error;
|
|
217 |
GVariantIter *iter;
|
|
218 |
gchar *str;
|
|
219 |
gboolean found = FALSE;
|
|
220 |
|
|
221 |
error = NULL;
|
|
222 |
connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
|
|
223 |
if (connection == NULL)
|
|
224 |
{
|
|
225 |
g_warning("Unable to connect to dbus: %s", error->message);
|
|
226 |
g_printerr ("Error: %s\n", error->message);
|
|
227 |
g_error_free (error);
|
|
228 |
return FALSE;
|
|
229 |
}
|
|
230 |
|
|
231 |
error = NULL;
|
|
232 |
result = g_dbus_connection_call_sync (connection,
|
|
233 |
"org.freedesktop.DBus",
|
|
234 |
"/org/freedesktop/DBus",
|
|
235 |
"org.freedesktop.DBus",
|
|
236 |
"ListNames",
|
|
237 |
NULL,
|
|
238 |
G_VARIANT_TYPE ("(as)"),
|
|
239 |
G_DBUS_CALL_FLAGS_NONE,
|
|
240 |
3000, /* 3 secs */
|
|
241 |
NULL,
|
|
242 |
&error);
|
|
243 |
if (result == NULL)
|
|
244 |
{
|
|
245 |
g_printerr (_("Error: %s\n"), error->message);
|
|
246 |
g_error_free (error);
|
|
247 |
return FALSE;
|
|
248 |
}
|
|
249 |
|
|
250 |
g_variant_get (result, "(as)", &iter);
|
|
251 |
while (g_variant_iter_loop (iter, "s", &str))
|
|
252 |
{
|
|
253 |
if (g_str_has_prefix (str, "org.x.StatusApplet"))
|
|
254 |
{
|
|
255 |
// printf("FOUND %s\n", str);
|
|
256 |
found = TRUE;
|
|
257 |
}
|
|
258 |
}
|
|
259 |
g_variant_iter_free (iter);
|
|
260 |
g_variant_unref (result);
|
|
261 |
return found;
|
|
262 |
}
|
|
263 |
|
|
264 |
static void
|
|
265 |
on_gtk_status_icon_activate (GtkStatusIcon *status_icon, gpointer user_data)
|
|
266 |
{
|
|
267 |
XAppStatusIcon *icon = user_data;
|
|
268 |
g_signal_emit (icon, signals[BUTTON_PRESS], 0, 0, 0, 1, 0, -1);
|
|
269 |
}
|
|
270 |
|
|
271 |
static void
|
|
272 |
on_gtk_status_icon_popup_menu (GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data)
|
|
273 |
{
|
|
274 |
XAppStatusIcon *icon = user_data;
|
|
275 |
g_signal_emit (icon, signals[BUTTON_RELEASE], 0, 0, 0, button, activate_time, -1);
|
|
276 |
}
|
|
277 |
|
|
278 |
static void
|
|
279 |
xapp_status_icon_init (XAppStatusIcon *self)
|
|
280 |
{
|
|
281 |
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, XAPP_TYPE_STATUS_ICON, XAppStatusIconPrivate);
|
|
282 |
self->priv->name = g_strdup_printf("%s", g_get_application_name());
|
|
283 |
self->priv->icon_name = NULL;
|
|
284 |
self->priv->tooltip_text = NULL;
|
|
285 |
self->priv->label = NULL;
|
|
286 |
self->priv->visible = TRUE;
|
|
287 |
self->priv->connection = NULL;
|
|
288 |
self->priv->owner_id = 0;
|
|
289 |
self->priv->registration_id = 0;
|
|
290 |
self->priv->gtk_status_icon = NULL;
|
|
291 |
|
|
292 |
// Check for an applet, if none.. fallback to delegating calls to a Gtk.StatusIcon
|
|
293 |
if (!check_at_least_one_status_applet_is_running())
|
|
294 |
{
|
|
295 |
self->priv->gtk_status_icon = gtk_status_icon_new ();
|
|
296 |
g_signal_connect (self->priv->gtk_status_icon, "activate", G_CALLBACK (on_gtk_status_icon_activate), self);
|
|
297 |
g_signal_connect (self->priv->gtk_status_icon, "popup-menu", G_CALLBACK (on_gtk_status_icon_popup_menu), self);
|
|
298 |
}
|
|
299 |
else
|
|
300 |
{
|
|
301 |
char *owner_name = g_strdup_printf("%s.PID-%d", DBUS_NAME, getpid());
|
|
302 |
self->priv->owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
|
|
303 |
owner_name,
|
|
304 |
G_DBUS_CONNECTION_FLAGS_NONE,
|
|
305 |
on_bus_acquired,
|
|
306 |
on_name_acquired,
|
|
307 |
on_name_lost,
|
|
308 |
self,
|
|
309 |
NULL);
|
|
310 |
|
|
311 |
g_free(owner_name);
|
|
312 |
}
|
|
313 |
|
|
314 |
signals[BUTTON_PRESS] =
|
|
315 |
g_signal_new ("button-press-event",
|
|
316 |
XAPP_TYPE_STATUS_ICON,
|
|
317 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
|
|
318 |
0,
|
|
319 |
NULL, NULL, NULL,
|
|
320 |
G_TYPE_NONE, 5, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT);
|
|
321 |
|
|
322 |
signals[BUTTON_RELEASE] =
|
|
323 |
g_signal_new ("button-release-event",
|
|
324 |
XAPP_TYPE_STATUS_ICON,
|
|
325 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
|
|
326 |
0,
|
|
327 |
NULL, NULL, NULL,
|
|
328 |
G_TYPE_NONE, 5, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT);
|
|
329 |
}
|
|
330 |
|
|
331 |
static void
|
|
332 |
xapp_status_icon_finalize (GObject *object)
|
|
333 |
{
|
|
334 |
XAppStatusIcon *self = XAPP_STATUS_ICON (object);
|
|
335 |
|
|
336 |
g_free (self->priv->name);
|
|
337 |
g_free (self->priv->icon_name);
|
|
338 |
g_free (self->priv->tooltip_text);
|
|
339 |
g_free (self->priv->label);
|
|
340 |
|
|
341 |
if (self->priv->gtk_status_icon != NULL)
|
|
342 |
{
|
|
343 |
g_signal_handlers_disconnect_by_func (self->priv->gtk_status_icon, on_gtk_status_icon_activate, self);
|
|
344 |
g_signal_handlers_disconnect_by_func (self->priv->gtk_status_icon, on_gtk_status_icon_popup_menu, self);
|
|
345 |
g_object_unref (self->priv->gtk_status_icon);
|
|
346 |
}
|
|
347 |
|
|
348 |
if (self->priv->connection != NULL && self->priv->registration_id > 0)
|
|
349 |
{
|
|
350 |
g_dbus_connection_unregister_object(self->priv->connection, self->priv->registration_id);
|
|
351 |
}
|
|
352 |
|
|
353 |
if (self->priv->owner_id > 0)
|
|
354 |
{
|
|
355 |
g_bus_unown_name(self->priv->owner_id);
|
|
356 |
}
|
|
357 |
|
|
358 |
G_OBJECT_CLASS (xapp_status_icon_parent_class)->finalize (object);
|
|
359 |
}
|
|
360 |
|
|
361 |
static void
|
|
362 |
xapp_status_icon_class_init (XAppStatusIconClass *klass)
|
|
363 |
{
|
|
364 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
365 |
|
|
366 |
gobject_class->finalize = xapp_status_icon_finalize;
|
|
367 |
|
|
368 |
g_type_class_add_private (gobject_class, sizeof (XAppStatusIconPrivate));
|
|
369 |
}
|
|
370 |
|
|
371 |
/**
|
|
372 |
* xapp_status_icon_set_name:
|
|
373 |
* @icon: a #XAppStatusIcon
|
|
374 |
* @name: a name (this defaults to the name of the application, if not set)
|
|
375 |
*
|
|
376 |
* Sets the status icon name. This is now shown to users.
|
|
377 |
*
|
|
378 |
* Since: 1.6
|
|
379 |
*/
|
|
380 |
void
|
|
381 |
xapp_status_icon_set_name (XAppStatusIcon *icon, const gchar *name)
|
|
382 |
{
|
|
383 |
g_return_if_fail (XAPP_IS_STATUS_ICON (icon));
|
|
384 |
g_free (icon->priv->name);
|
|
385 |
icon->priv->name = g_strdup (name);
|
|
386 |
emit_changed_properties_signal (icon);
|
|
387 |
|
|
388 |
if (icon->priv->gtk_status_icon != NULL)
|
|
389 |
{
|
|
390 |
gtk_status_icon_set_name (icon->priv->gtk_status_icon, name);
|
|
391 |
}
|
|
392 |
}
|
|
393 |
|
|
394 |
/**
|
|
395 |
* xapp_status_icon_set_icon_name:
|
|
396 |
* @icon: a #XAppStatusIcon
|
|
397 |
* @icon_name: an icon name
|
|
398 |
*
|
|
399 |
* Sets the icon name
|
|
400 |
*
|
|
401 |
* Since: 1.6
|
|
402 |
*/
|
|
403 |
void
|
|
404 |
xapp_status_icon_set_icon_name (XAppStatusIcon *icon, const gchar *icon_name)
|
|
405 |
{
|
|
406 |
g_return_if_fail (XAPP_IS_STATUS_ICON (icon));
|
|
407 |
g_free (icon->priv->icon_name);
|
|
408 |
icon->priv->icon_name = g_strdup (icon_name);
|
|
409 |
emit_changed_properties_signal (icon);
|
|
410 |
|
|
411 |
if (icon->priv->gtk_status_icon != NULL)
|
|
412 |
{
|
|
413 |
gtk_status_icon_set_from_icon_name (icon->priv->gtk_status_icon, icon_name);
|
|
414 |
}
|
|
415 |
}
|
|
416 |
|
|
417 |
/**
|
|
418 |
* xapp_status_icon_set_tooltip_text:
|
|
419 |
* @icon: a #XAppStatusIcon
|
|
420 |
* @tooltip_text: the text to show in the tooltip
|
|
421 |
*
|
|
422 |
* Sets the tooltip text
|
|
423 |
*
|
|
424 |
* Since: 1.6
|
|
425 |
*/
|
|
426 |
void
|
|
427 |
xapp_status_icon_set_tooltip_text (XAppStatusIcon *icon, const gchar *tooltip_text)
|
|
428 |
{
|
|
429 |
g_return_if_fail (XAPP_IS_STATUS_ICON (icon));
|
|
430 |
g_free (icon->priv->tooltip_text);
|
|
431 |
icon->priv->tooltip_text = g_strdup (tooltip_text);
|
|
432 |
emit_changed_properties_signal (icon);
|
|
433 |
|
|
434 |
if (icon->priv->gtk_status_icon != NULL)
|
|
435 |
{
|
|
436 |
gtk_status_icon_set_tooltip_text (icon->priv->gtk_status_icon, tooltip_text);
|
|
437 |
}
|
|
438 |
}
|
|
439 |
|
|
440 |
/**
|
|
441 |
* xapp_status_icon_set_label:
|
|
442 |
* @icon: a #XAppStatusIcon
|
|
443 |
* @label: some text
|
|
444 |
*
|
|
445 |
* Sets a label, shown beside the icon
|
|
446 |
*
|
|
447 |
* Since: 1.6
|
|
448 |
*/
|
|
449 |
void
|
|
450 |
xapp_status_icon_set_label (XAppStatusIcon *icon, const gchar *label)
|
|
451 |
{
|
|
452 |
g_return_if_fail (XAPP_IS_STATUS_ICON (icon));
|
|
453 |
g_free (icon->priv->label);
|
|
454 |
icon->priv->label = g_strdup (label);
|
|
455 |
emit_changed_properties_signal (icon);
|
|
456 |
}
|
|
457 |
|
|
458 |
/**
|
|
459 |
* xapp_status_icon_set_visible:
|
|
460 |
* @icon: a #XAppStatusIcon
|
|
461 |
* @visible: whether or not the status icon should be visible
|
|
462 |
*
|
|
463 |
* Sets the visibility of the status icon
|
|
464 |
*
|
|
465 |
* Since: 1.6
|
|
466 |
*/
|
|
467 |
void
|
|
468 |
xapp_status_icon_set_visible (XAppStatusIcon *icon, const gboolean visible)
|
|
469 |
{
|
|
470 |
g_return_if_fail (XAPP_IS_STATUS_ICON (icon));
|
|
471 |
icon->priv->visible = visible;
|
|
472 |
emit_changed_properties_signal (icon);
|
|
473 |
|
|
474 |
if (icon->priv->gtk_status_icon != NULL)
|
|
475 |
{
|
|
476 |
gtk_status_icon_set_visible (icon->priv->gtk_status_icon, visible);
|
|
477 |
}
|
|
478 |
}⏎
|