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