blob: e4b02ddd67a5f244d394ff9ce08c3d3fcfba30d2 [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
156EdsContactBackend::EdsContactBackend(CollectionMediator<Person>* mediator, EClient *client, CollectionInterface* parent)
157 : CollectionInterface(new EdsContactEditor(mediator,this), parent)
158 , mediator_(mediator)
159 , client_(client, g_object_unref)
160 , cancellable_(g_cancellable_new(), g_object_unref)
161 , contacts_(nullptr, free_contact_list)
162{
163}
164
165EdsContactBackend::~EdsContactBackend()
166{
167 /* cancel any cancellable operations */
168 g_cancellable_cancel(cancellable_.get());
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400169
170 /* cancel contact loading timeout source, if its not finished */
171 if (add_contacts_source_id != 0)
172 g_source_remove(add_contacts_source_id);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400173}
174
175QString EdsContactBackend::name() const
176{
177 return QObject::tr("Evolution-data-server backend");
178}
179
180QString EdsContactBackend::category() const
181{
182 return QObject::tr("Contacts");
183}
184
185bool EdsContactBackend::isEnabled() const
186{
187 return true;
188}
189
190static void
191contacts_cb(EBookClient *client, GAsyncResult *result, EdsContactBackend *self)
192{
193 g_return_if_fail(E_IS_BOOK_CLIENT(client));
194 GSList *contacts = NULL;
195 GError *error = NULL;
196 if(!e_book_client_get_contacts_finish(client, result, &contacts, &error)) {
197 g_critical("Unable to get contacts: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400198 g_clear_error(&error);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400199 return;
200 } else {
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400201 self->addContacts(contacts);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400202 }
203}
204
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400205void EdsContactBackend::parseContact(EContact *contact)
206{
207 /* Check if the photo is in-line or a URI, in the case that it is a URI,
208 * try to make it inline so that the lrc vcard parser is able to get the
209 * photo. Note that this will only work on local URIs
210 */
211 EContactPhoto *photo = (EContactPhoto *)e_contact_get(contact, E_CONTACT_PHOTO);
212 if (photo) {
213 if (photo->type == E_CONTACT_PHOTO_TYPE_URI) {
214 GError *error = NULL;
215 if (!e_contact_inline_local_photos(contact, &error)) {
216 g_warning("could not inline photo from vcard URI: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400217 g_clear_error(&error);
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400218 }
219 }
220 }
221 e_contact_photo_free(photo);
222
223 EVCard *vcard = E_VCARD(contact);
224 gchar *vcard_str = e_vcard_to_string(vcard, EVC_FORMAT_VCARD_30);
225 Person *p = new Person(vcard_str, Person::Encoding::vCard, this);
226 g_free(vcard_str);
227 editor<Person>()->addExisting(p);
228}
229
230typedef struct AddContactsData_
231{
232 EdsContactBackend* backend;
233 GSList *next;
234} AddContactsData;
235
236static gboolean
237add_contacts(AddContactsData *data)
238{
239 for (int i = 0; i < data->backend->CONTACT_ADD_LIMIT && data->next; i++) {
240 data->backend->parseContact(E_CONTACT(data->next->data));
241 data->next = data->next->next;
242 }
243
244 if (!data->next) {
245 data->backend->lastContactAdded();
246 return G_SOURCE_REMOVE;
247 }
248
249 return G_SOURCE_CONTINUE;
250}
251
252void EdsContactBackend::lastContactAdded()
253{
254 /* Sets the source id to 0 to make sure we don't try to remove this source
255 * after it has already finished */
256 add_contacts_source_id = 0;
257}
258
259
260void EdsContactBackend::addContacts(GSList *contacts)
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400261{
262 contacts_.reset(contacts);
263
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400264 /* add CONTACT_ADD_LIMIT # of contacts every CONTACT_ADD_INTERVAL miliseconds */
265 AddContactsData *data = g_new0(AddContactsData, 1);
266 data->backend = this;
267 data->next = contacts_.get();
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400268
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400269 g_timeout_add_full(G_PRIORITY_DEFAULT,
270 CONTACT_ADD_INTERVAL,
271 (GSourceFunc)add_contacts,
272 data,
273 (GDestroyNotify)g_free);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400274}
275
276bool EdsContactBackend::load()
277{
278 /**
279 * load the contacts by querying for them,
280 * we want the contact to have some kind of name
281 */
282 EBookQuery *queries[4];
283 int idx = 0;
284 queries[idx++] = e_book_query_field_exists(E_CONTACT_NAME_OR_ORG);
285 queries[idx++] = e_book_query_field_exists(E_CONTACT_GIVEN_NAME);
286 queries[idx++] = e_book_query_field_exists(E_CONTACT_FAMILY_NAME);
287 queries[idx++] = e_book_query_field_exists(E_CONTACT_NICKNAME);
288
289 EBookQuery *name_query = e_book_query_or(idx, queries, TRUE);
290 gchar *query_str = e_book_query_to_string(name_query);
291 e_book_query_unref(name_query);
292 e_book_client_get_contacts(E_BOOK_CLIENT(client_.get()),
293 query_str,
294 cancellable_.get(),
295 (GAsyncReadyCallback)contacts_cb,
296 this);
297 g_free(query_str);
298
299 return true;
300}
301
302bool EdsContactBackend::reload()
303{
304 return false;
305}
306
Stepan Salenikovich4e409932015-04-24 12:12:39 -0400307FlagPack<CollectionInterface::SupportedFeatures> EdsContactBackend::supportedFeatures() const
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400308{
Stepan Salenikovich4e409932015-04-24 12:12:39 -0400309 return (CollectionInterface::SupportedFeatures::NONE |
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400310 CollectionInterface::SupportedFeatures::LOAD);
311}
312
313bool EdsContactBackend::clear()
314{
315 return false;
316}
317
318QByteArray EdsContactBackend::id() const
319{
320 return "edscb";
321}