File indexing completed on 2024-04-21 15:02:48

0001 /*
0002     Persons Model
0003     SPDX-FileCopyrightText: 2012 Martin Klapetek <martin.klapetek@gmail.com>
0004     SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0005     SPDX-FileCopyrightText: 2013 David Edmundson <davidedmundson@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.1-or-later
0008 */
0009 
0010 #ifndef PERSONS_MODEL_H
0011 #define PERSONS_MODEL_H
0012 
0013 #include <kpeople/kpeople_export.h>
0014 
0015 #include "global.h"
0016 
0017 #include <QAbstractItemModel>
0018 
0019 namespace KPeople
0020 {
0021 class ContactItem;
0022 class PersonItem;
0023 class MetaContact;
0024 class PersonsModelPrivate;
0025 
0026 /**
0027  * This class creates a model of all known contacts from all sources
0028  * Contacts are represented as a tree where the top level represents a "person" which is an
0029  * amalgamation of all the sub-contacts
0030  *
0031  * @since 5.8
0032  */
0033 class KPEOPLE_EXPORT PersonsModel : public QAbstractItemModel
0034 {
0035     Q_OBJECT
0036     /** specifies whether the model has already been initialized */
0037     Q_PROPERTY(bool isInitialized READ isInitialized NOTIFY modelInitialized)
0038 public:
0039     enum Role {
0040         FormattedNameRole = Qt::DisplayRole, // QString best name for this person
0041         PhotoRole = Qt::DecorationRole, // QPixmap best photo for this person
0042         PersonUriRole = Qt::UserRole, // QString ID of this person
0043         PersonVCardRole, // AbstractContact::Ptr
0044         ContactsVCardRole, // AbstractContact::List (FIXME or map?)
0045 
0046         GroupsRole, /// groups QStringList
0047         PhoneNumberRole,
0048         PhotoImageProviderUri, ///< Provide a URL to use with QtQuick's Image.source, similar to the Photo Role. @since 5.93
0049 
0050         UserRole = Qt::UserRole + 0x1000, ///< in case it's needed to extend, use this one to start from
0051     };
0052     Q_ENUM(Role)
0053 
0054     explicit PersonsModel(QObject *parent = nullptr);
0055 
0056     ~PersonsModel() override;
0057 
0058     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0059     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0060     QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
0061     QModelIndex parent(const QModelIndex &index) const override;
0062     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0063     QHash<int, QByteArray> roleNames() const override;
0064 
0065     /** @returns the index for a given @p personUri */
0066     QModelIndex indexForPersonUri(const QString &personUri) const;
0067 
0068     /** Returns if all the backends have been initialized yet. */
0069     bool isInitialized() const;
0070 
0071     /** Helper class to ease model access through QML */
0072     Q_SCRIPTABLE QVariant get(int row, int role);
0073 
0074     /**
0075      * Makes it possible to access custom properties that are not available to the model
0076      *
0077      * @returns the property for the contact at @p index defined by the @p key
0078      */
0079     QVariant contactCustomProperty(const QModelIndex &index, const QString &key) const;
0080 
0081 Q_SIGNALS:
0082     /** Will emit when the model is finally initialized. @p success will specify if it succeeded */
0083     void modelInitialized(bool success);
0084 
0085 private:
0086     Q_DISABLE_COPY(PersonsModel)
0087 
0088     QScopedPointer<PersonsModelPrivate> const d_ptr;
0089     Q_DECLARE_PRIVATE(PersonsModel)
0090 };
0091 }
0092 
0093 #endif // PERSONS_MODEL_H