blob: f67d38d6063505dcf03599fe3a8fe2095172d79c [file] [log] [blame]
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovich6f687072015-03-26 10:43:37 -04003 * 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 Salenikovich6f687072015-03-26 10:43:37 -040018 */
19
20#ifndef EDSCONTACTBACKEND_H
21#define EDSCONTACTBACKEND_H
22
23/**
24 * QT_NO_KEYWORDS is needed to resolve the conflict between the symbols used
25 * in this C library (ie: signals) and Qt keywords
26 */
27#define QT_NO_KEYWORDS
28#include <libebook/libebook.h>
29#undef QT_NO_KEYWORDS
30
31#include <collectioninterface.h>
32#include <memory>
33
34class Person;
35
36template<typename T> class CollectionMediator;
37
38void load_eds_sources(GCancellable *cancellable);
39
40class EdsContactBackend : public CollectionInterface
41{
42public:
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -040043 /* load 300 contacts every 10ms; this is fast enough to load
44 * 1M contacts in about 30s, but can be increased if need be
45 */
46 /* this sets how many contacts will be added at a time */
47 constexpr static int CONTACT_ADD_LIMIT {300};
48 /* this sets the interval in miliseconds to wait before adding
49 * more contacts */
50 constexpr static int CONTACT_ADD_INTERVAL {10};
51
Stepan Salenikovichb53e9c02015-04-14 17:41:10 -040052#if EDS_CHECK_VERSION(3,16,0)
53 /* The wait_for_connected_seconds argument had been added since 3.16, to let
54 * the caller decide how long to wait for the backend to fully connect to
55 * its (possibly remote) data store. This is required due to a change in the
56 * authentication process, which is fully asynchronous and done on the
57 * client side, while not every client is supposed to response to
58 * authentication requests. In case the backend will not connect within the
59 * set interval, then it is opened in an offline mode. A special value -1
60 * can be used to not wait for the connected state at all.
Edric Milaret66c50a32015-05-06 10:38:42 -040061 */
Stepan Salenikovichb53e9c02015-04-14 17:41:10 -040062 constexpr static guint32 WAIT_FOR_CONNECTED_SECONDS {5};
63#endif
64
Stepan Salenikovich6f687072015-03-26 10:43:37 -040065 explicit EdsContactBackend(CollectionMediator<Person>* mediator, EClient *client, CollectionInterface* parent = nullptr);
66 virtual ~EdsContactBackend();
67
68 virtual bool load() override;
69 virtual bool reload() override;
70 virtual bool clear() override;
71 virtual QString name () const override;
72 virtual QString category () const override;
73 virtual bool isEnabled() const override;
74 virtual QByteArray id () const override;
Stepan Salenikovich4e409932015-04-24 12:12:39 -040075 virtual FlagPack<SupportedFeatures> supportedFeatures() const override;
Stepan Salenikovich6f687072015-03-26 10:43:37 -040076
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -040077 void addClientView(std::unique_ptr<EBookClientView, void(*)(EBookClientView *)> client_view);
78 void addContacts(std::unique_ptr<GSList, void(*)(GSList *)> contacts);
Stepan Salenikovich21366ed2015-06-26 17:11:45 -040079 void removeContacts(std::unique_ptr<GSList, void(*)(GSList *)> contacts);
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -040080 void parseContact(EContact *contact);
81 void lastContactAdded();
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040082 bool addNewPerson(Person *item);
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -040083 bool savePerson(const Person *item);
Stepan Salenikovich6f687072015-03-26 10:43:37 -040084
85private:
86 CollectionMediator<Person>* mediator_;
87 std::unique_ptr<EClient, decltype(g_object_unref)&> client_;
88 std::unique_ptr<GCancellable, decltype(g_object_unref)&> cancellable_;
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -040089 std::unique_ptr<EBookClientView, void(*)(EBookClientView *)> client_view_;
Stepan Salenikovichdc7178b2015-04-13 17:38:46 -040090
91 guint add_contacts_source_id {0};
Stepan Salenikovichf4149272015-07-16 16:12:19 -040092
93 QString name_;
Stepan Salenikovich6f687072015-03-26 10:43:37 -040094};
95
Stepan Salenikovich8e882cd2015-05-28 14:18:06 -040096#endif /* EDSCONTACTBACKEND_H */