• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Sonnet

  • sources
  • kde-4.14
  • kdelibs
  • sonnet
  • plugins
  • aspell
kspell_aspelldict.cpp
Go to the documentation of this file.
1 
21 #include "kspell_aspelldict.h"
22 
23 #include <kdebug.h>
24 
25 #include <QtCore/QTextCodec>
26 
27 using namespace Sonnet;
28 
29 ASpellDict::ASpellDict( const QString& lang )
30  : SpellerPlugin(lang), m_speller(0)
31 {
32  m_config = new_aspell_config();
33  aspell_config_replace( m_config, "lang", lang.toLatin1() );
34  /* All communication with Aspell is done in UTF-8 */
35  /* For reference, please look at BR#87250 */
36  aspell_config_replace( m_config, "encoding", "utf-8" );
37 
38 #ifdef Q_WS_WIN
39  QString aspell_data_dir();
40  aspell_config_replace( m_config, "data-dir", aspell_data_dir().toLocal8Bit().data());
41  aspell_config_replace( m_config, "dict-dir", aspell_data_dir().toLocal8Bit().data());
42 #endif
43 
44  AspellCanHaveError * possible_err = new_aspell_speller( m_config );
45 
46  if ( aspell_error_number( possible_err ) != 0 )
47  kDebug()<< "Error : "<< aspell_error_message( possible_err );
48  else
49  m_speller = to_aspell_speller( possible_err );
50 
51 }
52 
53 ASpellDict::~ASpellDict()
54 {
55  delete_aspell_speller( m_speller );
56  delete_aspell_config( m_config );
57 }
58 
59 bool ASpellDict::isCorrect(const QString &word) const
60 {
61  /* ASpell is expecting length of a string in char representation */
62  /* word.length() != word.toUtf8().length() for nonlatin strings */
63  if (!m_speller)
64  return false;
65  int correct = aspell_speller_check( m_speller, word.toUtf8(), word.toUtf8().length() );
66  return correct;
67 }
68 
69 QStringList ASpellDict::suggest(const QString &word) const
70 {
71  if (!m_speller)
72  return QStringList();
73  /* Needed for Unicode conversion */
74  QTextCodec *codec = QTextCodec::codecForName("utf8");
75 
76  /* ASpell is expecting length of a string in char representation */
77  /* word.length() != word.toUtf8().length() for nonlatin strings */
78  const AspellWordList * suggestions = aspell_speller_suggest( m_speller,
79  word.toUtf8(),
80  word.toUtf8().length() );
81 
82  AspellStringEnumeration * elements = aspell_word_list_elements( suggestions );
83 
84  QStringList qsug;
85  const char * cword;
86 
87  while ( (cword = aspell_string_enumeration_next( elements )) ) {
88  /* Since while creating the class ASpellDict the encoding is set */
89  /* to utf-8, one has to convert output from Aspell to Unicode */
90  qsug.append( codec->toUnicode( cword ) );
91  }
92 
93  delete_aspell_string_enumeration( elements );
94  return qsug;
95 }
96 
97 
98 bool ASpellDict::storeReplacement( const QString& bad,
99  const QString& good )
100 {
101  if (!m_speller)
102  return false;
103  /* ASpell is expecting length of a string in char representation */
104  /* word.length() != word.toUtf8().length() for nonlatin strings */
105  return aspell_speller_store_replacement( m_speller,
106  bad.toUtf8(), bad.toUtf8().length(),
107  good.toUtf8(), good.toUtf8().length() );
108 }
109 
110 bool ASpellDict::addToPersonal( const QString& word )
111 {
112  if (!m_speller)
113  return false;
114  kDebug() << "ASpellDict::addToPersonal: word = " << word;
115  /* ASpell is expecting length of a string in char representation */
116  /* word.length() != word.toUtf8().length() for nonlatin strings */
117  aspell_speller_add_to_personal( m_speller, word.toUtf8(),
118  word.toUtf8().length() );
119  /* Add is not enough, one has to save it. This is not documented */
120  /* in ASpell's API manual. I found it in */
121  /* aspell-0.60.2/example/example-c.c */
122  return aspell_speller_save_all_word_lists( m_speller );
123 }
124 
125 bool ASpellDict::addToSession( const QString& word )
126 {
127  if (!m_speller)
128  return false;
129  /* ASpell is expecting length of a string in char representation */
130  /* word.length() != word.toUtf8().length() for nonlatin strings */
131  return aspell_speller_add_to_session( m_speller, word.toUtf8(),
132  word.toUtf8().length() );
133 }
kdebug.h
ASpellDict::~ASpellDict
~ASpellDict()
Definition: kspell_aspelldict.cpp:53
QByteArray::length
int length() const
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
aspell_data_dir
QString aspell_data_dir()
Definition: kspell_aspellclient.cpp:38
QList::append
void append(const T &value)
ASpellDict::addToSession
virtual bool addToSession(const QString &word)
Definition: kspell_aspelldict.cpp:125
ASpellDict::suggest
virtual QStringList suggest(const QString &word) const
Definition: kspell_aspelldict.cpp:69
ASpellDict::ASpellDict
ASpellDict(const QString &lang)
Definition: kspell_aspelldict.cpp:29
kspell_aspelldict.h
QString
QTextCodec
QStringList
ASpellDict::addToPersonal
virtual bool addToPersonal(const QString &word)
Definition: kspell_aspelldict.cpp:110
Sonnet::SpellerPlugin
QString::toLatin1
QByteArray toLatin1() const
QTextCodec::codecForName
QTextCodec * codecForName(const QByteArray &name)
ASpellDict::storeReplacement
virtual bool storeReplacement(const QString &bad, const QString &good)
Definition: kspell_aspelldict.cpp:98
ASpellDict::isCorrect
virtual bool isCorrect(const QString &word) const
Definition: kspell_aspelldict.cpp:59
QTextCodec::toUnicode
QString toUnicode(const QByteArray &a) const
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Sonnet

Skip menu "Sonnet"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal