File indexing completed on 2024-04-14 11:02:39

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2003 Carsten Pfeiffer <pfeiffer@kde.org>
0004 
0005     This SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "addresseehelper.h"
0009 
0010 #include <KConfig>
0011 #include <KConfigGroup>
0012 #include <KLocalizedString>
0013 
0014 using namespace KContacts;
0015 
0016 class Q_DECL_HIDDEN KContacts::AddresseeHelperPrivate
0017 {
0018 public:
0019     QSet<QString> mTitles;
0020     QSet<QString> mPrefixes;
0021     QSet<QString> mSuffixes;
0022     bool mTreatAsFamilyName;
0023 };
0024 
0025 Q_GLOBAL_STATIC(AddresseeHelper, s_self)
0026 
0027 // static
0028 AddresseeHelper *AddresseeHelper::self()
0029 {
0030     return s_self;
0031 }
0032 
0033 AddresseeHelper::AddresseeHelper()
0034     : QObject()
0035     , d(new AddresseeHelperPrivate)
0036 {
0037     initSettings();
0038 }
0039 
0040 AddresseeHelper::~AddresseeHelper()
0041 {
0042 }
0043 
0044 static void addToSet(const QStringList &list, QSet<QString> &container)
0045 {
0046     for (const auto &str : list) {
0047         if (!str.isEmpty()) {
0048             container.insert(str);
0049         }
0050     }
0051 }
0052 
0053 void AddresseeHelper::initSettings()
0054 {
0055     d->mTitles.clear();
0056     d->mSuffixes.clear();
0057     d->mPrefixes.clear();
0058 
0059     d->mTitles.insert(i18n("Dr."));
0060     d->mTitles.insert(i18n("Miss"));
0061     d->mTitles.insert(i18n("Mr."));
0062     d->mTitles.insert(i18n("Mrs."));
0063     d->mTitles.insert(i18n("Ms."));
0064     d->mTitles.insert(i18n("Prof."));
0065 
0066     d->mSuffixes.insert(i18n("I"));
0067     d->mSuffixes.insert(i18n("II"));
0068     d->mSuffixes.insert(i18n("III"));
0069     d->mSuffixes.insert(i18n("Jr."));
0070     d->mSuffixes.insert(i18n("Sr."));
0071 
0072     d->mPrefixes.insert(QStringLiteral("van"));
0073     d->mPrefixes.insert(QStringLiteral("von"));
0074     d->mPrefixes.insert(QStringLiteral("de"));
0075 
0076     KConfig _config(QStringLiteral("kabcrc"), KConfig::NoGlobals);
0077     KConfigGroup config(&_config, "General");
0078 
0079     addToSet(config.readEntry("Prefixes", QStringList()), d->mTitles);
0080     addToSet(config.readEntry("Inclusions", QStringList()), d->mPrefixes);
0081     addToSet(config.readEntry("Suffixes", QStringList()), d->mSuffixes);
0082     d->mTreatAsFamilyName = config.readEntry("TreatAsFamilyName", true);
0083 }
0084 
0085 bool AddresseeHelper::containsTitle(const QString &title) const
0086 {
0087     return d->mTitles.contains(title);
0088 }
0089 
0090 bool AddresseeHelper::containsPrefix(const QString &prefix) const
0091 {
0092     return d->mPrefixes.contains(prefix);
0093 }
0094 
0095 bool AddresseeHelper::containsSuffix(const QString &suffix) const
0096 {
0097     return d->mSuffixes.contains(suffix);
0098 }
0099 
0100 bool AddresseeHelper::treatAsFamilyName() const
0101 {
0102     return d->mTreatAsFamilyName;
0103 }
0104 
0105 #include "moc_addresseehelper.cpp"