Sonnet

settingsimpl.cpp
1 /*
2  * SPDX-FileCopyrightText: 2003 Zack Rusin <[email protected]>
3  * SPDX-FileCopyrightText: 2006 Laurent Montel <[email protected]>
4  * SPDX-FileCopyrightText: 2013 Martin Sandsmark <[email protected]>
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()
52 {
53  delete d;
54 }
55 
56 bool SettingsImpl::setDefaultLanguage(const QString &lang)
57 {
58  const QStringList cs = d->loader->languages();
59  if (cs.indexOf(lang) != -1 && d->defaultLanguage != lang) {
60  d->defaultLanguage = lang;
61  d->modified = true;
62  d->loader->changed();
63  return true;
64  }
65  return false;
66 }
67 
68 QString SettingsImpl::defaultLanguage() const
69 {
70  return d->defaultLanguage;
71 }
72 
73 bool SettingsImpl::setPreferredLanguages(const QStringList &lang)
74 {
75  if (d->preferredLanguages != lang) {
76  d->modified = true;
77  d->preferredLanguages = lang;
78  return true;
79  }
80 
81  return false;
82 }
83 
84 QStringList SettingsImpl::preferredLanguages() const
85 {
86  return d->preferredLanguages;
87 }
88 
89 bool SettingsImpl::setDefaultClient(const QString &client)
90 {
91  // Different from setDefaultLanguage because
92  // the number of clients can't be even close
93  // as big as the number of languages
94  if (d->loader->clients().contains(client)) {
95  d->defaultClient = client;
96  d->modified = true;
97  d->loader->changed();
98  return true;
99  }
100  return false;
101 }
102 
103 QString SettingsImpl::defaultClient() const
104 {
105  return d->defaultClient;
106 }
107 
108 bool SettingsImpl::setCheckUppercase(bool check)
109 {
110  if (d->checkUppercase != check) {
111  d->modified = true;
112  d->checkUppercase = check;
113  return true;
114  }
115  return false;
116 }
117 
118 bool SettingsImpl::checkUppercase() const
119 {
120  return d->checkUppercase;
121 }
122 
123 bool SettingsImpl::setAutodetectLanguage(bool detect)
124 {
125  if (d->autodetectLanguage != detect) {
126  d->modified = true;
127  d->autodetectLanguage = detect;
128  return true;
129  }
130  return false;
131 }
132 
133 bool SettingsImpl::autodetectLanguage() const
134 {
135  return d->autodetectLanguage;
136 }
137 
138 bool SettingsImpl::setSkipRunTogether(bool skip)
139 {
140  if (d->skipRunTogether != skip) {
141  d->modified = true;
142  d->skipRunTogether = skip;
143  return true;
144  }
145  return false;
146 }
147 
148 bool SettingsImpl::skipRunTogether() const
149 {
150  return d->skipRunTogether;
151 }
152 
153 bool SettingsImpl::setCheckerEnabledByDefault(bool check)
154 {
155  if (d->checkerEnabledByDefault != check) {
156  d->modified = true;
157  d->checkerEnabledByDefault = check;
158  return true;
159  }
160  return false;
161 }
162 
163 bool SettingsImpl::checkerEnabledByDefault() const
164 {
165  return d->checkerEnabledByDefault;
166 }
167 
168 bool SettingsImpl::setBackgroundCheckerEnabled(bool enable)
169 {
170  if (d->backgroundCheckerEnabled != enable) {
171  d->modified = true;
172  d->backgroundCheckerEnabled = enable;
173  return true;
174  }
175  return false;
176 }
177 
178 bool SettingsImpl::backgroundCheckerEnabled() const
179 {
180  return d->backgroundCheckerEnabled;
181 }
182 
183 bool SettingsImpl::setCurrentIgnoreList(const QStringList &ignores)
184 {
185  bool changed = setQuietIgnoreList(ignores);
186  d->modified = true;
187  return changed;
188 }
189 
190 bool SettingsImpl::setQuietIgnoreList(const QStringList &ignores)
191 {
192  bool changed = false;
193  d->ignore = QMap<QString, bool>(); // clear out
194  for (QStringList::const_iterator itr = ignores.begin(); itr != ignores.end(); ++itr) {
195  d->ignore.insert(*itr, true);
196  changed = true;
197  }
198  return changed;
199 }
200 
201 QStringList SettingsImpl::currentIgnoreList() const
202 {
203  return d->ignore.keys();
204 }
205 
206 bool SettingsImpl::addWordToIgnore(const QString &word)
207 {
208  if (!d->ignore.contains(word)) {
209  d->modified = true;
210  d->ignore.insert(word, true);
211  return true;
212  }
213  return false;
214 }
215 
216 bool SettingsImpl::ignore(const QString &word)
217 {
218  return d->ignore.contains(word);
219 }
220 
221 int SettingsImpl::disablePercentageWordError() const
222 {
223  return d->disablePercentage;
224 }
225 
226 int SettingsImpl::disableWordErrorCount() const
227 {
228  return d->disableWordCount;
229 }
230 
231 void SettingsImpl::save()
232 {
233  QSettings settings(QStringLiteral("KDE"), QStringLiteral("Sonnet"));
234  settings.setValue(QStringLiteral("defaultClient"), d->defaultClient);
235  settings.setValue(QStringLiteral("defaultLanguage"), d->defaultLanguage);
236  settings.setValue(QStringLiteral("preferredLanguages"), d->preferredLanguages);
237  settings.setValue(QStringLiteral("checkUppercase"), d->checkUppercase);
238  settings.setValue(QStringLiteral("skipRunTogether"), d->skipRunTogether);
239  settings.setValue(QStringLiteral("backgroundCheckerEnabled"), d->backgroundCheckerEnabled);
240  settings.setValue(QStringLiteral("checkerEnabledByDefault"), d->checkerEnabledByDefault);
241  settings.setValue(QStringLiteral("autodetectLanguage"), d->autodetectLanguage);
242  QString defaultLanguage = QStringLiteral("ignore_%1").arg(d->defaultLanguage);
243  if (settings.contains(defaultLanguage) && d->ignore.isEmpty()) {
244  settings.remove(defaultLanguage);
245  } else if (!d->ignore.isEmpty()) {
246  settings.setValue(defaultLanguage, QStringList(d->ignore.keys()));
247  }
248  d->modified = false;
249 }
250 
251 void SettingsImpl::restore()
252 {
253  QSettings settings(QStringLiteral("KDE"), QStringLiteral("Sonnet"));
254  d->defaultClient = settings.value(QStringLiteral("defaultClient"), QString()).toString();
255  d->defaultLanguage = settings.value(QStringLiteral("defaultLanguage"), Settings::defaultDefaultLanguage()).toString();
256  d->preferredLanguages = settings.value(QStringLiteral("preferredLanguages"), Settings::defaultPreferredLanguages()).toStringList();
257 
258  // same defaults are in the default filter (filter.cpp)
259  d->checkUppercase = settings.value(QStringLiteral("checkUppercase"), !Settings::defaultSkipUppercase()).toBool();
260  d->skipRunTogether = settings.value(QStringLiteral("skipRunTogether"), Settings::defauktSkipRunTogether()).toBool();
261  d->backgroundCheckerEnabled = settings.value(QStringLiteral("backgroundCheckerEnabled"), Settings::defaultBackgroundCheckerEnabled()).toBool();
262  d->checkerEnabledByDefault = settings.value(QStringLiteral("checkerEnabledByDefault"), Settings::defaultCheckerEnabledByDefault()).toBool();
263  d->disablePercentage = settings.value(QStringLiteral("Sonnet_AsYouTypeDisablePercentage"), 90).toInt();
264  d->disableWordCount = settings.value(QStringLiteral("Sonnet_AsYouTypeDisableWordCount"), 100).toInt();
265  d->autodetectLanguage = settings.value(QStringLiteral("autodetectLanguage"), Settings::defaultAutodetectLanguage()).toBool();
266 
267  const QString ignoreEntry = QStringLiteral("ignore_%1").arg(d->defaultLanguage);
268  const QStringList ignores = settings.value(ignoreEntry, Settings::defaultIgnoreList()).toStringList();
269  setQuietIgnoreList(ignores);
270 }
271 
272 bool SettingsImpl::modified() const
273 {
274  return d->modified;
275 }
276 
277 void SettingsImpl::setModified(bool modified)
278 {
279  d->modified = modified;
280 }
281 } // namespace Sonnet
int indexOf(QStringView str, int from) const const
void insert(int i, const T &value)
QString & remove(int position, int n)
The sonnet namespace.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QList::iterator begin()
QList::iterator end()
T value(int i) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Sep 25 2022 04:14:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.