1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
4
* Copyright (C) 2007 Andris Pavenis <andris.pavenis@iki.fi>
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
* ***** END OF LICENSE BLOCK ***** */
22
#include "mozVoikko.hxx"
23
#ifdef SYSTEM_LIBVOIKKO
24
#include <libvoikko/voikko.h>
26
#include "voikko-dist.h"
28
#include "mozVoikkoSpell.hxx"
29
#include "mozVoikkoUtils.hxx"
31
#include <nsStringAPI.h>
32
#include <nsServiceManagerUtils.h>
33
#include <nsICategoryManager.h>
34
#include <nsISimpleEnumerator.h>
35
#include <nsIConsoleService.h>
36
#include <nsILocalFile.h>
47
const char *encoding = "UTF-8";
49
typedef const char * (*initvoikko_with_path_t)(int*, const char *, int, const char *);
51
typedef const char * (*initvoikko_t)(int *, const char *, int);
53
typedef int (*setsopt_t)(int, int, const char *);
55
typedef int (*setbopt_t)(int, int, int);
57
typedef int (*spell_t)(int, const char *);
59
typedef char ** (*suggest_t)(int, const char *);
61
typedef int (*terminate_voikko_t)(int);
63
typedef void (*free_suggest_cstr_t)(char **);
65
// Only one user can use voikko at the time, so we have only one copy and locking
66
PRLock *voikko_lock = NULL;
68
bool libvoikko_init_tried = false;
70
bool libvoikko_initialized = false;
72
PRLibrary *voikko_lib = NULL;
78
// The initialization function of the loaded library
79
initvoikko_with_path_t init_func_;
81
// The initialization function without explicit dictionary path
82
initvoikko_t init_system_func_;
84
// The termination function of the loaded library
85
terminate_voikko_t terminate_func_;
87
// The spell checking function of the loaded library
90
// The suggestion generation function of the loaded library
91
suggest_t suggest_func_;
93
// The procedure for releasing memory allocated by voikko_suggest_cstr().
94
free_suggest_cstr_t free_suggest_cstr_func_;
96
// The option setting functions of the loaded library
97
setsopt_t string_option_func_;
98
setbopt_t boolean_option_func_;
100
// Preloaded libraries
101
PreloadedLibraries *preloadedLibraries = NULL;
107
PR_Lock(voikko_lock);
112
PR_Unlock(voikko_lock);
116
struct VoikkoPluginInit
118
VoikkoPluginInit(void)
121
voikko_lock = PR_NewLock();
127
if (libvoikko_initialized)
129
(*terminate_func_)(voikkohandle);
133
PR_UnloadLibrary (voikko_lib);
136
if (preloadedLibraries)
138
delete preloadedLibraries;
141
PR_DestroyLock(voikko_lock);
145
VoikkoPluginInit voikko_init_0_;
147
bool LibvoikkoInit(void)
150
if (libvoikko_init_tried)
151
return libvoikko_initialized;
153
libvoikko_init_tried = true;
155
#ifndef SYSTEM_LIBVOIKKO
157
nsCOMPtr<nsIFile> libDir, dataDir;
158
rv = getMozVoikkoBaseDirs(getter_AddRefs(libDir), getter_AddRefs(dataDir));
162
logMessage("Failed to get directories where Voikko libraries and data are installed");
166
nsCString libDirPath, dataDirPath;
167
libDir->GetNativePath(libDirPath);
168
dataDir->GetNativePath(dataDirPath);
170
rv = dataDir->GetNativePath(dataDirPath);
171
NS_ENSURE_SUCCESS(rv, false);
173
logMessage("Using Voikko data directory %s", dataDirPath.get());
175
preloadedLibraries = new PreloadedLibraries(libDir, preloadLibNames, numPreloadedLibs);
176
if (!preloadedLibraries)
179
if (!*preloadedLibraries)
185
voikko_lib = PR_LoadLibrary(libvoikkoName);
187
if (voikko_lib == 0) {
188
logMessage("%s is not available: %s", libvoikkoName, prGetErrorText().get());
192
bool symbols_ok = LoadSymbol(voikko_lib, &init_func_, "voikko_init_with_path")
193
&& LoadSymbol(voikko_lib, &init_system_func_, "voikko_init")
194
&& LoadSymbol(voikko_lib, &terminate_func_, "voikko_terminate")
195
&& LoadSymbol(voikko_lib, &check_func_, "voikko_spell_cstr")
196
&& LoadSymbol(voikko_lib, &suggest_func_, "voikko_suggest_cstr")
197
&& LoadSymbol(voikko_lib, &string_option_func_, "voikko_set_string_option")
198
&& LoadSymbol(voikko_lib, &boolean_option_func_, "voikko_set_bool_option")
199
&& LoadSymbol(voikko_lib, &free_suggest_cstr_func_, "voikko_free_suggest_cstr");
202
logMessage("Failed to find at least one required symbol in %s.", libvoikkoName);
203
PR_UnloadLibrary (voikko_lib);
208
#ifdef SYSTEM_LIBVOIKKO
209
const char* error = init_system_func_(&voikkohandle, "fi_FI", 0);
211
const char* error = init_func_(&voikkohandle, "fi_FI", 0, dataDirPath.get());
214
logMessage("Failed to initialize libvoikko: %s.", error);
215
PR_UnloadLibrary (voikko_lib);
220
boolean_option_func_(voikkohandle, VOIKKO_OPT_IGNORE_DOT, 1);
221
boolean_option_func_(voikkohandle, VOIKKO_OPT_IGNORE_NUMBERS, 1);
222
boolean_option_func_(voikkohandle, VOIKKO_OPT_IGNORE_UPPERCASE, 1);
223
#ifdef VOIKKO_OPT_ACCEPT_MISSING_HYPHENS
224
boolean_option_func_(voikkohandle, VOIKKO_OPT_ACCEPT_MISSING_HYPHENS, 1);
227
int status = string_option_func_(voikkohandle, VOIKKO_OPT_ENCODING, encoding);
229
logMessage("Failed to set encoding %s for libvoikko.", encoding);
233
logMessage("%s is successfully initialized.", libvoikkoName);
235
libvoikko_initialized = true;
239
int check (const char * str)
243
// Always return spellcheck failure if not initialized properly (should never happen...).
244
if (!libvoikko_initialized) return 0;
246
status = (*check_func_)(voikkohandle, str);
247
return status ? 1 : 0;
250
char **get_suggestions (const char * word)
252
char **vsuggestions = NULL;
254
if (!libvoikko_initialized) return NULL;
255
vsuggestions = (*suggest_func_) (voikkohandle, word);
260
MozVoikko::MozVoikko (void)
263
if (LibvoikkoInit()) {
270
MozVoikko::~MozVoikko()
274
int MozVoikko::suggest (char *** slst, const char * word)
278
*slst = get_suggestions(word);
281
while ((*slst)[ns] != NULL) ns++;
287
void MozVoikko::freeSuggestions(char **slst)
289
free_suggest_cstr_func_(slst);
292
int MozVoikko::spell (const char * str)
296
// Always return spellcheck failure if not initialized properly.
297
if (!is_ok) return 0;
299
return status ? 1 : 0;
302
char * MozVoikko::get_dic_encoding()
304
return (char*) encoding;