src/manager/src/ws_manager.c

Go to the documentation of this file.
00001 /*******************************************************************************
00002 This file is part of mdictionary.
00003 
00004 mdictionary is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 mdictionary is distributed in the hope that it will be useful, 
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License 
00015 along with mdictionary; if not, write to the Free Software
00016 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00017 
00018 Copyright 2006-2008 ComArch S.A.
00019 *******************************************************************************/
00029 #include <ws_manager.h>
00030 
00031 #include <ws_mng_searching_threads.h>
00032 #include <ws_mng_dictionary_utils.h>
00033 #include <ws_mng_bookmarks_utils.h>
00034 #include <ws_mng_threads_utils.h>
00035 #include <ws_mng_gconf_utils.h>
00036 #include <ws_mng_callbacks.h>
00037 
00038 
00042 WSMngSearchData* ws_manager_create() {
00043         g_debug("<--> %s", __FUNCTION__);
00044         WSMngSearchData* tmp = g_try_new(WSMngSearchData, 1);
00045 
00046         if (NULL == tmp)
00047         {
00048                 g_debug("Not enough memory !");
00049         }
00050         return tmp;
00051 }
00052 
00057 gboolean ws_mng_start_main_loop(WSMngSearchData* search_data)
00058 {
00059         g_debug("<--> %s", __FUNCTION__);
00060         search_data->loop = g_main_loop_new (NULL, FALSE);
00061         if (search_data->loop == NULL)
00062         {
00063                 g_debug("Couldn't create new g_main_loop for Manager!");
00064                 return FALSE;
00065         }
00066         g_main_loop_run (search_data->loop);
00067         return TRUE;
00068 }
00069 
00073 void ws_mng_init_dbus (WSMngSearchData *data)
00074 {
00075         g_debug("->%s", __FUNCTION__);
00076 
00077         /* initialize GThread support if it is not initialized yet */
00078         if (!g_thread_supported ()) g_thread_init (NULL);
00079 
00080         /* create data structure needed to comunicate with dbus wrapper */
00081         data->dbus_data = ws_dbus_create ("mdictionaryManager", "v1.0");
00082 
00083         /* set data used to comunicate with gui */
00084         ws_dbus_config(data->dbus_data,
00085                        WS_DBUS_CONFIG_SERVICE,
00086                        MANAGER_SERVICE);
00087         ws_dbus_config(data->dbus_data,
00088                         WS_DBUS_CONFIG_OBJECT,
00089                         MANAGER_OBJECT);
00090         ws_dbus_config(data->dbus_data,
00091                         WS_DBUS_CONFIG_IFACE,
00092                         MANAGER_IFACE);
00093         ws_dbus_config(data->dbus_data, 
00094                         WS_DBUS_CONFIG_REMOTE_SERVICE,
00095                         GUI_SERVICE);
00096         ws_dbus_config(data->dbus_data, 
00097                         WS_DBUS_CONFIG_REMOTE_OBJECT,
00098                         GUI_OBJECT);
00099         ws_dbus_config(data->dbus_data, 
00100                         WS_DBUS_CONFIG_REMOTE_IFACE,
00101                         GUI_IFACE);
00102 
00103         /* defines all methods which could be called through D-Bus */
00104         ws_dbus_add_method ( data->dbus_data,
00105                              "find_word",
00106                              WS_DBUS_TYPE_STRING,
00107                              WS_DBUS_TYPE_INVALID );
00108         ws_dbus_add_method ( data->dbus_data,
00109                              "find_translation",
00110                              WS_DBUS_TYPE_STRING,
00111                              WS_DBUS_TYPE_INVALID );
00112         ws_dbus_add_method ( data->dbus_data,
00113                              "signal",
00114                              WS_DBUS_TYPE_SIGNAL,
00115                              WS_DBUS_TYPE_INVALID );
00116         ws_dbus_add_method ( data->dbus_data,
00117                              "add_bookmark",
00118                              WS_DBUS_TYPE_STRING,
00119                              WS_DBUS_TYPE_STRING,
00120                              WS_DBUS_TYPE_INVALID );
00121         ws_dbus_add_method ( data->dbus_data,
00122                              "remove_bookmark",
00123                              WS_DBUS_TYPE_STRING,
00124                              WS_DBUS_TYPE_INVALID );
00125         ws_dbus_add_method ( data->dbus_data,
00126                              "extract_dictionary",
00127                              WS_DBUS_TYPE_STRING,
00128                              WS_DBUS_TYPE_INVALID );
00129 
00130         /* set callback for find word signal */
00131         ws_dbus_set_cb( data->dbus_data,
00132                         "find_word",
00133                         ws_mng_on_search_word,
00134                         data );
00135 
00136         /* set callback for find translation signal */
00137          ws_dbus_set_cb( data->dbus_data,
00138                          "find_translation",
00139                          ws_mng_on_search_translation,
00140                          data );
00141 
00142         /* set callback for close program signal */
00143         ws_dbus_set_cb( data->dbus_data,
00144                         "signal",
00145                         ws_mng_signal_handling,
00146                         data );
00147 
00148          /* set callback for add bookmarks signal */
00149         ws_dbus_set_cb( data->dbus_data,
00150                         "add_bookmark",
00151                         ws_mng_add_bookmark,
00152                         data );
00153 
00154         /* set callback for remove bookmarks signal */
00155         ws_dbus_set_cb( data->dbus_data,
00156                         "remove_bookmark",
00157                         ws_mng_remove_bookmark,
00158                         data );
00159 
00160         /* set callback for extracting dictionary */
00161         ws_dbus_set_cb( data->dbus_data,
00162                         "extract_dictionary",
00163                         ws_mng_extract_dictionary,
00164                         data );
00165 
00166         /* initialize d-bus connection with remote service - UI */
00167         ws_dbus_connect(data->dbus_data);
00168 
00169         g_debug("<-%s", __FUNCTION__);
00170 }
00171 
00172 
00181 void ws_mng_init (WSMngSearchData *data)
00182 {
00183         g_debug("->%s", __FUNCTION__);
00184 
00185         /* create and initialize all data in WSMngSearchData structure */
00186         data->dict = g_array_new(TRUE, TRUE, sizeof(Engine *));
00187         data->modules = g_array_new(TRUE, TRUE, sizeof(EngineModule));
00188         data->libraries = g_array_new(TRUE, TRUE, sizeof(GModule*));
00189         data->word_list = NULL;
00190         data->last_search = NULL;
00191         data->trans = NULL;
00192         data->search_in_history = FALSE;
00193         data->word = NULL;
00194         data->bookmark = NULL;
00195         data->bookmark_mode = FALSE;
00196         /* added by Dariusz Wiechecki
00197          * mutex initialization */
00198         data->action_working =
00199                        (GStaticMutex*)g_try_malloc(sizeof(GStaticMutex));
00200         data->thread_creation =
00201                        (GStaticMutex*)g_try_malloc(sizeof(GStaticMutex));
00202         data->action_stop =
00203                        (GStaticRecMutex*)g_try_malloc(sizeof(GStaticRecMutex));
00204         g_assert(NULL != (data->action_working) );
00205         g_assert(NULL != (data->action_stop) );
00206         g_static_mutex_init (data->action_working);
00207         g_static_mutex_init (data->thread_creation);
00208         g_static_rec_mutex_init (data->action_stop);
00209         /* initialize static stop_if_needed function mutex*/
00210         stop_if_needed (NULL);
00211 
00212         /* send signal if there is not proper sqlite library */
00213         #ifdef SQLITE
00214         if( FALSE == g_file_test(LIBRARY, G_FILE_TEST_EXISTS) )
00215         {
00216                 ws_dbus_notify(data->dbus_data, WS_DBUS_LOAD_BOOKMARK_FAILED);
00217         }
00218         #endif
00219 
00220         /* load GConf configuration */
00221         GArray* dict_directory = ws_mng_read_gconf();
00222         data->library_path = ws_mng_get_engines_location();
00223 
00224         gint i = 0;
00225         for (; i < data->library_path->len; ++i)
00226         {
00227                 gchar* path= g_array_index(data->library_path, gchar*, i);
00228                 GModule* library = g_module_open(path, G_MODULE_BIND_LAZY);
00229                 g_array_append_val(data->libraries, library);
00230                 g_debug("%p library pinter %d iter", library, i);
00231         }
00232 
00233         getting_additional_t get_functions = NULL;
00234         for (i=0; i<data->libraries->len; i++)
00235         {
00236                 g_debug("%p", g_array_index(data->libraries, GModule*, i));
00237                 g_module_symbol( g_array_index(data->libraries, GModule*, i),
00238                                  GLOBAL_FUNCTIONS_NAME,
00239                                  (gpointer)&get_functions );
00240                 g_debug("%d     %p", i, &get_functions);
00241 
00242                 /* if function was not properly imported close manager */
00243                 if (NULL == get_functions)
00244                 {
00245                         ws_dbus_notify( data->dbus_data,
00246                                         WS_DBUS_ERROR_ENGINE_NOT_FOUND );
00247                         for (i=0; i<dict_directory->len; i++)
00248                         {
00249                                 g_free(g_array_index( dict_directory,
00250                                                       gchar*,
00251                                                       i ));
00252                         }
00253                         g_array_free(dict_directory, TRUE);
00254                         ws_mng_close(data);
00255                         exit(0);
00256                 }
00257 
00258                 /* get EngineModule struct and append it to the array*/
00259                 EngineModule module =  get_functions();
00260                 g_array_append_val(data->modules, module);
00261         }
00262 
00263         /* load each dictionary */
00264         ws_mng_load_bookmark(data);
00265         if (dict_directory->len > 0)
00266         {
00267                 ws_mng_load_dict(dict_directory, data);
00268                 guint i = 0;
00269                 g_debug("Trace bookmark engine %p", data->bookmark);
00270                 for (i=0; i<data->dict->len; i++)
00271                 {
00272                         g_debug( "dict engines at %p",
00273                                  g_array_index(data->dict, Engine*, i) );
00274                 }
00275         }
00276         else
00277         {
00278                 ws_dbus_notify( data->dbus_data,
00279                                 WS_DBUS_ERROR_FILE_NOT_FOUND );
00280         }
00281 
00282         /* free memory used by dictionaries path array */
00283         for (i=0; i<dict_directory->len; i++)
00284         {
00285                 g_free(g_array_index(dict_directory, gchar*, i));
00286         }
00287         g_array_free(dict_directory, TRUE);
00288 
00289         g_debug("<-%s", __FUNCTION__);
00290 }
00291 
00296 void ws_mng_close (WSMngSearchData *data)
00297 {
00298         int i = 0;
00299         g_debug("->%s", __FUNCTION__);
00300 
00301         ws_dbus_destroy (data->dbus_data);
00302         if (data->bookmark != NULL)
00303         {
00304                 dict_eng_destroy(data->bookmark);
00305         }
00306 
00307         for (i = 0; i < data->dict->len; i++) 
00308         {
00309                 dict_eng_destroy(g_array_index (data->dict, Engine*,i));
00310         }
00311         g_array_free(data->dict, TRUE);
00312 
00313         g_free(data->last_search);
00314         g_free(data->loop);
00315         g_static_mutex_free(data->thread_creation);
00316         g_static_mutex_free(data->action_working);
00317         g_static_rec_mutex_free(data->action_stop);
00318 
00319         for (i=0; i<data->library_path->len; i++)
00320         {
00321                 g_free(g_array_index(data->library_path, gchar*, i));
00322         }
00323         g_array_free(data->library_path, TRUE); 
00324         g_array_free(data->modules, TRUE); data->modules = NULL;
00325         
00326         for (i=0; i< data->libraries->len; i++)
00327         {
00328                 if (g_array_index(data->libraries, GModule*, i) != NULL)
00329                 {
00330                         g_module_close(g_array_index( data->libraries,
00331                                                       GModule*,
00332                                                       i ));
00333                 }
00334         }
00335         g_array_free(data->libraries, TRUE);
00336         g_free(data);
00337         g_debug("<-%s", __FUNCTION__);
00338 }
00339 

Generated on Fri Jan 11 14:30:17 2008 for mDictionary Project by  doxygen 1.5.1