libkdepim

recentaddresses.cpp

00001 /*  -*- mode: C++; c-file-style: "gnu" -*-
00002  *
00003  *  Copyright (c) 2001-2003 Carsten Pfeiffer <pfeiffer@kde.org>
00004  *  Copyright (c) 2003 Zack Rusin <zack@kde.org>
00005  *
00006  *  KMail is free software; you can redistribute it and/or modify it
00007  *  under the terms of the GNU General Public License, version 2, as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  KMail is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  *  In addition, as a special exception, the copyright holders give
00020  *  permission to link the code of this program with any edition of
00021  *  the TQt library by Trolltech AS, Norway (or with modified versions
00022  *  of TQt that use the same license as TQt), and distribute linked
00023  *  combinations including the two.  You must obey the GNU General
00024  *  Public License in all respects for all of the code used other than
00025  *  TQt.  If you modify this file, you may extend this exception to
00026  *  your version of the file, but you are not obligated to do so.  If
00027  *  you do not wish to do so, delete this exception statement from
00028  *  your version.
00029  */
00030 #include "recentaddresses.h"
00031 #include "libemailfunctions/email.h"
00032 
00033 #include <kstaticdeleter.h>
00034 #include <kconfig.h>
00035 #include <kglobal.h>
00036 
00037 #include <kdebug.h>
00038 #include <klocale.h>
00039 #include <keditlistbox.h>
00040 
00041 
00042 #include <tqlayout.h>
00043 
00044 using namespace KRecentAddress;
00045 
00046 static KStaticDeleter<RecentAddresses> sd;
00047 
00048 RecentAddresses * RecentAddresses::s_self = 0;
00049 
00050 RecentAddresses * RecentAddresses::self( KConfig *config)
00051 {
00052     if ( !s_self )
00053         sd.setObject( s_self, new RecentAddresses(config) );
00054     return s_self;
00055 }
00056 
00057 RecentAddresses::RecentAddresses(KConfig * config)
00058 {
00059   if ( !config )
00060     load( KGlobal::config() );
00061   else
00062     load( config );
00063 }
00064 
00065 RecentAddresses::~RecentAddresses()
00066 {
00067     // if you want this destructor to get called, use a KStaticDeleter
00068     // on s_self
00069 }
00070 
00071 void RecentAddresses::load( KConfig *config )
00072 {
00073     TQStringList addresses;
00074     TQString name;
00075     TQString email;
00076 
00077     m_addresseeList.clear();
00078     KConfigGroupSaver cs( config, "General" );
00079     m_maxCount = config->readNumEntry( "Maximum Recent Addresses", 40 );
00080     addresses = config->readListEntry( "Recent Addresses" );
00081     for ( TQStringList::Iterator it = addresses.begin(); it != addresses.end(); ++it ) {
00082         KABC::Addressee::parseEmailAddress( *it, name, email );
00083         if ( !email.isEmpty() ) {
00084             KABC::Addressee addr;
00085             addr.setNameFromString( name );
00086             addr.insertEmail( email, true );
00087             m_addresseeList.append( addr );
00088         }
00089     }
00090 
00091     adjustSize();
00092 }
00093 
00094 void RecentAddresses::save( KConfig *config )
00095 {
00096     KConfigGroupSaver cs( config, "General" );
00097     config->writeEntry( "Recent Addresses", addresses() );
00098 }
00099 
00100 void RecentAddresses::add( const TQString& entry )
00101 {
00102   if ( !entry.isEmpty() && m_maxCount > 0 ) {
00103     TQStringList list = KPIM::splitEmailAddrList( entry );
00104     for( TQStringList::const_iterator e_it = list.begin(); e_it != list.end(); ++e_it ) {
00105       KPIM::EmailParseResult errorCode = KPIM::isValidEmailAddress( *e_it );
00106       if ( errorCode != KPIM::AddressOk ) 
00107         continue;
00108       TQString email;
00109       TQString fullName;
00110       KABC::Addressee addr;
00111 
00112       KABC::Addressee::parseEmailAddress( *e_it, fullName, email );
00113 
00114       for ( KABC::Addressee::List::Iterator it = m_addresseeList.begin();
00115           it != m_addresseeList.end(); ++it )
00116       {
00117         if ( email == (*it).preferredEmail() ) {
00118           //already inside, remove it here and add it later at pos==1
00119           m_addresseeList.remove( it );
00120           break;
00121         }
00122       }
00123       addr.setNameFromString( fullName );
00124       addr.insertEmail( email, true );
00125       m_addresseeList.prepend( addr );
00126       adjustSize();
00127     }
00128   }
00129 }
00130 
00131 void RecentAddresses::setMaxCount( int count )
00132 {
00133     m_maxCount = count;
00134     adjustSize();
00135 }
00136 
00137 void RecentAddresses::adjustSize()
00138 {
00139     while ( m_addresseeList.count() > m_maxCount )
00140         m_addresseeList.remove( m_addresseeList.fromLast() );
00141 }
00142 
00143 void RecentAddresses::clear()
00144 {
00145     m_addresseeList.clear();
00146     adjustSize();
00147 }
00148 
00149 TQStringList RecentAddresses::addresses() const
00150 {
00151     TQStringList addresses;
00152     for ( KABC::Addressee::List::ConstIterator it = m_addresseeList.begin();
00153           it != m_addresseeList.end(); ++it )
00154     {
00155         addresses.append( (*it).fullEmail() );
00156     }
00157     return addresses;
00158 }
00159 
00160 RecentAddressDialog::RecentAddressDialog( TQWidget *parent, const char *name )
00161   : KDialogBase( Plain, i18n( "Edit Recent Addresses" ), Ok | Cancel, Ok,
00162                  parent, name, true )
00163 {
00164   TQWidget *page = plainPage();
00165   TQVBoxLayout *layout = new TQVBoxLayout( page, 0, spacingHint() );
00166 
00167   mEditor = new KEditListBox( i18n( "Recent Addresses" ), page, "", false,
00168                               KEditListBox::Add | KEditListBox::Remove );
00169   layout->addWidget( mEditor );
00170 }
00171 
00172 void RecentAddressDialog::setAddresses( const TQStringList &addrs )
00173 {
00174   mEditor->clear();
00175   mEditor->insertStringList( addrs );
00176 }
00177 
00178 TQStringList RecentAddressDialog::addresses() const
00179 {
00180   return mEditor->items();
00181 }