1 | /* |
2 | * SPDX-FileCopyrightText: 2003 Zack Rusin <zack@kde.org> |
3 | * SPDX-FileCopyrightText: 2006 Laurent Montel <montel@kde.org> |
4 | * SPDX-FileCopyrightText: 2013 Martin Sandsmark <martin.sandsmark@kde.org> |
5 | * |
6 | * SPDX-License-Identifier: LGPL-2.1-or-later |
7 | */ |
8 | #include "settingsimpl_p.h" |
9 | |
10 | #include "loader_p.h" |
11 | |
12 | #include <QMap> |
13 | #include <QSettings> |
14 | |
15 | #include "settings.h" |
16 | |
17 | namespace Sonnet |
18 | { |
19 | class SettingsImplPrivate |
20 | { |
21 | public: |
22 | Loader *loader = nullptr; // can't be a Ptr since we don't want to hold a ref on it |
23 | bool modified = false; |
24 | |
25 | QString defaultLanguage; |
26 | QStringList preferredLanguages; |
27 | QString defaultClient; |
28 | |
29 | bool checkUppercase = false; |
30 | bool skipRunTogether = false; |
31 | bool backgroundCheckerEnabled = false; |
32 | bool checkerEnabledByDefault = false; |
33 | bool autodetectLanguage = false; |
34 | |
35 | int disablePercentage; |
36 | int disableWordCount; |
37 | |
38 | QMap<QString, bool> ignore; |
39 | }; |
40 | |
41 | SettingsImpl::SettingsImpl(Loader *loader) |
42 | : d(new SettingsImplPrivate) |
43 | { |
44 | d->loader = loader; |
45 | |
46 | d->modified = false; |
47 | d->checkerEnabledByDefault = false; |
48 | restore(); |
49 | } |
50 | |
51 | SettingsImpl::~SettingsImpl() = default; |
52 | |
53 | bool SettingsImpl::setDefaultLanguage(const QString &lang) |
54 | { |
55 | const QStringList cs = d->loader->languages(); |
56 | if (cs.indexOf(str: lang) != -1 && d->defaultLanguage != lang) { |
57 | d->defaultLanguage = lang; |
58 | d->modified = true; |
59 | d->loader->changed(); |
60 | return true; |
61 | } |
62 | return false; |
63 | } |
64 | |
65 | QString SettingsImpl::defaultLanguage() const |
66 | { |
67 | return d->defaultLanguage; |
68 | } |
69 | |
70 | bool SettingsImpl::setPreferredLanguages(const QStringList &lang) |
71 | { |
72 | if (d->preferredLanguages != lang) { |
73 | d->modified = true; |
74 | d->preferredLanguages = lang; |
75 | return true; |
76 | } |
77 | |
78 | return false; |
79 | } |
80 | |
81 | QStringList SettingsImpl::preferredLanguages() const |
82 | { |
83 | return d->preferredLanguages; |
84 | } |
85 | |
86 | bool SettingsImpl::setDefaultClient(const QString &client) |
87 | { |
88 | // Different from setDefaultLanguage because |
89 | // the number of clients can't be even close |
90 | // as big as the number of languages |
91 | if (d->loader->clients().contains(str: client)) { |
92 | d->defaultClient = client; |
93 | d->modified = true; |
94 | d->loader->changed(); |
95 | return true; |
96 | } |
97 | return false; |
98 | } |
99 | |
100 | QString SettingsImpl::defaultClient() const |
101 | { |
102 | return d->defaultClient; |
103 | } |
104 | |
105 | bool SettingsImpl::setCheckUppercase(bool check) |
106 | { |
107 | if (d->checkUppercase != check) { |
108 | d->modified = true; |
109 | d->checkUppercase = check; |
110 | return true; |
111 | } |
112 | return false; |
113 | } |
114 | |
115 | bool SettingsImpl::checkUppercase() const |
116 | { |
117 | return d->checkUppercase; |
118 | } |
119 | |
120 | bool SettingsImpl::setAutodetectLanguage(bool detect) |
121 | { |
122 | if (d->autodetectLanguage != detect) { |
123 | d->modified = true; |
124 | d->autodetectLanguage = detect; |
125 | return true; |
126 | } |
127 | return false; |
128 | } |
129 | |
130 | bool SettingsImpl::autodetectLanguage() const |
131 | { |
132 | return d->autodetectLanguage; |
133 | } |
134 | |
135 | bool SettingsImpl::setSkipRunTogether(bool skip) |
136 | { |
137 | if (d->skipRunTogether != skip) { |
138 | d->modified = true; |
139 | d->skipRunTogether = skip; |
140 | return true; |
141 | } |
142 | return false; |
143 | } |
144 | |
145 | bool SettingsImpl::skipRunTogether() const |
146 | { |
147 | return d->skipRunTogether; |
148 | } |
149 | |
150 | bool SettingsImpl::setCheckerEnabledByDefault(bool check) |
151 | { |
152 | if (d->checkerEnabledByDefault != check) { |
153 | d->modified = true; |
154 | d->checkerEnabledByDefault = check; |
155 | return true; |
156 | } |
157 | return false; |
158 | } |
159 | |
160 | bool SettingsImpl::checkerEnabledByDefault() const |
161 | { |
162 | return d->checkerEnabledByDefault; |
163 | } |
164 | |
165 | bool SettingsImpl::setBackgroundCheckerEnabled(bool enable) |
166 | { |
167 | if (d->backgroundCheckerEnabled != enable) { |
168 | d->modified = true; |
169 | d->backgroundCheckerEnabled = enable; |
170 | return true; |
171 | } |
172 | return false; |
173 | } |
174 | |
175 | bool SettingsImpl::backgroundCheckerEnabled() const |
176 | { |
177 | return d->backgroundCheckerEnabled; |
178 | } |
179 | |
180 | bool SettingsImpl::setCurrentIgnoreList(const QStringList &ignores) |
181 | { |
182 | bool changed = setQuietIgnoreList(ignores); |
183 | d->modified = true; |
184 | return changed; |
185 | } |
186 | |
187 | bool SettingsImpl::setQuietIgnoreList(const QStringList &ignores) |
188 | { |
189 | bool changed = false; |
190 | d->ignore = QMap<QString, bool>(); // clear out |
191 | for (QStringList::const_iterator itr = ignores.begin(); itr != ignores.end(); ++itr) { |
192 | d->ignore.insert(key: *itr, value: true); |
193 | changed = true; |
194 | } |
195 | return changed; |
196 | } |
197 | |
198 | QStringList SettingsImpl::currentIgnoreList() const |
199 | { |
200 | return d->ignore.keys(); |
201 | } |
202 | |
203 | bool SettingsImpl::addWordToIgnore(const QString &word) |
204 | { |
205 | if (!d->ignore.contains(key: word)) { |
206 | d->modified = true; |
207 | d->ignore.insert(key: word, value: true); |
208 | return true; |
209 | } |
210 | return false; |
211 | } |
212 | |
213 | bool SettingsImpl::ignore(const QString &word) |
214 | { |
215 | return d->ignore.contains(key: word); |
216 | } |
217 | |
218 | int SettingsImpl::disablePercentageWordError() const |
219 | { |
220 | return d->disablePercentage; |
221 | } |
222 | |
223 | int SettingsImpl::disableWordErrorCount() const |
224 | { |
225 | return d->disableWordCount; |
226 | } |
227 | |
228 | void SettingsImpl::save() |
229 | { |
230 | QSettings settings(QStringLiteral("KDE" ), QStringLiteral("Sonnet" )); |
231 | settings.setValue(QStringLiteral("defaultClient" ), value: d->defaultClient); |
232 | settings.setValue(QStringLiteral("defaultLanguage" ), value: d->defaultLanguage); |
233 | settings.setValue(QStringLiteral("preferredLanguages" ), value: d->preferredLanguages); |
234 | settings.setValue(QStringLiteral("checkUppercase" ), value: d->checkUppercase); |
235 | settings.setValue(QStringLiteral("skipRunTogether" ), value: d->skipRunTogether); |
236 | settings.setValue(QStringLiteral("backgroundCheckerEnabled" ), value: d->backgroundCheckerEnabled); |
237 | settings.setValue(QStringLiteral("checkerEnabledByDefault" ), value: d->checkerEnabledByDefault); |
238 | settings.setValue(QStringLiteral("autodetectLanguage" ), value: d->autodetectLanguage); |
239 | QString defaultLanguage = QStringLiteral("ignore_%1" ).arg(a: d->defaultLanguage); |
240 | if (settings.contains(key: defaultLanguage) && d->ignore.isEmpty()) { |
241 | settings.remove(key: defaultLanguage); |
242 | } else if (!d->ignore.isEmpty()) { |
243 | settings.setValue(key: defaultLanguage, value: QStringList(d->ignore.keys())); |
244 | } |
245 | d->modified = false; |
246 | } |
247 | |
248 | void SettingsImpl::restore() |
249 | { |
250 | QSettings settings(QStringLiteral("KDE" ), QStringLiteral("Sonnet" )); |
251 | d->defaultClient = settings.value(QStringLiteral("defaultClient" ), defaultValue: QString()).toString(); |
252 | d->defaultLanguage = settings.value(QStringLiteral("defaultLanguage" ), defaultValue: Settings::defaultDefaultLanguage()).toString(); |
253 | d->preferredLanguages = settings.value(QStringLiteral("preferredLanguages" ), defaultValue: Settings::defaultPreferredLanguages()).toStringList(); |
254 | |
255 | // same defaults are in the default filter (filter.cpp) |
256 | d->checkUppercase = settings.value(QStringLiteral("checkUppercase" ), defaultValue: !Settings::defaultSkipUppercase()).toBool(); |
257 | d->skipRunTogether = settings.value(QStringLiteral("skipRunTogether" ), defaultValue: Settings::defauktSkipRunTogether()).toBool(); |
258 | d->backgroundCheckerEnabled = settings.value(QStringLiteral("backgroundCheckerEnabled" ), defaultValue: Settings::defaultBackgroundCheckerEnabled()).toBool(); |
259 | d->checkerEnabledByDefault = settings.value(QStringLiteral("checkerEnabledByDefault" ), defaultValue: Settings::defaultCheckerEnabledByDefault()).toBool(); |
260 | d->disablePercentage = settings.value(QStringLiteral("Sonnet_AsYouTypeDisablePercentage" ), defaultValue: 90).toInt(); |
261 | d->disableWordCount = settings.value(QStringLiteral("Sonnet_AsYouTypeDisableWordCount" ), defaultValue: 100).toInt(); |
262 | d->autodetectLanguage = settings.value(QStringLiteral("autodetectLanguage" ), defaultValue: Settings::defaultAutodetectLanguage()).toBool(); |
263 | |
264 | const QString ignoreEntry = QStringLiteral("ignore_%1" ).arg(a: d->defaultLanguage); |
265 | const QStringList ignores = settings.value(key: ignoreEntry, defaultValue: Settings::defaultIgnoreList()).toStringList(); |
266 | setQuietIgnoreList(ignores); |
267 | } |
268 | |
269 | bool SettingsImpl::modified() const |
270 | { |
271 | return d->modified; |
272 | } |
273 | |
274 | void SettingsImpl::setModified(bool modified) |
275 | { |
276 | d->modified = modified; |
277 | } |
278 | } // namespace Sonnet |
279 | |