changes: Bump to 3.8.1
[platform/upstream/evolution-data-server.git] / libedataserver / e-source-collection.c
1 /*
2  * e-source-collection.c
3  *
4  * This library is free software you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 /**
19  * SECTION: e-source-collection
20  * @include: libedataserver/libedataserver.h
21  * @short_description: #ESource extension for grouping related resources
22  *
23  * The #ESourceCollection extension identifies the #ESource as the root
24  * of a data source collection.
25  *
26  * Access the extension as follows:
27  *
28  * |[
29  *    #include <libedataserver/libedataserver.h>
30  *
31  *    ESourceCollection *extension;
32  *
33  *    extension = e_source_get_extension (source, E_SOURCE_EXTENSION_COLLECTION);
34  * ]|
35  **/
36
37 #include "e-source-collection.h"
38
39 #include <config.h>
40 #include <glib/gi18n-lib.h>
41
42 /* Private D-Bus classes. */
43 #include <e-dbus-source.h>
44
45 #include <libedataserver/e-data-server-util.h>
46
47 #define E_SOURCE_COLLECTION_GET_PRIVATE(obj) \
48         (G_TYPE_INSTANCE_GET_PRIVATE \
49         ((obj), E_TYPE_SOURCE_COLLECTION, ESourceCollectionPrivate))
50
51 struct _ESourceCollectionPrivate {
52         GMutex property_lock;
53         gchar *identity;
54         gboolean calendar_enabled;
55         gboolean contacts_enabled;
56         gboolean mail_enabled;
57 };
58
59 enum {
60         PROP_0,
61         PROP_CALENDAR_ENABLED,
62         PROP_CONTACTS_ENABLED,
63         PROP_IDENTITY,
64         PROP_MAIL_ENABLED
65 };
66
67 G_DEFINE_TYPE (
68         ESourceCollection,
69         e_source_collection,
70         E_TYPE_SOURCE_BACKEND)
71
72 static void
73 source_collection_set_property (GObject *object,
74                                 guint property_id,
75                                 const GValue *value,
76                                 GParamSpec *pspec)
77 {
78         switch (property_id) {
79                 case PROP_CALENDAR_ENABLED:
80                         e_source_collection_set_calendar_enabled (
81                                 E_SOURCE_COLLECTION (object),
82                                 g_value_get_boolean (value));
83                         return;
84
85                 case PROP_CONTACTS_ENABLED:
86                         e_source_collection_set_contacts_enabled (
87                                 E_SOURCE_COLLECTION (object),
88                                 g_value_get_boolean (value));
89                         return;
90
91                 case PROP_IDENTITY:
92                         e_source_collection_set_identity (
93                                 E_SOURCE_COLLECTION (object),
94                                 g_value_get_string (value));
95                         return;
96
97                 case PROP_MAIL_ENABLED:
98                         e_source_collection_set_mail_enabled (
99                                 E_SOURCE_COLLECTION (object),
100                                 g_value_get_boolean (value));
101                         return;
102         }
103
104         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
105 }
106
107 static void
108 source_collection_get_property (GObject *object,
109                                 guint property_id,
110                                 GValue *value,
111                                 GParamSpec *pspec)
112 {
113         switch (property_id) {
114                 case PROP_CALENDAR_ENABLED:
115                         g_value_set_boolean (
116                                 value,
117                                 e_source_collection_get_calendar_enabled (
118                                 E_SOURCE_COLLECTION (object)));
119                         return;
120
121                 case PROP_CONTACTS_ENABLED:
122                         g_value_set_boolean (
123                                 value,
124                                 e_source_collection_get_contacts_enabled (
125                                 E_SOURCE_COLLECTION (object)));
126                         return;
127
128                 case PROP_IDENTITY:
129                         g_value_take_string (
130                                 value,
131                                 e_source_collection_dup_identity (
132                                 E_SOURCE_COLLECTION (object)));
133                         return;
134
135                 case PROP_MAIL_ENABLED:
136                         g_value_set_boolean (
137                                 value,
138                                 e_source_collection_get_mail_enabled (
139                                 E_SOURCE_COLLECTION (object)));
140                         return;
141         }
142
143         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
144 }
145
146 static void
147 source_collection_finalize (GObject *object)
148 {
149         ESourceCollectionPrivate *priv;
150
151         priv = E_SOURCE_COLLECTION_GET_PRIVATE (object);
152
153         g_mutex_clear (&priv->property_lock);
154
155         g_free (priv->identity);
156
157         /* Chain up to parent's finalize() method. */
158         G_OBJECT_CLASS (e_source_collection_parent_class)->finalize (object);
159 }
160
161 static void
162 e_source_collection_class_init (ESourceCollectionClass *class)
163 {
164         GObjectClass *object_class;
165         ESourceExtensionClass *extension_class;
166
167         g_type_class_add_private (class, sizeof (ESourceCollectionPrivate));
168
169         object_class = G_OBJECT_CLASS (class);
170         object_class->set_property = source_collection_set_property;
171         object_class->get_property = source_collection_get_property;
172         object_class->finalize = source_collection_finalize;
173
174         extension_class = E_SOURCE_EXTENSION_CLASS (class);
175         extension_class->name = E_SOURCE_EXTENSION_COLLECTION;
176
177         g_object_class_install_property (
178                 object_class,
179                 PROP_CALENDAR_ENABLED,
180                 g_param_spec_boolean (
181                         "calendar-enabled",
182                         "Calendar Enabled",
183                         "Whether calendar resources are enabled",
184                         TRUE,
185                         G_PARAM_READWRITE |
186                         G_PARAM_CONSTRUCT |
187                         G_PARAM_STATIC_STRINGS |
188                         E_SOURCE_PARAM_SETTING));
189
190         g_object_class_install_property (
191                 object_class,
192                 PROP_CONTACTS_ENABLED,
193                 g_param_spec_boolean (
194                         "contacts-enabled",
195                         "Contacts Enabled",
196                         "Whether contact resources are enabled",
197                         TRUE,
198                         G_PARAM_READWRITE |
199                         G_PARAM_CONSTRUCT |
200                         G_PARAM_STATIC_STRINGS |
201                         E_SOURCE_PARAM_SETTING));
202
203         g_object_class_install_property (
204                 object_class,
205                 PROP_IDENTITY,
206                 g_param_spec_string (
207                         "identity",
208                         "Identity",
209                         "Uniquely identifies the account "
210                         "at the service provider",
211                         NULL,
212                         G_PARAM_READWRITE |
213                         G_PARAM_CONSTRUCT |
214                         G_PARAM_STATIC_STRINGS |
215                         E_SOURCE_PARAM_SETTING));
216
217         g_object_class_install_property (
218                 object_class,
219                 PROP_MAIL_ENABLED,
220                 g_param_spec_boolean (
221                         "mail-enabled",
222                         "Mail Enabled",
223                         "Whether mail resources are enabled",
224                         TRUE,
225                         G_PARAM_READWRITE |
226                         G_PARAM_CONSTRUCT |
227                         G_PARAM_STATIC_STRINGS |
228                         E_SOURCE_PARAM_SETTING));
229 }
230
231 static void
232 e_source_collection_init (ESourceCollection *extension)
233 {
234         extension->priv = E_SOURCE_COLLECTION_GET_PRIVATE (extension);
235         g_mutex_init (&extension->priv->property_lock);
236 }
237
238 /**
239  * e_source_collection_get_identity:
240  * @extension: an #ESourceCollection
241  *
242  * Returns the string used to uniquely identify the user account at
243  * the service provider.  Often this is an email address or user name.
244  *
245  * Returns: the collection identity
246  *
247  * Since: 3.6
248  **/
249 const gchar *
250 e_source_collection_get_identity (ESourceCollection *extension)
251 {
252         g_return_val_if_fail (E_IS_SOURCE_COLLECTION (extension), NULL);
253
254         return extension->priv->identity;
255 }
256
257 /**
258  * e_source_collection_dup_identity:
259  * @extension: an #ESourceCollection
260  *
261  * Thread-safe variation of e_source_collection_get_identity().
262  * Use this function when accessing @extension from multiple threads.
263  *
264  * The returned string should be freed with g_free() when no longer needed.
265  *
266  * Returns: a newly-allocated copy of #ESourceCollection:identity
267  *
268  * Since: 3.6
269  **/
270 gchar *
271 e_source_collection_dup_identity (ESourceCollection *extension)
272 {
273         const gchar *protected;
274         gchar *duplicate;
275
276         g_return_val_if_fail (E_IS_SOURCE_COLLECTION (extension), NULL);
277
278         g_mutex_lock (&extension->priv->property_lock);
279
280         protected = e_source_collection_get_identity (extension);
281         duplicate = g_strdup (protected);
282
283         g_mutex_unlock (&extension->priv->property_lock);
284
285         return duplicate;
286 }
287
288 /**
289  * e_source_collection_set_identity:
290  * @extension: an #ESourceCollection
291  * @identity: (allow-none): the collection identity, or %NULL
292  *
293  * Sets the string used to uniquely identify the user account at the
294  * service provider.  Often this is an email address or user name.
295  *
296  * The internal copy of @identity is automatically stripped of leading
297  * and trailing whitespace.  If the resulting string is empty, %NULL is
298  * set instead.
299  *
300  * Since: 3.6
301  **/
302 void
303 e_source_collection_set_identity (ESourceCollection *extension,
304                                   const gchar *identity)
305 {
306         g_return_if_fail (E_IS_SOURCE_COLLECTION (extension));
307
308         g_mutex_lock (&extension->priv->property_lock);
309
310         if (g_strcmp0 (extension->priv->identity, identity) == 0) {
311                 g_mutex_unlock (&extension->priv->property_lock);
312                 return;
313         }
314
315         g_free (extension->priv->identity);
316         extension->priv->identity = e_util_strdup_strip (identity);
317
318         g_mutex_unlock (&extension->priv->property_lock);
319
320         g_object_notify (G_OBJECT (extension), "identity");
321 }
322
323 /**
324  * e_source_collection_get_calendar_enabled:
325  * @extension: an #ESourceCollection
326  *
327  * Returns whether calendar sources within the collection should be
328  * enabled.
329  *
330  * An #ECollectionBackend running within the registry D-Bus service will
331  * automatically synchronize any calendar sources it maintains with the
332  * #ESourceCollection:calendar-enabled property.
333  *
334  * Returns: whether calendar sources should be enabled
335  *
336  * Since: 3.6
337  **/
338 gboolean
339 e_source_collection_get_calendar_enabled (ESourceCollection *extension)
340 {
341         g_return_val_if_fail (E_IS_SOURCE_COLLECTION (extension), FALSE);
342
343         return extension->priv->calendar_enabled;
344 }
345
346 /**
347  * e_source_collection_set_calendar_enabled:
348  * @extension: an #ESourceCollection
349  * @calendar_enabled: whether calendar sources should be enabled
350  *
351  * Sets whether calendar sources within the collection should be enabled.
352  *
353  * An #ECollectionBackend running within the registry D-Bus service will
354  * automatically synchronize any calendar sources it maintains with the
355  * #ESourceCollection:calendar-enabled property.
356  *
357  * Calling this function from a registry service client has no effect until
358  * the change is submitted to the registry service through e_source_write(),
359  * but there should rarely be any need for clients to call this.
360  *
361  * Since: 3.6
362  **/
363 void
364 e_source_collection_set_calendar_enabled (ESourceCollection *extension,
365                                           gboolean calendar_enabled)
366 {
367         g_return_if_fail (E_IS_SOURCE_COLLECTION (extension));
368
369         if (extension->priv->calendar_enabled == calendar_enabled)
370                 return;
371
372         extension->priv->calendar_enabled = calendar_enabled;
373
374         g_object_notify (G_OBJECT (extension), "calendar-enabled");
375 }
376
377 /**
378  * e_source_collection_get_contacts_enabled:
379  * @extension: an #ESourceCollection
380  *
381  * Returns whether address book sources within the collection should be
382  * enabled.
383  *
384  * An #ECollectionBackend running within the registry D-Bus service will
385  * automatically synchronize any address book sources it maintains with
386  * the #ESourceCollection:contacts-enabled property.
387  *
388  * Returns: whether address book sources should be enabled
389  *
390  * Since: 3.6
391  **/
392 gboolean
393 e_source_collection_get_contacts_enabled (ESourceCollection *extension)
394 {
395         g_return_val_if_fail (E_IS_SOURCE_COLLECTION (extension), FALSE);
396
397         return extension->priv->contacts_enabled;
398 }
399
400 /**
401  * e_source_collection_set_contacts_enabled:
402  * @extension: an #ESourceCollection
403  * @contacts_enabled: whether address book sources should be enabled
404  *
405  * Sets whether address book sources within the collection should be enabled.
406  *
407  * An #ECollectionBackend running within the registry D-Bus service will
408  * automatically synchronize any address book sources it maintains with
409  * the #ESourceCollection:contacts-enabled property.
410  *
411  * Calling this function from a registry service client has no effect until
412  * the change is submitted to the registry service through e_source_write(),
413  * but there should rarely be any need for clients to call this.
414  *
415  * Since: 3.6
416  **/
417 void
418 e_source_collection_set_contacts_enabled (ESourceCollection *extension,
419                                           gboolean contacts_enabled)
420 {
421         g_return_if_fail (E_IS_SOURCE_COLLECTION (extension));
422
423         if (extension->priv->contacts_enabled == contacts_enabled)
424                 return;
425
426         extension->priv->contacts_enabled = contacts_enabled;
427
428         g_object_notify (G_OBJECT (extension), "contacts-enabled");
429 }
430
431 /**
432  * e_source_collection_get_mail_enabled:
433  * @extension: an #ESourceCollection
434  *
435  * Returns whether mail sources within the collection should be enabled.
436  *
437  * An #ECollectionBackend running within the registry D-Bus service will
438  * automatically synchronize any mail sources it maintains with the
439  * #ESourceCollection:mail-enabled property.
440  *
441  * Returns: whether mail sources should be enabled
442  *
443  * Since: 3.6
444  **/
445 gboolean
446 e_source_collection_get_mail_enabled (ESourceCollection *extension)
447 {
448         g_return_val_if_fail (E_IS_SOURCE_COLLECTION (extension), FALSE);
449
450         return extension->priv->mail_enabled;
451 }
452
453 /**
454  * e_source_collection_set_mail_enabled:
455  * @extension: an #ESourceCollection
456  * @mail_enabled: whether mail sources should be enabled
457  *
458  * Sets whether mail sources within the collection should be enabled.
459  *
460  * An #ECollectionBackend running within the registry D-Bus service will
461  * automatically synchronize any mail sources it maintains with the
462  * #ESourceCollection:mail-enabled property.
463  *
464  * Calling this function from a registry service client has no effect until
465  * the changes is submitted to the registry service through e_source_write(),
466  * but there should rarely be any need for clients to call this.
467  *
468  * Since: 3.6
469  **/
470 void
471 e_source_collection_set_mail_enabled (ESourceCollection *extension,
472                                       gboolean mail_enabled)
473 {
474         g_return_if_fail (E_IS_SOURCE_COLLECTION (extension));
475
476         if (extension->priv->mail_enabled == mail_enabled)
477                 return;
478
479         extension->priv->mail_enabled = mail_enabled;
480
481         g_object_notify (G_OBJECT (extension), "mail-enabled");
482 }
483