1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 * kmail: KDE mail client
3 * Copyright (C) 2000 Espen Sand, espen@kde.org
4 * Copyright (C) 2001-2003 Marc Mutz, mutz@kde.org
5 * Contains code segments and ideas from earlier kmail dialog code.
6 * Copyright (C) 2010 Volker Krause <vkrause@kde.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <config-enterprise.h>
25 #include "identitypage.h"
27 #include "identitydialog.h"
28 #include "newidentitydialog.h"
29 #ifndef KCM_KPIMIDENTITIES_STANDALONE
31 #include "globalsettings.h"
34 #include <mailcommon/mailkernel.h>
35 #include <messageviewer/autoqpointer.h>
36 #include <kpimidentities/identity.h>
37 #include <kpimidentities/identitymanager.h>
40 #include <KMessageBox>
43 using namespace KMail
;
45 QString
IdentityPage::helpAnchor() const
47 return QString::fromLatin1( "configure-identity" );
50 IdentityPage::IdentityPage( const KComponentData
&instance
, QWidget
*parent
)
51 : ConfigModule( instance
, parent
),
53 mIdentityManager( KernelIf
->identityManager() )
55 mIPage
.setupUi( this );
56 mIPage
.mIdentityList
->setIdentityManager( mIdentityManager
);
58 connect( mIPage
.mIdentityList
, SIGNAL( itemSelectionChanged() ),
59 SLOT( slotIdentitySelectionChanged() ) );
60 connect( this, SIGNAL( changed(bool) ),
61 SLOT( slotIdentitySelectionChanged() ) );
62 connect( mIPage
.mIdentityList
, SIGNAL( rename( KMail::IdentityListViewItem
*, const QString
& ) ),
63 SLOT( slotRenameIdentity(KMail::IdentityListViewItem
*, const QString
& ) ) );
64 connect( mIPage
.mIdentityList
, SIGNAL( itemDoubleClicked( QTreeWidgetItem
*, int ) ),
65 SLOT( slotModifyIdentity() ) );
66 connect( mIPage
.mIdentityList
, SIGNAL( contextMenu( KMail::IdentityListViewItem
*, const QPoint
& ) ),
67 SLOT( slotContextMenu( KMail::IdentityListViewItem
*, const QPoint
& ) ) );
68 // ### connect dragged(...), ...
70 connect( mIPage
.mButtonAdd
, SIGNAL( clicked() ),
71 this, SLOT( slotNewIdentity() ) );
72 connect( mIPage
.mModifyButton
, SIGNAL( clicked() ),
73 this, SLOT( slotModifyIdentity() ) );
74 connect( mIPage
.mRenameButton
, SIGNAL( clicked() ),
75 this, SLOT( slotRenameIdentity() ) );
76 connect( mIPage
.mRemoveButton
, SIGNAL( clicked() ),
77 this, SLOT( slotRemoveIdentity() ) );
78 connect( mIPage
.mSetAsDefaultButton
, SIGNAL( clicked() ),
79 this, SLOT( slotSetAsDefault() ) );
82 IdentityPage::~IdentityPage()
86 void IdentityPage::load()
88 mOldNumberOfIdentities
= mIdentityManager
->shadowIdentities().count();
90 mIPage
.mIdentityList
->clear();
91 QTreeWidgetItem
*item
= 0;
92 for ( KPIMIdentities::IdentityManager::Iterator it
= mIdentityManager
->modifyBegin(); it
!= mIdentityManager
->modifyEnd(); ++it
) {
93 item
= new IdentityListViewItem( mIPage
.mIdentityList
, item
, *it
);
95 if ( mIPage
.mIdentityList
->currentItem() ) {
96 mIPage
.mIdentityList
->currentItem()->setSelected( true );
100 void IdentityPage::save()
102 Q_ASSERT( !mIdentityDialog
);
104 mIdentityManager
->sort();
105 mIdentityManager
->commit();
107 #ifndef KCM_KPIMIDENTITIES_STANDALONE
108 if( mOldNumberOfIdentities
< 2 && mIPage
.mIdentityList
->topLevelItemCount() > 1 ) {
109 // have more than one identity, so better show the combo in the
111 int showHeaders
= GlobalSettings::self()->headers();
112 showHeaders
|= KMail::Composer::HDR_IDENTITY
;
113 GlobalSettings::self()->setHeaders( showHeaders
);
115 // and now the reverse
116 if( mOldNumberOfIdentities
> 1 && mIPage
.mIdentityList
->topLevelItemCount() < 2 ) {
117 // have only one identity, so remove the combo in the composer:
118 int showHeaders
= GlobalSettings::self()->headers();
119 showHeaders
&= ~KMail::Composer::HDR_IDENTITY
;
120 GlobalSettings::self()->setHeaders( showHeaders
);
125 void IdentityPage::slotNewIdentity()
127 Q_ASSERT( !mIdentityDialog
);
129 MessageViewer::AutoQPointer
<NewIdentityDialog
> dialog( new NewIdentityDialog(
130 mIdentityManager
, this ) );
131 dialog
->setObjectName( "new" );
133 if ( dialog
->exec() == QDialog::Accepted
&& dialog
) {
134 QString identityName
= dialog
->identityName().trimmed();
135 Q_ASSERT( !identityName
.isEmpty() );
138 // Construct a new Identity:
140 switch ( dialog
->duplicateMode() ) {
141 case NewIdentityDialog::ExistingEntry
:
143 KPIMIdentities::Identity
&dupThis
= mIdentityManager
->modifyIdentityForName( dialog
->duplicateIdentity() );
144 mIdentityManager
->newFromExisting( dupThis
, identityName
);
147 case NewIdentityDialog::ControlCenter
:
148 mIdentityManager
->newFromControlCenter( identityName
);
150 case NewIdentityDialog::Empty
:
151 mIdentityManager
->newFromScratch( identityName
);
156 // Insert into listview:
158 KPIMIdentities::Identity
&newIdent
= mIdentityManager
->modifyIdentityForName( identityName
);
159 QTreeWidgetItem
*item
= 0;
160 if ( mIPage
.mIdentityList
->selectedItems().size() > 0 ) {
161 item
= mIPage
.mIdentityList
->selectedItems()[0];
164 QTreeWidgetItem
* newItem
= 0;
166 newItem
= new IdentityListViewItem( mIPage
.mIdentityList
, mIPage
.mIdentityList
->itemAbove( item
), newIdent
);
168 newItem
= new IdentityListViewItem( mIPage
.mIdentityList
, newIdent
);
171 mIPage
.mIdentityList
->selectionModel()->clearSelection();
173 newItem
->setSelected( true );
176 slotModifyIdentity();
180 void IdentityPage::slotModifyIdentity()
182 Q_ASSERT( !mIdentityDialog
);
184 IdentityListViewItem
*item
= 0;
185 if ( mIPage
.mIdentityList
->selectedItems().size() > 0 ) {
186 item
= dynamic_cast<IdentityListViewItem
*>( mIPage
.mIdentityList
->selectedItems()[0] );
192 mIdentityDialog
= new IdentityDialog( this );
193 mIdentityDialog
->setIdentity( item
->identity() );
195 // Hmm, an unmodal dialog would be nicer, but a modal one is easier ;-)
196 if ( mIdentityDialog
->exec() == QDialog::Accepted
) {
197 mIdentityDialog
->updateIdentity( item
->identity() );
199 emit
changed( true );
202 delete mIdentityDialog
;
206 void IdentityPage::slotRemoveIdentity()
208 Q_ASSERT( !mIdentityDialog
);
210 if ( mIdentityManager
->shadowIdentities().count() < 2 ) {
211 kFatal() << "Attempted to remove the last identity!";
214 IdentityListViewItem
*item
= 0;
215 if ( mIPage
.mIdentityList
->selectedItems().size() > 0 ) {
216 item
= dynamic_cast<IdentityListViewItem
*>( mIPage
.mIdentityList
->selectedItems()[0] );
222 QString msg
= i18n( "<qt>Do you really want to remove the identity named "
223 "<b>%1</b>?</qt>", item
->identity().identityName() );
224 if( KMessageBox::warningContinueCancel( this, msg
, i18n("Remove Identity"),
225 KGuiItem(i18n("&Remove"),
227 == KMessageBox::Continue
) {
228 if ( mIdentityManager
->removeIdentity( item
->identity().identityName() ) ) {
230 if ( mIPage
.mIdentityList
->currentItem() ) {
231 mIPage
.mIdentityList
->currentItem()->setSelected( true );
238 void IdentityPage::slotRenameIdentity()
240 Q_ASSERT( !mIdentityDialog
);
242 QTreeWidgetItem
*item
= 0;
244 if ( mIPage
.mIdentityList
->selectedItems().size() > 0 ) {
245 item
= mIPage
.mIdentityList
->selectedItems()[0];
249 mIPage
.mIdentityList
->editItem( item
);
252 void IdentityPage::slotRenameIdentity( KMail::IdentityListViewItem
*item
, const QString
&text
)
256 const QString newName
= text
.trimmed();
257 if ( !newName
.isEmpty() &&
258 !mIdentityManager
->shadowIdentities().contains( newName
) ) {
259 KPIMIdentities::Identity
&ident
= item
->identity();
260 ident
.setIdentityName( newName
);
261 emit
changed( true );
266 void IdentityPage::slotContextMenu( IdentityListViewItem
*item
, const QPoint
&pos
)
268 QMenu
*menu
= new QMenu( this );
269 menu
->addAction( i18n( "Add..." ), this, SLOT( slotNewIdentity() ) );
271 menu
->addAction( i18n( "Modify..." ), this, SLOT( slotModifyIdentity() ) );
272 if ( mIPage
.mIdentityList
->topLevelItemCount() > 1 ) {
273 menu
->addAction( i18n( "Remove" ), this, SLOT( slotRemoveIdentity() ) );
275 if ( !item
->identity().isDefault() ) {
276 menu
->addAction( i18n( "Set as Default" ), this, SLOT( slotSetAsDefault() ) );
284 void IdentityPage::slotSetAsDefault()
286 Q_ASSERT( !mIdentityDialog
);
288 IdentityListViewItem
*item
= 0;
289 if ( mIPage
.mIdentityList
->selectedItems().size() > 0 ) {
290 item
= dynamic_cast<IdentityListViewItem
*>( mIPage
.mIdentityList
->selectedItems()[0] );
296 mIdentityManager
->setAsDefault( item
->identity().uoid() );
300 void IdentityPage::refreshList()
302 for ( int i
= 0; i
< mIPage
.mIdentityList
->topLevelItemCount(); ++i
) {
303 IdentityListViewItem
*item
= dynamic_cast<IdentityListViewItem
*>( mIPage
.mIdentityList
->topLevelItem( i
) );
308 emit
changed( true );
311 void IdentityPage::slotIdentitySelectionChanged()
313 IdentityListViewItem
*item
= 0;
314 if ( mIPage
.mIdentityList
->selectedItems().size() > 0 ) {
315 item
= dynamic_cast<IdentityListViewItem
*>( mIPage
.mIdentityList
->selectedItems()[0] );
318 mIPage
.mRemoveButton
->setEnabled( item
&& mIPage
.mIdentityList
->topLevelItemCount() > 1 );
319 mIPage
.mModifyButton
->setEnabled( item
);
320 mIPage
.mRenameButton
->setEnabled( item
);
321 mIPage
.mSetAsDefaultButton
->setEnabled( item
&& !item
->identity().isDefault() );
324 #include "identitypage.moc"