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 Qt library by Trolltech AS, Norway (or with modified versions
00022  *  of Qt that use the same license as Qt), 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  *  Qt.  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 
00032 #include <kstaticdeleter.h>
00033 #include <kconfig.h>
00034 #include <kglobal.h>
00035 
00036 #include <kdebug.h>
00037 #include <klocale.h>
00038 #include <keditlistbox.h>
00039 
00040 
00041 #include <qlayout.h>
00042 
00043 using namespace KRecentAddress;
00044 
00045 static KStaticDeleter<RecentAddresses> sd;
00046 
00047 RecentAddresses * RecentAddresses::s_self = 0;
00048 
00049 RecentAddresses * RecentAddresses::self( KConfig *config)
00050 {
00051     if ( !s_self )
00052         sd.setObject( s_self, new RecentAddresses(config) );
00053     return s_self;
00054 }
00055 
00056 RecentAddresses::RecentAddresses(KConfig * config)
00057 {
00058   if ( !config )
00059     load( KGlobal::config() );
00060   else
00061     load( config );
00062 }
00063 
00064 RecentAddresses::~RecentAddresses()
00065 {
00066     // if you want this destructor to get called, use a KStaticDeleter
00067     // on s_self
00068 }
00069 
00070 void RecentAddresses::load( KConfig *config )
00071 {
00072     QStringList addresses;
00073     QString name;
00074     QString email;
00075 
00076     m_addresseeList.clear();
00077     KConfigGroupSaver cs( config, "General" );
00078     m_maxCount = config->readNumEntry( "Maximum Recent Addresses", 40 );
00079     addresses = config->readListEntry( "Recent Addresses" );
00080     for ( QStringList::Iterator it = addresses.begin(); it != addresses.end(); ++it ) {
00081         KABC::Addressee::parseEmailAddress( *it, name, email );
00082         if ( !email.isEmpty() ) {
00083             KABC::Addressee addr;
00084             addr.setNameFromString( name );
00085             addr.insertEmail( email, true );
00086             m_addresseeList.append( addr );
00087         }
00088     }
00089 
00090     adjustSize();
00091 }
00092 
00093 void RecentAddresses::save( KConfig *config )
00094 {
00095     KConfigGroupSaver cs( config, "General" );
00096     config->writeEntry( "Recent Addresses", addresses() );
00097 }
00098 
00099 void RecentAddresses::add( const QString& entry )
00100 {
00101     if ( !entry.isEmpty() && m_maxCount > 0 ) {
00102         QString email;
00103         QString fullName;
00104         KABC::Addressee addr;
00105 
00106         KABC::Addressee::parseEmailAddress( entry, fullName, email );
00107 
00108         for ( KABC::Addressee::List::Iterator it = m_addresseeList.begin();
00109               it != m_addresseeList.end(); ++it )
00110         {
00111             if ( email == (*it).preferredEmail() )
00112                 return;//already inside
00113         }
00114         addr.setNameFromString( fullName );
00115         addr.insertEmail( email, true );
00116         m_addresseeList.prepend( addr );
00117         adjustSize();
00118     }
00119 }
00120 
00121 void RecentAddresses::setMaxCount( int count )
00122 {
00123     m_maxCount = count;
00124     adjustSize();
00125 }
00126 
00127 void RecentAddresses::adjustSize()
00128 {
00129     while ( m_addresseeList.count() > m_maxCount )
00130         m_addresseeList.remove( m_addresseeList.fromLast() );
00131 }
00132 
00133 void RecentAddresses::clear()
00134 {
00135     m_addresseeList.clear();
00136     adjustSize();
00137 }
00138 
00139 QStringList RecentAddresses::addresses() const
00140 {
00141     QStringList addresses;
00142     for ( KABC::Addressee::List::ConstIterator it = m_addresseeList.begin();
00143           it != m_addresseeList.end(); ++it )
00144     {
00145         addresses.append( (*it).fullEmail() );
00146     }
00147     return addresses;
00148 }
00149 
00150 RecentAddressDialog::RecentAddressDialog( QWidget *parent, const char *name )
00151   : KDialogBase( Plain, i18n( "Edit Recent Addresses" ), Ok | Cancel, Ok,
00152                  parent, name, true )
00153 {
00154   QWidget *page = plainPage();
00155   QVBoxLayout *layout = new QVBoxLayout( page, 0, spacingHint() );
00156 
00157   mEditor = new KEditListBox( i18n( "Recent Addresses" ), page, "", false,
00158                               KEditListBox::Add | KEditListBox::Remove );
00159   layout->addWidget( mEditor );
00160 }
00161 
00162 void RecentAddressDialog::setAddresses( const QStringList &addrs )
00163 {
00164   mEditor->clear();
00165   mEditor->insertStringList( addrs );
00166 }
00167 
00168 QStringList RecentAddressDialog::addresses() const
00169 {
00170   return mEditor->items();
00171 }
KDE Home | KDE Accessibility Home | Description of Access Keys