Pimcommon

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

KDE's Doxygen guidelines are available online.