static void on_state_changed(HippoConnection *connection, HippoStatusIcon *icon) { g_object_set(G_OBJECT(icon), "icon-name", get_icon_name(connection), NULL); gtk_status_icon_set_tooltip(GTK_STATUS_ICON(icon), hippo_connection_get_tooltip(connection)); }
HippoStatusIcon* hippo_status_icon_new(HippoDataCache *cache) { HippoConnection *connection = hippo_data_cache_get_connection(cache); HippoStatusIcon *icon; icon = g_object_new(HIPPO_TYPE_STATUS_ICON, "icon-name", get_icon_name(connection), NULL); icon->cache = cache; g_object_ref(icon->cache); g_signal_connect(connection, "state-changed", G_CALLBACK(on_state_changed), icon); gtk_status_icon_set_tooltip(GTK_STATUS_ICON(icon), hippo_connection_get_tooltip(connection)); return HIPPO_STATUS_ICON(icon); }
static void update_tray_icon_status (GtkStatusIcon *tray_icon) { gboolean battery_present = FALSE; gboolean ac_online = FALSE; gint battery_status = -1; static gint old_battery_status = -1; static gboolean battery_low = FALSE; static gboolean battery_critical = FALSE; static gboolean spawn_command_critical = FALSE; gint percentage, time; gchar *battery_string, *time_string; GError *error = NULL; #ifdef WITH_NOTIFY NotifyNotification *notification = NULL; #endif g_return_if_fail (tray_icon != NULL); /* update tray icon for AC only */ if (battery_path == NULL) { static gboolean ac_notified = FALSE; if (ac_notified == FALSE) { ac_notified = TRUE; NOTIFY_MESSAGE (¬ification, _("AC only, no battery!"), NULL, NOTIFY_EXPIRES_NEVER, NOTIFY_URGENCY_NORMAL); } gtk_status_icon_set_tooltip_text (tray_icon, _("AC only, no battery!")); gtk_status_icon_set_from_icon_name (tray_icon, "ac-adapter"); return; } /* update tray icon for battery */ if (get_battery_present (&battery_present) == FALSE) { return; } if (battery_present == FALSE) { battery_status = MISSING; } else { if (get_battery_status (&battery_status) == FALSE) { return; } /* workaround for limited/bugged batteries/drivers */ /* that unduly return unknown status */ if (battery_status == UNKNOWN && get_ac_online (&ac_online) == TRUE) { if (ac_online == TRUE) { battery_status = CHARGING; if (get_battery_charge (FALSE, &percentage, NULL) == TRUE && percentage >= 99) { battery_status = CHARGED; } } else { battery_status = DISCHARGING; } } } #define HANDLE_BATTERY_STATUS(PCT,TIM,EXP,URG) \ \ percentage = PCT; \ \ battery_string = get_battery_string (battery_status, percentage); \ time_string = get_time_string (TIM); \ \ if (old_battery_status != battery_status) { \ old_battery_status = battery_status; \ NOTIFY_MESSAGE (¬ification, battery_string, time_string, EXP, URG); \ } \ \ gtk_status_icon_set_tooltip_text (tray_icon, get_tooltip_string (battery_string, time_string)); \ gtk_status_icon_set_from_icon_name (tray_icon, get_icon_name (battery_status, percentage)); switch (battery_status) { case MISSING: HANDLE_BATTERY_STATUS (0, -1, NOTIFY_EXPIRES_NEVER, NOTIFY_URGENCY_NORMAL) break; case UNKNOWN: HANDLE_BATTERY_STATUS (0, -1, NOTIFY_EXPIRES_DEFAULT, NOTIFY_URGENCY_NORMAL) break; case CHARGED: HANDLE_BATTERY_STATUS (100, -1, NOTIFY_EXPIRES_DEFAULT, NOTIFY_URGENCY_NORMAL) break; case CHARGING: if (old_battery_status != CHARGING && estimation_needed == TRUE) { reset_battery_time_estimation (); } if (get_battery_charge (FALSE, &percentage, &time) == FALSE) { return; } HANDLE_BATTERY_STATUS (percentage, time, NOTIFY_EXPIRES_DEFAULT, NOTIFY_URGENCY_NORMAL) break; case DISCHARGING: case NOT_CHARGING: if (old_battery_status != DISCHARGING && estimation_needed == TRUE) { reset_battery_time_estimation (); } if (get_battery_charge (TRUE, &percentage, &time) == FALSE) { return; } battery_string = get_battery_string (battery_status, percentage); time_string = get_time_string (time); if (old_battery_status != DISCHARGING) { old_battery_status = DISCHARGING; NOTIFY_MESSAGE (¬ification, battery_string, time_string, NOTIFY_EXPIRES_DEFAULT, NOTIFY_URGENCY_NORMAL); battery_low = FALSE; battery_critical = FALSE; spawn_command_critical = FALSE; } if (battery_low == FALSE && percentage <= configuration.low_level) { battery_low = TRUE; battery_string = get_battery_string (LOW_LEVEL, percentage); NOTIFY_MESSAGE (¬ification, battery_string, time_string, NOTIFY_EXPIRES_NEVER, NOTIFY_URGENCY_NORMAL); } if (battery_critical == FALSE && percentage <= configuration.critical_level) { battery_critical = TRUE; battery_string = get_battery_string (CRITICAL_LEVEL, percentage); NOTIFY_MESSAGE (¬ification, battery_string, time_string, NOTIFY_EXPIRES_NEVER, NOTIFY_URGENCY_CRITICAL); spawn_command_critical = TRUE; } gtk_status_icon_set_tooltip_text (tray_icon, get_tooltip_string (battery_string, time_string)); gtk_status_icon_set_from_icon_name (tray_icon, get_icon_name (battery_status, percentage)); if (spawn_command_critical == TRUE) { spawn_command_critical = FALSE; if (configuration.command_critical_level != NULL) { syslog (LOG_CRIT, _("Spawning critical battery level command in 30 seconds: %s"), configuration.command_critical_level); g_usleep (G_USEC_PER_SEC * 30); if (g_spawn_command_line_async (configuration.command_critical_level, &error) == FALSE) { syslog (LOG_CRIT, _("Cannot spawn critical battery level command: %s"), error->message); g_error_free (error); error = NULL; NOTIFY_MESSAGE (¬ification, _("Cannot spawn critical battery level command!"), configuration.command_critical_level, NOTIFY_EXPIRES_NEVER, NOTIFY_URGENCY_CRITICAL); } } } break; } }