Don't offer to restart a Running (=busy) agent, it won't work.
[kdepim.git] / libkdepim / recentaddresses.cpp
blob4d9f05e9f5895fa523b8a257915eb44f7e65f9a7
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
28 * your version.
30 #include "recentaddresses.h"
31 #include <kpimutils/email.h>
33 #include <KConfig>
34 #include <KConfigGroup>
35 #include <KDebug>
36 #include <KGlobal>
37 #include <KLocale>
38 #include <KEditListWidget>
40 #include <QCoreApplication>
41 #include <QLayout>
42 #include <QVBoxLayout>
44 using namespace KPIM;
46 RecentAddresses *s_self = 0;
48 void deleteGlobalRecentAddresses()
50 delete s_self;
51 s_self = 0;
54 RecentAddresses *RecentAddresses::self( KConfig *config )
56 if ( !s_self ) {
57 s_self = new RecentAddresses( config );
58 qAddPostRoutine( deleteGlobalRecentAddresses );
60 return s_self;
63 bool RecentAddresses::exists()
65 return s_self != 0;
68 RecentAddresses::RecentAddresses( KConfig *config )
70 if ( !config ) {
71 load( KGlobal::config().data() );
72 } else {
73 load( config );
77 RecentAddresses::~RecentAddresses()
79 // if you want this destructor to get called, use a K3StaticDeleter
80 // on s_self
83 void RecentAddresses::load( KConfig *config )
85 QStringList addresses;
86 QString name;
87 QString email;
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() ) {
97 KABC::Addressee addr;
98 addr.setNameFromString( name );
99 addr.insertEmail( email, true );
100 m_addresseeList.append( addr );
104 adjustSize();
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 ) {
121 continue;
123 QString email;
124 QString fullName;
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();
131 it != end; ++it ) {
132 if ( email == (*it).preferredEmail() ) {
133 //already inside, remove it here and add it later at pos==1
134 m_addresseeList.erase( it );
135 break;
138 addr.setNameFromString( fullName );
139 addr.insertEmail( email, true );
140 m_addresseeList.prepend( addr );
141 adjustSize();
146 void RecentAddresses::setMaxCount( int count )
148 m_maxCount = count;
149 adjustSize();
152 void RecentAddresses::adjustSize()
154 while ( m_addresseeList.count() > m_maxCount ) {
155 m_addresseeList.takeLast();
159 void RecentAddresses::clear()
161 m_addresseeList.clear();
162 adjustSize();
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();
170 it != end; ++it ) {
171 addresses.append( (*it).fullEmail() );
173 return addresses;
176 RecentAddressDialog::RecentAddressDialog( QWidget *parent )
177 : KDialog( parent )
179 setCaption( i18n( "Edit Recent Addresses" ) );
180 setButtons( Ok|Cancel );
181 setDefaultButton( Ok );
182 setModal( true );
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 )
196 mEditor->clear();
197 mEditor->insertStringList( addrs );
200 QStringList RecentAddressDialog::addresses() const
202 return mEditor->items();