Split up functionality of plugin in separate modules
[mtetherd] / status.c
diff --git a/status.c b/status.c
deleted file mode 100644 (file)
index 4d7ec06..0000000
--- a/status.c
+++ /dev/null
@@ -1,297 +0,0 @@
-/*
-  mtetherd
-  (c) 2010 Gregor Riepl <onitake@gmail.com>
-  
-  Tethering utility for Maemo
-  
-  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 3 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, see <http://www.gnu.org/licenses/>.
-*/
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <pwd.h>
-#include <sys/types.h>
-#include <gtk/gtk.h>
-#include <hildon/hildon.h>
-#include <dbus/dbus-glib-lowlevel.h>
-#include <libhal.h>
-
-#include "status.h"
-
-#define MTETHERD_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginPrivate))
-
-struct _MTetherDStatusPluginPrivate {
-       GtkWidget *enable_button;
-       gboolean usb_on;
-       gboolean net_on;
-       DBusGConnection *dbus_connection;
-       LibHalContext *hal_context;
-};
-
-static const char *USBDEV_PATH = "/org/freedesktop/Hal/devices/usb_device_1d6b_2_musb_hdrc";
-static const char *USBNET_MODULE = "g_ether";
-
-#ifndef IMAGE_DIR
-#define IMAGE_DIR "/usr/share/pixmaps"
-#endif
-#ifndef SBIN_DIR
-#define SBIN_DIR "/usr/sbin"
-#endif
-
-HD_DEFINE_PLUGIN_MODULE(MTetherDStatusPlugin, mtetherd_status_plugin, HD_TYPE_STATUS_MENU_ITEM);
-
-static void mtetherd_status_plugin_class_finalize(MTetherDStatusPluginClass *klass) { }
-
-static void mtetherd_status_plugin_finalize(GObject *object) {
-       g_message("Destroying mtetherd status plugin");
-
-       MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(object);
-
-       if (plugin && plugin->priv) {
-               if (plugin->priv->hal_context) {
-                       libhal_ctx_shutdown(plugin->priv->hal_context, NULL);
-                       libhal_ctx_free(plugin->priv->hal_context);
-               }
-               if (plugin->priv->dbus_connection) {
-                       dbus_g_connection_unref(plugin->priv->dbus_connection);
-               }
-       }
-}
-
-static void mtetherd_status_plugin_class_init(MTetherDStatusPluginClass *klass) {
-       GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
-
-       gobject_class->finalize = mtetherd_status_plugin_finalize;
-       g_type_class_add_private(klass, sizeof(MTetherDStatusPluginPrivate));
-}
-
-static gboolean get_usbnet_enabled(MTetherDStatusPlugin *plugin) {
-       g_message("Scanning /proc/modules");
-       //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Scanning /proc/modules");
-
-       FILE *fp = fopen("/proc/modules", "r");
-       if (!fp) {
-               return FALSE;
-       }
-       gboolean found = FALSE;
-       char *line = NULL;
-       while (!found && getline(&line, NULL, fp) != -1) {
-               g_message("Checking if '%s' contains g_ether...", line);
-               if (strncmp(line, USBNET_MODULE, sizeof(USBNET_MODULE) - 1) == 0) {
-                       hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Found g_ether");
-                       found = TRUE;
-               }
-               free(line);
-               line = NULL;
-       }
-       fclose(fp);
-       return found;
-}
-
-static gboolean launch_usbnet_script(gboolean enable) {
-       const char *arg;
-       if (enable) {
-               arg = SBIN_DIR "mtetherd-usbnet-enable.sh";
-       } else {
-               arg = SBIN_DIR "mtetherd-usbnet-disable.sh";
-       }
-       g_debug("Launching %s", arg);
-       const char *command[] = { "/usr/bin/sudo", arg, NULL };
-       pid_t pid = fork();
-       if (pid == 0) {
-               if (execv(command[0], (char **const) command) == -1) {
-                       exit(1);
-               }
-       } else if (pid == -1) {
-               // error forking
-               return FALSE;
-       }
-       // launch ok
-       return TRUE;
-}
-
-static void enable_button_set_text(GtkWidget *button, gboolean enabled) {
-       if (enabled) {
-               hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Enabled");
-       } else {
-               hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Disabled");
-       }
-}
-
-static void enable_button_clicked(GtkWidget *button, gpointer data) {
-       MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(data);
-
-       if (plugin && plugin->priv && button == plugin->priv->enable_button) {
-               if (plugin->priv->net_on) {
-                       if (!launch_usbnet_script(FALSE)) {
-                               g_error("Error starting USB networking");
-                       }
-               } else {
-                       if (!launch_usbnet_script(TRUE)) {
-                               g_error("Error starting USB networking");
-                       }
-               }
-       }
-}
-
-static void mtetherd_status_plugin_usb_plugged_show(MTetherDStatusPlugin *plugin) {
-       if (plugin) {
-               if (plugin->priv && plugin->priv->hal_context) {
-                       DBusError derr;
-                       dbus_error_init(&derr);
-                       dbus_bool_t plugged = libhal_device_get_property_bool(plugin->priv->hal_context, USBDEV_PATH, "button.state.value", &derr);
-                       if (dbus_error_is_set(&derr)) {
-                               g_warning("Error getting USB plugged status (%s): %s", derr.name, derr.message);
-                               hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error getting USB plugged status (%s): %s", derr.name, derr.message);
-                               dbus_error_free(&derr);
-                       } else {
-                               plugin->priv->usb_on = plugged;
-                               if (plugin->priv->usb_on) {
-                                       gtk_widget_show(GTK_WIDGET(plugin));
-                               } else {
-                                       gtk_widget_hide(GTK_WIDGET(plugin));
-                               }
-                       }
-               } else {
-                       // DEBUG
-                       //gtk_widget_show(GTK_WIDGET(plugin));
-               }
-       }
-}
-
-static void mtetherd_status_plugin_mapped(GtkWidget *widget, gpointer data) {
-       hildon_banner_show_informationf(widget, NULL, "Plugin mapped");
-       MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(widget);
-       
-       if (plugin && plugin->priv) {
-               plugin->priv->net_on = get_usbnet_enabled(plugin);
-               if (plugin->priv->enable_button) {
-                       enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
-               }
-       }
-}
-
-static gboolean mtetherd_status_plugin_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) {
-       g_message("Got event %d", event->type);
-       return FALSE;
-}
-
-static void mtetherd_status_plugin_device_condition(LibHalContext *ctx, const char *udi, const char *condition, const char *detail) {
-       MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
-       
-       if (plugin) {
-               g_message("Got HAL condition %s on %s: %s", condition, udi, detail);
-               //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Got HAL condition %s on %s: %s", condition, udi, detail);
-               if (strcmp(USBDEV_CONDITION, "ButtonPressed") == 0) {
-                       if (strcmp(USBDEV_PATH, udi) == 0) {
-                               mtetherd_status_plugin_usb_plugged_show(plugin);
-                       }
-               } else if (strcmp("DeviceAdded", condition) == 0) {
-                       if (strcmp(USBDEV_PATH, detail) == 0) {
-                               plugin->priv->net_on = TRUE;
-                               enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
-                       }
-               } else if (strcmp("DeviceRemoved", condition) == 0) {
-                       if (strcmp(USBDEV_PATH, detail) == 0) {
-                               plugin->priv->net_on = FALSE;
-                               enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
-                       }
-               }
-       }
-}
-
-static void mtetherd_status_plugin_init(MTetherDStatusPlugin *plugin) {
-       plugin->priv = MTETHERD_STATUS_PLUGIN_GET_PRIVATE(plugin);
-
-       plugin->priv->usb_on = FALSE;
-       plugin->priv->net_on = 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;
-               GdkPixbuf *icon = gdk_pixbuf_new_from_file(IMAGE_DIR "/mtetherd-net-icon.png", &err);
-               if (err) {
-                       g_warning("Can't load mtetherd icon: %s", err->message);
-                       hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Can't load mtetherd icon: %s", err->message);
-                       g_error_free(err);
-                       err = NULL;
-               }
-               if (icon) {
-                       GtkWidget *image = gtk_image_new_from_pixbuf(icon);
-                       hildon_button_set_image(HILDON_BUTTON(plugin->priv->enable_button), image);
-                       hildon_button_set_image_position(HILDON_BUTTON(plugin->priv->enable_button), GTK_POS_LEFT);
-                       g_object_unref(icon);
-               }
-               gboolean enabled = get_usbnet_enabled(plugin);
-               enable_button_set_text(plugin->priv->enable_button, enabled);
-               g_signal_connect(plugin->priv->enable_button, "clicked", G_CALLBACK(enable_button_clicked), plugin);
-               gtk_container_add(GTK_CONTAINER(plugin), plugin->priv->enable_button);
-               gtk_widget_show_all(plugin->priv->enable_button);
-       }
-       //g_signal_connect(plugin, "expose-event", G_CALLBACK(mtetherd_status_plugin_event), NULL);
-
-       //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Initialized mtetherd status plugin");
-       GError *err = NULL;
-       plugin->priv->dbus_connection = hd_status_plugin_item_get_dbus_g_connection(HD_STATUS_PLUGIN_ITEM(plugin), DBUS_BUS_SYSTEM, &err);
-       if (err) {
-               g_warning("Can't open DBUS connection: %s", err->message);
-               hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error opening DBUS connection: %s", err->message);
-               g_error_free(err);
-               err = NULL;
-       } else {
-               g_message("Got DBUS Glib connection: %p", plugin->priv->dbus_connection);
-       }
-       if (plugin->priv->dbus_connection) {
-               plugin->priv->hal_context = libhal_ctx_new();
-               if (plugin->priv->hal_context) {
-                       if (libhal_ctx_set_dbus_connection(plugin->priv->hal_context, dbus_g_connection_get_connection(plugin->priv->dbus_connection))) {
-                               if (!libhal_ctx_set_user_data(plugin->priv->hal_context, plugin)) {
-                                       g_warning("Can't set user data of HAL context");
-                                       hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Can't set user data of HAL context");
-                               }
-                               if (!libhal_ctx_set_device_condition(plugin->priv->hal_context, mtetherd_status_plugin_device_condition)) {
-                                       g_warning("Error assigning device condition callback");
-                                       hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error assigning device condition callback");
-                               }
-                               DBusError derr;
-                               dbus_error_init(&derr);
-                               if (!libhal_ctx_init(plugin->priv->hal_context, &derr)) {
-                                       if (dbus_error_is_set(&derr)) {
-                                               g_warning("Error initializing HAL context (%s): %s", derr.name, derr.message);
-                                               hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error initializing HAL context (%s): %s", derr.name, derr.message);
-                                               dbus_error_free(&derr);
-                                       } else {
-                                               g_warning("Error initializing HAL context: unknown error");
-                                               //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error initializing HAL context: unknown error");
-                                       }
-                                       libhal_ctx_free(plugin->priv->hal_context);
-                                       plugin->priv->hal_context = NULL;
-                               }
-                       } else {
-                               g_warning("Can't set DBUS connection of HAL context");
-                               hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Can't set DBUS connection of HAL context");
-                               libhal_ctx_free(plugin->priv->hal_context);
-                               plugin->priv->hal_context = NULL;
-                       }
-               } else {
-                       g_warning("Can't allocate HAL context");
-                       hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Can't allocate HAL context");
-               }
-       }
-
-       mtetherd_status_plugin_usb_plugged_show(plugin);
-}
-