1 | /* |
2 | Copyright (c) 2002 Marc Mutz <mutz@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | /** |
20 | @file |
21 | This file is part of the API for handling user identities and defines the |
22 | IdentityCombo class. |
23 | |
24 | @brief |
25 | A combo box that always shows the up-to-date identity list. |
26 | |
27 | @author Marc Mutz \<mutz@kde.org\> |
28 | */ |
29 | |
30 | #include "identitycombo.h" |
31 | #include "identity.h" |
32 | #include "identitymanager.h" |
33 | |
34 | #include <klocalizedstring.h> |
35 | |
36 | #include <assert.h> |
37 | |
38 | using namespace KPIMIdentities; |
39 | |
40 | /** |
41 | Private class that helps to provide binary compatibility between releases. |
42 | @internal |
43 | */ |
44 | //@cond PRIVATE |
45 | class KPIMIdentities::IdentityCombo::Private |
46 | { |
47 | }; |
48 | //@endcond |
49 | |
50 | IdentityCombo::IdentityCombo( IdentityManager *manager, QWidget *parent ) |
51 | : QComboBox( parent ), mIdentityManager( manager ), d( 0 ) |
52 | { |
53 | reloadCombo(); |
54 | reloadUoidList(); |
55 | connect( this, SIGNAL(activated(int)), SLOT(slotEmitChanged(int)) ); |
56 | connect( this, SIGNAL(identityChanged(uint)), this, SLOT(slotUpdateTooltip(uint)) ); |
57 | connect( manager, SIGNAL(changed()), |
58 | SLOT(slotIdentityManagerChanged()) ); |
59 | slotUpdateTooltip( currentIdentity() ); |
60 | } |
61 | |
62 | IdentityCombo::~IdentityCombo() |
63 | { |
64 | delete d; |
65 | } |
66 | |
67 | QString IdentityCombo::currentIdentityName() const |
68 | { |
69 | return mIdentityManager->identities()[ currentIndex()]; |
70 | } |
71 | |
72 | uint IdentityCombo::currentIdentity() const |
73 | { |
74 | return mUoidList[ currentIndex()]; |
75 | } |
76 | |
77 | void IdentityCombo::setCurrentIdentity( const Identity &identity ) |
78 | { |
79 | setCurrentIdentity( identity.uoid() ); |
80 | } |
81 | |
82 | void IdentityCombo::setCurrentIdentity( const QString &name ) |
83 | { |
84 | int idx = mIdentityManager->identities().indexOf( name ); |
85 | if ( ( idx < 0 ) || ( idx == currentIndex() ) ) { |
86 | return; |
87 | } |
88 | |
89 | blockSignals( true ); // just in case Qt gets fixed to emit activated() here |
90 | setCurrentIndex( idx ); |
91 | blockSignals( false ); |
92 | |
93 | slotEmitChanged( idx ); |
94 | } |
95 | |
96 | void IdentityCombo::setCurrentIdentity( uint uoid ) |
97 | { |
98 | int idx = mUoidList.indexOf( uoid ); |
99 | if ( ( idx < 0 ) || ( idx == currentIndex() ) ) { |
100 | return; |
101 | } |
102 | |
103 | blockSignals( true ); // just in case Qt gets fixed to emit activated() here |
104 | setCurrentIndex( idx ); |
105 | blockSignals( false ); |
106 | |
107 | slotEmitChanged( idx ); |
108 | } |
109 | |
110 | void IdentityCombo::reloadCombo() |
111 | { |
112 | QStringList identities = mIdentityManager->identities(); |
113 | // the IM should prevent this from happening: |
114 | assert( !identities.isEmpty() ); |
115 | clear(); |
116 | addItems( identities ); |
117 | } |
118 | |
119 | void IdentityCombo::reloadUoidList() |
120 | { |
121 | mUoidList.clear(); |
122 | IdentityManager::ConstIterator it; |
123 | IdentityManager::ConstIterator end( mIdentityManager->end() ); |
124 | for ( it = mIdentityManager->begin(); it != end; ++it ) { |
125 | mUoidList << ( *it ).uoid(); |
126 | } |
127 | } |
128 | |
129 | void IdentityCombo::slotIdentityManagerChanged() |
130 | { |
131 | uint oldIdentity = mUoidList[ currentIndex()]; |
132 | |
133 | reloadUoidList(); |
134 | int idx = mUoidList.indexOf( oldIdentity ); |
135 | |
136 | blockSignals( true ); |
137 | reloadCombo(); |
138 | setCurrentIndex( idx < 0 ? 0 : idx ); |
139 | blockSignals( false ); |
140 | |
141 | slotUpdateTooltip( currentIdentity() ); |
142 | |
143 | if ( idx < 0 ) { |
144 | // apparently our oldIdentity got deleted: |
145 | slotEmitChanged( currentIndex() ); |
146 | } |
147 | } |
148 | |
149 | void IdentityCombo::slotEmitChanged( int idx ) |
150 | { |
151 | emit identityChanged( mUoidList[idx] ); |
152 | } |
153 | |
154 | void IdentityCombo::slotUpdateTooltip( uint uoid ) |
155 | { |
156 | setToolTip( mIdentityManager->identityForUoid( uoid ).fullEmailAddr() ); |
157 | } |
158 | |
159 | IdentityManager* IdentityCombo::identityManager() const |
160 | { |
161 | return mIdentityManager; |
162 | } |
163 | |
164 | |
165 | |