Uploaded initial version.
[guivpn] / trunk / vpngui / src / vpn_notify.c
1 /*
2  * This file is part of vpngui
3  *
4  * Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * */
20
21 #include "vpn_notify.h"
22
23 static gboolean
24 vpn_notify_get_status (VpnNotify * self,
25     gboolean * ret_up, gchar ** ret_ip, GError ** error);
26
27 #include "vpn_notify-glue.h"
28
29 #define VPN_NOTIFY_GET_PRIVATE(obj) \
30   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), VPN_TYPE_NOTIFY, VpnNotifyPrivate))
31
32 typedef struct _VpnNotifyPrivate VpnNotifyPrivate;
33
34 struct _VpnNotifyPrivate
35 {
36   gchar *ip;
37 };
38
39 enum {
40   PROP_IP = 1,
41 };
42
43 G_DEFINE_TYPE (VpnNotify, vpn_notify, G_TYPE_OBJECT)
44
45 static void
46 vpn_notify_finalize (GObject * object)
47 {
48   VpnNotifyPrivate *priv;
49
50   g_debug ("%s", G_STRFUNC);
51
52   g_assert (object);
53   g_assert (VPN_IS_NOTIFY (object));
54   priv = VPN_NOTIFY_GET_PRIVATE (object);
55   g_assert (priv);
56
57   g_free (priv->ip);
58 }
59
60 static void
61 vpn_notify_get_property (GObject * object,
62     guint property_id, GValue * value, GParamSpec * pspec)
63 {
64   VpnNotifyPrivate *priv;
65
66   g_debug ("%s", G_STRFUNC);
67
68   g_assert (object);
69   g_assert (VPN_IS_NOTIFY (object));
70   priv = VPN_NOTIFY_GET_PRIVATE (object);
71   g_assert (priv);
72
73   switch (property_id)
74     {
75       case PROP_IP:
76         g_value_set_string (value, priv->ip);
77         g_debug("ip = %s", priv->ip);
78         break;
79       default:
80         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
81     }
82 }
83
84 static void
85 vpn_notify_set_property (GObject * object,
86     guint property_id, const GValue * value, GParamSpec * pspec)
87 {
88   VpnNotifyPrivate *priv;
89
90   g_debug ("%s", G_STRFUNC);
91
92   g_assert (object);
93   g_assert (VPN_IS_NOTIFY (object));
94   priv = VPN_NOTIFY_GET_PRIVATE (object);
95   g_assert (priv);
96
97   switch (property_id)
98     {
99       case PROP_IP:
100         g_free (priv->ip);
101         priv->ip = g_value_dup_string (value);
102         g_debug("ip = %s", priv->ip);
103         if (priv->ip[0] != '\0')
104           g_signal_emit_by_name (object, "up", priv->ip);
105         else
106           g_signal_emit_by_name (object, "down");
107         break;
108       default:
109         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
110     }
111 }
112
113 static void
114 vpn_notify_class_init (VpnNotifyClass * klass)
115 {
116   GObjectClass *gobject_class;
117
118   g_debug ("%s", G_STRFUNC);
119
120   gobject_class = G_OBJECT_CLASS (klass);
121
122   gobject_class->finalize = vpn_notify_finalize;
123   gobject_class->get_property = vpn_notify_get_property;
124   gobject_class->set_property = vpn_notify_set_property;
125
126   g_object_class_install_property (gobject_class,
127       PROP_IP, g_param_spec_string ("ip",
128           "IP address", "The local IP address", "", G_PARAM_READWRITE));
129
130   g_signal_new ("up",
131       G_OBJECT_CLASS_TYPE (klass),
132       G_SIGNAL_RUN_LAST,
133       0,
134       NULL, NULL,
135       g_cclosure_marshal_VOID__STRING,
136       G_TYPE_NONE,
137       1, G_TYPE_STRING);
138
139   g_signal_new ("down",
140       G_OBJECT_CLASS_TYPE (klass),
141       G_SIGNAL_RUN_LAST,
142       0,
143       NULL, NULL,
144       g_cclosure_marshal_VOID__VOID,
145       G_TYPE_NONE,
146       0);
147
148   g_type_class_add_private (klass, sizeof (VpnNotifyPrivate));
149
150   dbus_g_object_type_install_info (VPN_TYPE_NOTIFY,
151       &dbus_glib_vpn_notify_object_info);
152 }
153
154 static void
155 vpn_notify_init (VpnNotify * self)
156 {
157   VpnNotifyPrivate *priv;
158
159   g_debug ("%s", G_STRFUNC);
160
161   g_assert (self);
162   g_assert (VPN_IS_NOTIFY (self));
163   priv = VPN_NOTIFY_GET_PRIVATE (self);
164   g_assert (priv);
165
166   priv->ip = g_strdup ("");
167 }
168
169 static gboolean
170 vpn_notify_get_status (VpnNotify * self,
171     gboolean * ret_up, gchar ** ret_ip, GError ** error)
172 {
173   VpnNotifyPrivate *priv;
174
175   g_debug ("%s", G_STRFUNC);
176
177   g_assert (self);
178   g_assert (VPN_IS_NOTIFY (self));
179   priv = VPN_NOTIFY_GET_PRIVATE (self);
180   g_assert (priv);
181
182   *ret_up = priv->ip[0] != '\0';
183   *ret_ip = g_strdup (priv->ip);
184
185   g_debug ("%s: up = %d, ip = %s", G_STRFUNC, *ret_up, *ret_ip);
186
187   return TRUE;
188 }