Fix code examples in ESourceExtension API docs.
[platform/upstream/evolution-data-server.git] / libedataserver / e-source-mail-submission.c
1 /*
2  * e-source-mail-submission.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-mail-submission
21  * @include: libedataserver/libedataserver.h
22  * @short_description: #ESource extension for submitting emails
23  *
24  * The #ESourceMailSubmission extension tracks settings to be applied
25  * when submitting a mail message for delivery.
26  *
27  * Access the extension as follows:
28  *
29  * |[
30  *   #include <libedataserver/libedataserver.h>
31  *
32  *   ESourceMailSubmission *extension;
33  *
34  *   extension = e_source_get_extension (source, E_SOURCE_EXTENSION_MAIL_SUBMISSION);
35  * ]|
36  **/
37
38 #include "e-source-mail-submission.h"
39
40 #include <libedataserver/e-data-server-util.h>
41
42 #define E_SOURCE_MAIL_SUBMISSION_GET_PRIVATE(obj) \
43         (G_TYPE_INSTANCE_GET_PRIVATE \
44         ((obj), E_TYPE_SOURCE_MAIL_SUBMISSION, ESourceMailSubmissionPrivate))
45
46 struct _ESourceMailSubmissionPrivate {
47         GMutex *property_lock;
48         gchar *sent_folder;
49         gchar *transport_uid;
50 };
51
52 enum {
53         PROP_0,
54         PROP_SENT_FOLDER,
55         PROP_TRANSPORT_UID
56 };
57
58 G_DEFINE_TYPE (
59         ESourceMailSubmission,
60         e_source_mail_submission,
61         E_TYPE_SOURCE_EXTENSION)
62
63 static void
64 source_mail_submission_set_property (GObject *object,
65                                      guint property_id,
66                                      const GValue *value,
67                                      GParamSpec *pspec)
68 {
69         switch (property_id) {
70                 case PROP_SENT_FOLDER:
71                         e_source_mail_submission_set_sent_folder (
72                                 E_SOURCE_MAIL_SUBMISSION (object),
73                                 g_value_get_string (value));
74                         return;
75
76                 case PROP_TRANSPORT_UID:
77                         e_source_mail_submission_set_transport_uid (
78                                 E_SOURCE_MAIL_SUBMISSION (object),
79                                 g_value_get_string (value));
80                         return;
81         }
82
83         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
84 }
85
86 static void
87 source_mail_submission_get_property (GObject *object,
88                                      guint property_id,
89                                      GValue *value,
90                                      GParamSpec *pspec)
91 {
92         switch (property_id) {
93                 case PROP_SENT_FOLDER:
94                         g_value_take_string (
95                                 value,
96                                 e_source_mail_submission_dup_sent_folder (
97                                 E_SOURCE_MAIL_SUBMISSION (object)));
98                         return;
99
100                 case PROP_TRANSPORT_UID:
101                         g_value_take_string (
102                                 value,
103                                 e_source_mail_submission_dup_transport_uid (
104                                 E_SOURCE_MAIL_SUBMISSION (object)));
105                         return;
106         }
107
108         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
109 }
110
111 static void
112 source_mail_submission_finalize (GObject *object)
113 {
114         ESourceMailSubmissionPrivate *priv;
115
116         priv = E_SOURCE_MAIL_SUBMISSION_GET_PRIVATE (object);
117
118         g_mutex_free (priv->property_lock);
119
120         g_free (priv->sent_folder);
121         g_free (priv->transport_uid);
122
123         /* Chain up to parent's finalize() method. */
124         G_OBJECT_CLASS (e_source_mail_submission_parent_class)->
125                 finalize (object);
126 }
127
128 static void
129 e_source_mail_submission_class_init (ESourceMailSubmissionClass *class)
130 {
131         GObjectClass *object_class;
132         ESourceExtensionClass *extension_class;
133
134         g_type_class_add_private (
135                 class, sizeof (ESourceMailSubmissionPrivate));
136
137         object_class = G_OBJECT_CLASS (class);
138         object_class->set_property = source_mail_submission_set_property;
139         object_class->get_property = source_mail_submission_get_property;
140         object_class->finalize = source_mail_submission_finalize;
141
142         extension_class = E_SOURCE_EXTENSION_CLASS (class);
143         extension_class->name = E_SOURCE_EXTENSION_MAIL_SUBMISSION;
144
145         g_object_class_install_property (
146                 object_class,
147                 PROP_SENT_FOLDER,
148                 g_param_spec_string (
149                         "sent-folder",
150                         "Sent Folder",
151                         "Preferred folder for sent messages",
152                         NULL,
153                         G_PARAM_READWRITE |
154                         G_PARAM_CONSTRUCT |
155                         G_PARAM_STATIC_STRINGS |
156                         E_SOURCE_PARAM_SETTING));
157
158         g_object_class_install_property (
159                 object_class,
160                 PROP_TRANSPORT_UID,
161                 g_param_spec_string (
162                         "transport-uid",
163                         "Transport UID",
164                         "ESource UID of a Mail Transport",
165                         NULL,
166                         G_PARAM_READWRITE |
167                         G_PARAM_CONSTRUCT |
168                         G_PARAM_STATIC_STRINGS |
169                         E_SOURCE_PARAM_SETTING));
170 }
171
172 static void
173 e_source_mail_submission_init (ESourceMailSubmission *extension)
174 {
175         extension->priv = E_SOURCE_MAIL_SUBMISSION_GET_PRIVATE (extension);
176         extension->priv->property_lock = g_mutex_new ();
177 }
178
179 /**
180  * e_source_mail_submission_get_sent_folder:
181  * @extension: an #ESourceMailSubmission
182  *
183  * Returns a string identifying the preferred folder for sent messages.
184  * The format of the identifier string is defined by the client application.
185  *
186  * Returns: an identifier for the preferred sent folder
187  *
188  * Since: 3.6
189  **/
190 const gchar *
191 e_source_mail_submission_get_sent_folder (ESourceMailSubmission *extension)
192 {
193         g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), NULL);
194
195         return extension->priv->sent_folder;
196 }
197
198 /**
199  * e_source_mail_submission_dup_sent_folder:
200  * @extension: an #ESourceMailSubmission
201  *
202  * Thread-safe variation of e_source_mail_submission_get_sent_folder().
203  * Use this function when accessing @extension from multiple threads.
204  *
205  * The returned string should be freed with g_free() when no longer needed.
206  *
207  * Returns: a newly-allocated copy of #ESourceMailSubmission:sent-folder
208  *
209  * Since: 3.6
210  **/
211 gchar *
212 e_source_mail_submission_dup_sent_folder (ESourceMailSubmission *extension)
213 {
214         const gchar *protected;
215         gchar *duplicate;
216
217         g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), NULL);
218
219         g_mutex_lock (extension->priv->property_lock);
220
221         protected = e_source_mail_submission_get_sent_folder (extension);
222         duplicate = g_strdup (protected);
223
224         g_mutex_unlock (extension->priv->property_lock);
225
226         return duplicate;
227 }
228
229 /**
230  * e_source_mail_submission_set_sent_folder:
231  * @extension: an #ESourceMailSubmission
232  * @sent_folder: (allow-none): an identifier for the preferred sent folder,
233  *               or %NULL
234  *
235  * Sets the preferred folder for sent messages by an identifier string.
236  * The format of the identifier string is defined by the client application.
237  *
238  * The internal copy of @sent_folder is automatically stripped of leading
239  * and trailing whitespace.  If the resulting string is empty, %NULL is set
240  * instead.
241  *
242  * Since: 3.6
243  **/
244 void
245 e_source_mail_submission_set_sent_folder (ESourceMailSubmission *extension,
246                                           const gchar *sent_folder)
247 {
248         g_return_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension));
249
250         g_mutex_lock (extension->priv->property_lock);
251
252         if (g_strcmp0 (extension->priv->sent_folder, sent_folder) == 0) {
253                 g_mutex_unlock (extension->priv->property_lock);
254                 return;
255         }
256
257         g_free (extension->priv->sent_folder);
258         extension->priv->sent_folder = e_util_strdup_strip (sent_folder);
259
260         g_mutex_unlock (extension->priv->property_lock);
261
262         g_object_notify (G_OBJECT (extension), "sent-folder");
263 }
264
265 /**
266  * e_source_mail_submission_get_transport_uid:
267  * @extension: an #ESourceMailSubmission
268  *
269  * Returns the #ESource:uid of the #ESource that describes the mail
270  * transport to be used for outgoing messages.
271  *
272  * Returns: the mail transport #ESource:uid
273  *
274  * Since: 3.6
275  **/
276 const gchar *
277 e_source_mail_submission_get_transport_uid (ESourceMailSubmission *extension)
278 {
279         g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), NULL);
280
281         return extension->priv->transport_uid;
282 }
283
284 /**
285  * e_source_mail_submission_dup_transport_uid:
286  * @extension: an #ESourceMailSubmission
287  *
288  * Thread-safe variation of e_source_mail_submission_get_transport_uid().
289  * Use this function when accessing @extension from multiple threads.
290  *
291  * The returned string should be freed with g_free() when no longer needed.
292  *
293  * Returns: a newly-allocated copy of #ESourceMailSubmission:transport-uid
294  *
295  * Since: 3.6
296  **/
297 gchar *
298 e_source_mail_submission_dup_transport_uid (ESourceMailSubmission *extension)
299 {
300         const gchar *protected;
301         gchar *duplicate;
302
303         g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), NULL);
304
305         g_mutex_lock (extension->priv->property_lock);
306
307         protected = e_source_mail_submission_get_transport_uid (extension);
308         duplicate = g_strdup (protected);
309
310         g_mutex_unlock (extension->priv->property_lock);
311
312         return duplicate;
313 }
314
315 /**
316  * e_source_mail_submission_set_transport_uid:
317  * @extension: an #ESourceMailSubmission
318  * @transport_uid: (allow-none): the mail transport #ESource:uid, or %NULL
319  *
320  * Sets the #ESource:uid of the #ESource that describes the mail
321  * transport to be used for outgoing messages.
322  *
323  * Since: 3.6
324  **/
325 void
326 e_source_mail_submission_set_transport_uid (ESourceMailSubmission *extension,
327                                             const gchar *transport_uid)
328 {
329         g_return_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension));
330
331         g_mutex_lock (extension->priv->property_lock);
332
333         if (g_strcmp0 (extension->priv->transport_uid, transport_uid) == 0) {
334                 g_mutex_unlock (extension->priv->property_lock);
335                 return;
336         }
337
338         g_free (extension->priv->transport_uid);
339         extension->priv->transport_uid = g_strdup (transport_uid);
340
341         g_mutex_unlock (extension->priv->property_lock);
342
343         g_object_notify (G_OBJECT (extension), "transport-uid");
344 }