summaryrefslogtreecommitdiff
path: root/src/libedataserver/e-source-mail-submission.c
blob: ba9aafd0d96fe726dc987df838ad40833d9600fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 * e-source-mail-submission.c
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * SECTION: e-source-mail-submission
 * @include: libedataserver/libedataserver.h
 * @short_description: #ESource extension for submitting emails
 *
 * The #ESourceMailSubmission extension tracks settings to be applied
 * when submitting a mail message for delivery.
 *
 * Access the extension as follows:
 *
 * |[
 *   #include <libedataserver/libedataserver.h>
 *
 *   ESourceMailSubmission *extension;
 *
 *   extension = e_source_get_extension (source, E_SOURCE_EXTENSION_MAIL_SUBMISSION);
 * ]|
 **/

#include "e-source-mail-submission.h"

#include <libedataserver/e-data-server-util.h>

struct _ESourceMailSubmissionPrivate {
	gchar *sent_folder;
	gchar *transport_uid;
	gboolean replies_to_origin_folder;
	gboolean use_sent_folder;
};

enum {
	PROP_0,
	PROP_SENT_FOLDER,
	PROP_TRANSPORT_UID,
	PROP_REPLIES_TO_ORIGIN_FOLDER,
	PROP_USE_SENT_FOLDER
};

G_DEFINE_TYPE_WITH_PRIVATE (
	ESourceMailSubmission,
	e_source_mail_submission,
	E_TYPE_SOURCE_EXTENSION)

static void
source_mail_submission_set_property (GObject *object,
                                     guint property_id,
                                     const GValue *value,
                                     GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_SENT_FOLDER:
			e_source_mail_submission_set_sent_folder (
				E_SOURCE_MAIL_SUBMISSION (object),
				g_value_get_string (value));
			return;

		case PROP_TRANSPORT_UID:
			e_source_mail_submission_set_transport_uid (
				E_SOURCE_MAIL_SUBMISSION (object),
				g_value_get_string (value));
			return;

		case PROP_REPLIES_TO_ORIGIN_FOLDER:
			e_source_mail_submission_set_replies_to_origin_folder (
				E_SOURCE_MAIL_SUBMISSION (object),
				g_value_get_boolean (value));
			return;

		case PROP_USE_SENT_FOLDER:
			e_source_mail_submission_set_use_sent_folder (
				E_SOURCE_MAIL_SUBMISSION (object),
				g_value_get_boolean (value));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
source_mail_submission_get_property (GObject *object,
                                     guint property_id,
                                     GValue *value,
                                     GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_SENT_FOLDER:
			g_value_take_string (
				value,
				e_source_mail_submission_dup_sent_folder (
				E_SOURCE_MAIL_SUBMISSION (object)));
			return;

		case PROP_TRANSPORT_UID:
			g_value_take_string (
				value,
				e_source_mail_submission_dup_transport_uid (
				E_SOURCE_MAIL_SUBMISSION (object)));
			return;

		case PROP_REPLIES_TO_ORIGIN_FOLDER:
			g_value_set_boolean (
				value,
				e_source_mail_submission_get_replies_to_origin_folder (
				E_SOURCE_MAIL_SUBMISSION (object)));
			return;

		case PROP_USE_SENT_FOLDER:
			g_value_set_boolean (
				value,
				e_source_mail_submission_get_use_sent_folder (
				E_SOURCE_MAIL_SUBMISSION (object)));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
source_mail_submission_finalize (GObject *object)
{
	ESourceMailSubmissionPrivate *priv;

	priv = E_SOURCE_MAIL_SUBMISSION (object)->priv;

	g_free (priv->sent_folder);
	g_free (priv->transport_uid);

	/* Chain up to parent's finalize() method. */
	G_OBJECT_CLASS (e_source_mail_submission_parent_class)->
		finalize (object);
}

static void
e_source_mail_submission_class_init (ESourceMailSubmissionClass *class)
{
	GObjectClass *object_class;
	ESourceExtensionClass *extension_class;

	object_class = G_OBJECT_CLASS (class);
	object_class->set_property = source_mail_submission_set_property;
	object_class->get_property = source_mail_submission_get_property;
	object_class->finalize = source_mail_submission_finalize;

	extension_class = E_SOURCE_EXTENSION_CLASS (class);
	extension_class->name = E_SOURCE_EXTENSION_MAIL_SUBMISSION;

	g_object_class_install_property (
		object_class,
		PROP_SENT_FOLDER,
		g_param_spec_string (
			"sent-folder",
			"Sent Folder",
			"Preferred folder for sent messages",
			NULL,
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT |
			G_PARAM_EXPLICIT_NOTIFY |
			G_PARAM_STATIC_STRINGS |
			E_SOURCE_PARAM_SETTING));

	g_object_class_install_property (
		object_class,
		PROP_USE_SENT_FOLDER,
		g_param_spec_boolean (
			"use-sent-folder",
			"Use Sent Folder",
			"Whether to save sent messages to sent-folder",
			TRUE,
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT |
			G_PARAM_EXPLICIT_NOTIFY |
			G_PARAM_STATIC_STRINGS |
			E_SOURCE_PARAM_SETTING));

	g_object_class_install_property (
		object_class,
		PROP_TRANSPORT_UID,
		g_param_spec_string (
			"transport-uid",
			"Transport UID",
			"ESource UID of a Mail Transport",
			NULL,
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT |
			G_PARAM_EXPLICIT_NOTIFY |
			G_PARAM_STATIC_STRINGS |
			E_SOURCE_PARAM_SETTING));

	g_object_class_install_property (
		object_class,
		PROP_REPLIES_TO_ORIGIN_FOLDER,
		g_param_spec_boolean (
			"replies-to-origin-folder",
			"Replies to origin folder",
			"Whether to save replies to folder of the message "
			"being replied to, instead of the Sent folder",
			FALSE,
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT |
			G_PARAM_EXPLICIT_NOTIFY |
			G_PARAM_STATIC_STRINGS |
			E_SOURCE_PARAM_SETTING));
}

static void
e_source_mail_submission_init (ESourceMailSubmission *extension)
{
	extension->priv = e_source_mail_submission_get_instance_private (extension);
}

/**
 * e_source_mail_submission_get_sent_folder:
 * @extension: an #ESourceMailSubmission
 *
 * Returns a string identifying the preferred folder for sent messages.
 * The format of the identifier string is defined by the client application.
 *
 * Returns: (nullable): an identifier for the preferred sent folder
 *
 * Since: 3.6
 **/
const gchar *
e_source_mail_submission_get_sent_folder (ESourceMailSubmission *extension)
{
	g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), NULL);

	return extension->priv->sent_folder;
}

