Fix clipping/eliding of multi-line task descriptions.
[kdepim.git] / libkdepim / recentaddresses.cpp
blobfd4483b4dc62d6a080b92208a131ada835b3defe
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 for ( QStringList::Iterator it = addresses.begin(); it != addresses.end(); ++it ) {
94 KABC::Addressee::parseEmailAddress( *it, name, email );
95 if ( !email.isEmpty() ) {
96 KABC::Addressee addr;
97 addr.setNameFromString( name );
98 addr.insertEmail( email, true );
99 m_addresseeList.append( addr );
103 adjustSize();
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 ) {
119 continue;
121 QString email;
122 QString fullName;
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 );
132 break;
135 addr.setNameFromString( fullName );
136 addr.insertEmail( email, true );
137 m_addresseeList.prepend( addr );
138 adjustSize();
143 void RecentAddresses::setMaxCount( int count )
145 m_maxCount = count;
146 adjustSize();
149 void RecentAddresses::adjustSize()
151 while ( m_addresseeList.count() > m_maxCount ) {
152 m_addresseeList.takeLast();
156 void RecentAddresses::clear()
158 m_addresseeList.clear();
159 adjustSize();
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() );
169 return addresses;
172 RecentAddressDialog::RecentAddressDialog( QWidget *parent )
173 : KDialog( parent )
175 setCaption( i18n( "Edit Recent Addresses" ) );
176 setButtons( Ok|Cancel );
177 setDefaultButton( Ok );
178 setModal( true );
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 KEditListWidget( page );
186 mEditor->setButtons( KEditListWidget::Add | KEditListWidget::Remove );
187 layout->addWidget( mEditor );
190 void RecentAddressDialog::setAddresses( const QStringList &addrs )
192 mEditor->clear();
193 mEditor->insertStringList( addrs );
196 QStringList RecentAddressDialog::addresses() const
198 return mEditor->items();