X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=plugin.c;h=e7e9ad013dedcb399c2eadb7a09e86a13af0705d;hb=HEAD;hp=acfc390c25009000fccdc6e7605b8d1a4aeb8776;hpb=ac639792511f8baf419dd92c8a56ebfcb9b6d27c;p=mtetherd diff --git a/plugin.c b/plugin.c index acfc390..e7e9ad0 100644 --- a/plugin.c +++ b/plugin.c @@ -34,17 +34,11 @@ #define MTETHERD_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginPrivate)) -typedef enum { - MTETHERD_STATUS_PLUGIN_USB_NET_UNKNOWN = 0, - MTETHERD_STATUS_PLUGIN_USB_NET_DISABLED, - MTETHERD_STATUS_PLUGIN_USB_NET_ENABLED, -} MTetherDStatusPluginUsbNetState; - struct _MTetherDStatusPluginPrivate { GtkWidget *enable_button; gpointer devices; gboolean usb_plugged; - MTetherDStatusPluginUsbNetState usbnet_state; + gboolean usbnet_state; FILE *log_fp; guint log_handler; }; @@ -65,6 +59,7 @@ struct _MTetherDStatusPluginPrivate { // The UDI contains the MAC address and is thus unsuitable for // loaded status checking, so we just use the interface name static const char *USBNET_INTERFACE = "usb0"; +static const char *WAN_INTERFACE = "gprs0"; const char *MTETHERD_LOG_DOMAIN = "mtetherd"; HD_DEFINE_PLUGIN_MODULE(MTetherDStatusPlugin, mtetherd_status_plugin, HD_TYPE_STATUS_MENU_ITEM); @@ -97,17 +92,11 @@ static void mtetherd_status_plugin_class_init(MTetherDStatusPluginClass *klass) static void mtetherd_status_plugin_enable_button_set_text(MTetherDStatusPlugin *plugin) { if (plugin && plugin->priv && plugin->priv->enable_button) { - switch (plugin->priv->usbnet_state) { - case MTETHERD_STATUS_PLUGIN_USB_NET_ENABLED: - hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "USB networking", "Enabled"); - break; - case MTETHERD_STATUS_PLUGIN_USB_NET_DISABLED: - hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "USB networking", "Disabled"); - break; - default: - hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "USB networking", "Unknown"); - break; - } + if (plugin->priv->usbnet_state) { + hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "USB networking", "Enabled"); + } else { + hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "USB networking", "Disabled"); + } } } @@ -116,16 +105,10 @@ static void mtetherd_status_plugin_enable_button_clicked(GtkWidget *button, gpoi if (plugin && plugin->priv && button == plugin->priv->enable_button) { const char *arg; - switch (plugin->priv->usbnet_state) { - case MTETHERD_STATUS_PLUGIN_USB_NET_ENABLED: - arg = SBIN_DIR "/mtetherd-usbnet-disable.sh"; - break; - case MTETHERD_STATUS_PLUGIN_USB_NET_DISABLED: - arg = SBIN_DIR "/mtetherd-usbnet-enable.sh"; - break; - default: - arg = NULL; - break; + if (plugin->priv->usbnet_state) { + arg = SBIN_DIR "/mtetherd-usbnet-disable.sh"; + } else { + arg = SBIN_DIR "/mtetherd-usbnet-enable.sh"; } if (arg) { g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Launching %s", arg); @@ -195,6 +178,7 @@ static void mtetherd_status_plugin_init(MTetherDStatusPlugin *plugin) { plugin->priv->log_handler = g_log_set_handler(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_MASK, mtetherd_status_plugin_log, plugin); } + plugin->priv->usbnet_state = FALSE; plugin->priv->enable_button = hildon_button_new(HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL); if (plugin->priv->enable_button) { GError *err = NULL; @@ -235,6 +219,7 @@ static void mtetherd_status_plugin_init(MTetherDStatusPlugin *plugin) { void mtetherd_status_plugin_device_added(MTetherDStatusPlugin *plugin, MTetherDDevice *device) { g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "mtetherd_status_plugin_device_added(%p, %p)", plugin, device); + gboolean added = FALSE; if (plugin && plugin->priv) { const gchar *interface = mtetherd_device_get_interface(device); g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "interface=%s", interface); @@ -243,7 +228,7 @@ void mtetherd_status_plugin_device_added(MTetherDStatusPlugin *plugin, MTetherDD if (mtetherd_device_list_add(plugin->priv->devices, device)) { if (g_strcmp0(USBNET_INTERFACE, interface) == 0) { g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "is USB"); - plugin->priv->usbnet_state = MTETHERD_STATUS_PLUGIN_USB_NET_ENABLED; + plugin->priv->usbnet_state = TRUE; mtetherd_status_plugin_enable_button_set_text(plugin); } hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Starting network on %s", interface); @@ -252,8 +237,8 @@ void mtetherd_status_plugin_device_added(MTetherDStatusPlugin *plugin, MTetherDD gchar *netmask = mtetherd_device_get_netmask(device); gchar *dhcp_start = mtetherd_device_get_dhcp_start(device); gchar *dhcp_end = mtetherd_device_get_dhcp_end(device); - g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "addr=%s netmask=%s dhcp_start=%s dhcp_end=%s", addr, netmask, dhcp_start, dhcp_end); - const char *command[] = { BIN_DIR "/sudo", SBIN_DIR "/mtetherd-net-setup.sh", interface, addr, netmask, dhcp_start, dhcp_end, NULL }; + g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "interface=%s wan=%s addr=%s netmask=%s dhcp_start=%s dhcp_end=%s", interface, WAN_INTERFACE, addr, netmask, dhcp_start, dhcp_end); + const char *command[] = { BIN_DIR "/sudo", SBIN_DIR "/mtetherd-net-setup.sh", interface, WAN_INTERFACE, addr, netmask, dhcp_start, dhcp_end, NULL }; if (!mtetherd_launch_script(command)) { g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error launching USB networking setup script"); } @@ -262,28 +247,33 @@ void mtetherd_status_plugin_device_added(MTetherDStatusPlugin *plugin, MTetherDD g_free(dhcp_start); g_free(dhcp_end); g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "done"); + added = TRUE; } else { g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error adding network interface to list: Maximum number of devices exceeded"); } } } + if (!added) { + g_object_unref(G_OBJECT(device)); + } } void mtetherd_status_plugin_device_removed(MTetherDStatusPlugin *plugin, const gchar *udi) { g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "mtetherd_status_plugin_device_removed(%s)", udi); if (plugin && plugin->priv) { MTetherDDevice *device = mtetherd_device_list_find(plugin->priv->devices, udi); - g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "device=%p", device); if (device) { + g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "device=%p", device); const gchar *interface = mtetherd_device_get_interface(device); g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "interface=%s", interface); if (g_strcmp0(USBNET_INTERFACE, interface) == 0) { g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "is USB"); - plugin->priv->usbnet_state = MTETHERD_STATUS_PLUGIN_USB_NET_DISABLED; + plugin->priv->usbnet_state = FALSE; mtetherd_status_plugin_enable_button_set_text(plugin); } hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Shutting down network on %s", interface); g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Launching %s", SBIN_DIR "/mtetherd-net-shutdown.sh"); + // TODO: Check if this is the last interface to be shut down and pass WAN_INTERFACE after interface if yes const char *command[] = { BIN_DIR "/sudo", SBIN_DIR "/mtetherd-net-shutdown.sh", interface, NULL }; if (!mtetherd_launch_script(command)) { g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error launching USB networking shutdown script"); @@ -291,7 +281,9 @@ void mtetherd_status_plugin_device_removed(MTetherDStatusPlugin *plugin, const g g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "done"); } if (!mtetherd_device_list_remove(plugin->priv->devices, udi)) { - g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error removing network interface from list: Not found"); + g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Device %s not found in list, nothing removed", udi); + } else { + g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Device %s removed from list", udi); } } }