KIdentityManagement

identitycombo.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2002 Marc Mutz <mutz@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6/**
7 @file
8 This file is part of the API for handling user identities and defines the
9 IdentityCombo class.
10
11 @brief
12 A combo box that always shows the up-to-date identity list.
13
14 @author Marc Mutz <mutz@kde.org>
15 */
16
17#include "identitycombo.h"
18#include "identity.h"
19#include "identitymanager.h"
20
21#include <KLocalizedString>
22
23#include <cassert>
24
25using namespace KIdentityManagementWidgets;
26using namespace KIdentityManagementCore;
27/**
28 IdentityComboPrivate class that helps to provide binary compatibility between releases.
29 @internal
30*/
31//@cond PRIVATE
32class KIdentityManagementWidgets::IdentityComboPrivate
33{
34public:
35 IdentityComboPrivate(KIdentityManagementCore::IdentityManager *manager, IdentityCombo *qq)
36 : mIdentityManager(manager)
37 , q(qq)
38 {
39 }
40
41 void reloadCombo();
42 void reloadUoidList();
43
44 QList<uint> mUoidList;
45 KIdentityManagementCore::IdentityManager *const mIdentityManager;
46 IdentityCombo *const q;
47 bool showDefault = false;
48};
49
50void KIdentityManagementWidgets::IdentityComboPrivate::reloadCombo()
51{
52 QStringList identities;
53 identities.reserve(mIdentityManager->identities().count());
54 KIdentityManagementCore::IdentityManager::ConstIterator it;
55 KIdentityManagementCore::IdentityManager::ConstIterator end(mIdentityManager->end());
56 for (it = mIdentityManager->begin(); it != end; ++it) {
57 if (showDefault && it->isDefault()) {
58 identities << QString(it->identityName() + i18nc("Default identity", " (default)"));
59 } else {
60 identities << it->identityName();
61 }
62 }
63 // the IM should prevent this from happening:
64 assert(!identities.isEmpty());
65 q->clear();
66 q->addItems(identities);
67}
68
69void KIdentityManagementWidgets::IdentityComboPrivate::reloadUoidList()
70{
71 mUoidList.clear();
72 KIdentityManagementCore::IdentityManager::ConstIterator it;
73 KIdentityManagementCore::IdentityManager::ConstIterator end(mIdentityManager->end());
74 for (it = mIdentityManager->begin(); it != end; ++it) {
75 mUoidList << (*it).uoid();
76 }
77}
78
79//@endcond
80
82 : QComboBox(parent)
83 , d(new KIdentityManagementWidgets::IdentityComboPrivate(manager, this))
84{
85 d->reloadCombo();
86 d->reloadUoidList();
87 connect(this, &IdentityCombo::activated, this, &IdentityCombo::slotEmitChanged);
88 connect(this, &IdentityCombo::identityChanged, this, &IdentityCombo::slotUpdateTooltip);
89 connect(manager, &KIdentityManagementCore::IdentityManager::identitiesWereChanged, this, &IdentityCombo::slotIdentityManagerChanged);
90 connect(manager, &KIdentityManagementCore::IdentityManager::deleted, this, &IdentityCombo::identityDeleted);
91 slotUpdateTooltip(currentIdentity());
92}
93
94IdentityCombo::~IdentityCombo() = default;
95
97{
98 return d->mIdentityManager->identities().at(currentIndex());
99}
100
102{
103 return d->mUoidList.at(currentIndex());
104}
105
107{
108 return d->mUoidList.at(currentIndex()) == d->mIdentityManager->defaultIdentity().uoid();
109}
110
112{
113 setCurrentIdentity(identity.uoid());
114}
115
117{
118 if (name.isEmpty()) {
119 return;
120 }
121 const int idx = d->mIdentityManager->identities().indexOf(name);
122 if (idx < 0) {
123 Q_EMIT invalidIdentity();
124 return;
125 }
126
127 if (idx == currentIndex()) {
128 return;
129 }
130
131 blockSignals(true); // just in case Qt gets fixed to emit activated() here
132 setCurrentIndex(idx);
133 blockSignals(false);
134
135 slotEmitChanged(idx);
136}
137
139{
140 if (uoid == 0) {
141 return;
142 }
143 const int idx = d->mUoidList.indexOf(uoid);
144 if (idx < 0) {
145 Q_EMIT invalidIdentity();
146 return;
147 }
148 if (idx == currentIndex()) {
149 return;
150 }
151
152 blockSignals(true); // just in case Qt gets fixed to emit activated() here
153 setCurrentIndex(idx);
154 blockSignals(false);
155
156 slotEmitChanged(idx);
157}
158
160{
161 uint oldIdentity = d->mUoidList.at(currentIndex());
162
163 d->reloadUoidList();
164 int idx = d->mUoidList.indexOf(oldIdentity);
165
166 blockSignals(true);
167 d->reloadCombo();
168 setCurrentIndex(idx < 0 ? 0 : idx);
169 blockSignals(false);
170
171 slotUpdateTooltip(currentIdentity());
172
173 if (idx < 0) {
174 // apparently our oldIdentity got deleted:
175 slotEmitChanged(currentIndex());
176 }
177}
178
179void IdentityCombo::slotEmitChanged(int idx)
180{
181 Q_EMIT identityChanged(d->mUoidList.at(idx));
182}
183
184void IdentityCombo::slotUpdateTooltip(uint uoid)
185{
186 setToolTip(d->mIdentityManager->identityForUoid(uoid).fullEmailAddr());
187}
188
190{
191 return d->mIdentityManager;
192}
193
194void IdentityCombo::setShowDefault(bool showDefault)
195{
196 if (d->showDefault != showDefault) {
197 d->showDefault = showDefault;
198 d->reloadCombo();
199 }
200}
201
202#include "moc_identitycombo.cpp"
Manages the list of identities.
void deleted(uint uoid)
Emitted on commit() for each deleted identity.
User identity information.
Definition identity.h:72
A combo box that always shows the up-to-date identity list.
void identityChanged(KIdentityManagementCore::Identity::Id uoid)
Really emitted whenever the current identity changes.
bool isDefaultIdentity() const
Return whether the current identity is the default identity.
void setShowDefault(bool showDefault)
Show (default) on the default identity. By default this behavior is disabled.
QString currentIdentityName() const
Return the current identity name.
KIdentityManagementCore::IdentityManager * identityManager() const
Returns the IdentityManager used in this combo box.
IdentityCombo(KIdentityManagementCore::IdentityManager *manager, QWidget *parent=nullptr)
IdentityCombo contructor.
void setCurrentIdentity(const KIdentityManagementCore::Identity &identity)
Set the current identity.
void slotIdentityManagerChanged()
Connected to IdentityManager::changed(). Reloads the list of identities.
KIdentityManagementCore::Identity::Id currentIdentity() const
Return the current identity id.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
const QList< QKeySequence > & end()
void activated(int index)
bool isEmpty() const const
void reserve(qsizetype size)
Q_EMITQ_EMIT
bool blockSignals(bool block)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
const QChar at(qsizetype position) const const
bool isEmpty() const const
void setToolTip(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:09 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.