src/manager/src/ws_mng_searching_threads.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 *******************************************************************************/
00026 #include <ws_mng_searching_threads.h>
00027 #include <glib/gstdio.h>
00028 
00033 gpointer ws_mng_search_word (gpointer user_data)
00034 {
00035         g_debug("[T-word] %s: Entering thread...", __FUNCTION__);
00036         WSMngSearchAtom *search_atom = (WSMngSearchAtom*)user_data;
00037         WSMngSearchData *search = search_atom->data;
00038 
00039         /* enter into CRITICAL SECTION */
00040         if (try_lock_was_locked(search,(gchar*)__FUNCTION__))
00041         {
00042                 g_printf("[T-word] STOP mutex is locked! Aborting others!");
00043                 g_static_rec_mutex_lock(search->action_stop);
00044                 g_static_mutex_lock(search->action_working);
00045         }
00046         g_static_rec_mutex_unlock(search->action_stop);
00047 
00048         /* if another thread was run after this one - exit */
00049         stop_if_needed(search_atom);
00050 
00051         g_debug( "[T-word] %s - from now this thread is \'singleton\' ",
00052                  __FUNCTION__ );
00053         ws_dbus_notify(search->dbus_data, WS_DBUS_WORDS_LIST_STARTED);
00054         /* creating new GArray for word list */
00055         search_atom->word_list = g_array_new(TRUE, TRUE, sizeof(gchar*));
00056 
00057         g_debug("[T-word] %s - start searching... ",__FUNCTION__);
00058         if (search->bookmark_mode)
00059         {
00060                 /* search only in user bookmarks */
00061                 dict_eng_search_word_list( search->bookmark,
00062                                            search_atom->word,
00063                                            search_atom );
00064         }
00065         else
00066         {
00067                 /* search for word in each dictionary */
00068                 gint i = 0;
00069                 for (i = 0; i < search->dict->len; i++)
00070                 {
00071                         if (NULL == g_array_index(search->dict, Engine *, i))
00072                         {
00073                                 continue;
00074                         }
00075 
00076                         stop_if_needed(search_atom);
00077                         Engine* dict = g_array_index(search->dict,Engine *,i);
00078                         dict_eng_search_word_list( dict,
00079                                                    search_atom->word,
00080                                                    search_atom );
00081                 }
00082         }
00083         g_debug("[T-word] %s - searching finished.",__FUNCTION__);
00084 
00085         /* if another thread was run after this one - exit */
00086         stop_if_needed(search_atom);
00087 
00088         /* sort and cleaning words list - only if there were more than one
00089          * dictionary loaded */
00090         if ((FALSE == search->bookmark_mode) || (1 < search->dict->len))
00091         {
00092                 g_array_sort(search_atom->word_list, ws_mng_compare_strings);
00093                 ws_remove_multiple_words(search_atom);
00094         }
00095 
00096         /* if another thread was run after this one - exit */
00097         stop_if_needed(search_atom);
00098 
00099         ws_dbus_server_return_words(search->dbus_data, search_atom->word_list);
00100         ws_dbus_notify(search->dbus_data, WS_DBUS_WORDS_LIST_FINISHED);
00101 
00102         /* free memory used by each word from word list */
00103         gint i = 0;
00104         for (; i < search_atom->word_list->len; ++i)
00105         {
00106                 g_free(g_array_index(search_atom->word_list,gchar* ,i));
00107         }
00108 
00109         /* free memory used by GArray */
00110         g_array_free(search_atom->word_list, TRUE);
00111         search_atom->word_list = NULL;
00112 
00113         g_free(search_atom->word);
00114         g_free(search_atom);
00115         g_debug("[T-word] %s - leaving thread!", __FUNCTION__);
00116         g_static_mutex_unlock(search->action_working);
00117         return NULL;
00118 }
00119 
00126 void ws_mng_on_found_word(GArray* list,
00127                           gchar* pattern,
00128                           gpointer user_data,
00129                           EngineStatus error)
00130 {
00131         g_debug("[T-word-ret]-> %s", __FUNCTION__);
00132 
00133         WSMngSearchAtom* search_atom = (WSMngSearchAtom*)user_data;
00134         static gint i = 0;
00135         for (i = 0; i < list->len; i++)
00136         {
00137                 /* copy word found by search engine */
00138                 gchar* new_word = g_strdup(g_array_index(list, gchar*, i));
00139                 g_array_append_val(search_atom->word_list, new_word); 
00140                 
00141         }
00142 
00143         g_debug("[T-word-ret]<- %s", __FUNCTION__);
00144 }
00145 
00150 gpointer ws_mng_search_translation (gpointer data)
00151 {
00152         g_debug("[T-tran] %s: Entering thread...", __FUNCTION__);
00153         WSMngSearchAtom* search_atom = (WSMngSearchAtom*)data;
00154         WSMngSearchData* search = search_atom->data;
00155 
00156         /* ---> CRITICAL SECTION for this function */
00157         if (try_lock_was_locked(search,(gchar*)__FUNCTION__))
00158         {
00159                 g_printf("[T-tran] STOP mutex is locked! Aborting others!");
00160                 g_static_rec_mutex_lock(search->action_stop);
00161                 g_static_mutex_lock(search->action_working);
00162         }
00163         g_static_rec_mutex_unlock(search->action_stop);
00164         /* if another thread was run after this one - exit */
00165         stop_if_needed(search_atom);
00166 
00167         g_debug( "[T-tran] %s - from now this thread is \'singleton\' ",
00168                  __FUNCTION__ );
00169         ws_dbus_notify(search->dbus_data, WS_DBUS_TRANSLATION_STARTED);
00170 
00171         /* run search for translation for every dictionary */
00172         if (search->bookmark_mode)
00173         {
00174                 dict_eng_search_word_translation( search->bookmark,
00175                                                   search_atom->word,
00176                                                   search_atom );
00177         }
00178         else
00179         {
00180                 gint i;
00181                 for (i = 0; i < search->dict->len; i++)
00182                 {
00183                         stop_if_needed(search_atom);
00184                         if (NULL == g_array_index(search->dict, Engine*, i) )
00185                         {
00186                                 continue;
00187                         }
00188                         dict_eng_search_word_translation(
00189                                         g_array_index(search->dict, Engine*, i),
00190                                         search_atom->word,
00191                                         search_atom);
00192                 }
00193         }
00194         g_debug("[T-tran] %s - searching finished.",__FUNCTION__);
00195 
00196         /* if another thread was run after this one - exit */
00197         stop_if_needed(search_atom);
00198 
00199         /* send translation to gui */
00200         ws_dbus_server_return_translations(search->dbus_data, search_atom->trans);
00201         ws_dbus_notify(search->dbus_data, WS_DBUS_TRANSLATION_FINISHED);
00202 
00203         g_free(search->trans);
00204         search->trans = NULL;
00205 
00206         g_debug("[T-word] %s - leaving thread!", __FUNCTION__);
00207         g_static_mutex_unlock(search->action_working);
00208         return NULL;
00209 }
00210 
00217 void ws_mng_on_found_translation(gchar* translation,
00218                                  gchar* pattern,
00219                                  gpointer user_data,
00220                                  EngineStatus error
00221                                  )
00222 {
00223         g_debug("->%s", __FUNCTION__);
00224         WSMngSearchAtom* search_atom = (WSMngSearchAtom*)user_data;
00225 
00226         /* we get already the first translation */
00227         if ((NULL != translation) && (NULL == search_atom->trans))
00228         {
00229                 /* concatenate tags and searched word and translation */
00230                 search_atom->trans = g_strconcat("<PATTERN_OPEN>",
00231                                           pattern,
00232                                           "<PATTERN_CLOSED><TRANSLATION_OPEN>",
00233                                           translation,
00234                                           "<TRANSLATION_CLOSED>",
00235                                           NULL
00236                                          );
00237         }
00238         else if (NULL != translation)
00239         {
00240                 /* if there was stored translation
00241                  * copy stored translation to temporary variable */
00242                 gchar* tmp = g_strconcat(search_atom->trans,
00243                                          "<TRANSLATION_OPEN>",
00244                                          translation,
00245                                          "<TRANSLATION_CLOSED>",
00246                                          NULL);
00247                 /* free memory used by stored old translation */
00248                 gchar* loc_tmp = search_atom->trans;
00249                 search_atom->trans = tmp;
00250                 g_free(loc_tmp);
00251                 tmp = loc_tmp = NULL;
00252         }
00253 
00254         g_debug("<-%s", __FUNCTION__);
00255 }
00256 
00263 gint ws_mng_compare_strings (gconstpointer a, gconstpointer b)
00264 {
00265         gchar** str1 = (gchar**)(a);
00266         gchar** str2 = (gchar**)(b);
00267 
00268         gchar* stra = g_utf8_strdown(str1[0], -1);
00269         gchar* strb = g_utf8_strdown(str2[0], -1);
00270 
00271         gint result = g_utf8_collate(stra, strb);
00272 
00273         g_free(stra); 
00274         g_free(strb);
00275         return result;
00276 }
00277 
00286 void ws_remove_multiple_words(WSMngSearchAtom* user_data)
00287 {
00288         WSMngSearchAtom* search = (WSMngSearchAtom*)user_data;
00289         gint j = 0;
00290         gint i = 0;
00291         gint result = -1;
00292         gint temp = 256;
00293         gchar* tmp1 = NULL;
00294         gchar* tmp2 = NULL;
00295 
00296         /* check if words list is longer than 256 words */
00297         if (search->word_list->len < 256)
00298         {
00299                 temp = search->word_list->len;
00300                 if (temp >0)
00301                 {
00302                         ws_dbus_notify( search->data->dbus_data,
00303                                         WS_DBUS_WORDS_LIST_FILLED_NOT_FULL );
00304                 }
00305         }
00306         else
00307         {
00308                 ws_dbus_notify( search->data->dbus_data,
00309                                 WS_DBUS_WORDS_LIST_FULL );
00310         }
00311 
00312         /* remove repeating words in the first 256 places in the array - words
00313          * in places further than 256 are not being sent to UI so we do not need
00314          * to filter them for searching repeating words */
00315         for (i = 0; i < temp-1; i++)
00316         {
00317                 tmp1 = g_utf8_casefold(
00318                                g_array_index(search->word_list,gchar*,i),
00319                                -1     );
00320                 for (j = i + 1; j < temp; j++)
00321                 {
00322                         /* search if there is a word on word list */
00323                         tmp2 = g_utf8_casefold(
00324                                     g_array_index(search->word_list,gchar*,j),
00325                                     -1 );
00326                         result = g_utf8_collate(tmp1,tmp2);
00327                         g_free(tmp2);
00328                         tmp2 = NULL;
00329 
00330                         /* if there is a word on the word list 
00331                          * remove that word */
00332                         if (result == 0)
00333                         {
00334                                 g_array_remove_index(search->word_list, j);
00335                                 --j;
00336                                 --temp;
00337                                 if (search->word_list->len >= 256)
00338                                 {
00339                                         temp = 256;
00340                                 }
00341                         }
00342                         else {
00343                                 /* there is no possiblity that further
00344                                  * will be the same word, check next word */
00345                                 break;
00346                         }
00347                 }
00348                 g_free(tmp1);
00349                 tmp1 = NULL;
00350         }
00351 }
00352 

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