Adding upstream version 0.9.0.
[debian/lxpanel.git] / plugins / netstatus / netstatus.c
CommitLineData
0688b017
AG
1/*
2 * Copyright (C) 2006-2009 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
3 * 2008 Fred Chien <fred@lxde.org>
4 * 2009 martyj19 <martyj19@comcast.net>
5 * 2014 Andriy Grytsenko <andrej@rep.kiev.ua>
6b775dbb
AG
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include <gtk/gtk.h>
23#include <stdlib.h>
24
25#include <gdk-pixbuf/gdk-pixbuf.h>
26#include <glib/gi18n.h>
27
28#include "plugin.h"
29
30#include "dbg.h"
31
32#include "netstatus-icon.h"
33#include "netstatus-dialog.h"
34
35typedef struct {
36 config_setting_t *settings;
37 char *iface;
38 char *config_tool;
39 GtkWidget *dlg;
40} netstatus;
41
42
43static void on_response( GtkDialog* dlg, gint response, netstatus *ns );
44
45static void
46netstatus_destructor(gpointer user_data)
47{
48 netstatus *ns = (netstatus *)user_data;
49
50 ENTER;
51 /* The widget is destroyed in plugin_stop().
52 gtk_widget_destroy(ns->mainw);
53 */
54 g_free( ns->iface );
55 g_free( ns->config_tool );
56 if (ns->dlg)
57 {
58 g_signal_handlers_disconnect_by_func(ns->dlg, on_response, ns);
59 gtk_widget_destroy(ns->dlg);
60 }
61 g_free(ns);
62 RET();
63}
64
65static void on_response( GtkDialog* dlg, gint response, netstatus *ns )
66{
67 const char* iface;
68 switch( response )
69 {
70 case GTK_RESPONSE_CLOSE:
71 case GTK_RESPONSE_DELETE_EVENT:
72 case GTK_RESPONSE_NONE:
73 iface = netstatus_dialog_get_iface_name((GtkWidget*)dlg);
74 if( iface )
75 {
76 g_free(ns->iface);
77 ns->iface = g_strdup(iface);
78 gtk_widget_destroy( GTK_WIDGET(dlg) );
79 ns->dlg = NULL;
80 }
81 }
82}
83
84static gboolean on_button_press( GtkWidget* widget, GdkEventButton* evt, LXPanel *p )
85{
86 NetstatusIface* iface;
87 netstatus *ns = lxpanel_plugin_get_data(widget);
88
89 if( evt->button == 1 ) /* Left click*/
90 {
91 if( ! ns->dlg )
92 {
93 iface = netstatus_icon_get_iface( NETSTATUS_ICON(widget) );
94 ns->dlg = netstatus_dialog_new(iface);
95
96 /* fix background */
97 gtk_widget_set_style(ns->dlg, panel_get_defstyle(p));
98
99 netstatus_dialog_set_configuration_tool( ns->dlg, ns->config_tool );
100 g_signal_connect( ns->dlg, "response", G_CALLBACK(on_response), ns );
101 }
102 gtk_window_present( GTK_WINDOW(ns->dlg) );
103 }
104 return TRUE;
105}
106
107static GtkWidget *
108netstatus_constructor(LXPanel *panel, config_setting_t *settings)
109{
110 netstatus *ns;
111 NetstatusIface* iface;
112 GtkWidget *p;
113 const char *tmp;
114
115 ENTER;
116 ns = g_new0(netstatus, 1);
117 ns->settings = settings;
118 g_return_val_if_fail(ns != NULL, NULL);
119
120 if (!config_setting_lookup_string(settings, "iface", &tmp))
121 tmp = "eth0";
122 ns->iface = g_strdup(tmp);
123 if (!config_setting_lookup_string(settings, "configtool", &tmp))
0688b017 124 tmp = "nm-connection-editor";
6b775dbb
AG
125 ns->config_tool = g_strdup(tmp);
126
127 iface = netstatus_iface_new(ns->iface);
128 p = netstatus_icon_new( iface );
129 lxpanel_plugin_set_data(p, ns, netstatus_destructor);
130 netstatus_icon_set_show_signal((NetstatusIcon *)p, TRUE);
6b775dbb
AG
131 g_object_unref( iface );
132
133 RET(p);
134}
135
136static gboolean apply_config(gpointer user_data)
137{
138 GtkWidget *p = user_data;
139 netstatus *ns = lxpanel_plugin_get_data(p);
140 NetstatusIface* iface;
141
142 iface = netstatus_iface_new(ns->iface);
143 netstatus_icon_set_iface((NetstatusIcon *)p, iface);
144 g_object_unref(iface);
145 config_group_set_string(ns->settings, "iface", ns->iface);
146 config_group_set_string(ns->settings, "configtool", ns->config_tool);
147 return FALSE;
148}
149
150static GtkWidget *netstatus_config(LXPanel *panel, GtkWidget *p)
151{
152 GtkWidget* dlg;
153 netstatus *ns = lxpanel_plugin_get_data(p);
154 dlg = lxpanel_generic_config_dlg(_("Network Status Monitor"),
155 panel, apply_config, p,
156 _("Interface to monitor"), &ns->iface, CONF_TYPE_STR,
157 _("Config tool"), &ns->config_tool, CONF_TYPE_STR,
158 NULL );
159 return dlg;
160}
161
162
163FM_DEFINE_MODULE(lxpanel_gtk, netstatus)
164
165LXPanelPluginInit fm_module_init_lxpanel_gtk = {
166 .name = N_("Network Status Monitor"),
167 .description = N_("Monitor network status"),
168
169 .new_instance = netstatus_constructor,
170 .config = netstatus_config,
171 .button_press_event = on_button_press
172};