KTextAddons

autocorrectionutils.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "autocorrectionutils.h"
8#include <QCoreApplication>
9#include <QDir>
10#include <QFileInfo>
11#include <QStandardPaths>
12using namespace TextAutoCorrectionCore;
13AutoCorrectionUtils::TypographicQuotes AutoCorrectionUtils::typographicDefaultSingleQuotes()
14{
15 TypographicQuotes quote;
16 quote.begin = QChar(0x2018);
17 quote.end = QChar(0x2019);
18 return quote;
19}
20
21AutoCorrectionUtils::TypographicQuotes AutoCorrectionUtils::typographicDefaultDoubleQuotes()
22{
23 TypographicQuotes quote;
24 quote.begin = QChar(0x201c);
25 quote.end = QChar(0x201d);
26 return quote;
27}
28
29AutoCorrectionUtils::TypographicQuotes AutoCorrectionUtils::typographicDefaultFrenchQuotes()
30{
31 TypographicQuotes quote;
32 quote.begin = QChar(0x00AB);
33 quote.end = QChar(0x00BB);
34 return quote;
35}
36
37QDebug operator<<(QDebug d, TextAutoCorrectionCore::AutoCorrectionUtils::TypographicQuotes t)
38{
39 d << "TypographicQuotes.begin " << t.begin;
40 d << "TypographicQuotes.end " << t.end;
41 return d;
42}
43
44QString AutoCorrectionUtils::libreofficeFile(const QString &lang)
45{
46 return QStringLiteral("acor_%1.dat").arg(lang);
47}
48
49QStringList AutoCorrectionUtils::libreOfficeAutoCorrectionPath()
50{
51 QStringList dirList;
52 auto maybeAddPath = [&dirList](const QString &path) {
53 if (QFileInfo::exists(path)) {
54 dirList.append(path);
55 }
56 };
57 maybeAddPath(libreOfficeWritableLocalAutoCorrectionPath());
58 maybeAddPath(libreOfficeSystemPath());
59 return dirList;
60}
61
62QString AutoCorrectionUtils::libreOfficeSystemPath()
63{
64#ifdef Q_OS_WIN
65 return QStringLiteral("c:/Program Files/LibreOffice/share/autocorr/");
66#else
67#ifdef Q_OS_MACOS
68 return QStringLiteral("/Applications/LibreOffice.app/Contents/Resources/autocorr");
69#else
70 return QStringLiteral("/usr/lib64/libreoffice/share/autocorr/");
71#endif
72#endif
73}
74
75QString AutoCorrectionUtils::libreOfficeLocalPath()
76{
77#ifdef Q_OS_MACOS
78 // It seems that they don't use lowercase
79 return QStringLiteral("/LibreOffice/4/user/autocorr/");
80#else
81 return QStringLiteral("/libreoffice/4/user/autocorr/");
82#endif
83}
84
85QString AutoCorrectionUtils::libreOfficeWritableLocalAutoCorrectionPath()
86{
87#ifdef Q_OS_WIN
88 const QString writeablePath =
90 + AutoCorrectionUtils::libreOfficeLocalPath();
91 return writeablePath;
92#else
93#ifdef Q_OS_MACOS
94 // $HOME/Library/Application Support/OpenOffice/4/user/autocorr
95 const QString writeablePath =
97 + AutoCorrectionUtils::libreOfficeLocalPath();
98 return writeablePath;
99#else
101#endif
102#endif
103}
104
105QStringList AutoCorrectionUtils::searchAutoCorrectLibreOfficeFiles()
106{
107 QStringList files;
108 const QString path = libreOfficeSystemPath();
109 if (QFileInfo::exists(path)) {
110 QDir dir(path);
111 const QStringList entryList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
112 for (const QString &file : entryList) {
113 QString curFile = file;
114 curFile.remove(path);
115 curFile.remove(QStringLiteral(".dat"));
116 curFile.remove(QStringLiteral("acor_"));
117 files.append(curFile);
118 }
119 }
120 return files;
121}
122
123QStringList AutoCorrectionUtils::autoCorrectLibreOfficeLanguageToString(const QStringList &langs)
124{
125 QStringList languagesStr;
127 for (const QLocale &locale : allLocales) {
128 const QString languageCode = locale.name();
129 for (const QString &lang : langs) {
130 if (languageCode == lang) {
131 const QString nativeName = locale.nativeLanguageName();
132 // For some languages the native name might be empty.
133 // In this case use the non native language name as fallback.
134 // See: QTBUG-51323
135 const QString languageName = nativeName.isEmpty() ? QLocale::languageToString(locale.language()) : nativeName;
136 languagesStr.append(languageName);
137 break;
138 } else {
139 QString b = lang;
140 b.replace(QLatin1Char('-'), QLatin1Char('_'));
141 if (languageCode == b) {
142 const QString nativeName = locale.nativeLanguageName();
143 // For some languages the native name might be empty.
144 // In this case use the non native language name as fallback.
145 // See: QTBUG-51323
146 const QString languageName = nativeName.isEmpty() ? QLocale::languageToString(locale.language()) : nativeName;
147 languagesStr.append(languageName);
148 break;
149 }
150 }
151 }
152 }
153 return languagesStr;
154}
155
156QString AutoCorrectionUtils::TypographicQuotes::toString() const
157{
158 return begin + end;
159}
160
161bool AutoCorrectionUtils::TypographicQuotes::isEmpty() const
162{
163 return begin.isNull() && end.isNull();
164}
165
166AutoCorrectionUtils::TypographicQuotes AutoCorrectionUtils::TypographicQuotes::fromString(const QString &str)
167{
168 AutoCorrectionUtils::TypographicQuotes quotes;
169 if (str.size() == 2) {
170 quotes.begin = str.at(0);
171 quotes.end = str.at(1);
172 }
173 return quotes;
174}
175
176QString AutoCorrectionUtils::containsAutoCorrectionFile(const QString &lang, const QString &customSystemPath, const QString &customWritablePath)
177{
178 QStringList libreOfficeAutocorrectPaths;
179 if (!customWritablePath.isEmpty()) {
180 libreOfficeAutocorrectPaths.append(customWritablePath);
181 }
182 if (!customSystemPath.isEmpty()) {
183 libreOfficeAutocorrectPaths.append(customSystemPath);
184 }
185 libreOfficeAutocorrectPaths += AutoCorrectionUtils::libreOfficeAutoCorrectionPath();
186 if (!libreOfficeAutocorrectPaths.isEmpty()) {
187 QString fixLangExtension = lang;
188 fixLangExtension.replace(QLatin1Char('_'), QLatin1Char('-'));
189 for (const auto &path : std::as_const(libreOfficeAutocorrectPaths)) {
190 const QString filename = path + AutoCorrectionUtils::libreofficeFile(fixLangExtension);
191 // qDebug() << " filename " << filename;
192 if (QFileInfo::exists(filename)) {
193 return filename;
194 }
195 }
196 }
197 return {};
198}
199
200QStringList AutoCorrectionUtils::wordsFromSentence(const QString &string)
201{
202 QStringList lst;
203 if (!string.trimmed().isEmpty()) {
204 lst.append(string);
205 QString tmpString = string;
206 while (!tmpString.trimmed().isEmpty()) {
207 bool foundStr = false;
208 for (auto i = 0; i < tmpString.size(); i++) {
209 if (tmpString.at(i).isSpace()) {
210 QString value;
211 const auto pos = tmpString.size() - i - 1;
212 value = tmpString.last(pos);
213 if (!value.trimmed().isEmpty()) {
214 lst.append(value);
215 }
216 tmpString = value;
217 foundStr = true;
218 break;
219 }
220 }
221 if (!foundStr) {
222 break;
223 }
224 }
225 }
226 return lst;
227}
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
const QList< QKeySequence > & begin()
const QList< QKeySequence > & end()
KTEXTEDITOR_EXPORT QDebug operator<<(QDebug s, const MovingCursor &cursor)
bool isSpace(char32_t ucs4)
bool exists() const const
void append(QList< T > &&value)
bool isEmpty() const const
QString languageToString(Language language)
QList< QLocale > matchingLocales(QLocale::Language language, QLocale::Script script, QLocale::Territory territory)
QString writableLocation(StandardLocation type)
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
bool isEmpty() const const
QString last(qsizetype n) const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
qsizetype size() const const
QString trimmed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.