File indexing completed on 2024-06-23 05:15:11

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "mailtransport_export.h"
0010 #include "transportbase.h"
0011 #include "transporttype.h"
0012 
0013 #include <memory>
0014 
0015 class TransportPrivate;
0016 namespace QKeychain
0017 {
0018 class Job;
0019 }
0020 namespace MailTransport
0021 {
0022 class TransportType;
0023 
0024 /**
0025   Represents the settings of a specific mail transport.
0026 
0027   To create a new empty Transport object, use TransportManager::createTransport().
0028 
0029   Initialize an empty Transport object by calling the set...() methods defined in
0030   kcfg-generated TransportBase, and in this class.
0031 */
0032 class MAILTRANSPORT_EXPORT Transport : public TransportBase
0033 {
0034     Q_OBJECT
0035 
0036     Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
0037     Q_PROPERTY(TransportType transportType READ transportType NOTIFY transportTypeChanged)
0038 
0039     friend class TransportManager;
0040     friend class TransportManagerPrivate;
0041 
0042 public:
0043     /**
0044       Destructor
0045     */
0046     ~Transport() override;
0047 
0048     using List = QList<Transport *>;
0049     using Id = int;
0050 
0051     /**
0052       Returns true if this transport is valid, ie. has all necessary data set.
0053     */
0054     [[nodiscard]] Q_INVOKABLE bool isValid() const;
0055 
0056     /**
0057       Returns the password of this transport.
0058     */
0059     [[nodiscard]] QString password() const;
0060 
0061     /**
0062       Sets the password of this transport.
0063       @param passwd The new password.
0064     */
0065     void setPassword(const QString &passwd);
0066 
0067     /**
0068       Makes sure the transport has a unique name.  Adds #1, #2, #3 etc. if
0069       necessary.
0070       @since 4.4
0071     */
0072     void forceUniqueName();
0073 
0074     /**
0075       This function synchronizes the password of this transport with the
0076       password of the transport with the same ID that is managed by the
0077       transport manager. This is only useful for cloned transports, since
0078       their passwords don't automatically get updated when calling
0079       TransportManager::loadPasswordsAsync() or TransportManager::loadPasswords().
0080 
0081       @sa clone()
0082       @since 4.2
0083     */
0084     void updatePasswordState();
0085 
0086     /**
0087       Returns true if all settings have been loaded.
0088       This is the way to find out if the password has already been loaded
0089       from the wallet.
0090     */
0091     [[nodiscard]] bool isComplete() const;
0092 
0093     /**
0094       Returns a string representation of the authentication type.
0095     */
0096     [[nodiscard]] QString authenticationTypeString() const;
0097 
0098     /**
0099       Returns a string representation of the authentication type.
0100       Convenience function when there isn't a Transport object
0101       instantiated.
0102 
0103       @since 4.5
0104     */
0105     [[nodiscard]] static QString authenticationTypeString(int type);
0106 
0107     /**
0108       Returns a deep copy of this Transport object which will no longer be
0109       automatically updated. Use this if you need to store a Transport object
0110       over a longer time. However it is recommended to store transport identifiers
0111       instead if possible.
0112 
0113       @sa updatePasswordState()
0114     */
0115     Transport *clone() const;
0116 
0117     /**
0118       Returns the type of this transport.
0119       @see TransportType.
0120       @since 4.4
0121     */
0122     [[nodiscard]] TransportType transportType() const;
0123 
0124 protected:
0125     /**
0126       Creates a Transport object. Should only be used by TransportManager.
0127       @param cfgGroup The KConfig group to read its data from.
0128     */
0129     explicit Transport(const QString &cfgGroup);
0130 
0131     void usrRead() override;
0132     bool usrSave() override;
0133 
0134     /**
0135       Returns true if the password was not stored in the wallet.
0136     */
0137     [[nodiscard]] bool needsWalletMigration() const;
0138 
0139     /**
0140       Try to migrate the password from the config file to the wallet.
0141     */
0142     void migrateToWallet();
0143 
0144 private Q_SLOTS:
0145 
0146     // Used by our friend, TransportManager
0147     void readPassword();
0148 
0149 Q_SIGNALS:
0150     /**
0151       Emitted when passwords have been loaded from QKeyChain.
0152     */
0153     void passwordLoaded();
0154     /**
0155      * Emitted when the password is changed
0156      */
0157     void passwordChanged();
0158     /**
0159      * Emitted when the transport type is changed
0160      */
0161     void transportTypeChanged();
0162 
0163 private:
0164     void readTransportPasswordFinished(QKeychain::Job *baseJob);
0165     void loadPassword();
0166 
0167 private:
0168     std::unique_ptr<TransportPrivate> const d;
0169 };
0170 } // namespace MailTransport