Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 39 | static void |
| 40 | client_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 Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 46 | g_clear_error(&error); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 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 | |
| 54 | static void |
| 55 | registry_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 Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 61 | g_clear_error(&error); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 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 */ |
Stepan Salenikovich | b53e9c0 | 2015-04-14 17:41:10 -0400 | [diff] [blame] | 69 | #if EDS_CHECK_VERSION(3,16,0) |
Edric Milaret | 66c50a3 | 2015-05-06 10:38:42 -0400 | [diff] [blame] | 70 | 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 Salenikovich | b53e9c0 | 2015-04-14 17:41:10 -0400 | [diff] [blame] | 77 | #endif |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | g_list_free_full(list, g_object_unref); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void 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 | |
| 93 | class EdsContactEditor : public CollectionEditor<Person> |
| 94 | { |
| 95 | public: |
| 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 | |
| 104 | private: |
| 105 | virtual QVector<Person*> items() const override; |
| 106 | |
| 107 | QVector<Person*> items_; |
| 108 | EdsContactBackend* collection_; |
| 109 | }; |
| 110 | |
| 111 | EdsContactEditor::EdsContactEditor(CollectionMediator<Person>* m, EdsContactBackend* parent) : |
| 112 | CollectionEditor<Person>(m),collection_(parent) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | EdsContactEditor::~EdsContactEditor() |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | bool EdsContactEditor::save(const Person* item) |
| 121 | { |
| 122 | Q_UNUSED(item) |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | bool EdsContactEditor::remove(const Person* item) |
| 127 | { |
| 128 | Q_UNUSED(item) |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | bool EdsContactEditor::edit( Person* item) |
| 133 | { |
| 134 | Q_UNUSED(item) |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | bool EdsContactEditor::addNew(const Person* item) |
| 139 | { |
| 140 | Q_UNUSED(item) |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | bool EdsContactEditor::addExisting(const Person* item) |
| 145 | { |
| 146 | items_ << const_cast<Person*>(item); |
| 147 | mediator()->addItem(item); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | QVector<Person*> EdsContactEditor::items() const |
| 152 | { |
| 153 | return items_; |
| 154 | } |
| 155 | |
| 156 | EdsContactBackend::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 | |
| 165 | EdsContactBackend::~EdsContactBackend() |
| 166 | { |
| 167 | /* cancel any cancellable operations */ |
| 168 | g_cancellable_cancel(cancellable_.get()); |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 169 | |
| 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 Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | QString EdsContactBackend::name() const |
| 176 | { |
| 177 | return QObject::tr("Evolution-data-server backend"); |
| 178 | } |
| 179 | |
| 180 | QString EdsContactBackend::category() const |
| 181 | { |
| 182 | return QObject::tr("Contacts"); |
| 183 | } |
| 184 | |
| 185 | bool EdsContactBackend::isEnabled() const |
| 186 | { |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | contacts_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 Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 198 | g_clear_error(&error); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 199 | return; |
| 200 | } else { |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 201 | self->addContacts(contacts); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 205 | void 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 Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 217 | g_clear_error(&error); |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 218 | } |
| 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 | |
| 230 | typedef struct AddContactsData_ |
| 231 | { |
| 232 | EdsContactBackend* backend; |
| 233 | GSList *next; |
| 234 | } AddContactsData; |
| 235 | |
| 236 | static gboolean |
| 237 | add_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 | |
| 252 | void 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 | |
| 260 | void EdsContactBackend::addContacts(GSList *contacts) |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 261 | { |
| 262 | contacts_.reset(contacts); |
| 263 | |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 264 | /* 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 Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 268 | |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 269 | g_timeout_add_full(G_PRIORITY_DEFAULT, |
| 270 | CONTACT_ADD_INTERVAL, |
| 271 | (GSourceFunc)add_contacts, |
| 272 | data, |
| 273 | (GDestroyNotify)g_free); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | bool 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 | |
| 302 | bool EdsContactBackend::reload() |
| 303 | { |
| 304 | return false; |
| 305 | } |
| 306 | |
Stepan Salenikovich | 4e40993 | 2015-04-24 12:12:39 -0400 | [diff] [blame] | 307 | FlagPack<CollectionInterface::SupportedFeatures> EdsContactBackend::supportedFeatures() const |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 308 | { |
Stepan Salenikovich | 4e40993 | 2015-04-24 12:12:39 -0400 | [diff] [blame] | 309 | return (CollectionInterface::SupportedFeatures::NONE | |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 310 | CollectionInterface::SupportedFeatures::LOAD); |
| 311 | } |
| 312 | |
| 313 | bool EdsContactBackend::clear() |
| 314 | { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | QByteArray EdsContactBackend::id() const |
| 319 | { |
| 320 | return "edscb"; |
| 321 | } |