Subversion Repositories Scribus

Rev

Rev 25057 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17888 jghali 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
 
8
#include "hunspelldict.h"
9
 
10
#include <hunspell/hunspell.hxx>
21738 craig 11
#include <QDebug>
24986 craig 12
#include <QString>
17888 jghali 13
#include <QTextCodec>
14
 
21738 craig 15
#include "scconfig.h"
16
 
17888 jghali 17
HunspellDict::HunspellDict(const QString& affPath, const QString& dictPath)
18
{
19
        QString encoding = "ISO8859-1";
20
        m_hunspell = new Hunspell(affPath.toLocal8Bit().constData(), dictPath.toLocal8Bit().constData());
21
        if (m_hunspell)
22
        {
21531 craig 23
                const char* dictEncoding = m_hunspell->get_dic_encoding();
17888 jghali 24
                if (dictEncoding)
25
                        encoding = QString::fromLatin1(dictEncoding);
26
        }
27
 
28
        if (encoding.isEmpty())
29
                encoding = "ISO8859-1";
30
        m_codec = QTextCodec::codecForName(encoding.toLatin1().constData());
31
}
32
 
33
HunspellDict::~HunspellDict()
34
{
22630 craig 35
        delete m_hunspell;
36
        m_hunspell = nullptr;
17888 jghali 37
}
38
 
22633 jghali 39
int HunspellDict::spell(const QString& word)
17888 jghali 40
{
21738 craig 41
        if (!m_hunspell)
42
                return -1;
21739 craig 43
        std::string s = m_codec->fromUnicode(word).toStdString();
21738 craig 44
        return m_hunspell->spell(s);
45
}
46
 
22635 craig 47
QStringList HunspellDict::suggest(const QString& word)
21738 craig 48
{
49
        QStringList replacements;
50
        if (!m_hunspell)
51
                return replacements;
52
        std::string s = word.toStdString();
53
        std::vector<std::string> sugglist = m_hunspell->suggest(s);
25057 jghali 54
        replacements.reserve(sugglist.size());
21738 craig 55
        for (uint i = 0; i < sugglist.size(); ++i)
21739 craig 56
                replacements << m_codec->toUnicode(QByteArray::fromStdString(sugglist[i]));
21738 craig 57
        return replacements;
58
}