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
00035
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
00062
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
00074 QTextCodec *codec = QTextCodec::codecForName("utf8");
00075
00076
00077
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
00089
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
00104
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
00116
00117 aspell_speller_add_to_personal( m_speller, word.toUtf8(),
00118 word.toUtf8().length() );
00119
00120
00121
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
00130
00131 return aspell_speller_add_to_session( m_speller, word.toUtf8(),
00132 word.toUtf8().length() );
00133 }