No issues found
Tool | Failure ID | Location | Function | Message | Data |
---|---|---|---|---|---|
clang-analyzer | no-output-found | e-mail-parser-vcard-inline.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None | |
clang-analyzer | no-output-found | e-mail-parser-vcard-inline.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None |
1 /*
2 * e-mail-parser-vcard-inline.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 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <gtk/gtk.h>
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <glib/gstdio.h>
27
28 #include "e-mail-parser-vcard-inline.h"
29 #include "e-mail-part-vcard-inline.h"
30
31 #include <camel/camel.h>
32
33 #include <em-format/e-mail-extension-registry.h>
34 #include <em-format/e-mail-parser-extension.h>
35 #include <em-format/e-mail-part.h>
36 #include <em-format/e-mail-part-utils.h>
37 #include <em-format/e-mail-formatter-utils.h>
38
39 #include <libebook/libebook.h>
40 #include <libedataserver/libedataserver.h>
41 #include <libedataserverui/libedataserverui.h>
42
43 #include <shell/e-shell.h>
44 #include <addressbook/gui/merging/eab-contact-merging.h>
45 #include <addressbook/util/eab-book-util.h>
46
47 #include <libebackend/libebackend.h>
48
49 #define d(x)
50
51 typedef struct _EMailParserVCardInline {
52 EExtension parent;
53 } EMailParserVCardInline;
54
55 typedef struct _EMailParserVCardInlineClass {
56 EExtensionClass parent_class;
57 } EMailParserVCardInlineClass;
58
59 GType e_mail_parser_vcard_inline_get_type (void);
60 static void e_mail_parser_mail_extension_interface_init (EMailExtensionInterface *iface);
61 static void e_mail_parser_parser_extension_interface_init (EMailParserExtensionInterface *iface);
62
63 G_DEFINE_DYNAMIC_TYPE_EXTENDED (
64 EMailParserVCardInline,
65 e_mail_parser_vcard_inline,
66 E_TYPE_EXTENSION,
67 0,
68 G_IMPLEMENT_INTERFACE_DYNAMIC (
69 E_TYPE_MAIL_EXTENSION,
70 e_mail_parser_mail_extension_interface_init)
71 G_IMPLEMENT_INTERFACE_DYNAMIC (
72 E_TYPE_MAIL_PARSER_EXTENSION,
73 e_mail_parser_parser_extension_interface_init));
74
75 static const gchar * parser_mime_types[] = { "text/vcard", "text/x-vcard",
76 "text/directory", NULL };
77
78 static void
79 mail_part_vcard_inline_free (EMailPart *mail_part)
80 {
81 EMailPartVCardInline *vi_part = (EMailPartVCardInline *) mail_part;
82
83 g_clear_object (&vi_part->contact_display);
84 g_clear_object (&vi_part->message_label);
85 g_clear_object (&vi_part->formatter);
86 g_clear_object (&vi_part->iframe);
87 g_clear_object (&vi_part->save_button);
88 g_clear_object (&vi_part->toggle_button);
89 g_clear_object (&vi_part->folder);
90
91 if (vi_part->message_uid) {
92 g_free (vi_part->message_uid);
93 vi_part->message_uid = NULL;
94 }
95 }
96
97 static void
98 client_loaded_cb (ESource *source,
99 GAsyncResult *result,
100 GSList *contact_list)
101 {
102 EShell *shell;
103 EClient *client = NULL;
104 EBookClient *book_client;
105 ESourceRegistry *registry;
106 GSList *iter;
107 GError *error = NULL;
108
109 e_client_utils_open_new_finish (source, result, &client, &error);
110
111 if (error != NULL) {
112 g_warn_if_fail (client == NULL);
113 g_warning (
114 "%s: Failed to open book client: %s",
115 G_STRFUNC, error->message);
116 g_error_free (error);
117 goto exit;
118 }
119
120 g_return_if_fail (E_IS_BOOK_CLIENT (client));
121
122 book_client = E_BOOK_CLIENT (client);
123
124 shell = e_shell_get_default ();
125 registry = e_shell_get_registry (shell);
126
127 for (iter = contact_list; iter != NULL; iter = iter->next) {
128 EContact *contact;
129
130 contact = E_CONTACT (iter->data);
131 eab_merging_book_add_contact (
132 registry, book_client, contact, NULL, NULL);
133 }
134
135 g_object_unref (client);
136
137 exit:
138 e_client_util_free_object_slist (contact_list);
139 }
140
141 static void
142 save_vcard_cb (WebKitDOMEventTarget *button,
143 WebKitDOMEvent *event,
144 EMailPartVCardInline *vcard_part)
145 {
146 EShell *shell;
147 ESource *source;
148 ESourceRegistry *registry;
149 ESourceSelector *selector;
150 GSList *contact_list;
151 const gchar *extension_name;
152 GtkWidget *dialog;
153
154 shell = e_shell_get_default ();
155 registry = e_shell_get_registry (shell);
156 extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
157
158 dialog = e_source_selector_dialog_new (NULL, registry, extension_name);
159
160 selector = e_source_selector_dialog_get_selector (
161 E_SOURCE_SELECTOR_DIALOG (dialog));
162
163 source = e_source_registry_ref_default_address_book (registry);
164 e_source_selector_set_primary_selection (selector, source);
165 g_object_unref (source);
166
167 if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK) {
168 gtk_widget_destroy (dialog);
169 return;
170 }
171
172 source = e_source_selector_dialog_peek_primary_selection (
173 E_SOURCE_SELECTOR_DIALOG (dialog));
174
175 gtk_widget_destroy (dialog);
176
177 g_return_if_fail (source != NULL);
178
179 contact_list = e_client_util_copy_object_slist (NULL, vcard_part->contact_list);
180
181 e_client_utils_open_new (
182 source, E_CLIENT_SOURCE_TYPE_CONTACTS,
183 FALSE, NULL, (GAsyncReadyCallback) client_loaded_cb,
184 contact_list);
185 }
186
187 static void
188 display_mode_toggle_cb (WebKitDOMEventTarget *button,
189 WebKitDOMEvent *event,
190 EMailPartVCardInline *vcard_part)
191 {
192 EABContactDisplayMode mode;
193 gchar *uri;
194 gchar *html_label, *access_key;
195
196 mode = eab_contact_formatter_get_display_mode (vcard_part->formatter);
197 if (mode == EAB_CONTACT_DISPLAY_RENDER_NORMAL) {
198 mode = EAB_CONTACT_DISPLAY_RENDER_COMPACT;
199
200 html_label = e_mail_formatter_parse_html_mnemonics (
201 _("Show F_ull vCard"), &access_key);
202
203 webkit_dom_html_element_set_inner_html (
204 WEBKIT_DOM_HTML_ELEMENT (button),
205 html_label, NULL);
206 if (access_key) {
207 webkit_dom_html_element_set_access_key (
208 WEBKIT_DOM_HTML_ELEMENT (button),
209 access_key);
210 g_free (access_key);
211 }
212
213 g_free (html_label);
214
215 } else {
216 mode = EAB_CONTACT_DISPLAY_RENDER_NORMAL;
217
218 html_label = e_mail_formatter_parse_html_mnemonics (
219 _("Show Com_pact vCard"), &access_key);
220
221 webkit_dom_html_element_set_inner_html (
222 WEBKIT_DOM_HTML_ELEMENT (button),
223 html_label, NULL);
224 if (access_key) {
225 webkit_dom_html_element_set_access_key (
226 WEBKIT_DOM_HTML_ELEMENT (button),
227 access_key);
228 g_free (access_key);
229 }
230
231 g_free (html_label);
232 }
233
234 eab_contact_formatter_set_display_mode (vcard_part->formatter, mode);
235
236 uri = e_mail_part_build_uri (
237 vcard_part->folder, vcard_part->message_uid,
238 "part_id", G_TYPE_STRING, vcard_part->parent.id,
239 "mode", G_TYPE_INT, E_MAIL_FORMATTER_MODE_RAW, NULL);
240
241 webkit_dom_html_iframe_element_set_src (
242 WEBKIT_DOM_HTML_IFRAME_ELEMENT (vcard_part->iframe), uri);
243
244 g_free (uri);
245 }
246
247 static void
248 bind_dom (EMailPartVCardInline *vcard_part,
249 WebKitDOMElement *attachment)
250 {
251 WebKitDOMNodeList *list;
252 WebKitDOMElement *iframe, *toggle_button, *save_button;
253
254 /* IFRAME */
255 list = webkit_dom_element_get_elements_by_tag_name (attachment, "iframe");
256 if (webkit_dom_node_list_get_length (list) != 1)
257 return;
258 iframe = WEBKIT_DOM_ELEMENT (webkit_dom_node_list_item (list, 0));
259 if (vcard_part->iframe)
260 g_object_unref (vcard_part->iframe);
261 vcard_part->iframe = g_object_ref (iframe);
262
263 /* TOGGLE DISPLAY MODE BUTTON */
264 list = webkit_dom_element_get_elements_by_class_name (
265 attachment, "org-gnome-vcard-inline-display-mode-button");
266 if (webkit_dom_node_list_get_length (list) != 1)
267 return;
268 toggle_button = WEBKIT_DOM_ELEMENT (webkit_dom_node_list_item (list, 0));
269 if (vcard_part->toggle_button)
270 g_object_unref (vcard_part->toggle_button);
271 vcard_part->toggle_button = g_object_ref (toggle_button);
272
273 /* SAVE TO ADDRESSBOOK BUTTON */
274 list = webkit_dom_element_get_elements_by_class_name (
275 attachment, "org-gnome-vcard-inline-save-button");
276 if (webkit_dom_node_list_get_length (list) != 1)
277 return;
278 save_button = WEBKIT_DOM_ELEMENT (webkit_dom_node_list_item (list, 0));
279 if (vcard_part->save_button)
280 g_object_unref (vcard_part->save_button);
281 vcard_part->save_button = g_object_ref (save_button);
282
283 webkit_dom_event_target_add_event_listener (
284 WEBKIT_DOM_EVENT_TARGET (toggle_button),
285 "click", G_CALLBACK (display_mode_toggle_cb),
286 FALSE, vcard_part);
287
288 webkit_dom_event_target_add_event_listener (
289 WEBKIT_DOM_EVENT_TARGET (save_button),
290 "click", G_CALLBACK (save_vcard_cb),
291 FALSE, vcard_part);
292
293 /* Bind collapse buttons for contact lists. */
294 eab_contact_formatter_bind_dom (
295 webkit_dom_html_iframe_element_get_content_document (
296 WEBKIT_DOM_HTML_IFRAME_ELEMENT (iframe)));
297 }
298
299 static void
300 decode_vcard (EMailPartVCardInline *vcard_part,
301 CamelMimePart *mime_part)
302 {
303 CamelDataWrapper *data_wrapper;
304 CamelMedium *medium;
305 CamelStream *stream;
306 GSList *contact_list;
307 GByteArray *array;
308 const gchar *string;
309 const guint8 padding[2] = {0};
310
311 array = g_byte_array_new ();
312 medium = CAMEL_MEDIUM (mime_part);
313
314 /* Stream takes ownership of the byte array. */
315 stream = camel_stream_mem_new_with_byte_array (array);
316 data_wrapper = camel_medium_get_content (medium);
317 camel_data_wrapper_decode_to_stream_sync (
318 data_wrapper, stream, NULL, NULL);
319
320 /* because the result is not NULL-terminated */
321 g_byte_array_append (array, padding, 2);
322
323 string = (gchar *) array->data;
324 contact_list = eab_contact_list_from_string (string);
325 vcard_part->contact_list = contact_list;
326
327 g_object_unref (mime_part);
328 g_object_unref (stream);
329 }
330
331 static GSList *
332 empe_vcard_inline_parse (EMailParserExtension *extension,
333 EMailParser *parser,
334 CamelMimePart *part,
335 GString *part_id,
336 GCancellable *cancellable)
337 {
338 EMailPartVCardInline *vcard_part;
339 gint len;
340
341 len = part_id->len;
342 g_string_append (part_id, ".org-gnome-vcard-inline-display");
343
344 vcard_part = (EMailPartVCardInline *) e_mail_part_subclass_new (
345 part, part_id->str, sizeof (EMailPartVCardInline),
346 (GFreeFunc) mail_part_vcard_inline_free);
347 vcard_part->parent.mime_type = camel_content_type_simple (
348 camel_mime_part_get_content_type (part));
349 vcard_part->parent.bind_func = (EMailPartDOMBindFunc) bind_dom;
350 vcard_part->parent.is_attachment = TRUE;
351 vcard_part->formatter = g_object_new (
352 EAB_TYPE_CONTACT_FORMATTER,
353 "display-mode", EAB_CONTACT_DISPLAY_RENDER_COMPACT,
354 "render-maps", FALSE, NULL);
355 g_object_ref (part);
356
357 decode_vcard (vcard_part, part);
358
359 g_string_truncate (part_id, len);
360
361 return e_mail_parser_wrap_as_attachment (
362 parser, part, g_slist_append (NULL, vcard_part),
363 part_id, cancellable);
364 }
365
366 static guint32
367 empe_vcard_inline_get_flags (EMailParserExtension *extension)
368 {
369 return E_MAIL_PARSER_EXTENSION_INLINE_DISPOSITION;
370 }
371
372 static const gchar **
373 empe_mime_types (EMailExtension *extension)
374 {
375 return parser_mime_types;
376 }
377
378 void
379 e_mail_parser_vcard_inline_type_register (GTypeModule *type_module)
380 {
381 e_mail_parser_vcard_inline_register_type (type_module);
382 }
383
384 static void
385 e_mail_parser_mail_extension_interface_init (EMailExtensionInterface *iface)
386 {
387 iface->mime_types = empe_mime_types;
388 }
389
390 static void
391 e_mail_parser_parser_extension_interface_init (EMailParserExtensionInterface *iface)
392 {
393 iface->parse = empe_vcard_inline_parse;
394 iface->get_flags = empe_vcard_inline_get_flags;
395 }
396
397 static void
398 e_mail_parser_vcard_inline_constructed (GObject *object)
399 {
400 EExtensible *extensible;
401 EMailExtensionRegistry *reg;
402
403 extensible = e_extension_get_extensible (E_EXTENSION (object));
404 reg = E_MAIL_EXTENSION_REGISTRY (extensible);
405
406 e_mail_extension_registry_add_extension (reg, E_MAIL_EXTENSION (object));
407 }
408
409 static void
410 e_mail_parser_vcard_inline_class_init (EMailParserVCardInlineClass *class)
411 {
412 GObjectClass *object_class;
413 EExtensionClass *extension_class;
414
415 object_class = G_OBJECT_CLASS (class);
416 object_class->constructed = e_mail_parser_vcard_inline_constructed;
417
418 extension_class = E_EXTENSION_CLASS (class);
419 extension_class->extensible_type = E_TYPE_MAIL_PARSER_EXTENSION_REGISTRY;
420 }
421
422 static void
423 e_mail_parser_vcard_inline_class_finalize (EMailParserVCardInlineClass *class)
424 {
425
426 }
427
428 static void
429 e_mail_parser_vcard_inline_init (EMailParserVCardInline *self)
430 {
431 }