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

0001 /*
0002   SPDX-FileCopyrightText: 2002 Marc Mutz <mutz@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 /**
0007   @file
0008   This file is part of the API for handling user identities and defines the
0009   IdentityCombo class.
0010 
0011   @brief
0012   A combo box that always shows the up-to-date identity list.
0013 
0014   @author Marc Mutz \<mutz@kde.org\>
0015  */
0016 
0017 #include "identitycombo.h"
0018 #include "identity.h"
0019 #include "identitymanager.h"
0020 
0021 #include <KLocalizedString>
0022 
0023 #include <cassert>
0024 
0025 using namespace KIdentityManagementWidgets;
0026 using namespace KIdentityManagementCore;
0027 /**
0028   IdentityComboPrivate class that helps to provide binary compatibility between releases.
0029   @internal
0030 */
0031 //@cond PRIVATE
0032 class KIdentityManagementWidgets::IdentityComboPrivate
0033 {
0034 public:
0035     IdentityComboPrivate(KIdentityManagementCore::IdentityManager *manager, IdentityCombo *qq)
0036         : mIdentityManager(manager)
0037         , q(qq)
0038     {
0039     }
0040 
0041     void reloadCombo();
0042     void reloadUoidList();
0043 
0044     QList<uint> mUoidList;
0045     KIdentityManagementCore::IdentityManager *const mIdentityManager;
0046     IdentityCombo *const q;
0047     bool showDefault = false;
0048 };
0049 
0050 void KIdentityManagementWidgets::IdentityComboPrivate::reloadCombo()
0051 {
0052     QStringList identities;
0053     identities.reserve(mIdentityManager->identities().count());
0054     KIdentityManagementCore::IdentityManager::ConstIterator it;
0055     KIdentityManagementCore::IdentityManager::ConstIterator end(mIdentityManager->end());
0056     for (it = mIdentityManager->begin(); it != end; ++it) {
0057         if (showDefault && it->isDefault()) {
0058             identities << QString(it->identityName() + i18nc("Default identity", " (default)"));
0059         } else {
0060             identities << it->identityName();
0061         }
0062     }
0063     // the IM should prevent this from happening:
0064     assert(!identities.isEmpty());
0065     q->clear();
0066     q->addItems(identities);
0067 }
0068 
0069 void KIdentityManagementWidgets::IdentityComboPrivate::reloadUoidList()
0070 {
0071     mUoidList.clear();
0072     KIdentityManagementCore::IdentityManager::ConstIterator it;
0073     KIdentityManagementCore::IdentityManager::ConstIterator end(mIdentityManager->end());
0074     for (it = mIdentityManager->begin(); it != end; ++it) {
0075         mUoidList << (*it).uoid();
0076     }
0077 }
0078 
0079 //@endcond
0080 
0081 IdentityCombo::IdentityCombo(IdentityManager *manager, QWidget *parent)
0082     : QComboBox(parent)
0083     , d(new KIdentityManagementWidgets::IdentityComboPrivate(manager, this))
0084 {
0085     d->reloadCombo();
0086     d->reloadUoidList();
0087     connect(this, &IdentityCombo::activated, this, &IdentityCombo::slotEmitChanged);
0088     connect(this, &IdentityCombo::identityChanged, this, &IdentityCombo::slotUpdateTooltip);
0089     connect(manager, &KIdentityManagementCore::IdentityManager::identitiesWereChanged, this, &IdentityCombo::slotIdentityManagerChanged);
0090     connect(manager, &KIdentityManagementCore::IdentityManager::deleted, this, &IdentityCombo::identityDeleted);
0091     slotUpdateTooltip(currentIdentity());
0092 }
0093 
0094 IdentityCombo::~IdentityCombo() = default;
0095 
0096 QString IdentityCombo::currentIdentityName() const
0097 {
0098     return d->mIdentityManager->identities().at(currentIndex());
0099 }
0100 
0101 uint IdentityCombo::currentIdentity() const
0102 {
0103     return d->mUoidList.at(currentIndex());
0104 }
0105 
0106 bool IdentityCombo::isDefaultIdentity() const
0107 {
0108     return d->mUoidList.at(currentIndex()) == d->mIdentityManager->defaultIdentity().uoid();
0109 }
0110 
0111 void IdentityCombo::setCurrentIdentity(const Identity &identity)
0112 {
0113     setCurrentIdentity(identity.uoid());
0114 }
0115 
0116 void IdentityCombo::setCurrentIdentity(const QString &name)
0117 {
0118     if (name.isEmpty()) {
0119         return;
0120     }
0121     const int idx = d->mIdentityManager->identities().indexOf(name);
0122     if (idx < 0) {
0123         Q_EMIT invalidIdentity();
0124         return;
0125     }
0126 
0127     if (idx == currentIndex()) {
0128         return;
0129     }
0130 
0131     blockSignals(true); // just in case Qt gets fixed to emit activated() here
0132     setCurrentIndex(idx);
0133     blockSignals(false);
0134 
0135     slotEmitChanged(idx);
0136 }
0137 
0138 void IdentityCombo::setCurrentIdentity(uint uoid)
0139 {
0140     if (uoid == 0) {
0141         return;
0142     }
0143     const int idx = d->mUoidList.indexOf(uoid);
0144     if (idx < 0) {
0145         Q_EMIT invalidIdentity();
0146         return;
0147     }
0148     if (idx == currentIndex()) {
0149         return;
0150     }
0151 
0152     blockSignals(true); // just in case Qt gets fixed to emit activated() here
0153     setCurrentIndex(idx);
0154     blockSignals(false);
0155 
0156     slotEmitChanged(idx);
0157 }
0158 
0159 void IdentityCombo::slotIdentityManagerChanged()
0160 {
0161     uint oldIdentity = d->mUoidList.at(currentIndex());
0162 
0163     d->reloadUoidList();
0164     int idx = d->mUoidList.indexOf(oldIdentity);
0165 
0166     blockSignals(true);
0167     d->reloadCombo();
0168     setCurrentIndex(idx < 0 ? 0 : idx);
0169     blockSignals(false);
0170 
0171     slotUpdateTooltip(currentIdentity());
0172 
0173     if (idx < 0) {
0174         // apparently our oldIdentity got deleted:
0175         slotEmitChanged(currentIndex());
0176     }
0177 }
0178 
0179 void IdentityCombo::slotEmitChanged(int idx)
0180 {
0181     Q_EMIT identityChanged(d->mUoidList.at(idx));
0182 }
0183 
0184 void IdentityCombo::slotUpdateTooltip(uint uoid)
0185 {
0186     setToolTip(d->mIdentityManager->identityForUoid(uoid).fullEmailAddr());
0187 }
0188 
0189 IdentityManager *IdentityCombo::identityManager() const
0190 {
0191     return d->mIdentityManager;
0192 }
0193 
0194 void IdentityCombo::setShowDefault(bool showDefault)
0195 {
0196     if (d->showDefault != showDefault) {
0197         d->showDefault = showDefault;
0198         d->reloadCombo();
0199     }
0200 }
0201 
0202 #include "moc_identitycombo.cpp"