Add new ESource classes.
[platform/upstream/evolution-data-server.git] / libedataserver / e-source-authentication.c
1 /*
2  * e-source-authentication.c
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) version 3.
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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
16  *
17  */
18
19 /**
20  * SECTION: e-source-authentication
21  * @include: libedataserver/e-source-authentication.h
22  * @short_description: #ESource extension for authentication settings
23  *
24  * The #ESourceAuthentication extension tracks authentication settings
25  * for a user account on a remote server.
26  *
27  * Access the extension as follows:
28  *
29  * |[
30  *   #include <libedataserver/e-source-authentication.h>
31  *
32  *   ESourceAuthentication *extension;
33  *
34  *   extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
35  * ]|
36  **/
37
38 #include "e-source-authentication.h"
39
40 #include <libedataserver/e-data-server-util.h>
41
42 #define E_SOURCE_AUTHENTICATION_GET_PRIVATE(obj) \
43         (G_TYPE_INSTANCE_GET_PRIVATE \
44         ((obj), E_TYPE_SOURCE_AUTHENTICATION, ESourceAuthenticationPrivate))
45
46 struct _ESourceAuthenticationPrivate {
47         GMutex *property_lock;
48         gchar *host;
49         gchar *method;
50         guint16 port;
51         gchar *user;
52 };
53
54 enum {
55         PROP_0,
56         PROP_HOST,
57         PROP_METHOD,
58         PROP_PORT,
59         PROP_USER
60 };
61
62 G_DEFINE_TYPE (
63         ESourceAuthentication,
64         e_source_authentication,
65         E_TYPE_SOURCE_EXTENSION)
66
67 static void
68 source_authentication_set_property (GObject *object,
69                                     guint property_id,
70                                     const GValue *value,
71                                     GParamSpec *pspec)
72 {
73         switch (property_id) {
74                 case PROP_HOST:
75                         e_source_authentication_set_host (
76                                 E_SOURCE_AUTHENTICATION (object),
77                                 g_value_get_string (value));
78                         return;
79
80                 case PROP_METHOD:
81                         e_source_authentication_set_method (
82                                 E_SOURCE_AUTHENTICATION (object),
83                                 g_value_get_string (value));
84                         return;
85
86                 case PROP_PORT:
87                         e_source_authentication_set_port (
88                                 E_SOURCE_AUTHENTICATION (object),
89                                 g_value_get_uint (value));
90                         return;
91
92                 case PROP_USER:
93                         e_source_authentication_set_user (
94                                 E_SOURCE_AUTHENTICATION (object),
95                                 g_value_get_string (value));
96                         return;
97         }
98
99         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
100 }
101
102 static void
103 source_authentication_get_property (GObject *object,
104                                     guint property_id,
105                                     GValue *value,
106                                     GParamSpec *pspec)
107 {
108         switch (property_id) {
109                 case PROP_HOST:
110                         g_value_take_string (
111                                 value,
112                                 e_source_authentication_dup_host (
113                                 E_SOURCE_AUTHENTICATION (object)));
114                         return;
115
116                 case PROP_METHOD:
117                         g_value_take_string (
118                                 value,
119                                 e_source_authentication_dup_method (
120                                 E_SOURCE_AUTHENTICATION (object)));
121                         return;
122
123                 case PROP_PORT:
124                         g_value_set_uint (
125                                 value,
126                                 e_source_authentication_get_port (
127                                 E_SOURCE_AUTHENTICATION (object)));
128                         return;
129
130                 case PROP_USER:
131                         g_value_take_string (
132                                 value,
133                                 e_source_authentication_dup_user (
134                                 E_SOURCE_AUTHENTICATION (object)));
135                         return;
136         }
137
138         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
139 }
140
141 static void
142 source_authentication_finalize (GObject *object)
143 {
144         ESourceAuthenticationPrivate *priv;
145
146         priv = E_SOURCE_AUTHENTICATION_GET_PRIVATE (object);
147
148         g_mutex_free (priv->property_lock);
149
150         g_free (priv->host);
151         g_free (priv->method);
152         g_free (priv->user);
153
154         /* Chain up to parent's finalize() method. */
155         G_OBJECT_CLASS (e_source_authentication_parent_class)->finalize (object);
156 }
157
158 static void
159 e_source_authentication_class_init (ESourceAuthenticationClass *class)
160 {
161         GObjectClass *object_class;
162         ESourceExtensionClass *extension_class;
163
164         g_type_class_add_private (class, sizeof (ESourceAuthenticationPrivate));
165
166         object_class = G_OBJECT_CLASS (class);
167         object_class->set_property = source_authentication_set_property;
168         object_class->get_property = source_authentication_get_property;
169         object_class->finalize = source_authentication_finalize;
170
171         extension_class = E_SOURCE_EXTENSION_CLASS (class);
172         extension_class->name = E_SOURCE_EXTENSION_AUTHENTICATION;
173
174         g_object_class_install_property (
175                 object_class,
176                 PROP_HOST,
177                 g_param_spec_string (
178                         "host",
179                         "Host",
180                         "Host name for the remote account",
181                         "",
182                         G_PARAM_READWRITE |
183                         G_PARAM_CONSTRUCT |
184                         G_PARAM_STATIC_STRINGS |
185                         E_SOURCE_PARAM_SETTING));
186
187         g_object_class_install_property (
188                 object_class,
189                 PROP_METHOD,
190                 g_param_spec_string (
191                         "method",
192                         "Method",
193                         "Authentication method",
194                         "none",
195                         G_PARAM_READWRITE |
196                         G_PARAM_CONSTRUCT |
197                         G_PARAM_STATIC_STRINGS |
198                         E_SOURCE_PARAM_SETTING));
199
200         g_object_class_install_property (
201                 object_class,
202                 PROP_PORT,
203                 g_param_spec_uint (
204                         "port",
205                         "Port",
206                         "Port number for the remote account",
207                         0, G_MAXUINT16, 0,
208                         G_PARAM_READWRITE |
209                         G_PARAM_CONSTRUCT |
210                         G_PARAM_STATIC_STRINGS |
211                         E_SOURCE_PARAM_SETTING));
212
213         g_object_class_install_property (
214                 object_class,
215                 PROP_USER,
216                 g_param_spec_string (
217                         "user",
218                         "User",
219                         "User name for the remote account",
220                         NULL,
221                         G_PARAM_READWRITE |
222                         G_PARAM_CONSTRUCT |
223                         G_PARAM_STATIC_STRINGS |
224                         E_SOURCE_PARAM_SETTING));
225 }
226
227 static void
228 e_source_authentication_init (ESourceAuthentication *extension)
229 {
230         extension->priv = E_SOURCE_AUTHENTICATION_GET_PRIVATE (extension);
231         extension->priv->property_lock = g_mutex_new ();
232 }
233
234 /**
235  * e_source_authentication_required:
236  * @extension: an #ESourceAuthentication
237  *
238  * This is a convenience function which returns whether authentication
239  * is required at all, regardless of the method used.  This relies on
240  * the convention of setting #ESourceAuthentication:method to "none"
241  * when authentication is <emphasis>not</emphasis> required.
242  *
243  * Returns: whether authentication is required at all
244  *
245  * Since: 3.6
246  **/
247 gboolean
248 e_source_authentication_required (ESourceAuthentication *extension)
249 {
250         const gchar *method;
251
252         g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), FALSE);
253
254         method = e_source_authentication_get_method (extension);
255         g_return_val_if_fail (method != NULL && *method != '\0', FALSE);
256
257         return (g_strcmp0 (method, "none") != 0);
258 }
259
260 /**
261  * e_source_authentication_get_host:
262  * @extension: an #ESourceAuthentication
263  *
264  * Returns the host name used to authenticate to a remote account.
265  *
266  * Returns: the host name of a remote account
267  *
268  * Since: 3.6
269  **/
270 const gchar *
271 e_source_authentication_get_host (ESourceAuthentication *extension)
272 {
273         g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
274
275         return extension->priv->host;
276 }
277
278 /**
279  * e_source_authentication_dup_host:
280  * @extension: an #ESourceAuthentication
281  *
282  * Thread-safe variation of e_source_authentication_get_host().
283  * Use this function when accessing @extension from multiple threads.
284  *
285  * The returned string should be freed with g_free() when no longer needed.
286  *
287  * Returns: a newly-allocated copy of #ESourceAuthentication:host
288  *
289  * Since: 3.6
290  **/
291 gchar *
292 e_source_authentication_dup_host (ESourceAuthentication *extension)
293 {
294         const gchar *protected;
295         gchar *duplicate;
296
297         g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
298
299         g_mutex_lock (extension->priv->property_lock);
300
301         protected = e_source_authentication_get_host (extension);
302         duplicate = g_strdup (protected);
303
304         g_mutex_unlock (extension->priv->property_lock);
305
306         return duplicate;
307 }
308
309 /**
310  * e_source_authentication_set_host:
311  * @extension: an #ESourceAuthentication
312  * @host: (allow-none): a host name, or %NULL
313  *
314  * Sets the host name used to authenticate to a remote account.
315  *
316  * The internal copy of @host is automatically stripped of leading and
317  * trailing whitespace.  If the resulting string is empty, %NULL is set
318  * instead.
319  *
320  * Since: 3.6
321  **/
322 void
323 e_source_authentication_set_host (ESourceAuthentication *extension,
324                                   const gchar *host)
325 {
326         g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
327
328         g_mutex_lock (extension->priv->property_lock);
329
330         g_free (extension->priv->host);
331         extension->priv->host = e_util_strdup_strip (host);
332
333         g_mutex_unlock (extension->priv->property_lock);
334
335         g_object_notify (G_OBJECT (extension), "host");
336 }
337
338 /**
339  * e_source_authentication_get_method:
340  * @extension: an #ESourceAuthentication
341  *
342  * Returns the authentication method for a remote account.  There are
343  * no pre-defined method names; backends are free to set this however
344  * they wish.  If authentication is not required for a remote account,
345  * the convention is to set #ESourceAuthentication:method to "none".
346  *
347  * Returns: the authentication method for a remote account
348  *
349  * Since: 3.6
350  **/
351 const gchar *
352 e_source_authentication_get_method (ESourceAuthentication *extension)
353 {
354         g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
355
356         return extension->priv->method;
357 }
358
359 /**
360  * e_source_authentication_dup_method:
361  * @extension: an #ESourceAuthentication
362  *
363  * Thread-safe variation of e_source_authentication_get_method().
364  * Use this function when accessing @extension from multiple threads.
365  *
366  * The returned string should be freed with g_free() when no longer needed.
367  *
368  * Returns: a newly-allocated copy of #ESourceAuthentication:method
369  *
370  * Since: 3.6
371  **/
372 gchar *
373 e_source_authentication_dup_method (ESourceAuthentication *extension)
374 {
375         const gchar *protected;
376         gchar *duplicate;
377
378         g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
379
380         g_mutex_lock (extension->priv->property_lock);
381
382         protected = e_source_authentication_get_method (extension);
383         duplicate = g_strdup (protected);
384
385         g_mutex_unlock (extension->priv->property_lock);
386
387         return duplicate;
388 }
389
390 /**
391  * e_source_authentication_set_method:
392  * @extension: an #ESourceAuthentication
393  * @method: (allow-none): authentication method, or %NULL
394  *
395  * Sets the authentication method for a remote account.  There are no
396  * pre-defined method names; backends are free to set this however they
397  * wish.  If authentication is not required for a remote account, the
398  * convention is to set the method to "none".  In keeping with that
399  * convention, #ESourceAuthentication:method will be set to "none" if
400  * @method is %NULL or an empty string.
401  *
402  * Since: 3.6
403  **/
404 void
405 e_source_authentication_set_method (ESourceAuthentication *extension,
406                                     const gchar *method)
407 {
408         g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
409
410         g_mutex_lock (extension->priv->property_lock);
411
412         g_free (extension->priv->method);
413         extension->priv->method = e_util_strdup_strip (method);
414
415         if (extension->priv->method == NULL)
416                 extension->priv->method = g_strdup ("none");
417
418         g_mutex_unlock (extension->priv->property_lock);
419
420         g_object_notify (G_OBJECT (extension), "method");
421 }
422
423 /**
424  * e_source_authentication_get_port:
425  * @extension: an #ESourceAuthentication
426  *
427  * Returns the port number used to authenticate to a remote account.
428  *
429  * Returns: the port number of a remote account
430  *
431  * Since: 3.6
432  **/
433 guint16
434 e_source_authentication_get_port (ESourceAuthentication *extension)
435 {
436         g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), 0);
437
438         return extension->priv->port;
439 }
440
441 /**
442  * e_source_authentication_set_port:
443  * @extension: an #ESourceAuthentication
444  * @port: a port number
445  *
446  * Sets the port number used to authenticate to a remote account.
447  *
448  * Since: 3.6
449  **/
450 void
451 e_source_authentication_set_port (ESourceAuthentication *extension,
452                                   guint16 port)
453 {
454         g_return_if_fail (E_SOURCE_AUTHENTICATION (extension));
455
456         extension->priv->port = port;
457
458         g_object_notify (G_OBJECT (extension), "port");
459 }
460
461 /**
462  * e_source_authentication_get_user:
463  * @extension: an #ESourceAuthentication
464  *
465  * Returns the user name used to authenticate to a remote account.
466  *
467  * Returns: the user name of a remote account
468  *
469  * Since: 3.6
470  **/
471 const gchar *
472 e_source_authentication_get_user (ESourceAuthentication *extension)
473 {
474         g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
475
476         return extension->priv->user;
477 }
478
479 /**
480  * e_source_authentication_dup_user:
481  * @extension: an #ESourceAuthentication
482  *
483  * Thread-safe variation of e_source_authentication_get_user().
484  * Use this function when accessing @extension from multiple threads.
485  *
486  * The returned string should be freed with g_free() when no longer needed.
487  *
488  * Returns: a newly-allocated copy of #ESourceAuthentication:user
489  *
490  * Since: 3.6
491  **/
492 gchar *
493 e_source_authentication_dup_user (ESourceAuthentication *extension)
494 {
495         const gchar *protected;
496         gchar *duplicate;
497
498         g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
499
500         g_mutex_lock (extension->priv->property_lock);
501
502         protected = e_source_authentication_get_user (extension);
503         duplicate = g_strdup (protected);
504
505         g_mutex_unlock (extension->priv->property_lock);
506
507         return duplicate;
508 }
509
510 /**
511  * e_source_authentication_set_user:
512  * @extension: an #ESourceAuthentication
513  * @user: (allow-none): a user name, or %NULL
514  *
515  * Sets the user name used to authenticate to a remote account.
516  *
517  * The internal copy of @user is automatically stripped of leading and
518  * trailing whitespace.  If the resulting string is empty, %NULL is set
519  * instead.
520  *
521  * Since: 3.6
522  **/
523 void
524 e_source_authentication_set_user (ESourceAuthentication *extension,
525                                   const gchar *user)
526 {
527         g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
528
529         g_mutex_lock (extension->priv->property_lock);
530
531         g_free (extension->priv->user);
532         extension->priv->user = e_util_strdup_strip (user);
533
534         g_mutex_unlock (extension->priv->property_lock);
535
536         g_object_notify (G_OBJECT (extension), "user");
537 }