blob: d76324aaeea47f6c9d6ebdfaf47d0b05c16e7ef2 [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);
46 g_error_free(error);
47 } 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);
61 g_error_free(error);
62 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 */
69 e_book_client_connect(source, cancellable, (GAsyncReadyCallback)client_cb, NULL);
70 }
71
72 g_list_free_full(list, g_object_unref);
73 }
74}
75
76void load_eds_sources(GCancellable *cancellable)
77{
78 /* load the registery asynchronously, and then each source asynch as well
79 * pass the cancellable as a param so that the loading of each source can
80 * also be cancelled
81 */
82 e_source_registry_new(cancellable, (GAsyncReadyCallback)registry_cb, cancellable);
83}
84
85class EdsContactEditor : public CollectionEditor<Person>
86{
87public:
88 EdsContactEditor(CollectionMediator<Person>* m, EdsContactBackend* parent);
89 ~EdsContactEditor();
90 virtual bool save ( const Person* item ) override;
91 virtual bool remove ( const Person* item ) override;
92 virtual bool edit ( Person* item ) override;
93 virtual bool addNew ( const Person* item ) override;
94 virtual bool addExisting( const Person* item ) override;
95
96private:
97 virtual QVector<Person*> items() const override;
98
99 QVector<Person*> items_;
100 EdsContactBackend* collection_;
101};
102
103EdsContactEditor::EdsContactEditor(CollectionMediator<Person>* m, EdsContactBackend* parent) :
104CollectionEditor<Person>(m),collection_(parent)
105{
106}
107
108EdsContactEditor::~EdsContactEditor()
109{
110}
111
112bool EdsContactEditor::save(const Person* item)
113{
114 Q_UNUSED(item)
115 return false;
116}
117
118bool EdsContactEditor::remove(const Person* item)
119{
120 Q_UNUSED(item)
121 return false;
122}
123
124bool EdsContactEditor::edit( Person* item)
125{
126 Q_UNUSED(item)
127 return false;
128}
129
130bool EdsContactEditor::addNew(const Person* item)
131{
132 Q_UNUSED(item)
133 return false;
134}
135
136bool EdsContactEditor::addExisting(const Person* item)
137{
138 items_ << const_cast<Person*>(item);
139 mediator()->addItem(item);
140 return true;
141}
142
143QVector<Person*> EdsContactEditor::items() const
144{
145 return items_;
146}
147
148EdsContactBackend::EdsContactBackend(CollectionMediator<Person>* mediator, EClient *client, CollectionInterface* parent)
149 : CollectionInterface(new EdsContactEditor(mediator,this), parent)
150 , mediator_(mediator)
151 , client_(client, g_object_unref)
152 , cancellable_(g_cancellable_new(), g_object_unref)
153 , contacts_(nullptr, free_contact_list)
154{
155}
156
157EdsContactBackend::~EdsContactBackend()
158{
159 /* cancel any cancellable operations */
160 g_cancellable_cancel(cancellable_.get());
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400161
162 /* cancel contact loading timeout source, if its not finished */
163 if (add_contacts_source_id != 0)
164 g_source_remove(add_contacts_source_id);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400165}
166
167QString EdsContactBackend::name() const
168{
169 return QObject::tr("Evolution-data-server backend");
170}
171
172QString EdsContactBackend::category() const
173{
174 return QObject::tr("Contacts");
175}
176
177bool EdsContactBackend::isEnabled() const
178{
179 return true;
180}
181
182static void
183contacts_cb(EBookClient *client, GAsyncResult *result, EdsContactBackend *self)
184{
185 g_return_if_fail(E_IS_BOOK_CLIENT(client));
186 GSList *contacts = NULL;
187 GError *error = NULL;
188 if(!e_book_client_get_contacts_finish(client, result, &contacts, &error)) {
189 g_critical("Unable to get contacts: %s", error->message);
190 g_error_free(error);
191 return;
192 } else {
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400193 self->addContacts(contacts);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400194 }
195}
196
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400197void EdsContactBackend::parseContact(EContact *contact)
198{
199 /* Check if the photo is in-line or a URI, in the case that it is a URI,
200 * try to make it inline so that the lrc vcard parser is able to get the
201 * photo. Note that this will only work on local URIs
202 */
203 EContactPhoto *photo = (EContactPhoto *)e_contact_get(contact, E_CONTACT_PHOTO);
204 if (photo) {
205 if (photo->type == E_CONTACT_PHOTO_TYPE_URI) {
206 GError *error = NULL;
207 if (!e_contact_inline_local_photos(contact, &error)) {
208 g_warning("could not inline photo from vcard URI: %s", error->message);
209 g_error_free(error);
210 }
211 }
212 }
213 e_contact_photo_free(photo);
214
215 EVCard *vcard = E_VCARD(contact);
216 gchar *vcard_str = e_vcard_to_string(vcard, EVC_FORMAT_VCARD_30);
217 Person *p = new Person(vcard_str, Person::Encoding::vCard, this);
218 g_free(vcard_str);
219 editor<Person>()->addExisting(p);
220}
221
222typedef struct AddContactsData_
223{
224 EdsContactBackend* backend;
225 GSList *next;
226} AddContactsData;
227
228static gboolean
229add_contacts(AddContactsData *data)
230{
231 for (int i = 0; i < data->backend->CONTACT_ADD_LIMIT && data->next; i++) {
232 data->backend->parseContact(E_CONTACT(data->next->data));
233 data->next = data->next->next;
234 }
235
236 if (!data->next) {
237 data->backend->lastContactAdded();
238 return G_SOURCE_REMOVE;
239 }
240
241 return G_SOURCE_CONTINUE;
242}
243
244void EdsContactBackend::lastContactAdded()
245{
246 /* Sets the source id to 0 to make sure we don't try to remove this source
247 * after it has already finished */
248 add_contacts_source_id = 0;
249}
250
251
252void EdsContactBackend::addContacts(GSList *contacts)
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400253{
254 contacts_.reset(contacts);
255
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400256 /* add CONTACT_ADD_LIMIT # of contacts every CONTACT_ADD_INTERVAL miliseconds */
257 AddContactsData *data = g_new0(AddContactsData, 1);
258 data->backend = this;
259 data->next = contacts_.get();
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400260
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -0400261 g_timeout_add_full(G_PRIORITY_DEFAULT,
262 CONTACT_ADD_INTERVAL,
263 (GSourceFunc)add_contacts,
264 data,
265 (GDestroyNotify)g_free);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400266}
267
268bool EdsContactBackend::load()
269{
270 /**
271 * load the contacts by querying for them,
272 * we want the contact to have some kind of name
273 */
274 EBookQuery *queries[4];
275 int idx = 0;
276 queries[idx++] = e_book_query_field_exists(E_CONTACT_NAME_OR_ORG);
277 queries[idx++] = e_book_query_field_exists(E_CONTACT_GIVEN_NAME);
278 queries[idx++] = e_book_query_field_exists(E_CONTACT_FAMILY_NAME);
279 queries[idx++] = e_book_query_field_exists(E_CONTACT_NICKNAME);
280
281 EBookQuery *name_query = e_book_query_or(idx, queries, TRUE);
282 gchar *query_str = e_book_query_to_string(name_query);
283 e_book_query_unref(name_query);
284 e_book_client_get_contacts(E_BOOK_CLIENT(client_.get()),
285 query_str,
286 cancellable_.get(),
287 (GAsyncReadyCallback)contacts_cb,
288 this);
289 g_free(query_str);
290
291 return true;
292}
293
294bool EdsContactBackend::reload()
295{
296 return false;
297}
298
299CollectionInterface::SupportedFeatures EdsContactBackend::supportedFeatures() const
300{
301 return (CollectionInterface::SupportedFeatures)(
302 CollectionInterface::SupportedFeatures::NONE |
303 CollectionInterface::SupportedFeatures::LOAD);
304}
305
306bool EdsContactBackend::clear()
307{
308 return false;
309}
310
311QByteArray EdsContactBackend::id() const
312{
313 return "edscb";
314}