Add a quick utility to test datetime formatting
[platform/upstream/glib.git] / gio / gdbusauthmechanism.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include "gdbusauthmechanism.h"
26 #include "gcredentials.h"
27 #include "gdbuserror.h"
28 #include "gioenumtypes.h"
29 #include "giostream.h"
30
31 #include "glibintl.h"
32
33 /* ---------------------------------------------------------------------------------------------------- */
34
35 struct _GDBusAuthMechanismPrivate
36 {
37   GIOStream *stream;
38   GCredentials *credentials;
39 };
40
41 enum
42 {
43   PROP_0,
44   PROP_STREAM,
45   PROP_CREDENTIALS
46 };
47
48 G_DEFINE_ABSTRACT_TYPE (GDBusAuthMechanism, _g_dbus_auth_mechanism, G_TYPE_OBJECT);
49
50 /* ---------------------------------------------------------------------------------------------------- */
51
52 static void
53 _g_dbus_auth_mechanism_finalize (GObject *object)
54 {
55   GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
56
57   if (mechanism->priv->stream != NULL)
58     g_object_unref (mechanism->priv->stream);
59   if (mechanism->priv->credentials != NULL)
60     g_object_unref (mechanism->priv->credentials);
61
62   G_OBJECT_CLASS (_g_dbus_auth_mechanism_parent_class)->finalize (object);
63 }
64
65 static void
66 _g_dbus_auth_mechanism_get_property (GObject    *object,
67                                      guint       prop_id,
68                                      GValue     *value,
69                                      GParamSpec *pspec)
70 {
71   GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
72
73   switch (prop_id)
74     {
75     case PROP_STREAM:
76       g_value_set_object (value, mechanism->priv->stream);
77       break;
78
79     case PROP_CREDENTIALS:
80       g_value_set_object (value, mechanism->priv->credentials);
81       break;
82
83     default:
84       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85       break;
86     }
87 }
88
89 static void
90 _g_dbus_auth_mechanism_set_property (GObject      *object,
91                                      guint         prop_id,
92                                      const GValue *value,
93                                      GParamSpec   *pspec)
94 {
95   GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
96
97   switch (prop_id)
98     {
99     case PROP_STREAM:
100       mechanism->priv->stream = g_value_dup_object (value);
101       break;
102
103     case PROP_CREDENTIALS:
104       mechanism->priv->credentials = g_value_dup_object (value);
105       break;
106
107     default:
108       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109       break;
110     }
111 }
112
113 static void
114 _g_dbus_auth_mechanism_class_init (GDBusAuthMechanismClass *klass)
115 {
116   GObjectClass *gobject_class;
117
118   g_type_class_add_private (klass, sizeof (GDBusAuthMechanismPrivate));
119
120   gobject_class = G_OBJECT_CLASS (klass);
121   gobject_class->get_property = _g_dbus_auth_mechanism_get_property;
122   gobject_class->set_property = _g_dbus_auth_mechanism_set_property;
123   gobject_class->finalize     = _g_dbus_auth_mechanism_finalize;
124
125   g_object_class_install_property (gobject_class,
126                                    PROP_STREAM,
127                                    g_param_spec_object ("stream",
128                                                         P_("IO Stream"),
129                                                         P_("The underlying GIOStream used for I/O"),
130                                                         G_TYPE_IO_STREAM,
131                                                         G_PARAM_READABLE |
132                                                         G_PARAM_WRITABLE |
133                                                         G_PARAM_CONSTRUCT_ONLY |
134                                                         G_PARAM_STATIC_NAME |
135                                                         G_PARAM_STATIC_BLURB |
136                                                         G_PARAM_STATIC_NICK));
137
138   /**
139    * GDBusAuthMechanism:credentials:
140    *
141    * If authenticating as a server, this property contains the
142    * received credentials, if any.
143    *
144    * If authenticating as a client, the property contains the
145    * credentials that were sent, if any.
146    */
147   g_object_class_install_property (gobject_class,
148                                    PROP_CREDENTIALS,
149                                    g_param_spec_object ("credentials",
150                                                         P_("Credentials"),
151                                                         P_("The credentials of the remote peer"),
152                                                         G_TYPE_CREDENTIALS,
153                                                         G_PARAM_READABLE |
154                                                         G_PARAM_WRITABLE |
155                                                         G_PARAM_CONSTRUCT_ONLY |
156                                                         G_PARAM_STATIC_NAME |
157                                                         G_PARAM_STATIC_BLURB |
158                                                         G_PARAM_STATIC_NICK));
159 }
160
161 static void
162 _g_dbus_auth_mechanism_init (GDBusAuthMechanism *mechanism)
163 {
164   /* not used for now */
165   mechanism->priv = G_TYPE_INSTANCE_GET_PRIVATE (mechanism,
166                                                  G_TYPE_DBUS_AUTH_MECHANISM,
167                                                  GDBusAuthMechanismPrivate);
168 }
169
170 /* ---------------------------------------------------------------------------------------------------- */
171
172 GIOStream *
173 _g_dbus_auth_mechanism_get_stream (GDBusAuthMechanism *mechanism)
174 {
175   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
176   return mechanism->priv->stream;
177 }
178
179 GCredentials *
180 _g_dbus_auth_mechanism_get_credentials (GDBusAuthMechanism *mechanism)
181 {
182   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
183   return mechanism->priv->credentials;
184 }
185
186 /* ---------------------------------------------------------------------------------------------------- */
187
188 const gchar *
189 _g_dbus_auth_mechanism_get_name (GType mechanism_type)
190 {
191   const gchar *name;
192   GDBusAuthMechanismClass *klass;
193
194   g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), NULL);
195
196   klass = g_type_class_ref (mechanism_type);
197   g_assert (klass != NULL);
198   name = klass->get_name ();
199   //g_type_class_unref (klass);
200
201   return name;
202 }
203
204 gint
205 _g_dbus_auth_mechanism_get_priority (GType mechanism_type)
206 {
207   gint priority;
208   GDBusAuthMechanismClass *klass;
209
210   g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), 0);
211
212   klass = g_type_class_ref (mechanism_type);
213   g_assert (klass != NULL);
214   priority = klass->get_priority ();
215   //g_type_class_unref (klass);
216
217   return priority;
218 }
219
220 /* ---------------------------------------------------------------------------------------------------- */
221
222 gboolean
223 _g_dbus_auth_mechanism_is_supported (GDBusAuthMechanism *mechanism)
224 {
225   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), FALSE);
226   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->is_supported (mechanism);
227 }
228
229 gchar *
230 _g_dbus_auth_mechanism_encode_data (GDBusAuthMechanism *mechanism,
231                                     const gchar        *data,
232                                     gsize               data_len,
233                                     gsize              *out_data_len)
234 {
235   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
236   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->encode_data (mechanism, data, data_len, out_data_len);
237 }
238
239
240 gchar *
241 _g_dbus_auth_mechanism_decode_data (GDBusAuthMechanism *mechanism,
242                                     const gchar        *data,
243                                     gsize               data_len,
244                                     gsize              *out_data_len)
245 {
246   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
247   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->decode_data (mechanism, data, data_len, out_data_len);
248 }
249
250 /* ---------------------------------------------------------------------------------------------------- */
251
252 GDBusAuthMechanismState
253 _g_dbus_auth_mechanism_server_get_state (GDBusAuthMechanism *mechanism)
254 {
255   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
256   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_state (mechanism);
257 }
258
259 void
260 _g_dbus_auth_mechanism_server_initiate (GDBusAuthMechanism *mechanism,
261                                         const gchar        *initial_response,
262                                         gsize               initial_response_len)
263 {
264   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
265   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_initiate (mechanism, initial_response, initial_response_len);
266 }
267
268 void
269 _g_dbus_auth_mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
270                                             const gchar        *data,
271                                             gsize               data_len)
272 {
273   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
274   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_receive (mechanism, data, data_len);
275 }
276
277 gchar *
278 _g_dbus_auth_mechanism_server_data_send (GDBusAuthMechanism *mechanism,
279                                          gsize              *out_data_len)
280 {
281   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
282   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_send (mechanism, out_data_len);
283 }
284
285 gchar *
286 _g_dbus_auth_mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
287 {
288   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
289   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_reject_reason (mechanism);
290 }
291
292 void
293 _g_dbus_auth_mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
294 {
295   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
296   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_shutdown (mechanism);
297 }
298
299 /* ---------------------------------------------------------------------------------------------------- */
300
301 GDBusAuthMechanismState
302 _g_dbus_auth_mechanism_client_get_state (GDBusAuthMechanism *mechanism)
303 {
304   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
305   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_get_state (mechanism);
306 }
307
308 gchar *
309 _g_dbus_auth_mechanism_client_initiate (GDBusAuthMechanism *mechanism,
310                                         gsize              *out_initial_response_len)
311 {
312   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
313   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_initiate (mechanism,
314                                                                        out_initial_response_len);
315 }
316
317 void
318 _g_dbus_auth_mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
319                                             const gchar        *data,
320                                             gsize               data_len)
321 {
322   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
323   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_receive (mechanism, data, data_len);
324 }
325
326 gchar *
327 _g_dbus_auth_mechanism_client_data_send (GDBusAuthMechanism *mechanism,
328                                          gsize              *out_data_len)
329 {
330   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
331   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_send (mechanism, out_data_len);
332 }
333
334 void
335 _g_dbus_auth_mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
336 {
337   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
338   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_shutdown (mechanism);
339 }
340
341 /* ---------------------------------------------------------------------------------------------------- */