Camel: Allow SSL certificate resave and use detailed errors from SSL stream
[platform/upstream/evolution-data-server.git] / libebackend / e-backend.c
1 /*
2  * e-backend.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-backend
21  * @short_description: an abstract base class for backends
22  * @include: libebackend/e-backend.h
23  *
24  * An #EBackend is paired with an #ESource to facilitate performing
25  * actions on the local or remote resource described by the #ESource.
26  *
27  * In other words, whereas a certain backend type knows how to talk to a
28  * certain type of server or data store, the #ESource fills in configuration
29  * details such as host name, user name, resource path, etc.
30  *
31  * All #EBackend instances are created by an #EBackendFactory.
32  **/
33
34 #include "e-backend.h"
35
36 #include <config.h>
37 #include <gio/gio.h>
38
39 #define E_BACKEND_GET_PRIVATE(obj) \
40         (G_TYPE_INSTANCE_GET_PRIVATE \
41         ((obj), E_TYPE_BACKEND, EBackendPrivate))
42
43 struct _EBackendPrivate {
44         ESource *source;
45         gboolean online;
46 };
47
48 enum {
49         PROP_0,
50         PROP_ONLINE,
51         PROP_SOURCE
52 };
53
54 G_DEFINE_ABSTRACT_TYPE (EBackend, e_backend, G_TYPE_OBJECT)
55
56 static void
57 backend_set_source (EBackend *backend,
58                     ESource *source)
59 {
60         g_return_if_fail (E_IS_SOURCE (source));
61         g_return_if_fail (backend->priv->source == NULL);
62
63         backend->priv->source = g_object_ref (source);
64 }
65
66 static void
67 backend_set_property (GObject *object,
68                       guint property_id,
69                       const GValue *value,
70                       GParamSpec *pspec)
71 {
72         switch (property_id) {
73                 case PROP_ONLINE:
74                         e_backend_set_online (
75                                 E_BACKEND (object),
76                                 g_value_get_boolean (value));
77                         return;
78
79                 case PROP_SOURCE:
80                         backend_set_source (
81                                 E_BACKEND (object),
82                                 g_value_get_object (value));
83                         return;
84         }
85
86         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
87 }
88
89 static void
90 backend_get_property (GObject *object,
91                       guint property_id,
92                       GValue *value,
93                       GParamSpec *pspec)
94 {
95         switch (property_id) {
96                 case PROP_ONLINE:
97                         g_value_set_boolean (
98                                 value, e_backend_get_online (
99                                 E_BACKEND (object)));
100                         return;
101
102                 case PROP_SOURCE:
103                         g_value_set_object (
104                                 value, e_backend_get_source (
105                                 E_BACKEND (object)));
106                         return;
107         }
108
109         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
110 }
111
112 static void
113 backend_dispose (GObject *object)
114 {
115         EBackendPrivate *priv;
116
117         priv = E_BACKEND_GET_PRIVATE (object);
118
119         if (priv->source != NULL) {
120                 g_object_unref (priv->source);
121                 priv->source = NULL;
122         }
123
124         /* Chain up to parent's dispose() method. */
125         G_OBJECT_CLASS (e_backend_parent_class)->dispose (object);
126 }
127
128 static void
129 backend_constructed (GObject *object)
130 {
131         GNetworkMonitor *monitor;
132
133         /* Chain up to parent's constructed() method. */
134         G_OBJECT_CLASS (e_backend_parent_class)->constructed (object);
135
136         /* Synchronize network monitoring. */
137
138         monitor = g_network_monitor_get_default ();
139
140         g_object_bind_property (
141                 monitor, "network-available",
142                 object, "online",
143                 G_BINDING_SYNC_CREATE);
144 }
145
146 static void
147 e_backend_class_init (EBackendClass *class)
148 {
149         GObjectClass *object_class;
150
151         g_type_class_add_private (class, sizeof (EBackendPrivate));
152
153         object_class = G_OBJECT_CLASS (class);
154         object_class->set_property = backend_set_property;
155         object_class->get_property = backend_get_property;
156         object_class->dispose = backend_dispose;
157         object_class->constructed = backend_constructed;
158
159         g_object_class_install_property (
160                 object_class,
161                 PROP_ONLINE,
162                 g_param_spec_boolean (
163                         "online",
164                         "Online",
165                         "Whether the backend is online",
166                         TRUE,
167                         G_PARAM_READWRITE |
168                         G_PARAM_CONSTRUCT |
169                         G_PARAM_STATIC_STRINGS));
170
171         g_object_class_install_property (
172                 object_class,
173                 PROP_SOURCE,
174                 g_param_spec_object (
175                         "source",
176                         "Source",
177                         "The data source being acted upon",
178                         E_TYPE_SOURCE,
179                         G_PARAM_READWRITE |
180                         G_PARAM_CONSTRUCT_ONLY |
181                         G_PARAM_STATIC_STRINGS));
182 }
183
184 static void
185 e_backend_init (EBackend *backend)
186 {
187         backend->priv = E_BACKEND_GET_PRIVATE (backend);
188 }
189
190 /**
191  * e_backend_get_online:
192  * @backend: an #EBackend
193  *
194  * Returns the online state of @backend: %TRUE if @backend is online,
195  * %FALSE if offline.  The online state of each backend is bound to the
196  * online state of the #EDataFactory that created it.
197  *
198  * Returns: the online state
199  *
200  * Since: 3.4
201  **/
202 gboolean
203 e_backend_get_online (EBackend *backend)
204 {
205         g_return_val_if_fail (E_IS_BACKEND (backend), FALSE);
206
207         return backend->priv->online;
208 }
209
210 /**
211  * e_backend_set_online:
212  * @backend: an #EBackend
213  * @online: the online state
214  *
215  * Sets the online state of @backend: %TRUE if @backend is online,
216  * @FALSE if offline.  The online state of each backend is bound to
217  * the online state of the #EDataFactory that created it.
218  *
219  * Since: 3.4
220  **/
221 void
222 e_backend_set_online (EBackend *backend,
223                       gboolean online)
224 {
225         g_return_if_fail (E_IS_BACKEND (backend));
226
227         /* Avoid unnecessary "notify" signals. */
228         if (online == backend->priv->online)
229                 return;
230
231         backend->priv->online = online;
232
233         g_object_notify (G_OBJECT (backend), "online");
234 }
235
236 /**
237  * e_backend_get_source:
238  * @backend: an #EBackend
239  *
240  * Returns the #ESource to which @backend is paired.
241  *
242  * Returns: the #ESource to which @backend is paired
243  *
244  * Since: 3.4
245  **/
246 ESource *
247 e_backend_get_source (EBackend *backend)
248 {
249         g_return_val_if_fail (E_IS_BACKEND (backend), NULL);
250
251         return backend->priv->source;
252 }
253