libtdepim

recentaddresses.cpp
1 /*
2  *
3  * Copyright (c) 2001-2003 Carsten Pfeiffer <pfeiffer@kde.org>
4  * Copyright (c) 2003 Zack Rusin <zack@kde.org>
5  *
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.
9  *
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.
14  *
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
18  *
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 TQt library by Trolltech AS, Norway (or with modified versions
22  * of TQt that use the same license as TQt), 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  * TQt. 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.
29  */
30 #include "recentaddresses.h"
31 #include "libemailfunctions/email.h"
32 
33 #include <kstaticdeleter.h>
34 #include <tdeconfig.h>
35 #include <tdeglobal.h>
36 
37 #include <kdebug.h>
38 #include <tdelocale.h>
39 #include <keditlistbox.h>
40 
41 
42 #include <tqlayout.h>
43 
44 using namespace TDERecentAddress;
45 
46 static KStaticDeleter<RecentAddresses> sd;
47 
48 RecentAddresses * RecentAddresses::s_self = 0;
49 
51 {
52  if ( !s_self )
53  sd.setObject( s_self, new RecentAddresses(config) );
54  return s_self;
55 }
56 
57 RecentAddresses::RecentAddresses(TDEConfig * config)
58 {
59  if ( !config )
60  load( TDEGlobal::config() );
61  else
62  load( config );
63 }
64 
65 RecentAddresses::~RecentAddresses()
66 {
67  // if you want this destructor to get called, use a KStaticDeleter
68  // on s_self
69 }
70 
71 void RecentAddresses::load( TDEConfig *config )
72 {
73  TQStringList addresses;
74  TQString name;
75  TQString email;
76 
77  m_addresseeList.clear();
78  TDEConfigGroupSaver cs( config, "General" );
79  m_maxCount = config->readNumEntry( "Maximum Recent Addresses", 40 );
80  addresses = config->readListEntry( "Recent Addresses" );
81  for ( TQStringList::Iterator it = addresses.begin(); it != addresses.end(); ++it ) {
82  TDEABC::Addressee::parseEmailAddress( *it, name, email );
83  if ( !email.isEmpty() ) {
84  TDEABC::Addressee addr;
85  addr.setNameFromString( name );
86  addr.insertEmail( email, true );
87  m_addresseeList.append( addr );
88  }
89  }
90 
91  adjustSize();
92 }
93 
94 void RecentAddresses::save( TDEConfig *config )
95 {
96  TDEConfigGroupSaver cs( config, "General" );
97  config->writeEntry( "Recent Addresses", addresses() );
98 }
99 
100 void RecentAddresses::add( const TQString& entry )
101 {
102  if ( !entry.isEmpty() && m_maxCount > 0 ) {
103  TQStringList list = KPIM::splitEmailAddrList( entry );
104  for( TQStringList::const_iterator e_it = list.begin(); e_it != list.end(); ++e_it ) {
105  KPIM::EmailParseResult errorCode = KPIM::isValidEmailAddress( *e_it );
106  if ( errorCode != KPIM::AddressOk )
107  continue;
108  TQString email;
109  TQString fullName;
110  TDEABC::Addressee addr;
111 
112  TDEABC::Addressee::parseEmailAddress( *e_it, fullName, email );
113 
114  for ( TDEABC::Addressee::List::Iterator it = m_addresseeList.begin();
115  it != m_addresseeList.end(); ++it )
116  {
117  if ( email == (*it).preferredEmail() ) {
118  //already inside, remove it here and add it later at pos==1
119  m_addresseeList.remove( it );
120  break;
121  }
122  }
123  addr.setNameFromString( fullName );
124  addr.insertEmail( email, true );
125  m_addresseeList.prepend( addr );
126  adjustSize();
127  }
128  }
129 }
130 
132 {
133  m_maxCount = count;
134  adjustSize();
135 }
136 
137 void RecentAddresses::adjustSize()
138 {
139  while ( m_addresseeList.count() > m_maxCount )
140  m_addresseeList.remove( m_addresseeList.fromLast() );
141 }
142 
144 {
145  m_addresseeList.clear();
146  adjustSize();
147 }
148 
149 TQStringList RecentAddresses::addresses() const
150 {
151  TQStringList addresses;
152  for ( TDEABC::Addressee::List::ConstIterator it = m_addresseeList.begin();
153  it != m_addresseeList.end(); ++it )
154  {
155  addresses.append( (*it).fullEmail() );
156  }
157  return addresses;
158 }
159 
160 RecentAddressDialog::RecentAddressDialog( TQWidget *parent, const char *name )
161  : KDialogBase( Plain, i18n( "Edit Recent Addresses" ), Ok | Cancel, Ok,
162  parent, name, true )
163 {
164  TQWidget *page = plainPage();
165  TQVBoxLayout *layout = new TQVBoxLayout( page, 0, spacingHint() );
166 
167  mEditor = new KEditListBox( i18n( "Recent Addresses" ), page, "", false,
168  KEditListBox::Add | KEditListBox::Remove );
169  layout->addWidget( mEditor );
170 }
171 
172 void RecentAddressDialog::setAddresses( const TQStringList &addrs )
173 {
174  mEditor->clear();
175  mEditor->insertStringList( addrs );
176 }
177 
178 TQStringList RecentAddressDialog::addresses() const
179 {
180  return mEditor->items();
181 }
Handles a list of "recent email-addresses".
void save(TDEConfig *)
Saves the list of recently used addresses to the configfile.
void add(const TQString &entry)
Adds an entry to the list.
static RecentAddresses * self(TDEConfig *config=0L)
void setMaxCount(int count)
Sets the maximum number, the list can hold.
void load(TDEConfig *)
Loads the list of recently used addresses from the configfile.
void clear()
Removes all entries from the history.