5b34aee3c3ac0f35d03a4f56ab3e10ca8a3de808
[mdictionary] / include / dictionary_engine.h
1 /*******************************************************************************
2 This file is part of mdictionary.
3
4 mdictionary is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 mdictionary is distributed in the hope that it will be useful, 
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License 
15 along with mdictionary; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18 Copyright 2006 ComArch S.A.
19 *******************************************************************************/
20 #ifndef   _DICTIONARY_ENGINE_BASE
21 #define   _DICTIONARY_ENGINE_BASE
22
23 #ifdef    __cplusplus
24        extern "C" {
25 #endif
26
27 #ifndef NOLOGS
28         #include <glib/gstdio.h>
29         #define eg_debug g_debug
30         #define eg_warning g_warning
31         #define eg_error g_error
32         #define eg_critical g_critical
33 #else
34         #define eg_debug(frm,...) while(FALSE)
35         #define eg_warning(frm,...) while(FALSE)
36         #define eg_error(frm,...) while(FALSE)
37         #define eg_critical(frm,...) while(FALSE)
38 #endif
39
40 #define eg_malloc(n)    g_try_malloc0(n)
41 #define eg_free(n)      g_free(n); n = NULL
42
43 //______________________________________________________________________________
44 // *****************************************************************************
45 //************************************************************* HEADERS SECTION:
46 //------------------------------------------------------------------------------
47 // header with gnu library for C - most of types and functions needed it (all
48 // started from g* and g_*().
49 #include <glib.h>
50 //------------------------------------------------------------------------------
51
52 #define _GLOBAL_FUNCTIONS_NAME_  "engine_global_functions"
53 #define _GLOBAL_FUNCTIONS_        engine_global_functions
54
55 //______________________________________________________________________________
56 // *****************************************************************************
57 //******************************************************* GLOBAL MACROS SECTION:
58 //------------------------------------------------------------------------------
59 #define         dict_eng_module_check(module,location)   \
60                         ( (module).engine_check( (location) ) )
61 //------------------------------------------------------------------------------
62 #define         dict_eng_module_get_description(module)   \
63                         ( (module).engine_description() )
64 //------------------------------------------------------------------------------
65 #define         dict_eng_module_get_version(module)   \
66                         ( (module).engine_version() )
67 //------------------------------------------------------------------------------
68 #define         dict_eng_module_get_format(module)   \
69                         ( (module).engine_version() )
70 //------------------------------------------------------------------------------
71 #define         dict_eng_module_create(module,location,flags)   \
72         ( (module).engine_create(  (location), (flags), NULL, NULL, 0.01 ) )
73 //------------------------------------------------------------------------------
74 #define         dict_eng_module_create_ext(module,                      \
75                                            location,                    \
76                                            flags,                       \
77                                            progress_handler,            \
78                                            progress_data,               \
79                                            seed                         \
80                                            )  (                         \
81                        (module).engine_create( (location),              \
82                                                (flags),                 \
83                                                (progress_handler),      \
84                                                (progress_data),         \
85                                                (seed)                   \
86                                              ))
87 //------------------------------------------------------------------------------
88
89
90 //______________________________________________________________________________
91 // *****************************************************************************
92 //****************************************** CONCRETE DICTIONARY MACROS SECTION:
93 //------------------------------------------------------------------------------
94 #define         dict_eng_set_progress_seed(engine, signal, val) (              \
95                         (engine) -> engine_set_progress_seed( (engine),        \
96                                                               (signal),        \
97                                                               (val)            \
98                                                             ))
99 //------------------------------------------------------------------------------
100 #define         dict_eng_set_auto_free(engine, state)  \
101                         (engine) -> engine_set_auto_free( (engine), (state) )
102 //------------------------------------------------------------------------------
103 #define         dict_eng_optimize(engine)  \
104                         ((engine) -> engine_optimize( (engine) ))
105 //------------------------------------------------------------------------------
106 #define         dict_eng_is_optimized( engine )  \
107                         ((engine) -> engine_is_optimized( (engine) ))
108 //------------------------------------------------------------------------------
109 #define         dict_eng_get_location(engine)    \
110                         ((engine) -> engine_location( (engine) ))
111 //------------------------------------------------------------------------------
112 #define         dict_eng_get_last_status(engine)    \
113                         ((engine) -> engine_error( (engine) ))
114 //------------------------------------------------------------------------------
115 #define         dict_eng_status_message(engine, error)     \
116                         ((engine) -> engine_error_message( (error) ))
117 //------------------------------------------------------------------------------
118
119
120 //------------------------------------------------------------------------------
121 #define         dict_eng_destroy(engine)    \
122                         ((engine) -> engine_close( (engine) ))
123 //------------------------------------------------------------------------------
124 #define         dict_eng_search_word_list(engine,pattern,data)    \
125                         ((engine) -> engine_search_word_list( (engine),        \
126                                                               (pattern),       \
127                                                               (data)           \
128                                                             ))
129 //------------------------------------------------------------------------------
130 #define         dict_eng_search_word_translation(engine,word,data)    \
131                         ((engine) -> engine_search_word_translation( (engine), \
132                                                                      (word),   \
133                                                                      (data)    \
134                                                                    ))
135 //------------------------------------------------------------------------------
136 #define         dict_eng_search_word_translation_extended(engine,word)   \
137                 ((engine) -> engine_search_word_translation_extended((engine), \
138                                                                      (pattern) \
139                                                                      ))
140 //------------------------------------------------------------------------------
141 #define         dict_eng_set_callback(engine,signal,c_handler,data)     \
142                         ((engine) -> engine_set_callback( (engine),     \
143                                                           (signal),     \
144                                                           (c_handler),  \
145                                                           (data)        \
146                                                         ))
147 //------------------------------------------------------------------------------
148 #define         dict_eng_add_word(engine,word,tran)    \
149                         ((engine) -> engine_add_word( (engine), \
150                                                        (word),   \
151                                                        (tran) \
152                                                      ))
153 //------------------------------------------------------------------------------
154 #define         dict_eng_remove_word(engine,word)    \
155                         ((engine) -> engine_remove_word( (engine), \
156                                                           (word)   \
157                                                         ))
158 //------------------------------------------------------------------------------
159 #define         dict_eng_get_lang_from(engine)    \
160                         ((engine) -> engine_get_lang_from( (engine) ))
161 //------------------------------------------------------------------------------
162 #define         dict_eng_get_lang_to(engine)    \
163                         ((engine) -> engine_get_lang_to( (engine) ))
164 //------------------------------------------------------------------------------
165 #define         dict_eng_get_title(engine)    \
166                         ((engine) -> engine_get_title( (engine) ))
167 //------------------------------------------------------------------------------
168 #define         dict_eng_get_icon_path(engine)    \
169                         ((engine) -> engine_get_icon_path( (engine) ))
170 //------------------------------------------------------------------------------
171
172
173
174 //______________________________________________________________________________
175 // *****************************************************************************
176 //************************************ SIGNALS DEFINITION FOR CALLBACKS SECTION:
177 //------------------------------------------------------------------------------
178 #define ENGINE_WORD_LIST_SIGNAL                 "on_word_list_found"
179 //------------------------------------------------------------------------------
180 #define ENGINE_WORD_TRANSLATION_SIGNAL          "on_word_translation_found"
181 //------------------------------------------------------------------------------
182 #define ENGINE_PROGRESS_OPTIMIZING_SIGNAL       "on_progress_optimizing"
183 //------------------------------------------------------------------------------
184
185 //______________________________________________________________________________
186 // *****************************************************************************
187 //************************************************** DEFINITIONS OF ENUMS TYPES:
188 //------------------------------------------------------------------------------
189 typedef enum
190 {
191         ENGINE_CREATE = 0, 
192         ENGINE_NO,
193         ENGINE_REFRESH 
194 } EngineOptimizationFlag;
195 //------------------------------------------------------------------------------
196 typedef enum
197 {
198         ENGINE_NO_ERROR = 0,
199         ENGINE_WRONG_FILE,
200         ENGINE_COULDNT_READ,
201         ENGINE_COULDNT_WRITE,
202         ENGINE_INTERNAL_ERROR,
203         ENGINE_NO_FILE,     
204         ENGINE_OUT_OF_MEMORY
205 }
206 EngineStatus;
207 //------------------------------------------------------------------------------
208
209
210 //______________________________________________________________________________
211 // *****************************************************************************
212 //********************************************* DEFINITIONS OF CALLBACK'S TYPES:
213 //------------------------------------------------------------------------------
214 typedef void (*cb_word_list)(GArray* list,
215                              gchar* pattern,
216                              gpointer user_data,
217                              EngineStatus error);
218 //------------------------------------------------------------------------------
219 typedef void (*cb_word_translation)(gchar* translation,
220                                     gchar* word,
221                                     gpointer user_data,
222                                     EngineStatus error);
223 //------------------------------------------------------------------------------
224 typedef void (*cb_progress)(gdouble value,
225                             gpointer user_data,
226                             EngineStatus error);
227 //------------------------------------------------------------------------------
228
229 //------------------------------------------------------------------------------
230 struct _Engine;
231 typedef struct _Engine Engine;
232
233 struct _Engine
234 {
235         // 0.1 API specification
236         void            (*engine_set_auto_free)(Engine* engine,
237                                                 gboolean state);
238         gchar*          (*engine_location)(Engine* engine);       
239         gboolean        (*engine_is_optimized)(Engine* engine);
240         void            (*engine_optimize)(Engine* engine);
241         void            (*engine_search_word_list)(Engine* engine,
242                                                    gchar* pattern,
243                                                    gpointer data);
244         void            (*engine_search_word_translation)(Engine* engine,
245                                                           gchar* word,
246                                                           gpointer data);
247         void            (*engine_close)(Engine* engine);
248         // engine status
249         EngineStatus    (*engine_status)(Engine* engine);
250         gchar*          (*engine_error_message)(EngineStatus error);
251         // callbacks
252         gpointer        (*engine_set_callback)(Engine* engine, 
253                                                gchar* signal, 
254                                                gpointer c_handler, 
255                                                gpointer user_data);
256         void            (*engine_set_progress_seed)(Engine* engine,
257                                                     gchar* signal,
258                                                     gdouble seed);
259         // 0.2 API specification's functions
260         gboolean        (*engine_add_word)(Engine* engine,
261                                            gchar*  word,
262                                            gchar*  translation);
263         gboolean        (*engine_remove_word)(Engine* engine,
264                                               gchar*  word);        
265         gchar*          (*engine_get_lang_from)(Engine* engine);
266         gchar*          (*engine_get_lang_to)(Engine* engine);
267         gchar*          (*engine_get_title)(Engine* engine);
268         gchar*          (*engine_get_icon_path)(Engine* engine);
269
270         // dictionary independent data
271         gpointer engine_data;
272 };
273 //------------------------------------------------------------------------------
274 typedef struct  {
275         gboolean (*engine_check)(gchar* location); 
276         gchar*   (*engine_description)(void);     
277         gchar*   (*engine_format)(void);
278         gchar*   (*engine_version)(void);
279         Engine*  (*engine_create)(gchar* location,
280                                    EngineOptimizationFlag flags,
281                                    cb_progress progress_handler,
282                                    gpointer progress_data,
283                                    gdouble seed);
284
285 EngineModule;
286 //------------------------------------------------------------------------------
287 typedef EngineModule (*getting_additional)(void);
288 //------------------------------------------------------------------------------
289 extern EngineModule engine_global_functions(void);
290 //------------------------------------------------------------------------------
291
292           
293 #ifdef __cplusplus
294 }                     
295 #endif
296 #endif /* _DICTIONARY_ENGINE_BASE */