enchantdict.cpp

00001 // -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00021 #include "enchantdict.h"
00022 #include "enchantclient.h"
00023 
00024 #include <qtextcodec.h>
00025 #include <kdebug.h>
00026 
00027 using namespace KSpell2;
00028 
00029 QSpellEnchantDict::QSpellEnchantDict(QSpellEnchantClient *client, 
00030                                      EnchantBroker *broker,
00031                                      EnchantDict *dict,
00032                                      const QString &language)
00033     : Dictionary(language),
00034       m_broker(broker),
00035       m_dict(dict),
00036       m_client(client)
00037 {
00038     kdDebug()<<"Enchant dict for"<<language << dict << endl;
00039 }
00040 
00041 QSpellEnchantDict::~QSpellEnchantDict()
00042 {
00043     //Enchant caches dictionaries, so it will always return the same one.
00044     // therefore we do not want to delete the EnchantDict here but in the
00045     // client when it knows that nothing is using it anymore
00046     m_client->removeDictRef(m_dict);
00047 }
00048 
00049 bool QSpellEnchantDict::check(const QString &word)
00050 {
00051     int wrong = enchant_dict_check(m_dict, word.utf8(),
00052                                    word.utf8().length());
00053     return !wrong;
00054 }
00055 
00056 QStringList QSpellEnchantDict::suggest(const QString &word)
00057 {
00058     /* Needed for Unicode conversion */
00059     QTextCodec *codec = QTextCodec::codecForName("utf8");
00060 
00061     size_t number = 0;
00062     char **suggestions =
00063         enchant_dict_suggest(m_dict, word.utf8(), word.utf8().length(),
00064                              &number);
00065 
00066     QStringList qsug;
00067     for (size_t i = 0; i < number; ++i) {
00068         qsug.append(codec->toUnicode(suggestions[i]));
00069     }
00070 
00071     if (suggestions && number)
00072         enchant_dict_free_string_list(m_dict, suggestions);
00073     return qsug;
00074 }
00075 
00076 bool QSpellEnchantDict::checkAndSuggest(const QString& word,
00077                                         QStringList& suggestions)
00078 {
00079     bool c = check(word);
00080     if (c)
00081         suggestions = suggest(word);
00082     return c;
00083 }
00084 
00085 bool QSpellEnchantDict::storeReplacement(const QString &bad,
00086                                   const QString &good)
00087 {
00088     enchant_dict_store_replacement(m_dict,
00089                                    bad.utf8(), bad.utf8().length(),
00090                                    good.utf8(), good.utf8().length());
00091     return true;
00092 }
00093 
00094 bool QSpellEnchantDict::addToPersonal(const QString &word)
00095 {
00096     kdDebug() << "QSpellEnchantDict::addToPersonal: word = "
00097               << word << endl;
00098     enchant_dict_add_to_pwl(m_dict, word.utf8(),
00099                             word.utf8().length());
00100     return true;
00101 }
00102 
00103 bool QSpellEnchantDict::addToSession(const QString &word)
00104 {
00105     enchant_dict_add_to_session(m_dict, word.utf8(),
00106                                 word.utf8().length());
00107     return true;
00108 }
KDE Home | KDE Accessibility Home | Description of Access Keys