Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 1 | /* |
Stepan Salenikovich | be87d2c | 2016-01-25 14:14:34 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 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. |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "edscontactbackend.h" |
| 21 | |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 22 | #include <glib/gi18n.h> |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 23 | #include <person.h> |
| 24 | #include <personmodel.h> |
| 25 | #include <contactmethod.h> |
| 26 | #include <collectioneditor.h> |
| 27 | #include <memory> |
| 28 | |
| 29 | static void |
| 30 | client_cb(G_GNUC_UNUSED ESource *source, GAsyncResult *result, G_GNUC_UNUSED gpointer user_data) |
| 31 | { |
| 32 | GError *error = NULL; |
| 33 | EClient *client = e_book_client_connect_finish(result, &error); |
| 34 | if (!client) { |
| 35 | g_warning("%s", error->message); |
Stepan Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 36 | g_clear_error(&error); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 37 | } else { |
| 38 | /* got a client for this addressbook, add as backend */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 39 | PersonModel::instance().addCollection<EdsContactBackend, EClient *>( |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 40 | client, LoadOptions::FORCE_ENABLED); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static void |
| 45 | registry_cb(G_GNUC_UNUSED GObject *source, GAsyncResult *result, GCancellable *cancellable) |
| 46 | { |
| 47 | GError *error = NULL; |
| 48 | ESourceRegistry *registry = e_source_registry_new_finish(result, &error); |
| 49 | if(!registry) { |
Stepan Salenikovich | 6aa46d1 | 2017-02-10 12:24:57 -0500 | [diff] [blame] | 50 | g_warning("Unable to create EDS registry: %s", error->message); |
Stepan Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 51 | g_clear_error(&error); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 52 | return; |
| 53 | } else { |
| 54 | GList *list = e_source_registry_list_enabled(registry, E_SOURCE_EXTENSION_ADDRESS_BOOK); |
| 55 | |
| 56 | for (GList *l = list ; l; l = l->next) { |
| 57 | ESource *source = E_SOURCE(l->data); |
| 58 | /* try to connect to each source ansynch */ |
Stepan Salenikovich | b53e9c0 | 2015-04-14 17:41:10 -0400 | [diff] [blame] | 59 | #if EDS_CHECK_VERSION(3,16,0) |
Edric Milaret | 66c50a3 | 2015-05-06 10:38:42 -0400 | [diff] [blame] | 60 | e_book_client_connect(source, |
| 61 | EdsContactBackend::WAIT_FOR_CONNECTED_SECONDS, |
| 62 | cancellable, |
| 63 | (GAsyncReadyCallback)client_cb, |
| 64 | NULL); |
| 65 | #else |
| 66 | e_book_client_connect(source, cancellable, (GAsyncReadyCallback)client_cb, NULL); |
Stepan Salenikovich | b53e9c0 | 2015-04-14 17:41:10 -0400 | [diff] [blame] | 67 | #endif |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | g_list_free_full(list, g_object_unref); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void load_eds_sources(GCancellable *cancellable) |
| 75 | { |
| 76 | /* load the registery asynchronously, and then each source asynch as well |
| 77 | * pass the cancellable as a param so that the loading of each source can |
| 78 | * also be cancelled |
| 79 | */ |
| 80 | e_source_registry_new(cancellable, (GAsyncReadyCallback)registry_cb, cancellable); |
| 81 | } |
| 82 | |
| 83 | class EdsContactEditor : public CollectionEditor<Person> |
| 84 | { |
| 85 | public: |
| 86 | EdsContactEditor(CollectionMediator<Person>* m, EdsContactBackend* parent); |
| 87 | ~EdsContactEditor(); |
| 88 | virtual bool save ( const Person* item ) override; |
| 89 | virtual bool remove ( const Person* item ) override; |
| 90 | virtual bool edit ( Person* item ) override; |
Stepan Salenikovich | 58dd9e1 | 2015-08-05 10:39:36 -0400 | [diff] [blame] | 91 | virtual bool addNew ( Person* item ) override; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 92 | virtual bool addExisting( const Person* item ) override; |
| 93 | |
| 94 | private: |
| 95 | virtual QVector<Person*> items() const override; |
| 96 | |
| 97 | QVector<Person*> items_; |
| 98 | EdsContactBackend* collection_; |
| 99 | }; |
| 100 | |
| 101 | EdsContactEditor::EdsContactEditor(CollectionMediator<Person>* m, EdsContactBackend* parent) : |
| 102 | CollectionEditor<Person>(m),collection_(parent) |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | EdsContactEditor::~EdsContactEditor() |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | bool EdsContactEditor::save(const Person* item) |
| 111 | { |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 112 | return collection_->savePerson(item); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | bool EdsContactEditor::remove(const Person* item) |
| 116 | { |
aviau | ff72795 | 2016-01-29 16:23:07 -0500 | [diff] [blame] | 117 | bool ret = collection_->removePerson(item); |
| 118 | if (ret) { |
| 119 | mediator()->removeItem(item); |
| 120 | } |
| 121 | return ret; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bool EdsContactEditor::edit( Person* item) |
| 125 | { |
| 126 | Q_UNUSED(item) |
| 127 | return false; |
| 128 | } |
| 129 | |
Stepan Salenikovich | 58dd9e1 | 2015-08-05 10:39:36 -0400 | [diff] [blame] | 130 | bool EdsContactEditor::addNew(Person* item) |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 131 | { |
Stepan Salenikovich | 58dd9e1 | 2015-08-05 10:39:36 -0400 | [diff] [blame] | 132 | bool ret = collection_->addNewPerson(item); |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 133 | if (ret) { |
Stepan Salenikovich | 58dd9e1 | 2015-08-05 10:39:36 -0400 | [diff] [blame] | 134 | items_ << item; |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 135 | mediator()->addItem(item); |
| 136 | } |
| 137 | return ret; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | bool EdsContactEditor::addExisting(const Person* item) |
| 141 | { |
| 142 | items_ << const_cast<Person*>(item); |
| 143 | mediator()->addItem(item); |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | QVector<Person*> EdsContactEditor::items() const |
| 148 | { |
| 149 | return items_; |
| 150 | } |
| 151 | |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 152 | static void free_client_view(EBookClientView *client_view) { |
| 153 | g_return_if_fail(client_view); |
| 154 | GError *error = NULL; |
| 155 | e_book_client_view_stop(client_view, &error); |
| 156 | if (error) { |
| 157 | g_warning("error stopping EBookClientView: %s", error->message); |
| 158 | g_clear_error(&error); |
| 159 | } |
| 160 | g_object_unref(client_view); |
| 161 | } |
| 162 | |
| 163 | static void |
Stepan Salenikovich | 21366ed | 2015-06-26 17:11:45 -0400 | [diff] [blame] | 164 | free_object_list(GSList *list) |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 165 | { |
| 166 | g_slist_free_full(list, g_object_unref); |
| 167 | }; |
| 168 | |
Stepan Salenikovich | 21366ed | 2015-06-26 17:11:45 -0400 | [diff] [blame] | 169 | static void |
| 170 | free_string_list(GSList *list) |
| 171 | { |
| 172 | g_slist_free_full(list, g_free); |
| 173 | }; |
| 174 | |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 175 | EdsContactBackend::EdsContactBackend(CollectionMediator<Person>* mediator, EClient *client, CollectionInterface* parent) |
| 176 | : CollectionInterface(new EdsContactEditor(mediator,this), parent) |
| 177 | , mediator_(mediator) |
| 178 | , client_(client, g_object_unref) |
| 179 | , cancellable_(g_cancellable_new(), g_object_unref) |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 180 | , client_view_(nullptr, &free_client_view) |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 181 | { |
Stepan Salenikovich | f414927 | 2015-07-16 16:12:19 -0400 | [diff] [blame] | 182 | // cache the name |
| 183 | if (client_) { |
| 184 | auto source = e_client_get_source(client_.get()); |
| 185 | auto extension = (ESourceAddressBook *)e_source_get_extension(source, E_SOURCE_EXTENSION_ADDRESS_BOOK); |
| 186 | auto backend = e_source_backend_get_backend_name(E_SOURCE_BACKEND(extension)); |
| 187 | auto addressbook = e_source_get_display_name(source); |
| 188 | |
| 189 | gchar *name = g_strdup_printf("%s (%s)", addressbook, backend); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 190 | name_ = name; |
Stepan Salenikovich | f414927 | 2015-07-16 16:12:19 -0400 | [diff] [blame] | 191 | g_free(name); |
| 192 | } else |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 193 | name_ = _("Unknown EDS addressbook"); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | EdsContactBackend::~EdsContactBackend() |
| 197 | { |
| 198 | /* cancel any cancellable operations */ |
| 199 | g_cancellable_cancel(cancellable_.get()); |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 200 | |
| 201 | /* cancel contact loading timeout source, if its not finished */ |
| 202 | if (add_contacts_source_id != 0) |
| 203 | g_source_remove(add_contacts_source_id); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | QString EdsContactBackend::name() const |
| 207 | { |
Stepan Salenikovich | f414927 | 2015-07-16 16:12:19 -0400 | [diff] [blame] | 208 | return name_; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | QString EdsContactBackend::category() const |
| 212 | { |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 213 | return C_("Backend type", "Contacts"); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | bool EdsContactBackend::isEnabled() const |
| 217 | { |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | static void |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 222 | contacts_added(G_GNUC_UNUSED EBookClientView *client_view, const GSList *objects, EdsContactBackend *self) |
| 223 | { |
| 224 | std::unique_ptr<GSList,void(*)(GSList *)> contacts( |
Stepan Salenikovich | 21366ed | 2015-06-26 17:11:45 -0400 | [diff] [blame] | 225 | g_slist_copy_deep((GSList *)objects, (GCopyFunc)g_object_ref, NULL ), &free_object_list); |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 226 | self->addContacts(std::move(contacts)); |
| 227 | } |
| 228 | |
| 229 | static void |
Stepan Salenikovich | eccd310 | 2015-06-26 15:49:34 -0400 | [diff] [blame] | 230 | contacts_modified(EBookClientView *client_view, const GSList *objects, EdsContactBackend *self) |
| 231 | { |
| 232 | /* The parseContact function will check if the contacts we're "adding" have |
| 233 | * the same URI as any existing ones and if so will update those instead */ |
| 234 | contacts_added(client_view, objects, self); |
| 235 | } |
| 236 | |
| 237 | static void |
Stepan Salenikovich | 21366ed | 2015-06-26 17:11:45 -0400 | [diff] [blame] | 238 | contacts_removed(G_GNUC_UNUSED EBookClientView *client_view, const GSList *uids, EdsContactBackend *self) |
| 239 | { |
| 240 | std::unique_ptr<GSList,void(*)(GSList *)> contact_uids( |
| 241 | g_slist_copy_deep((GSList *)uids, (GCopyFunc)g_strdup, NULL ), &free_string_list); |
| 242 | self->removeContacts(std::move(contact_uids)); |
| 243 | } |
| 244 | |
| 245 | static void |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 246 | client_view_cb(EBookClient *client, GAsyncResult *result, EdsContactBackend *self) |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 247 | { |
| 248 | g_return_if_fail(E_IS_BOOK_CLIENT(client)); |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 249 | EBookClientView *client_view = NULL; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 250 | GError *error = NULL; |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 251 | if(!e_book_client_get_view_finish(client, result, &client_view, &error)) { |
Stepan Salenikovich | 6aa46d1 | 2017-02-10 12:24:57 -0500 | [diff] [blame] | 252 | g_warning("Unable to get EDS client view: %s", error->message); |
Stepan Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 253 | g_clear_error(&error); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 254 | return; |
| 255 | } else { |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 256 | /* we want the EBookClientView to have the same life cycle as the backend */ |
| 257 | std::unique_ptr<EBookClientView, void(*)(EBookClientView *)> client_view_ptr( |
| 258 | client_view, &free_client_view); |
| 259 | self->addClientView(std::move(client_view_ptr)); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 263 | void EdsContactBackend::parseContact(EContact *contact) |
| 264 | { |
| 265 | /* Check if the photo is in-line or a URI, in the case that it is a URI, |
| 266 | * try to make it inline so that the lrc vcard parser is able to get the |
| 267 | * photo. Note that this will only work on local URIs |
| 268 | */ |
| 269 | EContactPhoto *photo = (EContactPhoto *)e_contact_get(contact, E_CONTACT_PHOTO); |
| 270 | if (photo) { |
| 271 | if (photo->type == E_CONTACT_PHOTO_TYPE_URI) { |
| 272 | GError *error = NULL; |
| 273 | if (!e_contact_inline_local_photos(contact, &error)) { |
| 274 | g_warning("could not inline photo from vcard URI: %s", error->message); |
Stepan Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 275 | g_clear_error(&error); |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | e_contact_photo_free(photo); |
| 280 | |
| 281 | EVCard *vcard = E_VCARD(contact); |
| 282 | gchar *vcard_str = e_vcard_to_string(vcard, EVC_FORMAT_VCARD_30); |
Stepan Salenikovich | eccd310 | 2015-06-26 15:49:34 -0400 | [diff] [blame] | 283 | |
| 284 | /* check if this person already exists */ |
Stepan Salenikovich | e86ffba | 2015-07-13 11:32:40 -0400 | [diff] [blame] | 285 | Person *existing = nullptr; |
| 286 | |
| 287 | gchar *uid = (gchar *)e_contact_get(contact, E_CONTACT_UID); |
| 288 | if (uid) { |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 289 | // g_warning("got uid: %s", uid); |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 290 | existing = PersonModel::instance().getPersonByUid(uid); |
Stepan Salenikovich | e86ffba | 2015-07-13 11:32:40 -0400 | [diff] [blame] | 291 | g_free(uid); |
| 292 | } |
| 293 | |
Stepan Salenikovich | eccd310 | 2015-06-26 15:49:34 -0400 | [diff] [blame] | 294 | if (existing) { |
| 295 | /* update existing person */ |
Stepan Salenikovich | e86ffba | 2015-07-13 11:32:40 -0400 | [diff] [blame] | 296 | existing->updateFromVCard(vcard_str); |
Stepan Salenikovich | eccd310 | 2015-06-26 15:49:34 -0400 | [diff] [blame] | 297 | } else { |
Stepan Salenikovich | e86ffba | 2015-07-13 11:32:40 -0400 | [diff] [blame] | 298 | Person *p = new Person(vcard_str, Person::Encoding::vCard, this); |
Stepan Salenikovich | eccd310 | 2015-06-26 15:49:34 -0400 | [diff] [blame] | 299 | editor<Person>()->addExisting(p); |
| 300 | } |
Stepan Salenikovich | e86ffba | 2015-07-13 11:32:40 -0400 | [diff] [blame] | 301 | |
| 302 | g_free(vcard_str); |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | typedef struct AddContactsData_ |
| 306 | { |
| 307 | EdsContactBackend* backend; |
| 308 | GSList *next; |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 309 | std::unique_ptr<GSList, void(*)(GSList *)> contacts; |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 310 | } AddContactsData; |
| 311 | |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 312 | void |
| 313 | free_add_contacts_data(AddContactsData *data) |
| 314 | { |
| 315 | g_return_if_fail(data && data->contacts); |
| 316 | data->contacts.reset(); |
| 317 | g_free(data); |
| 318 | } |
| 319 | |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 320 | static gboolean |
| 321 | add_contacts(AddContactsData *data) |
| 322 | { |
| 323 | for (int i = 0; i < data->backend->CONTACT_ADD_LIMIT && data->next; i++) { |
| 324 | data->backend->parseContact(E_CONTACT(data->next->data)); |
| 325 | data->next = data->next->next; |
| 326 | } |
| 327 | |
| 328 | if (!data->next) { |
| 329 | data->backend->lastContactAdded(); |
| 330 | return G_SOURCE_REMOVE; |
| 331 | } |
| 332 | |
| 333 | return G_SOURCE_CONTINUE; |
| 334 | } |
| 335 | |
| 336 | void EdsContactBackend::lastContactAdded() |
| 337 | { |
| 338 | /* Sets the source id to 0 to make sure we don't try to remove this source |
| 339 | * after it has already finished */ |
| 340 | add_contacts_source_id = 0; |
| 341 | } |
| 342 | |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 343 | void EdsContactBackend::addClientView(std::unique_ptr<EBookClientView, void(*)(EBookClientView *)> client_view) |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 344 | { |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 345 | client_view_ = std::move(client_view); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 346 | |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 347 | /* connect signals for adding, removing, and modifying contacts */ |
| 348 | g_signal_connect(client_view_.get(), "objects-added", G_CALLBACK(contacts_added), this); |
Stepan Salenikovich | eccd310 | 2015-06-26 15:49:34 -0400 | [diff] [blame] | 349 | g_signal_connect(client_view_.get(), "objects-modified", G_CALLBACK(contacts_modified), this); |
Stepan Salenikovich | 21366ed | 2015-06-26 17:11:45 -0400 | [diff] [blame] | 350 | g_signal_connect(client_view_.get(), "objects-removed", G_CALLBACK(contacts_removed), this); |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 351 | |
| 352 | /* start processing the signals */ |
| 353 | GError *error = NULL; |
| 354 | e_book_client_view_start(client_view_.get(), &error); |
| 355 | if (error) { |
Stepan Salenikovich | 6aa46d1 | 2017-02-10 12:24:57 -0500 | [diff] [blame] | 356 | g_warning("Unable to start EDS client view: %s", error->message); |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 357 | g_clear_error(&error); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void EdsContactBackend::addContacts(std::unique_ptr<GSList, void(*)(GSList *)> contacts) |
| 362 | { |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 363 | /* add CONTACT_ADD_LIMIT # of contacts every CONTACT_ADD_INTERVAL miliseconds */ |
| 364 | AddContactsData *data = g_new0(AddContactsData, 1); |
| 365 | data->backend = this; |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 366 | data->contacts = std::move(contacts); |
| 367 | data->next = data->contacts.get(); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 368 | |
Stepan Salenikovich | dc7178b | 2015-04-13 17:38:46 -0400 | [diff] [blame] | 369 | g_timeout_add_full(G_PRIORITY_DEFAULT, |
| 370 | CONTACT_ADD_INTERVAL, |
| 371 | (GSourceFunc)add_contacts, |
| 372 | data, |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 373 | (GDestroyNotify)free_add_contacts_data); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Stepan Salenikovich | 21366ed | 2015-06-26 17:11:45 -0400 | [diff] [blame] | 376 | void EdsContactBackend::removeContacts(std::unique_ptr<GSList, void(*)(GSList *)> contact_uids) |
| 377 | { |
| 378 | GSList *next = contact_uids.get(); |
| 379 | while(next) { |
| 380 | gchar *uid = (gchar *)next->data; |
| 381 | if (uid) { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 382 | Person *p = PersonModel::instance().getPersonByUid(uid); |
Stepan Salenikovich | 21366ed | 2015-06-26 17:11:45 -0400 | [diff] [blame] | 383 | if (p) { |
| 384 | g_debug("removing: %s", p->formattedName().toUtf8().constData()); |
| 385 | deactivate(p); |
aviau | ff72795 | 2016-01-29 16:23:07 -0500 | [diff] [blame] | 386 | mediator_->removeItem(p); |
Stepan Salenikovich | 21366ed | 2015-06-26 17:11:45 -0400 | [diff] [blame] | 387 | } else { |
| 388 | g_warning("person with given UID doesn't exist: %s", uid); |
| 389 | } |
| 390 | } else { |
| 391 | g_warning("null UID in list"); |
| 392 | } |
| 393 | next = g_slist_next(next); |
| 394 | } |
| 395 | } |
| 396 | |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 397 | bool EdsContactBackend::load() |
| 398 | { |
| 399 | /** |
| 400 | * load the contacts by querying for them, |
| 401 | * we want the contact to have some kind of name |
| 402 | */ |
| 403 | EBookQuery *queries[4]; |
| 404 | int idx = 0; |
| 405 | queries[idx++] = e_book_query_field_exists(E_CONTACT_NAME_OR_ORG); |
| 406 | queries[idx++] = e_book_query_field_exists(E_CONTACT_GIVEN_NAME); |
| 407 | queries[idx++] = e_book_query_field_exists(E_CONTACT_FAMILY_NAME); |
| 408 | queries[idx++] = e_book_query_field_exists(E_CONTACT_NICKNAME); |
| 409 | |
| 410 | EBookQuery *name_query = e_book_query_or(idx, queries, TRUE); |
| 411 | gchar *query_str = e_book_query_to_string(name_query); |
| 412 | e_book_query_unref(name_query); |
Stepan Salenikovich | 8e882cd | 2015-05-28 14:18:06 -0400 | [diff] [blame] | 413 | |
| 414 | /* test */ |
| 415 | e_book_client_get_view(E_BOOK_CLIENT(client_.get()), |
| 416 | query_str, |
| 417 | cancellable_.get(), |
| 418 | (GAsyncReadyCallback)client_view_cb, |
| 419 | this); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 420 | g_free(query_str); |
| 421 | |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | bool EdsContactBackend::reload() |
| 426 | { |
| 427 | return false; |
| 428 | } |
| 429 | |
Stepan Salenikovich | 4e40993 | 2015-04-24 12:12:39 -0400 | [diff] [blame] | 430 | FlagPack<CollectionInterface::SupportedFeatures> EdsContactBackend::supportedFeatures() const |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 431 | { |
Stepan Salenikovich | 4e40993 | 2015-04-24 12:12:39 -0400 | [diff] [blame] | 432 | return (CollectionInterface::SupportedFeatures::NONE | |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 433 | CollectionInterface::SupportedFeatures::LOAD | |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 434 | CollectionInterface::SupportedFeatures::ADD | |
aviau | ff72795 | 2016-01-29 16:23:07 -0500 | [diff] [blame] | 435 | CollectionInterface::SupportedFeatures::REMOVE | |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 436 | CollectionInterface::SupportedFeatures::SAVE ); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | bool EdsContactBackend::clear() |
| 440 | { |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | QByteArray EdsContactBackend::id() const |
| 445 | { |
Stepan Salenikovich | f414927 | 2015-07-16 16:12:19 -0400 | [diff] [blame] | 446 | if (client_) |
| 447 | return e_source_get_uid(e_client_get_source(client_.get())); |
| 448 | return "edscb"; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 449 | } |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 450 | |
| 451 | bool EdsContactBackend::addNewPerson(Person *item) |
| 452 | { |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 453 | g_return_val_if_fail(client_.get(), false); |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 454 | |
| 455 | auto contact = e_contact_new_from_vcard(item->toVCard().constData()); |
| 456 | gchar *uid = NULL; |
| 457 | GError *error = NULL; |
| 458 | |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 459 | /* FIXME: this methods returns True for a google addressbook, but it never |
| 460 | * actually adds the new contact... not clear if this is possible */ |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 461 | bool ret = e_book_client_add_contact_sync( |
| 462 | E_BOOK_CLIENT(client_.get()), |
| 463 | contact, |
| 464 | &uid, |
| 465 | cancellable_.get(), |
| 466 | &error |
| 467 | ); |
| 468 | |
| 469 | if (!ret) { |
| 470 | if (error) { |
| 471 | g_warning("could not add contact to collection: %s", error->message); |
| 472 | g_clear_error(&error); |
| 473 | } else { |
| 474 | g_warning("could not add contact to collection"); |
| 475 | } |
| 476 | } else { |
| 477 | item->setUid(uid); |
| 478 | } |
| 479 | |
| 480 | g_free(uid); |
| 481 | g_object_unref(contact); |
| 482 | |
| 483 | return ret; |
| 484 | } |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 485 | |
aviau | ff72795 | 2016-01-29 16:23:07 -0500 | [diff] [blame] | 486 | bool EdsContactBackend::removePerson(const Person *item) |
| 487 | { |
| 488 | g_return_val_if_fail(client_.get(), false); |
| 489 | |
| 490 | g_debug("removing person"); |
| 491 | |
| 492 | GError *error = NULL; |
| 493 | |
| 494 | bool ret = e_book_client_remove_contact_by_uid_sync( |
| 495 | E_BOOK_CLIENT(client_.get()), |
| 496 | item->uid(), |
| 497 | cancellable_.get(), |
| 498 | &error |
| 499 | ); |
| 500 | |
| 501 | if(!ret) { |
| 502 | if(error) { |
| 503 | g_warning("could not delete contact: %s", error->message); |
| 504 | g_clear_error(&error); |
| 505 | } |
| 506 | else { |
| 507 | g_warning("could not delete contact"); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | return ret; |
| 512 | } |
| 513 | |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 514 | bool EdsContactBackend::savePerson(const Person *item) |
| 515 | { |
| 516 | g_return_val_if_fail(client_.get(), false); |
| 517 | |
| 518 | g_debug("saving person"); |
| 519 | |
| 520 | auto contact = e_contact_new_from_vcard(item->toVCard().constData()); |
| 521 | GError *error = NULL; |
| 522 | |
| 523 | /* FIXME: this methods fails for a google addressbook, not clear if it is |
| 524 | * possible to edit a google address book... gnome contacts simply creates |
| 525 | * a local contact with the same uid */ |
| 526 | bool ret = e_book_client_modify_contact_sync( |
| 527 | E_BOOK_CLIENT(client_.get()), |
| 528 | contact, |
| 529 | cancellable_.get(), |
| 530 | &error |
| 531 | ); |
| 532 | |
| 533 | if (!ret) { |
| 534 | if (error) { |
| 535 | g_warning("could not modify contact: %s", error->message); |
| 536 | g_clear_error(&error); |
| 537 | } else { |
| 538 | g_warning("could not modify contact"); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | g_object_unref(contact); |
| 543 | |
| 544 | return ret; |
| 545 | } |