Enabling multithreaded compilation.
[debian/lxpanel.git] / src / plugins / netstatus / netstatus.c
1 /**
2 * Copyright (c) 2006 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <gtk/gtk.h>
20 #include <stdlib.h>
21
22 #include <gdk-pixbuf/gdk-pixbuf.h>
23 #include <glib/gi18n.h>
24
25 #include "panel.h"
26 #include "misc.h"
27 #include "plugin.h"
28
29 #include "dbg.h"
30
31 #include "netstatus-icon.h"
32 #include "netstatus-dialog.h"
33
34 typedef struct {
35 Plugin* plugin;
36 char *iface;
37 char *config_tool;
38 GtkWidget *mainw;
39 GtkWidget *dlg;
40 } netstatus;
41
42
43 static void
44 netstatus_destructor(Plugin *p)
45 {
46 netstatus *ns = (netstatus *)p->priv;
47
48 ENTER;
49 /* The widget is destroyed in plugin_stop().
50 gtk_widget_destroy(ns->mainw);
51 */
52 g_free( ns->iface );
53 g_free( ns->config_tool );
54 g_free(ns);
55 RET();
56 }
57
58 static void on_response( GtkDialog* dlg, gint response, netstatus *ns )
59 {
60 const char* iface;
61 switch( response )
62 {
63 case GTK_RESPONSE_CLOSE:
64 case GTK_RESPONSE_DELETE_EVENT:
65 case GTK_RESPONSE_NONE:
66 iface = netstatus_dialog_get_iface_name((GtkWidget*)dlg);
67 if( iface )
68 {
69 g_free(ns->iface);
70 ns->iface = g_strdup(iface);
71 gtk_widget_destroy( GTK_WIDGET(dlg) );
72 ns->dlg = NULL;
73 }
74 }
75 }
76
77 static gboolean on_button_press( GtkWidget* widget, GdkEventButton* evt, Plugin* p )
78 {
79 NetstatusIface* iface;
80 netstatus *ns = (netstatus*)p->priv;
81
82 /* Standard right-click handling. */
83 if (plugin_button_press_event(widget, evt, p))
84 return TRUE;
85
86 if( evt->button == 1 ) /* Left click*/
87 {
88 if( ! ns->dlg )
89 {
90 iface = netstatus_icon_get_iface( NETSTATUS_ICON(widget) );
91 ns->dlg = netstatus_dialog_new(iface);
92
93 /* fix background */
94 gtk_widget_set_style(ns->dlg, p->panel->defstyle);
95
96 netstatus_dialog_set_configuration_tool( ns->dlg, ns->config_tool );
97 g_signal_connect( ns->dlg, "response", G_CALLBACK(on_response), ns );
98 }
99 gtk_window_present( GTK_WINDOW(ns->dlg) );
100 }
101 return TRUE;
102 }
103
104 static int
105 netstatus_constructor(Plugin *p, char** fp)
106 {
107 netstatus *ns;
108 line s;
109 NetstatusIface* iface;
110
111 ENTER;
112 s.len = 256;
113 ns = g_new0(netstatus, 1);
114 g_return_val_if_fail(ns != NULL, 0);
115 p->priv = ns;
116 ns->plugin = p;
117 if( fp )
118 {
119 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END) {
120 if (s.type == LINE_NONE) {
121 ERR( "netstatus: illegal token %s\n", s.str);
122 goto error;
123 }
124 if (s.type == LINE_VAR) {
125 if (!g_ascii_strcasecmp(s.t[0], "iface"))
126 ns->iface = g_strdup(s.t[1]);
127 else if (!g_ascii_strcasecmp(s.t[0], "configtool"))
128 ns->config_tool = g_strdup(s.t[1]);
129 else {
130 ERR( "netstatus: unknown var %s\n", s.t[0]);
131 }
132 } else {
133 ERR( "netstatus: illegal in this context %s\n", s.str);
134 goto error;
135 }
136 }
137 }
138 else
139 {
140 ns->iface = g_strdup("eth0");
141 ns->config_tool = g_strdup("network-admin --configure %i");
142 }
143
144 iface = netstatus_iface_new(ns->iface);
145 ns->mainw = netstatus_icon_new( iface );
146 netstatus_icon_set_show_signal((NetstatusIcon *)ns->mainw, TRUE);
147 gtk_widget_add_events( ns->mainw, GDK_BUTTON_PRESS_MASK );
148 g_object_unref( iface );
149 g_signal_connect( ns->mainw, "button-press-event",
150 G_CALLBACK(on_button_press), p );
151
152 gtk_widget_show(ns->mainw);
153
154 p->pwid = ns->mainw;
155
156 RET(1);
157
158 error:
159 RET(0);
160 }
161
162 static void apply_config(Plugin* p)
163 {
164 netstatus *ns = (netstatus *)p->priv;
165 NetstatusIface* iface;
166
167 iface = netstatus_iface_new(ns->iface);
168 netstatus_icon_set_iface((NetstatusIcon *)ns->mainw, iface);
169 }
170
171 static void netstatus_config( Plugin* p, GtkWindow* parent )
172 {
173 GtkWidget* dlg;
174 netstatus *ns = (netstatus*)p->priv;
175 dlg = create_generic_config_dlg(
176 _(p->class->name),
177 GTK_WIDGET(parent),
178 (GSourceFunc) apply_config, p,
179 _("Interface to monitor"), &ns->iface, CONF_TYPE_STR,
180 _("Config tool"), &ns->config_tool, CONF_TYPE_STR,
181 NULL );
182 gtk_window_present( GTK_WINDOW(dlg) );
183 }
184
185 static void save_config( Plugin* p, FILE* fp )
186 {
187 netstatus *ns = (netstatus*)p->priv;
188 lxpanel_put_str( fp, "iface", ns->iface );
189 lxpanel_put_str( fp, "configtool", ns->config_tool );
190 }
191
192 PluginClass netstatus_plugin_class = {
193
194 PLUGINCLASS_VERSIONING,
195
196 type : "netstatus",
197 name : N_("Network Status Monitor"),
198 version: "1.0",
199 description : N_("Monitor network status"),
200
201 /* Reloading netstatus results in segfault due to registering static type. */
202 not_unloadable : TRUE,
203
204 constructor : netstatus_constructor,
205 destructor : netstatus_destructor,
206 config : netstatus_config,
207 save : save_config
208 };