summaryrefslogtreecommitdiffstats
blob: b6ac3cfc326b12519c8ff54635ae427911bb0a91 (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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/UserSearch/UserSearchModel.h>

#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {

UserSearchModel::UserSearchModel() {
}

void UserSearchModel::clear() {
    emit layoutAboutToBeChanged();
    results_.clear();
    emit layoutChanged();
}

void UserSearchModel::setResults(const std::vector<UserSearchResult>& results) {
    clear();
    emit layoutAboutToBeChanged();
    results_ = results;
    emit layoutChanged();
}

int UserSearchModel::columnCount(const QModelIndex& /*parent*/) const {
    return 1;
}

QVariant UserSearchModel::data(const QModelIndex& index, int role) const {
    if (!index.isValid()) return QVariant();
    UserSearchResult* result = static_cast<UserSearchResult*>(index.internalPointer());
    return data(result, role);
}

QVariant UserSearchModel::data(UserSearchResult* item, int role) {
    switch (role) {
        case Qt::DisplayRole: return QVariant(nameLine(item));
        case DetailTextRole: return QVariant(detailLine(item));
        default: return QVariant();
    }
}

QString UserSearchModel::nameLine(UserSearchResult* item) {
    QString result;
    const std::map<std::string, std::string> fields = item->getFields();
    std::map<std::string, std::string>::const_iterator first = fields.find("first");
    if (first != fields.end()) {
        result += P2QSTRING((*first).second);
    }
    std::map<std::string, std::string>::const_iterator last = fields.find("last");
    if (last != fields.end()) {
        if (!result.isEmpty()) {
            result += " ";
        }
        result += P2QSTRING((*last).second);
    }
    if (result.isEmpty()) {
        result = P2QSTRING(item->getJID().toString());
    }
    return result;
}

QString UserSearchModel::detailLine(UserSearchResult* item) {
    return P2QSTRING(item->getJID().toString());
}

QModelIndex UserSearchModel::index(int row, int column, const QModelIndex & parent) const {
    if (!hasIndex(row, column, parent)) {
        return QModelIndex();
    }
    return row < static_cast<int>(results_.size()) ? createIndex(row, column, reinterpret_cast<void*>(const_cast<UserSearchResult*>(&(results_[row])))) : QModelIndex();
}

QModelIndex UserSearchModel::parent(const QModelIndex& /*index*/) const {
    return QModelIndex();
}

int UserSearchModel::rowCount(const QModelIndex& parentIndex) const {
    if (!parentIndex.isValid()) {
        return results_.size();
    }
    return 0;
}

}