Fix build error
[claws.git] / src / addressbook-dbus.c
1 /*
2  * $Id$
3  */
4 /* vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:��,trail\:�: */
5
6 /*
7  * Claws-contacts is a proposed new design for the address book feature
8  * in Claws Mail. The goal for this new design was to create a
9  * solution more suitable for the term lightweight and to be more
10  * maintainable than the present implementation.
11  *
12  * More lightweight is achieved by design, in that sence that the whole
13  * structure is based on a plugable design.
14  *
15  * Claws Mail is Copyright (C) 1999-2018 by the Claws Mail Team and
16  * Claws-contacts is Copyright (C) 2018 by Michael Rasmussen.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include <glib.h>
38 #include <glib/gi18n.h>
39 #include <dbus/dbus.h>
40 #include <dbus/dbus-glib-bindings.h>
41 #include "dbus-contact.h"
42 #include "addrgather.h"
43 #include "folder.h"
44 #include "compose.h"
45 #include "hooks.h"
46
47 #include "addressbook-dbus.h"
48 #include "client-bindings.h"
49
50 static DBusGProxy* proxy = NULL;
51 static DBusGConnection* connection = NULL;
52 static Compose* compose_instance = NULL;
53
54 static GQuark client_object_error_quark() {
55         static GQuark quark = 0;
56         if (!quark)
57                 quark = g_quark_from_static_string ("client_object_error");
58
59         return quark;
60 }
61
62 static gboolean init(GError** error) {
63     connection = dbus_g_bus_get (DBUS_BUS_SESSION, error);
64     if (connection == NULL || *error) {
65         if (! *error)
66             g_set_error(error, client_object_error_quark(), 1, "Unable to connect to dbus");
67         g_warning("Unable to connect to dbus: %s", (*error)->message);
68         return FALSE;
69     }
70
71     proxy = dbus_g_proxy_new_for_name (connection,
72             "org.clawsmail.Contacts",
73             "/org/clawsmail/contacts",
74             "org.clawsmail.Contacts");
75     if (proxy == NULL) {
76         g_warning("Could not get a proxy object");
77         g_set_error(error, client_object_error_quark(), 1, "Could not get a proxy object");
78         return FALSE;
79     }
80
81     return TRUE;
82 }
83
84 static void dbus_contact_free(const DBusContact* contact) {
85     g_hash_table_destroy(contact->data);
86     g_ptr_array_free(contact->emails, TRUE);
87 }
88
89 static GHashTable* hash_table_new(void) {
90     GHashTable* hash_table;
91
92     hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
93
94     return hash_table;
95 }
96
97 static void g_value_email_free(gpointer data) {
98     GArray* email = (GArray *) data;
99     GValue* email_member;
100     guint i;
101
102     for (i = 0; i < email->len; i++) {
103         email_member = g_array_index(email, GValue*, i);
104         g_value_unset(email_member);
105     }
106 }
107
108 static GPtrArray* g_value_email_new() {
109     return g_ptr_array_new_with_free_func(g_value_email_free);
110 }
111
112 static gchar* convert_2_utf8(gchar* locale) {
113     gsize read, write;
114     GError* error = NULL;
115     gchar *current, *utf8;
116     const gchar* charset;
117
118     if (g_get_charset(&charset) || g_utf8_validate(locale, -1, 0))
119         return g_strdup(locale);
120
121     if (strcmp("ANSI_X3.4-1968", charset) == 0)
122         current = g_strdup("ISO-8859-1");
123     else
124         current = g_strdup(charset);
125
126     utf8 = g_convert(locale, -1, "UTF-8", current, &read, &write, &error);
127     if (error) {
128         g_warning("Failed to convert [%s]: %s", charset, error->message);
129         g_free(current);
130         return NULL;
131     }
132     g_free(current);
133
134     return utf8;
135 }
136
137 static void format_contact(DBusContact* contact, ContactData* c) {
138     gchar* firstname;
139     gchar* lastname;
140     GArray* email = NULL;
141     GValue email_member = {0};
142     gchar* str;
143     gchar* image = NULL;
144     gsize size;
145
146     contact->data = hash_table_new();
147     contact->emails = g_value_email_new();
148     firstname = lastname = NULL;
149
150     if (c->name) {
151         gchar* pos = strchr(c->name, ' ');
152         if (pos) {
153             firstname = g_strndup(c->name, pos - c->name);
154             lastname = g_strdup(++pos);
155             g_hash_table_replace(contact->data,
156                 g_strdup("first-name"), convert_2_utf8(firstname));
157             g_hash_table_replace(contact->data,
158                 g_strdup("last-name"), convert_2_utf8(lastname));
159         }
160         else {
161             lastname = g_strdup(c->name);
162             g_hash_table_replace(contact->data,
163                 g_strdup("last-name"), convert_2_utf8(lastname));
164         }
165         g_free(firstname);
166         g_free(lastname);
167     }
168     if (c->cn) {
169         g_hash_table_replace(contact->data,
170         g_strdup("cn"), convert_2_utf8(c->cn));
171     }
172
173     if (c->picture) {
174         gdk_pixbuf_save_to_buffer(
175         c->picture, &image, &size, "png", NULL, NULL);
176         g_hash_table_replace(contact->data,
177         g_strdup("image"), g_base64_encode((const guchar *) image, size));
178     }
179
180     email = g_array_new(FALSE, FALSE, sizeof(GValue*));
181
182     /* Alias is not available but needed so make an empty string */
183     g_value_init(&email_member, G_TYPE_STRING);
184     g_value_set_string(&email_member, "");
185     g_array_append_val(email, email_member);
186     g_value_unset(&email_member);
187
188     if (c->email)
189         str = convert_2_utf8(c->email);
190     else
191         str = g_strdup("");
192
193     g_value_init(&email_member, G_TYPE_STRING);
194     g_value_set_string(&email_member, str);
195     g_array_append_val(email, email_member);
196     g_value_unset(&email_member);
197     g_free(str);
198
199     if (c->remarks)
200         str = convert_2_utf8(c->remarks);
201     else
202         str = g_strdup("");
203
204     g_value_init(&email_member, G_TYPE_STRING);
205     g_value_set_string(&email_member, str);
206     g_array_append_val(email, email_member);
207     g_value_unset(&email_member);
208     g_free(str);
209
210     g_ptr_array_add(contact->emails, email);
211 }
212
213 static DBusHandlerResult contact_add_signal(DBusConnection* bus,
214                                             DBusMessage* message,
215                                             gpointer data) {
216     DBusError error;
217     gchar *s = NULL;
218
219     if (! compose_instance) {
220         g_message("Missing compose instance\n");
221         return DBUS_HANDLER_RESULT_HANDLED;
222     }
223
224     dbus_error_init (&error);
225
226     if (dbus_message_is_signal(message, "org.clawsmail.Contacts", "ContactMailTo")) {
227         if (dbus_message_get_args(
228             message, &error, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) {
229             debug_print("ContactMailTo address received: %s\n", s);
230             compose_entry_append(compose_instance, s, COMPOSE_TO, PREF_NONE);
231         }
232         else {
233             debug_print("ContactMailTo received with error: %s\n", error.message);
234             dbus_error_free(&error);
235         }
236     }
237     else if (dbus_message_is_signal(message, "org.clawsmail.Contacts", "ContactMailCc")) {
238         if (dbus_message_get_args(
239             message, &error, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) {
240             debug_print("ContactMailTo address received: %s\n", s);
241             compose_entry_append(compose_instance, s, COMPOSE_CC, PREF_NONE);
242         }
243         else {
244             debug_print("ContactMailTo received with error: %s\n", error.message);
245             dbus_error_free(&error);
246         }
247     }
248     else if (dbus_message_is_signal(message, "org.clawsmail.Contacts", "ContactMailBcc")) {
249         if (dbus_message_get_args(
250             message, &error, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) {
251             debug_print("ContactMailTo address received: %s\n", s);
252             compose_entry_append(compose_instance, s, COMPOSE_BCC, PREF_NONE);
253         }
254         else {
255             debug_print("ContactMailTo received with error: %s\n", error.message);
256             dbus_error_free(&error);
257         }
258     }
259     else {
260         if (error.message) {
261             g_warning("Reception error: %s", error.message);
262             dbus_error_free(&error);
263         }
264         debug_print("Unhandled signal received\n");
265         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
266     }
267
268     return DBUS_HANDLER_RESULT_HANDLED;
269 }
270
271 gboolean addressbook_start_service(GError** error) {
272     gchar* reply = NULL;
273     gboolean result = FALSE;
274
275     if (! init(error))
276         return result;
277
278     if (!org_clawsmail_Contacts_ping(proxy, &reply, error)) {
279         if (! *error)
280             g_set_error(error, client_object_error_quark(), 1, "Woops remote method failed");
281         g_warning ("Woops remote method failed: %s", (*error)->message);
282     }
283     if (reply && strcmp("PONG", reply) == 0)
284         result = TRUE;
285
286     return result;
287 }
288
289 int addressbook_dbus_add_contact(ContactData* contact, GError** error) {
290     DBusContact dbus_contact;
291
292     if (! init(error))
293         return -1;
294
295     format_contact(&dbus_contact, contact);
296     if (!org_clawsmail_Contacts_add_contact(
297         proxy, contact->book, dbus_contact.data, dbus_contact.emails, error)) {
298         if (! *error)
299             g_set_error(error, client_object_error_quark(), 1, "Woops remote method failed");
300         g_warning ("Woops remote method failed: %s", (*error)->message);
301         dbus_contact_free(&dbus_contact);
302         return -1;
303     }
304     dbus_contact_free(&dbus_contact);
305     return 0;
306 }
307
308 gboolean addrindex_dbus_load_completion(gint (*callBackFunc)
309                                         (const gchar* name,
310                                          const gchar* address,
311                                          const gchar* nick,
312                                          const gchar* alias,
313                                          GList* grp_emails),
314                                          GError** error) {
315     gchar **list = NULL, **contacts;
316     gchar *name, *email;
317
318     if (! init(error))
319         return FALSE;
320
321     if (!org_clawsmail_Contacts_search_addressbook(
322         proxy, "*", NULL, &list, error)) {
323         if (! *error)
324             g_set_error(error, client_object_error_quark(), 1, "Woops remote method failed");
325         g_warning ("Woops remote method failed: %s", (*error)->message);
326         g_strfreev(list);
327         return FALSE;
328     }
329     for (contacts = list; *contacts != NULL; contacts += 1) {
330         gchar* tmp = g_strdup(*contacts);
331         gchar* pos = g_strrstr(tmp, "\"");
332     if (pos) {
333             /* Contact has a name as part of email address */
334             *pos = '\0';
335             name = tmp;
336             name += 1;
337             pos += 3;
338             email = pos;
339             pos = g_strrstr(email, ">");
340             if (pos)
341             *pos = '\0';
342         }
343         else {
344             name = "";
345             email = tmp;
346         }
347         debug_print("Adding: %s <%s> to completition\n", name, email);
348         callBackFunc(name, email, NULL, NULL, NULL);
349         g_free(tmp);
350     }
351
352     return TRUE;
353 }
354
355 void addressbook_dbus_open(gboolean compose, GError** error) {
356     if (! init(error))
357         return;
358
359     if (!org_clawsmail_Contacts_show_addressbook(proxy, compose, error)) {
360         if (! *error)
361             g_set_error(error, client_object_error_quark(), 1, "Woops remote method failed");
362         g_warning ("Woops remote method failed: %s", (*error)->message);
363     }
364 }
365
366 GSList* addressbook_dbus_get_books(GError** error) {
367     gchar **book_names = NULL, **cur;
368     GSList* books = NULL;
369
370     if (! init(error)) {
371         return books;
372     }
373
374     if (!org_clawsmail_Contacts_book_list(proxy, &book_names, error)) {
375         if (! *error)
376             g_set_error(error, client_object_error_quark(), 1, "Woops remote method failed");
377         g_warning ("Woops remote method failed: %s", (*error)->message);
378         g_strfreev(book_names);
379         return books;
380     }
381     for (cur = book_names; *cur; cur += 1)
382         books = g_slist_prepend(books, g_strdup(*cur));
383
384     g_strfreev(book_names);
385
386     return books;
387 }
388
389 void contact_data_free(ContactData** data) {
390     ContactData* contact;
391
392     if (! data && ! *data)
393         return;
394
395     contact = *data;
396     g_free(contact->cn);
397     g_free(contact->email);
398     g_free(contact->remarks);
399     g_free(contact->name);
400     g_free(contact->book);
401     g_free(contact);
402     *data = NULL;
403 }
404
405 void addressbook_harvest(FolderItem *folderItem,
406                          gboolean sourceInd,
407                          GList *msgList ) {
408     addrgather_dlg_execute(folderItem, sourceInd, msgList);
409 }
410
411 void addressbook_connect_signals(Compose* compose) {
412     DBusConnection* bus;
413     DBusError* error = NULL;
414
415     g_return_if_fail(compose != NULL);
416
417     bus = dbus_bus_get (DBUS_BUS_SESSION, error);
418     if (!bus) {
419         g_warning ("Failed to connect to the D-BUS daemon: %s", error->message);
420         dbus_error_free(error);
421         return;
422     }
423
424     debug_print("Compose: %p\n", compose);
425     compose_instance = compose;
426     dbus_bus_add_match(bus, "type='signal',interface='org.clawsmail.Contacts'", error);
427     if (error) {
428         debug_print("Failed to add match to the D-BUS daemon: %s\n", error->message);
429         dbus_error_free(error);
430         return;
431     }
432     dbus_connection_add_filter(bus, contact_add_signal, NULL, NULL);
433 }
434
435 gchar* addressbook_get_vcard(const gchar* account, GError** error) {
436     gchar* vcard = NULL;
437
438     g_return_val_if_fail(account != NULL, vcard);
439
440     if (! init(error)) {
441         return vcard;
442     }
443
444     if (!org_clawsmail_Contacts_get_vcard(proxy, account, &vcard, error)) {
445         if (! *error)
446             g_set_error(error, client_object_error_quark(), 1, "Woops remote method failed");
447         g_warning ("Woops remote method failed: %s", (*error)->message);
448         g_free(vcard);
449         vcard = NULL;
450 }
451
452     return vcard;
453 }
454
455 gboolean addressbook_add_vcard(const gchar* abook, const gchar* vcard, GError** error) {
456     gboolean result = FALSE;
457
458     return result;
459 }
460
461 static gboolean my_compose_create_hook(gpointer source, gpointer user_data) {
462     //Compose *compose = (Compose*) source;
463     GError* error = NULL;
464
465     gchar* vcard = addressbook_get_vcard("test", &error);
466     if (error) {
467         g_warning("%s", error->message);
468         g_clear_error(&error);
469     }
470     else {
471         debug_print("test.vcf:\n%s\n", vcard);
472         g_free(vcard);
473 }
474
475     return FALSE;
476 }
477
478 void addressbook_install_hooks(GError** error) {
479     if ((guint)-1 == hooks_register_hook(COMPOSE_CREATED_HOOKLIST, my_compose_create_hook, NULL)) {
480         g_warning("Could not register hook for adding vCards");
481         if (error) {
482             g_set_error(error, client_object_error_quark(), 1,
483                 "Could not register hook for adding vCards");
484         }
485     }
486 }
487