File indexing completed on 2025-01-05 04:58:21
0001 /* 0002 * 0003 * SPDX-FileCopyrightText: 2001-2003 Carsten Pfeiffer <pfeiffer@kde.org> 0004 * SPDX-FileCopyrightText: 2003 Zack Rusin <zack@kde.org> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 #include "recentaddresses.h" 0009 0010 #include <KConfig> 0011 #include <KConfigGroup> 0012 #include <KEmailAddress> 0013 #include <KSharedConfig> 0014 #include <QCoreApplication> 0015 0016 using namespace PimCommon; 0017 0018 RecentAddresses *s_self = nullptr; 0019 0020 void deleteGlobalRecentAddresses() 0021 { 0022 delete s_self; 0023 s_self = nullptr; 0024 } 0025 0026 RecentAddresses *RecentAddresses::self(KConfig *config) 0027 { 0028 if (!s_self) { 0029 s_self = new RecentAddresses(config); 0030 qAddPostRoutine(deleteGlobalRecentAddresses); 0031 } 0032 return s_self; 0033 } 0034 0035 bool RecentAddresses::exists() 0036 { 0037 return s_self != nullptr; 0038 } 0039 0040 RecentAddresses::RecentAddresses(KConfig *config) 0041 { 0042 if (config) { 0043 load(config); 0044 } else { 0045 load(KSharedConfig::openConfig().data()); 0046 } 0047 } 0048 0049 RecentAddresses::~RecentAddresses() = default; 0050 0051 void RecentAddresses::load(KConfig *config) 0052 { 0053 QString name; 0054 QString emailString; 0055 0056 m_addresseeList.clear(); 0057 KConfigGroup cg(config, QStringLiteral("General")); 0058 m_maxCount = cg.readEntry("Maximum Recent Addresses", 200); 0059 const QStringList addresses = cg.readEntry("Recent Addresses", QStringList()); 0060 for (const QString &address : addresses) { 0061 KContacts::Addressee::parseEmailAddress(address, name, emailString); 0062 if (!emailString.isEmpty()) { 0063 KContacts::Addressee addr; 0064 addr.setNameFromString(name); 0065 KContacts::Email email(emailString); 0066 email.setPreferred(true); 0067 addr.addEmail(email); 0068 m_addresseeList.append(addr); 0069 } 0070 } 0071 0072 adjustSize(); 0073 } 0074 0075 void RecentAddresses::save(KConfig *config) 0076 { 0077 KConfigGroup cg(config, QStringLiteral("General")); 0078 cg.writeEntry("Recent Addresses", addresses()); 0079 } 0080 0081 void RecentAddresses::add(const QString &entry) 0082 { 0083 if (!entry.isEmpty() && m_maxCount > 0) { 0084 const QStringList list = KEmailAddress::splitAddressList(entry); 0085 for (const QString &str : list) { 0086 KEmailAddress::EmailParseResult errorCode = KEmailAddress::isValidAddress(str); 0087 if (errorCode != KEmailAddress::AddressOk) { 0088 continue; 0089 } 0090 QString emailString; 0091 QString fullName; 0092 KContacts::Addressee addr; 0093 0094 KContacts::Addressee::parseEmailAddress(str, fullName, emailString); 0095 0096 KContacts::Addressee::List::Iterator end(m_addresseeList.end()); 0097 for (KContacts::Addressee::List::Iterator it = m_addresseeList.begin(); it != end; ++it) { 0098 if (emailString == (*it).preferredEmail()) { 0099 // already inside, remove it here and add it later at pos==1 0100 m_addresseeList.erase(it); 0101 break; 0102 } 0103 } 0104 addr.setNameFromString(fullName); 0105 KContacts::Email email(emailString); 0106 email.setPreferred(true); 0107 addr.addEmail(email); 0108 m_addresseeList.prepend(addr); 0109 adjustSize(); 0110 } 0111 } 0112 } 0113 0114 void RecentAddresses::setMaxCount(int count) 0115 { 0116 if (count != m_maxCount) { 0117 m_maxCount = count; 0118 adjustSize(); 0119 } 0120 } 0121 0122 int RecentAddresses::maxCount() const 0123 { 0124 return m_maxCount; 0125 } 0126 0127 void RecentAddresses::adjustSize() 0128 { 0129 while (m_addresseeList.count() > m_maxCount) { 0130 m_addresseeList.takeLast(); 0131 } 0132 } 0133 0134 void RecentAddresses::clear() 0135 { 0136 m_addresseeList.clear(); 0137 } 0138 0139 QStringList RecentAddresses::addresses() const 0140 { 0141 QStringList addresses; 0142 addresses.reserve(m_addresseeList.count()); 0143 for (const KContacts::Addressee &addr : m_addresseeList) { 0144 addresses.append(addr.fullEmail()); 0145 } 0146 return addresses; 0147 }