1/*
2 SPDX-FileCopyrightText: 2000 Alex Zepeda <zipzippy@sonic.net>
3
4 SPDX-License-Identifier: BSD-2-Clause
5*/
6
7#include "kemailsettings.h"
8
9#include <kconfig.h>
10#include <kconfiggroup.h>
11
12class KEMailSettingsPrivate
13{
14public:
15 KEMailSettingsPrivate()
16 : m_pConfig(nullptr)
17 {
18 }
19 ~KEMailSettingsPrivate()
20 {
21 delete m_pConfig;
22 }
23 KConfig *m_pConfig;
24 QStringList profiles;
25 QString m_sDefaultProfile, m_sCurrentProfile;
26};
27
28QString KEMailSettings::defaultProfileName() const
29{
30 return p->m_sDefaultProfile;
31}
32
33QString KEMailSettings::getSetting(KEMailSettings::Setting s) const
34{
35 KConfigGroup cg(p->m_pConfig, QLatin1String("PROFILE_") + p->m_sCurrentProfile);
36 switch (s) {
37 case ClientProgram: {
38 return cg.readEntry(key: "EmailClient");
39 }
40 case ClientTerminal: {
41 return cg.readEntry(key: "TerminalClient", aDefault: QVariant(false)).toString();
42 }
43 case RealName: {
44 return cg.readEntry(key: "FullName");
45 }
46 case EmailAddress: {
47 return cg.readEntry(key: "EmailAddress");
48 }
49 case ReplyToAddress: {
50 return cg.readEntry(key: "ReplyAddr");
51 }
52 case Organization: {
53 return cg.readEntry(key: "Organization");
54 }
55 };
56 return QString();
57}
58void KEMailSettings::setSetting(KEMailSettings::Setting s, const QString &v)
59{
60 KConfigGroup cg(p->m_pConfig, QLatin1String("PROFILE_") + p->m_sCurrentProfile);
61 switch (s) {
62 case ClientProgram: {
63 cg.writePathEntry(Key: "EmailClient", path: v);
64 break;
65 }
66 case ClientTerminal: {
67 cg.writeEntry(key: "TerminalClient", value: (v == QLatin1String("true")));
68 break;
69 }
70 case RealName: {
71 cg.writeEntry(key: "FullName", value: v);
72 break;
73 }
74 case EmailAddress: {
75 cg.writeEntry(key: "EmailAddress", value: v);
76 break;
77 }
78 case ReplyToAddress: {
79 cg.writeEntry(key: "ReplyAddr", value: v);
80 break;
81 }
82 case Organization: {
83 cg.writeEntry(key: "Organization", value: v);
84 break;
85 }
86 };
87 cg.sync();
88}
89
90void KEMailSettings::setDefault(const QString &s)
91{
92 p->m_pConfig->group(QStringLiteral("Defaults")).writeEntry(key: "Profile", value: s);
93 p->m_pConfig->sync();
94 p->m_sDefaultProfile = s;
95}
96
97void KEMailSettings::setProfile(const QString &s)
98{
99 const QString groupname = QLatin1String("PROFILE_") + s;
100 p->m_sCurrentProfile = s;
101 if (!p->m_pConfig->hasGroup(group: groupname)) { // Create a group if it doesn't exist
102 KConfigGroup cg(p->m_pConfig, groupname);
103 cg.writeEntry(key: "ServerType", value: QString());
104 p->profiles += s;
105 }
106}
107
108QStringList KEMailSettings::profiles() const
109{
110 return p->profiles;
111}
112
113KEMailSettings::KEMailSettings()
114 : p(new KEMailSettingsPrivate())
115{
116 p->m_sCurrentProfile.clear();
117
118 p->m_pConfig = new KConfig(QStringLiteral("emaildefaults"));
119
120 const QStringList groups = p->m_pConfig->groupList();
121 for (const auto &grp : groups) {
122 if (grp.startsWith(s: QLatin1String("PROFILE_"))) {
123 p->profiles += grp.mid(position: 8 /* length of "PROFILE_" */);
124 }
125 }
126
127 KConfigGroup cg(p->m_pConfig, QStringLiteral("Defaults"));
128 p->m_sDefaultProfile = cg.readEntry(key: "Profile", aDefault: tr(sourceText: "Default"));
129 if (!p->m_sDefaultProfile.isNull()) {
130 if (!p->m_pConfig->hasGroup(group: QLatin1String("PROFILE_") + p->m_sDefaultProfile)) {
131 setDefault(tr(sourceText: "Default"));
132 } else {
133 setDefault(p->m_sDefaultProfile);
134 }
135 } else {
136 if (!p->profiles.isEmpty()) {
137 setDefault(p->profiles[0]);
138 } else {
139 setDefault(tr(sourceText: "Default"));
140 }
141 }
142 setProfile(defaultProfileName());
143}
144
145KEMailSettings::~KEMailSettings()
146{
147 delete p;
148}
149

source code of kconfig/src/core/kemailsettings.cpp