/**
 * e_source_mail_submission_dup_sent_folder:
 * @extension: an #ESourceMailSubmission
 *
 * Thread-safe variation of e_source_mail_submission_get_sent_folder().
 * Use this function when accessing @extension from multiple threads.
 *
 * The returned string should be freed with g_free() when no longer needed.
 *
 * Returns: (nullable): a newly-allocated copy of #ESourceMailSubmission:sent-folder
 *
 * Since: 3.6
 **/
gchar *
e_source_mail_submission_dup_sent_folder (ESourceMailSubmission *extension)
{
	const gchar *protected;
	gchar *duplicate;

	g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), NULL);

	e_source_extension_property_lock (E_SOURCE_EXTENSION (extension));

	protected = e_source_mail_submission_get_sent_folder (extension);
	duplicate = g_strdup (protected);

	e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));

	return duplicate;
}

/**
 * e_source_mail_submission_set_sent_folder:
 * @extension: an #ESourceMailSubmission
 * @sent_folder: (nullable): an identifier for the preferred sent folder,
 *               or %NULL
 *
 * Sets the preferred folder for sent messages by an identifier string.
 * The format of the identifier string is defined by the client application.
 *
 * The internal copy of @sent_folder is automatically stripped of leading
 * and trailing whitespace.  If the resulting string is empty, %NULL is set
 * instead.
 *
 * Since: 3.6
 **/
void
e_source_mail_submission_set_sent_folder (ESourceMailSubmission *extension,
                                          const gchar *sent_folder)
{
	g_return_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension));

	e_source_extension_property_lock (E_SOURCE_EXTENSION (extension));

	if (e_util_strcmp0 (extension->priv->sent_folder, sent_folder) == 0) {
		e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
		return;
	}

	g_free (extension->priv->sent_folder);
	extension->priv->sent_folder = e_util_strdup_strip (sent_folder);

	e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));

	g_object_notify (G_OBJECT (extension), "sent-folder");
}

/**
 * e_source_mail_submission_get_use_sent_folder:
 * @extension: an #ESourceMailSubmission
 *
 * Returns: whether save messages to the sent folder at all
 *
 * Since: 3.26
 **/
