File: Contact.cpp

package info (click to toggle)
swift-im 5.0~alpha2.145.g12d031cf8%2Bdfsg-4.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,256 kB
  • sloc: cpp: 134,640; python: 2,701; sh: 774; xml: 561; javascript: 69; makefile: 59
file content (77 lines) | stat: -rw-r--r-- 2,323 bytes parent folder | download
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
/*
 * Copyright (c) 2013 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/Controllers/Contact.h>

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/find.hpp>

namespace Swift {

Contact::Contact() : statusType(StatusShow::None) {
}

Contact::Contact(const std::string& name, const JID& jid, StatusShow::Type statusType, const boost::filesystem::path& path) : name(name), jid(jid), statusType(statusType), avatarPath(path) {
}

bool Contact::lexicographicalSortPredicate(const Contact::ref& a, const Contact::ref& b) {
    if (a->jid.isValid() && b->jid.isValid()) {
        return a->jid < b->jid;
    } else {
        return a->name < b->name;
    }
}

bool Contact::equalityPredicate(const Contact::ref& a, const Contact::ref& b) {
    if (a->jid.isValid() && b->jid.isValid()) {
        return a->jid == b->jid;
    } else {
        return a->name == b->name;
    }
}

bool Contact::sortPredicate(const Contact::ref& a, const Contact::ref& b, const std::string& search) {
    /* perform case insensitive comparisons */
    std::string aLower = a->name;
    boost::to_lower(aLower);
    std::string bLower = b->name;
    boost::to_lower(bLower);
    std::string searchLower = search;
    boost::to_lower(searchLower);

    /* name starts with the search term */
    if (aLower.find(searchLower) == 0 && bLower.find(searchLower) != 0) {
        return true;
    } else if (bLower.find(searchLower) == 0 && aLower.find(searchLower) != 0) {
        return false;
    }

    /* name contains search term */
    if (aLower.find(searchLower) != std::string::npos && bLower.find(searchLower) == std::string::npos) {
        return true;
    } else if (bLower.find(searchLower) != std::string::npos && aLower.find(searchLower) == std::string::npos) {
        return false;
    }

    /* Levenshtein should be done here */
    /* if edit distances are equal, fall through to the tests below */

    /* lexicographical sort */
    if (a->statusType == b->statusType) {
        return aLower.compare(bLower) < 0;
    }

    /* online status */
    return a->statusType < b->statusType;
}

}