blob: e8eb02dd475011dc2e2fcd192c8b06e046a8f1df [file] [log] [blame]
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
3 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#include "edscontactbackend.h"
32
33#include <person.h>
34#include <personmodel.h>
35#include <contactmethod.h>
36#include <collectioneditor.h>
37#include <memory>
38
39static void
40client_cb(G_GNUC_UNUSED ESource *source, GAsyncResult *result, G_GNUC_UNUSED gpointer user_data)
41{
42 GError *error = NULL;
43 EClient *client = e_book_client_connect_finish(result, &error);
44 if (!client) {
45 g_warning("%s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -040046 g_clear_error(&error);
Stepan Salenikovich6f687072015-03-26 10:43:37 -040047 } else {
48 /* got a client for this addressbook, add as backend */
49 PersonModel::instance()->addCollection<EdsContactBackend, EClient *>(
50 client, LoadOptions::FORCE_ENABLED);
51 }
52}
53
54static void
55registry_cb(G_GNUC_UNUSED GObject *source, GAsyncResult *result, GCancellable *cancellable)
56{
57 GError *error = NULL;
58 ESourceRegistry *registry = e_source_registry_new_finish(result, &error);
59 if(!registry) {
60 g_critical("Unable to create EDS registry: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -040061 g_clear_error(&error);
Stepan Salenikovich6f687072015-03-26 10:43:37 -040062 return;
63 } else {
64 GList *list = e_source_registry_list_enabled(registry, E_SOURCE_EXTENSION_ADDRESS_BOOK);
65
66 for (GList *l = list ; l; l = l->next) {
67 ESource *source = E_SOURCE(l->data);
68 /* try to connect to each source ansynch */
Stepan Salenikovichb53e9c02015-04-14 17:41:10 -040069#if EDS_CHECK_VERSION(3,16,0)
Edric Milaret66c50a32015-05-06 10:38:42 -040070 e_book_client_connect(source,
71 EdsContactBackend::WAIT_FOR_CONNECTED_SECONDS,
72 cancellable,
73 (GAsyncReadyCallback)client_cb,
74 NULL);
75#else
76 e_book_client_connect(source, cancellable, (GAsyncReadyCallback)client_cb, NULL);
Stepan Salenikovichb53e9c02015-04-14 17:41:10 -040077#endif
Stepan Salenikovich6f687072015-03-26 10:43:37 -040078 }
79
80 g_list_free_full(list, g_object_unref);
81 }
82}
83
84void load_eds_sources(GCancellable *cancellable)
85{
86 /* load the registery asynchronously, and then each source asynch as well
87 * pass the cancellable as a param so that the loading of each source can
88 * also be cancelled
89 */
90 e_source_registry_new(cancellable, (GAsyncReadyCallback)registry_cb, cancellable);
91}
92
93class EdsContactEditor : public CollectionEditor<Person>
94{
95public:
96 EdsContactEditor(CollectionMediator<Person>* m, EdsContactBackend* parent);
97 ~EdsContactEditor();
98 virtual bool save ( const Person* item ) override;
99 virtual bool remove ( const Person* item ) override;
100 virtual bool edit ( Person* item ) override;
101 virtual bool addNew ( const Person* item ) override;
102 virtual bool addExisting( const Person* item ) override;
103
104private:
105 virtual QVector<Person*> items() const override;
106
107 QVector<Person*> items_;
108 EdsContactBackend* collection_;
109};
110
111EdsContactEditor::EdsContactEditor(CollectionMediator<Person>* m, EdsContactBackend* parent) :
112CollectionEditor<Person>(m),collection_(parent)
113{
114}
115
116EdsContactEditor::~EdsContactEditor()
117{
118}
119
120bool EdsContactEditor::save(const Person* item)
121{
122 Q_UNUSED(item)
123 return false;
124}
125
126bool EdsContactEditor::remove(const Person* item)
127{
Stepan Salenikovichbb358592015-07-10 14:45:50 -0400128 mediator()->removeItem(item);
129 return true;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400130}
131
132bool EdsContactEditor::edit( Person* item)
133{
134 Q_UNUSED(item)
135 return false;
136}
137
138bool EdsContactEditor::addNew(const Person* item)
139{
140 Q_UNUSED(item)
141 return false;
142}
143
144bool EdsContactEditor::addExisting(const Person* item)
145{
146 items_ << const_cast<Person*>(item);
147 mediator()->addItem(item);
148 return true;
149}
150
151QVector<Person*> EdsContactEditor::items() const
152{
153 return items_;
154}
155
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400156static void free_client_view(EBookClientView *client_view) {
157 g_return_if_fail(client_view);
158 GError *error = NULL;
159 e_book_client_view_stop(client_view, &error);
160 if (error) {
161 g_warning("error stopping EBookClientView: %s", error->message);
162 g_clear_error(&error);
163 }
164 g_object_unref(client_view);
165}
166
167static void
Stepan Salenikovich21366ed2015-06-26 17:11:45 -0400168free_object_list(GSList *list)
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400169{
170 g_slist_free_full(list, g_object_unref);
171};
172
Stepan Salenikovich21366ed2015-06-26 17:11:45 -0400173static void
174free_string_list(GSList *list)
175{
176 g_slist_free_full(list, g_free);
177};
178
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400179EdsContactBackend::EdsContactBackend(CollectionMediator<Person>* mediator, EClient *client, CollectionInterface* parent)
180 : CollectionInterface(new EdsContactEditor(mediator,this), parent)
181 , mediator_(mediator)
182 , client_(client, g_object_unref)
183 , cancellable_(g_cancellable_new(), g_object_unref)
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400184 , client_view_(nullptr, &free_client_view)
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400185{
186}
187
188EdsContactBackend::~EdsContactBackend()
189{
190 /* cancel any cancellable operations */
191 g_cancellable_cancel(cancellable_.get());
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400192
193 /* cancel contact loading timeout source, if its not finished */
194 if (add_contacts_source_id != 0)
195 g_source_remove(add_contacts_source_id);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400196}
197
198QString EdsContactBackend::name() const
199{
200 return QObject::tr("Evolution-data-server backend");
201}
202
203QString EdsContactBackend::category() const
204{
205 return QObject::tr("Contacts");
206}
207
208bool EdsContactBackend::isEnabled() const
209{
210 return true;
211}
212
213static void
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400214contacts_added(G_GNUC_UNUSED EBookClientView *client_view, const GSList *objects, EdsContactBackend *self)
215{
216 std::unique_ptr<GSList,void(*)(GSList *)> contacts(
Stepan Salenikovich21366ed2015-06-26 17:11:45 -0400217 g_slist_copy_deep((GSList *)objects, (GCopyFunc)g_object_ref, NULL ), &free_object_list);
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400218 self->addContacts(std::move(contacts));
219}
220
221static void
Stepan Salenikovicheccd3102015-06-26 15:49:34 -0400222contacts_modified(EBookClientView *client_view, const GSList *objects, EdsContactBackend *self)
223{
224 /* The parseContact function will check if the contacts we're "adding" have
225 * the same URI as any existing ones and if so will update those instead */
226 contacts_added(client_view, objects, self);
227}
228
229static void
Stepan Salenikovich21366ed2015-06-26 17:11:45 -0400230contacts_removed(G_GNUC_UNUSED EBookClientView *client_view, const GSList *uids, EdsContactBackend *self)
231{
232 std::unique_ptr<GSList,void(*)(GSList *)> contact_uids(
233 g_slist_copy_deep((GSList *)uids, (GCopyFunc)g_strdup, NULL ), &free_string_list);
234 self->removeContacts(std::move(contact_uids));
235}
236
237static void
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400238client_view_cb(EBookClient *client, GAsyncResult *result, EdsContactBackend *self)
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400239{
240 g_return_if_fail(E_IS_BOOK_CLIENT(client));
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400241 EBookClientView *client_view = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400242 GError *error = NULL;
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400243 if(!e_book_client_get_view_finish(client, result, &client_view, &error)) {
244 g_critical("Unable to get client view: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400245 g_clear_error(&error);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400246 return;
247 } else {
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400248 /* we want the EBookClientView to have the same life cycle as the backend */
249 std::unique_ptr<EBookClientView, void(*)(EBookClientView *)> client_view_ptr(
250 client_view, &free_client_view);
251 self->addClientView(std::move(client_view_ptr));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400252 }
253}
254
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400255void EdsContactBackend::parseContact(EContact *contact)
256{
257 /* Check if the photo is in-line or a URI, in the case that it is a URI,
258 * try to make it inline so that the lrc vcard parser is able to get the
259 * photo. Note that this will only work on local URIs
260 */
261 EContactPhoto *photo = (EContactPhoto *)e_contact_get(contact, E_CONTACT_PHOTO);
262 if (photo) {
263 if (photo->type == E_CONTACT_PHOTO_TYPE_URI) {
264 GError *error = NULL;
265 if (!e_contact_inline_local_photos(contact, &error)) {
266 g_warning("could not inline photo from vcard URI: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400267 g_clear_error(&error);
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400268 }
269 }
270 }
271 e_contact_photo_free(photo);
272
273 EVCard *vcard = E_VCARD(contact);
274 gchar *vcard_str = e_vcard_to_string(vcard, EVC_FORMAT_VCARD_30);
275 Person *p = new Person(vcard_str, Person::Encoding::vCard, this);
276 g_free(vcard_str);
Stepan Salenikovicheccd3102015-06-26 15:49:34 -0400277
278 /* check if this person already exists */
279 Person *existing = PersonModel::instance()->getPersonByUid(p->uid());
280 if (existing) {
281 /* update existing person */
282 existing->setContactMethods ( p->phoneNumbers() );
283 existing->setNickName ( p->nickName() );
284 existing->setFirstName ( p->firstName() );
285 existing->setFamilyName ( p->secondName() );
286 existing->setFormattedName ( p->formattedName() );
287 existing->setOrganization ( p->organization() );
288 existing->setPreferredEmail ( p->preferredEmail() );
289 existing->setGroup ( p->group() );
290 existing->setDepartment ( p->department() );
291 existing->setPhoto ( p->photo() );
292
293 delete p;
294 } else {
295 editor<Person>()->addExisting(p);
296 }
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400297}
298
299typedef struct AddContactsData_
300{
301 EdsContactBackend* backend;
302 GSList *next;
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400303 std::unique_ptr<GSList, void(*)(GSList *)> contacts;
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400304} AddContactsData;
305
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400306void
307free_add_contacts_data(AddContactsData *data)
308{
309 g_return_if_fail(data && data->contacts);
310 data->contacts.reset();
311 g_free(data);
312}
313
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400314static gboolean
315add_contacts(AddContactsData *data)
316{
317 for (int i = 0; i < data->backend->CONTACT_ADD_LIMIT && data->next; i++) {
318 data->backend->parseContact(E_CONTACT(data->next->data));
319 data->next = data->next->next;
320 }
321
322 if (!data->next) {
323 data->backend->lastContactAdded();
324 return G_SOURCE_REMOVE;
325 }
326
327 return G_SOURCE_CONTINUE;
328}
329
330void EdsContactBackend::lastContactAdded()
331{
332 /* Sets the source id to 0 to make sure we don't try to remove this source
333 * after it has already finished */
334 add_contacts_source_id = 0;
335}
336
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400337void EdsContactBackend::addClientView(std::unique_ptr<EBookClientView, void(*)(EBookClientView *)> client_view)
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400338{
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400339 client_view_ = std::move(client_view);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400340
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400341 /* connect signals for adding, removing, and modifying contacts */
342 g_signal_connect(client_view_.get(), "objects-added", G_CALLBACK(contacts_added), this);
Stepan Salenikovicheccd3102015-06-26 15:49:34 -0400343 g_signal_connect(client_view_.get(), "objects-modified", G_CALLBACK(contacts_modified), this);
Stepan Salenikovich21366ed2015-06-26 17:11:45 -0400344 g_signal_connect(client_view_.get(), "objects-removed", G_CALLBACK(contacts_removed), this);
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400345
346 /* start processing the signals */
347 GError *error = NULL;
348 e_book_client_view_start(client_view_.get(), &error);
349 if (error) {
350 g_critical("Unable to get start client view: %s", error->message);
351 g_clear_error(&error);
352 }
353}
354
355void EdsContactBackend::addContacts(std::unique_ptr<GSList, void(*)(GSList *)> contacts)
356{
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400357 /* add CONTACT_ADD_LIMIT # of contacts every CONTACT_ADD_INTERVAL miliseconds */
358 AddContactsData *data = g_new0(AddContactsData, 1);
359 data->backend = this;
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400360 data->contacts = std::move(contacts);
361 data->next = data->contacts.get();
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400362
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400363 g_timeout_add_full(G_PRIORITY_DEFAULT,
364 CONTACT_ADD_INTERVAL,
365 (GSourceFunc)add_contacts,
366 data,
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400367 (GDestroyNotify)free_add_contacts_data);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400368}
369
Stepan Salenikovich21366ed2015-06-26 17:11:45 -0400370void EdsContactBackend::removeContacts(std::unique_ptr<GSList, void(*)(GSList *)> contact_uids)
371{
372 GSList *next = contact_uids.get();
373 while(next) {
374 gchar *uid = (gchar *)next->data;
375 if (uid) {
376 Person *p = PersonModel::instance()->getPersonByUid(uid);
377 if (p) {
378 g_debug("removing: %s", p->formattedName().toUtf8().constData());
379 deactivate(p);
Stepan Salenikovichbb358592015-07-10 14:45:50 -0400380 editor<Person>()->remove(p);
Stepan Salenikovich21366ed2015-06-26 17:11:45 -0400381 } else {
382 g_warning("person with given UID doesn't exist: %s", uid);
383 }
384 } else {
385 g_warning("null UID in list");
386 }
387 next = g_slist_next(next);
388 }
389}
390
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400391bool EdsContactBackend::load()
392{
393 /**
394 * load the contacts by querying for them,
395 * we want the contact to have some kind of name
396 */
397 EBookQuery *queries[4];
398 int idx = 0;
399 queries[idx++] = e_book_query_field_exists(E_CONTACT_NAME_OR_ORG);
400 queries[idx++] = e_book_query_field_exists(E_CONTACT_GIVEN_NAME);
401 queries[idx++] = e_book_query_field_exists(E_CONTACT_FAMILY_NAME);
402 queries[idx++] = e_book_query_field_exists(E_CONTACT_NICKNAME);
403
404 EBookQuery *name_query = e_book_query_or(idx, queries, TRUE);
405 gchar *query_str = e_book_query_to_string(name_query);
406 e_book_query_unref(name_query);
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400407
408 /* test */
409 e_book_client_get_view(E_BOOK_CLIENT(client_.get()),
410 query_str,
411 cancellable_.get(),
412 (GAsyncReadyCallback)client_view_cb,
413 this);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400414 g_free(query_str);
415
416 return true;
417}
418
419bool EdsContactBackend::reload()
420{
421 return false;
422}
423
Stepan Salenikovich4e409932015-04-24 12:12:39 -0400424FlagPack<CollectionInterface::SupportedFeatures> EdsContactBackend::supportedFeatures() const
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400425{
Stepan Salenikovich4e409932015-04-24 12:12:39 -0400426 return (CollectionInterface::SupportedFeatures::NONE |
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400427 CollectionInterface::SupportedFeatures::LOAD);
428}
429
430bool EdsContactBackend::clear()
431{
432 return false;
433}
434
435QByteArray EdsContactBackend::id() const
436{
437 return "edscb";
438}