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->mapToSource(d->mIdentityProxyModel->index(currentIndex(), KIdentityManagementCore::IdentityTreeModel::IdentityNameRole))
81 .data()
82 .toString();
83}
84
86{
87 return d->mIdentityProxyModel->mapToSource(d->mIdentityProxyModel->index(currentIndex(), KIdentityManagementCore::IdentityTreeModel::UoidRole))
88 .data()
89 .toInt();
90}
91
93{
94 return currentIdentity() == d->mIdentityModel->identityManager()->defaultIdentity().uoid();
95}
96
98{
99 setCurrentIdentity(identity.uoid());
100}
101
103{
104 if (name.isEmpty()) {
105 return;
106 }
107
108 const int idx = d->mIdentityModel->identityManager()->identities().indexOf(name);
109 const int newIndex = d->mIdentityProxyModel->mapFromSource(d->mIdentityModel->index(idx, KIdentityManagementCore::IdentityTreeModel::UoidRole)).row();
110
111 if (newIndex < 0) {
112 Q_EMIT invalidIdentity();
113 return;
114 }
115
116 if (newIndex == currentIndex()) {
117 return;
118 }
119
120 blockSignals(true); // just in case Qt gets fixed to emit activated() here
121 setCurrentIndex(newIndex);
122 blockSignals(false);
123
124 slotEmitChanged(newIndex);
125}
126
128{
129 if (uoid == 0) {
130 return;
131 }
132 const int idx = d->mIdentityModel->uoidIndex(uoid);
133 const int newIndex = d->mIdentityProxyModel->mapFromSource(d->mIdentityModel->index(idx, KIdentityManagementCore::IdentityTreeModel::UoidRole)).row();
134
135 if (newIndex < 0) {
136 Q_EMIT invalidIdentity();
137 return;
138 }
139 if (newIndex == currentIndex()) {
140 return;
141 }
142
143 blockSignals(true); // just in case Qt gets fixed to emit activated() here
144 setCurrentIndex(newIndex);
145 blockSignals(false);
146
147 slotEmitChanged(newIndex);
148}
149
151{
152 const uint oldIdentity = currentIdentity();
153
154 const int idx = d->mIdentityModel->uoidIndex(oldIdentity);
155 const int newIndex = d->mIdentityProxyModel->mapFromSource(d->mIdentityModel->index(idx, KIdentityManagementCore::IdentityTreeModel::UoidRole)).row();
156
157 blockSignals(true);
158 setCurrentIndex(newIndex < 0 ? 0 : newIndex);
159 blockSignals(false);
160
161 slotUpdateTooltip(currentIdentity());
162
163 if (newIndex < 0) {
164 // apparently our oldIdentity got deleted:
165 slotEmitChanged(currentIndex());
166 }
167}
168
169void IdentityCombo::slotEmitChanged(int idx)
170{
171 Q_EMIT identityChanged(d->mIdentityModel->identityUoid(idx));
172}
173
174void IdentityCombo::slotUpdateTooltip(uint uoid)
175{
176 setToolTip(d->mIdentityModel->identityManager()->identityForUoid(uoid).fullEmailAddr());
177}
178
180{
181 return d->mIdentityModel->identityManager();
182}
183
184void IdentityCombo::setShowDefault(bool showDefault)
185{
186 d->mIdentityModel->setShowDefault(showDefault);
187}
188
189#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:73
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 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 Jul 19 2024 11:59:49 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.