Split up functionality of plugin in separate modules
[mtetherd] / plugin.c
1 /*
2   mtetherd
3   (c) 2010 Gregor Riepl <onitake@gmail.com>
4   
5   Tethering utility for Maemo
6   
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.
11   
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.
16   
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/>.
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <pwd.h>
26 #include <sys/types.h>
27 #include <gtk/gtk.h>
28 #include <hildon/hildon.h>
29
30 #include "plugin.h"
31
32 #define MTETHERD_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginPrivate))
33
34 struct _MTetherDStatusPluginPrivate {
35         GtkWidget *enable_button;
36         gboolean usb_on;
37         gboolean net_on;
38 };
39
40 #ifndef IMAGE_DIR
41 #define IMAGE_DIR "/usr/share/pixmaps"
42 #endif
43 #ifndef SBIN_DIR
44 #define SBIN_DIR "/usr/sbin"
45 #endif
46
47 HD_DEFINE_PLUGIN_MODULE(MTetherDStatusPlugin, mtetherd_status_plugin, HD_TYPE_STATUS_MENU_ITEM);
48
49 static void mtetherd_status_plugin_class_finalize(MTetherDStatusPluginClass *klass) { }
50
51 static void mtetherd_status_plugin_finalize(GObject *object) {
52         g_message("Destroying mtetherd status plugin");
53
54         MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(object);
55
56         if (plugin) {
57                 mtetherd_hal_finalize(plugin);
58         }
59 }
60
61 static void mtetherd_status_plugin_class_init(MTetherDStatusPluginClass *klass) {
62         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
63
64         gobject_class->finalize = mtetherd_status_plugin_finalize;
65         g_type_class_add_private(klass, sizeof(MTetherDStatusPluginPrivate));
66 }
67
68 static void enable_button_set_text(GtkWidget *button, gboolean enabled) {
69         if (enabled) {
70                 hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Enabled");
71         } else {
72                 hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Disabled");
73         }
74 }
75
76 static void enable_button_clicked(GtkWidget *button, gpointer data) {
77         MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(data);
78
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");
83                         }
84                 } else {
85                         if (!launch_usbnet_script(TRUE)) {
86                                 g_error("Error starting USB networking");
87                         }
88                 }
89         }
90 }
91
92 static void mtetherd_status_plugin_usb_plugged_show(MTetherDStatusPlugin *plugin) {
93         if (plugin) {
94                 if (plugin->priv && plugin->priv->hal_context) {
95                         DBusError derr;
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);
102                         } else {
103                                 plugin->priv->usb_on = plugged;
104                                 if (plugin->priv->usb_on) {
105                                         gtk_widget_show(GTK_WIDGET(plugin));
106                                 } else {
107                                         gtk_widget_hide(GTK_WIDGET(plugin));
108                                 }
109                         }
110                 } else {
111                         // DEBUG
112                         //gtk_widget_show(GTK_WIDGET(plugin));
113                 }
114         }
115 }
116
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);
120         
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);
125                 }
126         }
127 }
128
129 static void mtetherd_status_plugin_init(MTetherDStatusPlugin *plugin) {
130         plugin->priv = MTETHERD_STATUS_PLUGIN_GET_PRIVATE(plugin);
131
132         plugin->priv->usb_on = FALSE;
133         plugin->priv->net_on = FALSE;
134         
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) {
137                 GError *err = NULL;
138                 GdkPixbuf *icon = gdk_pixbuf_new_from_file(IMAGE_DIR "/mtetherd-net-icon.png", &err);
139                 if (err) {
140                         g_warning("Can't load mtetherd icon: %s", err->message);
141                         g_error_free(err);
142                         err = NULL;
143                 }
144                 if (icon) {
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);
149                 }
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);
155         }
156
157         mtetherd_hal_init(plugin);
158
159         mtetherd_status_plugin_usb_plugged_show(plugin);
160
161         //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Initialized mtetherd status plugin");
162 }
163