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