/* * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include #include namespace Swift { UserSearchModel::UserSearchModel() { } void UserSearchModel::clear() { emit layoutAboutToBeChanged(); results_.clear(); emit layoutChanged(); } void UserSearchModel::setResults(const std::vector& 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(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 fields = item->getFields(); std::map::const_iterator first = fields.find("first"); if (first != fields.end()) { result += P2QSTRING((*first).second); } std::map::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(results_.size()) ? createIndex(row, column, reinterpret_cast(const_cast(&(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; } }