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

0001 /*
0002     SPDX-FileCopyrightText: 2013 David Edmundson <davidedmundson@kde.org>
0003     SPDX-FileCopyrightText: 2013 Martin Klapetek <mklapetek@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #ifndef PERSONMANAGER_H
0009 #define PERSONMANAGER_H
0010 
0011 #include <QMultiHash>
0012 #include <QObject>
0013 #include <QStringList>
0014 
0015 #include <QSqlDatabase>
0016 
0017 #include <kpeople/kpeople_export.h>
0018 
0019 /**
0020  * This is a private internal class that manages all the internal mapping of contacts <---> persons
0021  * It stores the connection to the database as well as signals communicating with other clients
0022  *
0023  * It is a singleton.
0024  *
0025  */
0026 
0027 class KPEOPLE_EXPORT PersonManager : public QObject
0028 {
0029     Q_OBJECT
0030 
0031 public:
0032     /**
0033      * Create or return a singleton instance of the PersonManager
0034      *
0035      * @databasePath the path to the database to be used.
0036      * If null the correct database is used.
0037      * This is for testing purposes only.
0038      */
0039     static PersonManager *instance(const QString &databasePath = QString());
0040 
0041     // DATA RETRIEVAL------------
0042 
0043     /** Returns a list of all known personIDs in the database*/
0044     QMultiHash<QString /*PersonUri*/, QString /*ContactUri*/> allPersons() const;
0045 
0046     /**
0047      * Returns the ID of a person associated with a given contact
0048      * If no person for that contact exists, an empty string is returned
0049      */
0050     QString personUriForContact(const QString &contactUri) const;
0051 
0052     /**
0053      * Returns a list of contactUris associated with a given person
0054      */
0055     QStringList contactsForPersonUri(const QString &personUri) const;
0056 
0057     /**
0058      * Creates a contact with the specified @p properties
0059      *
0060      * @since 5.62
0061      */
0062 
0063     bool addContact(const QVariantMap &properties);
0064 
0065 public Q_SLOTS:
0066     // merge all ids (person IDs and contactUris into a single person)
0067     // returns the ID that will be created
0068     // users should KPeople::mergeContacts from global.h
0069     QString mergeContacts(const QStringList &ids);
0070 
0071     // unmerge a contact. Either remove a contact from a given person or remove a person
0072     // users should KPeople::unmergeContact from global.h
0073     bool unmergeContact(const QString &id);
0074 
0075 Q_SIGNALS:
0076     void contactRemovedFromPerson(const QString &contactUri);
0077     void contactAddedToPerson(const QString &contactUri, const QString &newPersonUri);
0078 
0079 protected:
0080     explicit PersonManager(const QString &databasePath, QObject *parent = nullptr);
0081     ~PersonManager() override;
0082 
0083 private:
0084     QSqlDatabase m_db;
0085 };
0086 
0087 #endif // PERSONMANAGER_H