• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.14
  • kdepim
  • libkdepim
  • addressline
  • recentaddress
recentaddresses.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-file-style: "gnu" -*-
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 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.
29  */
30 #include "recentaddresses.h"
31 #include <kpimutils/email.h>
32 
33 #include <KConfig>
34 #include <KConfigGroup>
35 #include <KDebug>
36 #include <KGlobal>
37 #include <KLocale>
38 
39 #include <QCoreApplication>
40 
41 using namespace KPIM;
42 
43 RecentAddresses *s_self = 0;
44 
45 void deleteGlobalRecentAddresses()
46 {
47  delete s_self;
48  s_self = 0;
49 }
50 
51 RecentAddresses *RecentAddresses::self( KConfig *config )
52 {
53  if ( !s_self ) {
54  s_self = new RecentAddresses( config );
55  qAddPostRoutine( deleteGlobalRecentAddresses );
56  }
57  return s_self;
58 }
59 
60 bool RecentAddresses::exists()
61 {
62  return s_self != 0;
63 }
64 
65 RecentAddresses::RecentAddresses( KConfig *config )
66 {
67  if ( !config ) {
68  load( KGlobal::config().data() );
69  } else {
70  load( config );
71  }
72 }
73 
74 RecentAddresses::~RecentAddresses()
75 {
76  // if you want this destructor to get called, use K_GLOBAL_STATIC
77  // on s_self
78 }
79 
80 void RecentAddresses::load( KConfig *config )
81 {
82  QStringList addresses;
83  QString name;
84  QString email;
85 
86  m_addresseeList.clear();
87  KConfigGroup cg( config, "General" );
88  m_maxCount = cg.readEntry( "Maximum Recent Addresses", 40 );
89  addresses = cg.readEntry( "Recent Addresses", QStringList() );
90  QStringList::ConstIterator end( addresses.constEnd() );
91  for ( QStringList::ConstIterator it = addresses.constBegin(); it != end; ++it ) {
92  KABC::Addressee::parseEmailAddress( *it, name, email );
93  if ( !email.isEmpty() ) {
94  KABC::Addressee addr;
95  addr.setNameFromString( name );
96  addr.insertEmail( email, true );
97  m_addresseeList.append( addr );
98  }
99  }
100 
101  adjustSize();
102 }
103 
104 void RecentAddresses::save( KConfig *config )
105 {
106  KConfigGroup cg( config, "General" );
107  cg.writeEntry( "Recent Addresses", addresses() );
108 }
109 
110 void RecentAddresses::add( const QString &entry )
111 {
112  if ( !entry.isEmpty() && m_maxCount > 0 ) {
113  const QStringList list = KPIMUtils::splitAddressList( entry );
114  QStringList::const_iterator e_itEnd( list.constEnd() );
115  for ( QStringList::const_iterator e_it = list.constBegin(); e_it != e_itEnd; ++e_it ) {
116  KPIMUtils::EmailParseResult errorCode = KPIMUtils::isValidAddress( *e_it );
117  if ( errorCode != KPIMUtils::AddressOk ) {
118  continue;
119  }
120  QString email;
121  QString fullName;
122  KABC::Addressee addr;
123 
124  KABC::Addressee::parseEmailAddress( *e_it, fullName, email );
125 
126  KABC::Addressee::List::Iterator end( m_addresseeList.end() );
127  for ( KABC::Addressee::List::Iterator it = m_addresseeList.begin();
128  it != 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;
133  }
134  }
135  addr.setNameFromString( fullName );
136  addr.insertEmail( email, true );
137  m_addresseeList.prepend( addr );
138  adjustSize();
139  }
140  }
141 }
142 
143 void RecentAddresses::setMaxCount( int count )
144 {
145  if (count != m_maxCount) {
146  m_maxCount = count;
147  adjustSize();
148  }
149 }
150 
151 void RecentAddresses::adjustSize()
152 {
153  while ( m_addresseeList.count() > m_maxCount ) {
154  m_addresseeList.takeLast();
155  }
156 }
157 
158 void RecentAddresses::clear()
159 {
160  m_addresseeList.clear();
161  adjustSize();
162 }
163 
164 QStringList RecentAddresses::addresses() const
165 {
166  QStringList addresses;
167  KABC::Addressee::List::ConstIterator end = m_addresseeList.constEnd();
168  for ( KABC::Addressee::List::ConstIterator it = m_addresseeList.constBegin();
169  it != end; ++it ) {
170  addresses.append( (*it).fullEmail() );
171  }
172  return addresses;
173 }
174 
KPIM::RecentAddresses::add
void add(const QString &entry)
Adds an entry to the list.
Definition: recentaddresses.cpp:110
KPIM::RecentAddresses::self
static RecentAddresses * self(KConfig *config=0)
Definition: recentaddresses.cpp:51
deleteGlobalRecentAddresses
void deleteGlobalRecentAddresses()
Definition: recentaddresses.cpp:45
KPIM::RecentAddresses::~RecentAddresses
~RecentAddresses()
Definition: recentaddresses.cpp:74
KPIM::RecentAddresses::exists
static bool exists()
Definition: recentaddresses.cpp:60
QList::const_iterator
QList::append
void append(const T &value)
KPIM::RecentAddresses::save
void save(KConfig *)
Saves the list of recently used addresses to the configfile.
Definition: recentaddresses.cpp:104
KPIM::RecentAddresses::clear
void clear()
Removes all entries from the history.
Definition: recentaddresses.cpp:158
QString::isEmpty
bool isEmpty() const
QString
QStringList
KPIM::RecentAddresses::setMaxCount
void setMaxCount(int count)
Sets the maximum number, the list can hold.
Definition: recentaddresses.cpp:143
KPIM::RecentAddresses::addresses
QStringList addresses() const
Definition: recentaddresses.cpp:164
KPIM::RecentAddresses::load
void load(KConfig *)
Loads the list of recently used addresses from the configfile.
Definition: recentaddresses.cpp:80
QList::ConstIterator
typedef ConstIterator
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
s_self
RecentAddresses * s_self
Definition: recentaddresses.cpp:43
KPIM::RecentAddresses
Handles a list of "recent email-addresses".
Definition: recentaddresses.h:46
recentaddresses.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal