KIdentityManagement

identitycombo.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2002 Marc Mutz <mutz@kde.org>
3 SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7/**
8 @file
9 This file is part of the API for handling user identities and defines the
10 IdentityCombo class.
11
12 @brief
13 A combo box that always shows the up-to-date identity list.
14
15 @author Marc Mutz <mutz@kde.org>
16 */
17
18#include "identitycombo.h"
19#include "identity.h"
20#include "identitymanager.h"
21#include "identitytreemodel.h"
22#include "identitytreesortproxymodel.h"
23
24#include <KLocalizedString>
25
26#include <cassert>
27
28using namespace KIdentityManagementWidgets;
29using namespace KIdentityManagementCore;
30/**
31 IdentityComboPrivate class that helps to provide binary compatibility between releases.
32 @internal
33*/
34//@cond PRIVATE
35class KIdentityManagementWidgets::IdentityComboPrivate
36{
37public:
38 IdentityComboPrivate(KIdentityManagementCore::IdentityManager *manager, IdentityCombo *qq)
39 : mIdentityModel(new KIdentityManagementCore::IdentityTreeModel(manager, qq))
40 , mIdentityProxyModel(new KIdentityManagementCore::IdentityTreeSortProxyModel(qq))
41 , q(qq)
42 {
43 mIdentityProxyModel->setSourceModel(mIdentityModel);
44 q->setModel(mIdentityProxyModel);
45 }
46
47 KIdentityManagementCore::IdentityTreeModel *const mIdentityModel;
48 KIdentityManagementCore::IdentityTreeSortProxyModel *const mIdentityProxyModel;
49 IdentityCombo *const q;
50};
51
52//@endcond
53
55 : QComboBox(parent)
56 , d(new KIdentityManagementWidgets::IdentityComboPrivate(manager, this))
57{
58 connect(manager, &KIdentityManagementCore::IdentityManager::identitiesWereChanged, this, &IdentityCombo::slotIdentityManagerChanged);
59 connect(manager, &KIdentityManagementCore::IdentityManager::deleted, this, &IdentityCombo::identityDeleted);
60 connect(this, &IdentityCombo::activated, this, &IdentityCombo::slotEmitChanged);
61 connect(this, &IdentityCombo::identityChanged, this, &IdentityCombo::slotUpdateTooltip);
62 setModelColumn(KIdentityManagementCore::IdentityTreeModel::DisplayIdentityNameRole);
63 slotUpdateTooltip(currentIdentity());
64}
65
66IdentityCombo::~IdentityCombo() = default;
67
68IdentityActivitiesAbstract *IdentityCombo::identityActivitiesAbstract() const
69{
70 return d->mIdentityProxyModel->identityActivitiesAbstract();
71}
72
73void IdentityCombo::setIdentityActivitiesAbstract(IdentityActivitiesAbstract *newIdentityActivitiesAbstract)
74{
75 d->mIdentityProxyModel->setIdentityActivitiesAbstract(newIdentityActivitiesAbstract);
76}
77
79{
80 return d->mIdentityProxyModel->enablePlasmaActivities();
81}
82
83void IdentityCombo::setEnablePlasmaActivities(bool newEnablePlasmaActivities)
84{
85 d->mIdentityProxyModel->setEnablePlasmaActivities(newEnablePlasmaActivities);
86}
87
89{
90 return d->mIdentityProxyModel->mapToSource(d->mIdentityProxyModel->index(currentIndex(), KIdentityManagementCore::IdentityTreeModel::IdentityNameRole))
91 .data()
92 .toString();
93}
94
96{
97 return d->mIdentityProxyModel->mapToSource(d->mIdentityProxyModel->index(currentIndex(), KIdentityManagementCore::IdentityTreeModel::UoidRole))
98 .data()
99 .toInt();
100}
101
103{
104 return currentIdentity() == d->mIdentityModel->identityManager()->defaultIdentity().uoid();
105}
106
108{
109 setCurrentIdentity(identity.uoid());
110}
111
113{
114 if (name.isEmpty()) {
115 return;
116 }
117
118 const int idx = d->mIdentityModel->identityManager()->identities().indexOf(name);
119 const int newIndex = d->mIdentityProxyModel->mapFromSource(d->mIdentityModel->index(idx, KIdentityManagementCore::IdentityTreeModel::UoidRole)).row();
120
121 if (newIndex < 0) {
122 Q_EMIT invalidIdentity();
123 return;
124 }
125
126 if (newIndex == currentIndex()) {
127 return;
128 }
129
130 blockSignals(true); // just in case Qt gets fixed to emit activated() here
131 setCurrentIndex(newIndex);
132 blockSignals(false);
133
134 slotEmitChanged(newIndex);
135}
136
138{
139 if (uoid == 0) {
140 return;
141 }
142 const int idx = d->mIdentityModel->uoidIndex(uoid);
143 const int newIndex = d->mIdentityProxyModel->mapFromSource(d->mIdentityModel->index(idx, KIdentityManagementCore::IdentityTreeModel::UoidRole)).row();
144
145 if (newIndex < 0) {
146 Q_EMIT invalidIdentity();
147 return;
148 }
149 if (newIndex == currentIndex()) {
150 return;
151 }
152
153 blockSignals(true); // just in case Qt gets fixed to emit activated() here
154 setCurrentIndex(newIndex);
155 blockSignals(false);
156
157 slotEmitChanged(newIndex);
158}
159
161{
162 const uint oldIdentity = currentIdentity();
163
164 const int idx = d->mIdentityModel->uoidIndex(oldIdentity);
165 const int newIndex = d->mIdentityProxyModel->mapFromSource(d->mIdentityModel->index(idx, KIdentityManagementCore::IdentityTreeModel::UoidRole)).row();
166
167 blockSignals(true);
168 setCurrentIndex(newIndex < 0 ? 0 : newIndex);
169 blockSignals(false);
170
171 slotUpdateTooltip(currentIdentity());
172
173 if (newIndex < 0) {
174 // apparently our oldIdentity got deleted:
175 slotEmitChanged(currentIndex());
176 }
177}
178
179void IdentityCombo::slotEmitChanged(int idx)
180{
181 Q_EMIT identityChanged(d->mIdentityModel->identityUoid(idx));
182}
183
184void IdentityCombo::slotUpdateTooltip(uint uoid)
185{
186 setToolTip(d->mIdentityModel->identityManager()->identityForUoid(uoid).fullEmailAddr());
187}
188
190{
191 return d->mIdentityModel->identityManager();
192}
193
194void IdentityCombo::setShowDefault(bool showDefault)
195{
196 d->mIdentityModel->setShowDefault(showDefault);
197}
198
199#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:74
A combo box that always shows the up-to-date identity list.
void setIdentityActivitiesAbstract(KIdentityManagementCore::IdentityActivitiesAbstract *newIdentityActivitiesAbstract)
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 setEnablePlasmaActivities(bool newEnablePlasmaActivities)
void setShowDefault(bool showDefault)
Show (default) on the default identity. By default this behavior is disabled.
KIdentityManagementCore::IdentityActivitiesAbstract * identityActivitiesAbstract() const
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.
void activated(int index)
void setModelColumn(int visibleColumn)
Q_EMITQ_EMIT
bool blockSignals(bool block)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QChar * data()
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 Fri Dec 6 2024 12:11:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.