2
* * Copyright (C) 2008-2009 Ali <aliov@xfce.org>
4
* Licensed under the GNU General Public License Version 2
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32
#include <libxfce4util/libxfce4util.h>
33
#include <libxfcegui4/libxfcegui4.h>
35
#include "libxfpm/xfpm-common.h"
36
#include "libxfpm/xfpm-string.h"
37
#include "libxfpm/xfpm-notify.h"
38
#include "libxfpm/xfpm-icons.h"
40
#include "xfpm-tray-icon.h"
41
#include "xfpm-shutdown.h"
42
#include "xfpm-dbus-monitor.h"
43
#include "xfpm-inhibit.h"
44
#include "xfpm-screen-saver.h"
45
#include "xfpm-xfconf.h"
46
#include "xfpm-config.h"
48
static void xfpm_tray_icon_finalize (GObject *object);
50
#define XFPM_TRAY_ICON_GET_PRIVATE(o) \
51
(G_TYPE_INSTANCE_GET_PRIVATE((o), XFPM_TYPE_TRAY_ICON, XfpmTrayIconPrivate))
53
struct XfpmTrayIconPrivate
55
XfpmShutdown *shutdown;
60
XfpmDBusMonitor *monitor;
66
gboolean data_available;
78
static guint signals[LAST_SIGNAL] = { 0 };
80
G_DEFINE_TYPE(XfpmTrayIcon, xfpm_tray_icon, G_TYPE_OBJECT)
83
xfpm_tray_icon_size_changed_cb (GtkStatusIcon *icon, gint size, XfpmTrayIcon *tray)
87
g_return_val_if_fail (size > 0, FALSE);
89
g_debug ("Size Changed %d icon=%s", size, g_quark_to_string (tray->priv->icon_quark));
94
if ( tray->priv->icon_quark == 0 )
97
pix = xfce_themed_icon_load (g_quark_to_string (tray->priv->icon_quark), size);
101
gtk_status_icon_set_from_pixbuf (GTK_STATUS_ICON(tray->priv->icon), pix);
102
g_object_unref (pix);
109
xfpm_tray_info (GtkWidget *w, XfpmTrayIcon *tray)
111
g_signal_emit (G_OBJECT (tray), signals[SHOW_INFORMATION], 0);
115
xfpm_tray_icon_do_suspend (XfpmTrayIcon *tray)
117
GError *error = NULL;
119
xfpm_suspend (tray->priv->shutdown, &error);
123
g_warning ("%s", error->message);
124
xfpm_notify_show_notification (tray->priv->notify,
125
_("Xfce power manager"),
127
xfpm_tray_icon_get_icon_name (tray),
130
XFPM_NOTIFY_CRITICAL,
132
g_error_free (error);
138
xfpm_tray_icon_do_hibernate (XfpmTrayIcon *tray)
140
GError *error = NULL;
142
xfpm_hibernate (tray->priv->shutdown, &error);
146
g_warning ("%s", error->message);
147
xfpm_notify_show_notification (tray->priv->notify,
148
_("Xfce power manager"),
150
xfpm_tray_icon_get_icon_name (tray),
153
XFPM_NOTIFY_CRITICAL,
155
g_error_free (error);
161
xfpm_tray_icon_hibernate_cb (GtkWidget *w, XfpmTrayIcon *tray)
163
const gchar *message;
164
gboolean lock_screen;
167
g_object_get (G_OBJECT (tray->priv->conf),
168
LOCK_SCREEN_ON_SLEEP, &lock_screen,
171
message = tray->priv->inhibited ?
172
_("An application is currently disabling the automatic sleep,"
173
" doing this action now may damage the working state of this application,"
174
" are you sure you want to hibernate the system?") :
175
_("Are you sure you want to proceed?") ;
178
xfce_confirm (message,
184
xfpm_shutdown_add_callback (tray->priv->shutdown,
185
(GSourceFunc) xfpm_tray_icon_do_hibernate,
192
xfpm_tray_icon_suspend_cb (GtkWidget *w, XfpmTrayIcon *tray)
194
const gchar *message;
195
gboolean lock_screen;
198
g_object_get (G_OBJECT (tray->priv->conf),
199
LOCK_SCREEN_ON_SLEEP, &lock_screen,
202
message = tray->priv->inhibited ?
203
_("An application is currently disabling the automatic sleep,"
204
" doing this action now may damage the working state of this application,"
205
" are you sure you want to suspend the system?") :
206
_("Are you sure you want to proceed?") ;
209
xfce_confirm (message,
215
xfpm_shutdown_add_callback (tray->priv->shutdown,
216
(GSourceFunc) xfpm_tray_icon_do_suspend,
223
xfpm_tray_icon_exit_activated_cb (GtkWidget *w, gpointer data)
227
ret = xfce_confirm (_("Quit the Xfce power manager"),
237
xfpm_tray_icon_menu_selection_done (GtkMenuShell *menu, gpointer data)
239
gtk_widget_destroy (GTK_WIDGET (menu));
243
xfpm_tray_icon_inhibit_active_cb (GtkWidget *mi, XfpmTrayIcon *tray)
246
active = xfpm_screen_saver_get_inhibit (tray->priv->srv);
248
active == FALSE ? xfpm_screen_saver_inhibit (tray->priv->srv) :
249
xfpm_screen_saver_uninhibit (tray->priv->srv);
253
xfpm_tray_icon_popup_menu_cb (GtkStatusIcon *icon, guint button,
254
guint activate_time, XfpmTrayIcon *tray)
256
GtkWidget *menu, *mi, *img;
257
gboolean can_suspend = FALSE;
258
gboolean can_hibernate = FALSE ;
259
gboolean caller = FALSE;
261
gboolean saver_inhibited;
263
menu = gtk_menu_new();
265
g_object_get (G_OBJECT (tray->priv->shutdown),
266
"caller-privilege", &caller,
267
"can-suspend", &can_suspend,
268
"can-hibernate", &can_hibernate,
271
// Hibernate menu option
272
mi = gtk_image_menu_item_new_with_label (_("Hibernate"));
273
img = gtk_image_new_from_icon_name (XFPM_HIBERNATE_ICON, GTK_ICON_SIZE_MENU);
274
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM(mi), img);
275
gtk_widget_set_sensitive (mi,FALSE);
277
if ( caller && can_hibernate && tray->priv->data_available )
279
gtk_widget_set_sensitive (mi, TRUE);
280
g_signal_connect (G_OBJECT (mi), "activate",
281
G_CALLBACK (xfpm_tray_icon_hibernate_cb), tray);
284
gtk_menu_shell_append(GTK_MENU_SHELL(menu),mi);
286
// Suspend menu option
287
mi = gtk_image_menu_item_new_with_label(_("Suspend"));
288
img = gtk_image_new_from_icon_name (XFPM_SUSPEND_ICON, GTK_ICON_SIZE_MENU);
289
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mi),img);
291
gtk_widget_set_sensitive(mi,FALSE);
292
if ( caller && can_suspend && tray->priv->data_available )
294
gtk_widget_set_sensitive (mi,TRUE);
295
g_signal_connect (mi, "activate",
296
G_CALLBACK (xfpm_tray_icon_suspend_cb), tray);
299
gtk_menu_shell_append(GTK_MENU_SHELL(menu),mi);
301
saver_inhibited = xfpm_screen_saver_get_inhibit (tray->priv->srv);
302
mi = gtk_check_menu_item_new_with_label (_("Monitor power control"));
303
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mi), !saver_inhibited);
304
gtk_widget_set_tooltip_text (mi, _("Disable or enable monitor power control, "\
305
"for example you could disable the screen power "\
306
"control to avoid screen blanking when watching a movie."));
308
g_signal_connect (G_OBJECT (mi), "activate",
309
G_CALLBACK (xfpm_tray_icon_inhibit_active_cb), tray);
310
gtk_widget_set_sensitive (mi, TRUE);
312
gtk_menu_shell_append(GTK_MENU_SHELL(menu),mi);
314
if ( tray->priv->info_menu )
316
mi = gtk_separator_menu_item_new();
318
gtk_menu_shell_append(GTK_MENU_SHELL(menu),mi);
320
// Battery informations
322
mi = gtk_image_menu_item_new_from_stock (GTK_STOCK_INFO, NULL);
324
gtk_widget_set_sensitive(mi,FALSE);
325
gtk_widget_set_sensitive (mi,TRUE);
327
g_signal_connect(mi,"activate",
328
G_CALLBACK(xfpm_tray_info), tray);
331
gtk_menu_shell_append(GTK_MENU_SHELL(menu),mi);
335
mi = gtk_separator_menu_item_new();
337
gtk_menu_shell_append(GTK_MENU_SHELL(menu),mi);
340
mi = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP,NULL);
341
gtk_widget_set_sensitive(mi,TRUE);
343
g_signal_connect(mi,"activate",G_CALLBACK(xfpm_help),NULL);
345
gtk_menu_shell_append(GTK_MENU_SHELL(menu),mi);
348
mi = gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT,NULL);
349
gtk_widget_set_sensitive(mi,TRUE);
351
g_signal_connect(mi,"activate",G_CALLBACK(xfpm_about), _("Xfce Power Manager"));
353
gtk_menu_shell_append(GTK_MENU_SHELL(menu),mi);
355
mi = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES,NULL);
356
gtk_widget_set_sensitive(mi,TRUE);
358
g_signal_connect(mi,"activate",G_CALLBACK(xfpm_preferences),NULL);
360
gtk_menu_shell_append(GTK_MENU_SHELL(menu),mi);
362
mi = gtk_separator_menu_item_new ();
363
gtk_widget_show (mi);
364
gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
366
mi = gtk_image_menu_item_new_from_stock (GTK_STOCK_QUIT, NULL);
367
gtk_widget_set_sensitive (mi, TRUE);
368
gtk_widget_show (mi);
369
g_signal_connect (mi, "activate", G_CALLBACK (xfpm_tray_icon_exit_activated_cb), NULL);
370
gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
372
g_signal_connect (menu, "selection-done",
373
G_CALLBACK (xfpm_tray_icon_menu_selection_done), NULL);
376
gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
377
gtk_status_icon_position_menu,
378
icon, button, activate_time);
382
xfpm_tray_icon_inhibit_changed_cb (XfpmInhibit *inhibit, gboolean inhibited, XfpmTrayIcon *tray)
384
tray->priv->inhibited = inhibited;
388
xfpm_tray_icon_hal_connection_changed_cb (XfpmDBusMonitor *monitor, gboolean connected, XfpmTrayIcon *tray)
390
if ( connected == FALSE )
391
xfpm_tray_icon_set_tooltip (tray, _("No data available"));
394
* We set this variable after since xfpm_tray_icon_set_tooltip will return
395
* if data_available is set to FALSE
397
tray->priv->data_available = connected;
401
xfpm_tray_icon_activated_cb (XfpmTrayIcon *tray)
407
xfpm_tray_icon_class_init(XfpmTrayIconClass *klass)
409
GObjectClass *object_class = G_OBJECT_CLASS(klass);
411
signals[SHOW_INFORMATION] =
412
g_signal_new("show-information",
415
G_STRUCT_OFFSET(XfpmTrayIconClass, show_info),
417
g_cclosure_marshal_VOID__VOID,
418
G_TYPE_NONE, 0, G_TYPE_NONE);
420
object_class->finalize = xfpm_tray_icon_finalize;
422
g_type_class_add_private(klass,sizeof(XfpmTrayIconPrivate));
426
xfpm_tray_icon_init(XfpmTrayIcon *tray)
428
tray->priv = XFPM_TRAY_ICON_GET_PRIVATE(tray);
430
tray->priv->icon = gtk_status_icon_new();
431
tray->priv->shutdown = xfpm_shutdown_new ();
432
tray->priv->conf = xfpm_xfconf_new ();
433
tray->priv->notify = xfpm_notify_new ();
434
tray->priv->inhibited = FALSE;
435
tray->priv->inhibit = xfpm_inhibit_new ();
436
tray->priv->srv = xfpm_screen_saver_new ();
437
tray->priv->monitor = xfpm_dbus_monitor_new ();
439
tray->priv->info_menu = TRUE;
440
tray->priv->icon_quark = 0;
442
g_signal_connect (tray->priv->icon, "size-changed",
443
G_CALLBACK (xfpm_tray_icon_size_changed_cb), tray);
445
g_signal_connect (tray->priv->icon, "popup-menu",
446
G_CALLBACK (xfpm_tray_icon_popup_menu_cb), tray);
448
g_signal_connect_swapped (tray->priv->icon, "activate",
449
G_CALLBACK (xfpm_tray_icon_activated_cb), tray);
451
tray->priv->sig = g_signal_connect (tray->priv->inhibit, "has-inhibit-changed",
452
G_CALLBACK (xfpm_tray_icon_inhibit_changed_cb), tray);
454
tray->priv->sig_1 = g_signal_connect (tray->priv->monitor, "hal-connection-changed",
455
G_CALLBACK (xfpm_tray_icon_hal_connection_changed_cb), tray);
457
tray->priv->data_available = xfpm_dbus_monitor_hal_connected (tray->priv->monitor);
461
xfpm_tray_icon_finalize(GObject *object)
465
tray = XFPM_TRAY_ICON(object);
467
if ( g_signal_handler_is_connected (tray->priv->inhibit, tray->priv->sig ) )
468
g_signal_handler_disconnect (G_OBJECT (tray->priv->inhibit), tray->priv->sig);
470
if ( g_signal_handler_is_connected (tray->priv->monitor, tray->priv->sig_1) )
471
g_signal_handler_disconnect (G_OBJECT (tray->priv->monitor), tray->priv->sig_1);
473
g_object_unref (tray->priv->monitor);
475
g_object_unref (tray->priv->icon);
477
g_object_unref (tray->priv->shutdown);
479
g_object_unref (tray->priv->conf);
481
g_object_unref (tray->priv->notify);
483
g_object_unref (tray->priv->inhibit);
485
g_object_unref (tray->priv->srv);
487
G_OBJECT_CLASS(xfpm_tray_icon_parent_class)->finalize(object);
491
xfpm_tray_icon_new(void)
493
XfpmTrayIcon *tray = NULL;
494
tray = g_object_new (XFPM_TYPE_TRAY_ICON, NULL);
498
void xfpm_tray_icon_set_show_info_menu (XfpmTrayIcon *icon, gboolean value)
500
g_return_if_fail (XFPM_IS_TRAY_ICON (icon));
501
icon->priv->info_menu = value;
504
void xfpm_tray_icon_set_icon (XfpmTrayIcon *icon, const gchar *icon_name)
506
g_return_if_fail(XFPM_IS_TRAY_ICON(icon));
508
icon->priv->icon_quark = g_quark_from_string(icon_name);
510
xfpm_tray_icon_size_changed_cb (icon->priv->icon,
511
gtk_status_icon_get_size(icon->priv->icon),
515
void xfpm_tray_icon_set_tooltip (XfpmTrayIcon *icon, const gchar *tooltip)
517
g_return_if_fail (XFPM_IS_TRAY_ICON (icon));
519
* Hal is disconnected we have the tooltips set to "no data available"
521
g_return_if_fail (icon->priv->data_available);
523
#if GTK_CHECK_VERSION (2, 16, 0)
524
gtk_status_icon_set_tooltip_text (GTK_STATUS_ICON(icon->priv->icon), tooltip);
526
gtk_status_icon_set_tooltip (GTK_STATUS_ICON(icon->priv->icon), tooltip);
530
void xfpm_tray_icon_set_visible (XfpmTrayIcon *icon, gboolean visible)
532
g_return_if_fail(XFPM_IS_TRAY_ICON(icon));
534
gtk_status_icon_set_visible(GTK_STATUS_ICON(icon->priv->icon), visible);
537
gboolean xfpm_tray_icon_get_visible (XfpmTrayIcon *icon)
539
g_return_val_if_fail (XFPM_IS_TRAY_ICON(icon), FALSE);
541
return gtk_status_icon_get_visible (GTK_STATUS_ICON(icon->priv->icon));
544
GtkStatusIcon *xfpm_tray_icon_get_tray_icon (XfpmTrayIcon *icon)
546
g_return_val_if_fail(XFPM_IS_TRAY_ICON(icon), NULL);
548
return icon->priv->icon;
551
const gchar *xfpm_tray_icon_get_icon_name (XfpmTrayIcon *icon)
553
g_return_val_if_fail(XFPM_IS_TRAY_ICON(icon), NULL);
555
if ( icon->priv->icon_quark == 0 ) return NULL;
557
return g_quark_to_string (icon->priv->icon_quark);