Rename panel, plugin, and plugin_class to Panel, Plugin, and
[lxde/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 char *iface;
36 char *config_tool;
37 GtkWidget *mainw;
38 GtkWidget *dlg;
39 } netstatus;
40
41
42 static void
43 netstatus_destructor(Plugin *p)
44 {
45 netstatus *ns = (netstatus *)p->priv;
46
47 ENTER;
48 /* The widget is destroyed in plugin_stop().
49 gtk_widget_destroy(ns->mainw);
50 */
51 g_free( ns->iface );
52 g_free( ns->config_tool );
53 g_free(ns);
54 RET();
55 }
56
57 static void on_response( GtkDialog* dlg, gint response, netstatus *ns )
58 {
59 switch( response )
60 {
61 case GTK_RESPONSE_CLOSE:
62 case GTK_RESPONSE_DELETE_EVENT:
63 case GTK_RESPONSE_NONE:
64 gtk_widget_destroy( GTK_WIDGET(dlg) );
65 ns->dlg = NULL;
66 }
67 }
68
69 static void on_button_press( GtkWidget* widget, GdkEventButton* evt, Plugin* p )
70 {
71 NetstatusIface* iface;
72 netstatus *ns = (netstatus*)p->priv;
73
74 if( evt->button == 1 ) /* Left click*/
75 {
76 if( ! ns->dlg )
77 {
78 iface = netstatus_icon_get_iface( NETSTATUS_ICON(widget) );
79 ns->dlg = netstatus_dialog_new(iface);
80
81 /* fix background */
82 gtk_widget_set_style(ns->dlg, p->panel->defstyle);
83
84 netstatus_dialog_set_configuration_tool( ns->dlg, ns->config_tool );
85 g_signal_connect( ns->dlg, "response", G_CALLBACK(on_response), ns );
86 }
87 gtk_window_present( GTK_WINDOW(ns->dlg) );
88 }
89 }
90
91 static int
92 netstatus_constructor(Plugin *p, char** fp)
93 {
94 netstatus *ns;
95 line s;
96 NetstatusIface* iface;
97
98 ENTER;
99 s.len = 256;
100 ns = g_new0(netstatus, 1);
101 g_return_val_if_fail(ns != NULL, 0);
102 p->priv = ns;
103 if( fp )
104 {
105 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END) {
106 if (s.type == LINE_NONE) {
107 ERR( "netstatus: illegal token %s\n", s.str);
108 goto error;
109 }
110 if (s.type == LINE_VAR) {
111 if (!g_ascii_strcasecmp(s.t[0], "iface"))
112 ns->iface = g_strdup(s.t[1]);
113 else if (!g_ascii_strcasecmp(s.t[0], "configtool"))
114 ns->config_tool = g_strdup(s.t[1]);
115 else {
116 ERR( "netstatus: unknown var %s\n", s.t[0]);
117 goto error;
118 }
119 } else {
120 ERR( "netstatus: illegal in this context %s\n", s.str);
121 goto error;
122 }
123 }
124 }
125 else
126 {
127 ns->iface = g_strdup("eth0");
128 ns->config_tool = g_strdup("network-admin --configure %i");
129 }
130
131 iface = netstatus_iface_new(ns->iface);
132 ns->mainw = netstatus_icon_new( iface );
133 gtk_widget_add_events( ns->mainw, GDK_BUTTON_PRESS_MASK );
134 g_object_unref( iface );
135 g_signal_connect( ns->mainw, "button-press-event",
136 G_CALLBACK(on_button_press), p );
137 gtk_widget_set_size_request( ns->mainw, 26, 24 );
138
139 gtk_widget_show_all(ns->mainw);
140
141 p->pwid = ns->mainw;
142
143 RET(1);
144
145 error:
146 netstatus_destructor(p);
147 RET(0);
148 }
149
150 static void apply_config( Plugin* p )
151 {
152
153 }
154
155 static void netstatus_config( Plugin* p, GtkWindow* parent )
156 {
157 GtkWidget* dlg;
158 netstatus *ns = (netstatus*)p->priv;
159 dlg = create_generic_config_dlg(
160 _(p->class->name),
161 GTK_WIDGET(parent),
162 (GSourceFunc) apply_config, p,
163 _("Interface to monitor"), &ns->iface, G_TYPE_STRING,
164 _("Config tool"), &ns->config_tool, G_TYPE_STRING,
165 NULL );
166 gtk_window_present( GTK_WINDOW(dlg) );
167 }
168
169 static void save_config( Plugin* p, FILE* fp )
170 {
171 netstatus *ns = (netstatus*)p->priv;
172 lxpanel_put_str( fp, "iface", ns->iface );
173 lxpanel_put_str( fp, "configtool", ns->config_tool );
174 }
175
176 PluginClass netstatus_plugin_class = {
177 fname: NULL,
178 count: 0,
179
180 type : "netstatus",
181 name : N_("Net Status Monitor"),
182 version: "1.0",
183 description : N_("Monitor network status"),
184
185 constructor : netstatus_constructor,
186 destructor : netstatus_destructor,
187 config : netstatus_config,
188 save : save_config
189 };