1 /* -*- mode: C++; c-file-style: "gnu" -*-
3 * Copyright (c) 2001-2003 Carsten Pfeiffer <pfeiffer@kde.org>
4 * Copyright (c) 2003 Zack Rusin <zack@kde.org>
6 * KMail is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * KMail is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * In addition, as a special exception, the copyright holders give
20 * permission to link the code of this program with any edition of
21 * the Qt library by Trolltech AS, Norway (or with modified versions
22 * of Qt that use the same license as Qt), and distribute linked
23 * combinations including the two. You must obey the GNU General
24 * Public License in all respects for all of the code used other than
25 * Qt. If you modify this file, you may extend this exception to
26 * your version of the file, but you are not obligated to do so. If
27 * you do not wish to do so, delete this exception statement from
30 #include "recentaddresses.h"
31 #include <kpimutils/email.h>
34 #include <KConfigGroup>
38 #include <KEditListWidget>
40 #include <QCoreApplication>
42 #include <QVBoxLayout>
46 RecentAddresses
*s_self
= 0;
48 void deleteGlobalRecentAddresses()
54 RecentAddresses
*RecentAddresses::self( KConfig
*config
)
57 s_self
= new RecentAddresses( config
);
58 qAddPostRoutine( deleteGlobalRecentAddresses
);
63 bool RecentAddresses::exists()
68 RecentAddresses::RecentAddresses( KConfig
*config
)
71 load( KGlobal::config().data() );
77 RecentAddresses::~RecentAddresses()
79 // if you want this destructor to get called, use a K3StaticDeleter
83 void RecentAddresses::load( KConfig
*config
)
85 QStringList addresses
;
89 m_addresseeList
.clear();
90 KConfigGroup
cg( config
, "General" );
91 m_maxCount
= cg
.readEntry( "Maximum Recent Addresses", 40 );
92 addresses
= cg
.readEntry( "Recent Addresses", QStringList() );
93 QStringList::ConstIterator
end( addresses
.constEnd() );
94 for ( QStringList::ConstIterator it
= addresses
.constBegin(); it
!= end
; ++it
) {
95 KABC::Addressee::parseEmailAddress( *it
, name
, email
);
96 if ( !email
.isEmpty() ) {
98 addr
.setNameFromString( name
);
99 addr
.insertEmail( email
, true );
100 m_addresseeList
.append( addr
);
107 void RecentAddresses::save( KConfig
*config
)
109 KConfigGroup
cg( config
, "General" );
110 cg
.writeEntry( "Recent Addresses", addresses() );
113 void RecentAddresses::add( const QString
&entry
)
115 if ( !entry
.isEmpty() && m_maxCount
> 0 ) {
116 const QStringList list
= KPIMUtils::splitAddressList( entry
);
117 QStringList::const_iterator
e_itEnd( list
.constEnd() );
118 for ( QStringList::const_iterator e_it
= list
.constBegin(); e_it
!= e_itEnd
; ++e_it
) {
119 KPIMUtils::EmailParseResult errorCode
= KPIMUtils::isValidAddress( *e_it
);
120 if ( errorCode
!= KPIMUtils::AddressOk
) {
125 KABC::Addressee addr
;
127 KABC::Addressee::parseEmailAddress( *e_it
, fullName
, email
);
129 KABC::Addressee::List::Iterator
end( m_addresseeList
.end() );
130 for ( KABC::Addressee::List::Iterator it
= m_addresseeList
.begin();
132 if ( email
== (*it
).preferredEmail() ) {
133 //already inside, remove it here and add it later at pos==1
134 m_addresseeList
.erase( it
);
138 addr
.setNameFromString( fullName
);
139 addr
.insertEmail( email
, true );
140 m_addresseeList
.prepend( addr
);
146 void RecentAddresses::setMaxCount( int count
)
152 void RecentAddresses::adjustSize()
154 while ( m_addresseeList
.count() > m_maxCount
) {
155 m_addresseeList
.takeLast();
159 void RecentAddresses::clear()
161 m_addresseeList
.clear();
165 QStringList
RecentAddresses::addresses() const
167 QStringList addresses
;
168 KABC::Addressee::List::ConstIterator end
= m_addresseeList
.constEnd();
169 for ( KABC::Addressee::List::ConstIterator it
= m_addresseeList
.constBegin();
171 addresses
.append( (*it
).fullEmail() );
176 RecentAddressDialog::RecentAddressDialog( QWidget
*parent
)
179 setCaption( i18n( "Edit Recent Addresses" ) );
180 setButtons( Ok
|Cancel
);
181 setDefaultButton( Ok
);
183 QWidget
*page
= new QWidget( this );
184 setMainWidget( page
);
185 QVBoxLayout
*layout
= new QVBoxLayout( page
);
186 layout
->setSpacing( spacingHint() );
187 layout
->setMargin( 0 );
189 mEditor
= new KEditListWidget( page
);
190 mEditor
->setButtons( KEditListWidget::Add
| KEditListWidget::Remove
);
191 layout
->addWidget( mEditor
);
194 void RecentAddressDialog::setAddresses( const QStringList
&addrs
)
197 mEditor
->insertStringList( addrs
);
200 QStringList
RecentAddressDialog::addresses() const
202 return mEditor
->items();