gboolean
e_source_mail_submission_get_use_sent_folder (ESourceMailSubmission *extension)
{
	g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), FALSE);

	return extension->priv->use_sent_folder;
}

/**
 * e_source_mail_submission_set_use_sent_folder:
 * @extension: an #ESourceMailSubmission
 * @use_sent_folder: the value to set
 *
 * Sets whether save messages to the sent folder at all.
 *
 * Since: 3.26
 **/
void
e_source_mail_submission_set_use_sent_folder (ESourceMailSubmission *extension,
					      gboolean use_sent_folder)
{
	g_return_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension));

	if ((extension->priv->use_sent_folder ? 1 : 0) == (use_sent_folder ? 1 : 0))
		return;

	extension->priv->use_sent_folder = use_sent_folder;

	g_object_notify (G_OBJECT (extension), "use-sent-folder");
}

/**
 * e_source_mail_submission_get_transport_uid:
 * @extension: an #ESourceMailSubmission
 *
 * Returns the #ESource:uid of the #ESource that describes the mail
 * transport to be used for outgoing messages.
 *
 * Returns: (nullable): the mail transport #ESource:uid
 *
 * Since: 3.6
 **/
const gchar *
e_source_mail_submission_get_transport_uid (ESourceMailSubmission *extension)
{
	g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), NULL);

	return extension->priv->transport_uid;
}

/**
 * e_source_mail_submission_dup_transport_uid:
 * @extension: an #ESourceMailSubmission
 *
 * Thread-safe variation of e_source_mail_submission_get_transport_uid().
 * Use this function when accessing @extension from multiple threads.
 *
 * The returned string should be freed with g_free() when no longer needed.
 *
 * Returns: (nullable): a newly-allocated copy of #ESourceMailSubmission:transport-uid
 *
 * Since: 3.6
 **/
gchar *
e_source_mail_submission_dup_transport_uid (ESourceMailSubmission *extension)
{
	const gchar *protected;
	gchar *duplicate;

	g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), NULL);

	e_source_extension_property_lock (E_SOURCE_EXTENSION (extension));

	protected = e_source_mail_submission_get_transport_uid (extension);
	duplicate = g_strdup (protected);

	e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));

	return duplicate;
}

/**
 * e_source_mail_submission_set_transport_uid:
 * @extension: an #ESourceMailSubmission
 * @transport_uid: (nullable): the mail transport #ESource:uid, or %NULL
 *
 * Sets the #ESource:uid of the #ESource that describes the mail
 * transport to be used for outgoing messages.
 *
 * Since: 3.6
 **/
void
e_source_mail_submission_set_transport_uid (ESourceMailSubmission *extension,
                                            const gchar *transport_uid)
{
	g_return_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension));

	e_source_extension_property_lock (E_SOURCE_EXTENSION (extension));

	if (g_strcmp0 (extension->priv->transport_uid, transport_uid) == 0) {
		e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
		return;
	}

	g_free (extension->priv->transport_uid);
	extension->priv->transport_uid = g_strdup (transport_uid);

	e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));

	g_object_notify (G_OBJECT (extension), "transport-uid");
}

/**
 * e_source_mail_submission_get_replies_to_origin_folder:
 * @extension: an #ESourceMailSubmission
 *
 * Returns whether save replies in the folder of the message
 * being replied to, instead of the Sent folder.
 *
 * Returns: whether save replies in the folder of the message being replied to
 *
 * Since: 3.8
 **/
gboolean
e_source_mail_submission_get_replies_to_origin_folder (ESourceMailSubmission *extension)
{
	g_return_val_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension), FALSE);

	return extension->priv->replies_to_origin_folder;
}

/**
 * e_source_mail_submission_set_replies_to_origin_folder:
 * @extension: an #ESourceMailSubmission
 * @replies_to_origin_folder: new value
 *
 * Sets whether save replies in the folder of the message
 * being replied to, instead of the Sent folder.
 *
 * Since: 3.8
 **/
void
e_source_mail_submission_set_replies_to_origin_folder (ESourceMailSubmission *extension,
                                                       gboolean replies_to_origin_folder)
{
	g_return_if_fail (E_IS_SOURCE_MAIL_SUBMISSION (extension));

	if (extension->priv->replies_to_origin_folder == replies_to_origin_folder)
		return;

	extension->priv->replies_to_origin_folder = replies_to_origin_folder;

	g_object_notify (G_OBJECT (extension), "replies-to-origin-folder");
}