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

Sonnet

kspell_aspelldict.cpp

Go to the documentation of this file.
00001 
00021 #include "kspell_aspelldict.h"
00022 
00023 #include <kdebug.h>
00024 
00025 #include <QtCore/QTextCodec>
00026 
00027 using namespace Sonnet;
00028 
00029 ASpellDict::ASpellDict( const QString& lang )
00030     : SpellerPlugin(lang)
00031 {
00032     m_config = new_aspell_config();
00033     aspell_config_replace( m_config, "lang", lang.toLatin1() );
00034     /* All communication with Aspell is done in UTF-8 */
00035     /* For reference, please look at BR#87250         */
00036     aspell_config_replace( m_config, "encoding", "utf-8" );
00037 
00038     AspellCanHaveError * possible_err = new_aspell_speller( m_config );
00039 
00040     if ( aspell_error_number( possible_err ) != 0 )
00041         kDebug()<< "Error : "<< aspell_error_message( possible_err );
00042     else
00043         m_speller = to_aspell_speller( possible_err );
00044 
00045 }
00046 
00047 ASpellDict::~ASpellDict()
00048 {
00049     delete_aspell_speller( m_speller );
00050     delete_aspell_config( m_config );
00051 }
00052 
00053 bool ASpellDict::isCorrect(const QString &word) const
00054 {
00055     /* ASpell is expecting length of a string in char representation */
00056     /* word.length() != word.toUtf8().length() for nonlatin strings    */
00057     int correct = aspell_speller_check( m_speller, word.toUtf8(), word.toUtf8().length() );
00058     return correct;
00059 }
00060 
00061 QStringList ASpellDict::suggest(const QString &word) const
00062 {
00063     /* Needed for Unicode conversion */
00064     QTextCodec *codec = QTextCodec::codecForName("utf8");
00065 
00066     /* ASpell is expecting length of a string in char representation */
00067     /* word.length() != word.toUtf8().length() for nonlatin strings    */
00068     const AspellWordList * suggestions = aspell_speller_suggest( m_speller,
00069                                                                  word.toUtf8(),
00070                                                                  word.toUtf8().length() );
00071 
00072     AspellStringEnumeration * elements = aspell_word_list_elements( suggestions );
00073 
00074     QStringList qsug;
00075     const char * cword;
00076 
00077     while ( (cword = aspell_string_enumeration_next( elements )) ) {
00078         /* Since while creating the class ASpellDict the encoding is set */
00079         /* to utf-8, one has to convert output from Aspell to Unicode    */
00080         qsug.append( codec->toUnicode( cword ) );
00081     }
00082 
00083     delete_aspell_string_enumeration( elements );
00084     return qsug;
00085 }
00086 
00087 
00088 bool ASpellDict::storeReplacement( const QString& bad,
00089                                    const QString& good )
00090 {
00091     /* ASpell is expecting length of a string in char representation */
00092     /* word.length() != word.toUtf8().length() for nonlatin strings    */
00093     return aspell_speller_store_replacement( m_speller,
00094                                              bad.toUtf8(), bad.toUtf8().length(),
00095                                              good.toUtf8(), good.toUtf8().length() );
00096 }
00097 
00098 bool ASpellDict::addToPersonal( const QString& word )
00099 {
00100     kDebug() << "ASpellDict::addToPersonal: word = " << word;
00101     /* ASpell is expecting length of a string in char representation */
00102     /* word.length() != word.toUtf8().length() for nonlatin strings    */
00103     aspell_speller_add_to_personal( m_speller, word.toUtf8(),
00104                                     word.toUtf8().length() );
00105     /* Add is not enough, one has to save it. This is not documented */
00106     /* in ASpell's API manual. I found it in                         */
00107     /* aspell-0.60.2/example/example-c.c                             */
00108     return aspell_speller_save_all_word_lists( m_speller );
00109 }
00110 
00111 bool ASpellDict::addToSession( const QString& word )
00112 {
00113     /* ASpell is expecting length of a string in char representation */
00114     /* word.length() != word.toUtf8().length() for nonlatin strings    */
00115     return aspell_speller_add_to_session( m_speller, word.toUtf8(),
00116                                           word.toUtf8().length() );
00117 }

Sonnet

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal