1 /*******************************************************************************
2 This file is part of WhiteStork.
4 WhiteStork 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.
9 WhiteStork 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.
14 You should have received a copy of the GNU General Public License
15 along with WhiteStork; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 Copyright 2006 ComArch S.A.
19 *******************************************************************************/
20 #include <ws_manager.h>
22 #include <ws_mng_searching_threads.h>
23 #include <ws_mng_dictionary_utils.h>
24 #include <ws_mng_bookmarks_utils.h>
25 #include <ws_mng_threads_utils.h>
26 #include <ws_mng_gconf_utils.h>
27 #include <ws_mng_callbacks.h>
31 * Public function, see ws_manager.h
33 WSMngSearchData* ws_manager_create() {
34 g_debug("<--> %s", __FUNCTION__);
35 WSMngSearchData* tmp = g_try_new(WSMngSearchData, 1);
39 g_debug("Not enough memory !");
45 * Public function, see ws_manager.h
47 gboolean ws_mng_start_main_loop(WSMngSearchData* search_data)
49 g_debug("<--> %s", __FUNCTION__);
50 search_data->loop = g_main_loop_new (NULL, FALSE);
51 if (search_data->loop == NULL)
53 g_debug("Couldn't create new g_main_loop for Manager!");
56 g_main_loop_run (search_data->loop);
64 *Public function, see ws_manager.h
66 void ws_mng_init_dbus (WSMngSearchData *data)
68 g_debug("->%s", __FUNCTION__);
70 /* create data structure needed to comunicate with dbus wrapper */
71 data->dbus_data = ws_dbus_create ("WhiteStorkManager", "v1.0");
73 /* set data used to comunicate with gui */
74 ws_dbus_config(data->dbus_data,
75 WS_DBUS_CONFIG_SERVICE,
76 "org.maemo.WhiteStorkManager");
77 ws_dbus_config(data->dbus_data,
78 WS_DBUS_CONFIG_OBJECT,
79 "/org/maemo/WhiteStorkManager");
80 ws_dbus_config(data->dbus_data,
82 "org.maemo.WhiteStorkManager");
83 ws_dbus_config(data->dbus_data,
84 WS_DBUS_CONFIG_REMOTE_SERVICE,
85 "org.maemo.WhiteStorkGui");
86 ws_dbus_config(data->dbus_data,
87 WS_DBUS_CONFIG_REMOTE_OBJECT,
88 "/org/maemo/WhiteStorkGui");
89 ws_dbus_config(data->dbus_data,
90 WS_DBUS_CONFIG_REMOTE_IFACE,
91 "org.maemo.WhiteStorkGui");
94 ws_dbus_add_method (data->dbus_data, "find_word", WS_DBUS_TYPE_STRING,
95 WS_DBUS_TYPE_INVALID);
97 ws_dbus_add_method (data->dbus_data, "find_translation",
98 WS_DBUS_TYPE_STRING, WS_DBUS_TYPE_INVALID);
100 ws_dbus_add_method (data->dbus_data, "signal", WS_DBUS_TYPE_SIGNAL,
101 WS_DBUS_TYPE_INVALID);
103 ws_dbus_add_method (data->dbus_data, "add_bookmark",
104 WS_DBUS_TYPE_STRING, WS_DBUS_TYPE_STRING,
105 WS_DBUS_TYPE_INVALID);
106 ws_dbus_add_method (data->dbus_data, "remove_bookmark",
107 WS_DBUS_TYPE_STRING, WS_DBUS_TYPE_INVALID);
109 ws_dbus_add_method (data->dbus_data, "extract_dictionary",
110 WS_DBUS_TYPE_STRING, WS_DBUS_TYPE_INVALID);
112 /* set callback for find word signal */
113 ws_dbus_set_cb(data->dbus_data,
115 ws_mng_on_search_word,
118 /* set callback for find translation signal */
119 ws_dbus_set_cb(data->dbus_data,
121 ws_mng_on_search_translation,
124 /* set callback for close program signal */
125 ws_dbus_set_cb(data->dbus_data,
127 ws_mng_signal_handling,
130 /* set callback for add bookmarks signal */
131 ws_dbus_set_cb(data->dbus_data,
136 /* set callback for remove bookmarks signal */
137 ws_dbus_set_cb(data->dbus_data,
139 ws_mng_remove_bookmark,
142 /* set callback for extracting dictionary */
143 ws_dbus_set_cb(data->dbus_data,
144 "extract_dictionary",
145 ws_mng_extract_dictionary,
148 ws_dbus_connect(data->dbus_data);
151 g_debug("<-%s", __FUNCTION__);
156 *Public fuction, see ws_manager.h
158 void ws_mng_init (WSMngSearchData *data)
160 g_debug("->%s", __FUNCTION__);
161 data->dict = g_array_new(TRUE, TRUE, sizeof(Engine *));
162 data->modules = g_array_new(TRUE, TRUE, sizeof(EngineModule));
163 data->libraries = g_array_new(TRUE, TRUE, sizeof(GModule*));
164 data->word_list = NULL;
165 data->last_search = NULL;
167 data->search_in_history = FALSE;
169 data->bookmark = NULL;
170 data->bookmark_mode = FALSE;
171 /* added by Dariusz Wiechecki
172 * mutex initialization */
173 data->action_working = (GStaticMutex*)g_try_malloc(sizeof(GStaticMutex));
174 data->thread_creation = (GStaticMutex*)g_try_malloc(sizeof(GStaticMutex));
175 data->action_stop = (GStaticRecMutex*)g_try_malloc(sizeof(GStaticRecMutex));
176 g_assert(NULL != (data->action_working) );
177 g_assert(NULL != (data->action_stop) );
178 g_static_mutex_init (data->action_working);
179 g_static_mutex_init (data->thread_creation);
180 g_static_rec_mutex_init (data->action_stop);
181 /* initialize static stop_if_needed function mutex*/
182 stop_if_needed (NULL);
183 /* initialize GThread structure */
184 //data->action_thread = NULL;
187 if (g_file_test(LIBRARY,
188 G_FILE_TEST_EXISTS) == FALSE)
190 ws_dbus_notify(data->dbus_data, WS_DBUS_LOAD_BOOKMARK_FAILED);
194 GArray* dict_directory = ws_mng_read_gconf(); //paths to dictionaries
197 data->library_path = ws_mng_get_engines_location();
198 //load the engine function
199 for (i = 0; i < data->library_path->len; i++)
201 gchar* path= g_array_index(data->library_path, gchar*, i);
202 GModule* library = g_module_open(path, G_MODULE_BIND_LAZY);
203 g_array_append_val(data->libraries, library);
204 g_debug("%p library pinter %d iter", library, i);
207 //data->library = g_module_open(library_to_path, G_MODULE_BIND_LAZY);
209 for (i=0; i<data->libraries->len; i++)
211 getting_additional get_functions = NULL;
212 //get_functions = NULL;
214 g_debug("%p", g_array_index(data->libraries, GModule*, i));
215 g_module_symbol(g_array_index(data->libraries, GModule*, i),
216 "engine_global_functions",
217 (gpointer)&get_functions);
218 g_debug("%d %p", i, &get_functions);
219 if (get_functions == NULL) //check if function was loaded
221 ws_dbus_notify(data->dbus_data,
222 WS_DBUS_ERROR_ENGINE_NOT_FOUND);
223 for (i=0; i<dict_directory->len; i++)
225 g_free(g_array_index(dict_directory, gchar*, i));
227 g_array_free(dict_directory, TRUE);
229 exit(0); //exit program
231 EngineModule module = get_functions();
232 //tmp = (EngineModule*) g_memdup(&module, sizeof(EngineModule));
233 //module = (EngineModule**) &tmp;
234 g_array_append_val(data->modules, module);
238 ws_mng_load_bookmark(data);
239 if (dict_directory->len > 0)
241 ws_dbus_notify(data->dbus_data, WS_DBUS_INFO_CACHING);
243 ws_mng_load_dict(dict_directory, data);
245 g_debug("Trace bookmark engine %p", data->bookmark);
246 for (i=0; i<data->dict->len; i++)
248 g_debug("dict engines at %p",
249 g_array_index(data->dict, Engine*, i));
251 ws_dbus_notify(data->dbus_data,
252 WS_DBUS_INFO_CACHING_FINISHED);
256 ws_dbus_notify(data->dbus_data,
257 WS_DBUS_ERROR_FILE_NOT_FOUND);
259 for (i=0; i<dict_directory->len; i++)
261 g_free(g_array_index(dict_directory, gchar*, i));
263 g_array_free(dict_directory, TRUE);
264 g_debug("<-%s", __FUNCTION__);
268 *Public function, see ws_manager.h
270 void ws_mng_close (WSMngSearchData *data)
273 g_debug("->%s", __FUNCTION__);
275 ws_dbus_destroy (data->dbus_data); // deinitialization of dbus
276 if (data->bookmark != NULL)
278 dict_eng_destroy(data->bookmark);
281 for (i = 0; i < data->dict->len; i++)
283 //free memory taken by engine
284 dict_eng_destroy(g_array_index (data->dict, Engine*,i));
286 g_array_free(data->dict, TRUE);
288 /*for (i=0; i<data->word_list->len; i++)
290 g_free(g_array_index(data->word_list, gchar*, i));
293 g_array_free(data->word_list, TRUE);*/
295 //free memory used by modules
296 //g_array_free(data->modules, TRUE);
297 //free memory taken by dictionaries
298 g_free(data->last_search);
302 g_static_mutex_free(data->thread_creation);
303 g_static_mutex_free(data->action_working);
304 g_static_rec_mutex_free(data->action_stop);
305 /*if (data->word != NULL) g_free(data->word);*/
307 //if (data->trans != NULL) g_free(data->trans);
309 for (i=0; i<data->library_path->len; i++)
311 g_free(g_array_index(data->library_path, gchar*, i));
313 g_array_free(data->library_path, TRUE);
315 g_array_free(data->modules, TRUE); data->modules = NULL;
317 for (i=0; i< data->libraries->len; i++)
319 if (g_array_index(data->libraries, GModule*, i) != NULL)
322 g_module_close(g_array_index(data->libraries,
328 g_array_free(data->libraries, TRUE);
332 g_debug("<-%s", __FUNCTION__);