Rename panel, plugin, and plugin_class to Panel, Plugin, and
[lxde/lxpanel.git] / src / plugins / netstatus / netstatus.c
CommitLineData
4542c20d 1/**
a99ee9e1
JH
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
7ea0d27f
HJYP
19#include <gtk/gtk.h>
20#include <stdlib.h>
21
22#include <gdk-pixbuf/gdk-pixbuf.h>
ce522551 23#include <glib/gi18n.h>
7ea0d27f
HJYP
24
25#include "panel.h"
26#include "misc.h"
27#include "plugin.h"
28
7ea0d27f
HJYP
29#include "dbg.h"
30
31#include "netstatus-icon.h"
784fcafc 32#include "netstatus-dialog.h"
7ea0d27f
HJYP
33
34typedef struct {
35 char *iface;
f05f64ec 36 char *config_tool;
7ea0d27f 37 GtkWidget *mainw;
784fcafc 38 GtkWidget *dlg;
7ea0d27f
HJYP
39} netstatus;
40
41
42static void
22242ed4 43netstatus_destructor(Plugin *p)
7ea0d27f
HJYP
44{
45 netstatus *ns = (netstatus *)p->priv;
46
47 ENTER;
4542c20d 48 /* The widget is destroyed in plugin_stop().
7ea0d27f 49 gtk_widget_destroy(ns->mainw);
4542c20d 50 */
7ea0d27f 51 g_free( ns->iface );
f05f64ec 52 g_free( ns->config_tool );
7ea0d27f
HJYP
53 g_free(ns);
54 RET();
55}
56
784fcafc
HJYP
57static 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:
c6780e74 64 gtk_widget_destroy( GTK_WIDGET(dlg) );
784fcafc
HJYP
65 ns->dlg = NULL;
66 }
67}
68
22242ed4 69static void on_button_press( GtkWidget* widget, GdkEventButton* evt, Plugin* p )
784fcafc
HJYP
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);
78dc5880
FC
80
81 /* fix background */
82 gtk_widget_set_style(ns->dlg, p->panel->defstyle);
83
f05f64ec 84 netstatus_dialog_set_configuration_tool( ns->dlg, ns->config_tool );
c6780e74 85 g_signal_connect( ns->dlg, "response", G_CALLBACK(on_response), ns );
784fcafc
HJYP
86 }
87 gtk_window_present( GTK_WINDOW(ns->dlg) );
88 }
89}
90
7ea0d27f 91static int
22242ed4 92netstatus_constructor(Plugin *p, char** fp)
7ea0d27f
HJYP
93{
94 netstatus *ns;
95 line s;
7ea0d27f
HJYP
96 NetstatusIface* iface;
97
98 ENTER;
4542c20d 99 s.len = 256;
7ea0d27f
HJYP
100 ns = g_new0(netstatus, 1);
101 g_return_val_if_fail(ns != NULL, 0);
102 p->priv = ns;
db449f6e
HJYP
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);
7ea0d27f
HJYP
121 goto error;
122 }
7ea0d27f
HJYP
123 }
124 }
5297da29
HJYP
125 else
126 {
127 ns->iface = g_strdup("eth0");
128 ns->config_tool = g_strdup("network-admin --configure %i");
129 }
130
7ea0d27f
HJYP
131 iface = netstatus_iface_new(ns->iface);
132 ns->mainw = netstatus_icon_new( iface );
784fcafc
HJYP
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 );
04883e73 137 gtk_widget_set_size_request( ns->mainw, 26, 24 );
7ea0d27f
HJYP
138
139 gtk_widget_show_all(ns->mainw);
140
4542c20d 141 p->pwid = ns->mainw;
7ea0d27f
HJYP
142
143 RET(1);
144
145 error:
146 netstatus_destructor(p);
147 RET(0);
148}
149
22242ed4 150static void apply_config( Plugin* p )
8ccd023a 151{
04883e73
HJYP
152
153}
154
22242ed4 155static void netstatus_config( Plugin* p, GtkWindow* parent )
04883e73
HJYP
156{
157 GtkWidget* dlg;
8ccd023a 158 netstatus *ns = (netstatus*)p->priv;
04883e73
HJYP
159 dlg = create_generic_config_dlg(
160 _(p->class->name),
5a343ad5
JH
161 GTK_WIDGET(parent),
162 (GSourceFunc) apply_config, p,
8ccd023a
HJYP
163 _("Interface to monitor"), &ns->iface, G_TYPE_STRING,
164 _("Config tool"), &ns->config_tool, G_TYPE_STRING,
165 NULL );
04883e73 166 gtk_window_present( GTK_WINDOW(dlg) );
8ccd023a 167}
7ea0d27f 168
22242ed4 169static void save_config( Plugin* p, FILE* fp )
9c97f69e
HJYP
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
22242ed4 176PluginClass netstatus_plugin_class = {
7ea0d27f
HJYP
177 fname: NULL,
178 count: 0,
179
180 type : "netstatus",
ce522551 181 name : N_("Net Status Monitor"),
7ea0d27f 182 version: "1.0",
ce522551 183 description : N_("Monitor network status"),
7ea0d27f
HJYP
184
185 constructor : netstatus_constructor,
186 destructor : netstatus_destructor,
9c97f69e
HJYP
187 config : netstatus_config,
188 save : save_config
7ea0d27f 189};