No issues found
Tool | Failure ID | Location | Function | Message | Data |
---|---|---|---|---|---|
clang-analyzer | no-output-found | e-mail-signature-combo-box.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None | |
clang-analyzer | no-output-found | e-mail-signature-combo-box.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None |
1 /*
2 * e-mail-signature-combo-box.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 #include "e-mail-signature-combo-box.h"
20
21 #include <config.h>
22 #include <glib/gi18n-lib.h>
23
24 #define E_MAIL_SIGNATURE_COMBO_BOX_GET_PRIVATE(obj) \
25 (G_TYPE_INSTANCE_GET_PRIVATE \
26 ((obj), E_TYPE_MAIL_SIGNATURE_COMBO_BOX, EMailSignatureComboBoxPrivate))
27
28 #define SOURCE_IS_MAIL_SIGNATURE(source) \
29 (e_source_has_extension ((source), E_SOURCE_EXTENSION_MAIL_SIGNATURE))
30
31 struct _EMailSignatureComboBoxPrivate {
32 ESourceRegistry *registry;
33 guint refresh_idle_id;
34 gchar *identity_uid;
35 };
36
37 enum {
38 PROP_0,
39 PROP_IDENTITY_UID,
40 PROP_REGISTRY
41 };
42
43 enum {
44 COLUMN_DISPLAY_NAME,
45 COLUMN_UID
46 };
47
48 G_DEFINE_TYPE (
49 EMailSignatureComboBox,
50 e_mail_signature_combo_box,
51 GTK_TYPE_COMBO_BOX)
52
53 static gboolean
54 mail_signature_combo_box_refresh_idle_cb (EMailSignatureComboBox *combo_box)
55 {
56 /* The refresh function will clear the idle ID. */
57 e_mail_signature_combo_box_refresh (combo_box);
58
59 return FALSE;
60 }
61
62 static void
63 mail_signature_combo_box_registry_changed (ESourceRegistry *registry,
64 ESource *source,
65 EMailSignatureComboBox *combo_box)
66 {
67 /* If the ESource in question has a "Mail Signature" extension,
68 * schedule a refresh of the tree model. Otherwise ignore it.
69 * We use an idle callback to limit how frequently we refresh
70 * the tree model, in case the registry is emitting lots of
71 * signals at once. */
72
73 if (!SOURCE_IS_MAIL_SIGNATURE (source))
74 return;
75
76 if (combo_box->priv->refresh_idle_id > 0)
77 return;
78
79 combo_box->priv->refresh_idle_id = g_idle_add (
80 (GSourceFunc) mail_signature_combo_box_refresh_idle_cb,
81 combo_box);
82 }
83
84 static gboolean
85 mail_signature_combo_box_identity_to_signature (GBinding *binding,
86 const GValue *source_value,
87 GValue *target_value,
88 gpointer user_data)
89 {
90 EMailSignatureComboBox *combo_box;
91 ESourceRegistry *registry;
92 GObject *source_object;
93 ESource *source;
94 ESourceMailIdentity *extension;
95 const gchar *identity_uid;
96 const gchar *signature_uid = "none";
97 const gchar *extension_name;
98
99 /* Source and target are the same object. */
100 source_object = g_binding_get_source (binding);
101 combo_box = E_MAIL_SIGNATURE_COMBO_BOX (source_object);
102 registry = e_mail_signature_combo_box_get_registry (combo_box);
103
104 identity_uid = g_value_get_string (source_value);
105 if (identity_uid == NULL)
106 return FALSE;
107
108 source = e_source_registry_ref_source (registry, identity_uid);
109 if (source == NULL)
110 return FALSE;
111
112 extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
113 if (!e_source_has_extension (source, extension_name)) {
114 g_object_unref (source);
115 return FALSE;
116 }
117
118 extension = e_source_get_extension (source, extension_name);
119 signature_uid = e_source_mail_identity_get_signature_uid (extension);
120 g_value_set_string (target_value, signature_uid);
121
122 g_object_unref (source);
123
124 return TRUE;
125 }
126
127 static void
128 mail_signature_combo_box_set_registry (EMailSignatureComboBox *combo_box,
129 ESourceRegistry *registry)
130 {
131 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
132 g_return_if_fail (combo_box->priv->registry == NULL);
133
134 combo_box->priv->registry = g_object_ref (registry);
135
136 g_signal_connect (
137 registry, "source-added",
138 G_CALLBACK (mail_signature_combo_box_registry_changed),
139 combo_box);
140
141 g_signal_connect (
142 registry, "source-changed",
143 G_CALLBACK (mail_signature_combo_box_registry_changed),
144 combo_box);
145
146 g_signal_connect (
147 registry, "source-removed",
148 G_CALLBACK (mail_signature_combo_box_registry_changed),
149 combo_box);
150 }
151
152 static void
153 mail_signature_combo_box_set_property (GObject *object,
154 guint property_id,
155 const GValue *value,
156 GParamSpec *pspec)
157 {
158 switch (property_id) {
159 case PROP_IDENTITY_UID:
160 e_mail_signature_combo_box_set_identity_uid (
161 E_MAIL_SIGNATURE_COMBO_BOX (object),
162 g_value_get_string (value));
163 return;
164
165 case PROP_REGISTRY:
166 mail_signature_combo_box_set_registry (
167 E_MAIL_SIGNATURE_COMBO_BOX (object),
168 g_value_get_object (value));
169 return;
170 }
171
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
173 }
174
175 static void
176 mail_signature_combo_box_get_property (GObject *object,
177 guint property_id,
178 GValue *value,
179 GParamSpec *pspec)
180 {
181 switch (property_id) {
182 case PROP_IDENTITY_UID:
183 g_value_set_string (
184 value,
185 e_mail_signature_combo_box_get_identity_uid (
186 E_MAIL_SIGNATURE_COMBO_BOX (object)));
187 return;
188
189 case PROP_REGISTRY:
190 g_value_set_object (
191 value,
192 e_mail_signature_combo_box_get_registry (
193 E_MAIL_SIGNATURE_COMBO_BOX (object)));
194 return;
195 }
196
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
198 }
199
200 static void
201 mail_signature_combo_box_dispose (GObject *object)
202 {
203 EMailSignatureComboBoxPrivate *priv;
204
205 priv = E_MAIL_SIGNATURE_COMBO_BOX_GET_PRIVATE (object);
206
207 if (priv->registry != NULL) {
208 g_signal_handlers_disconnect_matched (
209 priv->registry, G_SIGNAL_MATCH_DATA,
210 0, 0, NULL, NULL, object);
211 g_object_unref (priv->registry);
212 priv->registry = NULL;
213 }
214
215 if (priv->refresh_idle_id > 0) {
216 g_source_remove (priv->refresh_idle_id);
217 priv->refresh_idle_id = 0;
218 }
219
220 /* Chain up to parent's dispose() method. */
221 G_OBJECT_CLASS (e_mail_signature_combo_box_parent_class)->
222 dispose (object);
223 }
224
225 static void
226 mail_signature_combo_box_finalize (GObject *object)
227 {
228 EMailSignatureComboBoxPrivate *priv;
229
230 priv = E_MAIL_SIGNATURE_COMBO_BOX_GET_PRIVATE (object);
231
232 g_free (priv->identity_uid);
233
234 /* Chain up to parent's finalize() method. */
235 G_OBJECT_CLASS (e_mail_signature_combo_box_parent_class)->
236 finalize (object);
237 }
238
239 static void
240 mail_signature_combo_box_constructed (GObject *object)
241 {
242 GtkListStore *list_store;
243 GtkComboBox *combo_box;
244 GtkCellLayout *cell_layout;
245 GtkCellRenderer *cell_renderer;
246
247 /* Chain up to parent's constructed() method. */
248 G_OBJECT_CLASS (e_mail_signature_combo_box_parent_class)->
249 constructed (object);
250
251 combo_box = GTK_COMBO_BOX (object);
252 cell_layout = GTK_CELL_LAYOUT (object);
253
254 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
255 gtk_combo_box_set_model (combo_box, GTK_TREE_MODEL (list_store));
256 gtk_combo_box_set_id_column (combo_box, COLUMN_UID);
257 g_object_unref (list_store);
258
259 cell_renderer = gtk_cell_renderer_text_new ();
260 gtk_cell_layout_pack_start (cell_layout, cell_renderer, TRUE);
261 gtk_cell_layout_add_attribute (
262 cell_layout, cell_renderer, "text", COLUMN_DISPLAY_NAME);
263
264 g_object_bind_property_full (
265 combo_box, "identity-uid",
266 combo_box, "active-id",
267 G_BINDING_DEFAULT,
268 mail_signature_combo_box_identity_to_signature,
269 NULL,
270 NULL, (GDestroyNotify) NULL);
271
272 e_mail_signature_combo_box_refresh (
273 E_MAIL_SIGNATURE_COMBO_BOX (object));
274 }
275
276 static void
277 e_mail_signature_combo_box_class_init (EMailSignatureComboBoxClass *class)
278 {
279 GObjectClass *object_class;
280
281 g_type_class_add_private (
282 class, sizeof (EMailSignatureComboBoxPrivate));
283
284 object_class = G_OBJECT_CLASS (class);
285 object_class->set_property = mail_signature_combo_box_set_property;
286 object_class->get_property = mail_signature_combo_box_get_property;
287 object_class->dispose = mail_signature_combo_box_dispose;
288 object_class->finalize = mail_signature_combo_box_finalize;
289 object_class->constructed = mail_signature_combo_box_constructed;
290
291 g_object_class_install_property (
292 object_class,
293 PROP_IDENTITY_UID,
294 g_param_spec_string (
295 "identity-uid",
296 "Identity UID",
297 NULL,
298 NULL,
299 G_PARAM_READWRITE |
300 G_PARAM_STATIC_STRINGS));
301
302 g_object_class_install_property (
303 object_class,
304 PROP_REGISTRY,
305 g_param_spec_object (
306 "registry",
307 "Registry",
308 NULL,
309 E_TYPE_SOURCE_REGISTRY,
310 G_PARAM_READWRITE |
311 G_PARAM_CONSTRUCT_ONLY |
312 G_PARAM_STATIC_STRINGS));
313 }
314
315 static void
316 e_mail_signature_combo_box_init (EMailSignatureComboBox *combo_box)
317 {
318 combo_box->priv = E_MAIL_SIGNATURE_COMBO_BOX_GET_PRIVATE (combo_box);
319 }
320
321 GtkWidget *
322 e_mail_signature_combo_box_new (ESourceRegistry *registry)
323 {
324 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
325
326 return g_object_new (
327 E_TYPE_MAIL_SIGNATURE_COMBO_BOX,
328 "registry", registry, NULL);
329 }
330
331 void
332 e_mail_signature_combo_box_refresh (EMailSignatureComboBox *combo_box)
333 {
334 ESourceRegistry *registry;
335 GtkComboBox *gtk_combo_box;
336 GtkTreeModel *tree_model;
337 GtkTreeIter iter;
338 ESource *source;
339 GList *list, *link;
340 const gchar *extension_name;
341 const gchar *saved_uid;
342
343 g_return_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box));
344
345 if (combo_box->priv->refresh_idle_id > 0) {
346 g_source_remove (combo_box->priv->refresh_idle_id);
347 combo_box->priv->refresh_idle_id = 0;
348 }
349
350 gtk_combo_box = GTK_COMBO_BOX (combo_box);
351 tree_model = gtk_combo_box_get_model (gtk_combo_box);
352
353 /* This is an interned string, which means it's safe
354 * to use even after clearing the combo box model. */
355 saved_uid = gtk_combo_box_get_active_id (gtk_combo_box);
356
357 gtk_list_store_clear (GTK_LIST_STORE (tree_model));
358
359 extension_name = E_SOURCE_EXTENSION_MAIL_SIGNATURE;
360 registry = e_mail_signature_combo_box_get_registry (combo_box);
361 list = e_source_registry_list_sources (registry, extension_name);
362
363 /* The "None" option always comes first. */
364
365 gtk_list_store_append (GTK_LIST_STORE (tree_model), &iter);
366
367 gtk_list_store_set (
368 GTK_LIST_STORE (tree_model), &iter,
369 COLUMN_DISPLAY_NAME, _("None"),
370 COLUMN_UID, "none", -1);
371
372 /* The "autogenerated" UID has special meaning. */
373
374 gtk_list_store_append (GTK_LIST_STORE (tree_model), &iter);
375
376 gtk_list_store_set (
377 GTK_LIST_STORE (tree_model), &iter,
378 COLUMN_DISPLAY_NAME, _("Autogenerated"),
379 COLUMN_UID, E_MAIL_SIGNATURE_AUTOGENERATED_UID, -1);
380
381 /* Followed by the other mail signatures, alphabetized. */
382
383 for (link = list; link != NULL; link = g_list_next (link)) {
384 GtkTreeIter iter;
385 const gchar *display_name;
386 const gchar *uid;
387
388 source = E_SOURCE (link->data);
389 display_name = e_source_get_display_name (source);
390 uid = e_source_get_uid (source);
391
392 gtk_list_store_append (GTK_LIST_STORE (tree_model), &iter);
393
394 gtk_list_store_set (
395 GTK_LIST_STORE (tree_model), &iter,
396 COLUMN_DISPLAY_NAME, display_name,
397 COLUMN_UID, uid, -1);
398 }
399
400 g_list_free_full (list, (GDestroyNotify) g_object_unref);
401
402 /* Try and restore the previous selected source, or else "None". */
403
404 if (saved_uid != NULL)
405 gtk_combo_box_set_active_id (gtk_combo_box, saved_uid);
406
407 if (gtk_combo_box_get_active_id (gtk_combo_box) == NULL)
408 gtk_combo_box_set_active (gtk_combo_box, 0);
409 }
410
411 ESourceRegistry *
412 e_mail_signature_combo_box_get_registry (EMailSignatureComboBox *combo_box)
413 {
414 g_return_val_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box), NULL);
415
416 return combo_box->priv->registry;
417 }
418
419 const gchar *
420 e_mail_signature_combo_box_get_identity_uid (EMailSignatureComboBox *combo_box)
421 {
422 g_return_val_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box), NULL);
423
424 return combo_box->priv->identity_uid;
425 }
426
427 void
428 e_mail_signature_combo_box_set_identity_uid (EMailSignatureComboBox *combo_box,
429 const gchar *identity_uid)
430 {
431 const gchar *active_id;
432
433 g_return_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box));
434
435 if (g_strcmp0 (combo_box->priv->identity_uid, identity_uid) == 0)
436 return;
437
438 g_free (combo_box->priv->identity_uid);
439 combo_box->priv->identity_uid = g_strdup (identity_uid);
440
441 g_object_notify (G_OBJECT (combo_box), "identity-uid");
442
443 /* If "Autogenerated" is selected, emit a "changed" signal as
444 * a hint to whomever is listening to reload the signature. */
445 active_id = gtk_combo_box_get_active_id (GTK_COMBO_BOX (combo_box));
446 if (g_strcmp0 (active_id, E_MAIL_SIGNATURE_AUTOGENERATED_UID) == 0)
447 g_signal_emit_by_name (combo_box, "changed");
448 }
449
450 /**************** e_mail_signature_combo_box_load_selected() *****************/
451
452 typedef struct _LoadContext LoadContext;
453
454 struct _LoadContext {
455 gchar *contents;
456 gsize length;
457 gboolean is_html;
458 };
459
460 static void
461 load_context_free (LoadContext *context)
462 {
463 g_free (context->contents);
464 g_slice_free (LoadContext, context);
465 }
466
467 static void
468 mail_signature_combo_box_autogenerate (EMailSignatureComboBox *combo_box,
469 LoadContext *context)
470 {
471 ESourceMailIdentity *extension;
472 ESourceRegistry *registry;
473 ESource *source;
474 GString *buffer;
475 const gchar *extension_name;
476 const gchar *identity_uid;
477 const gchar *text;
478 gchar *escaped;
479
480 identity_uid = e_mail_signature_combo_box_get_identity_uid (combo_box);
481
482 /* If we have no mail identity UID, handle it as though
483 * "None" were selected. No need to report an error. */
484 if (identity_uid == NULL)
485 return;
486
487 registry = e_mail_signature_combo_box_get_registry (combo_box);
488 source = e_source_registry_ref_source (registry, identity_uid);
489
490 /* If the mail identity lookup fails, handle it as though
491 * "None" were selected. No need to report an error. */
492 if (source == NULL)
493 return;
494
495 /* If the source is not actually a mail identity, handle it as
496 * though "None" were selected. No need to report an error. */
497 extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
498 if (!e_source_has_extension (source, extension_name)) {
499 g_object_unref (source);
500 return;
501 }
502
503 extension = e_source_get_extension (source, extension_name);
504
505 /* The autogenerated signature format is:
506 *
507 * <NAME> <ADDRESS>
508 * <ORGANIZATION>
509 *
510 * The <ADDRESS> is a mailto link and
511 * the <ORGANIZATION> line is optional.
512 */
513
514 buffer = g_string_sized_new (512);
515
516 text = e_source_mail_identity_get_name (extension);
517 escaped = (text != NULL) ? g_markup_escape_text (text, -1) : NULL;
518 if (escaped != NULL && *escaped != '\0')
519 g_string_append (buffer, escaped);
520 g_free (escaped);
521
522 text = e_source_mail_identity_get_address (extension);
523 escaped = (text != NULL) ? g_markup_escape_text (text, -1) : NULL;
524 if (escaped != NULL && *escaped != '\0')
525 g_string_append_printf (
526 buffer, " <<a href=\"mailto:%s\">%s</a>>",
527 escaped, escaped);
528 g_free (escaped);
529
530 text = e_source_mail_identity_get_organization (extension);
531 escaped = (text != NULL) ? g_markup_escape_text (text, -1) : NULL;
532 if (escaped != NULL && *escaped != '\0')
533 g_string_append_printf (buffer, "<br>%s", escaped);
534 g_free (escaped);
535
536 context->length = buffer->len;
537 context->contents = g_string_free (buffer, FALSE);
538 context->is_html = TRUE;
539
540 g_object_unref (source);
541 }
542
543 static void
544 mail_signature_combo_box_load_cb (ESource *source,
545 GAsyncResult *result,
546 GSimpleAsyncResult *simple)
547 {
548 ESourceMailSignature *extension;
549 LoadContext *context;
550 const gchar *extension_name;
551 const gchar *mime_type;
552 GError *error = NULL;
553
554 context = g_simple_async_result_get_op_res_gpointer (simple);
555
556 e_source_mail_signature_load_finish (
557 source, result, &context->contents, &context->length, &error);
558
559 if (error != NULL) {
560 g_simple_async_result_set_from_error (simple, error);
561 g_simple_async_result_complete (simple);
562 g_object_unref (simple);
563 g_error_free (error);
564 return;
565 }
566
567 extension_name = E_SOURCE_EXTENSION_MAIL_SIGNATURE;
568 extension = e_source_get_extension (source, extension_name);
569 mime_type = e_source_mail_signature_get_mime_type (extension);
570 context->is_html = (g_strcmp0 (mime_type, "text/html") == 0);
571
572 g_simple_async_result_complete (simple);
573
574 g_object_unref (simple);
575 }
576
577 void
578 e_mail_signature_combo_box_load_selected (EMailSignatureComboBox *combo_box,
579 gint io_priority,
580 GCancellable *cancellable,
581 GAsyncReadyCallback callback,
582 gpointer user_data)
583 {
584 GSimpleAsyncResult *simple;
585 ESourceRegistry *registry;
586 LoadContext *context;
587 ESource *source;
588 const gchar *active_id;
589
590 g_return_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box));
591
592 context = g_slice_new0 (LoadContext);
593
594 simple = g_simple_async_result_new (
595 G_OBJECT (combo_box), callback, user_data,
596 e_mail_signature_combo_box_load_selected);
597
598 g_simple_async_result_set_op_res_gpointer (
599 simple, context, (GDestroyNotify) load_context_free);
600
601 active_id = gtk_combo_box_get_active_id (GTK_COMBO_BOX (combo_box));
602
603 if (active_id == NULL) {
604 g_simple_async_result_complete_in_idle (simple);
605 g_object_unref (simple);
606 return;
607 }
608
609 if (g_strcmp0 (active_id, E_MAIL_SIGNATURE_AUTOGENERATED_UID) == 0) {
610 mail_signature_combo_box_autogenerate (combo_box, context);
611 g_simple_async_result_complete_in_idle (simple);
612 g_object_unref (simple);
613 return;
614 }
615
616 registry = e_mail_signature_combo_box_get_registry (combo_box);
617 source = e_source_registry_ref_source (registry, active_id);
618
619 /* If for some reason the ESource lookup fails, handle it as
620 * though "None" were selected. No need to report an error. */
621 if (source == NULL) {
622 g_simple_async_result_complete_in_idle (simple);
623 g_object_unref (simple);
624 return;
625 }
626
627 e_source_mail_signature_load (
628 source, io_priority, cancellable, (GAsyncReadyCallback)
629 mail_signature_combo_box_load_cb, simple);
630
631 g_object_unref (source);
632 }
633
634 gboolean
635 e_mail_signature_combo_box_load_selected_finish (EMailSignatureComboBox *combo_box,
636 GAsyncResult *result,
637 gchar **contents,
638 gsize *length,
639 gboolean *is_html,
640 GError **error)
641 {
642 GSimpleAsyncResult *simple;
643 LoadContext *context;
644
645 g_return_val_if_fail (
646 g_simple_async_result_is_valid (
647 result, G_OBJECT (combo_box),
648 e_mail_signature_combo_box_load_selected), FALSE);
649
650 simple = G_SIMPLE_ASYNC_RESULT (result);
651 context = g_simple_async_result_get_op_res_gpointer (simple);
652
653 if (g_simple_async_result_propagate_error (simple, error))
654 return FALSE;
655
656 if (contents != NULL) {
657 *contents = context->contents;
658 context->contents = NULL;
659 }
660
661 if (length != NULL)
662 *length = context->length;
663
664 if (is_html != NULL)
665 *is_html = context->is_html;
666
667 return TRUE;
668 }