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), m_speller(0) 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 #ifdef Q_WS_WIN 00039 QString aspell_data_dir(); 00040 aspell_config_replace( m_config, "data-dir", aspell_data_dir().toLocal8Bit().data()); 00041 aspell_config_replace( m_config, "dict-dir", aspell_data_dir().toLocal8Bit().data()); 00042 #endif 00043 00044 AspellCanHaveError * possible_err = new_aspell_speller( m_config ); 00045 00046 if ( aspell_error_number( possible_err ) != 0 ) 00047 kDebug()<< "Error : "<< aspell_error_message( possible_err ); 00048 else 00049 m_speller = to_aspell_speller( possible_err ); 00050 00051 } 00052 00053 ASpellDict::~ASpellDict() 00054 { 00055 delete_aspell_speller( m_speller ); 00056 delete_aspell_config( m_config ); 00057 } 00058 00059 bool ASpellDict::isCorrect(const QString &word) const 00060 { 00061 /* ASpell is expecting length of a string in char representation */ 00062 /* word.length() != word.toUtf8().length() for nonlatin strings */ 00063 if (!m_speller) 00064 return false; 00065 int correct = aspell_speller_check( m_speller, word.toUtf8(), word.toUtf8().length() ); 00066 return correct; 00067 } 00068 00069 QStringList ASpellDict::suggest(const QString &word) const 00070 { 00071 if (!m_speller) 00072 return QStringList(); 00073 /* Needed for Unicode conversion */ 00074 QTextCodec *codec = QTextCodec::codecForName("utf8"); 00075 00076 /* ASpell is expecting length of a string in char representation */ 00077 /* word.length() != word.toUtf8().length() for nonlatin strings */ 00078 const AspellWordList * suggestions = aspell_speller_suggest( m_speller, 00079 word.toUtf8(), 00080 word.toUtf8().length() ); 00081 00082 AspellStringEnumeration * elements = aspell_word_list_elements( suggestions ); 00083 00084 QStringList qsug; 00085 const char * cword; 00086 00087 while ( (cword = aspell_string_enumeration_next( elements )) ) { 00088 /* Since while creating the class ASpellDict the encoding is set */ 00089 /* to utf-8, one has to convert output from Aspell to Unicode */ 00090 qsug.append( codec->toUnicode( cword ) ); 00091 } 00092 00093 delete_aspell_string_enumeration( elements ); 00094 return qsug; 00095 } 00096 00097 00098 bool ASpellDict::storeReplacement( const QString& bad, 00099 const QString& good ) 00100 { 00101 if (!m_speller) 00102 return false; 00103 /* ASpell is expecting length of a string in char representation */ 00104 /* word.length() != word.toUtf8().length() for nonlatin strings */ 00105 return aspell_speller_store_replacement( m_speller, 00106 bad.toUtf8(), bad.toUtf8().length(), 00107 good.toUtf8(), good.toUtf8().length() ); 00108 } 00109 00110 bool ASpellDict::addToPersonal( const QString& word ) 00111 { 00112 if (!m_speller) 00113 return false; 00114 kDebug() << "ASpellDict::addToPersonal: word = " << word; 00115 /* ASpell is expecting length of a string in char representation */ 00116 /* word.length() != word.toUtf8().length() for nonlatin strings */ 00117 aspell_speller_add_to_personal( m_speller, word.toUtf8(), 00118 word.toUtf8().length() ); 00119 /* Add is not enough, one has to save it. This is not documented */ 00120 /* in ASpell's API manual. I found it in */ 00121 /* aspell-0.60.2/example/example-c.c */ 00122 return aspell_speller_save_all_word_lists( m_speller ); 00123 } 00124 00125 bool ASpellDict::addToSession( const QString& word ) 00126 { 00127 if (!m_speller) 00128 return false; 00129 /* ASpell is expecting length of a string in char representation */ 00130 /* word.length() != word.toUtf8().length() for nonlatin strings */ 00131 return aspell_speller_add_to_session( m_speller, word.toUtf8(), 00132 word.toUtf8().length() ); 00133 }
KDE 4.6 API Reference