/***************************************************************************
*   Copyright (C) 2006-2008 by Ian Reinhart Geiser                        *
*   geiseri@yahoo.com                                                     *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "qaspell.h"

#ifndef Q_WS_MACX
#include <hunspell.hxx>
#else
#include <macspell.h>
#endif

#include  <QString>
#include  <QStringList>
#include  <QVector>
#include  <QCoreApplication>
#include  <QDir>
#include <qdebug.h>

QASpell::QASpell() : m_speller(0)
{
#ifndef Q_WS_MACX

# if defined Q_WS_WIN
	QByteArray dict = (QCoreApplication::applicationDirPath() + QDir::separator() + "dict" + QDir::separator() + "en_US.dic").toLatin1();
	QByteArray affix = (QCoreApplication::applicationDirPath() + QDir::separator() + "dict" + QDir::separator() + "en_US.aff").toLatin1();
# else
	QByteArray dict = "/usr/share/myspell/dicts/en_US.dic";
	QByteArray affix = "/usr/share/myspell/dicts/en_US.aff";

# endif
	m_speller = new Hunspell( affix.constData(), dict.constData() );
#endif
}

QASpell::~QASpell()
{
#ifndef Q_WS_MACX
	delete m_speller;
#endif
}

bool QASpell::configureSpellchecker( const char *language )
{
#ifndef Q_WS_MACX
/*
	aspell_config_replace(m_config, "lang", language);

	if( m_speller )
		delete_aspell_speller( m_speller );

	AspellCanHaveError * possible_err = new_aspell_speller(m_config);
	m_speller = 0;
	if (aspell_error_number(possible_err) != 0)
		qWarning( aspell_error_message(possible_err) );
	else
		m_speller = to_aspell_speller(possible_err);

	if( m_speller == 0 )
		return false;
*/
#endif
	return true;
}

bool QASpell::checkWord( const QString &word) const
{
#ifndef Q_WS_MACX

	if( m_speller == 0 )
		return false;
	return (m_speller->spell(word.toUtf8().constData()) == 1);
#else
	return QMacSpell::checkWord(word);
#endif
}

QStringList QASpell::getSuggestions( const QString &word) const
{
	QStringList suggestionList;
#ifndef Q_WS_MACX
	if( m_speller == 0 )
		return QStringList();

	char ** wlst;
	int ns = m_speller->suggest( &wlst, word.toUtf8().constData() );
	for (int idx=0; idx < ns; idx++)
	{
		suggestionList << QString::fromUtf8( wlst[idx] );
		delete wlst[idx];
	}
	delete wlst;
#else
	suggestionList = QMacSpell::getSuggestions(word);
#endif
	return suggestionList;
}





