Example #1
0
void LLSpellChecker::addToCustomDictionary(const std::string& word)
{
	if (mHunspell)
	{
		mHunspell->add(word.c_str());
	}
	addToDictFile(getDictionaryUserPath() + mDictFile + DICT_CUSTOM_SUFFIX + ".dic", word);
	sSettingsChangeSignal();
}
Example #2
0
void LLSpellChecker::addToIgnoreList(const std::string& word)
{
	std::string word_lower(word);
	LLStringUtil::toLower(word_lower);
	if (mIgnoreList.end() != std::find(mIgnoreList.begin(), mIgnoreList.end(), word_lower))
	{
		mIgnoreList.push_back(word_lower);
		addToDictFile(getDictionaryUserPath() + mDictFile + DICT_IGNORE_SUFFIX + ".dic", word_lower);
		sSettingsChangeSignal();
	}
}
Example #3
0
// static
void LLSpellChecker::refreshDictionaryMap()
{
	const std::string app_path = getDictionaryAppPath();
	const std::string user_path = getDictionaryUserPath();

	// Load dictionary information (file name, friendly name, ...)
    std::string user_filename(user_path + DICT_FILE_MAIN);
	llifstream user_file(user_filename.c_str(), std::ios::binary);
	if ( (!user_file.is_open()) 
		|| (LLSDParser::PARSE_FAILURE == LLSDSerialize::fromXMLDocument(sDictMap, user_file)) 
		|| (0 == sDictMap.size()) )
	{
        std::string app_filename(app_path + DICT_FILE_MAIN);
		llifstream app_file(app_filename.c_str(), std::ios::binary);
		if ( (!app_file.is_open()) 
			|| (LLSDParser::PARSE_FAILURE == LLSDSerialize::fromXMLDocument(sDictMap, app_file)) 
			|| (0 == sDictMap.size()) )
		{
			return;
		}
	}

	// Load user installed dictionary information
	llifstream custom_file(user_filename.c_str(), std::ios::binary);
	if (custom_file.is_open())
	{
		LLSD custom_dict_map;
		LLSDSerialize::fromXMLDocument(custom_dict_map, custom_file);
		for (LLSD::array_iterator it = custom_dict_map.beginArray(); it != custom_dict_map.endArray(); ++it)
		{
			LLSD& dict_info = *it;
			dict_info["user_installed"] = true;
			setDictionaryData(dict_info);
		}
		custom_file.close();
	}

	// Look for installed dictionaries
	std::string tmp_app_path, tmp_user_path;
	for (LLSD::array_iterator it = sDictMap.beginArray(); it != sDictMap.endArray(); ++it)
	{
		LLSD& sdDict = *it;
		tmp_app_path = (sdDict.has("name")) ? app_path + sdDict["name"].asString() : LLStringUtil::null;
		tmp_user_path = (sdDict.has("name")) ? user_path + sdDict["name"].asString() : LLStringUtil::null;
		sdDict["installed"] = 
			(!tmp_app_path.empty()) && ((gDirUtilp->fileExists(tmp_user_path + ".dic")) || (gDirUtilp->fileExists(tmp_app_path + ".dic")));
	}

	sSettingsChangeSignal();
}
Example #4
0
void LLSpellChecker::setSecondaryDictionaries(dict_list_t dict_list)
{
	if (!getUseSpellCheck())
	{
		return;
	}

	// Check if we're only adding secondary dictionaries, or removing them
	dict_list_t dict_add(llmax(dict_list.size(), mDictSecondary.size())), dict_rem(llmax(dict_list.size(), mDictSecondary.size()));
	dict_list.sort();
	mDictSecondary.sort();
	dict_list_t::iterator end_added = std::set_difference(dict_list.begin(), dict_list.end(), mDictSecondary.begin(), mDictSecondary.end(), dict_add.begin());
	dict_list_t::iterator end_removed = std::set_difference(mDictSecondary.begin(), mDictSecondary.end(), dict_list.begin(), dict_list.end(), dict_rem.begin());

	if (end_removed != dict_rem.begin())		// We can't remove secondary dictionaries so we need to recreate the Hunspell instance
	{
		mDictSecondary = dict_list;

		std::string dict_language = mDictLanguage;
		initHunspell(dict_language);
	}
	else if (end_added != dict_add.begin())		// Add the new secondary dictionaries one by one
	{
		const std::string app_path = getDictionaryAppPath();
		const std::string user_path = getDictionaryUserPath();
		for (dict_list_t::const_iterator it_added = dict_add.begin(); it_added != end_added; ++it_added)
		{
			const LLSD dict_entry = getDictionaryData(*it_added);
			if ( (!dict_entry.isDefined()) || (!dict_entry["installed"].asBoolean()) )
			{
				continue;
			}

			const std::string strFileDic = dict_entry["name"].asString() + ".dic";
			if (gDirUtilp->fileExists(user_path + strFileDic))
			{
				mHunspell->add_dic((user_path + strFileDic).c_str());
			}
			else if (gDirUtilp->fileExists(app_path + strFileDic))
			{
				mHunspell->add_dic((app_path + strFileDic).c_str());
			}
		}
		mDictSecondary = dict_list;
		sSettingsChangeSignal();
	}
}
Example #5
0
void LLSpellChecker::initHunspell(const std::string& dict_name)
{
	if (mHunspell)
	{
		delete mHunspell;
		mHunspell = NULL;
		mDictName.clear();
		mDictFile.clear();
		mIgnoreList.clear();
	}

	const LLSD dict_entry = (!dict_name.empty()) ? getDictionaryData(dict_name) : LLSD();
	if ( (!dict_entry.isDefined()) || (!dict_entry["installed"].asBoolean()) || (!dict_entry["is_primary"].asBoolean()))
	{
		sSettingsChangeSignal();
		return;
	}

	const std::string app_path = getDictionaryAppPath();
	const std::string user_path = getDictionaryUserPath();
	if (dict_entry.has("name"))
	{
		const std::string filename_aff = dict_entry["name"].asString() + ".aff";
		const std::string filename_dic = dict_entry["name"].asString() + ".dic";
		if ( (gDirUtilp->fileExists(user_path + filename_aff)) && (gDirUtilp->fileExists(user_path + filename_dic)) )
			mHunspell = new Hunspell((user_path + filename_aff).c_str(), (user_path + filename_dic).c_str());
		else if ( (gDirUtilp->fileExists(app_path + filename_aff)) && (gDirUtilp->fileExists(app_path + filename_dic)) )
			mHunspell = new Hunspell((app_path + filename_aff).c_str(), (app_path + filename_dic).c_str());
		if (!mHunspell)
			return;

		mDictName = dict_name;
		mDictFile = dict_entry["name"].asString();

		if (dict_entry["has_custom"].asBoolean())
		{
			const std::string filename_dic = user_path + mDictFile + DICT_CUSTOM_SUFFIX + ".dic";
			mHunspell->add_dic(filename_dic.c_str());
		}

		if (dict_entry["has_ignore"].asBoolean())
		{
			llifstream file_in(user_path + mDictFile + DICT_IGNORE_SUFFIX + ".dic", std::ios::in);
			if (file_in.is_open())
			{
				std::string word; int idxLine = 0;
				while (getline(file_in, word))
				{
					// Skip over the first line since that's just a line count
					if (0 != idxLine)
					{
						LLStringUtil::toLower(word);
						mIgnoreList.push_back(word);
					}
					idxLine++;
				}
			}
		}

		for (dict_list_t::const_iterator it = mDictSecondary.begin(); it != mDictSecondary.end(); ++it)
		{
			const LLSD dict_entry = getDictionaryData(*it);
			if ( (!dict_entry.isDefined()) || (!dict_entry["installed"].asBoolean()) )
				continue;

			const std::string filename_dic = dict_entry["name"].asString() + ".dic";
			if (gDirUtilp->fileExists(user_path + filename_dic))
				mHunspell->add_dic((user_path + filename_dic).c_str());
			else if (gDirUtilp->fileExists(app_path + filename_dic))
				mHunspell->add_dic((app_path + filename_dic).c_str());
		}
	}

	sSettingsChangeSignal();
}