Sonnet

hspelldict.cpp
1 /*
2  * kspell_hspelldict.cpp
3  *
4  * SPDX-FileCopyrightText: 2003 Zack Rusin <[email protected]>
5  * SPDX-FileCopyrightText: 2005 Mashrab Kuvatov <[email protected]>
6  * SPDX-FileCopyrightText: 2013 Martin Sandsmark <[email protected]>
7  *
8  * SPDX-License-Identifier: LGPL-2.1-or-later
9  */
10 
11 #include "hspelldict.h"
12 
13 #include "hspell_debug.h"
14 
15 #include <QSettings>
16 #include <QTextCodec>
17 
18 using namespace Sonnet;
19 
20 HSpellDict::HSpellDict(const QString &lang)
21  : SpellerPlugin(lang)
22 {
23  int int_error = hspell_init(&m_speller, HSPELL_OPT_DEFAULT);
24  if (int_error == -1) {
25  qCWarning(SONNET_LOG_HSPELL) << "HSpellDict::HSpellDict: Init failed";
26  initialized = false;
27  } else {
28  /* hspell understands only iso8859-8-i */
29  codec = QTextCodec::codecForName("iso8859-8-i");
30  initialized = true;
31  }
32 
33  QSettings settings(QStringLiteral("KDE"), QStringLiteral("SonnetHSpellPlugin"));
34  const QStringList personalWordsList = settings.value(QStringLiteral("PersonalWords"), QStringList()).toStringList();
35  m_personalWords = QSet<QString>(personalWordsList.begin(), personalWordsList.end());
36  QVariantHash replacementMap = settings.value(QStringLiteral("Replacements"), QVariant()).toHash();
37  for (const QString &key : replacementMap.keys()) {
38  m_replacements[key] = replacementMap[key].toString();
39  }
40 }
41 
42 HSpellDict::~HSpellDict()
43 {
44  /* It exists in =< hspell-0.8 */
45  if (initialized) {
46  hspell_uninit(m_speller);
47  }
48 }
49 
50 bool HSpellDict::isCorrect(const QString &word) const
51 {
52  if (m_sessionWords.contains(word)) {
53  return true;
54  }
55 
56  if (m_personalWords.contains(word)) {
57  return true;
58  }
59 
60  if (!initialized) {
61  // Not much we can do, so just return true (less annoying for the user)
62  return true;
63  }
64 
65  int preflen;
66  QByteArray wordISO = codec->fromUnicode(word);
67 
68  // returns 1 if the word is correct, 0 otherwise
69  int correct = hspell_check_word(m_speller, wordISO.constData(),
70  &preflen); // this argument might be removed, it isn't useful
71 
72  // gimatria is a representation of numbers with hebrew letters, we accept these
73  if (correct != 1) {
74  if (hspell_is_canonic_gimatria(wordISO.constData()) != 0) {
75  correct = 1;
76  }
77  }
78  return correct == 1;
79 }
80 
81 QStringList HSpellDict::suggest(const QString &word) const
82 {
83  QStringList suggestions;
84 
85  if (m_replacements.contains(word)) {
86  suggestions.append(m_replacements[word]);
87  }
88 
89  struct corlist correctionList;
90  int suggestionCount;
91  corlist_init(&correctionList);
92  hspell_trycorrect(m_speller, codec->fromUnicode(word).constData(), &correctionList);
93  for (suggestionCount = 0; suggestionCount < corlist_n(&correctionList); suggestionCount++) {
94  suggestions.append(codec->toUnicode(corlist_str(&correctionList, suggestionCount)));
95  }
96  corlist_free(&correctionList);
97  return suggestions;
98 }
99 
100 bool HSpellDict::storeReplacement(const QString &bad, const QString &good)
101 {
102  m_replacements[bad] = good;
103  storePersonalWords();
104  return true;
105 }
106 
107 bool HSpellDict::addToPersonal(const QString &word)
108 {
109  m_personalWords.insert(word);
110  storePersonalWords();
111  return true;
112 }
113 
114 bool HSpellDict::addToSession(const QString &word)
115 {
116  m_sessionWords.insert(word);
117  return true;
118 }
119 
120 void HSpellDict::storePersonalWords()
121 {
122  QSettings settings(QStringLiteral("KDE"), QStringLiteral("SonnetHSpellPlugin"));
123  const QStringList personalWordsList(m_personalWords.begin(), m_personalWords.end());
124  settings.setValue(QStringLiteral("PersonalWords"), QVariant(personalWordsList));
125  QVariantHash variantHash;
126  for (const QString &key : m_replacements.keys()) {
127  variantHash[key] = QVariant(m_replacements[key]);
128  }
129  settings.setValue(QStringLiteral("Replacements"), variantHash);
130 }
void append(const T &value)
QTextCodec * codecForName(const QByteArray &name)
The sonnet namespace.
const char * constData() 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.