2011-10-18 18:44:29 +01:00
|
|
|
/*
|
|
|
|
*
|
2013-11-29 16:48:43 +01:00
|
|
|
* Copyright (C) 2013 Bastien Nocera <hadess@hadess.net>
|
2011-10-18 18:44:29 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <glib/gi18n-lib.h>
|
2011-11-07 17:50:40 +00:00
|
|
|
#include <shell/cc-shell.h>
|
2013-11-29 16:48:43 +01:00
|
|
|
#include <bluetooth-settings-widget.h>
|
2011-10-18 18:44:29 +01:00
|
|
|
|
|
|
|
#include "cc-bluetooth-panel.h"
|
2013-01-04 13:29:29 +01:00
|
|
|
#include "cc-bluetooth-resources.h"
|
2011-10-18 18:44:29 +01:00
|
|
|
|
|
|
|
|
2012-08-21 14:29:22 -04:00
|
|
|
CC_PANEL_REGISTER (CcBluetoothPanel, cc_bluetooth_panel)
|
2011-10-18 18:44:29 +01:00
|
|
|
|
|
|
|
#define BLUETOOTH_PANEL_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_BLUETOOTH_PANEL, CcBluetoothPanelPrivate))
|
|
|
|
|
|
|
|
#define WID(s) GTK_WIDGET (gtk_builder_get_object (self->priv->builder, s))
|
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
#define BLUETOOTH_DISABLED_PAGE "disabled-page"
|
|
|
|
#define BLUETOOTH_HW_DISABLED_PAGE "hw-disabled-page"
|
|
|
|
#define BLUETOOTH_NO_DEVICES_PAGE "no-devices-page"
|
|
|
|
#define BLUETOOTH_WORKING_PAGE "working-page"
|
2011-10-18 18:44:29 +01:00
|
|
|
|
|
|
|
struct CcBluetoothPanelPrivate {
|
|
|
|
GtkBuilder *builder;
|
2013-11-29 16:48:43 +01:00
|
|
|
GtkWidget *stack;
|
|
|
|
GtkWidget *widget;
|
2013-09-20 19:04:07 +02:00
|
|
|
GCancellable *cancellable;
|
2013-11-29 16:48:43 +01:00
|
|
|
|
|
|
|
/* Killswitch */
|
|
|
|
GtkWidget *kill_switch_header;
|
|
|
|
GDBusProxy *rfkill, *properties;
|
|
|
|
gboolean airplane_mode;
|
|
|
|
gboolean hardware_airplane_mode;
|
|
|
|
gboolean has_airplane_mode;
|
2011-10-18 18:44:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void cc_bluetooth_panel_finalize (GObject *object);
|
2012-12-17 10:51:07 +01:00
|
|
|
static void cc_bluetooth_panel_constructed (GObject *object);
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2012-05-18 16:59:27 +02:00
|
|
|
static const char *
|
|
|
|
cc_bluetooth_panel_get_help_uri (CcPanel *panel)
|
|
|
|
{
|
2013-11-29 16:48:43 +01:00
|
|
|
return "help:gnome-help/bluetooth";
|
2012-05-18 16:59:27 +02:00
|
|
|
}
|
|
|
|
|
2011-10-18 18:44:29 +01:00
|
|
|
static void
|
|
|
|
cc_bluetooth_panel_class_init (CcBluetoothPanelClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2012-05-18 16:59:27 +02:00
|
|
|
CcPanelClass *panel_class = CC_PANEL_CLASS (klass);
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2012-12-17 10:51:07 +01:00
|
|
|
object_class->constructed = cc_bluetooth_panel_constructed;
|
2011-10-18 18:44:29 +01:00
|
|
|
object_class->finalize = cc_bluetooth_panel_finalize;
|
|
|
|
|
2012-05-18 16:59:27 +02:00
|
|
|
panel_class->get_help_uri = cc_bluetooth_panel_get_help_uri;
|
|
|
|
|
2011-10-18 18:44:29 +01:00
|
|
|
g_type_class_add_private (klass, sizeof (CcBluetoothPanelPrivate));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc_bluetooth_panel_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
CcBluetoothPanel *self;
|
|
|
|
|
|
|
|
self = CC_BLUETOOTH_PANEL (object);
|
2013-11-29 16:48:43 +01:00
|
|
|
|
2013-09-20 19:04:07 +02:00
|
|
|
g_cancellable_cancel (self->priv->cancellable);
|
|
|
|
g_clear_object (&self->priv->cancellable);
|
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
g_clear_object (&self->priv->properties);
|
|
|
|
g_clear_object (&self->priv->rfkill);
|
2012-12-17 10:51:07 +01:00
|
|
|
g_clear_object (&self->priv->kill_switch_header);
|
2012-08-28 16:27:19 +01:00
|
|
|
|
2011-10-18 18:44:29 +01:00
|
|
|
G_OBJECT_CLASS (cc_bluetooth_panel_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2012-12-17 10:51:07 +01:00
|
|
|
static void
|
|
|
|
cc_bluetooth_panel_constructed (GObject *object)
|
|
|
|
{
|
|
|
|
CcBluetoothPanel *self = CC_BLUETOOTH_PANEL (object);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (cc_bluetooth_panel_parent_class)->constructed (object);
|
|
|
|
|
|
|
|
/* add kill switch widgets */
|
|
|
|
self->priv->kill_switch_header = g_object_ref (WID ("box_power"));
|
|
|
|
cc_shell_embed_widget_in_header (cc_panel_get_shell (CC_PANEL (self)),
|
|
|
|
self->priv->kill_switch_header);
|
|
|
|
gtk_widget_show_all (self->priv->kill_switch_header);
|
|
|
|
}
|
|
|
|
|
2011-10-18 18:44:29 +01:00
|
|
|
static void
|
|
|
|
power_callback (GObject *object,
|
|
|
|
GParamSpec *spec,
|
|
|
|
CcBluetoothPanel *self)
|
|
|
|
{
|
|
|
|
gboolean state;
|
|
|
|
|
|
|
|
state = gtk_switch_get_active (GTK_SWITCH (WID ("switch_bluetooth")));
|
2012-12-10 08:24:55 +01:00
|
|
|
g_debug ("Power switched to %s", state ? "on" : "off");
|
2013-11-29 16:48:43 +01:00
|
|
|
g_dbus_proxy_call (self->priv->properties,
|
|
|
|
"Set",
|
|
|
|
g_variant_new_parsed ("('org.gnome.SettingsDaemon.Rfkill', 'BluetoothAirplaneMode', %v)",
|
|
|
|
g_variant_new_boolean (!state)),
|
|
|
|
G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
-1,
|
|
|
|
self->priv->cancellable,
|
|
|
|
NULL, NULL);
|
2011-10-18 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-11-29 16:48:43 +01:00
|
|
|
cc_bluetooth_panel_update_power (CcBluetoothPanel *self)
|
2011-10-18 18:44:29 +01:00
|
|
|
{
|
2013-11-29 16:48:43 +01:00
|
|
|
GObject *toggle;
|
|
|
|
gboolean sensitive, powered;
|
|
|
|
const char *page;
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
g_debug ("Updating airplane mode: has_airplane_mode %d, hardware_airplane_mode %d, airplane_mode %d",
|
|
|
|
self->priv->has_airplane_mode, self->priv->hardware_airplane_mode, self->priv->airplane_mode);
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
if (self->priv->has_airplane_mode == FALSE) {
|
|
|
|
g_debug ("No Bluetooth available");
|
2011-10-18 18:44:29 +01:00
|
|
|
sensitive = FALSE;
|
2013-11-29 16:48:43 +01:00
|
|
|
powered = FALSE;
|
|
|
|
page = BLUETOOTH_NO_DEVICES_PAGE;
|
|
|
|
} else if (self->priv->hardware_airplane_mode) {
|
|
|
|
g_debug ("Bluetooth is Hard blocked");
|
2011-10-18 18:44:29 +01:00
|
|
|
sensitive = FALSE;
|
2013-11-29 16:48:43 +01:00
|
|
|
powered = FALSE;
|
|
|
|
page = BLUETOOTH_HW_DISABLED_PAGE;
|
|
|
|
} else if (self->priv->airplane_mode) {
|
|
|
|
g_debug ("Default adapter is unpowered, but should be available");
|
2011-10-18 18:44:29 +01:00
|
|
|
sensitive = TRUE;
|
2013-11-29 16:48:43 +01:00
|
|
|
powered = FALSE;
|
|
|
|
page = BLUETOOTH_DISABLED_PAGE;
|
|
|
|
} else {
|
2011-10-18 18:44:29 +01:00
|
|
|
g_debug ("Bluetooth is available and powered");
|
2013-11-29 16:48:43 +01:00
|
|
|
sensitive = TRUE;
|
|
|
|
powered = TRUE;
|
|
|
|
page = BLUETOOTH_WORKING_PAGE;
|
2011-10-18 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_set_sensitive (WID ("box_power") , sensitive);
|
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
toggle = G_OBJECT (WID ("switch_bluetooth"));
|
|
|
|
g_signal_handlers_block_by_func (toggle, power_callback, self);
|
|
|
|
gtk_switch_set_active (GTK_SWITCH (toggle), powered);
|
|
|
|
g_signal_handlers_unblock_by_func (toggle, power_callback, self);
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (self->priv->stack), page);
|
2011-10-18 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-11-29 16:48:43 +01:00
|
|
|
airplane_mode_changed (GDBusProxy *proxy,
|
|
|
|
GVariant *changed_properties,
|
|
|
|
GStrv invalidated_properties,
|
|
|
|
CcBluetoothPanel *self)
|
2011-10-18 18:44:29 +01:00
|
|
|
{
|
2013-11-29 16:48:43 +01:00
|
|
|
GVariant *v;
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
v = g_dbus_proxy_get_cached_property (self->priv->rfkill, "BluetoothAirplaneMode");
|
|
|
|
self->priv->airplane_mode = g_variant_get_boolean (v);
|
|
|
|
g_variant_unref (v);
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
v = g_dbus_proxy_get_cached_property (self->priv->rfkill, "BluetoothHardwareAirplaneMode");
|
|
|
|
self->priv->hardware_airplane_mode = g_variant_get_boolean (v);
|
|
|
|
g_variant_unref (v);
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
v = g_dbus_proxy_get_cached_property (self->priv->rfkill, "BluetoothHasAirplaneMode");
|
|
|
|
self->priv->has_airplane_mode = g_variant_get_boolean (v);
|
|
|
|
g_variant_unref (v);
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
cc_bluetooth_panel_update_power (self);
|
2011-10-18 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-11-29 16:48:43 +01:00
|
|
|
add_stack_page (CcBluetoothPanel *self,
|
|
|
|
const char *message,
|
|
|
|
const char *name)
|
2011-10-18 18:44:29 +01:00
|
|
|
{
|
2013-11-29 16:48:43 +01:00
|
|
|
GtkWidget *label;
|
2011-10-18 18:44:29 +01:00
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
label = gtk_label_new (message);
|
|
|
|
gtk_stack_add_named (GTK_STACK (self->priv->stack), label, name);
|
|
|
|
gtk_widget_show (label);
|
2011-10-18 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-11-29 16:48:43 +01:00
|
|
|
panel_changed (GtkWidget *settings_widget,
|
|
|
|
const char *panel,
|
|
|
|
CcBluetoothPanel *self)
|
2011-10-18 18:44:29 +01:00
|
|
|
{
|
2013-11-29 16:48:43 +01:00
|
|
|
CcShell *shell;
|
2011-10-18 18:44:29 +01:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
shell = cc_panel_get_shell (CC_PANEL (self));
|
|
|
|
if (cc_shell_set_active_panel_from_id (shell, panel, NULL, &error) == FALSE) {
|
|
|
|
g_warning ("Failed to activate '%s' panel: %s", panel, error->message);
|
2011-10-18 18:44:29 +01:00
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc_bluetooth_panel_init (CcBluetoothPanel *self)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
self->priv = BLUETOOTH_PANEL_PRIVATE (self);
|
2013-01-04 13:29:29 +01:00
|
|
|
g_resources_register (cc_bluetooth_get_resource ());
|
2011-10-18 18:44:29 +01:00
|
|
|
|
|
|
|
self->priv->builder = gtk_builder_new ();
|
|
|
|
gtk_builder_set_translation_domain (self->priv->builder, GETTEXT_PACKAGE);
|
2013-01-04 13:29:29 +01:00
|
|
|
gtk_builder_add_from_resource (self->priv->builder,
|
|
|
|
"/org/gnome/control-center/bluetooth/bluetooth.ui",
|
|
|
|
&error);
|
2011-10-18 18:44:29 +01:00
|
|
|
if (error != NULL) {
|
2013-01-04 13:29:29 +01:00
|
|
|
g_warning ("Could not load ui: %s", error->message);
|
2011-10-18 18:44:29 +01:00
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:48:43 +01:00
|
|
|
self->priv->cancellable = g_cancellable_new ();
|
|
|
|
|
|
|
|
/* RFKill */
|
|
|
|
self->priv->rfkill = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
|
|
|
|
G_DBUS_PROXY_FLAGS_NONE,
|
|
|
|
NULL,
|
|
|
|
"org.gnome.SettingsDaemon.Rfkill",
|
|
|
|
"/org/gnome/SettingsDaemon/Rfkill",
|
|
|
|
"org.gnome.SettingsDaemon.Rfkill",
|
|
|
|
NULL, NULL);
|
|
|
|
self->priv->properties = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
|
|
|
|
G_DBUS_PROXY_FLAGS_NONE,
|
|
|
|
NULL,
|
|
|
|
"org.gnome.SettingsDaemon.Rfkill",
|
|
|
|
"/org/gnome/SettingsDaemon/Rfkill",
|
|
|
|
"org.freedesktop.DBus.Properties",
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
self->priv->stack = gtk_stack_new ();
|
|
|
|
add_stack_page (self, _("Bluetooth is disabled"), BLUETOOTH_DISABLED_PAGE);
|
|
|
|
add_stack_page (self, _("No Bluetooth adapters found"), BLUETOOTH_NO_DEVICES_PAGE);
|
|
|
|
add_stack_page (self, _("Bluetooth is disabled by hardware switch"), BLUETOOTH_HW_DISABLED_PAGE);
|
|
|
|
|
|
|
|
self->priv->widget = bluetooth_settings_widget_new ();
|
|
|
|
g_signal_connect (G_OBJECT (self->priv->widget), "panel-changed",
|
|
|
|
G_CALLBACK (panel_changed), self);
|
|
|
|
gtk_stack_add_named (GTK_STACK (self->priv->stack),
|
|
|
|
self->priv->widget, BLUETOOTH_WORKING_PAGE);
|
|
|
|
gtk_widget_show (self->priv->widget);
|
|
|
|
gtk_widget_show (self->priv->stack);
|
|
|
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (self), self->priv->stack);
|
|
|
|
|
|
|
|
airplane_mode_changed (NULL, NULL, NULL, self);
|
|
|
|
g_signal_connect (self->priv->rfkill, "g-properties-changed",
|
|
|
|
G_CALLBACK (airplane_mode_changed), self);
|
|
|
|
|
2011-10-18 18:44:29 +01:00
|
|
|
g_signal_connect (G_OBJECT (WID ("switch_bluetooth")), "notify::active",
|
|
|
|
G_CALLBACK (power_callback), self);
|
|
|
|
}
|