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.
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.
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/>
20 * SECTION: e-source-resource
21 * @include: libedataserver/libedataserver.h
22 * @short_description: #ESource extension for a remote resource
24 * The #ESourceResource extension holds the server-assigned identity of a
25 * remote calendar, address book, or whatever else an #ESource can represent.
27 * This extension is typically used by an #ECollectionBackend to note a
28 * server-assigned resource identity in an #ESource. Then in a later session,
29 * after querying the server for available resources, a resource identity can
30 * be paired with the same #ESource #ESource:uid from the previous session,
31 * allowing locally cached data from the previous session to be reused.
33 * Access the extension as follows:
36 * #include <libedataserver/libedataserver.h>
38 * ESourceResource *extension;
40 * extension = e_source_get_extension (source, E_SOURCE_EXTENSION_RESOURCE);
44 #include "e-source-resource.h"
46 #include <libedataserver/e-data-server-util.h>
48 #define E_SOURCE_RESOURCE_GET_PRIVATE(obj) \
49 (G_TYPE_INSTANCE_GET_PRIVATE \
50 ((obj), E_TYPE_SOURCE_RESOURCE, ESourceResourcePrivate))
52 struct _ESourceResourcePrivate {
65 E_TYPE_SOURCE_EXTENSION)
68 source_resource_set_property (GObject *object,
73 switch (property_id) {
75 e_source_resource_set_identity (
76 E_SOURCE_RESOURCE (object),
77 g_value_get_string (value));
81 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
85 source_resource_get_property (GObject *object,
90 switch (property_id) {
94 e_source_resource_dup_identity (
95 E_SOURCE_RESOURCE (object)));
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
103 source_resource_finalize (GObject *object)
105 ESourceResourcePrivate *priv;
107 priv = E_SOURCE_RESOURCE_GET_PRIVATE (object);
109 g_mutex_clear (&priv->property_lock);
111 g_free (priv->identity);
113 /* Chain up to parent's finalize() method. */
114 G_OBJECT_CLASS (e_source_resource_parent_class)->finalize (object);
118 e_source_resource_class_init (ESourceResourceClass *class)
120 GObjectClass *object_class;
121 ESourceExtensionClass *extension_class;
123 g_type_class_add_private (class, sizeof (ESourceResourcePrivate));
125 object_class = G_OBJECT_CLASS (class);
126 object_class->set_property = source_resource_set_property;
127 object_class->get_property = source_resource_get_property;
128 object_class->finalize = source_resource_finalize;
130 extension_class = E_SOURCE_EXTENSION_CLASS (class);
131 extension_class->name = E_SOURCE_EXTENSION_RESOURCE;
133 g_object_class_install_property (
136 g_param_spec_string (
143 G_PARAM_STATIC_STRINGS |
144 E_SOURCE_PARAM_SETTING));
148 e_source_resource_init (ESourceResource *extension)
150 extension->priv = E_SOURCE_RESOURCE_GET_PRIVATE (extension);
151 g_mutex_init (&extension->priv->property_lock);
155 * e_source_resource_get_identity:
156 * @extension: an #ESourceResource
158 * Returns the server-assigned identity of the remote resource associated
159 * with the #ESource to which @extension belongs.
161 * Returns: the identity of a remote resource
166 e_source_resource_get_identity (ESourceResource *extension)
168 g_return_val_if_fail (E_IS_SOURCE_RESOURCE (extension), NULL);
170 return extension->priv->identity;
174 * e_source_resource_dup_identity:
175 * @extension: an #ESourceResource
177 * Thread-safe variation of e_source_resource_get_identity().
178 * Use this function when accessing @extension from multiple threads.
180 * The returned string should be freed with g_free() when no longer needed.
182 * Returns: a newly-allocated copy of #ESourceResource:identity
187 e_source_resource_dup_identity (ESourceResource *extension)
189 const gchar *protected;
192 g_return_val_if_fail (E_IS_SOURCE_RESOURCE (extension), NULL);
194 g_mutex_lock (&extension->priv->property_lock);
196 protected = e_source_resource_get_identity (extension);
197 duplicate = g_strdup (protected);
199 g_mutex_unlock (&extension->priv->property_lock);
205 * e_source_resource_set_identity:
206 * @extension: an #ESourceResource
207 * @identity: (allow-none): the identity of a remote resource
209 * Sets the server-assigned identity of the remote resource associated with
210 * the #ESource to which @extension belongs.
212 * The internal copy of @identity is automatically stripped of leading and
213 * trailing whitespace. If the resulting string is empty, %NULL is set
219 e_source_resource_set_identity (ESourceResource *extension,
220 const gchar *identity)
222 g_return_if_fail (E_IS_SOURCE_RESOURCE (extension));
224 g_mutex_lock (&extension->priv->property_lock);
226 if (g_strcmp0 (extension->priv->identity, identity) == 0) {
227 g_mutex_unlock (&extension->priv->property_lock);
231 g_free (extension->priv->identity);
232 extension->priv->identity = e_util_strdup_strip (identity);
234 g_mutex_unlock (&extension->priv->property_lock);
236 g_object_notify (G_OBJECT (extension), "identity");