Flexible dbus-wrapper interface
[mdictionary] / src / manager / src / ws_manager.c
1 /*******************************************************************************
2 This file is part of WhiteStork.
3
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.
8
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.
13
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
17
18 Copyright 2006 ComArch S.A.
19 *******************************************************************************/
20 #include <ws_manager.h>
21
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>
28
29
30 /**
31  * Public function, see ws_manager.h
32  */
33 WSMngSearchData* ws_manager_create() {
34         g_debug("<--> %s", __FUNCTION__);
35         WSMngSearchData* tmp = g_try_new(WSMngSearchData, 1);
36
37         if (NULL == tmp)
38         {
39                 g_debug("Not enough memory !");
40         }
41         return tmp;
42 }
43
44 /**
45  * Public function, see ws_manager.h
46  */
47 gboolean ws_mng_start_main_loop(WSMngSearchData* search_data)
48 {
49         g_debug("<--> %s", __FUNCTION__);
50         search_data->loop = g_main_loop_new (NULL, FALSE);
51         if (search_data->loop == NULL)
52         {
53                 g_debug("Couldn't create new g_main_loop for Manager!");
54                 return FALSE;
55         }
56         g_main_loop_run (search_data->loop);
57         return TRUE;
58 }
59
60
61
62
63 /**
64  *Public function, see ws_manager.h
65  */
66 void ws_mng_init_dbus (WSMngSearchData *data)
67 {
68         g_debug("->%s", __FUNCTION__);
69
70         /* create data structure needed to comunicate with dbus wrapper */
71         data->dbus_data = ws_dbus_create ("WhiteStorkManager", "v1.0"); 
72
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,
81                         WS_DBUS_CONFIG_IFACE,
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");
92
93
94         ws_dbus_add_method (data->dbus_data, "find_word", WS_DBUS_TYPE_STRING, 
95                             WS_DBUS_TYPE_INVALID);
96         
97         ws_dbus_add_method (data->dbus_data, "find_translation", 
98                             WS_DBUS_TYPE_STRING, WS_DBUS_TYPE_INVALID);
99                         
100         ws_dbus_add_method (data->dbus_data, "signal", WS_DBUS_TYPE_SIGNAL, 
101                             WS_DBUS_TYPE_INVALID);
102                         
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);
108
109         ws_dbus_add_method (data->dbus_data, "extract_dictionary", 
110                             WS_DBUS_TYPE_STRING, WS_DBUS_TYPE_INVALID);
111                         
112         /* set callback for find word signal */
113         ws_dbus_set_cb(data->dbus_data, 
114                        "find_word", 
115                        ws_mng_on_search_word,
116                        data);
117
118         /* set callback for find translation signal */
119          ws_dbus_set_cb(data->dbus_data,
120                         "find_translation",
121                         ws_mng_on_search_translation,
122                         data);
123
124         /* set callback for close program signal */
125         ws_dbus_set_cb(data->dbus_data,
126                        "signal",
127                        ws_mng_signal_handling,
128                        data);
129
130          /* set callback for add bookmarks signal */
131         ws_dbus_set_cb(data->dbus_data,
132                        "add_bookmark",
133                        ws_mng_add_bookmark,
134                        data);
135
136         /* set callback for remove bookmarks signal */
137         ws_dbus_set_cb(data->dbus_data,
138                        "remove_bookmark",
139                        ws_mng_remove_bookmark,
140                        data);
141
142         /* set callback for extracting dictionary */
143         ws_dbus_set_cb(data->dbus_data,
144                        "extract_dictionary",
145                        ws_mng_extract_dictionary,
146                        data);
147
148         ws_dbus_connect(data->dbus_data);
149
150
151         g_debug("<-%s", __FUNCTION__);
152 }
153
154
155 /**
156  *Public fuction, see ws_manager.h
157  */
158 void ws_mng_init (WSMngSearchData *data)
159 {
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;
166         data->trans = NULL;
167         data->search_in_history = FALSE;
168         data->word = NULL;
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;
185
186         #ifdef SQLITE
187         if (g_file_test(LIBRARY, 
188                         G_FILE_TEST_EXISTS) == FALSE)
189         {
190                 ws_dbus_notify(data->dbus_data, WS_DBUS_LOAD_BOOKMARK_FAILED);
191         }
192         #endif
193         
194         GArray* dict_directory = ws_mng_read_gconf(); //paths to dictionaries
195         gint i = 0;
196
197         data->library_path = ws_mng_get_engines_location();
198         //load the engine function
199         for (i = 0; i < data->library_path->len; i++)
200         {
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);
205                 //g_free(path);
206         }
207         //data->library = g_module_open(library_to_path, G_MODULE_BIND_LAZY); 
208         
209         for (i=0; i<data->libraries->len; i++)
210         {
211                 getting_additional get_functions = NULL;
212                 //get_functions = NULL;
213                 //data->library
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
220                 {
221                         ws_dbus_notify(data->dbus_data,
222                                        WS_DBUS_ERROR_ENGINE_NOT_FOUND);
223                         for (i=0; i<dict_directory->len; i++)
224                         {
225                                 g_free(g_array_index(dict_directory, gchar*, i));
226                         }
227                         g_array_free(dict_directory, TRUE);
228                         ws_mng_close(data);
229                         exit(0); //exit program
230                 }
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);
235                 
236         }
237         
238         ws_mng_load_bookmark(data);
239         if (dict_directory->len > 0)
240         {
241                 ws_dbus_notify(data->dbus_data, WS_DBUS_INFO_CACHING); 
242                 
243                 ws_mng_load_dict(dict_directory, data);
244                 guint i = 0;
245                 g_debug("Trace bookmark engine %p", data->bookmark);
246                 for (i=0; i<data->dict->len; i++)
247                 {
248                         g_debug("dict engines at %p", 
249                         g_array_index(data->dict, Engine*, i));
250                 }
251                 ws_dbus_notify(data->dbus_data, 
252                                 WS_DBUS_INFO_CACHING_FINISHED);
253         }
254         else
255         {
256                 ws_dbus_notify(data->dbus_data,
257                                 WS_DBUS_ERROR_FILE_NOT_FOUND);
258         }
259         for (i=0; i<dict_directory->len; i++)
260         {
261                 g_free(g_array_index(dict_directory, gchar*, i));
262         }
263         g_array_free(dict_directory, TRUE);
264         g_debug("<-%s", __FUNCTION__);
265 }
266
267 /**
268  *Public function, see ws_manager.h
269  */
270 void ws_mng_close (WSMngSearchData *data)
271 {
272         int i = 0;
273         g_debug("->%s", __FUNCTION__);
274
275         ws_dbus_destroy (data->dbus_data); // deinitialization of dbus
276         if (data->bookmark != NULL)
277         {
278                 dict_eng_destroy(data->bookmark);
279         }
280         
281         for (i = 0; i < data->dict->len; i++) 
282         {
283                  //free memory taken by engine
284                 dict_eng_destroy(g_array_index (data->dict, Engine*,i));
285         }
286         g_array_free(data->dict, TRUE);
287         
288         /*for (i=0; i<data->word_list->len; i++)
289         {
290                 g_free(g_array_index(data->word_list, gchar*, i));
291         }
292         
293         g_array_free(data->word_list, TRUE);*/
294         
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);
299         
300         g_free(data->loop); 
301         
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);*/
306         
307         //if (data->trans != NULL) g_free(data->trans);
308         
309         for (i=0; i<data->library_path->len; i++)
310         {
311                 g_free(g_array_index(data->library_path, gchar*, i));
312         }
313         g_array_free(data->library_path, TRUE); 
314         
315         g_array_free(data->modules, TRUE); data->modules = NULL;
316         
317         for (i=0; i< data->libraries->len; i++)
318         {
319                 if (g_array_index(data->libraries, GModule*, i) != NULL)
320                 {
321                            //close library
322                         g_module_close(g_array_index(data->libraries, 
323                                                 GModule*, i));
324                 }
325                 
326         }
327         
328         g_array_free(data->libraries, TRUE);
329         
330         g_free(data);
331         
332         g_debug("<-%s", __FUNCTION__);
333 }
334
335