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