summaryrefslogtreecommitdiff
path: root/src/dbus/server/pim/locale-factory-boost.cpp
blob: c82f67e6764069a5e0a4e4180514c5cf8238e080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * Copyright (C) 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

/**
 * The boost::locale based implementation of locale-factory.h.
 */

#include "locale-factory.h"
#include "folks.h"

#include <libebook/libebook.h>

#include <phonenumbers/phonenumberutil.h>
#include <boost/locale.hpp>
#include <boost/lexical_cast.hpp>

SE_GLIB_TYPE(EBookQuery, e_book_query)

SE_BEGIN_CXX

/**
 * Use higher levels to break ties between strings which are
 * considered equal at the lower levels. For example, "Façade" and
 * "facade" would be compared as equal when using only base
 * characters, but compare differently when also considering a higher
 * level which includes accents.
 *
 * The drawback of higher levels is that they are computationally more
 * expensive (transformation is slower and leads to longer transformed
 * strings, thus a longer string comparisons during compare) and may
 * end up comparing aspects that are irrelevant (like case).
 *
 * The default here pays attention to accents, but ignores the case.
 */
static const boost::locale::collator_base::level_type DEFAULT_COLLATION_LEVEL = boost::locale::collator_base::secondary;

class CompareFirstLastBoost : public IndividualCompare {
    std::locale m_locale;
    const boost::locale::collator<char> &m_collator;
public:
    CompareFirstLastBoost(const std::locale &locale) :
        m_locale(locale),
        m_collator(std::use_facet< boost::locale::collator<char> >(m_locale))
    {}

    virtual void createCriteria(FolksIndividual *individual, Criteria_t &criteria) const {
        FolksStructuredName *fn =
            folks_name_details_get_structured_name(FOLKS_NAME_DETAILS(individual));
        if (fn) {
            const char *family = folks_structured_name_get_family_name(fn);
            const char *given = folks_structured_name_get_given_name(fn);
            criteria.push_back(given ? m_collator.transform(DEFAULT_COLLATION_LEVEL, given) : "");
            criteria.push_back(family ? m_collator.transform(DEFAULT_COLLATION_LEVEL, family) : "");
        }
    }
};

class CompareLastFirstBoost : public IndividualCompare {
    std::locale m_locale;
    const boost::locale::collator<char> &m_collator;
public:
    CompareLastFirstBoost(const std::locale &locale) :
        m_locale(locale),
        m_collator(std::use_facet< boost::locale::collator<char> >(m_locale))
    {}

    virtual void createCriteria(FolksIndividual *individual, Criteria_t &criteria) const {
        FolksStructuredName *fn =
            folks_name_details_get_structured_name(FOLKS_NAME_DETAILS(individual));
        if (fn) {
            const char *family = folks_structured_name_get_family_name(fn);
            const char *given = folks_structured_name_get_given_name(fn);
            criteria.push_back(family ? m_collator.transform(DEFAULT_COLLATION_LEVEL, family) : "");
            criteria.push_back(given ? m_collator.transform(DEFAULT_COLLATION_LEVEL, given) : "");
        }
    }
};

class CompareFullnameBoost : public IndividualCompare {
    std::locale m_locale;
    const boost::locale::collator<char> &m_collator;
public:
    CompareFullnameBoost(const std::locale &locale) :
        m_locale(locale),
        m_collator(std::use_facet< boost::locale::collator<char> >(m_locale))
    {}

    virtual void createCriteria(FolksIndividual *individual, Criteria_t &criteria) const {
        const char *fullname = folks_name_details_get_full_name(FOLKS_NAME_DETAILS(individual));
        if (fullname) {
            criteria.push_back(m_collator.transform(DEFAULT_COLLATION_LEVEL, fullname));
        } else {
            FolksStructuredName *fn =
                folks_name_details_get_structured_name(FOLKS_NAME_DETAILS(individual));
            if (fn) {
                const char *given = folks_structured_name_get_given_name(fn);
                const char *middle = folks_structured_name_get_additional_names(fn);
                const char *family = folks_structured_name_get_family_name(fn);
                const char *suffix = folks_structured_name_get_suffixes(fn);
                std::string buffer;
                buffer.reserve(256);
#define APPEND(_str) \
                if (_str && *_str) { \
                    if (!buffer.empty()) { \
                        buffer += _str; \
                    } \
                }
                APPEND(given);
                APPEND(middle);
                APPEND(family);
                APPEND(suffix);
#undef APPEND
                criteria.push_back(m_collator.transform(DEFAULT_COLLATION_LEVEL, buffer));
            }
        }
    }
};

