Pimcommon

recentaddresses.cpp
1 /*
2  *
3  * SPDX-FileCopyrightText: 2001-2003 Carsten Pfeiffer <[email protected]>
4  * SPDX-FileCopyrightText: 2003 Zack Rusin <[email protected]>
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 #include "recentaddresses.h"
9 
10 #include "pimcommonakonadi_debug.h"
11 #include <KConfig>
12 #include <KConfigGroup>
13 #include <KEmailAddress>
14 #include <KSharedConfig>
15 #include <QCoreApplication>
16 
17 using namespace PimCommon;
18 
19 RecentAddresses *s_self = nullptr;
20 
21 void deleteGlobalRecentAddresses()
22 {
23  delete s_self;
24  s_self = nullptr;
25 }
26 
28 {
29  if (!s_self) {
30  s_self = new RecentAddresses(config);
31  qAddPostRoutine(deleteGlobalRecentAddresses);
32  }
33  return s_self;
34 }
35 
36 bool RecentAddresses::exists()
37 {
38  return s_self != nullptr;
39 }
40 
41 RecentAddresses::RecentAddresses(KConfig *config)
42 {
43  if (config) {
44  load(config);
45  } else {
47  }
48 }
49 
50 RecentAddresses::~RecentAddresses() = default;
51 
53 {
54  QString name;
55  QString emailString;
56 
57  m_addresseeList.clear();
58  KConfigGroup cg(config, "General");
59  m_maxCount = cg.readEntry("Maximum Recent Addresses", 200);
60  const QStringList addresses = cg.readEntry("Recent Addresses", QStringList());
61  for (const QString &address : addresses) {
62  KContacts::Addressee::parseEmailAddress(address, name, emailString);
63  if (!emailString.isEmpty()) {
65  addr.setNameFromString(name);
66  KContacts::Email email(emailString);
67  email.setPreferred(true);
68  addr.addEmail(email);
69  m_addresseeList.append(addr);
70  }
71  }
72 
73  adjustSize();
74 }
75 
77 {
78  KConfigGroup cg(config, "General");
79  cg.writeEntry("Recent Addresses", addresses());
80 }
81 
82 void RecentAddresses::add(const QString &entry)
83 {
84  if (!entry.isEmpty() && m_maxCount > 0) {
85  const QStringList list = KEmailAddress::splitAddressList(entry);
86  for (const QString &str : list) {
88  if (errorCode != KEmailAddress::AddressOk) {
89  continue;
90  }
91  QString emailString;
92  QString fullName;
94 
95  KContacts::Addressee::parseEmailAddress(str, fullName, emailString);
96 
97  KContacts::Addressee::List::Iterator end(m_addresseeList.end());
98  for (KContacts::Addressee::List::Iterator it = m_addresseeList.begin(); it != end; ++it) {
99  if (emailString == (*it).preferredEmail()) {
100  // already inside, remove it here and add it later at pos==1
101  m_addresseeList.erase(it);
102  break;
103  }
104  }
105  addr.setNameFromString(fullName);
106  KContacts::Email email(emailString);
107  email.setPreferred(true);
108  addr.addEmail(email);
109  m_addresseeList.prepend(addr);
110  adjustSize();
111  }
112  }
113 }
114 
116 {
117  if (count != m_maxCount) {
118  m_maxCount = count;
119  adjustSize();
120  }
121 }
122 
124 {
125  return m_maxCount;
126 }
127 
128 void RecentAddresses::adjustSize()
129 {
130  while (m_addresseeList.count() > m_maxCount) {
131  m_addresseeList.takeLast();
132  }
133 }
134 
136 {
137  m_addresseeList.clear();
138 }
139 
141 {
143  addresses.reserve(m_addresseeList.count());
144  for (const KContacts::Addressee &addr : m_addresseeList) {
145  addresses.append(addr.fullEmail());
146  }
147  return addresses;
148 }
void append(const T &value)
QString readEntry(const char *key, const char *aDefault=nullptr) const
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
folderdialogacltab.h
void save(KConfig *)
Saves the list of recently used addresses to the configfile.
KCODECS_EXPORT QStringList splitAddressList(const QString &aStr)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void load(KConfig *)
Loads the list of recently used addresses from the configfile.
void reserve(int alloc)
static void parseEmailAddress(const QString &rawEmail, QString &fullName, QString &email)
Handles a list of "recent email-addresses".
bool isEmpty() const const
void setMaxCount(int count)
Sets the maximum number, the list can hold.
void setPreferred(bool preferred)
void add(const QString &entry)
Adds an entry to the list.
void setNameFromString(const QString &s)
void addEmail(const Email &email)
KCODECS_EXPORT EmailParseResult isValidAddress(const QString &aStr)
QStringList addresses() const
void clear()
Removes all entries from the history.
static RecentAddresses * self(KConfig *config=nullptr)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri May 20 2022 04:01:22 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.