7#include "autocorrectionsettings.h"
8#include "export/exportlibreofficeautocorrection.h"
9#include "import/importlibreofficeautocorrection.h"
11#include "settings/textautocorrectionsettings.h"
12#include "textautocorrection_debug.h"
14#include <QRegularExpression>
15#include <QStandardPaths>
17using namespace TextAutoCorrectionCore;
19namespace TextAutoCorrectionCore
21class AutoCorrectionSettingsPrivate
24 AutoCorrectionUtils::TypographicQuotes mTypographicSingleQuotes;
25 AutoCorrectionUtils::TypographicQuotes mTypographicDoubleQuotes;
26 AutoCorrectionUtils::TypographicQuotes mDoubleFrenchQuotes;
39 QChar mNonBreakingSpace;
41 int mMaxFindStringLength = 0;
42 int mMinFindStringLength = 0;
44 bool mSingleSpaces =
true;
45 bool mUppercaseFirstCharOfSentence =
false;
46 bool mFixTwoUppercaseChars =
false;
47 bool mAutoFractions =
true;
48 bool mCapitalizeWeekDays =
false;
49 bool mAdvancedAutocorrect =
false;
51 bool mReplaceDoubleQuotes =
false;
52 bool mReplaceSingleQuotes =
false;
54 bool mAutoFormatUrl =
false;
55 bool mAutoBoldUnderline =
false;
56 bool mEnabled =
false;
57 bool mSuperScriptAppendix =
false;
59 bool mAddNonBreakingSpace =
false;
60 bool mReplaceDoubleQuotesByFrenchQuotes =
false;
64AutoCorrectionSettings::AutoCorrectionSettings()
65 : d(new TextAutoCorrectionCore::AutoCorrectionSettingsPrivate)
71 d->mTypographicSingleQuotes = AutoCorrectionUtils::typographicDefaultSingleQuotes();
72 d->mTypographicDoubleQuotes = AutoCorrectionUtils::typographicDefaultDoubleQuotes();
73 d->mDoubleFrenchQuotes = AutoCorrectionUtils::typographicDefaultFrenchQuotes();
78AutoCorrectionSettings::~AutoCorrectionSettings() =
default;
80void AutoCorrectionSettings::setCapitalizeWeekDays(
bool b)
82 d->mCapitalizeWeekDays = b;
85void AutoCorrectionSettings::setReplaceDoubleQuotes(
bool b)
87 d->mReplaceDoubleQuotes = b;
90void AutoCorrectionSettings::setReplaceSingleQuotes(
bool b)
92 d->mReplaceSingleQuotes = b;
95void AutoCorrectionSettings::setAdvancedAutocorrect(
bool b)
97 d->mAdvancedAutocorrect = b;
100void AutoCorrectionSettings::setAutoFormatUrl(
bool b)
102 d->mAutoFormatUrl = b;
105void AutoCorrectionSettings::setAutoBoldUnderline(
bool b)
107 d->mAutoBoldUnderline = b;
110void AutoCorrectionSettings::setSuperScript(
bool b)
112 d->mSuperScriptAppendix = b;
115void AutoCorrectionSettings::setAddNonBreakingSpace(
bool b)
117 d->mAddNonBreakingSpace = b;
120bool AutoCorrectionSettings::isEnabledAutoCorrection()
const
125bool AutoCorrectionSettings::isUppercaseFirstCharOfSentence()
const
127 return d->mUppercaseFirstCharOfSentence;
130bool AutoCorrectionSettings::isFixTwoUppercaseChars()
const
132 return d->mFixTwoUppercaseChars;
135bool AutoCorrectionSettings::isSingleSpaces()
const
137 return d->mSingleSpaces;
140bool AutoCorrectionSettings::isAutoFractions()
const
142 return d->mAutoFractions;
145bool AutoCorrectionSettings::isCapitalizeWeekDays()
const
147 return d->mCapitalizeWeekDays;
150bool AutoCorrectionSettings::isReplaceDoubleQuotes()
const
152 return d->mReplaceDoubleQuotes;
155bool AutoCorrectionSettings::isReplaceSingleQuotes()
const
157 return d->mReplaceSingleQuotes;
160bool AutoCorrectionSettings::isAdvancedAutocorrect()
const
162 return d->mAdvancedAutocorrect;
165bool AutoCorrectionSettings::isAutoFormatUrl()
const
167 return d->mAutoFormatUrl;
170bool AutoCorrectionSettings::isAutoBoldUnderline()
const
172 return d->mAutoBoldUnderline;
175bool AutoCorrectionSettings::isSuperScript()
const
177 return d->mSuperScriptAppendix;
180bool AutoCorrectionSettings::isAddNonBreakingSpace()
const
182 return d->mAddNonBreakingSpace;
185bool AutoCorrectionSettings::isReplaceDoubleQuotesByFrenchQuotes()
const
187 return d->mReplaceDoubleQuotesByFrenchQuotes;
190void AutoCorrectionSettings::setEnabledAutoCorrection(
bool b)
195void AutoCorrectionSettings::setReplaceDoubleQuotesByFrenchQuotes(
bool b)
197 d->mReplaceDoubleQuotesByFrenchQuotes = b;
200TextAutoCorrectionCore::AutoCorrectionUtils::TypographicQuotes AutoCorrectionSettings::typographicSingleQuotes()
const
202 return d->mTypographicSingleQuotes;
205TextAutoCorrectionCore::AutoCorrectionUtils::TypographicQuotes AutoCorrectionSettings::typographicDoubleQuotes()
const
207 return d->mTypographicDoubleQuotes;
210void AutoCorrectionSettings::setTypographicSingleQuotes(TextAutoCorrectionCore::AutoCorrectionUtils::TypographicQuotes singleQuote)
212 d->mTypographicSingleQuotes = singleQuote;
215void AutoCorrectionSettings::setTypographicDoubleQuotes(AutoCorrectionUtils::TypographicQuotes doubleQuote)
217 d->mTypographicDoubleQuotes = doubleQuote;
220void AutoCorrectionSettings::readConfig()
222 d->mAutoBoldUnderline = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->autoBoldUnderline();
223 d->mAutoFormatUrl = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->autoFormatUrl();
224 d->mUppercaseFirstCharOfSentence = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->uppercaseFirstCharOfSentence();
225 d->mFixTwoUppercaseChars = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->fixTwoUppercaseChars();
226 d->mSingleSpaces = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->singleSpaces();
227 d->mAutoFractions = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->autoFractions();
228 d->mCapitalizeWeekDays = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->capitalizeWeekDays();
229 d->mAdvancedAutocorrect = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->advancedAutocorrect();
230 d->mReplaceDoubleQuotes = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->replaceDoubleQuotes();
231 d->mReplaceSingleQuotes = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->replaceSingleQuotes();
232 d->mEnabled = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->enabled();
233 d->mSuperScriptAppendix = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->superScript();
234 d->mAddNonBreakingSpace = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->addNonBreakingSpaceInFrench();
235 d->mReplaceDoubleQuotesByFrenchQuotes = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->replaceDoubleQuotesByFrenchQuotes();
236 d->mCustomSystemPath = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->customSystemPath();
237 d->mCustomWritablePath = TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->customWritablePath();
239 d->mTypographicSingleQuotes =
240 AutoCorrectionUtils::TypographicQuotes::fromString(TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->typographicSingleQuotes());
241 if (d->mTypographicSingleQuotes.isEmpty()) {
242 d->mTypographicSingleQuotes = AutoCorrectionUtils::typographicDefaultSingleQuotes();
244 d->mTypographicDoubleQuotes =
245 AutoCorrectionUtils::TypographicQuotes::fromString(TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->typographicDoubleQuotes());
246 if (d->mTypographicDoubleQuotes.isEmpty()) {
247 d->mTypographicDoubleQuotes = AutoCorrectionUtils::typographicDefaultDoubleQuotes();
249 readAutoCorrectionFile();
252void AutoCorrectionSettings::writeConfig()
254 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setAutoBoldUnderline(d->mAutoBoldUnderline);
255 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setAutoFormatUrl(d->mAutoFormatUrl);
256 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setUppercaseFirstCharOfSentence(d->mUppercaseFirstCharOfSentence);
257 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setFixTwoUppercaseChars(d->mFixTwoUppercaseChars);
258 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setSingleSpaces(d->mSingleSpaces);
259 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setAutoFractions(d->mAutoFractions);
260 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setCapitalizeWeekDays(d->mCapitalizeWeekDays);
261 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setAdvancedAutocorrect(d->mAdvancedAutocorrect);
262 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setReplaceDoubleQuotes(d->mReplaceDoubleQuotes);
263 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setReplaceSingleQuotes(d->mReplaceSingleQuotes);
264 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setEnabled(d->mEnabled);
265 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setSuperScript(d->mSuperScriptAppendix);
266 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setAddNonBreakingSpaceInFrench(d->mAddNonBreakingSpace);
267 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setTypographicSingleQuotes(d->mTypographicSingleQuotes.toString());
268 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setTypographicDoubleQuotes(d->mTypographicDoubleQuotes.toString());
269 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setReplaceDoubleQuotesByFrenchQuotes(d->mReplaceDoubleQuotesByFrenchQuotes);
270 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setCustomWritablePath(d->mCustomWritablePath);
271 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setCustomSystemPath(d->mCustomSystemPath);
272 TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->
requestSync();
273 writeAutoCorrectionFile();
276void AutoCorrectionSettings::setAutoFractions(
bool newAutoFractions)
278 d->mAutoFractions = newAutoFractions;
281void AutoCorrectionSettings::setSingleSpaces(
bool newSingleSpaces)
283 d->mSingleSpaces = newSingleSpaces;
286void AutoCorrectionSettings::setFixTwoUppercaseChars(
bool newFixTwoUppercaseChars)
288 d->mFixTwoUppercaseChars = newFixTwoUppercaseChars;
291void AutoCorrectionSettings::setUppercaseFirstCharOfSentence(
bool newUppercaseFirstCharOfSentence)
293 d->mUppercaseFirstCharOfSentence = newUppercaseFirstCharOfSentence;
296void AutoCorrectionSettings::setUpperCaseExceptions(
const QSet<QString> &exceptions)
298 d->mUpperCaseExceptions = exceptions;
301void AutoCorrectionSettings::setTwoUpperLetterExceptions(
const QSet<QString> &exceptions)
303 d->mTwoUpperLetterExceptions = exceptions;
306QSet<QString> AutoCorrectionSettings::upperCaseExceptions()
const
308 return d->mUpperCaseExceptions;
311QSet<QString> AutoCorrectionSettings::twoUpperLetterExceptions()
const
313 return d->mTwoUpperLetterExceptions;
316QString AutoCorrectionSettings::language()
const
318 return d->mAutoCorrectLang;
321void AutoCorrectionSettings::setLanguage(
const QString &lang,
bool forceGlobal)
323 if (d->mAutoCorrectLang != lang || forceGlobal) {
324 d->mAutoCorrectLang = lang;
326 readAutoCorrectionFile(forceGlobal);
330bool AutoCorrectionSettings::isFrenchLanguage()
const
332 return d->mAutoCorrectLang ==
"FR_fr"_L1 || d->mAutoCorrectLang ==
"fr"_L1;
335bool AutoCorrectionSettings::addAutoCorrect(
const QString ¤tWord,
const QString &replaceWord)
337 if (!d->mAutocorrectEntries.contains(currentWord)) {
338 d->mAutocorrectEntries.insert(currentWord, replaceWord);
339 writeAutoCorrectionFile();
346QChar AutoCorrectionSettings::nonBreakingSpace()
const
348 return d->mNonBreakingSpace;
351void AutoCorrectionSettings::setNonBreakingSpace(
const QChar &newNonBreakingSpace)
353 d->mNonBreakingSpace = newNonBreakingSpace;
358 return d->mSuperScriptEntries;
363 d->mSuperScriptEntries = newSuperScriptEntries;
368 d->mMaxFindStringLength = 0;
369 d->mMinFindStringLength = 0;
371 while (i.hasNext()) {
373 const int findStringLenght(i.key().length());
374 d->mMaxFindStringLength = qMax(d->mMaxFindStringLength, findStringLenght);
375 d->mMinFindStringLength = qMin(d->mMinFindStringLength, findStringLenght);
377 d->mAutocorrectEntries = entries;
382 return d->mAutocorrectEntries;
385void AutoCorrectionSettings::writeAutoCorrectionFile(
const QString &filename)
387 ExportLibreOfficeAutocorrection correct;
388 correct.setAutocorrectEntries(d->mAutocorrectEntries);
389 correct.setUpperCaseExceptions(d->mUpperCaseExceptions);
390 correct.setTwoUpperLetterExceptions(d->mTwoUpperLetterExceptions);
392 if (!correct.exportData(d->mAutoCorrectLang, filename, message, d->mCustomWritablePath)) {
393 qCDebug(TEXTAUTOCORRECTION_LOG) <<
"We can't save in file :" << filename;
397int AutoCorrectionSettings::maxFindStringLength()
const
399 return d->mMaxFindStringLength;
402int AutoCorrectionSettings::minFindStringLength()
const
404 return d->mMinFindStringLength;
407void AutoCorrectionSettings::loadLocalFileName(
const QString &localFileName,
const QString &fname)
409 ImportLibreOfficeAutocorrection
import;
411 if (
import.
import(localFileName, messageError, ImportAbstractAutocorrection::All)) {
412 d->mUpperCaseExceptions =
import.upperCaseExceptions();
413 d->mTwoUpperLetterExceptions =
import.twoUpperLetterExceptions();
414 d->mAutocorrectEntries =
import.autocorrectEntries();
418 if (!fname.
isEmpty() &&
import.import(fname, messageError, ImportAbstractAutocorrection::SuperScript)) {
419 d->mSuperScriptEntries =
import.superScriptEntries();
421 d->mMaxFindStringLength =
import.maxFindStringLenght();
422 d->mMinFindStringLength =
import.minFindStringLenght();
425void AutoCorrectionSettings::loadGlobalFileName(
const QString &fname)
428 const QString fileName = containsAutoCorrectionFile(d->mAutoCorrectLang);
431 ImportLibreOfficeAutocorrection
import;
432 if (
import.
import(fileName, errorMessage)) {
433 d->mUpperCaseExceptions =
import.upperCaseExceptions();
434 d->mTwoUpperLetterExceptions =
import.twoUpperLetterExceptions();
435 d->mAutocorrectEntries =
import.autocorrectEntries();
436 d->mSuperScriptEntries =
import.superScriptEntries();
437 d->mMaxFindStringLength =
import.maxFindStringLenght();
438 d->mMinFindStringLength =
import.minFindStringLenght();
442 qDebug() <<
" import libreoffice file " << fname;
443 ImportLibreOfficeAutocorrection
import;
445 if (
import.
import(fname, messageError, ImportAbstractAutocorrection::All)) {
446 d->mUpperCaseExceptions =
import.upperCaseExceptions();
447 d->mTwoUpperLetterExceptions =
import.twoUpperLetterExceptions();
448 d->mAutocorrectEntries =
import.autocorrectEntries();
449 d->mSuperScriptEntries =
import.superScriptEntries();
450 d->mMaxFindStringLength =
import.maxFindStringLenght();
451 d->mMinFindStringLength =
import.minFindStringLenght();
456void AutoCorrectionSettings::migrateKMailXmlFile()
460 QString kdelang = QStringLiteral(
"en_US");
465 kdelang = QStringLiteral(
"en_US");
475 if (!mAutoCorrectLang.isEmpty()) {
492 if (!mAutoCorrectLang.isEmpty()) {
493 if (mAutoCorrectLang ==
"en_US"_L1) {
511 if (mAutoCorrectLang.isEmpty()) {
512 mAutoCorrectLang = kdelang;
517void AutoCorrectionSettings::readAutoCorrectionFile(
bool forceGlobal)
519 d->mUpperCaseExceptions.clear();
520 d->mAutocorrectEntries.clear();
521 d->mTwoUpperLetterExceptions.clear();
522 d->mSuperScriptEntries.clear();
524 QString kdelang = QStringLiteral(
"en-US");
529 kdelang = QStringLiteral(
"en-US");
539 if (d->mAutoCorrectLang.isEmpty()) {
541 localFileName = containsAutoCorrectionFile(kdelang);
545 localFileName = containsAutoCorrectionFile(kdelang);
551 if (!d->mAutoCorrectLang.isEmpty()) {
552 localFileName = containsAutoCorrectionFile(d->mAutoCorrectLang);
555 fname = containsAutoCorrectionFile(kdelang);
559 fname = containsAutoCorrectionFile(kdelang);
563 if (d->mAutoCorrectLang.isEmpty()) {
564 d->mAutoCorrectLang = kdelang;
570 loadGlobalFileName(fname);
572 loadLocalFileName(localFileName, fname);
576QString AutoCorrectionSettings::containsAutoCorrectionFile(
const QString &fileName)
578 return AutoCorrectionUtils::containsAutoCorrectionFile(fileName, d->mCustomSystemPath, d->mCustomWritablePath);
581AutoCorrectionUtils::TypographicQuotes AutoCorrectionSettings::doubleFrenchQuotes()
const
583 return d->mDoubleFrenchQuotes;
586void AutoCorrectionSettings::setDoubleFrenchQuotes(
const AutoCorrectionUtils::TypographicQuotes &newDoubleFrenchQuotes)
588 d->mDoubleFrenchQuotes = newDoubleFrenchQuotes;
591QString AutoCorrectionSettings::customWritablePath()
const
593 return d->mCustomWritablePath;
596void AutoCorrectionSettings::setCustomWritablePath(
const QString &path)
598 d->mCustomWritablePath =
path;
601QString AutoCorrectionSettings::customSystemPath()
const
603 return d->mCustomSystemPath;
606void AutoCorrectionSettings::setCustomSystemPath(
const QString &path)
608 d->mCustomSystemPath =
path;
613 d <<
"mAddNonBreakingSpace " << t.nonBreakingSpace();
614 d <<
"mSuperScriptAppendix " << t.isSuperScript();
615 d <<
"mEnabled " << t.isEnabledAutoCorrection();
616 d <<
"mAutoBoldUnderline " << t.isAutoBoldUnderline();
617 d <<
"mAutoFormatUrl " << t.isAutoFormatUrl();
618 d <<
"mReplaceSingleQuotes " << t.isReplaceSingleQuotes();
619 d <<
"mReplaceDoubleQuotes " << t.isReplaceDoubleQuotes();
620 d <<
"mAdvancedAutocorrect " << t.isAdvancedAutocorrect();
621 d <<
"mCapitalizeWeekDays " << t.isCapitalizeWeekDays();
622 d <<
"mAutoFractions " << t.isAutoFractions();
623 d <<
"mFixTwoUppercaseChars " << t.isFixTwoUppercaseChars();
624 d <<
"mUppercaseFirstCharOfSentence " << t.isUppercaseFirstCharOfSentence();
625 d <<
"mSingleSpaces " << t.isSingleSpaces();
626 d <<
"mMaxFindStringLength " << t.maxFindStringLength();
627 d <<
"mMinFindStringLength " << t.minFindStringLength();
628 d <<
"mNonBreakingSpace " << t.nonBreakingSpace();
629 d <<
"mAutoCorrectLang " << t.language();
630 d <<
"mTwoUpperLetterExceptions " << t.twoUpperLetterExceptions();
631 d <<
"mUpperCaseExceptions " << t.upperCaseExceptions();
632 d <<
"mSuperScriptEntries " << t.superScriptEntries();
633 d <<
"mAutocorrectEntries " << t.autocorrectEntries();
634 d <<
"mTypographicDoubleQuotes " << t.typographicDoubleQuotes();
635 d <<
"mTypographicSingleQuotes " << t.typographicSingleQuotes();
636 d <<
"mReplaceDoubleQuotesByFrenchQuotes " << t.isReplaceDoubleQuotesByFrenchQuotes();
void requestSync()
Call this slot instead of directly KConfig::sync() to minimize the overall config writes.
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
QString path(const QString &relativePath)
const_reference at(qsizetype i) const const
bool isEmpty() const const
QStringList uiLanguages() const const
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)