3 (c) 2010 Gregor Riepl <onitake@gmail.com>
5 Tethering utility for Maemo
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
28 #include <hildon/hildon.h>
32 #define MTETHERD_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginPrivate))
34 struct _MTetherDStatusPluginPrivate {
35 GtkWidget *enable_button;
41 #define IMAGE_DIR "/usr/share/pixmaps"
44 #define SBIN_DIR "/usr/sbin"
47 HD_DEFINE_PLUGIN_MODULE(MTetherDStatusPlugin, mtetherd_status_plugin, HD_TYPE_STATUS_MENU_ITEM);
49 static void mtetherd_status_plugin_class_finalize(MTetherDStatusPluginClass *klass) { }
51 static void mtetherd_status_plugin_finalize(GObject *object) {
52 g_message("Destroying mtetherd status plugin");
54 MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(object);
57 mtetherd_hal_finalize(plugin);
61 static void mtetherd_status_plugin_class_init(MTetherDStatusPluginClass *klass) {
62 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
64 gobject_class->finalize = mtetherd_status_plugin_finalize;
65 g_type_class_add_private(klass, sizeof(MTetherDStatusPluginPrivate));
68 static void enable_button_set_text(GtkWidget *button, gboolean enabled) {
70 hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Enabled");
72 hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Disabled");
76 static void enable_button_clicked(GtkWidget *button, gpointer data) {
77 MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(data);
79 if (plugin && plugin->priv && button == plugin->priv->enable_button) {
80 if (plugin->priv->net_on) {
81 if (!launch_usbnet_script(FALSE)) {
82 g_error("Error starting USB networking");
85 if (!launch_usbnet_script(TRUE)) {
86 g_error("Error starting USB networking");
92 static void mtetherd_status_plugin_usb_plugged_show(MTetherDStatusPlugin *plugin) {
94 if (plugin->priv && plugin->priv->hal_context) {
96 dbus_error_init(&derr);
97 dbus_bool_t plugged = libhal_device_get_property_bool(plugin->priv->hal_context, USBDEV_PATH, "button.state.value", &derr);
98 if (dbus_error_is_set(&derr)) {
99 g_warning("Error getting USB plugged status (%s): %s", derr.name, derr.message);
100 hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error getting USB plugged status (%s): %s", derr.name, derr.message);
101 dbus_error_free(&derr);
103 plugin->priv->usb_on = plugged;
104 if (plugin->priv->usb_on) {
105 gtk_widget_show(GTK_WIDGET(plugin));
107 gtk_widget_hide(GTK_WIDGET(plugin));
112 //gtk_widget_show(GTK_WIDGET(plugin));
117 static void mtetherd_status_plugin_mapped(GtkWidget *widget, gpointer data) {
118 hildon_banner_show_informationf(widget, NULL, "Plugin mapped");
119 MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(widget);
121 if (plugin && plugin->priv) {
122 plugin->priv->net_on = get_usbnet_enabled(plugin);
123 if (plugin->priv->enable_button) {
124 enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
129 static void mtetherd_status_plugin_init(MTetherDStatusPlugin *plugin) {
130 plugin->priv = MTETHERD_STATUS_PLUGIN_GET_PRIVATE(plugin);
132 plugin->priv->usb_on = FALSE;
133 plugin->priv->net_on = FALSE;
135 plugin->priv->enable_button = hildon_button_new(HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
136 if (plugin->priv->enable_button) {
138 GdkPixbuf *icon = gdk_pixbuf_new_from_file(IMAGE_DIR "/mtetherd-net-icon.png", &err);
140 g_warning("Can't load mtetherd icon: %s", err->message);
145 GtkWidget *image = gtk_image_new_from_pixbuf(icon);
146 hildon_button_set_image(HILDON_BUTTON(plugin->priv->enable_button), image);
147 hildon_button_set_image_position(HILDON_BUTTON(plugin->priv->enable_button), GTK_POS_LEFT);
148 g_object_unref(icon);
150 gboolean enabled = get_usbnet_enabled(plugin);
151 enable_button_set_text(plugin->priv->enable_button, enabled);
152 g_signal_connect(plugin->priv->enable_button, "clicked", G_CALLBACK(enable_button_clicked), plugin);
153 gtk_container_add(GTK_CONTAINER(plugin), plugin->priv->enable_button);
154 gtk_widget_show_all(plugin->priv->enable_button);
157 mtetherd_hal_init(plugin);
159 mtetherd_status_plugin_usb_plugged_show(plugin);
161 //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Initialized mtetherd status plugin");