blob: 0fb502d7d1861839bdb1338368703b28fa0ac628 [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{
128 Q_UNUSED(item)
129 return false;
130}
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
168free_contact_list(GSList *list)
169{
170 g_slist_free_full(list, g_object_unref);
171};
172
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400173EdsContactBackend::EdsContactBackend(CollectionMediator<Person>* mediator, EClient *client, CollectionInterface* parent)
174 : CollectionInterface(new EdsContactEditor(mediator,this), parent)
175 , mediator_(mediator)
176 , client_(client, g_object_unref)
177 , cancellable_(g_cancellable_new(), g_object_unref)
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400178 , client_view_(nullptr, &free_client_view)
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400179{
180}
181
182EdsContactBackend::~EdsContactBackend()
183{
184 /* cancel any cancellable operations */
185 g_cancellable_cancel(cancellable_.get());
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400186
187 /* cancel contact loading timeout source, if its not finished */
188 if (add_contacts_source_id != 0)
189 g_source_remove(add_contacts_source_id);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400190}
191
192QString EdsContactBackend::name() const
193{
194 return QObject::tr("Evolution-data-server backend");
195}
196
197QString EdsContactBackend::category() const
198{
199 return QObject::tr("Contacts");
200}
201
202bool EdsContactBackend::isEnabled() const
203{
204 return true;
205}
206
207static void
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400208contacts_added(G_GNUC_UNUSED EBookClientView *client_view, const GSList *objects, EdsContactBackend *self)
209{
210 std::unique_ptr<GSList,void(*)(GSList *)> contacts(
211 g_slist_copy_deep((GSList *)objects, (GCopyFunc)g_object_ref, NULL ), &free_contact_list);
212 self->addContacts(std::move(contacts));
213}
214
215static void
216client_view_cb(EBookClient *client, GAsyncResult *result, EdsContactBackend *self)
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400217{
218 g_return_if_fail(E_IS_BOOK_CLIENT(client));
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400219 EBookClientView *client_view = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400220 GError *error = NULL;
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400221 if(!e_book_client_get_view_finish(client, result, &client_view, &error)) {
222 g_critical("Unable to get client view: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400223 g_clear_error(&error);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400224 return;
225 } else {
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400226 /* we want the EBookClientView to have the same life cycle as the backend */
227 std::unique_ptr<EBookClientView, void(*)(EBookClientView *)> client_view_ptr(
228 client_view, &free_client_view);
229 self->addClientView(std::move(client_view_ptr));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400230 }
231}
232
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400233void EdsContactBackend::parseContact(EContact *contact)
234{
235 /* Check if the photo is in-line or a URI, in the case that it is a URI,
236 * try to make it inline so that the lrc vcard parser is able to get the
237 * photo. Note that this will only work on local URIs
238 */
239 EContactPhoto *photo = (EContactPhoto *)e_contact_get(contact, E_CONTACT_PHOTO);
240 if (photo) {
241 if (photo->type == E_CONTACT_PHOTO_TYPE_URI) {
242 GError *error = NULL;
243 if (!e_contact_inline_local_photos(contact, &error)) {
244 g_warning("could not inline photo from vcard URI: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400245 g_clear_error(&error);
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400246 }
247 }
248 }
249 e_contact_photo_free(photo);
250
251 EVCard *vcard = E_VCARD(contact);
252 gchar *vcard_str = e_vcard_to_string(vcard, EVC_FORMAT_VCARD_30);
253 Person *p = new Person(vcard_str, Person::Encoding::vCard, this);
254 g_free(vcard_str);
255 editor<Person>()->addExisting(p);
256}
257
258typedef struct AddContactsData_
259{
260 EdsContactBackend* backend;
261 GSList *next;
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400262 std::unique_ptr<GSList, void(*)(GSList *)> contacts;
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400263} AddContactsData;
264
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400265void
266free_add_contacts_data(AddContactsData *data)
267{
268 g_return_if_fail(data && data->contacts);
269 data->contacts.reset();
270 g_free(data);
271}
272
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400273static gboolean
274add_contacts(AddContactsData *data)
275{
276 for (int i = 0; i < data->backend->CONTACT_ADD_LIMIT && data->next; i++) {
277 data->backend->parseContact(E_CONTACT(data->next->data));
278 data->next = data->next->next;
279 }
280
281 if (!data->next) {
282 data->backend->lastContactAdded();
283 return G_SOURCE_REMOVE;
284 }
285
286 return G_SOURCE_CONTINUE;
287}
288
289void EdsContactBackend::lastContactAdded()
290{
291 /* Sets the source id to 0 to make sure we don't try to remove this source
292 * after it has already finished */
293 add_contacts_source_id = 0;
294}
295
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400296void EdsContactBackend::addClientView(std::unique_ptr<EBookClientView, void(*)(EBookClientView *)> client_view)
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400297{
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400298 client_view_ = std::move(client_view);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400299
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400300 /* connect signals for adding, removing, and modifying contacts */
301 g_signal_connect(client_view_.get(), "objects-added", G_CALLBACK(contacts_added), this);
302
303 /* start processing the signals */
304 GError *error = NULL;
305 e_book_client_view_start(client_view_.get(), &error);
306 if (error) {
307 g_critical("Unable to get start client view: %s", error->message);
308 g_clear_error(&error);
309 }
310}
311
312void EdsContactBackend::addContacts(std::unique_ptr<GSList, void(*)(GSList *)> contacts)
313{
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400314 /* add CONTACT_ADD_LIMIT # of contacts every CONTACT_ADD_INTERVAL miliseconds */
315 AddContactsData *data = g_new0(AddContactsData, 1);
316 data->backend = this;
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400317 data->contacts = std::move(contacts);
318 data->next = data->contacts.get();
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400319
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400320 g_timeout_add_full(G_PRIORITY_DEFAULT,
321 CONTACT_ADD_INTERVAL,
322 (GSourceFunc)add_contacts,
323 data,
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400324 (GDestroyNotify)free_add_contacts_data);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400325}
326
327bool EdsContactBackend::load()
328{
329 /**
330 * load the contacts by querying for them,
331 * we want the contact to have some kind of name
332 */
333 EBookQuery *queries[4];
334 int idx = 0;
335 queries[idx++] = e_book_query_field_exists(E_CONTACT_NAME_OR_ORG);
336 queries[idx++] = e_book_query_field_exists(E_CONTACT_GIVEN_NAME);
337 queries[idx++] = e_book_query_field_exists(E_CONTACT_FAMILY_NAME);
338 queries[idx++] = e_book_query_field_exists(E_CONTACT_NICKNAME);
339
340 EBookQuery *name_query = e_book_query_or(idx, queries, TRUE);
341 gchar *query_str = e_book_query_to_string(name_query);
342 e_book_query_unref(name_query);
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -0400343
344 /* test */
345 e_book_client_get_view(E_BOOK_CLIENT(client_.get()),
346 query_str,
347 cancellable_.get(),
348 (GAsyncReadyCallback)client_view_cb,
349 this);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400350 g_free(query_str);
351
352 return true;
353}
354
355bool EdsContactBackend::reload()
356{
357 return false;
358}
359
Stepan Salenikovich4e409932015-04-24 12:12:39 -0400360FlagPack<CollectionInterface::SupportedFeatures> EdsContactBackend::supportedFeatures() const
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400361{
Stepan Salenikovich4e409932015-04-24 12:12:39 -0400362 return (CollectionInterface::SupportedFeatures::NONE |
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400363 CollectionInterface::SupportedFeatures::LOAD);
364}
365
366bool EdsContactBackend::clear()
367{
368 return false;
369}
370
371QByteArray EdsContactBackend::id() const
372{
373 return "edscb";
374}