blob: 2c38adda8e68b07ee50866f5746ccf803aef8e4e [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());
161}
162
163QString EdsContactBackend::name() const
164{
165 return QObject::tr("Evolution-data-server backend");
166}
167
168QString EdsContactBackend::category() const
169{
170 return QObject::tr("Contacts");
171}
172
173bool EdsContactBackend::isEnabled() const
174{
175 return true;
176}
177
178static void
179contacts_cb(EBookClient *client, GAsyncResult *result, EdsContactBackend *self)
180{
181 g_return_if_fail(E_IS_BOOK_CLIENT(client));
182 GSList *contacts = NULL;
183 GError *error = NULL;
184 if(!e_book_client_get_contacts_finish(client, result, &contacts, &error)) {
185 g_critical("Unable to get contacts: %s", error->message);
186 g_error_free(error);
187 return;
188 } else {
189 self->parseContacts(contacts);
190 }
191}
192
193void EdsContactBackend::parseContacts(GSList *contacts)
194{
195 contacts_.reset(contacts);
196
197 /**
198 * parse each contact and create a person
199 *
200 * TODO: add only X at a time via a timeout function to reduce the load
201 */
202
203 for (GSList *l = contacts_.get(); l; l = l->next) {
204 EContact *contact = E_CONTACT(l->data);
205
206 /* Check if the photo is in-line or a URI, in the case that it is a URI,
207 * try to make it inline so that the lrc vcard parser is able to get the
208 * photo. Note that this will only work on local URIs
209 */
210 EContactPhoto *photo = (EContactPhoto *)e_contact_get(contact, E_CONTACT_PHOTO);
211 if (photo) {
212 if (photo->type == E_CONTACT_PHOTO_TYPE_URI) {
213 GError *error = NULL;
214 if (!e_contact_inline_local_photos(contact, &error)) {
215 g_warning("could not inline photo from vcard URI: %s", error->message);
216 g_error_free(error);
217 }
218 }
219 }
220 e_contact_photo_free(photo);
221
222 EVCard *vcard = E_VCARD(l->data);
223 Person *p = new Person(e_vcard_to_string(vcard, EVC_FORMAT_VCARD_30), Person::Encoding::vCard, this);
224 editor<Person>()->addExisting(p);
225 }
226}
227
228bool EdsContactBackend::load()
229{
230 /**
231 * load the contacts by querying for them,
232 * we want the contact to have some kind of name
233 */
234 EBookQuery *queries[4];
235 int idx = 0;
236 queries[idx++] = e_book_query_field_exists(E_CONTACT_NAME_OR_ORG);
237 queries[idx++] = e_book_query_field_exists(E_CONTACT_GIVEN_NAME);
238 queries[idx++] = e_book_query_field_exists(E_CONTACT_FAMILY_NAME);
239 queries[idx++] = e_book_query_field_exists(E_CONTACT_NICKNAME);
240
241 EBookQuery *name_query = e_book_query_or(idx, queries, TRUE);
242 gchar *query_str = e_book_query_to_string(name_query);
243 e_book_query_unref(name_query);
244 e_book_client_get_contacts(E_BOOK_CLIENT(client_.get()),
245 query_str,
246 cancellable_.get(),
247 (GAsyncReadyCallback)contacts_cb,
248 this);
249 g_free(query_str);
250
251 return true;
252}
253
254bool EdsContactBackend::reload()
255{
256 return false;
257}
258
259CollectionInterface::SupportedFeatures EdsContactBackend::supportedFeatures() const
260{
261 return (CollectionInterface::SupportedFeatures)(
262 CollectionInterface::SupportedFeatures::NONE |
263 CollectionInterface::SupportedFeatures::LOAD);
264}
265
266bool EdsContactBackend::clear()
267{
268 return false;
269}
270
271QByteArray EdsContactBackend::id() const
272{
273 return "edscb";
274}