kspell_aspelldict.cpp
00001 00021 #include "kspell_aspelldict.h" 00022 00023 #include <kdebug.h> 00024 00025 #include <qtextcodec.h> 00026 00027 using namespace KSpell2; 00028 00029 ASpellDict::ASpellDict( const QString& lang ) 00030 : Dictionary( lang ) 00031 { 00032 m_config = new_aspell_config(); 00033 aspell_config_replace( m_config, "lang", lang.latin1() ); 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 kdDebug()<< "Error : "<< aspell_error_message( possible_err ) <<endl; 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::check( const QString& word ) 00054 { 00055 /* ASpell is expecting length of a string in char representation */ 00056 /* word.length() != word.utf8().length() for nonlatin strings */ 00057 int correct = aspell_speller_check( m_speller, word.utf8(), word.utf8().length() ); 00058 return correct; 00059 } 00060 00061 QStringList ASpellDict::suggest( const QString& word ) 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.utf8().length() for nonlatin strings */ 00068 const AspellWordList * suggestions = aspell_speller_suggest( m_speller, 00069 word.utf8(), 00070 word.utf8().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 bool ASpellDict::checkAndSuggest( const QString& word, 00088 QStringList& suggestions ) 00089 { 00090 bool c = check( word ); 00091 if ( c ) 00092 suggestions = suggest( word ); 00093 return c; 00094 } 00095 00096 bool ASpellDict::storeReplacement( const QString& bad, 00097 const QString& good ) 00098 { 00099 /* ASpell is expecting length of a string in char representation */ 00100 /* word.length() != word.utf8().length() for nonlatin strings */ 00101 return aspell_speller_store_replacement( m_speller, 00102 bad.utf8(), bad.utf8().length(), 00103 good.utf8(), good.utf8().length() ); 00104 } 00105 00106 bool ASpellDict::addToPersonal( const QString& word ) 00107 { 00108 kdDebug() << "ASpellDict::addToPersonal: word = " << word << endl; 00109 /* ASpell is expecting length of a string in char representation */ 00110 /* word.length() != word.utf8().length() for nonlatin strings */ 00111 aspell_speller_add_to_personal( m_speller, word.utf8(), 00112 word.utf8().length() ); 00113 /* Add is not enough, one has to save it. This is not documented */ 00114 /* in ASpell's API manual. I found it in */ 00115 /* aspell-0.60.2/example/example-c.c */ 00116 return aspell_speller_save_all_word_lists( m_speller ); 00117 } 00118 00119 bool ASpellDict::addToSession( const QString& word ) 00120 { 00121 /* ASpell is expecting length of a string in char representation */ 00122 /* word.length() != word.utf8().length() for nonlatin strings */ 00123 return aspell_speller_add_to_session( m_speller, word.utf8(), 00124 word.utf8().length() ); 00125 }