28 #include <KMessageBox>
29 #include <KPIMIdentities/Identity>
30 #include <KPIMIdentities/IdentityManager>
32 using namespace KPIMIdentities;
37 IdentityEditionDialog::IdentityEditionDialog( uint uoid,
QWidget *parent )
39 mCurrentIdentityUoid( -1 )
43 setCaption( i18nc(
"@title:window",
"Manage your identities" ) );
45 mSigningKeyRequester->dialogButton()->setText( i18nc(
"@action:button Change signing key",
"Change..." ) );
46 mSigningKeyRequester->setDialogCaption( i18nc(
"@title:window PGP key chooser",
"Your OpenPGP Key") );
47 mSigningKeyRequester->setDialogMessage( i18n(
"Select the OpenPGP key which should be "
48 "used for signing articles." ) );
49 mSigningKeyRequester->setAllowedKeys( Kleo::SigningKeyRequester::OpenPGP );
51 mButtonNewIdentity->setIcon( KIcon(
"list-add" ) );
52 mButtonDuplicateIdentity->setIcon( KIcon(
"edit-copy" ) );
53 mButtonRenameIdentity->setIcon( KIcon(
"edit-rename" ) );
54 mButtonRemoveIdentity->setIcon( KIcon(
"edit-delete" ) );
56 setMainWidget( page );
59 connect( mIdentitySelector, SIGNAL(currentIndexChanged(
int)),
60 this, SLOT(identitySelected(
int)) );
62 connect( mButtonNewIdentity, SIGNAL(clicked(
bool)),
63 this, SLOT(createNewIdentity()) );
64 connect( mButtonDuplicateIdentity, SIGNAL(clicked(
bool)),
65 this, SLOT(duplicateCurrentIdentity()) );
66 connect( mButtonRenameIdentity, SIGNAL(clicked(
bool)),
67 this, SLOT(startIdentityRenaming()) );
68 connect( mButtonRemoveIdentity, SIGNAL(clicked(
bool)),
69 this, SLOT(deleteCurrentIdentity()) );
73 setCurrentIdentity( uoid );
81 void IdentityEditionDialog::reload()
85 IdentityManager::Iterator it = im->modifyBegin();
86 IdentityManager::Iterator end = im->modifyEnd();
88 mIdentitySelector->blockSignals(
true );
89 mIdentitySelector->clear();
91 mUoids << (*it).uoid();
92 mIdentitySelector->addItem( (*it).identityName(), (*it).uoid() );
95 mIdentitySelector->blockSignals(
false );
97 bool canDelIdentity = ( mUoids.size() > 1 );
98 mButtonRemoveIdentity->setEnabled( canDelIdentity );
99 mIdentitySelector->setEditable(
false );
111 if ( mCurrentIdentityUoid != -1 ) {
112 saveIntoIdentity( mCurrentIdentityUoid );
117 case KDialog::Cancel:
123 KDialog::slotButtonClicked( button );
127 void IdentityEditionDialog::identitySelected(
int index )
129 if ( index < 0 || index >= mUoids.size() ) {
130 kWarning() <<
"Bad state: called with the index" << index <<
"when mUoids.size()==" << mUoids.size();
134 setCurrentIdentity( mUoids[ index ] );
138 void IdentityEditionDialog::loadFromIdentity( uint uoid )
142 Identity identity = im->modifyIdentityForUoid( uoid );
144 mNameEdit->setText( identity.fullName() );
145 mOrganisationEdit->setText( identity.organization() );
146 mEmailEdit->setText( identity.primaryEmailAddress() );
147 mReplytoEdit->setText( identity.replyToAddr() );
148 mMailcopiestoEdit->setText( identity.property(
"Mail-Copies-To" ).toString() );
149 mSignatureConfigurator->setSignature( identity.signature() );
150 mSigningKeyRequester->setFingerprint( QString::fromLatin1( identity.pgpSigningKey() ) );
151 mSigningKeyRequester->setInitialQuery( identity.primaryEmailAddress() );
154 void IdentityEditionDialog::saveIntoIdentity( uint uoid )
const
157 Identity &identity = im->modifyIdentityForUoid( uoid );
159 identity.setFullName( mNameEdit->text().trimmed() );
160 identity.setOrganization( mOrganisationEdit->text().trimmed() );
161 identity.setPrimaryEmailAddress( mEmailEdit->text().trimmed() );
162 identity.setReplyToAddr( mReplytoEdit->text().trimmed() );
163 identity.setProperty(
"Mail-Copies-To", mMailcopiestoEdit->text().trimmed() );
164 identity.setSignature( mSignatureConfigurator->signature() );
165 identity.setPGPSigningKey( mSigningKeyRequester->fingerprint().toLatin1() );
168 void IdentityEditionDialog::setCurrentIdentity( uint uoid )
170 stopIdentityRenaming();
171 if ( mCurrentIdentityUoid != -1 ) {
172 saveIntoIdentity( mCurrentIdentityUoid );
175 int index = mUoids.indexOf( uoid );
180 mCurrentIdentityUoid = mUoids[ index ];
181 mIdentitySelector->blockSignals(
true );
182 mIdentitySelector->setCurrentIndex( index );
183 mIdentitySelector->blockSignals(
false );
184 loadFromIdentity( mCurrentIdentityUoid );
187 void IdentityEditionDialog::createNewIdentity()
190 const QString idName = im->makeUnique( i18nc(
"Name of a newly created identity",
"New identity" ) );
191 Identity &identity = im->newFromScratch( idName );
194 setCurrentIdentity( identity.uoid() );
195 startIdentityRenaming();
198 void IdentityEditionDialog::duplicateCurrentIdentity()
201 const Identity ¤tIdentity = im->modifyIdentityForUoid( mCurrentIdentityUoid );
202 const QString newName = im->makeUnique( currentIdentity.identityName() );
203 const Identity &newIdentity = im->newFromExisting( currentIdentity, newName );
206 setCurrentIdentity( newIdentity.uoid() );
207 startIdentityRenaming();
210 void IdentityEditionDialog::startIdentityRenaming()
212 if ( !mIdentitySelector->isEditable() ) {
213 mIdentitySelector->setEditable(
true );
214 if ( !mIdentityNameEdit ) {
215 mIdentityNameEdit =
new IdentityNameEditPrivate();
216 mIdentitySelector->setLineEdit( mIdentityNameEdit );
217 connect( mIdentityNameEdit, SIGNAL(identityNameChanged(QString)),
218 this, SLOT(changeIdentityName(QString)) );
220 mIdentitySelector->setTrapReturnKey(
true );
221 mIdentitySelector->lineEdit()->selectAll();
222 mIdentitySelector->lineEdit()->setFocus( Qt::OtherFocusReason );
226 void IdentityEditionDialog::stopIdentityRenaming()
228 if ( mIdentitySelector->isEditable() ) {
229 mIdentitySelector->setEditable(
false );
233 void IdentityEditionDialog::changeIdentityName(
const QString &newName )
236 Identity &identity = im->modifyIdentityForUoid( mCurrentIdentityUoid );
237 kDebug() <<
"Change identity name from" << identity.identityName() <<
"to" << newName;
238 Q_ASSERT( !identity.isNull() );
239 identity.setIdentityName( newName );
241 stopIdentityRenaming();
244 setCurrentIdentity( identity.uoid() );
246 mNameEdit->setFocus( Qt::OtherFocusReason );
250 void IdentityEditionDialog::deleteCurrentIdentity()
252 if ( mUoids.size() <= 1 ) {
253 kWarning() <<
"Only one identity left and deleteCurrentIdentity() was called!";
258 const Identity identity = im->modifyIdentityForUoid( mUoids[ mIdentitySelector->currentIndex() ] );
259 int answer = KMessageBox::questionYesNo(
this,
260 i18n(
"<qt>Do you really want to remove the identity <emphasis>%1</emphasis>?</qt>",
261 identity.identityName() ),
262 i18nc(
"@title:window",
"Delete identity" ) );
263 if ( answer == KMessageBox::Yes ) {
264 mCurrentIdentityUoid = -1;
265 im->removeIdentity( identity.identityName() );
268 setCurrentIdentity( mUoids[ 0 ] );
274 #include "identity_edition_dialog.moc"
static KNGlobals * self()
Return the KNGlobals instance.
~IdentityEditionDialog()
Destructor.
KPIMIdentities::IdentityManager * identityManager()
Returns the identity manager.
virtual void slotButtonClicked(int button)
Handles button clicks.