class AnyContainsBoost : public IndividualFilter
{
public:
    enum Mode {
        CASE_SENSITIVE,
        CASE_INSENSITIVE
    };

    AnyContainsBoost(const std::locale &locale,
                     const std::string &searchValue,
                     Mode mode) :
        m_locale(locale),
        // m_collator(std::use_facet<boost::locale::collator>(locale)),
        m_searchValue(searchValue),
        m_mode(mode)
    {
        switch (mode) {
        case CASE_SENSITIVE:
            // Search directly, no preprocessing.
            break;
        case CASE_INSENSITIVE:
            // Locale-aware conversion to fold case (= case
            // independent) representation before search.
            m_searchValueTransformed = boost::locale::fold_case(m_searchValue, m_locale);
            break;
        }
        m_searchValueTel = normalizePhoneText(m_searchValue.c_str());
    }

    /**
     * The search text is not necessarily a full phone number,
     * therefore we cannot parse it with libphonenumber. Instead
     * do a sub-string search after telephone specific normalization,
     * to let the search ignore irrelevant formatting aspects:
     *
     * - Map ASCII characters to the corresponding digit.
     * - Reduce to just the digits before comparison (no spaces, no
     *   punctuation).
     *
     * Example: +1-800-FOOBAR -> 1800366227
     */
    static std::string normalizePhoneText(const char *tel)
    {
        static const i18n::phonenumbers::PhoneNumberUtil &util(*i18n::phonenumbers::PhoneNumberUtil::GetInstance());
        std::string res;
        char c;
        bool haveAlpha = false;
        while ((c = *tel) != '\0') {
            if (isdigit(c)) {
                res += c;
            } else if (isalpha(c)) {
                haveAlpha = true;
                res += c;
            }
            ++tel;
        }
        // Only scan through the string again if we really have to.
        if (haveAlpha) {
            util.ConvertAlphaCharactersInNumber(&res);
        }

        return res;
    }

    bool containsSearchText(const char *text) const
    {
        if (!text) {
            return false;
        }
        switch (m_mode) {
        case CASE_SENSITIVE:
            return boost::contains(text, m_searchValue);
            break;
        case CASE_INSENSITIVE: {
            std::string lower(boost::locale::fold_case(text, m_locale));
            return boost::contains(lower, m_searchValueTransformed);
            break;
        }
        }
        // not reached
        return false;
    }

    bool containsSearchTel(const char *text) const
    {
        std::string tel = normalizePhoneText(text);
        return boost::contains(tel, m_searchValueTel);
    }

    virtual bool matches(const IndividualData &data) const
    {
        FolksIndividual *individual = data.m_individual.get();
        FolksNameDetails *name = FOLKS_NAME_DETAILS(individual);
        const char *fullname = folks_name_details_get_full_name(name);
        if (containsSearchText(fullname)) {
            return true;
        }
        const char *nickname = folks_name_details_get_nickname(name);
        if (containsSearchText(nickname)) {
            return true;
        }
        FolksStructuredName *fn =
            folks_name_details_get_structured_name(FOLKS_NAME_DETAILS(individual));
        if (fn) {
            const char *given = folks_structured_name_get_given_name(fn);
            if (containsSearchText(given)) {
                return true;
            }
            const char *middle = folks_structured_name_get_additional_names(fn);
            if (containsSearchText(middle)) {
                return true;
            }
            const char *family = folks_structured_name_get_family_name(fn);
            if (containsSearchText(family)) {
                return true;
            }
        }
        FolksEmailDetails *emailDetails = FOLKS_EMAIL_DETAILS(individual);
        GeeSet *emails = folks_email_details_get_email_addresses(emailDetails);
        BOOST_FOREACH (FolksEmailFieldDetails *email, GeeCollCXX<FolksEmailFieldDetails *>(emails)) {
            const gchar *value =
                reinterpret_cast<const gchar *>(folks_abstract_field_details_get_value(FOLKS_ABSTRACT_FIELD_DETAILS(email)));
            if (containsSearchText(value)) {
                return true;
            }
        }
        FolksPhoneDetails *phoneDetails = FOLKS_PHONE_DETAILS(individual);
        GeeSet *phones = folks_phone_details_get_phone_numbers(phoneDetails);
        BOOST_FOREACH (FolksAbstractFieldDetails *phone, GeeCollCXX<FolksAbstractFieldDetails *>(phones)) {
            const gchar *value =
                reinterpret_cast<const gchar *>(folks_abstract_field_details_get_value(phone));
            if (containsSearchTel(value)) {
                return true;
            }
        }
        return false;
    }

private:
    std::locale m_locale;
    std::string m_searchValue;
    std::string m_searchValueTransformed;
    std::string m_searchValueTel;
    Mode m_mode;
    // const bool (*m_contains)(const std::string &, const std::string &, const std::locale &);
};

