menu bookmarks dimmed when caching
[mdictionary] / src / 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 #include <ws_gui_callbacks.h>
21 #include <ws_gui.h>
22 #include <ws_gui_layout.h>
23 #include <ws_dbus.h>
24
25 /** \brief show how much time did take a callback of another function
26  *
27  */
28 static double timer(gboolean start, gchar* message)
29 {
30         static GArray* stack = NULL;
31         static gboolean first_run = TRUE;
32         static struct timeval actual_time;
33         static struct timeval last_time;
34         static struct timeval result;
35         static double seconds = 0.0;
36         if(first_run)
37         {
38                 first_run = FALSE;
39                 stack = g_array_new(TRUE, TRUE, sizeof(struct timeval));
40         };
41         // things to do on the beggining of function's work
42         if (start)
43         {
44                 g_debug("XDXF->%s() start counting time for function '%s()'.\n",
45                         __FUNCTION__,message);
46                 g_array_prepend_val(stack, actual_time);
47                 gettimeofday(&g_array_index(stack, struct timeval, 0),NULL);
48                 return -1.0;
49         }
50         // we just want to end some timer - print some information 
51         //about working time;
52         else {
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 - 
59                                              actual_time.tv_usec) / 1000000 + 1;
60                         last_time.tv_usec -= 1000000 * nsec;
61                         last_time.tv_sec += nsec;
62                 }
63                 if (actual_time.tv_usec - last_time.tv_usec > 1000000) {
64                         int nsec = (last_time.tv_usec -
65                                                  actual_time.tv_usec) / 1000000;
66                         last_time.tv_usec += 1000000 * nsec;
67                         last_time.tv_sec -= nsec;
68                 }
69                 result.tv_sec = actual_time.tv_sec - last_time.tv_sec;
70                 result.tv_usec = actual_time.tv_usec - last_time.tv_usec;
71                 seconds = (((double)(result.tv_usec)) / 1e6) + 
72                                                       ((double)(result.tv_sec));
73
74                 g_debug("XDXF->%s() function \'%s()\' was working for: %g [s] "
75                         "or %ld [us].\n",
76                         __FUNCTION__,
77                         message,seconds,
78                         ((long)(result.tv_sec*1e6)+(result.tv_usec)));
79                // stack is empty so we delete everything
80                if(stack->len == 0) 
81                {
82                         g_array_free(stack, TRUE);
83                         first_run = TRUE;
84                }
85                return seconds;
86         }
87         return -2.0;
88 }
89
90
91 /**  this function handles signals from dbus; it is called 
92 when there are any messages from other modules
93  *
94  * @param error - error message recived from DBUS
95  * @param words - array with recived data structure
96  * @param user_data - pointer to data structure
97  * @return
98  */
99 void ws_gui_signal_hander (GError *error, GArray *words, gpointer user_data)
100 {
101         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
102         osso_rpc_t osss_data;
103         osss_data = g_array_index (words, osso_rpc_t, 0);
104         switch(osss_data.value.i)
105         {
106                 case WS_DBUS_ERROR_ENGINE_NOT_FOUND:
107                 {
108                         ws_gui_app->ws_message_dialog = 
109                         gtk_message_dialog_new(
110                                    GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
111                                    GTK_DIALOG_DESTROY_WITH_PARENT,
112                                    GTK_MESSAGE_ERROR,
113                                    GTK_BUTTONS_OK,
114                                    _("ws_ni_error_occured"));
115                         gtk_widget_show_all(ws_gui_app->ws_message_dialog);
116
117                         g_signal_connect_swapped(
118                                      GTK_OBJECT (ws_gui_app->ws_message_dialog),
119                                      "response",
120                                      G_CALLBACK (gtk_main_quit),
121                                      ws_gui_app);
122                         break;
123                 }
124
125                 case WS_DBUS_ERROR_FILE_NOT_FOUND:
126                 {
127                         ws_gui_app->ws_message_dialog = 
128                                 gtk_message_dialog_new(
129                                    GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
130                                    GTK_DIALOG_DESTROY_WITH_PARENT,
131                                    GTK_MESSAGE_ERROR,
132                                    GTK_BUTTONS_OK,
133                                    _("ws_ni_no_dictionary_available"));
134
135                         gtk_widget_show_all(ws_gui_app->ws_message_dialog);
136
137                         GArray *tmp;
138                         tmp = g_array_new(TRUE, TRUE, sizeof(gchar*));
139                         gtk_list_store_clear(
140                                        ws_gui_app->ws_gui_w_list->ws_gui_store);
141                         ws_gui_app->ws_gui_w_list->ws_gui_model = 
142                                 create_and_fill_model(tmp, ws_gui_app);
143                         ws_gui_fill_html(" ", ws_gui_app);
144                         ws_gui_app->html_flag = FALSE;
145
146                         gtk_widget_set_sensitive(
147                              GTK_WIDGET(ws_gui_app->ws_gui_hildon_find_toolbar),
148                              FALSE);
149                         gtk_widget_set_sensitive(
150                         GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_search),
151                         FALSE);
152
153                         if (gtk_dialog_run(
154                                 GTK_DIALOG(ws_gui_app->ws_message_dialog)) 
155                                                              == GTK_RESPONSE_OK)
156                         {
157                                 gtk_widget_destroy(
158                                                  ws_gui_app->ws_message_dialog);
159                         }
160                         break;
161                 }
162
163                 case WS_DBUS_INFO_CACHING:
164                 {
165                         ws_gui_app->ws_gui_banner_caching =
166                                 hildon_banner_show_progress(
167                                    GTK_WIDGET(ws_gui_app->ws_gui_hildon_window),
168                                    NULL,
169                                    _("ws_pb_caching"));
170                        ws_gui_app->caching_flag = TRUE;
171                        hildon_banner_set_fraction(
172                                HILDON_BANNER(ws_gui_app->ws_gui_banner_caching),
173                                0.0);
174                        gtk_widget_set_sensitive(
175                              GTK_WIDGET(ws_gui_app->ws_gui_hildon_find_toolbar),
176                              FALSE);
177                        gtk_widget_set_sensitive(
178                         GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_search),
179                         FALSE);
180                          gtk_widget_set_sensitive(
181                         GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_bookmarks),
182                         FALSE);
183                        gtk_widget_set_sensitive(
184                        GTK_WIDGET(ws_gui_app->ws_gui_w_list->ws_gui_view),
185                        FALSE);
186
187                        break;
188                 }
189
190                 case WS_DBUS_INFO_CACHING_FINISHED:
191                 {
192                        gtk_widget_destroy(
193                                  GTK_WIDGET(ws_gui_app->ws_gui_banner_caching));
194                        ws_gui_app->caching_flag = FALSE;
195                        gtk_widget_set_sensitive(
196                              GTK_WIDGET(ws_gui_app->ws_gui_hildon_find_toolbar),
197                              TRUE);
198                        gtk_widget_set_sensitive(
199                        GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_search),
200                        TRUE);
201                         gtk_widget_set_sensitive(
202                         GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_bookmarks),
203                         TRUE);
204                         gtk_widget_set_sensitive(
205                         GTK_WIDGET(ws_gui_app->ws_gui_w_list->ws_gui_view),
206                         TRUE);
207
208                         break;
209                 }
210
211                 case WS_DBUS_ERROR_DICTIONARY_NOT_LOAD:
212                 {
213                         if (ws_gui_app->ws_gui_history_cur_pos >= 0 &&
214                                ws_gui_app->ws_gui_history_cur_pos <=HISTORY_LEN)
215                         { 
216                                 g_array_remove_index(ws_gui_app->ws_gui_history,
217                                             ws_gui_app->ws_gui_history_cur_pos);
218                         }
219
220                         ws_gui_app->ws_message_dialog = gtk_message_dialog_new(
221                                    GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
222                                    GTK_DIALOG_DESTROY_WITH_PARENT,
223                                    GTK_MESSAGE_ERROR,
224                                    GTK_BUTTONS_OK,
225                                   _("ws_ni_dictionary_unavailable"));
226                        gtk_widget_show_all(ws_gui_app->ws_message_dialog);
227                        if (gtk_dialog_run(
228                        GTK_DIALOG(ws_gui_app->ws_message_dialog)) 
229                                                              == GTK_RESPONSE_OK)
230                        {
231                               gtk_widget_destroy(ws_gui_app->ws_message_dialog);
232                        }
233                        break;
234                 }
235                 
236                 case  WS_DBUS_BOOKMARKS_ADDED_OK:
237                 {
238                         gtk_infoprint(GTK_WINDOW(
239                                         ws_gui_app->ws_gui_hildon_window),
240                                         _("ws_ni_bookmark_added"));
241                 break;
242                 }
243
244                 case WS_DBUS_BOOKMARKS_REMOVED_OK:
245                 {
246                         gtk_infoprint(GTK_WINDOW(
247                                         ws_gui_app->ws_gui_hildon_window),
248                                         _("ws_ni_bookmark_removed"));
249                 break;
250                 }
251         
252                 case WS_DBUS_BOOKMARKS_ADDED_FAIL:
253                 {
254                         gtk_infoprint(GTK_WINDOW(
255                                         ws_gui_app->ws_gui_hildon_window),
256                                         _("ws_ni_bookmark_not_added"));
257                 break;
258                 }
259
260                 case WS_DBUS_BOOKMARKS_REMOVED_FAIL:
261                 {
262                         gtk_infoprint(GTK_WINDOW(
263                                         ws_gui_app->ws_gui_hildon_window),
264                                         _("ws_ni_bookmark_not_removed"));
265                 break;
266                 }
267
268                 case WS_DBUS_LOAD_BOOKMARK_FAILED:
269                 {
270                      gtk_widget_set_sensitive(
271                      GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_bookmarks),
272                      FALSE);
273                      ws_gui_app->bookmark_avail = FALSE;
274                                 
275                         ws_gui_app->ws_message_dialog = 
276                         gtk_message_dialog_new(
277                                    GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
278                                    GTK_DIALOG_DESTROY_WITH_PARENT,
279                                    GTK_MESSAGE_ERROR,
280                                    GTK_BUTTONS_OK,
281                                    _("ws_ni_bookmarks_unavailable"));
282
283                         if (gtk_dialog_run(
284                                 GTK_DIALOG(ws_gui_app->ws_message_dialog)) 
285                                                              == GTK_RESPONSE_OK)
286                         {
287                                 gtk_widget_destroy(
288                                                  ws_gui_app->ws_message_dialog);
289                         }
290
291                         
292                         break;  
293                 }
294                 case WS_DBUS_EXTRACT_FILE:
295                 {
296                         ws_gui_app->ws_gui_banner_extracting =
297                                hildon_banner_show_animation(
298                                    GTK_WIDGET(ws_gui_app->ws_gui_hildon_window),
299                                    NULL,
300                                    _("ws_pb_extracting"));
301
302                         ws_gui_app->caching_flag = TRUE;
303                         
304                         gtk_widget_set_sensitive(
305                            GTK_WIDGET(ws_gui_app->ws_gui_hildon_find_toolbar),
306                            FALSE);
307                         gtk_widget_set_sensitive(
308                         GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_search),
309                         FALSE);
310
311                        break;
312                 }
313
314                 case WS_DBUS_EXTRACT_FILE_FINISHED:
315                 {
316                         gtk_widget_destroy(
317                                         GTK_WIDGET(ws_gui_app->ws_gui_banner_extracting));
318                         ws_gui_app->caching_flag = FALSE;
319                         
320                         gtk_infoprint(GTK_WINDOW(
321                                       ws_gui_app->ws_gui_hildon_window),
322                                       _("ws_ni_dictionary_added"));
323                         
324                         gtk_widget_set_sensitive(
325                                 GTK_WIDGET(ws_gui_app->ws_gui_hildon_find_toolbar),
326                                 TRUE);
327                         gtk_widget_set_sensitive(
328                         GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_search),
329                         TRUE);
330
331                         break;
332                 }
333
334         }
335 }
336 /**  this function handles signals from dbus; it is called when progress bar
337  status has been changed
338  *
339  * @param error - error message recived from DBUS
340  * @param words - array with recived data structure
341  * @param user_data - pointer to data structure
342  * @return
343  */
344 void ws_dbus_progress_bar(GError *error, GArray *words, gpointer user_data)
345 {
346         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
347         osso_rpc_t osss_data;
348         osss_data = g_array_index (words, osso_rpc_t, 0);
349         double progress = osss_data.value.d;
350         if (ws_gui_app->caching_flag == TRUE)
351         {
352                 hildon_banner_set_fraction(
353                                HILDON_BANNER(ws_gui_app->ws_gui_banner_caching),
354                                progress);
355         }
356 }
357
358 /** this function clean GtkListStore row by row
359  *
360  * @param list - GtkListStore to be remoeved
361  * @return
362  */
363 void ws_gui_clear_list (GtkListStore* list) 
364 {
365         GtkTreeIter iter;
366         gboolean tmp;
367         
368         tmp = gtk_tree_model_get_iter_first(
369                         GTK_TREE_MODEL(list),
370                         &iter);
371         
372         while (tmp == TRUE)
373         {
374                 tmp = gtk_list_store_remove(list, &iter);
375                 
376         }
377 }
378
379
380 /** this function handles signal from dbus and transfer recived 
381 (found in a dictionary) words to the words list
382  *
383  * @param error - error message recived from DBUS
384  * @param words - array with recived data structure
385  * @param user_data - pointer to data structure
386  * @return
387  */
388 void ws_gui_dbus_return_words (GError *error, GArray *words, gpointer user_data)
389 {
390         timer(TIMER_START, (gchar*)__FUNCTION__);
391         guint i;
392         osso_rpc_t data;
393
394         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
395
396         g_signal_handlers_block_by_func(G_OBJECT (ws_gui_app->ws_gui_w_list->ws_gui_view),
397                                         G_CALLBACK (ws_gui_view_cursor_changed), 
398                                         ws_gui_app);    
399
400
401         GArray *tmp;
402         tmp = g_array_new(TRUE, TRUE, sizeof(gchar*));
403         gchar *tmp_word;
404
405         for (i=0;i<words->len;++i)
406         {
407                 data = g_array_index (words, osso_rpc_t, i);
408                 tmp_word = g_strconcat(data.value.s,NULL);
409                 g_array_append_val(tmp, tmp_word);
410         }
411         
412         g_assert(ws_gui_app->ws_gui_banner);
413         gtk_widget_hide(GTK_WIDGET(ws_gui_app->ws_gui_banner));
414
415         ws_gui_app->ws_gui_banner_flag = FALSE;
416         gtk_widget_set_sensitive(
417                               GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_stop),
418                               FALSE);
419         gtk_widget_set_sensitive(
420                           GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_stop),
421                           FALSE);
422
423         ws_gui_clear_list(ws_gui_app->ws_gui_w_list->ws_gui_store);
424
425
426         ws_gui_app->ws_gui_w_list->ws_gui_model = 
427                                         create_and_fill_model(tmp, ws_gui_app);
428         
429         if (ws_gui_app->history_flag == TRUE)
430         {
431
432         GValue value = { 0, };
433         GtkTreeIter tmp_iter;
434         gchar *pattern;
435         gboolean keep_searching = TRUE;
436
437         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(
438             ws_gui_app->ws_gui_w_list->ws_gui_model), &tmp_iter) == TRUE)
439         {
440
441                 pattern = strdup(g_array_index(ws_gui_app->ws_gui_history,
442                                         gchar*, 
443                                         ws_gui_app->ws_gui_history_cur_pos));
444                 
445                 gtk_tree_model_get_value(GTK_TREE_MODEL(
446                                 ws_gui_app->ws_gui_w_list->ws_gui_model), 
447                                 &tmp_iter, 
448                                 COL_WORD, 
449                                 &value);
450
451                 if (strcmp(((gchar *)g_value_get_string (&value)), pattern) == 0)
452                 {
453                         gtk_tree_selection_select_iter(
454                                 ws_gui_app->ws_gui_w_list->ws_gui_selection,
455                                 &tmp_iter);
456
457                 }
458                 else
459                 {
460                 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(ws_gui_app->ws_gui_w_list->ws_gui_model), &tmp_iter) == TRUE && keep_searching == TRUE)
461                 {
462                         gtk_tree_model_get_value(GTK_TREE_MODEL(
463                                 ws_gui_app->ws_gui_w_list->ws_gui_model), 
464                                 &tmp_iter, 
465                                 COL_WORD, 
466                                 &value);
467
468                         if (strcmp((gchar *)g_value_get_string (&value), pattern) == 0)
469                                 {
470                                 gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL(
471                                 ws_gui_app->ws_gui_w_list->ws_gui_model),
472                                         &tmp_iter,
473                                         g_array_index(
474                                         ws_gui_app->ws_gui_history_iter,
475                                         gchar*, 
476                                         ws_gui_app->ws_gui_history_cur_pos));
477
478                                 gtk_tree_selection_select_iter(
479                                                 ws_gui_app->ws_gui_w_list->ws_gui_selection,
480                                                 &tmp_iter);
481                                                                 
482                                 keep_searching = FALSE;
483                                 }
484                         }
485                 }
486         }
487         g_free(pattern);
488
489         ws_gui_app->history_flag = FALSE;
490         ws_dbus_client_find_translation(ws_gui_app->dbus_data, 
491                                 g_array_index(ws_gui_app->ws_gui_history,
492                                 gchar*, 
493                                 ws_gui_app->ws_gui_history_cur_pos));
494         
495         ws_gui_app->html_flag = TRUE;
496         }
497         
498         
499         if (tmp->len == 0)
500         {
501                 gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
502                               _("ws_ni_no_words_found"));
503                 
504                 ws_gui_fill_html(" ", ws_gui_app);
505                 ws_gui_app->html_flag = FALSE;
506                 g_free(ws_gui_app->last_word);
507                 ws_gui_app->last_word = NULL;
508         }
509
510         g_signal_handlers_unblock_by_func(G_OBJECT(
511                                         ws_gui_app->ws_gui_w_list->ws_gui_view),
512                                         G_CALLBACK (ws_gui_view_cursor_changed), 
513                                         ws_gui_app);    
514
515         
516         timer(TIMER_STOP, (gchar*)__FUNCTION__);
517 }
518
519 /** this function handles signal from dbus and send recived data to 
520 the translation area
521  *
522  * @param error - error message recived from DBUS
523  * @param words - array with recived data structure
524  * @param user_data - pointer to data structure
525  * @return
526  */
527 void ws_gui_dbus_return_translation (GError *error,
528                                      GArray *words,
529                                      gpointer user_data)
530 {
531         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
532
533         osso_rpc_t data;
534
535         data = g_array_index (words, osso_rpc_t, 0);
536         ws_gui_fill_html(format_html(data.value.s, ws_gui_app), ws_gui_app);
537         ws_gui_app->html_flag = TRUE;
538
539 }
540
541 /** this function handles signal from dbus and send recived data to 
542 the translation area
543  *
544  * @param error - error message recived from DBUS
545  * @param words - array with recived data structure
546  * @param user_data - pointer to data structure
547  * @return
548  */
549 void ws_dbus_server_return_extracted_bzip(GError *error,
550                                      GArray *words,
551                                      gpointer user_data)
552 {
553         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
554
555         osso_rpc_t data;
556
557         data = g_array_index (words, osso_rpc_t, 0);
558
559         if (data.value.s[0] == '\0')
560         {
561                 gtk_infoprint(GTK_WINDOW(
562                                 ws_gui_app->ws_gui_hildon_window),
563                                 _("ws_ni_dictionary_not_added"));
564         }
565         else
566         {
567         if (ws_gui_load_dict(data.value.s, ws_gui_app) == TRUE)
568         {
569                 
570         }
571         }
572 }
573
574 /**
575 * this function allows to free allocated memory
576 *
577 * @param user_data - pointer to data structure
578 */
579 void ws_gui_free_memory(gpointer user_data)
580 {
581         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
582         g_array_free(ws_gui_app->ws_gui_history, TRUE);
583         pango_font_description_free(ws_gui_app->p);
584         g_free(ws_gui_app->last_word);
585         g_free(ws_gui_app->ws_gui_w_list);
586         g_free(ws_gui_app->ws_gui_menu);
587         g_free(ws_gui_app);
588 }
589
590 /** this function handle press signals (keyboard)
591  * 
592  * @param widget
593  * @param keyevent
594  * @param user_data - ponter to data structure
595  * @return TRUE to stop other handlers from being invoked for the event. 
596  FALSE to propagate the event further.
597  */
598 gboolean hildon_key_press_listener (GtkWidget * widget,
599                                     GdkEventKey * keyevent,
600                                     gpointer user_data)
601 {
602         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
603
604         switch ((guint)(keyevent->keyval)) {
605                 case HILDON_HARDKEY_UP: 
606                 {
607                         gtk_container_set_focus_vadjustment(
608                           GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left),
609                           gtk_scrolled_window_get_vadjustment(
610                           GTK_SCROLLED_WINDOW(
611                                       ws_gui_app->ws_gui_scrolledwindow_left)));
612                       ws_gui_app->v_new_value =
613                          gtk_adjustment_get_value(
614                            GTK_ADJUSTMENT(
615                                 ws_gui_app->ws_gui_vadj)) - ws_gui_app->v_delta;
616                       if (ws_gui_app->v_new_value > 
617                           ws_gui_app->ws_gui_vadj->lower) 
618                       {
619                               gtk_adjustment_set_value(
620                                         GTK_ADJUSTMENT(ws_gui_app->ws_gui_vadj),
621                                         ws_gui_app->v_new_value);
622                       }
623                         
624                         break;
625                 }
626
627                 case HILDON_HARDKEY_DOWN: 
628                 {
629                       gtk_container_set_focus_vadjustment(
630                           GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left),
631                           gtk_scrolled_window_get_vadjustment(
632                           GTK_SCROLLED_WINDOW(
633                                       ws_gui_app->ws_gui_scrolledwindow_left)));
634                       ws_gui_app->v_new_value = gtk_adjustment_get_value(
635                                         GTK_ADJUSTMENT(ws_gui_app->ws_gui_vadj))
636                                         + ws_gui_app->v_delta;
637
638                       if (ws_gui_app->v_new_value < 
639                                      (ws_gui_app->ws_gui_vadj->upper - 
640                                             ws_gui_app->ws_gui_vadj->page_size)) 
641                       {
642                                 gtk_adjustment_set_value(
643                                         GTK_ADJUSTMENT(ws_gui_app->ws_gui_vadj),
644                                         ws_gui_app->v_new_value);
645                       }
646                       break;
647                 }
648
649                 case HILDON_HARDKEY_LEFT:
650                 {
651                       gtk_container_set_focus_hadjustment(
652                       GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left),
653                       gtk_scrolled_window_get_hadjustment(
654                       GTK_SCROLLED_WINDOW(
655                                       ws_gui_app->ws_gui_scrolledwindow_left)));
656
657                       ws_gui_app->h_new_value = gtk_adjustment_get_value(
658                       GTK_ADJUSTMENT(ws_gui_app->ws_gui_hadj)) 
659                       - ws_gui_app->h_delta;
660
661                       if (ws_gui_app->h_new_value > 
662                           ws_gui_app->ws_gui_hadj->lower) 
663                       {
664                                 gtk_adjustment_set_value(
665                                         GTK_ADJUSTMENT(ws_gui_app->ws_gui_hadj),
666                                         ws_gui_app->h_new_value);
667                       }
668                 }
669                 break;
670
671                 case HILDON_HARDKEY_RIGHT: 
672                 {
673                       gtk_container_set_focus_hadjustment(
674                        GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left),
675                        gtk_scrolled_window_get_hadjustment(
676                        GTK_SCROLLED_WINDOW(
677                        ws_gui_app->ws_gui_scrolledwindow_left)));
678
679                       ws_gui_app->h_new_value = gtk_adjustment_get_value(
680                       GTK_ADJUSTMENT(ws_gui_app->ws_gui_hadj)) 
681                       + ws_gui_app->h_delta;
682
683                       if (ws_gui_app->h_new_value < 
684                                 (ws_gui_app->ws_gui_hadj->upper - 
685                                         ws_gui_app->ws_gui_hadj->page_size)) 
686                       {
687                       gtk_adjustment_set_value(
688                                         GTK_ADJUSTMENT(ws_gui_app->ws_gui_hadj),
689                                         ws_gui_app->h_new_value);
690                       }
691                  }
692                  break;
693
694                  case HILDON_HARDKEY_SELECT: 
695                         ws_gui_search(NULL, ws_gui_app);
696                  break;
697
698                  case HILDON_HARDKEY_FULLSCREEN: 
699                       ws_gui_full_screen(NULL, ws_gui_app);
700                  break;
701
702                  case HILDON_HARDKEY_INCREASE: 
703                       ws_gui_html_zoom_in(NULL, ws_gui_app);
704                  break;
705
706                  case HILDON_HARDKEY_DECREASE:
707                       ws_gui_html_zoom_out(NULL, ws_gui_app);
708                  break;
709
710                  case HILDON_HARDKEY_ESC: 
711                       ws_gui_search_stop(NULL, ws_gui_app);
712                  break;
713
714                  default:
715                       return FALSE;
716                  break;
717         }
718         return TRUE;
719 }
720
721 /** this function allow to hide words list using menu item from application menu
722  *
723  * @param checkmenuitem - the object which received the signal
724  * @param user_data - user data set when the signal handler was connected
725  * @return
726  */
727 void ws_gui_words_list_hide_from_menu(GtkCheckMenuItem *checkmenuitem,
728                                       gpointer user_data)
729 {
730         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
731         if (gtk_check_menu_item_get_active(
732            GTK_CHECK_MENU_ITEM(ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list)))
733         {
734                 gtk_widget_hide(ws_gui_app->ws_gui_scrolledwindow_left);
735                 gtk_toggle_tool_button_set_active(
736                   GTK_TOGGLE_TOOL_BUTTON(ws_gui_app->ws_gui_toobar_button_hide),
737                   TRUE);
738                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
739                                 ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list),
740                                 TRUE);
741         }
742         else 
743         {
744                 gtk_widget_show(ws_gui_app->ws_gui_scrolledwindow_left);
745                 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(
746                                          ws_gui_app->ws_gui_toobar_button_hide),
747                                          FALSE);
748                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
749                                 ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list),
750                                 FALSE);
751         }
752 }
753
754 /** this function allow to hide words list using toggle button placed in 
755 the find toolbar
756  *
757  * @param toolbar - the object which received the signal
758  * @param user_data - user data set when the signal handler was connected
759  * @return
760  */
761 void ws_gui_words_list_hide(GtkToggleButton *togglebutton, gpointer user_data)
762 {
763         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
764
765         if (gtk_toggle_tool_button_get_active(
766                  GTK_TOGGLE_TOOL_BUTTON(ws_gui_app->ws_gui_toobar_button_hide)))
767         {
768                 gtk_widget_hide(ws_gui_app->ws_gui_scrolledwindow_left);
769                 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(
770                                          ws_gui_app->ws_gui_toobar_button_hide),
771                                          TRUE);
772                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
773                                 ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list),
774                                 TRUE);
775         }
776         else 
777         {
778                 gtk_widget_show(ws_gui_app->ws_gui_scrolledwindow_left);
779                 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(
780                                          ws_gui_app->ws_gui_toobar_button_hide),
781                                          FALSE);
782                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
783                                 ws_gui_app->ws_gui_menu->ws_gui_menu_hide_list),
784                                 FALSE);
785         }
786 }
787
788 /** add word to the history
789  *
790  * @param new_word - word which is going to be append to the history array
791  * @param user_data - user data set when the signal handler was connected
792  * @return
793  */
794 void ws_gui_history_add(char *new_word, gpointer user_data)
795 {
796         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
797
798         if (ws_gui_app->history_flag != TRUE)
799         {
800
801                 guint i;
802                 gchar *tmp_word;
803                 gchar *tmp_last_searched;
804                 gchar *tmp_iter = NULL;
805                 gchar *previous_word = " ";
806         
807                 if (ws_gui_app->ws_gui_history_cur_pos > -1 &&
808                     g_array_index(ws_gui_app->ws_gui_history, 
809                                   gchar*, 
810                                   ws_gui_app->ws_gui_history_cur_pos) != NULL)
811                 {
812                         previous_word = NULL;
813                         previous_word = g_array_index(
814                                         ws_gui_app->ws_gui_history, 
815                                         gchar*, 
816                                         ws_gui_app->ws_gui_history_cur_pos);
817                 }
818         
819                 i = ws_gui_app->ws_gui_history_cur_pos + 1;
820                 gchar *tmp = g_array_index(ws_gui_app->ws_gui_history, 
821                                            gchar*, 
822                                            i);
823                 tmp_iter = gtk_tree_model_get_string_from_iter (GTK_TREE_MODEL(ws_gui_app->ws_gui_w_list->ws_gui_model), &ws_gui_app->ws_gui_w_list->ws_gui_iter);
824         
825                 if (previous_word != NULL && 
826                     strcmp(previous_word, new_word) != 0)
827                 {
828                         while (tmp != NULL) 
829                         {
830                                 g_array_remove_index(
831                                 ws_gui_app->ws_gui_history_list, 
832                                 i);
833                                 g_array_remove_index(
834                                 ws_gui_app->ws_gui_history_iter, 
835                                 i);
836
837                                 
838                                 g_array_remove_index(
839                                 ws_gui_app->ws_gui_history, 
840                                 i);
841                                 
842                                 tmp = g_array_index(
843                                       ws_gui_app->ws_gui_history, 
844                                       gchar*, 
845                                       i);
846                         }
847         
848                 i = 0;
849                 ws_gui_app->ws_gui_history_cur_pos ++;
850         
851                 if (ws_gui_app->bookmark_mode == FALSE)
852                 {
853                         tmp_last_searched = g_strdup(ws_gui_app->last_searched);
854                 }
855                 else
856                 {
857                 tmp_last_searched = g_strdup(ws_gui_app->last_searched_in_book);
858                 }
859
860                 g_array_append_val(ws_gui_app->ws_gui_history_list, 
861                                    tmp_last_searched);
862                                 
863                 tmp_word = g_strdup(new_word);
864                 
865                 g_array_append_val(ws_gui_app->ws_gui_history, tmp_word);
866                 g_array_append_val(ws_gui_app->ws_gui_history_iter, tmp_iter);
867                 
868         
869                 if(ws_gui_app->ws_gui_history->len > HISTORY_LEN)
870                 {
871                         g_array_remove_index(ws_gui_app->ws_gui_history, 0);
872                         g_array_remove_index(ws_gui_app->ws_gui_history_list, 
873                                         0);
874                         g_array_remove_index(ws_gui_app->ws_gui_history_iter, 
875                                         0);
876                         
877                         ws_gui_app->ws_gui_history_cur_pos--;
878                 }
879         
880                 i = 0;
881                 tmp = g_array_index(ws_gui_app->ws_gui_history, gchar*, i);
882                 }
883                 
884         }
885
886         ws_gui_check_history(ws_gui_app);
887 }
888
889 /** display previously choosen word (previous from the history array)
890      if avaible, sets current position in the history array
891  *
892  * @param button - button which recived a signal
893  * @param user_data - user data set when the signal handler was connected
894  * @return 
895  */
896 void ws_gui_history_back(GtkButton *button, gpointer user_data) 
897 {
898         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
899         
900         ws_gui_app->history_flag = TRUE;
901
902         if (ws_gui_app->ws_gui_history_cur_pos > 0) 
903         {
904                 ws_gui_app->ws_gui_history_cur_pos = 
905                                         ws_gui_app->ws_gui_history_cur_pos - 1;
906
907                 
908
909         g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "\n\n\n\nHISTORY BACK:\n pattern: %s,\nlist: %s,\niter: %s\n\n", g_array_index(ws_gui_app->ws_gui_history,
910                              gchar*, 
911                              ws_gui_app->ws_gui_history_cur_pos), g_array_index(ws_gui_app->ws_gui_history_list,
912                              gchar*, 
913                              ws_gui_app->ws_gui_history_cur_pos), g_array_index(ws_gui_app->ws_gui_history_iter,
914                              gchar*, 
915                              ws_gui_app->ws_gui_history_cur_pos));
916
917                 
918                 g_object_set(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
919                              "prefix",
920                              (g_array_index(ws_gui_app->ws_gui_history_list,
921                              gchar*, 
922                              ws_gui_app->ws_gui_history_cur_pos)),
923                              NULL);
924         
925                 
926         
927                 ws_dbus_client_find_word (ws_gui_app->dbus_data, 
928                                         g_array_index(ws_gui_app->ws_gui_history_list,
929                                         gchar*, 
930                                         ws_gui_app->ws_gui_history_cur_pos)
931                                         );
932         }
933         else 
934         {
935                 gtk_widget_set_sensitive (
936                               GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_back),
937                               FALSE);
938                 gtk_widget_set_sensitive(
939                      GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_prev),
940                      FALSE);
941         }
942
943         ws_gui_check_history(ws_gui_app);
944 }
945
946 /** display choosen word, next in the history array (if avaible), 
947 sets current position in the history array
948  *
949  * @param button - button which recived a signal
950  * @param user_data - user data set when the signal handler was connected
951  * @return 
952  */
953 void ws_gui_history_next(GtkButton *button, gpointer user_data) 
954 {
955         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
956         
957         ws_gui_app->history_flag = TRUE;
958
959         gchar *tmp = g_array_index(ws_gui_app->ws_gui_history,
960                                    gchar*,
961                                    ws_gui_app->ws_gui_history_cur_pos+1);
962
963         if ((ws_gui_app->ws_gui_history_cur_pos < HISTORY_LEN-1) 
964              && (tmp != NULL)) 
965         {
966                 ws_gui_app->ws_gui_history_cur_pos =
967                          ws_gui_app->ws_gui_history_cur_pos + 1;
968
969
970                 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "\n\n\n\nHISTORY NEXT:\n pattern: %s,\nlist: %s,\niter: %s\n\n", g_array_index(ws_gui_app->ws_gui_history,
971                              gchar*, 
972                              ws_gui_app->ws_gui_history_cur_pos), g_array_index(ws_gui_app->ws_gui_history_list,
973                              gchar*, 
974                              ws_gui_app->ws_gui_history_cur_pos), g_array_index(ws_gui_app->ws_gui_history_iter,
975                              gchar*, 
976                              ws_gui_app->ws_gui_history_cur_pos));
977
978                 g_object_set(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
979                                "prefix",
980                                (g_array_index(ws_gui_app->ws_gui_history_list,
981                                gchar*, 
982                                ws_gui_app->ws_gui_history_cur_pos)),
983                                NULL);
984         
985                 
986                 ws_dbus_client_find_word(ws_gui_app->dbus_data, 
987                                         g_array_index(ws_gui_app->ws_gui_history_list,
988                                         gchar*, 
989                                         ws_gui_app->ws_gui_history_cur_pos)
990                                         );
991
992         }
993         else 
994         {
995                 gtk_widget_set_sensitive(
996                            GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_forward),
997                            FALSE);
998                 gtk_widget_set_sensitive(
999                      GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_next),
1000                      FALSE);
1001         }
1002
1003         ws_gui_check_history(ws_gui_app);
1004 }
1005
1006 /** check current position in the history array and sets sensitivity of buttons 
1007 / menu items, depends on availablity of words in the history
1008  *
1009  * @param user_data - user data set when the signal handler was connected
1010  * @return 
1011  */
1012 void ws_gui_check_history(gpointer user_data) 
1013 {
1014         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
1015
1016         gchar *tmp = g_array_index(ws_gui_app->ws_gui_history,
1017                                    gchar*,
1018                                    ws_gui_app->ws_gui_history_cur_pos+1);
1019
1020         if ((ws_gui_app->ws_gui_history_cur_pos+1 < HISTORY_LEN) 
1021              && (tmp != NULL))
1022         {
1023                 gtk_widget_set_sensitive(
1024                            GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_forward),
1025                            TRUE);
1026                 gtk_widget_set_sensitive(
1027                      GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_next),
1028                      TRUE);
1029         }
1030         else 
1031         {
1032                 gtk_widget_set_sensitive(
1033                            GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_forward),
1034                            FALSE);
1035                 gtk_widget_set_sensitive(
1036                      GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_next),
1037                      FALSE);
1038         }
1039
1040         tmp = g_array_index(ws_gui_app->ws_gui_history,
1041                             gchar*,
1042                             ws_gui_app->ws_gui_history_cur_pos-1);
1043         if ((ws_gui_app->ws_gui_history_cur_pos > 0) && (tmp != NULL))
1044         {
1045                 gtk_widget_set_sensitive(
1046                               GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_back),
1047                               TRUE);
1048                 gtk_widget_set_sensitive(
1049                      GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_prev),
1050                      TRUE);
1051        }
1052        else
1053        {
1054                 gtk_widget_set_sensitive (
1055                         GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_back),
1056                         FALSE);
1057                 gtk_widget_set_sensitive(
1058                      GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_find_prev),
1059                      FALSE);
1060        }
1061 }
1062
1063 /** create TreeView Model, which allows to display words list
1064  *
1065  * @param words_list - array with words(found in a dictionary), recived from 
1066  * DBUS;
1067  * @param user_data - user data set when the signal handler was connected
1068  * @return 
1069  */
1070 GtkTreeModel * create_and_fill_model (GArray *words_list, gpointer user_data)
1071 {
1072         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1073
1074         g_signal_handlers_block_by_func(G_OBJECT (ws_gui_app->ws_gui_w_list->ws_gui_view),
1075                                         G_CALLBACK (ws_gui_view_cursor_changed), 
1076                                         ws_gui_app);    
1077
1078         guint i = 0;
1079         gchar *tmp = g_strdup(g_array_index(words_list, gchar*, i));
1080
1081         gboolean valid;
1082         valid = gtk_tree_model_get_iter_first(
1083                         GTK_TREE_MODEL(ws_gui_app->ws_gui_w_list->ws_gui_store),
1084                         &ws_gui_app->ws_gui_w_list->ws_gui_iter);
1085
1086         /* Append a row and fill in some data */
1087         while (tmp != NULL)
1088         {
1089                 gtk_list_store_append (ws_gui_app->ws_gui_w_list->ws_gui_store,
1090                                        &ws_gui_app->ws_gui_w_list->ws_gui_iter);
1091
1092                 gtk_list_store_set (ws_gui_app->ws_gui_w_list->ws_gui_store,
1093                               &ws_gui_app->ws_gui_w_list->ws_gui_iter, 
1094                               COL_WORD, tmp,
1095                               -1);
1096                 i=i+1;
1097                 tmp = g_strdup(g_array_index(words_list, gchar*, i));
1098         };
1099
1100         tmp = g_strdup(g_array_index(words_list, gchar*, 0));
1101
1102                 
1103         if (ws_gui_app->history_flag == FALSE && tmp != NULL)
1104         {
1105                 ws_dbus_client_find_translation(ws_gui_app->dbus_data, tmp);
1106                 ws_gui_history_add(tmp, ws_gui_app);
1107
1108                 g_free(ws_gui_app->last_word);
1109                 ws_gui_app->last_word = NULL;
1110                 ws_gui_app->last_word = g_strdup (tmp);
1111                 ws_gui_app->history_flag = FALSE;
1112         }
1113
1114         g_signal_handlers_unblock_by_func(G_OBJECT(
1115                                         ws_gui_app->ws_gui_w_list->ws_gui_view),
1116                                         G_CALLBACK (ws_gui_view_cursor_changed), 
1117                                         ws_gui_app);
1118
1119         return GTK_TREE_MODEL(ws_gui_app->ws_gui_w_list->ws_gui_store);
1120         
1121 }
1122
1123 /** create TreeView and TreeModel using create_and_fill_model() function;
1124  it is necessary to display found words in a words list;
1125  *
1126  * @param words_list - array with words(found in a dictionary), 
1127  * recived from DBUS;
1128  * @param user_data - user data set when the signal handler was connected
1129  * @return 
1130  */
1131 GtkWidget * create_view_and_model (GArray *words_list, gpointer user_data)
1132 {
1133         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1134
1135         ws_gui_app->ws_gui_w_list->ws_gui_view = gtk_tree_view_new ();
1136
1137         ws_gui_app->ws_gui_w_list->ws_gui_renderer=gtk_cell_renderer_text_new();
1138         gtk_tree_view_insert_column_with_attributes(
1139                          GTK_TREE_VIEW (ws_gui_app->ws_gui_w_list->ws_gui_view),
1140                          -1,
1141                          "Name",
1142                          ws_gui_app->ws_gui_w_list->ws_gui_renderer,
1143                          "text",
1144                          COL_WORD,
1145                          NULL);
1146         ws_gui_app->ws_gui_w_list->ws_gui_model = 
1147                                 create_and_fill_model(words_list, ws_gui_app);
1148
1149         gtk_tree_view_set_model(
1150                          GTK_TREE_VIEW (ws_gui_app->ws_gui_w_list->ws_gui_view),
1151                          ws_gui_app->ws_gui_w_list->ws_gui_model);
1152         g_object_unref (ws_gui_app->ws_gui_w_list->ws_gui_model);
1153         
1154         return ws_gui_app->ws_gui_w_list->ws_gui_view;
1155 }
1156
1157 /** switch application between fun screen and normal mode
1158  *
1159  * @param menuitem - object which recived the signal
1160  * @param user_data - user data set when the signal handler was connected 
1161  * @return
1162  */
1163 void ws_gui_full_screen(GtkMenuItem *menuitem, gpointer user_data)
1164 {
1165
1166         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
1167         if (ws_gui_app->ws_gui_full_screen_flag == FALSE) {
1168
1169                 gtk_window_fullscreen(
1170                                   GTK_WINDOW(ws_gui_app->ws_gui_hildon_window));
1171                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
1172                               ws_gui_app->ws_gui_menu->ws_gui_menu_full_screen),
1173                               TRUE);
1174                 ws_gui_app->ws_gui_full_screen_flag = TRUE;
1175                 gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
1176                                  _("ws_ib_fullscreen_on"));
1177         }
1178         else 
1179         {
1180                 gtk_window_unfullscreen(
1181                                   GTK_WINDOW(ws_gui_app->ws_gui_hildon_window));
1182                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
1183                               ws_gui_app->ws_gui_menu->ws_gui_menu_full_screen),
1184                               FALSE);
1185                 ws_gui_app->ws_gui_full_screen_flag = FALSE;
1186                 gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
1187                                  _("ws_ib_fullscreen_off"));
1188         }
1189 }
1190
1191 /** search for selected text in a dictionary
1192  *
1193  * @param menuitem - object which recived the signal
1194  * @param user_data - user data set when the signal handler was connected 
1195  * @return
1196  */
1197 void ws_gui_popup_search(GtkMenuItem *menuitem, gpointer user_data)
1198 {
1199         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
1200         gchar *temp;
1201         ws_gui_app->ws_gui_clipboard_primary = 
1202                 gtk_widget_get_clipboard(ws_gui_app->ws_gui_html,
1203                                          GDK_SELECTION_PRIMARY);
1204         temp = gtk_clipboard_wait_for_text(
1205                                           ws_gui_app->ws_gui_clipboard_primary);
1206
1207         g_strstrip(temp);
1208         if (temp != NULL || strcmp(temp, " "))
1209         {
1210                 g_object_set(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
1211                                       "prefix",
1212                                       temp,
1213                                       NULL);
1214                 ws_dbus_client_find_word (ws_gui_app->dbus_data, temp);
1215         }
1216         else
1217         {
1218                 hildon_banner_show_information(
1219                                    GTK_WIDGET(ws_gui_app->ws_gui_hildon_window),
1220                                    NULL,
1221                                    _("ws_ni_no_text_selected"));
1222         }
1223 }
1224
1225 /** select whole text in the translation (html) area
1226  *
1227  * @param menuitem - object which recived the signal
1228  * @param user_data - user data set when the signal handler was connected 
1229  * @return
1230  */
1231 void ws_gui_html_select_all(GtkMenuItem *menuitem, gpointer user_data) 
1232 {
1233         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
1234         gtk_html_select_all(GTK_HTML(ws_gui_app->ws_gui_html));
1235 }
1236
1237 /** copy selected text to the clipoard from context popup menu
1238  *
1239  * @param menuitem - object which recived the signal
1240  * @param user_data - user data set when the signal handler was connected 
1241  * @return
1242  */
1243 void ws_gui_html_copy(GtkMenuItem *menuitem, gpointer user_data) 
1244 {
1245         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
1246         gtk_html_copy(GTK_HTML(ws_gui_app->ws_gui_html));
1247         gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
1248                                  _("ws_ib_copied"));
1249 }
1250
1251 /** paste copied text into toolbar entry
1252  *
1253  * @param menuitem - object which recived the signal
1254  * @param user_data - user data set when the signal handler was connected 
1255  * @return
1256  */
1257 void ws_gui_html_paste(GtkMenuItem *menuitem, gpointer user_data)
1258 {
1259         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
1260
1261         gchar *temp;
1262         gchar *temp2;
1263         temp = gtk_clipboard_wait_for_text(ws_gui_app->ws_gui_clipboard);
1264         g_object_get(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
1265                      "prefix",
1266                      &temp2,
1267                      NULL);
1268         temp = g_strconcat(temp2, temp, NULL);
1269         temp = g_strdelimit(temp, "\n", ' ');
1270         g_object_set(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
1271                      "prefix",
1272                      temp,
1273                      NULL);
1274         gtk_infoprint(GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
1275                                  _("ws_ib_pasted"));
1276 }
1277
1278 /** zoom in text in translation (html) area
1279  *
1280  * @param menuitem - object which recived the signal
1281  * @param user_data - user data set when the signal handler was connected 
1282  * @return
1283  */
1284 void ws_gui_html_zoom_in(GtkMenuItem *menuitem, gpointer user_data)
1285 {
1286         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
1287         ws_gui_app->zoom = ws_gui_app->zoom + ZOOM_STEP;
1288         if (ws_gui_app->zoom > ZOOM_MAX)
1289         {
1290                 ws_gui_app->zoom = ws_gui_app->zoom - ZOOM_STEP;
1291                 gtk_infoprint(GTK_WINDOW(
1292                                       ws_gui_app->ws_gui_hildon_window),
1293                                       _("ws_ib_max_zoom"));
1294         }
1295         else
1296         {
1297                 gtk_html_set_magnification(GTK_HTML(ws_gui_app->ws_gui_html), 
1298                                                 ws_gui_app->zoom);
1299
1300                 if ((WS_GUI_ABS(ws_gui_app->zoom - ZOOM_DEFAULT)) < 0.000001)
1301                 {
1302                         gtk_infoprint(GTK_WINDOW(
1303                                         ws_gui_app->ws_gui_hildon_window),
1304                                         _("ws_ib_zoom_default"));
1305                 }
1306                 else
1307                 {
1308                         gtk_infoprint(GTK_WINDOW(
1309                                         ws_gui_app->ws_gui_hildon_window),
1310                                         _("ws_ib_zoom_in"));
1311                 }
1312         }
1313 }
1314
1315 /** zoom out text in translation (html) area
1316  *
1317  * @param menuitem - object which recived the signal
1318  * @param user_data - user data set when the signal handler was connected 
1319  * @return
1320  */
1321 void ws_gui_html_zoom_out(GtkMenuItem *menuitem, gpointer user_data)
1322 {
1323         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
1324         ws_gui_app->zoom = ws_gui_app->zoom - ZOOM_STEP;
1325         if (ws_gui_app->zoom < ZOOM_MIN)
1326         {
1327                 ws_gui_app->zoom = ws_gui_app->zoom + ZOOM_STEP;
1328                 gtk_infoprint(GTK_WINDOW(
1329                                       ws_gui_app->ws_gui_hildon_window),
1330                                       _("ws_ib_min_zoom"));
1331         }
1332         else
1333         {
1334                 gtk_html_set_magnification(GTK_HTML(ws_gui_app->ws_gui_html), 
1335                                                 ws_gui_app->zoom);
1336                 
1337                 if ((WS_GUI_ABS(ws_gui_app->zoom - ZOOM_DEFAULT)) < 0.000001)
1338                 {
1339                         gtk_infoprint(GTK_WINDOW(
1340                                         ws_gui_app->ws_gui_hildon_window),
1341                                         _("ws_ib_zoom_default"));
1342                 }
1343                 else
1344                 {
1345                         gtk_infoprint(GTK_WINDOW(
1346                                         ws_gui_app->ws_gui_hildon_window),
1347                                         _("ws_ib_zoom_out"));
1348                 }
1349         }
1350 }
1351
1352 /** start searching, send typed word to DBUS and query for words
1353  *
1354  * @param widget - object which recived the signal
1355  * @param user_data - user data set when the signal handler was connected 
1356  * @return
1357  */
1358 void ws_gui_search(GtkWidget * widget, gpointer user_data) 
1359 {
1360         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
1361
1362         if (ws_gui_app->ws_gui_banner_flag == FALSE) 
1363         {
1364         gchar* ws_gui_text = NULL;
1365         g_object_get(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
1366                      "prefix",
1367                      &ws_gui_text,
1368                      NULL);
1369
1370         if (ws_gui_app->bookmark_mode == FALSE) 
1371         {
1372                 g_free(ws_gui_app->last_searched);
1373                 ws_gui_app->last_searched = NULL;
1374                 ws_gui_app->last_searched = g_strdup(ws_gui_text);
1375         }
1376         else
1377         {
1378                 g_free(ws_gui_app->last_searched_in_book);
1379                 ws_gui_app->last_searched_in_book = NULL;
1380                 ws_gui_app->last_searched_in_book = g_strdup(ws_gui_text);
1381         }
1382         
1383         g_strstrip(ws_gui_text);
1384         if (strlen(ws_gui_text) != 0) 
1385         {
1386                 gtk_widget_show(ws_gui_app->ws_gui_banner);
1387                 
1388                 gtk_widget_set_sensitive(
1389                               GTK_WIDGET(ws_gui_app->ws_gui_toobar_button_stop),
1390                               TRUE);
1391                 gtk_widget_set_sensitive(
1392                           GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_stop),
1393                           TRUE);
1394
1395                 ws_gui_app->ws_gui_banner_flag = TRUE;
1396                 ws_gui_fill_html(" ", ws_gui_app);
1397                 ws_gui_app->html_flag = FALSE;
1398                 ws_dbus_client_find_word (ws_gui_app->dbus_data, ws_gui_text);
1399
1400         }
1401         else 
1402         {
1403                 gtk_infoprint(
1404                               GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
1405                               _("ws_ni_no_word_typed"));
1406         }
1407         g_free(ws_gui_app->last_word);
1408         ws_gui_app->last_word=NULL;
1409         }
1410 }
1411
1412 /** stop search process
1413  *
1414  * @param button - object which recived the signal
1415  * @param user_data - user data set when the signal handler was connected 
1416  * @return
1417  */
1418 void ws_gui_search_stop(GtkButton *button, gpointer user_data) 
1419 {
1420         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
1421         if (ws_gui_app->ws_gui_banner_flag == TRUE) 
1422         {
1423                 gtk_widget_hide(GTK_WIDGET(ws_gui_app->ws_gui_banner));
1424                 ws_gui_app->ws_gui_banner_flag = FALSE;
1425                 ws_dbus_notify(ws_gui_app->dbus_data, WS_DBUS_INFO_STOP_SEARCH);
1426                 gtk_infoprint(
1427                               GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
1428                               _("ws_ni_search_aborted"));
1429
1430         gtk_widget_set_sensitive(GTK_WIDGET
1431                                (ws_gui_app->ws_gui_toobar_button_stop),
1432                                FALSE);
1433         }
1434 }
1435
1436 /** this function is called just before closing application; 
1437 it sends signal to DBUS and destroys it; 
1438  *
1439  * @param widget - object which recived the signal
1440  * @param event - 
1441  * @param user_data - user data set when the signal handler was connected 
1442  * @return
1443  */
1444 void ws_gui_on_exit (GtkWidget *widget, GdkEvent *event, gpointer user_data) 
1445 {
1446         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
1447         ws_gui_app->bookmark_mode = FALSE;
1448         ws_gui_set_bookmarks_sensitivity(ws_gui_app);
1449         g_timer_destroy(ws_gui_app->timer);
1450         ws_dbus_notify(ws_gui_app->dbus_data, WS_DBUS_INFO_TERMINATE);
1451         ws_dbus_destroy (ws_gui_app->dbus_data);
1452         ws_gui_free_memory(ws_gui_app);
1453         gtk_main_quit();
1454         exit (0);
1455 }
1456
1457 /** this function is called just before closing application, 
1458 from application menu; it sends signal to DBUS and destroys it;
1459  *
1460  * @param menuitem - object which recived the signal
1461  * @param user_data - user data set when the signal handler was connected 
1462  * @return
1463  */
1464 void ws_gui_menu_quit(GtkMenuItem *menuitem, gpointer user_data)
1465 {
1466         WSGuiApp* ws_gui_app = (WSGuiApp *)user_data;
1467         ws_gui_app->bookmark_mode = FALSE;
1468         ws_gui_set_bookmarks_sensitivity(ws_gui_app);
1469         g_timer_destroy(ws_gui_app->timer);
1470         ws_dbus_notify(ws_gui_app->dbus_data, WS_DBUS_INFO_TERMINATE);
1471         ws_dbus_destroy (ws_gui_app->dbus_data);
1472         ws_gui_free_memory(ws_gui_app);
1473         gtk_main_quit();
1474         exit (0);
1475 }
1476
1477 /** fill translation area with text (html) recived from DBUS
1478  *
1479  * @param html_context - text which is going to be displayed; it should be html
1480  * @param user_data - user data set when the function was called
1481  * @return
1482  */
1483 void ws_gui_fill_html(char *html_context, gpointer user_data) 
1484 {
1485         WSGuiApp* ws_gui_app = (WSGuiApp*)user_data;
1486         gtk_html_set_editable (GTK_HTML(ws_gui_app->ws_gui_html), FALSE);
1487
1488         gtk_html_load_from_string(GTK_HTML(ws_gui_app->ws_gui_html),
1489                                   html_context,
1490                                   -1);
1491 }
1492
1493 /** read adjustment of left scrollwindow, which is necessary to navigate with 
1494 arrow keys inside words list
1495  *
1496  * @param user_data - user data set when the signal handler was connected 
1497  * @return
1498  */
1499 void ws_gui_read_adjustment(gpointer user_data)
1500 {
1501         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1502
1503         ws_gui_app->ws_gui_hadj = 
1504                 gtk_scrolled_window_get_hadjustment(
1505                 GTK_SCROLLED_WINDOW(
1506                 ws_gui_app->ws_gui_scrolledwindow_left));
1507
1508         ws_gui_app->h_delta = (ws_gui_app->ws_gui_hadj->upper -
1509                                   ws_gui_app->ws_gui_hadj->lower)/SCROLL_STEP_H;
1510
1511         ws_gui_app->ws_gui_vadj = gtk_scrolled_window_get_vadjustment(
1512         GTK_SCROLLED_WINDOW(ws_gui_app->ws_gui_scrolledwindow_left));
1513
1514         ws_gui_app->v_delta = (ws_gui_app->ws_gui_vadj->upper -
1515                                   ws_gui_app->ws_gui_vadj->lower)/SCROLL_STEP_V;
1516         ws_gui_app->v_new_value =
1517            gtk_adjustment_get_value(GTK_ADJUSTMENT(ws_gui_app->ws_gui_vadj)) +
1518                                                             ws_gui_app->v_delta;
1519
1520         gtk_container_set_focus_vadjustment(
1521                GTK_CONTAINER(ws_gui_app->ws_gui_scrolledwindow_left), 
1522                gtk_scrolled_window_get_vadjustment(
1523                   GTK_SCROLLED_WINDOW(ws_gui_app->ws_gui_scrolledwindow_left)));
1524
1525 }
1526
1527 /** allows to display image in html area
1528  *
1529  * @param html - object which received a signal 
1530  * @param url - url to the image
1531  * @param stream - html stream 
1532  * @return
1533  */
1534 void ws_gui_url_requested (
1535                           GtkHTML *html, 
1536                           const char *url, 
1537                           GtkHTMLStream *stream)
1538 {
1539         int fd;
1540
1541         if (url && !strncmp (url, "file:", 5)) {
1542                 url += 5;
1543                 fd = open (url, O_RDONLY);
1544                 if (fd != -1) {
1545                         gchar *buf;
1546                         size_t size;
1547                         buf = alloca (8192);
1548                         while ((size = read (fd, buf, 8192)) > 0) {
1549                                 gtk_html_stream_write (stream, buf, size);
1550                         }
1551                        gtk_html_stream_close(stream, size == -1
1552                                          ? GTK_HTML_STREAM_ERROR
1553                                          : GTK_HTML_STREAM_OK);
1554                        close (fd);
1555
1556                       return;
1557                 }
1558         }
1559
1560         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
1561 }
1562
1563 /** handles button press event and examines what kind of event was it 
1564         suppose to be
1565  *
1566  * @param widget - object which received a signal
1567  * @param event - type of event which has been performed
1568  * @param user_data - user data set when the signal handler was connected 
1569  * @return TRUE to stop other handlers from being invoked for the event. 
1570  FALSE to propagate the event further
1571  */
1572 gboolean ws_gui_button_press(GtkWidget *widget,
1573                              GdkEventButton *event,
1574                              gpointer user_data)
1575 {
1576         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1577         if (ws_gui_app->ws_gui_sel_flag == FALSE)
1578         {
1579                 if (event->type == GDK_2BUTTON_PRESS) 
1580                 {
1581                         ws_gui_app->ws_gui_double_click = TRUE;
1582                         g_timer_stop(ws_gui_app->timer);
1583                         g_timer_reset(ws_gui_app->timer);
1584                         return FALSE;
1585                 }
1586
1587                 g_signal_stop_emission_by_name(G_OBJECT(
1588                                                 ws_gui_app->ws_gui_html),
1589                                                "button-press-event");
1590                 g_timer_start(ws_gui_app->timer);
1591                 gtk_timeout_add((guint)(PRESS_TIME*1000),
1592                                 (GtkFunction)(ws_gui_show_popup),
1593                                 ws_gui_app);
1594                 return FALSE;
1595         }
1596         else
1597         {
1598                 ws_gui_app->ws_gui_sel_flag = FALSE;
1599                 return FALSE;
1600         }
1601
1602 }
1603
1604 /** handles button release event and examines whether 'click' or 'tap and hold' 
1605 event it supposed to be
1606  *
1607  * @param widget - object which received a signal
1608  * @param event - type of event which has been performed
1609  * @param user_data - user data set when the signal handler was connected 
1610  * @return TRUE to stop other handlers from being invoked for the event. 
1611  FALSE to propagate the event further
1612  */
1613 gboolean ws_gui_button_release (GtkWidget *widget,
1614                                 GdkEventButton *event,
1615                                 gpointer user_data)
1616 {
1617         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1618         gdouble tmp = g_timer_elapsed(ws_gui_app->timer, NULL);
1619         g_timer_stop(ws_gui_app->timer);
1620         g_timer_reset(ws_gui_app->timer);
1621         if (ws_gui_app->ws_gui_double_click == TRUE)
1622         {
1623                 ws_gui_app->ws_gui_double_click = FALSE;
1624                 return FALSE;
1625         }
1626         else if (tmp < PRESS_TIME)
1627         {
1628                 struct _GtkHTML *tmp = (struct _GtkHTML *)
1629                                         (ws_gui_app->ws_gui_html);
1630                 html_engine_unselect_all(tmp->engine);
1631                 return TRUE;
1632         }
1633                 return FALSE;
1634 }
1635
1636 /** displays popup menu if user holds a stylus more than PRESS_TIME seconds
1637  *
1638  * @param user_data - user data set when the signal handler was connected 
1639  * @return 
1640  */
1641 guint ws_gui_show_popup (gpointer user_data)
1642 {
1643         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1644         gdouble tmp = g_timer_elapsed(ws_gui_app->timer, NULL);
1645         if (tmp > PRESS_TIME)
1646         {
1647                 ws_gui_create_popup_menu(ws_gui_app);
1648         }
1649         return (guint)(FALSE);
1650
1651 }
1652
1653 /** checks clipboard content and sets sensitivity of widgets 
1654  *
1655  * @param widget - object which recived a signal
1656  * @param user_data - user data set when the signal handler was connected 
1657  * @return 
1658  */
1659 void ws_gui_check_clipboard (GtkWidget *widget, gpointer user_data) 
1660 {
1661         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1662         if (gtk_clipboard_wait_for_text(ws_gui_app->ws_gui_clipboard) != NULL)
1663         {
1664                 gtk_widget_set_sensitive(
1665                          GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_paste),
1666                          TRUE);
1667         }
1668         else
1669         {
1670                 gtk_widget_set_sensitive(
1671                          GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_paste),
1672                          FALSE);
1673         }
1674
1675         struct _GtkHTML *tmp = (struct _GtkHTML *)(ws_gui_app->ws_gui_html);
1676
1677         if (html_engine_is_selection_active(tmp->engine) == TRUE)
1678         {
1679                 gtk_widget_set_sensitive(GTK_WIDGET
1680                                 (ws_gui_app->ws_gui_menu->ws_gui_menu_copy),
1681                                 TRUE);
1682         }
1683         else 
1684         {
1685                 gtk_widget_set_sensitive(GTK_WIDGET
1686                                 (ws_gui_app->ws_gui_menu->ws_gui_menu_copy),
1687                                 FALSE);
1688         }
1689
1690         if (ws_gui_app->html_flag == FALSE)
1691         {
1692                 gtk_widget_set_sensitive(GTK_WIDGET
1693                         (ws_gui_app->ws_gui_menu->ws_gui_menu_select_all),
1694                         FALSE);
1695         }
1696         else
1697         {
1698                 gtk_widget_set_sensitive(GTK_WIDGET
1699                         (ws_gui_app->ws_gui_menu->ws_gui_menu_select_all),
1700                         TRUE);
1701         }       
1702
1703 }
1704
1705 /** gets received string (with translation), formats it and sends it to be 
1706  * displayed
1707  *
1708  * @param received_string - content received from Manager
1709  * @param user_data - user data set when the function has been called
1710  * @return 
1711  */
1712 gchar * format_html (gchar * received_string, gpointer user_data)
1713 {
1714         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1715         GString *str_final;
1716         GString *str_tmp;
1717         str_final = g_string_new(received_string);
1718         str_tmp = g_string_new(received_string);
1719         gchar * tmp;
1720         gchar * tmp2;
1721         
1722         ws_gui_app->raw_translation=g_string_erase(ws_gui_app->raw_translation, 
1723                                                    0, 
1724                                                   -1);
1725
1726         while (strstr(str_final->str, "<PATTERN_OPEN>") != NULL)
1727         {
1728                 tmp = strstr(str_final->str, "<PATTERN_OPEN>");
1729                 str_final =  g_string_erase(str_final,
1730                                            (long)(tmp - str_final->str),
1731                                            14);
1732                 str_final =  g_string_insert(str_final,
1733                  (long)(tmp - str_final->str),
1734                  "<html><head></head><body><center><table width=\"400px\"><tr>"
1735                  "<td><table border=1 width=100%><tr>"
1736                  "<th background=\"file:/usr/share/pixmaps/ws_top.png\">"
1737                  "<img align=left src=\"file:/usr/share/pixmaps/"
1738                  "engine_xdxf_icon.png\"><font color=\"#eeeeee\">");
1739                 
1740                 tmp2 = strstr(str_tmp->str, "<PATTERN_OPEN>");
1741                 if (ws_gui_app->last_word != NULL)
1742                 {
1743                         str_tmp =  g_string_erase(str_tmp,
1744                                            (long)(tmp2 - str_tmp->str),
1745                                            14 + strlen(ws_gui_app->last_word));
1746                 }
1747         }
1748
1749         while (strstr(str_final->str, "<PATTERN_CLOSED>") != NULL)
1750         {
1751                 tmp = strstr(str_final->str, "<PATTERN_CLOSED>");
1752                 str_final =  g_string_erase(str_final,
1753                                             (long)(tmp - str_final->str),
1754                                             16);
1755                 str_final =  g_string_insert(str_final,
1756                                             (long)(tmp - str_final->str),
1757                                             "</font></th></tr></table>");
1758
1759                 tmp2 = strstr(str_tmp->str, "<PATTERN_CLOSED>");
1760                 str_tmp =  g_string_erase(str_tmp,
1761                                           (long)(tmp2 - str_tmp->str),
1762                                           16);
1763         }
1764
1765         while (strstr(str_final->str, "<TRANSLATION_OPEN>") != NULL)
1766         {
1767                 tmp = strstr(str_final->str, "<TRANSLATION_OPEN>");
1768                 str_final =  g_string_erase (str_final,
1769                                              (long)(tmp - str_final->str),
1770                                              18);
1771                 str_final =  g_string_insert (str_final,
1772                 (long)(tmp - str_final->str),
1773                 "<table border=1 width=100%><tr><td background=\"file:/usr/"
1774                 "share/pixmaps/ws_tra.png\">");
1775
1776                 tmp2 = strstr(str_tmp->str, "<TRANSLATION_OPEN>");
1777                 str_tmp =  g_string_erase(str_tmp,
1778                                            (long)(tmp2 - str_tmp->str),
1779                                            18);
1780         }
1781
1782         while (strstr(str_final->str, "<TRANSLATION_CLOSED>") != NULL)
1783         {
1784                 tmp = strstr(str_final->str, "<TRANSLATION_CLOSED>");
1785                 str_final =  g_string_erase(str_final,
1786                                             (long)(tmp - str_final->str),
1787                                             20);
1788                 str_final =  g_string_insert(str_final,
1789                                              (long)(tmp - str_final->str),
1790                                              "</td></tr></table></center>");
1791
1792                 tmp2 = strstr(str_tmp->str, "<TRANSLATION_CLOSED>");
1793                 str_tmp =  g_string_erase(str_tmp,
1794                                           (long)(tmp2 - str_tmp->str),
1795                                           20);
1796         }
1797
1798         str_final = g_string_append(str_final,
1799                                     "</td></tr></table></body></html>");
1800
1801         ws_gui_app->raw_translation = g_string_insert(
1802                                                 ws_gui_app->raw_translation,
1803                                                 0,
1804                                                 str_tmp->str);
1805         
1806         return str_final->str;
1807 }
1808
1809 /** open bookmarks database 
1810  *
1811  * @param menuitem - object which recived the signal
1812  * @param user_data - user data set when the signal handler was connected 
1813  * @return
1814  */
1815 void ws_gui_dictionary_open_bookmark(GtkMenuItem *menuitem, gpointer user_data)
1816 {
1817         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1818
1819         ws_gui_read_active_dictionaries(ws_gui_app);
1820         
1821         g_free(ws_gui_app->last_word);
1822         ws_gui_app->last_word = NULL;
1823
1824         ws_gui_app->bookmark_mode = TRUE;
1825         ws_gui_set_bookmarks_sensitivity(ws_gui_app);
1826         g_object_set(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
1827                                      "prefix",
1828                                       "*",
1829                                       NULL);
1830         ws_dbus_notify(ws_gui_app->dbus_data, WS_DBUS_BOOKMARK_MODE_ON);
1831         
1832         if(ws_gui_app->ws_gui_history->len >= 0)
1833         {
1834         ws_gui_clear_array(ws_gui_app->ws_gui_history);
1835         ws_gui_clear_array(ws_gui_app->ws_gui_history_list);
1836         ws_gui_clear_array(ws_gui_app->ws_gui_history_iter);
1837         ws_gui_app->ws_gui_history_cur_pos = -1;
1838         }
1839         ws_gui_check_history(ws_gui_app);
1840         
1841         ws_gui_fill_html(" ", ws_gui_app);
1842         ws_gui_app->html_flag = FALSE;
1843         //ws_dbus_client_find_word(ws_gui_app->dbus_data, "*");
1844         ws_gui_search(NULL, ws_gui_app);
1845         
1846 }
1847
1848 /** closes bookmarks database 
1849  *
1850  * @param menuitem - object which recived the signal
1851  * @param user_data - user data set when the signal handler was connected 
1852  * @return
1853  */
1854 void ws_gui_dictionary_close_bookmark(GtkMenuItem *menuitem, gpointer user_data)
1855 {
1856         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1857
1858         ws_gui_read_active_dictionaries(ws_gui_app);
1859         
1860         g_free(ws_gui_app->last_word);
1861         ws_gui_app->last_word = NULL;
1862
1863         ws_gui_app->bookmark_mode = FALSE;
1864         ws_gui_set_bookmarks_sensitivity(ws_gui_app);   
1865         ws_dbus_notify(ws_gui_app->dbus_data, WS_DBUS_BOOKMARK_MODE_OFF);
1866
1867         if(ws_gui_app->ws_gui_history->len >= 0)
1868         {
1869         ws_gui_clear_array(ws_gui_app->ws_gui_history);
1870         ws_gui_clear_array(ws_gui_app->ws_gui_history_list);
1871         ws_gui_clear_array(ws_gui_app->ws_gui_history_iter);
1872         ws_gui_app->ws_gui_history_cur_pos = -1;
1873         }
1874         ws_gui_check_history(ws_gui_app);
1875
1876         ws_gui_fill_html(" ", ws_gui_app);
1877         ws_gui_app->html_flag = FALSE;
1878         g_strstrip(ws_gui_app->last_searched);
1879         if (ws_gui_app->last_searched != NULL) 
1880         {
1881         g_object_set(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
1882                                      "prefix",
1883                                       ws_gui_app->last_searched,
1884                                       NULL);
1885         //ws_dbus_client_find_word(ws_gui_app->dbus_data, ws_gui_app->last_searched);
1886         ws_gui_search(NULL, ws_gui_app);
1887         }
1888         else
1889         {
1890                 g_object_set(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
1891                                      "prefix",
1892                                       "\0",
1893                                       NULL);
1894                 GArray *tmp;
1895                 tmp = g_array_new(TRUE, TRUE, sizeof(gchar*));
1896                 gtk_list_store_clear(ws_gui_app->ws_gui_w_list->ws_gui_store);
1897                 ws_gui_app->ws_gui_w_list->ws_gui_model = 
1898                                         create_and_fill_model(tmp, ws_gui_app);
1899                 
1900                 //ws_dbus_client_find_word(ws_gui_app->dbus_data, ws_gui_app->last_searched);
1901                 ws_gui_search(NULL, ws_gui_app);        
1902         }
1903 }
1904
1905 /** adds bookmark to bookmarks database 
1906  *
1907  * @param menuitem - object which recived the signal
1908  * @param user_data - user data set when the signal handler was connected 
1909  * @return
1910  */
1911 void ws_gui_dictionary_add_bookmark(GtkMenuItem *menuitem, gpointer user_data)
1912 {
1913         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1914
1915         if (ws_gui_app->last_word != NULL)
1916         {
1917                 ws_gui_add_bookmark_dialog(ws_gui_app);
1918         } 
1919         else
1920         {
1921                 gtk_infoprint(
1922                               GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
1923                               _("ws_ni_select_word_to_add"));
1924         }
1925 }
1926
1927 /** removes bookmark from bookmarks database 
1928  *
1929  * @param menuitem - object which recived the signal
1930  * @param user_data - user data set when the signal handler was connected 
1931  * @return
1932  */
1933 void ws_gui_dictionary_remove_bookmark(GtkMenuItem *menuitem, 
1934                                        gpointer user_data)
1935 {
1936         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1937
1938         if (ws_gui_app->last_word != NULL)
1939         {
1940                 ws_gui_remove_bookmark_dialog(ws_gui_app);
1941
1942                 gchar *temp;
1943                 g_object_get(G_OBJECT(ws_gui_app->ws_gui_hildon_find_toolbar),
1944                                       "prefix",
1945                                       &temp,
1946                                       NULL);
1947                 ws_dbus_client_find_word (ws_gui_app->dbus_data, temp);
1948                 
1949         }
1950         else
1951         {
1952                 gtk_infoprint(
1953                               GTK_WINDOW(ws_gui_app->ws_gui_hildon_window),
1954                               _("ws_ni_select_word_to_remove"));
1955         }
1956         
1957         
1958 }
1959
1960 /** checks wheather dictionaries are available or not; according to the result
1961  * dimmes proper menuitem 
1962  *
1963  * @param menuitem - object which recived the signal
1964  * @param user_data - user data set when the signal handler was connected 
1965  * @return
1966  */
1967 void ws_gui_dict_availablity(GtkMenuItem *menuitem, gpointer user_data)
1968 {
1969         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
1970         if (gconf_client_dir_exists(ws_gui_app->client, 
1971                                     GCONF_PATH, NULL) == TRUE)
1972         {
1973                 ws_gui_app->directories = gconf_client_all_dirs(
1974                                                         ws_gui_app->client,
1975                                                         GCONF_PATH,
1976                                                         NULL
1977                                                         );
1978                 
1979         
1980                 if(g_slist_length(ws_gui_app->directories) != 0)
1981                 {
1982                         gtk_widget_set_sensitive(GTK_WIDGET
1983                         (ws_gui_app->ws_gui_menu->ws_gui_menu_remove_dict),
1984                         TRUE);
1985                         gtk_widget_set_sensitive(GTK_WIDGET
1986                         (ws_gui_app->ws_gui_menu->ws_gui_menu_select_dict),
1987                         TRUE);
1988                         gtk_widget_set_sensitive(GTK_WIDGET
1989                         (ws_gui_app->ws_gui_menu->ws_gui_menu_optimize_dict),
1990                         TRUE);
1991                 }
1992
1993         }
1994         else 
1995         {
1996         gtk_widget_set_sensitive(GTK_WIDGET
1997                              (ws_gui_app->ws_gui_menu->ws_gui_menu_remove_dict),
1998                              FALSE);
1999         gtk_widget_set_sensitive(GTK_WIDGET
2000                              (ws_gui_app->ws_gui_menu->ws_gui_menu_select_dict),
2001                              FALSE);
2002         gtk_widget_set_sensitive(GTK_WIDGET
2003                            (ws_gui_app->ws_gui_menu->ws_gui_menu_optimize_dict),
2004                            FALSE);
2005         }
2006
2007 }
2008
2009 /** sets sensitivity of menu items due to bookmark's mode
2010  *
2011  * @param user_data - user data set when the function was called 
2012  * @return
2013  */
2014 void ws_gui_set_bookmarks_sensitivity(gpointer user_data)
2015 {
2016         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
2017         int i;
2018         gchar *string;
2019         gchar *name;
2020
2021         if (ws_gui_app->bookmark_mode == TRUE)
2022         {
2023                 g_slist_free(ws_gui_app->directories_last);
2024                 ws_gui_app->directories_last = NULL;
2025                 ws_gui_app->directories_last = g_slist_alloc();
2026                 g_slist_free(ws_gui_app->directories);
2027                 ws_gui_app->directories = g_slist_alloc();
2028
2029                 ws_gui_app->directories = gconf_client_all_dirs(
2030                                                         ws_gui_app->client, 
2031                                                         GCONF_PATH, NULL);
2032                 
2033         for (i=0; i<g_slist_length(ws_gui_app->directories); i++)
2034         {
2035                 string = (gchar*)g_slist_nth_data(ws_gui_app->directories, i);
2036                 name = g_path_get_basename(string);
2037                 string = g_strconcat(string, "/active", NULL);
2038                 
2039                 if (gconf_client_get_bool(ws_gui_app->client, string, NULL) == TRUE) 
2040                 {
2041                         ws_gui_app->directories_last = 
2042                                 g_slist_append(ws_gui_app->directories_last, 
2043                                                string);
2044                 }
2045
2046                 if (strcmp(name, "bookmarks") == 0)
2047                 {
2048                         gconf_client_set_bool(ws_gui_app->client, 
2049                                               string, 
2050                                               TRUE, 
2051                                               NULL);
2052                 }
2053                 else
2054                 {
2055                         gconf_client_set_bool(ws_gui_app->client, 
2056                                               string, 
2057                                               FALSE, 
2058                                               NULL);
2059                 }
2060         }
2061
2062         //setting sensitivity of components
2063         gtk_widget_set_sensitive(
2064                 GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_open_bookmark),
2065                 FALSE);
2066         gtk_widget_set_sensitive(
2067                 GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_close_bookmark),
2068                 TRUE);
2069         gtk_widget_set_sensitive(
2070                GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_remove_bookmark),
2071                TRUE);
2072         gtk_widget_set_sensitive(
2073                 GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_add_bookmark),
2074                 FALSE);
2075         gtk_widget_set_sensitive(
2076                 GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_dictionaries),
2077                 FALSE);
2078         }
2079         else 
2080         {
2081         gchar *bookmarks_path = g_strconcat(GCONF_PATH, "/bookmarks/active", NULL);
2082         if (g_slist_length(ws_gui_app->directories_last) > 0)
2083         {
2084         gconf_client_set_bool(ws_gui_app->client, bookmarks_path, FALSE, NULL);
2085         }
2086         
2087         for (i=0; i<g_slist_length(ws_gui_app->directories_last); i++)
2088         {
2089                 string = (gchar*)g_slist_nth_data(ws_gui_app->directories_last, 
2090                                                   i);
2091                 
2092                 if (string != NULL)
2093                 {
2094                 gconf_client_set_bool(ws_gui_app->client, 
2095                                 string, 
2096                                 TRUE, 
2097                                 NULL);
2098                 }
2099         }
2100
2101         //setting sensitivity of components
2102         gtk_widget_set_sensitive(
2103                 GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_open_bookmark),
2104                 TRUE);
2105         gtk_widget_set_sensitive(
2106                 GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_close_bookmark),
2107                 FALSE);
2108         gtk_widget_set_sensitive(
2109                GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_remove_bookmark),
2110                FALSE);
2111         gtk_widget_set_sensitive(
2112                 GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_add_bookmark),
2113                 TRUE);
2114         gtk_widget_set_sensitive(
2115                 GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_dictionaries),
2116                 TRUE);
2117         }
2118 }
2119
2120 /** sets sensitivity of menu items due to user selection made
2121  *
2122  * @param user_data - user data set when the function was called 
2123  * @return
2124  */
2125 void ws_gui_set_bookmark_menu_items(GtkMenuItem *menuitem, gpointer user_data)
2126 {
2127         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
2128         if (ws_gui_app->bookmark_mode == TRUE && ws_gui_app->last_word != NULL)
2129         {
2130                gtk_widget_set_sensitive(
2131                GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_remove_bookmark),
2132                TRUE);
2133         }
2134         else
2135         {
2136                gtk_widget_set_sensitive(
2137                GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_remove_bookmark),
2138                FALSE);
2139         }
2140
2141         if (ws_gui_app->last_word == NULL || 
2142             (ws_gui_app->last_word[0] == '\0') ||
2143             ws_gui_app->bookmark_mode == TRUE)
2144         {
2145                gtk_widget_set_sensitive(
2146                GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_add_bookmark),
2147                FALSE);
2148         }
2149         else
2150         {
2151                gtk_widget_set_sensitive(
2152                GTK_WIDGET(ws_gui_app->ws_gui_menu->ws_gui_menu_add_bookmark),
2153                TRUE);
2154         }
2155 }
2156 /** reads gconf entries and sets variables with a proper data; it is necessary
2157  * for changing the bookmarks mode
2158  *
2159  * @param user_data - user data set when the function was called
2160  * @return
2161  */
2162 void ws_gui_read_active_dictionaries(gpointer user_data)
2163 {
2164         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
2165
2166         
2167         ws_gui_app->directories = gconf_client_all_dirs(ws_gui_app->client,
2168                                              GCONF_PATH,
2169                                              NULL);
2170 }
2171
2172 void ws_gui_clear_array(GArray *history)
2173 {
2174         if(history->len >= 0)
2175         {
2176                 history = g_array_remove_range(history, 0, history->len);
2177         }
2178 }
2179
2180 void ws_gui_view_cursor_changed(GtkTreeView *treeview, gpointer user_data)
2181 {
2182         WSGuiApp* ws_gui_app=(WSGuiApp*)user_data;
2183         gchar *string;
2184         if (gtk_tree_selection_get_selected(ws_gui_app->ws_gui_w_list->ws_gui_selection,
2185                                        &ws_gui_app->ws_gui_w_list->ws_gui_model,
2186                                        &ws_gui_app->ws_gui_w_list->ws_gui_iter))
2187         {
2188         gtk_tree_model_get(ws_gui_app->ws_gui_w_list->ws_gui_model,
2189                                    &ws_gui_app->ws_gui_w_list->ws_gui_iter,
2190                                    COL_WORD,
2191                                    &string,
2192                                    -1);
2193         if (string != NULL)
2194                 {
2195                 
2196                 ws_dbus_client_find_translation(ws_gui_app->dbus_data, string);
2197                 
2198                 
2199                 g_free(ws_gui_app->last_word);
2200                 ws_gui_app->last_word = NULL;
2201                 ws_gui_app->last_word = g_strdup (string);
2202
2203                 if (ws_gui_app->history_flag == FALSE)
2204                 {
2205                        ws_gui_history_add(string, ws_gui_app);
2206                 
2207                 }
2208                 g_free (string);
2209                 }
2210                 
2211         }
2212         
2213         
2214 }