Update copyrights everywhere.
[lxde/lxpanel.git] / src / plugins / netstatus / netstatus.c
1 /**
2 * Copyright (c) 2006-2014 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 "plugin.h"
26 #include "misc.h"
27
28 #include "dbg.h"
29
30 #include "netstatus-icon.h"
31 #include "netstatus-dialog.h"
32
33 typedef struct {
34 config_setting_t *settings;
35 char *iface;
36 char *config_tool;
37 GtkWidget *dlg;
38 } netstatus;
39
40
41 static void on_response( GtkDialog* dlg, gint response, netstatus *ns );
42
43 static void
44 netstatus_destructor(gpointer user_data)
45 {
46 netstatus *ns = (netstatus *)user_data;
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 if (ns->dlg)
55 {
56 g_signal_handlers_disconnect_by_func(ns->dlg, on_response, ns);
57 gtk_widget_destroy(ns->dlg);
58 }
59 g_free(ns);
60 RET();
61 }
62
63 static void on_response( GtkDialog* dlg, gint response, netstatus *ns )
64 {
65 const char* iface;
66 switch( response )
67 {
68 case GTK_RESPONSE_CLOSE:
69 case GTK_RESPONSE_DELETE_EVENT:
70 case GTK_RESPONSE_NONE:
71 iface = netstatus_dialog_get_iface_name((GtkWidget*)dlg);
72 if( iface )
73 {
74 g_free(ns->iface);
75 ns->iface = g_strdup(iface);
76 gtk_widget_destroy( GTK_WIDGET(dlg) );
77 ns->dlg = NULL;
78 }
79 }
80 }
81
82 static gboolean on_button_press( GtkWidget* widget, GdkEventButton* evt, LXPanel *p )
83 {
84 NetstatusIface* iface;
85 netstatus *ns = lxpanel_plugin_get_data(widget);
86
87 if( evt->button == 1 ) /* Left click*/
88 {
89 if( ! ns->dlg )
90 {
91 iface = netstatus_icon_get_iface( NETSTATUS_ICON(widget) );
92 ns->dlg = netstatus_dialog_new(iface);
93
94 /* fix background */
95 gtk_widget_set_style(ns->dlg, panel_get_defstyle(p));
96
97 netstatus_dialog_set_configuration_tool( ns->dlg, ns->config_tool );
98 g_signal_connect( ns->dlg, "response", G_CALLBACK(on_response), ns );
99 }
100 gtk_window_present( GTK_WINDOW(ns->dlg) );
101 }
102 return TRUE;
103 }
104
105 static GtkWidget *
106 netstatus_constructor(LXPanel *panel, config_setting_t *settings)
107 {
108 netstatus *ns;
109 NetstatusIface* iface;
110 GtkWidget *p;
111 const char *tmp;
112
113 ENTER;
114 ns = g_new0(netstatus, 1);
115 ns->settings = settings;
116 g_return_val_if_fail(ns != NULL, NULL);
117
118 if (!config_setting_lookup_string(settings, "iface", &tmp))
119 tmp = "eth0";
120 ns->iface = g_strdup(tmp);
121 if (!config_setting_lookup_string(settings, "configtool", &tmp))
122 tmp = "network-admin --configure %i";
123 ns->config_tool = g_strdup(tmp);
124
125 iface = netstatus_iface_new(ns->iface);
126 p = netstatus_icon_new( iface );
127 lxpanel_plugin_set_data(p, ns, netstatus_destructor);
128 netstatus_icon_set_show_signal((NetstatusIcon *)p, TRUE);
129 gtk_widget_add_events( p, GDK_BUTTON_PRESS_MASK );
130 g_object_unref( iface );
131
132 RET(p);
133 }
134
135 static gboolean apply_config(gpointer user_data)
136 {
137 GtkWidget *p = user_data;
138 netstatus *ns = lxpanel_plugin_get_data(p);
139 NetstatusIface* iface;
140
141 iface = netstatus_iface_new(ns->iface);
142 netstatus_icon_set_iface((NetstatusIcon *)p, iface);
143 g_object_unref(iface);
144 config_group_set_string(ns->settings, "iface", ns->iface);
145 config_group_set_string(ns->settings, "configtool", ns->config_tool);
146 return FALSE;
147 }
148
149 static GtkWidget *netstatus_config(LXPanel *panel, GtkWidget *p)
150 {
151 GtkWidget* dlg;
152 netstatus *ns = lxpanel_plugin_get_data(p);
153 dlg = lxpanel_generic_config_dlg(_("Network Status Monitor"),
154 panel, apply_config, p,
155 _("Interface to monitor"), &ns->iface, CONF_TYPE_STR,
156 _("Config tool"), &ns->config_tool, CONF_TYPE_STR,
157 NULL );
158 return dlg;
159 }
160
161
162 FM_DEFINE_MODULE(lxpanel_gtk, netstatus)
163
164 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
165 .name = N_("Network Status Monitor"),
166 .description = N_("Monitor network status"),
167
168 .new_instance = netstatus_constructor,
169 .config = netstatus_config,
170 .button_press_event = on_button_press
171 };