/**
 * Search value must be a valid caller ID. The telephone numbers
 * in the contacts may or may not be valid; only valid ones
 * will match. The user is expected to clean up that data to get
 * exact matches for the others.
 */
class PhoneStartsWith : public IndividualFilter
{
public:
    PhoneStartsWith(const std::locale &m_locale,
                    const std::string &tel) :
        m_phoneNumberUtil(*i18n::phonenumbers::PhoneNumberUtil::GetInstance()),
        m_country(std::use_facet<boost::locale::info>(m_locale).country())
    {
        i18n::phonenumbers::PhoneNumber number;
        switch (m_phoneNumberUtil.Parse(tel, m_country, &number)) {
        case i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR:
            // okay
            break;
        case i18n::phonenumbers::PhoneNumberUtil::INVALID_COUNTRY_CODE_ERROR:
            SE_THROW("boost locale factory: invalid country code");
            break;
        case i18n::phonenumbers::PhoneNumberUtil::NOT_A_NUMBER:
            SE_THROW("boost locale factory: not a caller ID: " + tel);
            break;
        case i18n::phonenumbers::PhoneNumberUtil::TOO_SHORT_AFTER_IDD:
            SE_THROW("boost locale factory: too short after IDD: " + tel);
            break;
        case i18n::phonenumbers::PhoneNumberUtil::TOO_SHORT_NSN:
            SE_THROW("boost locale factory: too short NSN: " + tel);
            break;
        case i18n::phonenumbers::PhoneNumberUtil::TOO_LONG_NSN:
            SE_THROW("boost locale factory: too long NSN: " + tel);
            break;
        }

        // Search based on full internal format, without formatting.
        // For example: +41446681800
        //
        // A prefix match is good enough. That way a caller ID
        // with suppressed extension still matches a contact with
        // extension.
        m_phoneNumberUtil.Format(number, i18n::phonenumbers::PhoneNumberUtil::E164, &m_tel);
    }

    virtual bool matches(const IndividualData &data) const
    {
        BOOST_FOREACH(const std::string &tel, data.m_precomputed.m_phoneNumbers) {
            if (boost::starts_with(tel, m_tel)) {
                return true;
            }
        }
        return false;
    }

    virtual std::string getEBookFilter() const
    {
        // A suffix match with a limited number of digits is most
        // likely to find the right contacts.
        size_t len = std::min((size_t)4, m_tel.size());
        EBookQueryCXX query(e_book_query_field_test(E_CONTACT_TEL, E_BOOK_QUERY_ENDS_WITH,
                                                    m_tel.substr(m_tel.size() - len, len).c_str()),
                            false);
        PlainGStr filter(e_book_query_to_string(query.get()));
        return filter.get();
    }

private:
    const i18n::phonenumbers::PhoneNumberUtil &m_phoneNumberUtil;
    std::string m_country;
    std::string m_tel;
};

class LocaleFactoryBoost : public LocaleFactory
{
    const i18n::phonenumbers::PhoneNumberUtil &m_phoneNumberUtil;
    std::locale m_locale;
    std::string m_country;

public:
    LocaleFactoryBoost() :
        m_phoneNumberUtil(*i18n::phonenumbers::PhoneNumberUtil::GetInstance()),
        m_locale(genLocale()),
        m_country(std::use_facet<boost::locale::info>(m_locale).country())
    {}

    static std::locale genLocale()
    {
        // Get current locale from environment. Configure the
        // generated locale so that it supports what we need and
        // nothing more.
        boost::locale::generator gen;
        gen.characters(boost::locale::char_facet);
        gen.categories(boost::locale::collation_facet |
                       boost::locale::convert_facet |
                       boost::locale::information_facet);
        // TODO: Check env vars, then append @collation=phonebook
        // to get phonebook specific sorting.
        return gen("");
    }

