File indexing completed on 2024-05-19 05:17:27

0001 /*
0002     SPDX-FileCopyrightText: 2002 Marc Mutz <mutz@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "kidentitymanagementcore_export.h"
0010 
0011 #include <QList>
0012 #include <QObject>
0013 #include <QStringList>
0014 
0015 #include <memory>
0016 
0017 namespace KIdentityManagementCore
0018 {
0019 class IdentityManagerPrivate;
0020 class Identity;
0021 /**
0022  * @short Manages the list of identities.
0023  * @author Marc Mutz <mutz@kde.org>
0024  **/
0025 class KIDENTITYMANAGEMENTCORE_EXPORT IdentityManager : public QObject
0026 {
0027     Q_OBJECT
0028 public:
0029     /**
0030      * Create an identity manager, which loads the emailidentities file
0031      * to create identities.
0032      * @param readonly if true, no changes can be made to the identity manager
0033      * This means in particular that if there is no identity configured,
0034      * the default identity created here will not be saved.
0035      * It is assumed that a minimum of one identity is always present.
0036      */
0037     explicit IdentityManager(bool readonly = false, QObject *parent = nullptr, const char *name = nullptr);
0038     ~IdentityManager() override;
0039 
0040     /**
0041      * Creates or reuses the identity manager instance for this process.
0042      * It loads the emailidentities file to create identities.
0043      * This sets readonly to false, so you should create a separate instance
0044      * if you need it to be readonly.
0045      * @since 5.2.91
0046      */
0047     static IdentityManager *self();
0048 
0049 public:
0050     using Iterator = QList<Identity>::Iterator;
0051     using ConstIterator = QList<Identity>::ConstIterator;
0052 
0053     /**
0054      * Typedef for STL style iterator
0055      */
0056     using iterator = Iterator;
0057 
0058     /**
0059      * Typedef for STL style iterator
0060      */
0061     using const_iterator = ConstIterator;
0062 
0063     /** @return a unique name for a new identity based on @p name
0064      *  @param name the name of the base identity
0065      */
0066     [[nodiscard]] QString makeUnique(const QString &name) const;
0067 
0068     /** @return whether the @p name is unique
0069      *  @param name the name to be examined
0070      */
0071     [[nodiscard]] bool isUnique(const QString &name) const;
0072 
0073     /** Commit changes to disk and emit changed() if necessary. */
0074     void commit();
0075 
0076     /** Re-read the config from disk and forget changes. */
0077     void rollback();
0078 
0079     /** Store a new identity or modify an existing identity based on an
0080      *  independent identity object
0081      *  @param ident the identity to be saved
0082      */
0083     void saveIdentity(const Identity &ident);
0084 
0085     /** Check whether there are any unsaved changes. */
0086     [[nodiscard]] bool hasPendingChanges() const;
0087 
0088     /** @return the list of identities */
0089     [[nodiscard]] QStringList identities() const;
0090 
0091     /** Convenience method.
0092 
0093         @return the list of (shadow) identities, ie. the ones currently
0094         under configuration.
0095     */
0096     [[nodiscard]] QStringList shadowIdentities() const;
0097 
0098     /** Sort the identities by name (the default is always first). This
0099         operates on the @em shadow list, so you need to @ref commit for
0100         the changes to take effect.
0101     **/
0102     void sort();
0103 
0104     /** @return an identity whose address matches any in @p addresses
0105                 or @ref Identity::null if no such identity exists.
0106         @param addresses the string of addresses to scan for matches
0107     **/
0108     const Identity &identityForAddress(const QString &addresses) const;
0109 
0110     /** @return true if @p addressList contains any of our addresses,
0111                 false otherwise.
0112         @param addressList the addressList to examine
0113         @see #identityForAddress
0114     **/
0115     [[nodiscard]] bool thatIsMe(const QString &addressList) const;
0116 
0117     /** @return the identity with Unique Object Identifier (UOID) @p
0118                 uoid or @ref Identity::null if not found.
0119         @param uoid the Unique Object Identifier to find identity with
0120      **/
0121     const Identity &identityForUoid(uint uoid) const;
0122 
0123     /** Convenience method.
0124 
0125         @return the identity with Unique Object Identifier (UOID) @p
0126                 uoid or the default identity if not found.
0127         @param uoid the Unique Object Identifier to find identity with
0128     **/
0129     const Identity &identityForUoidOrDefault(uint uoid) const;
0130 
0131     /** @return the default identity */
0132     const Identity &defaultIdentity() const;
0133 
0134     /** Sets the identity with Unique Object Identifier (UOID) @p uoid
0135         to be new the default identity. As usual, use @ref commit to
0136         make this permanent.
0137 
0138         @param uoid the default identity to set
0139         @return false if an identity with UOID @p uoid was not found
0140     **/
0141     bool setAsDefault(uint uoid);
0142 
0143     /** @return the identity named @p identityName. This method returns a
0144         reference to the identity that can be modified. To let others
0145         see this change, use @ref commit.
0146         @param identityName the identity name to return modifiable reference
0147     **/
0148     Identity &modifyIdentityForName(const QString &identityName);
0149 
0150     /** @return the identity with Unique Object Identifier (UOID) @p uoid.
0151         This method returns a reference to the identity that can
0152         be modified. To let others see this change, use @ref commit.
0153     **/
0154     Identity &modifyIdentityForUoid(uint uoid);
0155 
0156     /** Removes the identity with name @p identityName
0157         Will return false if the identity is not found,
0158         or when one tries to remove the last identity.
0159         @param identityName the identity to remove
0160      **/
0161     [[nodiscard]] bool removeIdentity(const QString &identityName);
0162 
0163     /**
0164      * Removes the identity with name @p identityName
0165      * Will return @c false if the identity is not found, @c true otherwise.
0166      *
0167      * @note In opposite to removeIdentity, this method allows to remove the
0168      *       last remaining identity.
0169      *
0170      * @since 4.6
0171      */
0172     [[nodiscard]] bool removeIdentityForced(const QString &identityName);
0173 
0174     ConstIterator begin() const;
0175     ConstIterator end() const;
0176     /// Iterator used by the configuration dialog, which works on a separate list
0177     /// of identities, for modification. Changes are made effective by commit().
0178     Iterator modifyBegin();
0179     Iterator modifyEnd();
0180 
0181     Identity &newFromScratch(const QString &name);
0182     Identity &newFromControlCenter(const QString &name);
0183     Identity &newFromExisting(const Identity &other, const QString &name = QString());
0184 
0185     /** Returns the list of all email addresses (only name@host) from all
0186         identities */
0187     [[nodiscard]] QStringList allEmails() const;
0188 
0189 Q_SIGNALS:
0190     /** Emitted whenever a commit changes any configure option */
0191     void changed();
0192     void identitiesWereChanged();
0193     /** Emitted whenever the identity with Unique Object Identifier
0194         (UOID) @p uoid changed. Useful for more fine-grained change
0195         notifications than what is possible with the standard @ref
0196         changed() signal. */
0197     void changed(uint uoid);
0198     /** Emitted whenever the identity @p ident changed. Useful for more
0199         fine-grained change notifications than what is possible with the
0200         standard @ref changed() signal. */
0201     void changed(const KIdentityManagementCore::Identity &ident);
0202     void identityChanged(const KIdentityManagementCore::Identity &ident);
0203     /** Emitted on @ref commit() for each deleted identity. At the time
0204         this signal is emitted, the identity does still exist and can be
0205         retrieved by @ref identityForUoid() if needed */
0206     void deleted(uint uoid);
0207     /** Emitted on @ref commit() for each new identity */
0208     void added(const KIdentityManagementCore::Identity &ident);
0209 
0210     void needToReloadIdentitySettings();
0211 
0212     void identitiesChanged(const QString &id);
0213 
0214 protected:
0215     /**
0216      * This is called when no identity has been defined, so we need to
0217      * create a default one. The parameters are filled with some default
0218      * values from KUser, but reimplementations of this method can give
0219      * them another value.
0220      */
0221     virtual void createDefaultIdentity(QString & /*fullName*/, QString & /*emailAddress*/);
0222 
0223 protected Q_SLOTS:
0224     void slotRollback();
0225 
0226 private:
0227     //@cond PRIVATE
0228     friend class IdentityManagerPrivate;
0229     std::unique_ptr<IdentityManagerPrivate> const d;
0230     //@endcond
0231     Q_PRIVATE_SLOT(d, void slotIdentitiesChanged(const QString &id))
0232 };
0233 } // namespace