File indexing completed on 2025-01-19 04:52:00

0001 /*
0002     Copyright (c) 2016 Michael Bohlender <michael.bohlender@kdemail.net>
0003     Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0004 
0005     This library is free software; you can redistribute it and/or modify it
0006     under the terms of the GNU Library General Public License as published by
0007     the Free Software Foundation; either version 2 of the License, or (at your
0008     option) any later version.
0009 
0010     This library is distributed in the hope that it will be useful, but WITHOUT
0011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0013     License for more details.
0014 
0015     You should have received a copy of the GNU Library General Public License
0016     along with this library; see the file COPYING.LIB.  If not, write to the
0017     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0018     02110-1301, USA.
0019 */
0020 #include "peoplemodel.h"
0021 
0022 #include <sink/standardqueries.h>
0023 #include <sink/store.h>
0024 
0025 PeopleModel::PeopleModel(QObject *parent)
0026     : QSortFilterProxyModel(parent)
0027 {
0028     using namespace Sink::ApplicationDomain;
0029 
0030     setDynamicSortFilter(true);
0031     sort(0, Qt::DescendingOrder);
0032     setFilterCaseSensitivity(Qt::CaseInsensitive);
0033     Sink::Query query;
0034     query.setFlags(Sink::Query::LiveQuery);
0035     query.request<Contact::Fn>();
0036     query.request<Contact::Emails>();
0037     query.request<Contact::Addressbook>();
0038     query.request<Contact::Vcard>();
0039     query.request<Contact::Firstname>();
0040     query.request<Contact::Lastname>();
0041     query.request<Contact::Photo>();
0042     runQuery(query);
0043 }
0044 
0045 PeopleModel::~PeopleModel()
0046 {
0047 
0048 }
0049 
0050 void PeopleModel::setFilter(const QString &filter)
0051 {
0052     setFilterWildcard(filter);
0053 }
0054 
0055 QString PeopleModel::filter() const
0056 {
0057      return {};
0058 }
0059 
0060 QHash< int, QByteArray > PeopleModel::roleNames() const
0061 {
0062     static QHash<int, QByteArray> roles = {
0063         {Name, "name"},
0064         {Emails, "emails"},
0065         {Addressbook, "addressbook"},
0066         {Type, "type"},
0067         {DomainObject, "domainObject"},
0068         {FirstName, "firstName"},
0069         {LastName, "lastName"},
0070         {ImageData, "imageData"}
0071         };
0072     return roles;
0073 }
0074 
0075 static QStringList toStringList(const QList<Sink::ApplicationDomain::Contact::Email> &list)
0076 {
0077     QStringList out;
0078     std::transform(list.constBegin(), list.constEnd(), std::back_inserter(out), [] (const Sink::ApplicationDomain::Contact::Email &s) { return s.email; });
0079     return out;
0080 }
0081 
0082 QPair<QString, QString> getFirstnameLastname(const QString &fn)
0083 {
0084     auto parts = fn.split(' ');
0085     if (parts.isEmpty()) {
0086         return {};
0087     }
0088     if (parts.size() == 1) {
0089         return {parts.first(), {}};
0090     }
0091     const auto lastName = parts.takeLast();
0092     return {parts.join(' '), lastName};
0093 }
0094 
0095 QVariant PeopleModel::data(const QModelIndex &idx, int role) const
0096 {
0097     auto srcIdx = mapToSource(idx);
0098     auto contact = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Contact::Ptr>();
0099     switch (role) {
0100         case Name:
0101             return contact->getFn();
0102         case Emails:
0103             return QVariant::fromValue(toStringList(contact->getEmails()));
0104         case Addressbook:
0105             return contact->getAddressbook();
0106         case Type:
0107             return "contact";
0108         case DomainObject:
0109             return QVariant::fromValue(contact);
0110         case FirstName: {
0111             const auto n = contact->getFirstname();
0112             //Fall back to the fn if we have no name
0113             if (n.isEmpty()) {
0114                 return getFirstnameLastname(contact->getFn()).first;
0115             }
0116             return n;
0117         }
0118         case LastName: {
0119             const auto n = contact->getLastname();
0120             //Fall back to the fn if we have no name
0121             if (n.isEmpty()) {
0122                 return getFirstnameLastname(contact->getFn()).second;
0123             }
0124             return n;
0125         }
0126         case ImageData:
0127             return contact->getPhoto();
0128     }
0129     return QSortFilterProxyModel::data(idx, role);
0130 }
0131 
0132 bool PeopleModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0133 {
0134     const auto leftName = left.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Contact::Ptr>()->getFn();
0135     const auto rightName = right.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Contact::Ptr>()->getFn();
0136     return leftName < rightName;
0137 }
0138 
0139 void PeopleModel::runQuery(const Sink::Query &query)
0140 {
0141     mModel = Sink::Store::loadModel<Sink::ApplicationDomain::Contact>(query);
0142     setSourceModel(mModel.data());
0143 }
0144 
0145 void PeopleModel::setAddressbook(const QVariant &/*parentFolder*/)
0146 {
0147     //TODO filter query by addressbook
0148     qWarning() << "The addressbook filter is not yet implemented";
0149 }
0150 
0151 QVariant PeopleModel::addressbook() const
0152 {
0153     return QVariant();
0154 }
0155 
0156