    virtual boost::shared_ptr<IndividualCompare> createCompare(const std::string &order)
    {
        boost::shared_ptr<IndividualCompare> res;
        if (order == "first/last") {
            res.reset(new CompareFirstLastBoost(m_locale));
        } else if (order == "last/first") {
            res.reset(new CompareLastFirstBoost(m_locale));
        } else if (order == "fullname") {
            res.reset(new CompareFullnameBoost(m_locale));
        } else {
            SE_THROW("boost locale factory: sort order '" + order + "' not supported");
        }
        return res;
    }

    virtual boost::shared_ptr<IndividualFilter> createFilter(const Filter_t &filter)
    {
        int maxResults = -1;
        boost::shared_ptr<IndividualFilter> res;
        BOOST_FOREACH (const Filter_t::value_type &term, filter) {
            if (term.empty()) {
                SE_THROW("boost locale factory: empty search term not supported");
            }
            // Check for flags.
            if (term[0] == "limit") {
                if (term.size() != 2) {
                    SE_THROW("boost locale factory: 'limit' needs exactly one parameter");
                }
                maxResults = boost::lexical_cast<int>(term[1]);
            } else if (term[0] == "any-contains") {
                if (res) {
                    SE_THROW("boost locale factory: already have a search filter, 'any-contains' not valid");
                }

                AnyContainsBoost::Mode mode = AnyContainsBoost::CASE_INSENSITIVE;
                if (term.size() < 2) {
                    SE_THROW("boost locale factory: any-contains search needs one parameter");
                }
                for (size_t i = 2; i < term.size(); i++) {
                    const std::string &flag = term[i];
                    if (flag == "case-sensitive") {
                        mode = AnyContainsBoost::CASE_SENSITIVE;
                    } else if (flag == "case-insensitive") {
                        mode = AnyContainsBoost::CASE_INSENSITIVE;
                    } else {
                        SE_THROW("boost locale factory: unknown flag for any-contains: " + flag);
                    }
                }
                res.reset(new AnyContainsBoost(m_locale, term[1], mode));
            } else if (term[0] == "phone") {
                if (res) {
                    SE_THROW("boost locale factory: already have a search filter, 'phone' not valid");
                }

                if (filter.size() != 1) {
                    SE_THROW(StringPrintf("boost locale factory: only filter with one term are supported (was given %ld)",
                                          (long)filter.size()));
                }
                res.reset(new PhoneStartsWith(m_locale,
                                              term[1]));
            } else {
                SE_THROW("boost locale factory: unknown search term: " + term[0]);
            }
        }

        // May be empty (unfiltered). If a limit was given, then
        // we have to create a filter which matches everything, because
        // the FullView cannot apply a limit.
        if (maxResults != -1) {
            if (!res) {
                res.reset(new MatchAll());
            }
            res->setMaxResults(maxResults);
        }
        return res;
    }

    virtual void precompute(FolksIndividual *individual, Precomputed &precomputed) const
    {
        precomputed.m_phoneNumbers.clear();

        FolksPhoneDetails *phoneDetails = FOLKS_PHONE_DETAILS(individual);
        GeeSet *phones = folks_phone_details_get_phone_numbers(phoneDetails);
        precomputed.m_phoneNumbers.reserve(gee_collection_get_size(GEE_COLLECTION(phones)));
        BOOST_FOREACH (FolksAbstractFieldDetails *phone, GeeCollCXX<FolksAbstractFieldDetails *>(phones)) {
            const gchar *value =
                reinterpret_cast<const gchar *>(folks_abstract_field_details_get_value(phone));
            if (value) {
                i18n::phonenumbers::PhoneNumber number;
                // TODO
                i18n::phonenumbers::PhoneNumberUtil::ErrorType error =
                    m_phoneNumberUtil.Parse(value, m_country, &number);
                if (error == i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) {
                    std::string tel;
                    m_phoneNumberUtil.Format(number, i18n::phonenumbers::PhoneNumberUtil::E164, &tel);
                    precomputed.m_phoneNumbers.push_back(tel);
                }
            }
        }
    }
};

boost::shared_ptr<LocaleFactory> LocaleFactory::createFactory()
{
    return boost::shared_ptr<LocaleFactory>(new LocaleFactoryBoost());
}

SE_END_CXX