Welcome picture has been added:-)
[mdictionary] / gui / src / ws_gui_callbacks.c
1 /*****************
2 * Copyright 2006, ComArch S.A
3 * All rights reserved
4 *****************/
5
6 #include "../../include/ws_gui_callbacks.h"
7 #include "../../include/ws_gui.h"
8 #include "../../include/ws_gui_layout.h"
9 #include "../../include/ws_dbus.h"
10
11 /** \brief show how much time did take a callback of another function
12  *
13  */
14 static double timer(gboolean start, gchar* message)
15 {
16         static GArray* stack = NULL;
17         static gboolean first_run = TRUE;
18         static struct timeval actual_time;
19         static struct timeval last_time;
20         static struct timeval result;
21         static double seconds = 0.0;
22         if(first_run)   
23         {
24                 first_run = FALSE;
25                 //g_debug("XDXF->%s() - TOP OF THE STACK!\n",__FUNCTION__);
26                 stack = g_array_new(TRUE, TRUE, sizeof(struct timeval));
27         };      
28
29         if (start)      // things to do on the beggining of function's work
30         {
31                 g_debug("XDXF->%s() start counting time for function '%s()'.\n",__FUNCTION__,message);
32                 g_array_prepend_val(stack, actual_time);
33                 gettimeofday(&g_array_index(stack, struct timeval, 0),NULL);
34                 return -1.0;
35         }
36         else {          // we just want to end some timer - print some information about working time;
37                 gettimeofday(&actual_time,NULL);
38                 last_time = g_array_index(stack, struct timeval, 0);
39                 g_array_remove_index(stack, 0);
40
41                 if (actual_time.tv_usec < last_time.tv_usec) {
42                         int nsec = (last_time.tv_usec - actual_time.tv_usec) / 1000000 + 1;
43                         last_time.tv_usec -= 1000000 * nsec;
44                         last_time.tv_sec += nsec;
45                 }
46                 if (actual_time.tv_usec - last_time.tv_usec > 1000000) {
47                         int nsec = (last_time.tv_usec - actual_time.tv_usec) / 1000000;
48                         last_time.tv_usec += 1000000 * nsec;
49                         last_time.tv_sec -= nsec;
50                 }
51                 result.tv_sec = actual_time.tv_sec - last_time.tv_sec;
52                 result.tv_usec = actual_time.tv_usec - last_time.tv_usec;
53                 seconds = (((double)(result.tv_usec)) / 1e6) + ((double)(result.tv_sec));
54
55                 g_debug("XDXF->%s() function \'%s()\' was working for: %g [s] or %ld [us].\n",__FUNCTION__,message,seconds,((long)(result.tv_sec*1e6)+(result.tv_usec)));
56                 if(stack->len == 0)     // stack is empty so we delete everything
57                 {
58                         g_array_free(stack, TRUE);
59                         first_run = TRUE;
60                 }
61                 return seconds;
62         }
63         return -2.0;
64 }
65
66
67
68
69
70 /*dbus*/
71 /**  this function handles signals from dbus; it is called when there are any messages from other modules
72  *
73  * @param error - error message recived from DBUS
74  * @param words - array with recived data structure
75  * @param user_data - pointer to data structure
76  * @return
77  */
78 void ws_gui_signal_hander (GError *error, GArray *words, gpointer user_data)
79 {
80         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
81         osso_rpc_t osss_data;
82         osss_data = g_array_index (words, osso_rpc_t, 0);
83         switch(osss_data.value.i)
84         {
85                 case WS_DBUS_ERROR_ENGINE_NOT_FOUND:
86                 {
87                 ws_gui_app->ws_message_dialog = gtk_message_dialog_new (GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
88                                   GTK_DIALOG_DESTROY_WITH_PARENT,
89                                   GTK_MESSAGE_ERROR,
90                                   GTK_BUTTONS_OK,
91                                   "An error occured");
92                 gtk_widget_show_all(ws_gui_app->ws_message_dialog);
93
94                 g_signal_connect_swapped (GTK_OBJECT (ws_gui_app->ws_message_dialog), "response", G_CALLBACK (gtk_main_quit), ws_gui_app);
95                 //g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, ".::GUI::. Engine not found");
96                 break;
97                 }
98
99                 case WS_DBUS_ERROR_FILE_NOT_FOUND:
100                 {
101                 ws_gui_app->ws_message_dialog = gtk_message_dialog_new (GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
102                                   GTK_DIALOG_DESTROY_WITH_PARENT,
103                                   GTK_MESSAGE_ERROR,
104                                   GTK_BUTTONS_OK,
105                                   "There is no dictionary file available");
106                 
107                 gtk_widget_show_all(ws_gui_app->ws_message_dialog);
108                 g_signal_connect_swapped (GTK_OBJECT (ws_gui_app->ws_message_dialog), "response", G_CALLBACK (gtk_main_quit), ws_gui_app);
109                 break;
110                 }       
111
112                 case WS_DBUS_INFO_CACHING:
113                 {
114                 ws_gui_app->ws_gui_banner_caching = hildon_banner_show_animation(GTK_WIDGET(ws_gui_app->ws_gui_hildon_window), NULL, "caching ..." );
115                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_hildon_find_toolbar), FALSE);
116                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_search), FALSE);
117                 break;
118                 }
119                 
120                 case WS_DBUS_INFO_CACHING_FINISHED:
121                 {
122                 gtk_widget_destroy(GTK_WIDGET(ws_gui_app->ws_gui_banner_caching));
123                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_hildon_find_toolbar), TRUE);
124                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_search), TRUE);
125                 break;
126                 }
127         }
128 }
129
130 /** this function handles signal from dbus and transfer recived (found in a dictionary) words to the words list
131  *
132  * @param error - error message recived from DBUS
133  * @param words - array with recived data structure
134  * @param user_data - pointer to data structure
135  * @return
136  */
137 void ws_gui_dbus_return_words (GError *error, GArray *words, gpointer user_data)
138 {
139         timer(TIMER_START, (gchar*)__FUNCTION__);
140         guint i;
141         osso_rpc_t data;
142         //char *data;
143
144         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
145         
146         GArray *tmp;
147         tmp = g_array_new(TRUE, TRUE, sizeof(gchar*));
148         gchar *tmp_word;
149
150         for (i=0;i<words->len;++i)
151         {
152                 data = g_array_index (words, osso_rpc_t, i);
153                 tmp_word = g_strconcat(data.value.s,NULL);
154                 //data = g_array_index (words, char *, i);
155                 //tmp_word = g_strconcat(data,NULL);
156                 g_array_append_val(tmp, tmp_word);
157                 
158         }
159         
160         gtk_widget_destroy(GTK_WIDGET(ws_gui_app->ws_gui_banner));
161         ws_gui_app->ws_gui_banner_flag = FALSE;
162         gtk_widget_set_sensitive(GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_stop), FALSE);
163         gtk_widget_set_sensitive(GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_stop), FALSE);
164         gtk_list_store_clear(ws_gui_app->ws_gui_w_list->ws_gui_store);
165
166         ws_gui_app->ws_gui_w_list->ws_gui_model = create_and_fill_model(tmp, ws_gui_app);
167         
168         if (tmp->len == 0)
169         {
170                 gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window), "No words found" );
171         }
172         timer(TIMER_STOP, (gchar*)__FUNCTION__);
173 }
174
175 /** this function handles signal from dbus and send recived data to the translation area
176  *
177  * @param error - error message recived from DBUS
178  * @param words - array with recived data structure
179  * @param user_data - pointer to data structure
180  * @return
181  */
182 void ws_gui_dbus_return_translation (GError *error, GArray *words, gpointer user_data)
183 {
184         timer(TIMER_START, (gchar*)__FUNCTION__);       
185         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
186         //g_printf("\n\n .::GUI::. User data in dbus_return_translation: %p\n\n", ws_gui_app);
187         
188         osso_rpc_t data;
189         
190         {
191                 data = g_array_index (words, osso_rpc_t, 0);
192                 ws_gui_fill_html(data.value.s, ws_gui_app);
193         }
194         
195         timer(TIMER_STOP, (gchar*)__FUNCTION__);
196 }
197
198 /**
199 * this function allows to free allocated memory
200 *
201 * @param user_data - pointer to data structure
202 */
203 void ws_gui_free_memory(gpointer user_data)
204 {
205         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
206         g_array_free(ws_gui_app->ws_gui_history, TRUE);
207         g_free(ws_gui_app->ws_gui_w_list);
208         g_free(ws_gui_app->ws_gui_menu);
209         g_free(ws_gui_app);
210 }
211
212 /** this function handle press signals (keyboard)
213  * 
214  * @param widget
215  * @param keyevent
216  * @param user_data - ponter to data structure
217  * @return TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
218  */
219 gboolean hildon_key_press_listener (GtkWidget * widget, GdkEventKey * keyevent, gpointer user_data)
220 {
221 WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
222 // g_printf("\nKey code: Enter=%d, passede=%d", GDK_Return,(keyevent->keyval));
223 switch ((guint)(keyevent->keyval)) {
224         case GDK_Up: //up
225         {
226         gtk_container_set_focus_vadjustment(GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left), gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ws_gui_app->ws_gui_scrolledwindow_left)));
227         ws_gui_app->v_new_value = gtk_adjustment_get_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_vadj)) - ws_gui_app->v_delta;
228         if (ws_gui_app->v_new_value > ws_gui_app->ws_gui_vadj->lower) 
229         {
230         gtk_adjustment_set_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_vadj), ws_gui_app->v_new_value);
231         }
232         break;
233         }
234         
235         case GDK_Down: //down
236         {
237         gtk_container_set_focus_vadjustment(GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left), gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ws_gui_app->ws_gui_scrolledwindow_left)));
238         ws_gui_app->v_new_value = gtk_adjustment_get_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_vadj)) + ws_gui_app->v_delta;
239         if (ws_gui_app->v_new_value < (ws_gui_app->ws_gui_vadj->upper - ws_gui_app->ws_gui_vadj->page_size)) 
240         {
241         gtk_adjustment_set_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_vadj), ws_gui_app->v_new_value);
242         }
243         break;
244         }
245         
246         case GDK_Left: //left
247         {
248         gtk_container_set_focus_hadjustment(GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left), gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(ws_gui_app->ws_gui_scrolledwindow_left)));
249         ws_gui_app->h_new_value = gtk_adjustment_get_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_hadj)) - ws_gui_app->h_delta;
250         if (ws_gui_app->h_new_value > ws_gui_app->ws_gui_hadj->lower) 
251         {
252         gtk_adjustment_set_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_hadj), ws_gui_app->h_new_value);
253         }
254         }
255         break;
256         
257         case GDK_Right: //right
258         {
259         gtk_container_set_focus_hadjustment(GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left), gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(ws_gui_app->ws_gui_scrolledwindow_left)));
260         ws_gui_app->h_new_value = gtk_adjustment_get_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_hadj)) + ws_gui_app->h_delta;
261         if (ws_gui_app->h_new_value < (ws_gui_app->ws_gui_hadj->upper - ws_gui_app->ws_gui_hadj->page_size)) 
262         {
263         gtk_adjustment_set_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_hadj), ws_gui_app->h_new_value);
264         }
265         }
266         break;
267         
268         case GDK_Return: /*start search*/
269         //g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, ".::GUI::. Return");
270         ws_gui_search(NULL, ws_gui_app);
271         break;
272         
273         case GDK_F6: //FULL SCREEN
274         ws_gui_full_screen(NULL, ws_gui_app);
275         break;
276         
277         case GDK_F7: //zoom in, f7
278         ws_gui_html_zoom_in(NULL, ws_gui_app);
279         break;
280         
281         case GDK_F8://zoom out, f8
282         ws_gui_html_zoom_out(NULL, ws_gui_app);
283         break;
284         
285         case GDK_Escape: //escape
286         ws_gui_search_stop(NULL, ws_gui_app);
287         break;
288
289         case GDK_BackSpace: //backspace
290         return FALSE;
291         break;
292
293         default:
294         return FALSE;
295         break;
296     }
297         return TRUE;
298 }
299
300 /** this function allow to hide words list using menu item from application menu
301  *
302  * @param checkmenuitem - the object which received the signal
303  * @param user_data - user data set when the signal handler was connected
304  * @return
305  */
306 void ws_gui_words_list_hide_from_menu(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
307 {
308         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
309         if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list)))
310         {
311                 gtk_widget_hide(ws_gui_app->ws_gui_scrolledwindow_left);
312                 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(ws_gui_app->ws_gui_toobar_button_hide), TRUE);
313                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list), TRUE);
314         }
315         else 
316         {
317                 gtk_widget_show(ws_gui_app->ws_gui_scrolledwindow_left);
318                 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(ws_gui_app->ws_gui_toobar_button_hide), FALSE);
319                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list), FALSE);
320         }
321 }
322
323 /** this function allow to hide words list using toggle button placed in the find toolbar
324  *
325  * @param toolbar - the object which received the signal
326  * @param user_data - user data set when the signal handler was connected
327  * @return
328  */
329 void ws_gui_words_list_hide(GtkToggleButton *togglebutton, gpointer user_data)
330 {
331         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
332         if (gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(ws_gui_app->ws_gui_toobar_button_hide)))
333         {
334                 gtk_widget_hide(ws_gui_app->ws_gui_scrolledwindow_left);
335                 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(ws_gui_app->ws_gui_toobar_button_hide), TRUE);
336                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list), TRUE);
337         }
338         else 
339         {
340                 gtk_widget_show(ws_gui_app->ws_gui_scrolledwindow_left);
341                 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(ws_gui_app->ws_gui_toobar_button_hide), FALSE);
342                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list), FALSE);
343         }
344 }
345
346 /** add word to the history
347  *
348  * @param new_word - word which is going to be append to the history array
349  * @param user_data - user data set when the signal handler was connected
350  * @return
351  */
352 void ws_gui_history_add(char *new_word, gpointer user_data)
353 {
354         guint i;
355         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
356         gchar *tmp_word;
357         
358         i = ws_gui_app->ws_gui_history_cur_pos + 1;
359         gchar *tmp = g_array_index(ws_gui_app->ws_gui_history, gchar*, i);
360         
361         while (tmp != NULL) 
362         {
363                 g_array_remove_index(ws_gui_app->ws_gui_history, i);
364                 tmp = g_array_index(ws_gui_app->ws_gui_history, gchar*, i);
365         }
366
367         i = 0;
368         ws_gui_app->ws_gui_history_cur_pos ++;
369         
370         tmp_word = g_strdup(new_word);
371         g_array_append_val(ws_gui_app->ws_gui_history, tmp_word);
372         if(ws_gui_app->ws_gui_history->len > HISTORY_LEN)
373         {
374                 g_array_remove_index(ws_gui_app->ws_gui_history, 0);
375                 ws_gui_app->ws_gui_history_cur_pos--;
376         }
377         
378         i = 0;
379         tmp = g_array_index(ws_gui_app->ws_gui_history, gchar*, i);
380
381         ws_gui_check_history(ws_gui_app);       
382 }
383
384 /** display previously choosen word (previous from the history array) if avaible, sets current position in the history array
385  *
386  * @param button - button which recived a signal
387  * @param user_data - user data set when the signal handler was connected
388  * @return 
389  */
390 void ws_gui_history_back(GtkButton *button, gpointer user_data) 
391 {
392         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
393         
394         if (ws_gui_app->ws_gui_history_cur_pos > 0) {
395                         ws_gui_app->ws_gui_history_cur_pos = ws_gui_app->ws_gui_history_cur_pos - 1;
396                         ws_dbus_client_find_translation(ws_gui_app->dbus_data, (g_array_index(ws_gui_app->ws_gui_history, gchar*, ws_gui_app->ws_gui_history_cur_pos)));
397         }
398         else 
399         {
400         gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window), "no words in the history" );
401         gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_back), FALSE);
402         gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_prev), FALSE);
403         }
404
405         ws_gui_check_history(ws_gui_app);
406         
407 }
408
409 /** display choosen word, next in the history array (if avaible), sets current position in the history array
410  *
411  * @param button - button which recived a signal
412  * @param user_data - user data set when the signal handler was connected
413  * @return 
414  */
415 void ws_gui_history_next(GtkButton *button, gpointer user_data) 
416 {
417         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
418         
419         gchar *tmp = g_array_index(ws_gui_app->ws_gui_history, gchar*, ws_gui_app->ws_gui_history_cur_pos+1);
420         
421         if ((ws_gui_app->ws_gui_history_cur_pos < HISTORY_LEN-1) && (tmp != NULL)) 
422         {
423                 ws_gui_app->ws_gui_history_cur_pos = ws_gui_app->ws_gui_history_cur_pos + 1;
424                 ws_dbus_client_find_translation(ws_gui_app->dbus_data, (g_array_index(ws_gui_app->ws_gui_history, gchar*, ws_gui_app->ws_gui_history_cur_pos)));
425         }
426         else 
427         {
428         gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window), "end of history" );
429         gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_forward), FALSE);
430         gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_next), FALSE);
431         }
432
433         ws_gui_check_history(ws_gui_app);
434 }
435
436 /** check current position in the history array and sets sensitivity of buttons / menu items, depends on availablity of words in the history
437  *
438  * @param user_data - user data set when the signal handler was connected
439  * @return 
440  */
441 void ws_gui_check_history(gpointer user_data) 
442 {
443         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
444         
445         gchar *tmp = g_array_index(ws_gui_app->ws_gui_history, gchar*, ws_gui_app->ws_gui_history_cur_pos+1);
446         
447         if ((ws_gui_app->ws_gui_history_cur_pos+1 < HISTORY_LEN) && (tmp != NULL))
448         {       
449                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_forward), TRUE);
450                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_next), TRUE);
451         }
452         else 
453         {
454                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_forward), FALSE);
455                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_next), FALSE);
456         }
457         
458         tmp = g_array_index(ws_gui_app->ws_gui_history, gchar*, ws_gui_app->ws_gui_history_cur_pos-1);
459         if ((ws_gui_app->ws_gui_history_cur_pos > 0) && (tmp != NULL))
460         {
461                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_back), TRUE);
462                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_prev), TRUE);
463         }
464         else
465         {
466                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_back), FALSE);
467                 gtk_widget_set_sensitive (GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_prev), FALSE);
468         }
469 }
470
471 /** create TreeView Model, which allows to display words list
472  *
473  * @param words_list - array with words(found in a dictionary), recived from DBUS;
474  * @param user_data - user data set when the signal handler was connected
475  * @return 
476  */
477 GtkTreeModel * create_and_fill_model (GArray *words_list, gpointer user_data)
478 {
479         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
480         guint i = 0;
481         gchar *tmp = g_strdup(g_array_index(words_list, gchar*, i));
482         gchar *tmp2;
483         gboolean valid;
484         valid =gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ws_gui_app->ws_gui_w_list->ws_gui_store), &ws_gui_app->ws_gui_w_list->ws_gui_iter);
485         
486          /* Append a row and fill in some data */
487         while (tmp != NULL)
488         {
489         //tmp2 = g_utf8_normalize(tmp, strlen(tmp), G_NORMALIZE_ALL);
490         gtk_list_store_append (ws_gui_app->ws_gui_w_list->ws_gui_store, &ws_gui_app->ws_gui_w_list->ws_gui_iter);
491         gtk_list_store_set (ws_gui_app->ws_gui_w_list->ws_gui_store,                                                               &ws_gui_app->ws_gui_w_list->ws_gui_iter,                                                                COL_WORD,                                                                                               tmp,                                                                                                    -1);
492         i=i+1;
493         tmp = g_strdup(g_array_index(words_list, gchar*, i));
494         };
495
496         tmp = g_strdup(g_array_index(words_list, gchar*, 0));
497         if (tmp != NULL)
498         {
499                 ws_dbus_client_find_translation(ws_gui_app->dbus_data, tmp);
500                 ws_gui_history_add(tmp, ws_gui_app);
501         }
502         
503         return GTK_TREE_MODEL(ws_gui_app->ws_gui_w_list->ws_gui_store);
504 }
505
506 /** create TreeView and TreeModel using create_and_fill_model() function; it is necessary to display found words in a words list;
507  *
508  * @param words_list - array with words(found in a dictionary), recived from DBUS;
509  * @param user_data - user data set when the signal handler was connected
510  * @return 
511  */
512 GtkWidget * create_view_and_model (GArray *words_list, gpointer user_data)
513 {
514         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
515         
516         ws_gui_app->ws_gui_w_list->ws_gui_view = gtk_tree_view_new ();
517
518         ws_gui_app->ws_gui_w_list->ws_gui_renderer = gtk_cell_renderer_text_new ();
519         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (ws_gui_app->ws_gui_w_list->ws_gui_view),                                                 -1,                                                                                                     "Name",                                                                                                 ws_gui_app->ws_gui_w_list->ws_gui_renderer,                                                             "text",                                                                                                 COL_WORD,                                                                                               NULL);
520         
521         ws_gui_app->ws_gui_w_list->ws_gui_model = create_and_fill_model (words_list, ws_gui_app);
522         
523         gtk_tree_view_set_model (GTK_TREE_VIEW (ws_gui_app->ws_gui_w_list->ws_gui_view), ws_gui_app->ws_gui_w_list->ws_gui_model);
524         
525         g_object_unref (ws_gui_app->ws_gui_w_list->ws_gui_model);
526         return ws_gui_app->ws_gui_w_list->ws_gui_view;
527 }
528
529 /** handle signal from words list, get selected word and calls 'find translation' function
530  *
531  * @param selection - the object which received the signal
532  * @param user_data - user data set when the signal handler was connected
533  * @return 
534  */
535 void ws_gui_tree_selection_changed(GtkTreeSelection *selection, gpointer user_data)
536 {
537 WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
538 gchar *string;
539
540         if (gtk_tree_selection_get_selected (selection, &ws_gui_app->ws_gui_w_list->ws_gui_model,                                                               &ws_gui_app->ws_gui_w_list->ws_gui_iter))
541                 {
542                         gtk_tree_model_get (ws_gui_app->ws_gui_w_list->ws_gui_model, &ws_gui_app->ws_gui_w_list->ws_gui_iter, COL_WORD, &string, -1);
543         
544                         ws_dbus_client_find_translation(ws_gui_app->dbus_data, string); 
545                 
546                         ws_gui_history_add(string, ws_gui_app);
547         
548                         g_free (string);
549                 }
550
551 }
552
553
554 /** switch application between fun screen and normal mode
555  *
556  * @param menuitem - object which recived the signal
557  * @param user_data - user data set when the signal handler was connected 
558  * @return
559  */
560 void ws_gui_full_screen(GtkMenuItem *menuitem, gpointer user_data)
561 {
562
563         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
564         if (ws_gui_app->ws_gui_full_screen_flag == FALSE) {
565         
566         gtk_window_fullscreen(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window));
567         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ws_gui_app->ws_gui_menu->ws_gui_menu_full_screen), TRUE);
568         ws_gui_app->ws_gui_full_screen_flag = TRUE;
569         }
570         else 
571         {
572         gtk_window_unfullscreen(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window));
573         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ws_gui_app->ws_gui_menu->ws_gui_menu_full_screen), FALSE);
574         ws_gui_app->ws_gui_full_screen_flag = FALSE;
575         }
576 }
577
578 /** select whole text in the translation (html) area
579  *
580  * @param menuitem - object which recived the signal
581  * @param user_data - user data set when the signal handler was connected 
582  * @return
583  */
584 void ws_gui_html_select_all(GtkMenuItem *menuitem, gpointer user_data) 
585 {
586         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
587         gtk_html_select_all(GTK_HTML(ws_gui_app->ws_gui_html));
588 }
589
590 /** copy selected text to the clipoard
591  *
592  * @param menuitem - object which recived the signal
593  * @param user_data - user data set when the signal handler was connected 
594  * @return
595  */
596 void ws_gui_html_copy(GtkMenuItem *menuitem, gpointer user_data) 
597 {
598         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
599         gtk_html_copy(GTK_HTML(ws_gui_app->ws_gui_html));
600         
601 }
602
603 /** zoom in text in translation (html) area
604  *
605  * @param menuitem - object which recived the signal
606  * @param user_data - user data set when the signal handler was connected 
607  * @return
608  */
609 void ws_gui_html_zoom_in(GtkMenuItem *menuitem, gpointer user_data)
610 {
611         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
612         gtk_html_zoom_in(GTK_HTML(ws_gui_app->ws_gui_html));
613 }
614
615 /** zoom out text in translation (html) area
616  *
617  * @param menuitem - object which recived the signal
618  * @param user_data - user data set when the signal handler was connected 
619  * @return
620  */
621 void ws_gui_html_zoom_out(GtkMenuItem *menuitem, gpointer user_data)
622 {
623         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
624         gtk_html_zoom_out(GTK_HTML(ws_gui_app->ws_gui_html));
625 }
626
627 /** start searching, send typed word to DBUS and query for words
628  *
629  * @param widget - object which recived the signal
630  * @param user_data - user data set when the signal handler was connected 
631  * @return
632  */
633 void ws_gui_search(GtkWidget * widget, gpointer user_data) 
634 {
635         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
636         gchar* ws_gui_text = NULL;
637         gchar *empty = " ";
638         
639         if (ws_gui_app->ws_gui_banner_flag == TRUE) {
640         ws_gui_app->ws_gui_banner_flag = FALSE;
641         }
642         
643         g_object_get(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar), "prefix", &ws_gui_text, NULL);
644         
645         if (strlen(ws_gui_text) != 0) 
646         {
647                 ws_gui_app->ws_gui_banner = hildon_banner_show_animation(GTK_WIDGET(ws_gui_app->ws_gui_hildon_window), NULL, "searching ..." );
648
649                 gtk_widget_set_sensitive(GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_stop),TRUE);
650                 gtk_widget_set_sensitive(GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_stop), TRUE);
651
652                 ws_gui_app->ws_gui_banner_flag = TRUE;
653                 ws_gui_fill_html(empty, ws_gui_app);
654                 ws_dbus_client_find_word (ws_gui_app->dbus_data, ws_gui_text);
655         }
656         
657         else gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window), "No word typed.");
658                         
659         
660 }
661
662 /** stop search process
663  *
664  * @param button - object which recived the signal
665  * @param user_data - user data set when the signal handler was connected 
666  * @return
667  */
668 void ws_gui_search_stop(GtkButton *button, gpointer user_data) 
669 {
670         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;   
671         if (ws_gui_app->ws_gui_banner_flag == TRUE) 
672         {
673         gtk_widget_destroy(GTK_WIDGET(ws_gui_app->ws_gui_banner));
674         ws_gui_app->ws_gui_banner_flag = FALSE;
675         ws_dbus_notify(ws_gui_app->dbus_data, WS_DBUS_INFO_STOP_SEARCH);
676         gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window), "Search has been aborted" );
677         }
678         
679 }
680
681 /** this function is called just before closing application; it sends signal to DBUS and destroys it; 
682  *
683  * @param widget - object which recived the signal
684  * @param event - 
685  * @param user_data - user data set when the signal handler was connected 
686  * @return
687  */
688 void ws_gui_on_exit (GtkWidget *widget, GdkEvent *event, gpointer user_data) 
689 {
690         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;   
691         ws_dbus_notify(ws_gui_app->dbus_data, WS_DBUS_INFO_TERMINATE);
692         g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "\nClosing WhiteStork Dictionary ... \n\n");
693         ws_dbus_destroy (ws_gui_app->dbus_data);
694         gtk_main_quit();
695         exit (0);
696 }
697
698 /** this function is called just before closing application, from application menu; it sends signal to DBUS and destroys it;
699  *
700  * @param menuitem - object which recived the signal
701  * @param user_data - user data set when the signal handler was connected 
702  * @return
703  */
704 void ws_gui_menu_quit(GtkMenuItem *menuitem, gpointer user_data)
705 {
706         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;   
707         ws_dbus_notify(ws_gui_app->dbus_data, WS_DBUS_INFO_TERMINATE);
708         g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "\nClosing WhiteStork Dictionary ... \n\n");
709         ws_dbus_destroy (ws_gui_app->dbus_data);
710         gtk_main_quit();
711         exit (0);
712 }
713
714 /** fill translation area with text (html) recived from DBUS
715  *
716  * @param html_context - text which is going to be displayed; it should be html;
717  * @param user_data - user data set when the function was called
718  * @return
719  */
720 void ws_gui_fill_html(char *html_context, gpointer user_data) 
721 {
722         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
723         gtk_html_set_editable (GTK_HTML(ws_gui_app->ws_gui_html), FALSE);
724         gtk_html_load_from_string (GTK_HTML(ws_gui_app->ws_gui_html), html_context, -1);
725         gtk_html_set_editable (GTK_HTML(ws_gui_app->ws_gui_html), TRUE);
726         
727 }
728
729 /** read adjustment of left scrollwindow, which is necessary to navigate with arrow keys inside words list
730  *
731  * @param user_data - user data set when the signal handler was connected 
732  * @return
733  */
734 void ws_gui_read_adjustment(gpointer user_data)
735 {
736         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
737         
738         ws_gui_app->ws_gui_hadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(ws_gui_app->ws_gui_scrolledwindow_left));
739
740         ws_gui_app->h_delta = (ws_gui_app->ws_gui_hadj->upper - ws_gui_app->ws_gui_hadj->lower)/SCROLL_STEP_H;
741         
742         ws_gui_app->ws_gui_vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ws_gui_app->ws_gui_scrolledwindow_left));
743
744         ws_gui_app->v_delta = (ws_gui_app->ws_gui_vadj->upper - ws_gui_app->ws_gui_vadj->lower)/SCROLL_STEP_V;
745         ws_gui_app->v_new_value = gtk_adjustment_get_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_vadj)) + ws_gui_app->v_delta;
746                 
747         gtk_container_set_focus_vadjustment(GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left), gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ws_gui_app->ws_gui_scrolledwindow_left)));
748         
749 }
750
751 void ws_gui_url_requested (GtkHTML *html, const char *url, GtkHTMLStream *stream)
752 {
753         int fd;
754
755         if (url && !strncmp (url, "file:", 5)) {
756                 url += 5;
757                 fd = open (url, O_RDONLY);
758
759                 if (fd != -1) {
760                         gchar *buf;
761                         size_t size;
762
763                         buf = alloca (8192);
764                         while ((size = read (fd, buf, 8192)) > 0) {
765                                 gtk_html_stream_write (stream, buf, size);
766                         }
767                         gtk_html_stream_close (stream,
768                                                size == -1
769                                                ? GTK_HTML_STREAM_ERROR
770                                                : GTK_HTML_STREAM_OK);
771                         close (fd);
772
773                         return;
774                 }
775         }
776
777         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
778 }
779