1 /*
2  * camel-sendmail-settings.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 Lesser 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 #include "camel-sendmail-settings.h"
19 
20 struct _CamelSendmailSettingsPrivate {
21 	GMutex property_lock;
22 	gchar *custom_binary;
23 	gchar *custom_args;
24 
25 	gboolean use_custom_binary;
26 	gboolean use_custom_args;
27 	gboolean send_in_offline;
28 };
29 
30 enum {
31 	PROP_0,
32 	PROP_USE_CUSTOM_BINARY,
33 	PROP_USE_CUSTOM_ARGS,
34 	PROP_CUSTOM_BINARY,
35 	PROP_CUSTOM_ARGS,
36 	PROP_SEND_IN_OFFLINE
37 };
38 
G_DEFINE_TYPE_WITH_PRIVATE(CamelSendmailSettings,camel_sendmail_settings,CAMEL_TYPE_SETTINGS)39 G_DEFINE_TYPE_WITH_PRIVATE (CamelSendmailSettings, camel_sendmail_settings, CAMEL_TYPE_SETTINGS)
40 
41 static void
42 sendmail_settings_set_property (GObject *object,
43                                 guint property_id,
44                                 const GValue *value,
45                                 GParamSpec *pspec)
46 {
47 	switch (property_id) {
48 		case PROP_USE_CUSTOM_BINARY:
49 			camel_sendmail_settings_set_use_custom_binary (
50 				CAMEL_SENDMAIL_SETTINGS (object),
51 				g_value_get_boolean (value));
52 			return;
53 
54 		case PROP_USE_CUSTOM_ARGS:
55 			camel_sendmail_settings_set_use_custom_args (
56 				CAMEL_SENDMAIL_SETTINGS (object),
57 				g_value_get_boolean (value));
58 			return;
59 
60 		case PROP_CUSTOM_BINARY:
61 			camel_sendmail_settings_set_custom_binary (
62 				CAMEL_SENDMAIL_SETTINGS (object),
63 				g_value_get_string (value));
64 			return;
65 
66 		case PROP_CUSTOM_ARGS:
67 			camel_sendmail_settings_set_custom_args (
68 				CAMEL_SENDMAIL_SETTINGS (object),
69 				g_value_get_string (value));
70 			return;
71 
72 		case PROP_SEND_IN_OFFLINE:
73 			camel_sendmail_settings_set_send_in_offline (
74 				CAMEL_SENDMAIL_SETTINGS (object),
75 				g_value_get_boolean (value));
76 			return;
77 	}
78 
79 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
80 }
81 
82 static void
sendmail_settings_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)83 sendmail_settings_get_property (GObject *object,
84                                 guint property_id,
85                                 GValue *value,
86                                 GParamSpec *pspec)
87 {
88 	switch (property_id) {
89 		case PROP_USE_CUSTOM_BINARY:
90 			g_value_set_boolean (
91 				value,
92 				camel_sendmail_settings_get_use_custom_binary (
93 				CAMEL_SENDMAIL_SETTINGS (object)));
94 			return;
95 
96 		case PROP_USE_CUSTOM_ARGS:
97 			g_value_set_boolean (
98 				value,
99 				camel_sendmail_settings_get_use_custom_args (
100 				CAMEL_SENDMAIL_SETTINGS (object)));
101 			return;
102 
103 		case PROP_CUSTOM_BINARY:
104 			g_value_take_string (
105 				value,
106 				camel_sendmail_settings_dup_custom_binary (
107 				CAMEL_SENDMAIL_SETTINGS (object)));
108 			return;
109 
110 		case PROP_CUSTOM_ARGS:
111 			g_value_take_string (
112 				value,
113 				camel_sendmail_settings_dup_custom_args (
114 				CAMEL_SENDMAIL_SETTINGS (object)));
115 			return;
116 
117 		case PROP_SEND_IN_OFFLINE:
118 			g_value_set_boolean (
119 				value,
120 				camel_sendmail_settings_get_send_in_offline (
121 				CAMEL_SENDMAIL_SETTINGS (object)));
122 			return;
123 	}
124 
125 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
126 }
127 
128 static void
sendmail_settings_finalize(GObject * object)129 sendmail_settings_finalize (GObject *object)
130 {
131 	CamelSendmailSettingsPrivate *priv;
132 
133 	priv = CAMEL_SENDMAIL_SETTINGS (object)->priv;
134 
135 	g_mutex_clear (&priv->property_lock);
136 
137 	g_free (priv->custom_binary);
138 	g_free (priv->custom_args);
139 
140 	/* Chain up to parent's finalize() method. */
141 	G_OBJECT_CLASS (camel_sendmail_settings_parent_class)->finalize (object);
142 }
143 
144 static void
camel_sendmail_settings_class_init(CamelSendmailSettingsClass * class)145 camel_sendmail_settings_class_init (CamelSendmailSettingsClass *class)
146 {
147 	GObjectClass *object_class;
148 
149 	object_class = G_OBJECT_CLASS (class);
150 	object_class->set_property = sendmail_settings_set_property;
151 	object_class->get_property = sendmail_settings_get_property;
152 	object_class->finalize = sendmail_settings_finalize;
153 
154 	g_object_class_install_property (
155 		object_class,
156 		PROP_USE_CUSTOM_BINARY,
157 		g_param_spec_boolean (
158 			"use-custom-binary",
159 			"Use Custom Binary",
160 			"Whether the custom-binary property identifies binary to run",
161 			FALSE,
162 			G_PARAM_READWRITE |
163 			G_PARAM_CONSTRUCT |
164 			G_PARAM_EXPLICIT_NOTIFY |
165 			G_PARAM_STATIC_STRINGS));
166 
167 	g_object_class_install_property (
168 		object_class,
169 		PROP_USE_CUSTOM_ARGS,
170 		g_param_spec_boolean (
171 			"use-custom-args",
172 			"Use Custom Arguments",
173 			"Whether the custom-args property identifies arguments to use",
174 			FALSE,
175 			G_PARAM_READWRITE |
176 			G_PARAM_CONSTRUCT |
177 			G_PARAM_EXPLICIT_NOTIFY |
178 			G_PARAM_STATIC_STRINGS));
179 
180 	g_object_class_install_property (
181 		object_class,
182 		PROP_CUSTOM_BINARY,
183 		g_param_spec_string (
184 			"custom-binary",
185 			"Custom Binary",
186 			"Custom binary to run, instead of sendmail",
187 			NULL,
188 			G_PARAM_READWRITE |
189 			G_PARAM_CONSTRUCT |
190 			G_PARAM_EXPLICIT_NOTIFY |
191 			G_PARAM_STATIC_STRINGS));
192 
193 	g_object_class_install_property (
194 		object_class,
195 		PROP_CUSTOM_ARGS,
196 		g_param_spec_string (
197 			"custom-args",
198 			"Custom Arguments",
199 			"Custom arguments to use, instead of default (predefined) arguments",
200 			NULL,
201 			G_PARAM_READWRITE |
202 			G_PARAM_CONSTRUCT |
203 			G_PARAM_EXPLICIT_NOTIFY |
204 			G_PARAM_STATIC_STRINGS));
205 
206 	g_object_class_install_property (
207 		object_class,
208 		PROP_SEND_IN_OFFLINE,
209 		g_param_spec_boolean (
210 			"send-in-offline",
211 			"Send in offline",
212 			"Whether to allow message sending in offline mode",
213 			TRUE,
214 			G_PARAM_READWRITE |
215 			G_PARAM_CONSTRUCT |
216 			G_PARAM_EXPLICIT_NOTIFY |
217 			G_PARAM_STATIC_STRINGS));
218 }
219 
220 static void
camel_sendmail_settings_init(CamelSendmailSettings * settings)221 camel_sendmail_settings_init (CamelSendmailSettings *settings)
222 {
223 	settings->priv = camel_sendmail_settings_get_instance_private (settings);
224 	g_mutex_init (&settings->priv->property_lock);
225 }
226 
227 /**
228  * camel_sendmail_settings_get_use_custom_binary:
229  * @settings: a #CamelSendmailSettings
230  *
231  * Returns whether the 'custom-binary' property should be used as binary to run, instead of sendmail.
232  *
233  * Returns: whether the 'custom-binary' property should be used as binary to run, instead of sendmail
234  *
235  * Since: 3.8
236  **/
237 gboolean
camel_sendmail_settings_get_use_custom_binary(CamelSendmailSettings * settings)238 camel_sendmail_settings_get_use_custom_binary (CamelSendmailSettings *settings)
239 {
240 	g_return_val_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings), FALSE);
241 
242 	return settings->priv->use_custom_binary;
243 }
244 
245 /**
246  * camel_sendmail_settings_set_use_custom_binary:
247  * @settings: a #CamelSendmailSettings
248  * @use_custom_binary: whether to use custom binary
249  *
250  * Sets whether to use custom binary, instead of sendmail.
251  *
252  * Since: 3.8
253  **/
254 void
camel_sendmail_settings_set_use_custom_binary(CamelSendmailSettings * settings,gboolean use_custom_binary)255 camel_sendmail_settings_set_use_custom_binary (CamelSendmailSettings *settings,
256                                                gboolean use_custom_binary)
257 {
258 	g_return_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings));
259 
260 	if (settings->priv->use_custom_binary == use_custom_binary)
261 		return;
262 
263 	settings->priv->use_custom_binary = use_custom_binary;
264 
265 	g_object_notify (G_OBJECT (settings), "use-custom-binary");
266 }
267 
268 /**
269  * camel_sendmail_settings_get_use_custom_args:
270  * @settings: a #CamelSendmailSettings
271  *
272  * Returns whether the 'custom-args' property should be used as arguments to use, instead of default arguments.
273  *
274  * Returns: whether the 'custom-args' property should be used as arguments to use, instead of default arguments
275  *
276  * Since: 3.8
277  **/
278 gboolean
camel_sendmail_settings_get_use_custom_args(CamelSendmailSettings * settings)279 camel_sendmail_settings_get_use_custom_args (CamelSendmailSettings *settings)
280 {
281 	g_return_val_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings), FALSE);
282 
283 	return settings->priv->use_custom_args;
284 }
285 
286 /**
287  * camel_sendmail_settings_set_use_custom_args:
288  * @settings: a #CamelSendmailSettings
289  * @use_custom_args: whether to use custom arguments
290  *
291  * Sets whether to use custom arguments, instead of default arguments.
292  *
293  * Since: 3.8
294  **/
295 void
camel_sendmail_settings_set_use_custom_args(CamelSendmailSettings * settings,gboolean use_custom_args)296 camel_sendmail_settings_set_use_custom_args (CamelSendmailSettings *settings,
297                                              gboolean use_custom_args)
298 {
299 	g_return_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings));
300 
301 	if (settings->priv->use_custom_args == use_custom_args)
302 		return;
303 
304 	settings->priv->use_custom_args = use_custom_args;
305 
306 	g_object_notify (G_OBJECT (settings), "use-custom-args");
307 }
308 
309 /**
310  * camel_sendmail_settings_get_custom_binary:
311  * @settings: a #CamelSendmailSettings
312  *
313  * Returns the custom binary to run, instead of sendmail.
314  *
315  * Returns: the custom binary to run, instead of sendmail, or %NULL
316  *
317  * Since: 3.8
318  **/
319 const gchar *
camel_sendmail_settings_get_custom_binary(CamelSendmailSettings * settings)320 camel_sendmail_settings_get_custom_binary (CamelSendmailSettings *settings)
321 {
322 	g_return_val_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings), NULL);
323 
324 	return settings->priv->custom_binary;
325 }
326 
327 /**
328  * camel_sendmail_settings_dup_custom_binary:
329  * @settings: a #CamelSendmailSettings
330  *
331  * Thread-safe variation of camel_sendmail_settings_get_custom_binary().
332  * Use this function when accessing @settings from multiple threads.
333  *
334  * The returned string should be freed with g_free() when no longer needed.
335  *
336  * Returns: a newly-allocated copy of #CamelSendmailSettings:custom-binary
337  *
338  * Since: 3.8
339  **/
340 gchar *
camel_sendmail_settings_dup_custom_binary(CamelSendmailSettings * settings)341 camel_sendmail_settings_dup_custom_binary (CamelSendmailSettings *settings)
342 {
343 	const gchar *protected;
344 	gchar *duplicate;
345 
346 	g_return_val_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings), NULL);
347 
348 	g_mutex_lock (&settings->priv->property_lock);
349 
350 	protected = camel_sendmail_settings_get_custom_binary (settings);
351 	duplicate = g_strdup (protected);
352 
353 	g_mutex_unlock (&settings->priv->property_lock);
354 
355 	return duplicate;
356 }
357 
358 /**
359  * camel_sendmail_settings_set_custom_binary:
360  * @settings: a #CamelSendmailSettings
361  * @custom_binary: a custom binary name, or %NULL
362  *
363  * Sets the custom binary name to run, instead of sendmail.
364  *
365  * Since: 3.8
366  **/
367 void
camel_sendmail_settings_set_custom_binary(CamelSendmailSettings * settings,const gchar * custom_binary)368 camel_sendmail_settings_set_custom_binary (CamelSendmailSettings *settings,
369                                            const gchar *custom_binary)
370 {
371 	g_return_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings));
372 
373 	/* The default namespace is an empty string. */
374 	if (custom_binary && !*custom_binary)
375 		custom_binary = NULL;
376 
377 	g_mutex_lock (&settings->priv->property_lock);
378 
379 	if (g_strcmp0 (settings->priv->custom_binary, custom_binary) == 0) {
380 		g_mutex_unlock (&settings->priv->property_lock);
381 		return;
382 	}
383 
384 	g_free (settings->priv->custom_binary);
385 	settings->priv->custom_binary = g_strdup (custom_binary);
386 
387 	g_mutex_unlock (&settings->priv->property_lock);
388 
389 	g_object_notify (G_OBJECT (settings), "custom-binary");
390 }
391 
392 /**
393  * camel_sendmail_settings_get_custom_args:
394  * @settings: a #CamelSendmailSettings
395  *
396  * Returns the custom arguments to use, instead of default arguments.
397  *
398  * Returns: the custom arguments to use, instead of default arguments, or %NULL
399  *
400  * Since: 3.8
401  **/
402 const gchar *
camel_sendmail_settings_get_custom_args(CamelSendmailSettings * settings)403 camel_sendmail_settings_get_custom_args (CamelSendmailSettings *settings)
404 {
405 	g_return_val_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings), NULL);
406 
407 	return settings->priv->custom_args;
408 }
409 
410 /**
411  * camel_sendmail_settings_dup_custom_args:
412  * @settings: a #CamelSendmailSettings
413  *
414  * Thread-safe variation of camel_sendmail_settings_get_custom_args().
415  * Use this function when accessing @settings from multiple threads.
416  *
417  * The returned string should be freed with g_free() when no longer needed.
418  *
419  * Returns: a newly-allocated copy of #CamelSendmailSettings:custom-args
420  *
421  * Since: 3.8
422  **/
423 gchar *
camel_sendmail_settings_dup_custom_args(CamelSendmailSettings * settings)424 camel_sendmail_settings_dup_custom_args (CamelSendmailSettings *settings)
425 {
426 	const gchar *protected;
427 	gchar *duplicate;
428 
429 	g_return_val_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings), NULL);
430 
431 	g_mutex_lock (&settings->priv->property_lock);
432 
433 	protected = camel_sendmail_settings_get_custom_args (settings);
434 	duplicate = g_strdup (protected);
435 
436 	g_mutex_unlock (&settings->priv->property_lock);
437 
438 	return duplicate;
439 }
440 
441 /**
442  * camel_sendmail_settings_set_custom_args:
443  * @settings: a #CamelSendmailSettings
444  * @custom_args: a custom arguments, or %NULL
445  *
446  * Sets the custom arguments to use, instead of default arguments.
447  *
448  * Since: 3.8
449  **/
450 void
camel_sendmail_settings_set_custom_args(CamelSendmailSettings * settings,const gchar * custom_args)451 camel_sendmail_settings_set_custom_args (CamelSendmailSettings *settings,
452                                         const gchar *custom_args)
453 {
454 	g_return_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings));
455 
456 	/* The default namespace is an empty string. */
457 	if (custom_args && !*custom_args)
458 		custom_args = NULL;
459 
460 	g_mutex_lock (&settings->priv->property_lock);
461 
462 	if (g_strcmp0 (settings->priv->custom_args, custom_args) == 0) {
463 		g_mutex_unlock (&settings->priv->property_lock);
464 		return;
465 	}
466 
467 	g_free (settings->priv->custom_args);
468 	settings->priv->custom_args = g_strdup (custom_args);
469 
470 	g_mutex_unlock (&settings->priv->property_lock);
471 
472 	g_object_notify (G_OBJECT (settings), "custom-args");
473 }
474 
475 /**
476  * camel_sendmail_settings_get_send_in_offline:
477  * @settings: a #CamelSendmailSettings
478  *
479  * Returns whether can send messages in offline mode.
480  *
481  * Returns: whether can send messages in offline mode
482  *
483  * Since: 3.10
484  **/
485 gboolean
camel_sendmail_settings_get_send_in_offline(CamelSendmailSettings * settings)486 camel_sendmail_settings_get_send_in_offline (CamelSendmailSettings *settings)
487 {
488 	g_return_val_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings), FALSE);
489 
490 	return settings->priv->send_in_offline;
491 }
492 
493 /**
494  * camel_sendmail_settings_set_send_in_offline:
495  * @settings: a #CamelSendmailSettings
496  * @send_in_offline: whether can send messages in offline mode
497  *
498  * Sets whether can send messages in offline mode.
499  *
500  * Since: 3.10
501  **/
502 void
camel_sendmail_settings_set_send_in_offline(CamelSendmailSettings * settings,gboolean send_in_offline)503 camel_sendmail_settings_set_send_in_offline (CamelSendmailSettings *settings,
504                                              gboolean send_in_offline)
505 {
506 	g_return_if_fail (CAMEL_IS_SENDMAIL_SETTINGS (settings));
507 
508 	if ((settings->priv->send_in_offline ? 1 : 0) == (send_in_offline ? 1 : 0))
509 		return;
510 
511 	settings->priv->send_in_offline = send_in_offline;
512 
513 	g_object_notify (G_OBJECT (settings), "send-in-offline");
514 }
515