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 <KEditListBox>
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 for ( QStringList::Iterator it
= addresses
.begin(); it
!= addresses
.end(); ++it
) {
94 KABC::Addressee::parseEmailAddress( *it
, name
, email
);
95 if ( !email
.isEmpty() ) {
97 addr
.setNameFromString( name
);
98 addr
.insertEmail( email
, true );
99 m_addresseeList
.append( addr
);
106 void RecentAddresses::save( KConfig
*config
)
108 KConfigGroup
cg( config
, "General" );
109 cg
.writeEntry( "Recent Addresses", addresses() );
112 void RecentAddresses::add( const QString
&entry
)
114 if ( !entry
.isEmpty() && m_maxCount
> 0 ) {
115 const QStringList list
= KPIMUtils::splitAddressList( entry
);
116 for ( QStringList::const_iterator e_it
= list
.constBegin(); e_it
!= list
.constEnd(); ++e_it
) {
117 KPIMUtils::EmailParseResult errorCode
= KPIMUtils::isValidAddress( *e_it
);
118 if ( errorCode
!= KPIMUtils::AddressOk
) {
123 KABC::Addressee addr
;
125 KABC::Addressee::parseEmailAddress( *e_it
, fullName
, email
);
127 for ( KABC::Addressee::List::Iterator it
= m_addresseeList
.begin();
128 it
!= m_addresseeList
.end(); ++it
) {
129 if ( email
== (*it
).preferredEmail() ) {
130 //already inside, remove it here and add it later at pos==1
131 m_addresseeList
.erase( it
);
135 addr
.setNameFromString( fullName
);
136 addr
.insertEmail( email
, true );
137 m_addresseeList
.prepend( addr
);
143 void RecentAddresses::setMaxCount( int count
)
149 void RecentAddresses::adjustSize()
151 while ( m_addresseeList
.count() > m_maxCount
) {
152 m_addresseeList
.takeLast();
156 void RecentAddresses::clear()
158 m_addresseeList
.clear();
162 QStringList
RecentAddresses::addresses() const
164 QStringList addresses
;
165 for ( KABC::Addressee::List::ConstIterator it
= m_addresseeList
.constBegin();
166 it
!= m_addresseeList
.constEnd(); ++it
) {
167 addresses
.append( (*it
).fullEmail() );
172 RecentAddressDialog::RecentAddressDialog( QWidget
*parent
)
175 setCaption( i18n( "Edit Recent Addresses" ) );
176 setButtons( Ok
|Cancel
);
177 setDefaultButton( Ok
);
179 QWidget
*page
= new QWidget( this );
180 setMainWidget( page
);
181 QVBoxLayout
*layout
= new QVBoxLayout( page
);
182 layout
->setSpacing( spacingHint() );
183 layout
->setMargin( 0 );
185 mEditor
= new KEditListBox( i18n( "Recent Addresses" ), page
);
186 mEditor
->setButtons( KEditListBox::Add
| KEditListBox::Remove
);
187 layout
->addWidget( mEditor
);
190 void RecentAddressDialog::setAddresses( const QStringList
&addrs
)
193 mEditor
->insertStringList( addrs
);
196 QStringList
RecentAddressDialog::addresses() const
198 return mEditor
->items();