Simple GObject based class for connection settings added.
[strongswan.git] / src / frontends / maemo / src / strongswan-connection.c
1 /*
2 * Copyright (C) 2010 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <string.h>
17
18 #include "strongswan-connection.h"
19
20 #define STRONGSWAN_CONNECTION_GET_PRIVATE(object) \
21 (G_TYPE_INSTANCE_GET_PRIVATE ((object), \
22 STRONGSWAN_TYPE_CONNECTION, \
23 StrongswanConnectionPrivate))
24
25 struct _StrongswanConnectionPrivate
26 {
27 gchar *name;
28 gchar *host;
29 gchar *cert;
30 gchar *user;
31 gchar *pass;
32 };
33
34 enum
35 {
36 PROP_0,
37 PROP_NAME,
38 PROP_HOST,
39 PROP_CERT,
40 PROP_USER,
41 PROP_PASS,
42 };
43
44 G_DEFINE_TYPE (StrongswanConnection, strongswan_connection, G_TYPE_OBJECT);
45
46 static void
47 strongswan_connection_get_property (GObject *object,
48 guint prop_id,
49 GValue *value,
50 GParamSpec *pspec)
51 {
52 StrongswanConnectionPrivate *priv = STRONGSWAN_CONNECTION (object)->priv;
53 switch (prop_id)
54 {
55 case PROP_NAME:
56 g_value_set_string (value, priv->name);
57 break;
58 case PROP_HOST:
59 g_value_set_string (value, priv->host);
60 break;
61 case PROP_CERT:
62 g_value_set_string (value, priv->cert);
63 break;
64 case PROP_USER:
65 g_value_set_string (value, priv->user);
66 break;
67 case PROP_PASS:
68 g_value_set_string (value, priv->pass);
69 break;
70 default:
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
72 break;
73 }
74 }
75
76 static void
77 strongswan_connection_set_property (GObject *object,
78 guint prop_id,
79 const GValue *value,
80 GParamSpec *pspec)
81 {
82 StrongswanConnectionPrivate *priv = STRONGSWAN_CONNECTION (object)->priv;
83 switch (prop_id)
84 {
85 case PROP_NAME:
86 g_free (priv->name);
87 priv->name = g_value_dup_string (value);
88 case PROP_HOST:
89 g_free (priv->host);
90 priv->host = g_value_dup_string (value);
91 break;
92 case PROP_CERT:
93 g_free (priv->cert);
94 priv->cert = g_value_dup_string (value);
95 break;
96 case PROP_USER:
97 g_free (priv->user);
98 priv->user = g_value_dup_string (value);
99 break;
100 case PROP_PASS:
101 g_free (priv->pass);
102 priv->pass = g_value_dup_string (value);
103 break;
104 default:
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 break;
107 }
108 }
109
110 static void
111 strongswan_connection_init (StrongswanConnection *connection)
112 {
113 connection->priv = STRONGSWAN_CONNECTION_GET_PRIVATE (connection);
114 }
115
116 static void
117 strongswan_connection_constructed (GObject *object)
118 {
119 if (G_OBJECT_CLASS (strongswan_connection_parent_class)->constructed)
120 {
121 G_OBJECT_CLASS (strongswan_connection_parent_class)->constructed (object);
122 }
123 }
124
125 static void
126 strongswan_connection_dispose (GObject *object)
127 {
128 G_OBJECT_CLASS (strongswan_connection_parent_class)->dispose (object);
129 }
130
131 static void
132 strongswan_connection_finalize (GObject *object)
133 {
134 StrongswanConnectionPrivate *priv = STRONGSWAN_CONNECTION (object)->priv;
135 g_free (priv->name);
136 g_free (priv->host);
137 g_free (priv->cert);
138 g_free (priv->user);
139 g_free (priv->pass);
140 G_OBJECT_CLASS (strongswan_connection_parent_class)->finalize (object);
141 }
142
143 static void
144 strongswan_connection_class_init (StrongswanConnectionClass *klass)
145 {
146 GObjectClass *object_class = G_OBJECT_CLASS (klass);
147
148 object_class->constructed = strongswan_connection_constructed;
149 object_class->get_property = strongswan_connection_get_property;
150 object_class->set_property = strongswan_connection_set_property;
151 object_class->dispose = strongswan_connection_dispose;
152 object_class->finalize = strongswan_connection_finalize;
153
154 g_object_class_install_property (object_class, PROP_NAME,
155 g_param_spec_string ("name", "Connection name",
156 "The unique name of a connection",
157 NULL,
158 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159
160 g_object_class_install_property (object_class, PROP_HOST,
161 g_param_spec_string ("host", "Hostname or IP address",
162 "The hostname or IP address of the Gateway",
163 NULL,
164 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165
166 g_object_class_install_property (object_class, PROP_CERT,
167 g_param_spec_string ("cert", "Gateway or CA certificate",
168 "The certificate of the gateway or the CA",
169
170 NULL,
171 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172
173 g_object_class_install_property (object_class, PROP_USER,
174 g_param_spec_string ("user", "Username",
175 "The username for EAP authentication",
176 NULL,
177 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178
179 g_object_class_install_property (object_class, PROP_PASS,
180 g_param_spec_string ("pass", "Password",
181 "The password for EAP authentication",
182 NULL,
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184
185 g_type_class_add_private (klass, sizeof (StrongswanConnectionPrivate));
186 }
187
188 StrongswanConnection *
189 strongswan_connection_new (const gchar *name)
190 {
191 StrongswanConnection *conn;
192 conn = g_object_new (STRONGSWAN_TYPE_CONNECTION,
193 "name", name,
194 NULL);
195 g_return_val_if_fail (conn->priv != NULL, NULL);
196 return conn;
197 }
198