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
|
/*
* e-mail-part-vcard.c
*
* This program 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; either
* version 2 of the License, or (at your option) version 3.
*
* This program 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 the program; if not, see <http://www.gnu.org/licenses/>
*
*/
#include "e-mail-part-vcard.h"
#include <config.h>
#include <glib/gi18n.h>
#include <em-format/e-mail-formatter-utils.h>
#include <em-format/e-mail-part-utils.h>
#include <shell/e-shell.h>
#include <addressbook/gui/merging/eab-contact-merging.h>
#define E_MAIL_PART_VCARD_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_MAIL_PART_VCARD, EMailPartVCardPrivate))
struct _EMailPartVCardPrivate {
gint placeholder;
};
G_DEFINE_DYNAMIC_TYPE (
EMailPartVCard,
e_mail_part_vcard,
E_TYPE_MAIL_PART)
static void
client_connect_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GSList *contact_list = user_data;
EShell *shell;
EClient *client;
EBookClient *book_client;
ESourceRegistry *registry;
GSList *iter;
GError *error = NULL;
client = e_book_client_connect_finish (result, &error);
/* Sanity check. */
g_return_if_fail (
((client != NULL) && (error == NULL)) ||
((client == NULL) && (error != NULL)));
if (error != NULL) {
g_warning ("%s: %s", G_STRFUNC, error->message);
g_error_free (error);
goto exit;
}
book_client = E_BOOK_CLIENT (client);
shell = e_shell_get_default ();
registry = e_shell_get_registry (shell);
for (iter = contact_list; iter != NULL; iter = iter->next) {
EContact *contact;
contact = E_CONTACT (iter->data);
eab_merging_book_add_contact (
registry, book_client, contact, NULL, NULL);
}
g_object_unref (client);
exit:
g_slist_free_full (contact_list, (GDestroyNotify) g_object_unref);
}
static void
save_vcard_cb (WebKitDOMEventTarget *button,
WebKitDOMEvent *event,
EMailPartVCard *vcard_part)
{
EShell *shell;
ESource *source;
ESourceRegistry *registry;
ESourceSelector *selector;
GSList *contact_list;
const gchar *extension_name;
GtkWidget *dialog;
shell = e_shell_get_default ();
registry = e_shell_get_registry (shell);
extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
dialog = e_source_selector_dialog_new (NULL, registry, extension_name);
selector = e_source_selector_dialog_get_selector (
E_SOURCE_SELECTOR_DIALOG (dialog));
source = e_source_registry_ref_default_address_book (registry);
e_source_selector_set_primary_selection (selector, source);
g_object_unref (source);
if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK) {
gtk_widget_destroy (dialog);
return;
}
source = e_source_selector_dialog_peek_primary_selection (
E_SOURCE_SELECTOR_DIALOG (dialog));
gtk_widget_destroy (dialog);
g_return_if_fail (source != NULL);
contact_list = g_slist_copy_deep (
vcard_part->contact_list,
(GCopyFunc) g_object_ref, NULL);
e_book_client_connect (
source, NULL, client_connect_cb, contact_list);
}
static void
display_mode_toggle_cb (WebKitDOMEventTarget *button,
WebKitDOMEvent *event,
EMailPartVCard *vcard_part)
{
EABContactDisplayMode mode;
gchar *uri;
gchar *html_label;
gchar *access_key;
const gchar *part_id;
part_id = e_mail_part_get_id (E_MAIL_PART (vcard_part));
mode = eab_contact_formatter_get_display_mode (vcard_part->formatter);
if (mode == EAB_CONTACT_DISPLAY_RENDER_NORMAL) {
mode = EAB_CONTACT_DISPLAY_RENDER_COMPACT;
html_label = e_mail_formatter_parse_html_mnemonics (
_("Show F_ull vCard"), &access_key);
webkit_dom_html_element_set_inner_html (
WEBKIT_DOM_HTML_ELEMENT (button),
html_label, NULL);
if (access_key) {
webkit_dom_html_element_set_access_key (
WEBKIT_DOM_HTML_ELEMENT (button),
access_key);
g_free (access_key);
}
g_free (html_label);
} else {
mode = EAB_CONTACT_DISPLAY_RENDER_NORMAL;
html_label = e_mail_formatter_parse_html_mnemonics (
_("Show Com_pact vCard"), &access_key);
webkit_dom_html_element_set_inner_html (
WEBKIT_DOM_HTML_ELEMENT (button),
html_label, NULL);
if (access_key) {
webkit_dom_html_element_set_access_key (
WEBKIT_DOM_HTML_ELEMENT (button),
access_key);
g_free (access_key);
}
g_free (html_label);
}
eab_contact_formatter_set_display_mode (vcard_part->formatter, mode);
uri = e_mail_part_build_uri (
vcard_part->folder, vcard_part->message_uid,
"part_id", G_TYPE_STRING, part_id,
"mode", G_TYPE_INT, E_MAIL_FORMATTER_MODE_RAW, NULL);
webkit_dom_html_iframe_element_set_src (
WEBKIT_DOM_HTML_IFRAME_ELEMENT (vcard_part->iframe), uri);
g_free (uri);
}
static void
mail_part_vcard_dispose (GObject *object)
{
EMailPartVCard *part = E_MAIL_PART_VCARD (object);
g_clear_object (&part->contact_display);
g_clear_object (&part->message_label);
g_clear_object (&part->formatter);
g_clear_object (&part->iframe);
g_clear_object (&part->save_button);
g_clear_object (&part->toggle_button);
g_clear_object (&part->folder);
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (e_mail_part_vcard_parent_class)->dispose (object);
}
static void
mail_part_vcard_finalize (GObject *object)
{
EMailPartVCard *part = E_MAIL_PART_VCARD (object);
g_free (part->message_uid);
/* Chain up to parent's finalize() method. */
G_OBJECT_CLASS (e_mail_part_vcard_parent_class)->finalize (object);
}
static void
mail_part_vcard_constructed (GObject *object)
{
EMailPart *part;
CamelMimePart *mime_part;
CamelContentType *content_type;
gchar *mime_type;
part = E_MAIL_PART (object);
/* Chain up to parent's constructed() method. */
G_OBJECT_CLASS (e_mail_part_vcard_parent_class)->constructed (object);
e_mail_part_set_is_attachment (part, TRUE);
mime_part = e_mail_part_ref_mime_part (part);
content_type = camel_mime_part_get_content_type (mime_part);
mime_type = camel_content_type_simple (content_type);
e_mail_part_set_mime_type (part, mime_type);
g_free (mime_type);
g_object_unref (mime_part);
}
static void
mail_part_vcard_bind_dom_element (EMailPart *part,
WebKitDOMElement *element)
{
EMailPartVCard *vcard_part;
WebKitDOMNodeList *list;
WebKitDOMElement *iframe;
WebKitDOMElement *toggle_button;
WebKitDOMElement *save_button;
vcard_part = E_MAIL_PART_VCARD (part);
/* IFRAME */
list = webkit_dom_element_get_elements_by_tag_name (
element, "iframe");
if (webkit_dom_node_list_get_length (list) != 1)
return;
iframe = WEBKIT_DOM_ELEMENT (webkit_dom_node_list_item (list, 0));
g_clear_object (&vcard_part->iframe);
vcard_part->iframe = g_object_ref (iframe);
/* TOGGLE DISPLAY MODE BUTTON */
list = webkit_dom_element_get_elements_by_class_name (
element, "org-gnome-vcard-display-mode-button");
if (webkit_dom_node_list_get_length (list) != 1)
return;
toggle_button = WEBKIT_DOM_ELEMENT (webkit_dom_node_list_item (list, 0));
g_clear_object (&vcard_part->toggle_button);
vcard_part->toggle_button = g_object_ref (toggle_button);
/* SAVE TO ADDRESSBOOK BUTTON */
list = webkit_dom_element_get_elements_by_class_name (
element, "org-gnome-vcard-save-button");
if (webkit_dom_node_list_get_length (list) != 1)
return;
save_button = WEBKIT_DOM_ELEMENT (webkit_dom_node_list_item (list, 0));
g_clear_object (&vcard_part->save_button);
vcard_part->save_button = g_object_ref (save_button);
webkit_dom_event_target_add_event_listener (
WEBKIT_DOM_EVENT_TARGET (toggle_button),
"click", G_CALLBACK (display_mode_toggle_cb),
FALSE, vcard_part);
webkit_dom_event_target_add_event_listener (
WEBKIT_DOM_EVENT_TARGET (save_button),
"click", G_CALLBACK (save_vcard_cb),
FALSE, vcard_part);
/* Bind collapse buttons for contact lists. */
eab_contact_formatter_bind_dom (
webkit_dom_html_iframe_element_get_content_document (
WEBKIT_DOM_HTML_IFRAME_ELEMENT (iframe)));
}
static void
e_mail_part_vcard_class_init (EMailPartVCardClass *class)
{
GObjectClass *object_class;
EMailPartClass *mail_part_class;
g_type_class_add_private (class, sizeof (EMailPartVCardPrivate));
object_class = G_OBJECT_CLASS (class);
object_class->dispose = mail_part_vcard_dispose;
object_class->finalize = mail_part_vcard_finalize;
object_class->constructed = mail_part_vcard_constructed;
mail_part_class = E_MAIL_PART_CLASS (class);
mail_part_class->bind_dom_element = mail_part_vcard_bind_dom_element;
}
static void
e_mail_part_vcard_class_finalize (EMailPartVCardClass *class)
{
}
static void
e_mail_part_vcard_init (EMailPartVCard *part)
{
part->priv = E_MAIL_PART_VCARD_GET_PRIVATE (part);
}
void
e_mail_part_vcard_type_register (GTypeModule *type_module)
{
/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
* function, so we have to wrap it with a public function in
* order to register types from a separate compilation unit. */
e_mail_part_vcard_register_type (type_module);
}
EMailPartVCard *
e_mail_part_vcard_new (CamelMimePart *mime_part,
const gchar *id)
{
g_return_val_if_fail (id != NULL, NULL);
return g_object_new (
E_TYPE_MAIL_PART_VCARD,
"id", id, "mime-part", mime_part, NULL);
}
|