Copyright 2006 ComArch S.A.
*******************************************************************************/
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-#include <unistd.h>
-#include <glib/gstdio.h>
-#include <gconf/gconf-client.h>
-#include <gconf/gconf.h>
-#include <pthread.h>
-#include <gmodule.h>
-#include <string.h>
#include <ws_manager.h>
-#include <untar.h>
-#define GCONF_KEY "/apps/maemo/WhiteStork/Dictionaries"
-#define GCONF_LIB_PATH "/apps/maemo/WhiteStork/Engines"
-
-#if SQLITE==0
- #define LIBRARY "/usr/lib/libsqlite.so.0"
-#elif SQLITE==3
- #define LIBRARY "/usr/lib/libsqlite3.so.0"
-#endif
-
-#define g_strlen(string) ( NULL == (string) ? 0 : strlen(string) )
-
-struct _WSMngSearchData
+
+
+/* added by Dariusz Wiechecki
+ * trying to lock mutext and tells if it was locked */
+gboolean try_lock_was_locked(WSMngSearchData* data, gchar* fun)
+{
+ gboolean res = !g_static_mutex_trylock(data->action_working);
+ if (res)
+ {
+ g_debug("[M-action] %s - FAILED - locked already!", fun);
+ }
+ else
+ {
+ g_debug("[M-action] %s - SUCCESS - locked!", fun);
+ }
+
+ return res;
+}
+
+/* added by Dariusz Wiechecki
+ * check if recursive mutex is already locked
+ * must be call from critical section!!! */
+gboolean is_rec_locked(GStaticRecMutex* m)
{
- GArray* dict; ///< pointer to GArray structure which stores pointers to fuction used to service dictionaries
- GArray* modules; ///< pointer to GArray structure which stores pointers to fuction used to service modules loading
- gchar* word; ///< word which will be used to search a dictionaries
- pthread_t p_thread; ///< a thread in which we serching
- pthread_t p_thread_trans;
- WSDBusData* dbus_data; ///< a pointer DBUS data
- GMainLoop* loop; ///< pointer to main loop
- //GModule* library; ///< library used to load modules
- GArray* word_list; ///< searched word list
- gchar* trans; ///< searched translation
- guint returned_results; ///< count of returned results
- guint returned_trans_results;
- gchar* last_search; ///< last searched word
- gboolean search_in_history; ///< flag signalig when search in hisory is commieted
- GArray* libraries; ///<tabele used for storing modules libraries
- Engine* bookmark; ///<bookmark engine
- gboolean is_searching;///<searching word list flag
- gboolean is_searching_trans;///<searching translation flag
- GArray* library_path;///<path to library
- gboolean bookmark_mode;
- GModule* bookmark_library;
-};
+ gboolean r = !g_static_rec_mutex_trylock(m);
+ if (FALSE == r)
+ {
+ g_static_rec_mutex_unlock(m);
+ g_debug("[M-stop] STOP mutex is UNLOCKED!");
+ }
+ else
+ {
+ g_debug("[M-stop] STOP mutex is LOCKED");
+ }
+ return r;
+}
+
+/* added by Dariusz Wiechecki
+ * stop current thread if there is such a need - newer thread is waiting */
+void stop_if_needed(WSMngSearchData* data)
+{
+ static GStaticMutex _loc;
+ static GStaticMutex* loc = NULL;
+ if(NULL == loc && NULL == data)
+ {
+ g_debug("Initializing static mutex. function:%s\n",__FUNCTION__);
+ g_static_mutex_init (loc);
+ loc = &_loc;
+ return;
+ }
+
+ /* critical section for calling is_rec_locked() function*/
+ g_static_mutex_lock(loc);
+ if ( is_rec_locked(data->action_stop) )
+ {
+ g_debug("~~~ <---- Leaving thread (not finished).\n");
+ /* TODO clean after this thread!!!! */
+ g_static_mutex_unlock(data->action_working);
+ g_static_mutex_unlock(loc);
+ g_thread_exit (NULL);
+ }
+ g_static_mutex_unlock(loc);
+}
+
/**
* Public function, see ws_manager.h
{
g_debug("%s<->", __FUNCTION__);
WSMngSearchData* result = g_try_new(WSMngSearchData, 1);
- if (result != NULL)
- {
- return result;
- }
- else
+
+ if (NULL == result)
{
- g_debug("Not enought memory !");
+ g_debug("Not enough memory !");
}
- return NULL;
+ return result;
}
-static void ws_mng_extract_dictionary(GError *error, GArray *word,
- gpointer user_data);
/**
* Public function, see ws_manager.h
*/
gboolean ws_mng_start_main_loop(WSMngSearchData* search_data)
{
- g_debug("%s<-->", __FUNCTION__);
+ g_debug("%s <-->", __FUNCTION__);
search_data->loop = g_main_loop_new (NULL, FALSE);
if (search_data->loop == NULL)
{
+ g_debug("Couldn't create new g_main_loop for Manager!");
return FALSE;
}
g_main_loop_run (search_data->loop);
* @param user_data data passed to function
* @param error engine status information
*/
-static void ws_mng_on_found_translation(gchar* translation,
- gchar* pattern,
- gpointer user_data,
- EngineStatus error
+void ws_mng_on_found_translation(gchar* translation,
+ gchar* pattern,
+ gpointer user_data,
+ EngineStatus error
)
{
g_debug("->%s", __FUNCTION__);
WSMngSearchData *data = (WSMngSearchData *) user_data;
-
- data->returned_trans_results--;
- //if there is returned translation
- if (translation != NULL)
- {
- if (data->trans == NULL)
- {
- //concatenate tags and searched word and translation
- data->trans = g_strconcat("<PATTERN_OPEN>",
- pattern,
- "<PATTERN_CLOSED><TRANSLATION_OPEN>",
- translation,
- "<TRANSLATION_CLOSED>",
- NULL);
- }
- else //if thre was stored trnasloation
- {
- if (data->returned_trans_results >= 0)
- {
- //copy stored trnasloation to temporery variable
- gchar* tmp = g_strconcat(data->trans,
- "<TRANSLATION_OPEN>",
- translation,
- "<TRANSLATION_CLOSED>",
- NULL);
- //free memory used by stored translation
- //g_free(data->trans); data->trans = NULL;
- data->trans = g_strdup(tmp);
- g_free(tmp);
- tmp = NULL;
- }
- }
- }
- //if there will be no more returned translations
- if (!data->bookmark_mode && data->returned_trans_results <= 0)
- {
- data->is_searching_trans = FALSE;
- ws_dbus_server_return_translations(data->dbus_data,
- data->trans);
- g_free(data->trans); data->trans = NULL;
- }else if (data->bookmark_mode)
- {
- data->search_in_history = FALSE;
- //send translation to gui
- ws_dbus_server_return_translations(data->dbus_data,
- data->trans);
- g_free(data->trans); data->trans = NULL;
- data->is_searching_trans = FALSE;
+
+ /* if user want to stop searching */
+ stop_if_needed(data);
+
+ /* we get already the first translation */
+ if ((NULL != translation) && (NULL == data->trans))
+ { /* concatenate tags and searched word and translation */
+ data->trans = g_strconcat("<PATTERN_OPEN>",
+ pattern,
+ "<PATTERN_CLOSED><TRANSLATION_OPEN>",
+ translation,
+ "<TRANSLATION_CLOSED>",
+ NULL
+ );
+ }
+ else if (NULL != translation)
+ { /* if there was stored translation *
+ * copy stored translation to temporary variable*/
+ gchar* tmp = g_strconcat(data->trans,
+ "<TRANSLATION_OPEN>",
+ translation,
+ "<TRANSLATION_CLOSED>",
+ NULL);
+ /* free memory used by stored old translation */
+ gchar* loc_tmp = data->trans;
+ data->trans = tmp;
+ g_free(loc_tmp);
+ tmp = loc_tmp = NULL;
}
+
+ /* if user want to stop searching */
+ stop_if_needed(data);
+
g_debug("<-%s", __FUNCTION__);
}
+/* -------------------------------------------------------------------------- */
/**
* Function used to send inforamtion about progress of caching XDXF file to UI
- *@param progess how much file is cached
- *@param user_data data needed to send information to UI
- *@param error error messages
+ * @param progess how much file is cached
+ * @param user_data data needed to send information to UI
+ * @param error error messages
*/
-static void ws_mng_progress_bar(double progress,
- gpointer user_data,
- EngineStatus error
- )
+void ws_mng_progress_bar(double progress,
+ gpointer user_data,
+ EngineStatus error
+ )
{
- //g_debug("->%s", __FUNCTION__);
+ g_debug("<-> %s; progress=%f", __FUNCTION__, progress);
WSMngSearchData *data = (WSMngSearchData *) user_data;
- //return progress of caching to user interface
+ /* return progress of caching to user interface */
ws_dbus_server_update_progressbar(data->dbus_data, progress);
- //g_debug("<-%s", __FUNCTION__);
+
}
/**
*@param dict path to dictionary file
*@return TRUE if dictionary is optimize, FALSE otherwise
*/
-static gboolean ws_mng_if_optimized(gchar* dict)
+gboolean ws_mng_if_optimized(gchar* dict)
{
- GConfClient* client;
-
- gchar* path = NULL;
- //gchar* key_value = NULL;
+ g_debug("->%s", __FUNCTION__);
+
+ GConfClient* client;
+ gchar* path = NULL;
gboolean key_optimized = FALSE;
+ gboolean key_found = FALSE;
gint i = 0;
- //g_type_init();
- g_debug("->%s", __FUNCTION__);
+
client = gconf_client_get_default();
GSList* list = gconf_client_all_dirs(client, GCONF_KEY, NULL);
for (i=0; i<g_slist_length(list); i++)
new_path,
NULL
);
- if (g_ascii_strcasecmp(key_value, dict)==0)
- {
- key_optimized = gconf_client_get_bool(client,
- new_optimized,
- NULL
- );
+ if (g_ascii_strcasecmp(key_value, dict)==0)
+ {
+ key_optimized = gconf_client_get_bool(client,
+ new_optimized,
+ NULL
+ );
+ key_found = TRUE;
}
g_free(new_path);
g_free(new_optimized);
g_free(key_value);
+ if ( TRUE == key_found )
+ {
+ break;
+ }
}
for (i=0; i<g_slist_length(list); i++)
{
g_slist_free(list);
g_object_unref (client);
+ g_debug("<-%s", __FUNCTION__);
return key_optimized;
}
-/**
-* \brief Function used to run search for word engine in threads
-*
-* @param data required data for searach word event
-* @return gpointer return value
-*/
-static gpointer ws_mng_search_word (gpointer data)
-{
- g_debug("--%s", __FUNCTION__);
- WSMngSearchData *search = (WSMngSearchData *) data;
- gint i = 0;
- // set cancel type for thread
- pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
- pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
- //search for word in dictionaries each dictionary
- if (search->bookmark_mode)
- {
- dict_eng_search_word_list(search->bookmark,
- search->word);
- }
- else
- {
- for (i=0; i<search->dict->len; i++)
- {
- if (g_array_index(search->dict, Engine *, i) != NULL)
- {
- dict_eng_search_word_list(
- g_array_index(search->dict, Engine *, i),
- search->word);
- }
- }
- }
- pthread_exit(NULL);
- //g_free(search->word);
-}
+
+/* ------------------------------------------------------------------------ */
/**
* \brief Called when find word event occurs
*
* @param error error messages
* @param user_data user data passed to function
*/
-
-/*static void ws_change_compare_callback (GArray* dicts, gpointer handler, WSMngSearchData *data)
+void ws_mng_on_search_word(GError *error,
+ GArray *word,
+ gpointer user_data
+ )
{
- guint i;
- Engine* engine;
-
- for (i = 0; i < dicts->len; ++i)
+ g_debug("[L-word] -> %s: reading parameters...", __FUNCTION__);
+ WSMngSearchData *search = (WSMngSearchData *) user_data;
+ osso_rpc_t* osso_data = NULL;
+
+ /* ---> CRITICAL SECTION for this function */
+ g_static_mutex_lock( search->thread_creation );
+
+ /* get the word passed by dbus */
+ osso_data = &g_array_index (word, osso_rpc_t, 0);
+ search_t* tmp = (search_t*)g_try_malloc( sizeof(search_t) );
+
+ if (( g_utf8_strchr(osso_data->value.s, -1, '*') == NULL ) &&
+ ( g_utf8_strchr(osso_data->value.s, -1, '?') == NULL ))
{
- engine = g_array_index (dicts, Engine *, i);
-
- dict_eng_set_callback(engine,
- ENGINE_WORD_COMPARE,
- handler,
- data);
-
- };
-};
+ tmp->word = g_strconcat(osso_data->value.s, "*", NULL);
+ }
+ else
+ {
+ tmp->word = g_strdup(osso_data->value.s);
+ }
+ tmp->data = search;
+ tmp->thread = NULL;
+ g_debug("[L-word] creating GThread object...");
+ tmp->thread = g_thread_create (ws_mng_search_word, tmp, TRUE, NULL);
+ g_debug("[L-word] GThread object created. Exiting from CREATOR.");
-static gboolean ws_mng_compare_without_wildcards (gchar* pattern,
- gchar* word)
-{
- if (g_strncasecmp (pattern, word, strlen(pattern)) == 0)
- return TRUE;
-
- else return FALSE;
-};
+ g_static_mutex_unlock( search->thread_creation );
+ /* <--- end of CRITICAL SECTION for this function */
-static gboolean ws_mng_compare_with_wildcards (gchar* pattern,
- gchar* word)
+ g_debug("[L-word] <-%s", __FUNCTION__);
+}
+/* ------------------------------------------------------------------------ */
+void ws_remove_multiple_words(WSMngSearchData* user_data)
{
- GPatternSpec* regex;
- regex = g_pattern_spec_new (pattern);
- gboolean result = g_pattern_match_string (regex, word);
- g_pattern_spec_free (regex);
-
- return result;
-};*/
+ WSMngSearchData* search = (WSMngSearchData*)user_data;
+ gint j = 0;
+ gint i = 0;
+ gint result = -1;
+ gint temp = 256;
+ gchar* tmp1 = NULL;
+ gchar* tmp2 = NULL;
-static void ws_mng_on_search_word(GError *error, GArray *word, gpointer user_data)
-{
- g_debug("->%s", __FUNCTION__);
- WSMngSearchData *data = (WSMngSearchData *) user_data;
- osso_rpc_t* osso_data = NULL;
- //gint i = 0;
-
- //get the word passed by dbus
- osso_data = &g_array_index (word, osso_rpc_t, 0);
- //free memory used by last searched word
- if (data->last_search != NULL) g_free(data->last_search);
- //set last searched word to word passed by dbus
- data->last_search = g_strdup(osso_data->value.s);
- //free memory used by word currently searched
- //if (data->word != NULL) g_free(data->word);
- //set word to search
-
- if ((g_utf8_strchr (osso_data->value.s, -1, '*') == NULL) &&
- (g_utf8_strchr (osso_data->value.s, -1, '?') == NULL))
+ if (search->word_list->len < 256)
{
- data->word = g_strconcat(osso_data->value.s, "*", NULL);
+ temp = search->word_list->len;
}
- else data->word = g_strdup(osso_data->value.s);
-
-
- //free memory sended by dbus
- //osso_rpc_free_val(osso_data);
- //set new number of number of finished searchings
- data->returned_results = data->dict->len;
- //creating new GArray for word list
- data->word_list = g_array_new(TRUE, TRUE, sizeof(gchar*));
- if (data->is_searching == TRUE)
+
+ for (i = 0; i < temp-1; i++)
{
- pthread_cancel(data->p_thread);
- //ws_dbus_server_return_words(data->dbus_data, data->word_list);
+ tmp1 = g_utf8_casefold(
+ g_array_index(search->word_list,gchar*,i),
+ -1
+ );
+ for (j = i + 1; j < temp; j++)
+ {
+ /* search if there is a word on word list */
+ tmp2 = g_utf8_casefold(
+ g_array_index(search->word_list,gchar*,j),
+ -1
+ );
+ result = g_utf8_collate(tmp1,tmp2);
+ g_free(tmp2);
+ tmp2 = NULL;
+ /* if there is a word on the word list
+ * remove that word */
+ if (result == 0)
+ {
+ g_array_remove_index(search->word_list, j);
+ --j;
+ --temp;
+ if (search->word_list->len >= 256){
+ temp = 256;
+ }
+ }
+ else {
+ /* there is no possiblity that further
+ * will be the same word, check next word */
+ break;
+ }
+ }
+ g_free(tmp1);
+ tmp1 = NULL;
}
-
- data->is_searching = TRUE;
- pthread_create(&data->p_thread,
- NULL,
- ws_mng_search_word,
- data); //create thread and run search
- g_debug("<-%s", __FUNCTION__);
}
-
-
-
+/* ------------------------------------------------------------------------ */
/**
* \brief Function used to compare string. Used in sorting GArray object
*
*@return result of compare <0 if the second is greater than first 0 if the
strings are the same >0 if the first string is greater than second
*/
-static gint ws_mng_compare_strings (gconstpointer a, gconstpointer b)
+gint ws_mng_compare_strings (gconstpointer a, gconstpointer b)
{
- //gets the parameters to compare
- //g_debug("--%s", __FUNCTION__);
- gchar** str1 = (gchar**)(a);
- gchar** str2 = (gchar**)(b);
- //converts a string into a form that is independent of case
- gchar* stra = g_utf8_strdown(str1[0], -1);
- gchar* strb = g_utf8_strdown(str2[0], -1);
- //g_debug("Comparing strings: %s vs. %s [ORIGINAL: %s vs. %s]\n",stra, strb, str1[0], str2[0]);
- //compare two string
- gint result = g_utf8_collate(stra, strb);
- //free memory used by converted strings
- g_free(stra);
- g_free(strb);
- //return compare result
- return result;
+ gchar** str1 = (gchar**)(a);
+ gchar** str2 = (gchar**)(b);
+
+ gchar* stra = g_utf8_strdown(str1[0], -1);
+ gchar* strb = g_utf8_strdown(str2[0], -1);
+
+ gint result = g_utf8_collate(stra, strb);
+
+ g_free(stra);
+ g_free(strb);
+ return result;
}
+/* ------------------------------------------------------------------------ */
+/**
+* \brief Function used to run search for word engine in threads
+*
+* @param data required data for search word event
+* @return gpointer return value
+*/
+gpointer ws_mng_search_word (gpointer user_data)
+{
+ g_debug("[T-word] %s: Entering thread...", __FUNCTION__);
+ search_t* data = (search_t*)user_data;
+ WSMngSearchData *search = data->data;
+
+ /* enter into CRITICAL SECTION */
+ if (try_lock_was_locked(search,(gchar*)__FUNCTION__))
+ {
+ g_printf("[T-word] STOP mutex is locked! Aborting others!");
+ g_static_rec_mutex_lock(search->action_stop);
+ g_static_mutex_lock(search->action_working);
+ }
+ g_static_rec_mutex_unlock(search->action_stop);
+
+ /* if another thread was run after this one - exit */
+ stop_if_needed(search);
+
+ g_debug("[T-word] %s - from now this thread is \'singleton\' ",
+ __FUNCTION__
+ );
+
+ /* creating new GArray for word list */
+ search->word_list = g_array_new(TRUE, TRUE, sizeof(gchar*));
+
+ g_debug("[T-word] %s - start searching... ",__FUNCTION__);
+ if (search->bookmark_mode)
+ { /* search only in user bookmarks */
+ dict_eng_search_word_list(search->bookmark,
+ data->word);
+ }
+ else
+ { /* search for word in each dictionary */
+ gint i = 0;
+ for (i = 0; i < search->dict->len; i++)
+ {
+ if (NULL == g_array_index(search->dict, Engine *, i))
+ {
+ continue;
+ }
+
+ stop_if_needed(search);
+ Engine* dict = g_array_index(search->dict,Engine *,i);
+ dict_eng_search_word_list(dict, data->word);
+ }
+ }
+ g_debug("[T-word] %s - searching finished.",__FUNCTION__);
+ /* if another thread was run after this one - exit */
+ stop_if_needed(search);
+
+ /* sort and cleaning words list - only if there were more than one
+ * dictionary loaded */
+ if ((FALSE == search->bookmark_mode) || (1 < search->dict->len))
+ {
+ g_array_sort(search->word_list, ws_mng_compare_strings);
+ ws_remove_multiple_words(search);
+ }
+
+ /* if another thread was run after this one - exit */
+ stop_if_needed(search);
+
+ ws_dbus_server_return_words(search->dbus_data, search->word_list);
+
+ /* free memory used by each word from word list */
+ gint i = 0;
+ for (; i < search->word_list->len; i++)
+ {
+ g_free(g_array_index(search->word_list,gchar* ,i));
+ }
+
+ /* free memory used by GArray */
+ g_array_free(search->word_list, TRUE);
+ search->word_list = NULL;
+
+ g_free(data->word);
+ g_free(data);
+ g_debug("[T-word] %s - leaving thread!", __FUNCTION__);
+ g_static_mutex_unlock(search->action_working);
+ return NULL;
+}
/**
* \brief Used to return results of found word from threads
* @param user_data data passed to function
* @param error engine status information
*/
-static void ws_mng_on_found_word(GArray* list,
+void ws_mng_on_found_word(GArray* list,
gchar* pattern,
gpointer user_data,
EngineStatus error)
{
- g_debug("->%s", __FUNCTION__);
- WSMngSearchData *data = (WSMngSearchData *) user_data;
- gint i = 0;
- //decrement returned results count
- data->returned_results--;
+ g_debug("[T-word-ret]-> %s", __FUNCTION__);
- for (i=0; i<list->len; i++)
- {
- //copy word found by search engine
- gchar* new_word = g_strdup(g_array_index(list, gchar*, i));
- //add word to word list
+ WSMngSearchData *data = (WSMngSearchData *) user_data;
+ static gint i = 0;
+ for (i = 0; i < list->len; i++)
+ {
+ /* copy word found by search engine */
+ gchar* new_word = g_strdup(g_array_index(list, gchar*, i));
g_array_append_val(data->word_list, new_word);
}
-
- if (!data->bookmark_mode && data->returned_results <= 0)
- {
- int j = 0;
- //sort list of words
- g_array_sort(data->word_list, ws_mng_compare_strings);
- gint result = -1;
- gint temp = 256;
- gchar* tmp1 = NULL;
- gchar* tmp2 = NULL;
- if (data->word_list->len < 256) temp = data->word_list->len;
- for (i = 0; i < temp-1; i++)
- {
- tmp1 = g_utf8_casefold(
- g_array_index(data->word_list,gchar*,i),
- -1
- );
- for (j = i + 1; j < temp; j++)
- {
- //search if there is a word on word list
- tmp2 = g_utf8_casefold(
- g_array_index(data->word_list,gchar*,j),
- -1
- );
- result = g_utf8_collate(tmp1,tmp2);
- g_free(tmp2);
- tmp2 = NULL;
- //if there is a word on the word list
- //remove that word
- if (result == 0)
- {
- g_array_remove_index(data->word_list, j);
- --j;
- --temp;
- if (data->word_list->len >= 256){
- temp = 256;
- }
- }
- else {
- //there is no possiblity that further
- //will be the same word, check next word
- break;
- }
- }
- g_free(tmp1);
- tmp1 = NULL;
- }
- //send words via dbus to gui
- data->is_searching = FALSE;
- ws_dbus_server_return_words(data->dbus_data, data->word_list);
- //free memory used by each word from word list
- for (i=0; i<data->word_list->len; i++)
- {
- g_free(g_array_index(data->word_list,gchar* ,i));
- }
- //free memory used by GArray
- g_array_free(data->word_list, TRUE); data->word_list = NULL;
- }else if (data->bookmark_mode)
- {
-
- ws_dbus_server_return_words(data->dbus_data, data->word_list);
- //free memory used by each word from word list
- for (i=0; i<data->word_list->len; i++)
- {
- g_free(g_array_index(data->word_list,gchar* ,i));
- }
- //free memory used by GArray
- g_array_free(data->word_list, TRUE); data->word_list = NULL;
- data->is_searching = FALSE;
- }
- g_debug("<-%s", __FUNCTION__);
+
+ g_debug("[T-word-ret]<- %s", __FUNCTION__);
}
+
+
+/**
+* \brief Called when find translation event occurs
+*
+* @param word word to search
+* @param error error messages
+* @param user_data user data passed to function
+*/
+void ws_mng_on_search_translation (GError *error, GArray *word,
+ gpointer user_data)
+{
+ g_debug("[L-tran] ->%s", __FUNCTION__);
+
+ WSMngSearchData *data = (WSMngSearchData *) user_data;
+
+ /* ---> CRITICAL SECTION for this function */
+ g_static_mutex_lock(data->thread_creation);
+
+ osso_rpc_t *osso_data;
+ /* get the data sended by dbus */
+ osso_data = &g_array_index (word, osso_rpc_t, 0);
+ /* get the word sended by dbus */
+ data->word = g_strdup(osso_data->value.s);
+
+ g_debug("[L-tran] creating GThread object...");
+ g_thread_create (ws_mng_search_translation, data, TRUE, NULL);
+ g_debug("[L-tran] GThread object created. Exiting from CREATOR.");
+
+
+ g_static_mutex_unlock(data->thread_creation);
+ /* <--- end of CRITICAL SECTION for this function */
+
+ g_debug("[L-tran] <-%s", __FUNCTION__);
+}
+
+/* -------------------------------------------------------------------------- */
/**
* \brief Function used to run search for transaltion of word engine in threads
*
* @param data required data for search translation
* @return gpointer return value
*/
-static gpointer ws_mng_search_translation (gpointer data)
+gpointer ws_mng_search_translation (gpointer data)
{
- g_debug("--%s", __FUNCTION__);
+ g_debug("[T-tran] %s: Entering thread...", __FUNCTION__);
WSMngSearchData *search = (WSMngSearchData *) data;
- gint i = 0;
- // set cancel type for thread
- pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
- pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
- //run search for translation for evry dictionary
- if (search->bookmark_mode)
+
+ /* ---> CRITICAL SECTION for this function */
+ if (try_lock_was_locked(search,(gchar*)__FUNCTION__))
+ {
+ g_printf("[T-tran] STOP mutex is locked! Aborting others!");
+ g_static_rec_mutex_lock(search->action_stop);
+ g_static_mutex_lock(search->action_working);
+ }
+ g_static_rec_mutex_unlock(search->action_stop);
+ /* if another thread was run after this one - exit */
+ stop_if_needed(search);
+
+ g_debug("[T-tran] %s - from now this thread is \'singleton\' ",
+ __FUNCTION__
+ );
+
+ /* run search for translation for every dictionary */
+ if (search->bookmark_mode)
{
dict_eng_search_word_translation(search->bookmark,
- search->word);
+ search->word);
}
else
{
- for (i=0; i<search->dict->len; i++)
+ gint i;
+ for (i = 0; i < search->dict->len; i++)
{
- if (g_array_index(search->dict, Engine*, i) != NULL)
+ stop_if_needed(search);
+ if (NULL == g_array_index(search->dict, Engine*, i) )
{
- dict_eng_search_word_translation(g_array_index(
- search->dict,
- Engine*,
- i),
- search->word);
- }
+ continue;
+ }
+ dict_eng_search_word_translation(
+ g_array_index(search->dict, Engine*, i),
+ search->word
+ );
}
}
- pthread_exit(NULL);
-}
+ g_debug("[T-tran] %s - searching finished.",__FUNCTION__);
-/**
-* \brief Called when find translation event occurs
-*
-* @param word word to search
-* @param error error messages
-* @param user_data user data passed to function
-*/
-static void ws_mng_on_search_translation (GError *error, GArray *word,
- gpointer user_data)
-{
- g_debug("->%s", __FUNCTION__);
- WSMngSearchData *data = (WSMngSearchData *) user_data;
- osso_rpc_t *osso_data;
- //get the data sended by dbus
- osso_data = &g_array_index (word, osso_rpc_t, 0);
- //get the word sended by dbus
- data->word = g_strdup(osso_data->value.s);
- //set how many dictionaries to search
- //data->returned_results = data->dict->len;
- data->returned_trans_results = data->dict->len;
- //g_printf("%s word %s\n", __FUNCTION__, data->word);
- //data->trans = NULL;
- //free data sended by dbus
- //osso_rpc_free_val(osso_data);
- //create thread
- if (data->is_searching_trans == TRUE)
- {
- pthread_cancel(data->p_thread_trans);
- g_free(data->trans); data->trans = NULL;
- gchar* tmp = " ";
- ws_dbus_server_return_translations(data->dbus_data,
- tmp);
-
- }
- data->is_searching_trans = TRUE;
- pthread_create(&data->p_thread_trans,
- NULL,
- ws_mng_search_translation,
- (gpointer) data);
- g_debug("<-%s", __FUNCTION__);
+ /* if another thread was run after this one - exit */
+ stop_if_needed(search);
+
+ /* send translation to gui */
+ ws_dbus_server_return_translations(search->dbus_data, search->trans);
+
+ g_free(search->trans);
+ search->trans = NULL;
+
+ g_debug("[T-word] %s - leaving thread!", __FUNCTION__);
+ g_static_mutex_unlock(search->action_working);
+ return NULL;
}
+
/**
*Function used to load dictionaries engines
*@param dict_directory path to dictionaries
*@param data structure which contains all data of program
*/
-static void ws_mng_load_dict(GArray* dict_directory, WSMngSearchData* data)
+void ws_mng_load_dict(GArray* dict_directory, WSMngSearchData* data)
{
gint i = 0;
}
/**
- * Function used to run search for transaltion when search in history event occurs
- *@param error error messages
- *@param word pattern to serch for translation
- *@param user_data data needed to send results to UI
- */
-static void ws_mng_on_search_in_history(GError *error, GArray *word,
- gpointer user_data)
-{
- g_debug("->%s", __FUNCTION__);
- WSMngSearchData *data = (WSMngSearchData *) user_data;
- osso_rpc_t *osso_data;
- //set the falg used to signaling
- data->search_in_history = TRUE;
- //get data sended by dbus
- osso_data = &g_array_index (word, osso_rpc_t, 0);
- //copy the word sended by dbus
- data->word = g_strdup(osso_data->value.s);
- //set how many dictionaries to search
- //data->returned_results = data->dict->len;
- data->returned_trans_results = data->dict->len;
- data->trans = NULL;
- //free memory sended by dbus
- //osso_rpc_free_val(osso_data);
- //create thread
- if (data->is_searching_trans == TRUE)
- {
- pthread_cancel(data->p_thread_trans);
- }
- data->is_searching_trans = TRUE;
- //g_printf("\n\n-----------------------------Search in history word - %s--\n\n", data->word);
- pthread_create(&data->p_thread_trans,
- NULL,
- ws_mng_search_translation,
- (gpointer) data);
- g_debug("<-%s", __FUNCTION__);
-}
-
-/**
*Function used to get the dictionaries location read fron GConf
*@return path to dictionaries location
*/
-static GArray* ws_mng_read_gconf()
+GArray* ws_mng_read_gconf()
{
//g_type_init();
g_debug("--%s", __FUNCTION__);
* @param signal type of signal
* @param user_data data passed to function
*/
-static void ws_mng_signal_handling(GError *error, GArray *signal,
+void ws_mng_signal_handling(GError *error, GArray *signal,
gpointer user_data)
{
g_debug("->%s", __FUNCTION__);
switch(osss_data.value.i)
{
case WS_DBUS_INFO_TERMINATE:
-
+
g_main_loop_quit (data->loop);
- break;
-
- case WS_DBUS_INFO_STOP_SEARCH:
- if (data->is_searching == TRUE)
- {
- pthread_cancel(data->p_thread);
- //data->word_list = NULL;
- GArray* tmp_array =
- g_array_new(TRUE, TRUE, sizeof(gchar*));
- ws_dbus_server_return_words(data->dbus_data,
- tmp_array);
- }
- data->is_searching = FALSE;
- if (data->is_searching_trans == TRUE)
+ break;
+ case WS_DBUS_INFO_STOP_SEARCH:
+ /* added by Dariusz Wiechecki
+ * canceling GLib Threads */
+ if (try_lock_was_locked(data,(gchar*)__FUNCTION__))
{
- pthread_cancel(data->p_thread_trans);
- //data->trans = NULL;
- gchar* tmp = " ";
- ws_dbus_server_return_translations(
- data->dbus_data,
- tmp);
+ g_printf("~~~STOP ACTION! %s\n",__FUNCTION__);
+ g_static_rec_mutex_lock(data->action_stop);
+ g_static_mutex_lock(data->action_working);
}
- data->is_searching_trans = FALSE;
- //pthread_t pthread_self (void);
- //pthread_exit(NULL);
+ g_static_rec_mutex_unlock(data->action_stop);
+ g_static_mutex_unlock(data->action_working);
break;
-
+
case WS_DBUS_INFO_CONFIG_CHANGED:
ws_dbus_notify(data->dbus_data, WS_DBUS_INFO_CACHING);
)
);
}
- //g_free(g_array_index(data->dict, Engine*, i));
}
g_array_free(data->dict, TRUE);
- data->dict = g_array_new (TRUE, TRUE, sizeof(Engine*));
- //dict_eng_destroy(data->bookmark);
- //ws_mng_load_bookmark(data);
- //g_debug("bookmark engine at %p", data->bookmark);
+ data->dict = g_array_new (TRUE, TRUE, sizeof(Engine*));;
//paths to directories
GArray* dir_array = ws_mng_read_gconf();
//fill the table again with new dicnioraries
g_free(data->word); data->word = NULL;
data->word = g_strdup(data->last_search);
//create searching thread
- pthread_create (&data->p_thread,
+ /*pthread_create (&data->p_thread,
NULL,
ws_mng_search_word,
- data);
+ data);*/
}
//signal end of dictionary load to gui
ws_dbus_notify(data->dbus_data,
*@param param word and translation of word to adding
*@param user_data structure holding nedded parameters
*/
-static void ws_mng_add_bookmark(GError *error, GArray* param,
+void ws_mng_add_bookmark(GError *error, GArray* param,
gpointer user_data)
{
g_debug("->%s", __FUNCTION__);
*@param param word and translation of word to adding
*@param user_data structure holding nedded parameters
*/
-static void ws_mng_remove_bookmark(GError *error, GArray* param,
+void ws_mng_remove_bookmark(GError *error, GArray* param,
gpointer user_data)
{
g_debug("->%s", __FUNCTION__);
void ws_mng_init_dbus (WSMngSearchData *data)
{
g_debug("->%s", __FUNCTION__);
- //create data structure needed to comunicate with dbus wrapper
- data->dbus_data = ws_dbus_create ("WhiteStorkManager", "v1.0");
- //set data used to comunicate with gui
- ws_dbus_config(data->dbus_data,
- WS_DBUS_CONFIG_SERVICE,
- "org.maemo.WhiteStorkManager");
- ws_dbus_config(data->dbus_data,
- WS_DBUS_CONFIG_OBJECT,
- "/org/maemo/WhiteStorkManager");
- ws_dbus_config(data->dbus_data,
- WS_DBUS_CONFIG_IFACE,
- "org.maemo.WhiteStorkManager");
- ws_dbus_config(data->dbus_data,
- WS_DBUS_CONFIG_REMOTE_SERVICE,
- "org.maemo.WhiteStorkGui");
- ws_dbus_config(data->dbus_data,
- WS_DBUS_CONFIG_REMOTE_OBJECT,
- "/org/maemo/WhiteStorkGui");
- ws_dbus_config(data->dbus_data,
- WS_DBUS_CONFIG_REMOTE_IFACE,
- "org.maemo.WhiteStorkGui");
-
- ws_dbus_connect(data->dbus_data);
-
- ws_dbus_set_cb(data->dbus_data,
- "find_word",
- ws_mng_on_search_word,
- data);// set callback for find word signal
- ws_dbus_set_cb(data->dbus_data,
- "find_translation",
- ws_mng_on_search_translation,
- data); //set callback for find translation signal
- ws_dbus_set_cb(data->dbus_data,
- "signal",
- ws_mng_signal_handling,
- data); //set callback for close program signal
- ws_dbus_set_cb(data->dbus_data,
- "search_in_history",
- ws_mng_on_search_in_history,
- data);//set callback for search in history signal
+
+ /* create data structure needed to comunicate with dbus wrapper */
+ data->dbus_data = ws_dbus_create ("WhiteStorkManager", "v1.0");
+
+ /* set data used to comunicate with gui */
+ ws_dbus_config(data->dbus_data,
+ WS_DBUS_CONFIG_SERVICE,
+ "org.maemo.WhiteStorkManager");
+ ws_dbus_config(data->dbus_data,
+ WS_DBUS_CONFIG_OBJECT,
+ "/org/maemo/WhiteStorkManager");
+ ws_dbus_config(data->dbus_data,
+ WS_DBUS_CONFIG_IFACE,
+ "org.maemo.WhiteStorkManager");
+ ws_dbus_config(data->dbus_data,
+ WS_DBUS_CONFIG_REMOTE_SERVICE,
+ "org.maemo.WhiteStorkGui");
+ ws_dbus_config(data->dbus_data,
+ WS_DBUS_CONFIG_REMOTE_OBJECT,
+ "/org/maemo/WhiteStorkGui");
+ ws_dbus_config(data->dbus_data,
+ WS_DBUS_CONFIG_REMOTE_IFACE,
+ "org.maemo.WhiteStorkGui");
+
+ ws_dbus_connect(data->dbus_data);
+
+ /* set callback for find word signal */
+ ws_dbus_set_cb(data->dbus_data,
+ "find_word",
+ ws_mng_on_search_word,
+ data);
+
+ /* set callback for find translation signal */
+ ws_dbus_set_cb(data->dbus_data,
+ "find_translation",
+ ws_mng_on_search_translation,
+ data);
+
+ /* set callback for close program signal */
ws_dbus_set_cb(data->dbus_data,
- "add_bookmark",
- ws_mng_add_bookmark,
- data);//set callback for add bookmarks signal
+ "signal",
+ ws_mng_signal_handling,
+ data);
+
+ /* set callback for add bookmarks signal */
ws_dbus_set_cb(data->dbus_data,
- "remove_bookmark",
- ws_mng_remove_bookmark,
- data);//set callback for remove bookmarks signal
+ "add_bookmark",
+ ws_mng_add_bookmark,
+ data);
+
+ /* set callback for remove bookmarks signal */
ws_dbus_set_cb(data->dbus_data,
- "extract_dictionary",
- ws_mng_extract_dictionary,
- data);
+ "remove_bookmark",
+ ws_mng_remove_bookmark,
+ data);
+ /* set callback for extracting dictionary */
+ ws_dbus_set_cb(data->dbus_data,
+ "extract_dictionary",
+ ws_mng_extract_dictionary,
+ data);
- g_debug("<-%s", __FUNCTION__);
+ g_debug("<-%s", __FUNCTION__);
}
/**
*Function used to get plugins location read from GConf
*@return libraries location
*/
-static GArray* ws_mng_get_engines_location()
+GArray* ws_mng_get_engines_location()
{
//g_type_init();
*Function used to get bookmark library location
*@return path to bookmark location
*/
-static gchar* ws_mng_get_boomark_location()
+gchar* ws_mng_get_boomark_location()
{
g_debug("->%s", __FUNCTION__);
//g_type_init();
*Function used to load bookmark module
*@param data structure which holds the loaded module
*/
-static void ws_mng_load_bookmark(WSMngSearchData* data)
+void ws_mng_load_bookmark(WSMngSearchData* data)
{
g_debug("->%s", __FUNCTION__);
guint i = 0;
*/
void ws_mng_init (WSMngSearchData *data)
{
-
g_debug("->%s", __FUNCTION__);
data->dict = g_array_new(TRUE, TRUE, sizeof(Engine *));
data->modules = g_array_new(TRUE, TRUE, sizeof(EngineModule));
data->libraries = g_array_new(TRUE, TRUE, sizeof(GModule*));
- data->is_searching = FALSE;
- data->is_searching_trans = FALSE;
- //data->library = NULL;
data->word_list = NULL;
- //g_array_new(TRUE, TRUE, sizeof(gchar*));
data->last_search = NULL;
data->trans = NULL;
data->search_in_history = FALSE;
data->word = NULL;
data->bookmark = NULL;
data->bookmark_mode = FALSE;
-
+ /* added by Dariusz Wiechecki
+ * mutex initialization */
+ data->action_working = (GStaticMutex*)g_try_malloc(sizeof(GStaticMutex));
+ data->thread_creation = (GStaticMutex*)g_try_malloc(sizeof(GStaticMutex));
+ data->action_stop = (GStaticRecMutex*)g_try_malloc(sizeof(GStaticRecMutex));
+ g_assert(NULL != (data->action_working) );
+ g_assert(NULL != (data->action_stop) );
+ g_static_mutex_init (data->action_working);
+ g_static_mutex_init (data->thread_creation);
+ g_static_rec_mutex_init (data->action_stop);
+ /* initialize static stop_if_needed function mutex*/
+ stop_if_needed (NULL);
+ /* initialize GThread structure */
+ //data->action_thread = NULL;
+
#ifdef SQLITE
if (g_file_test(LIBRARY,
G_FILE_TEST_EXISTS) == FALSE)
#endif
GArray* dict_directory = ws_mng_read_gconf(); //paths to dictionaries
- gint i = 0;
+ gint i = 0;
data->returned_results = dict_directory->len;
data->returned_trans_results = dict_directory->len;
ws_mng_load_bookmark(data);
if (dict_directory->len > 0)
{
- ws_dbus_notify(data->dbus_data, WS_DBUS_INFO_CACHING);
-
- ws_mng_load_dict(dict_directory, data);
- guint i = 0;
- g_debug("Trace bookmark engine %p", data->bookmark);
- for (i=0; i<data->dict->len; i++)
- {
- g_debug("dict engines at %p",
- g_array_index(data->dict, Engine*, i));
- }
- ws_dbus_notify(data->dbus_data,
- WS_DBUS_INFO_CACHING_FINISHED);
+ ws_dbus_notify(data->dbus_data, WS_DBUS_INFO_CACHING);
+
+ ws_mng_load_dict(dict_directory, data);
+ guint i = 0;
+ g_debug("Trace bookmark engine %p", data->bookmark);
+ for (i=0; i<data->dict->len; i++)
+ {
+ g_debug("dict engines at %p",
+ g_array_index(data->dict, Engine*, i));
+ }
+ ws_dbus_notify(data->dbus_data,
+ WS_DBUS_INFO_CACHING_FINISHED);
}
else
{
- ws_dbus_notify(data->dbus_data,
- WS_DBUS_ERROR_FILE_NOT_FOUND);
+ ws_dbus_notify(data->dbus_data,
+ WS_DBUS_ERROR_FILE_NOT_FOUND);
}
for (i=0; i<dict_directory->len; i++)
{
g_free(g_array_index(dict_directory, gchar*, i));
}
g_array_free(dict_directory, TRUE);
- g_debug("<-%s", __FUNCTION__);
-}
+ g_debug("<-%s", __FUNCTION__);
+}
/**
*Public function, see ws_manager.h
{
int i = 0;
g_debug("->%s", __FUNCTION__);
- //pthread_exit(NULL);
- //pthread_exit(NULL);
+
ws_dbus_destroy (data->dbus_data); // deinitialization of dbus
if (data->bookmark != NULL)
{
g_free(data->loop);
+ g_free(data->thread_creation);
+ g_free(data->action_working);
+ g_free(data->action_stop);
/*if (data->word != NULL) g_free(data->word);*/
//if (data->trans != NULL) g_free(data->trans);
g_debug("<-%s", __FUNCTION__);
}
-static void ws_mng_extract_dictionary(GError *error, GArray* param,
- gpointer user_data)
+void ws_mng_extract_dictionary(GError *error,
+ GArray* param,
+ gpointer user_data)
{
g_debug("->%s", __FUNCTION__);
WSMngSearchData *data = (WSMngSearchData *) user_data;
}
g_free(path);
- //_free(dest);
g_debug("<-%s", __FUNCTION__);
}
-#ifdef __cplusplus
-}
-#endif
+