83b123f52b7af4532a54804a000affaa6ce302a6
[mdictionary] / src / dbus_wrapper / src / ws_dbus.c
1 /*
2 This library is free software; you can redistribute it and/or
3 modify it under the terms of the GNU Lesser General Public
4 License as published by the Free Software Foundation;
5 version 2.1 of the License.
6
7 This library is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 Lesser General Public License for more details.
11
12 You should have received a copy of the GNU Lesser General Public
13 License along with this library; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
16 Copyright 2006 ComArch S.A.
17 */
18
19 #include <ws_dbus.h>
20
21 struct _WSDBusCBData
22 {
23         GQuark id;
24         gpointer handler;
25         gpointer user_data;
26 };
27
28 typedef struct _WSDBusCBData WSDBusCBData;
29
30
31 static void ws_dbus_fill_message (DBusMessage *msg, void *data) 
32 {
33
34         guint i;
35          gchar *buffer;
36         GArray *temp;
37
38         temp = (GArray *) data;
39
40         for (i=0;((i<temp->len) && (i<250));++i)
41         {
42                 buffer = g_array_index (temp, gchar *, i);
43                 dbus_message_append_args (msg, 
44                                 DBUS_TYPE_STRING, &buffer,
45                                 DBUS_TYPE_INVALID);
46         };
47         g_debug ("DBUS: Added %d words\n", i);
48
49 };
50
51 static gint ws_dbus_cb_handler (const gchar * interface,
52                                 const gchar * method,
53                                 GArray * arguments,
54                                 gpointer data,
55                                 osso_rpc_t * retval)
56
57 {
58         WSDBusCBData cb_data;
59         GArray *dbus_cb_data;
60         GQuark temp;
61         guint i;
62         ws_dbus_cb cb;
63         dbus_cb_data = (GArray *) data;
64         gpointer tmp;
65         retval = NULL;
66         
67         g_debug ("DBUS: Method:  %s\n", method);
68
69         temp = g_quark_try_string (method);
70
71         if (temp != 0) 
72
73         {
74
75                 cb_data = g_array_index (dbus_cb_data, WSDBusCBData, temp-1);
76
77                 if (cb_data.id == temp) 
78                 {
79                         cb = (ws_dbus_cb) cb_data.handler;
80                         tmp = cb_data.user_data;
81                 }
82
83                 else 
84
85                 {
86
87                         for (i=0; i < dbus_cb_data->len; ++i)     
88                         {
89                                 cb_data = g_array_index (dbus_cb_data,
90                                                          WSDBusCBData,
91                                                          i);
92                                 if (cb_data.id == temp) 
93                                 {
94                                         cb = (ws_dbus_cb) cb_data.handler;
95                                         tmp = cb_data.user_data;
96                                 };
97                         };
98                 };
99         }
100
101         else 
102         {
103                 g_debug ("Error in function %s, couldn't find the signal %s\n",
104                         __FUNCTION__,
105                         method);
106                 return OSSO_ERROR;
107         };
108
109         if (cb != NULL) 
110         {
111                 g_debug ("\nDBUS: Running callback for %s\n", method);
112                 cb (NULL, arguments, tmp); 
113                 retval = DBUS_TYPE_INVALID;
114                 return OSSO_OK;
115         }
116         else
117         {
118                 g_debug ("DBUS: No callback defined for this function\n");
119                 return OSSO_ERROR;
120         };
121
122 };
123
124
125 static void ws_dbus_libosso_errors (osso_return_t result)
126 {
127         switch (result) 
128         {
129                 case OSSO_OK: 
130                         g_debug ("All OK\n");
131                         break;
132                         
133                 case OSSO_ERROR: 
134                         g_debug ("Ordinary Error\n");
135                         break;
136
137                 case OSSO_INVALID: 
138                         g_debug ("At least one parameter is invalid\n");
139                         break;
140
141                 case OSSO_RPC_ERROR: 
142                         g_debug ("Osso RPC method returned an error\n");
143                         break;
144
145                 case OSSO_ERROR_NAME: 
146                         g_debug ("Error Name\n");
147                         break;
148
149                 case OSSO_ERROR_NO_STATE: 
150                         g_debug ("No state file found to read\n");
151                         break;
152
153                 case OSSO_ERROR_STATE_SIZE: 
154                         g_debug("The size of the given structure");
155                         g_debug(" is different from the saved size\n");
156                         break;
157
158         };
159 };
160
161 WSDBusData * ws_dbus_create (gchar *name, gchar *version)
162 {
163         gchar *cb_table[] = {"find_word",
164                              "find_translation",
165                              "return_words",
166                              "return_translations",
167                              "signal",
168                              "update_progressbar",
169                              "search_in_history",
170                              "add_bookmark",
171                              "remove_bookmark",
172                              "send_message",
173                              "extract_dictionary",
174                              "return_extracted_dict",
175                              "search_home_applet",
176                              NULL};
177
178         guint array_size = 0;
179         guint i;
180
181         while (cb_table[array_size] != NULL) array_size++;
182
183         WSDBusData *temp;
184         WSDBusCBData temp_cb_data;
185         GQuark temp_quark;
186
187         temp = (gpointer) g_try_malloc (sizeof (WSDBusData));
188
189         g_debug ("\nDBUS: ");
190
191         if (temp == NULL)
192         {
193                 g_debug ("Error in function %s - cannot allocate memory\n",
194                         __FUNCTION__);
195                 g_assert_not_reached();
196         }
197
198         else g_debug ("Memory allocation successful\n");
199
200         temp->name = g_strconcat (name, NULL);
201         temp->version = g_strconcat (version, NULL);
202
203         temp->cb_data = g_array_new (TRUE, TRUE, sizeof (WSDBusCBData));      
204     
205         for (i = 0; i < array_size; i++)
206         {
207                 temp_quark = g_quark_try_string (cb_table[i]);
208
209                 if (temp_quark != 0)
210                 {
211                      g_debug(
212                      "DBUS: \"%s\" - signal has already been registered - %d\n",
213                       cb_table[i],
214                       g_quark_from_string(cb_table[i]));                        
215                 }
216
217                 else
218                 {
219                         temp_quark = g_quark_from_string (cb_table[i]);
220                         g_debug ("DBUS: Assigning signal \"%s\" an id %d\n",
221                                 cb_table [i],
222                                 temp_quark);
223                         temp_cb_data.id = temp_quark;
224                         temp_cb_data.handler = NULL;
225                         temp_cb_data.user_data = NULL;
226                         g_array_append_val (temp->cb_data, temp_cb_data);
227                 };
228
229         };
230
231         g_debug ("DBUS: Signals were successfully added\n");
232
233         return temp;
234 };
235
236 /* added by Gadomska Anna - HISA */
237 WSDBusStatus ws_dbus_server_search_home_applet (WSDBusData * ws_dbus_data, gchar * word)
238 {
239        osso_return_t result;
240        osso_rpc_t *retval;
241
242        retval = g_try_malloc (sizeof (osso_rpc_t));
243
244        if (retval == NULL) 
245        {
246                g_debug("DBUS: Error in function %s:",__FUNCTION__);
247                g_debug(" Couldn't allocate memory for message's return value\n");
248        };
249
250        result = osso_rpc_run (ws_dbus_data->context, 
251                               ws_dbus_data->remote_service, 
252                               ws_dbus_data->remote_object, 
253                               ws_dbus_data->remote_iface, 
254                               "search_home_applet", 
255                               retval,
256                               DBUS_TYPE_STRING,
257                               word,
258                               DBUS_TYPE_INVALID);
259
260        g_debug ("\nDBUS: %s: ", __FUNCTION__);
261
262        ws_dbus_libosso_errors (result);
263
264        if (result != OSSO_OK)
265        {
266                g_debug ("Error message: %s\n", retval->value.s);
267                 osso_rpc_free_val (retval);
268                g_free (retval);
269                return WS_DBUS_STATUS_ERROR;
270        };
271
272         osso_rpc_free_val (retval);
273        g_free (retval);
274        return WS_DBUS_STATUS_OK;
275 };
276
277 WSDBusStatus ws_dbus_config (WSDBusData * ws_dbus_data,
278                              WSDBusConfig field,
279                              gchar *value)
280
281 {
282         if (ws_dbus_data == NULL) 
283         {
284                 g_debug ("\nDBUS: Error in function %s - ws_dbus_data is NULL\n",
285                         __FUNCTION__);
286         };
287
288         switch (field) 
289         {
290                 case WS_DBUS_CONFIG_SERVICE :
291                         ws_dbus_data->service = g_strconcat (value, NULL);
292                         break;
293                 case WS_DBUS_CONFIG_OBJECT :
294                         ws_dbus_data->object = g_strconcat (value, NULL);
295                         break;
296                 case WS_DBUS_CONFIG_IFACE :
297                         ws_dbus_data->iface = g_strconcat (value, NULL);
298                         break;
299                 case WS_DBUS_CONFIG_REMOTE_SERVICE :
300                         ws_dbus_data->remote_service = g_strconcat (value, NULL);
301                         break;
302                 case WS_DBUS_CONFIG_REMOTE_OBJECT :
303                         ws_dbus_data->remote_object = g_strconcat (value, NULL);
304                         break;
305                 case WS_DBUS_CONFIG_REMOTE_IFACE :
306                         ws_dbus_data->remote_iface = g_strconcat (value, NULL);
307                         break;
308         };
309
310         return WS_DBUS_STATUS_OK;
311 };
312
313 WSDBusStatus ws_dbus_connect (WSDBusData * ws_dbus_data)
314
315 {
316         osso_return_t result;
317
318         ws_dbus_data->context = osso_initialize (ws_dbus_data->name,
319                                                  ws_dbus_data->version,
320                                                  FALSE,
321                                                  NULL);
322         osso_rpc_set_timeout (ws_dbus_data->context, 100);
323
324         result = osso_rpc_set_cb_f(ws_dbus_data->context, 
325                                 ws_dbus_data->service, 
326                                 ws_dbus_data->object, 
327                                 ws_dbus_data->iface,
328                                 ws_dbus_cb_handler, 
329                                 ws_dbus_data->cb_data);
330
331         if (result == OSSO_OK) return WS_DBUS_STATUS_OK;
332         else return WS_DBUS_STATUS_ERROR;
333 };
334
335 void ws_dbus_destroy (WSDBusData * ws_dbus_data)
336
337 {
338         if (ws_dbus_data == NULL) 
339         {
340             g_debug ("\nDBUS: Error in function %s - cannot free osso_context\n",
341                     __FUNCTION__);
342             g_free (ws_dbus_data);
343             g_assert_not_reached();
344         };
345
346         g_debug ("\nDBUS deinitialization by %s:\n---------------------------\n",
347                 ws_dbus_data->service);
348         osso_deinitialize (ws_dbus_data->context);
349         g_debug ("| Deinitializing osso context    |\n");
350         if (ws_dbus_data->cb_data != NULL) 
351         {
352                 g_array_free (ws_dbus_data->cb_data, TRUE);
353                 g_debug ("| Freeing callback pointers list |\n");
354         };
355
356         g_free (ws_dbus_data->name);
357         g_free (ws_dbus_data->version);
358         g_free (ws_dbus_data->service);
359         g_free (ws_dbus_data->object);
360         g_free (ws_dbus_data->iface);
361
362         g_free (ws_dbus_data->remote_service);
363         g_free (ws_dbus_data->remote_object);
364         g_free (ws_dbus_data->remote_iface);
365
366         g_free (ws_dbus_data);
367         g_debug ("| Freeing WSDBusData structure   |\n----------------------\n");
368 };
369
370
371 WSDBusStatus ws_dbus_set_cb (WSDBusData * ws_dbus_data,
372                              gchar * detailed_signal,
373                              gpointer c_func,
374                              gpointer user_data)
375
376 {
377         WSDBusCBData cb_data;
378         GQuark temp;
379         guint i;
380
381         temp = g_quark_try_string  (detailed_signal);
382
383         if (temp == 0) 
384         {
385               g_debug ("DBUS:\"%s\"-signal not defined or invalid signal name\n",
386                       detailed_signal);
387               return WS_DBUS_STATUS_ERROR;
388         }
389
390         else
391         {
392                 for (i = 0; i < ws_dbus_data->cb_data->len; ++i)
393                 {
394                         cb_data = g_array_index (ws_dbus_data->cb_data,
395                                                  WSDBusCBData,
396                                                  i);
397                         if (cb_data.id == temp)
398
399                         {
400                                 cb_data.handler = c_func;
401                                 cb_data.user_data = user_data;
402                                 g_array_insert_val(ws_dbus_data->cb_data,
403                                                    i,
404                                                    cb_data);
405                                 g_array_remove_index (ws_dbus_data->cb_data,
406                                                       i+1);
407                         };
408
409                 };
410
411         };
412
413         return WS_DBUS_STATUS_OK;
414 };
415
416 WSDBusStatus ws_dbus_notify (WSDBusData * ws_dbus_data,
417                              WSDBusNotify ws_dbus_info)
418 {
419         osso_return_t result;
420         osso_rpc_t *retval;
421
422         retval = g_try_malloc (sizeof (osso_rpc_t));
423
424         if (retval == NULL) 
425         {
426                 g_debug("DBUS: Error in function %s: ",__FUNCTION__);
427                 g_debug("Couldn't allocate memory for message's return value\n");
428         };
429
430         result = osso_rpc_run (ws_dbus_data->context, 
431                                ws_dbus_data->remote_service, 
432                                ws_dbus_data->remote_object, 
433                                ws_dbus_data->remote_iface, 
434                                "signal", 
435                                retval,
436                                DBUS_TYPE_INT32,
437                                ws_dbus_info,
438                                DBUS_TYPE_INVALID);       
439         g_debug ("\nDBUS: %s: ", __FUNCTION__);
440         ws_dbus_libosso_errors (result);
441
442         if (result != OSSO_OK)
443         {
444                 g_debug ("Error message: %s\n", retval->value.s);
445                 if (/*strcmp */g_ascii_strcasecmp(retval->value.s,
446                             "No reply within specified time") != 0) 
447                 {
448                         osso_rpc_free_val (retval);
449                         g_free (retval);
450                         return WS_DBUS_STATUS_ERROR;
451                 };
452         };
453
454         osso_rpc_free_val (retval);
455         g_free (retval);
456         return WS_DBUS_STATUS_OK;
457 };
458
459 WSDBusStatus ws_dbus_client_find_word (WSDBusData * ws_dbus_data, gchar * word)
460 {
461         osso_return_t result;
462         osso_rpc_t *retval;
463
464         retval = g_try_malloc (sizeof (osso_rpc_t));
465
466         if (retval == NULL) 
467         {
468                 g_debug("DBUS: Error in function %s:",__FUNCTION__);
469                 g_debug(" Couldn't allocate memory for message's return value\n");
470         };
471
472         result = osso_rpc_run (ws_dbus_data->context, 
473                                ws_dbus_data->remote_service, 
474                                ws_dbus_data->remote_object, 
475                                ws_dbus_data->remote_iface, 
476                                "find_word", 
477                                retval,
478                                DBUS_TYPE_STRING,
479                                word,
480                                DBUS_TYPE_INVALID);
481
482         g_debug ("\nDBUS: %s: ", __FUNCTION__);
483
484         ws_dbus_libosso_errors (result);
485
486         if (result != OSSO_OK)
487         {
488                 g_debug ("Error message: %s\n", retval->value.s);
489                  osso_rpc_free_val (retval);
490                 g_free (retval);
491                 return WS_DBUS_STATUS_ERROR;
492         };
493
494         osso_rpc_free_val (retval);
495         g_free (retval);
496         return WS_DBUS_STATUS_OK;
497 };
498
499 WSDBusStatus ws_dbus_client_find_translation (WSDBusData * ws_dbus_data,
500                                               gchar * word)
501 {
502         osso_return_t result;
503         osso_rpc_t *retval;
504         
505         retval = g_try_malloc (sizeof (osso_rpc_t));
506
507         if (retval == NULL) 
508         {
509                 g_debug("DBUS: Error in function %s: ",__FUNCTION__);
510                 g_debug(" Couldn't allocate memory for message's return value\n");
511         };
512
513         result = osso_rpc_run (ws_dbus_data->context, 
514                                ws_dbus_data->remote_service, 
515                                ws_dbus_data->remote_object, 
516                                ws_dbus_data->remote_iface, 
517                                "find_translation", 
518                                retval,
519                                DBUS_TYPE_STRING,
520                                word,
521                                DBUS_TYPE_INVALID);
522
523         g_debug ("\nDBUS: %s: ", __FUNCTION__);
524
525         ws_dbus_libosso_errors (result);
526
527         if (result != OSSO_OK)
528         {
529                 g_debug ("Error message: %s\n", retval->value.s);
530                  osso_rpc_free_val (retval);
531                 g_free (retval);
532                 return WS_DBUS_STATUS_ERROR;
533         };
534
535          osso_rpc_free_val (retval);
536         g_free (retval);
537         return WS_DBUS_STATUS_OK;
538 };
539
540 WSDBusStatus ws_dbus_client_search_in_history (WSDBusData* ws_dbus_data,
541                                                gchar* word)
542 {
543         osso_return_t result;
544         osso_rpc_t *retval;
545
546         retval = g_try_malloc (sizeof (osso_rpc_t));
547
548         if (retval == NULL) 
549         {
550                 g_debug("DBUS: Error in function %s:",__FUNCTION__);
551                 g_debug(" Couldn't allocate memory for message's return value\n");
552         };
553
554         result = osso_rpc_run (ws_dbus_data->context, 
555                                ws_dbus_data->remote_service, 
556                                ws_dbus_data->remote_object, 
557                                ws_dbus_data->remote_iface, 
558                                "search_in_history", 
559                                retval,
560                                DBUS_TYPE_STRING,
561                                word,
562                                DBUS_TYPE_INVALID);
563
564         g_debug ("\nDBUS: %s: ", __FUNCTION__);
565
566         ws_dbus_libosso_errors (result);
567
568         if (result != OSSO_OK)
569         {
570                 g_debug ("Error message: %s\n", retval->value.s);
571                 osso_rpc_free_val (retval);
572                 g_free (retval);
573                 return WS_DBUS_STATUS_ERROR;
574         };
575
576         osso_rpc_free_val (retval);
577         g_free (retval);
578         return WS_DBUS_STATUS_OK;
579 };
580
581 WSDBusStatus ws_dbus_server_return_words  (WSDBusData * ws_dbus_data, 
582                                                 GArray * words)
583
584 {
585         osso_return_t result;
586         osso_rpc_t *retval;
587
588         retval = g_try_malloc (sizeof (osso_rpc_t));
589
590         if (retval == NULL) 
591         {
592                 g_debug("DBUS: Error in function %s:",__FUNCTION__);
593                 g_debug(" Couldn't allocate memory for message's return value\n");
594         };
595
596         result = osso_rpc_run_with_argfill (ws_dbus_data->context, 
597                                             ws_dbus_data->remote_service, 
598                                             ws_dbus_data->remote_object, 
599                                             ws_dbus_data->remote_iface, 
600                                             "return_words",
601                                             retval,
602                                             ws_dbus_fill_message,
603                                             words);
604         g_debug ("\nDBUS: %s: ", __FUNCTION__);
605
606         ws_dbus_libosso_errors (result);
607
608         if (result != OSSO_OK)
609         {
610                 g_debug ("Error message: %s\n", retval->value.s);
611                  osso_rpc_free_val (retval);
612                 g_free (retval);
613                 return WS_DBUS_STATUS_ERROR;
614         };
615
616         osso_rpc_free_val (retval);
617         g_free (retval);
618         return WS_DBUS_STATUS_OK;
619 };
620
621 WSDBusStatus ws_dbus_server_return_words_last(WSDBusData * ws_dbus_data,
622                                               GArray * words)
623
624 {
625         osso_return_t result;
626         osso_rpc_t *retval;
627
628         retval = g_try_malloc (sizeof (osso_rpc_t));
629
630         if (retval == NULL) 
631         {
632                 g_debug("DBUS: Error in function %s:",__FUNCTION__);
633                 g_debug(" Couldn't allocate memory for message's return value\n");
634         };
635
636         result = osso_rpc_run_with_argfill (ws_dbus_data->context, 
637                                             ws_dbus_data->remote_service, 
638                                             ws_dbus_data->remote_object, 
639                                             ws_dbus_data->remote_iface, 
640                                             "return_words", 
641                                             retval,
642                                             ws_dbus_fill_message,
643                                             words);
644
645         g_debug ("\nDBUS: %s: ", __FUNCTION__);
646
647         ws_dbus_libosso_errors (result);
648
649         if (result != OSSO_OK)
650         {
651                 g_debug ("Error message: %s\n", retval->value.s);
652                  osso_rpc_free_val (retval);
653                 g_free (retval);
654                 return WS_DBUS_STATUS_ERROR;
655         };
656
657          osso_rpc_free_val (retval);
658         g_free (retval);
659         return WS_DBUS_STATUS_OK;
660 };
661
662 WSDBusStatus ws_dbus_server_return_translations(WSDBusData * ws_dbus_data,
663                                                 gchar * translation)
664
665 {
666         osso_return_t result;
667         osso_rpc_t *retval;
668
669         retval = g_try_malloc (sizeof (osso_rpc_t));
670
671         if (retval == NULL) 
672         {
673                 g_debug("DBUS: Error in function %s:",  __FUNCTION__);
674                 g_debug(" Couldn't allocate memory for message's return value\n");
675         };
676
677         result = osso_rpc_run (ws_dbus_data->context, 
678                                ws_dbus_data->remote_service, 
679                                ws_dbus_data->remote_object, 
680                                ws_dbus_data->remote_iface, 
681                                "return_translations", 
682                                retval,
683                                DBUS_TYPE_STRING,
684                                translation,
685                                DBUS_TYPE_INVALID);
686
687         g_debug ("\nDBUS: %s: ", __FUNCTION__);
688
689         ws_dbus_libosso_errors (result);
690
691         if (result != OSSO_OK)
692         {
693                 g_debug ("Error message: %s\n", retval->value.s);
694                  osso_rpc_free_val (retval);
695                 g_free (retval);
696                 return WS_DBUS_STATUS_ERROR;
697         };
698
699          osso_rpc_free_val (retval);
700         g_free (retval);
701         return WS_DBUS_STATUS_OK;
702 };
703
704 WSDBusStatus ws_dbus_send_ptrtodata(WSDBusData * ws_dbus_data,
705                                     gchar *method,
706                                     gulong data_address)
707
708 {
709         osso_return_t result;
710         osso_rpc_t *retval;
711
712         retval = g_try_malloc (sizeof (osso_rpc_t));
713
714         if (retval == NULL) 
715         {
716                 g_debug("DBUS: Error in function %s:",__FUNCTION__);
717                 g_debug(" Couldn't allocate memory for message's return value\n");
718         };
719
720         result = osso_rpc_run (ws_dbus_data->context, 
721                                ws_dbus_data->remote_service, 
722                                ws_dbus_data->remote_object, 
723                                ws_dbus_data->remote_iface, 
724                                method, 
725                                retval,
726                                DBUS_TYPE_INT32,
727                                data_address,
728                                DBUS_TYPE_INVALID);
729
730         g_debug ("\nDBUS: %s: ", __FUNCTION__);
731
732         ws_dbus_libosso_errors (result);
733
734         if (result != OSSO_OK)
735         {
736                 g_debug ("Error message: %s\n", retval->value.s);
737                  osso_rpc_free_val (retval);
738                 g_free (retval);
739                 return WS_DBUS_STATUS_ERROR;
740         };
741
742          osso_rpc_free_val (retval);
743         g_free (retval);
744         return WS_DBUS_STATUS_OK;
745
746 };
747
748
749 WSDBusStatus ws_dbus_server_return_translations_last(WSDBusData * ws_dbus_data,
750                                                      gchar * translation)
751
752 {
753         osso_return_t result;
754         osso_rpc_t *retval;
755
756         retval = g_try_malloc (sizeof (osso_rpc_t));
757
758         if (retval == NULL) 
759         {
760                 g_debug("DBUS: Error in function %s:", __FUNCTION__);
761                 g_debug(" Couldn't allocate memory for message's return value\n");
762         };
763
764         result = osso_rpc_run(ws_dbus_data->context, 
765                               ws_dbus_data->remote_service, 
766                               ws_dbus_data->remote_object, 
767                               ws_dbus_data->remote_iface, 
768                               "return_translations", 
769                               retval,
770                               DBUS_TYPE_STRING,
771                               translation,
772                               DBUS_TYPE_INVALID);
773
774         g_debug ("\nDBUS: %s: ", __FUNCTION__);
775
776         ws_dbus_libosso_errors (result);
777
778         if (result != OSSO_OK)
779         {
780                 g_debug ("Error message: %s\n", retval->value.s);
781                  osso_rpc_free_val (retval);
782                 g_free (retval);
783                 return WS_DBUS_STATUS_ERROR;
784         };
785
786         osso_rpc_free_val (retval);
787         g_free (retval);
788         return WS_DBUS_STATUS_OK;
789 };
790
791 gpointer ws_dbus_concat_data (GArray *garray)
792 {
793         guint i;
794         gchar temp_len = 0;
795         gulong total_len = 0;
796         gpointer temp = NULL, start;
797         gchar *array;
798
799         for (i=0;i<garray->len;++i)
800         {
801                 array = g_array_index (garray, gchar *, i);
802                 g_debug ("DBUS: %s: text added: %s\n", __FUNCTION__, array);
803                 temp = start;
804                 temp_len = strlen (array);
805                 g_debug ("length: %d\n", temp_len);
806                 start = g_malloc (total_len + temp_len + 1);
807                 memcpy (start, temp, total_len);
808                 if (i > 0) g_free (temp);
809                 temp = start + total_len;
810                 memcpy (temp, &temp_len, 1);
811                 memcpy (temp+1, array, temp_len);
812                 total_len = total_len + temp_len + 1;
813         };
814         return start;
815 };
816
817 GArray * ws_dbus_split_data (gpointer buffer)
818 {
819         gchar *temp;
820         gchar *str;
821         guint len;
822         guint total_len = 0;
823         guint buffer_len;
824         guint i = 0;
825         GArray *garray;
826
827         temp = buffer;
828
829         buffer_len = strlen (buffer);
830
831         g_debug ("Buffer length %d\n", strlen (buffer));
832         garray = g_array_new (TRUE, TRUE, sizeof(gchar *));
833
834         while (temp[0] != 0)
835         {
836                 len = temp[0];
837                 total_len = total_len + len;
838                 str = g_malloc (len + 1);
839                 memcpy (str, temp+1, len);
840                 str[len] = 0;
841                 temp = temp + len + 1;
842                 g_debug ("Received text: %s, %d\n", str, len);
843                 g_array_append_val (garray, str);
844                 ++i;
845         };
846
847         return garray;
848 };
849
850 WSDBusStatus ws_dbus_server_update_progressbar (WSDBusData * ws_dbus_data,
851                                          double progress)
852
853 {
854         osso_return_t result;
855         osso_rpc_t *retval;
856
857         retval = g_try_malloc (sizeof (osso_rpc_t));
858
859         if (retval == NULL) 
860         {
861                 g_debug("DBUS: Error in function %s:", __FUNCTION__);
862                 g_debug(" Couldn't allocate memory for message's return value\n");
863         };
864
865         result = osso_rpc_run (ws_dbus_data->context, 
866                                ws_dbus_data->remote_service, 
867                                ws_dbus_data->remote_object, 
868                                ws_dbus_data->remote_iface, 
869                                "update_progressbar", 
870                                retval,
871                                DBUS_TYPE_DOUBLE,
872                                progress,
873                                DBUS_TYPE_INVALID);
874
875         g_debug ("\nDBUS: %s: ", __FUNCTION__);
876
877         ws_dbus_libosso_errors (result);
878
879         if (result != OSSO_OK)
880         {
881                 g_debug ("Error message: %s\n", retval->value.s);
882                  osso_rpc_free_val (retval);
883                 g_free (retval);
884                 return WS_DBUS_STATUS_ERROR;
885         };
886
887         osso_rpc_free_val (retval);
888         g_free (retval);
889         return WS_DBUS_STATUS_OK;
890 };
891
892 WSDBusStatus ws_dbus_client_add_bookmark (WSDBusData * ws_dbus_data, gchar * word, gchar *translation)
893
894 {
895         osso_return_t result;
896         osso_rpc_t *retval;
897
898         retval = g_try_malloc (sizeof (osso_rpc_t));
899
900         if (retval == NULL) 
901         {
902                 g_debug("DBUS: Error in function %s:",__FUNCTION__);
903                 g_debug(" Couldn't allocate memory for message's return value\n");
904         };
905
906         result = osso_rpc_run (ws_dbus_data->context, 
907                                ws_dbus_data->remote_service, 
908                                ws_dbus_data->remote_object, 
909                                ws_dbus_data->remote_iface, 
910                                "add_bookmark", 
911                                retval,
912                                DBUS_TYPE_STRING,
913                                word,
914                                DBUS_TYPE_STRING,
915                                translation,
916                                DBUS_TYPE_INVALID);
917
918         g_debug ("\nDBUS: %s: ", __FUNCTION__);
919
920         ws_dbus_libosso_errors (result);
921
922         if (result != OSSO_OK)
923         {
924                 g_debug ("Error message: %s\n", retval->value.s);
925                  osso_rpc_free_val (retval);
926                 g_free (retval);
927                 return WS_DBUS_STATUS_ERROR;
928         };
929
930          osso_rpc_free_val (retval);
931         g_free (retval);
932         return WS_DBUS_STATUS_OK;
933 };
934
935 WSDBusStatus ws_dbus_client_remove_bookmark (WSDBusData * ws_dbus_data, gchar * word)
936
937 {
938         osso_return_t result;
939         osso_rpc_t *retval;
940
941         retval = g_try_malloc (sizeof (osso_rpc_t));
942
943         if (retval == NULL) 
944         {
945                 g_debug("DBUS: Error in function %s:",__FUNCTION__);
946                 g_debug(" Couldn't allocate memory for message's return value\n");
947         };
948
949         result = osso_rpc_run (ws_dbus_data->context, 
950                                ws_dbus_data->remote_service, 
951                                ws_dbus_data->remote_object, 
952                                ws_dbus_data->remote_iface, 
953                                "remove_bookmark", 
954                                retval,
955                                DBUS_TYPE_STRING,
956                                word,
957                                DBUS_TYPE_INVALID);
958
959         g_debug ("\nDBUS: %s: ", __FUNCTION__);
960
961         ws_dbus_libosso_errors (result);
962
963         if (result != OSSO_OK)
964         {
965                 g_debug ("Error message: %s\n", retval->value.s);
966                  osso_rpc_free_val (retval);
967                 g_free (retval);
968                 return WS_DBUS_STATUS_ERROR;
969         };
970
971          osso_rpc_free_val (retval);
972         g_free (retval);
973         return WS_DBUS_STATUS_OK;
974 };
975
976 WSDBusStatus ws_dbus_server_send_message (WSDBusData * ws_dbus_data, gchar * word)
977
978 {
979         osso_return_t result;
980         osso_rpc_t *retval;
981
982         retval = g_try_malloc (sizeof (osso_rpc_t));
983
984         if (retval == NULL) 
985         {
986                 g_debug("DBUS: Error in function %s:",__FUNCTION__);
987                 g_debug(" Couldn't allocate memory for message's return value\n");
988         };
989
990         result = osso_rpc_run (ws_dbus_data->context, 
991                                ws_dbus_data->remote_service, 
992                                ws_dbus_data->remote_object, 
993                                ws_dbus_data->remote_iface, 
994                                "send_message", 
995                                retval,
996                                DBUS_TYPE_STRING,
997                                word,
998                                DBUS_TYPE_INVALID);
999
1000         g_debug ("\nDBUS: %s: ", __FUNCTION__);
1001
1002         ws_dbus_libosso_errors (result);
1003
1004         if (result != OSSO_OK)
1005         {
1006                 g_debug ("Error message: %s\n", retval->value.s);
1007                  osso_rpc_free_val (retval);
1008                 g_free (retval);
1009                 return WS_DBUS_STATUS_ERROR;
1010         };
1011
1012          osso_rpc_free_val (retval);
1013         g_free (retval);
1014         return WS_DBUS_STATUS_OK;
1015 };
1016
1017 WSDBusStatus ws_dbus_client_extract_dictionary(WSDBusData * ws_dbus_data, 
1018                                         gchar* path)
1019
1020 {
1021         osso_return_t result;
1022         osso_rpc_t *retval;
1023
1024         retval = g_try_malloc (sizeof (osso_rpc_t));
1025
1026         if (retval == NULL) 
1027         {
1028                 g_debug("DBUS: Error in function %s:",__FUNCTION__);
1029                 g_debug(" Couldn't allocate memory for message's return value\n");
1030         };
1031
1032         result = osso_rpc_run(ws_dbus_data->context, 
1033                                 ws_dbus_data->remote_service, 
1034                                 ws_dbus_data->remote_object, 
1035                                 ws_dbus_data->remote_iface, 
1036                                 "extract_dictionary", 
1037                                 retval,
1038                                 DBUS_TYPE_STRING,
1039                                 path,
1040                                 DBUS_TYPE_INVALID);
1041
1042         g_debug ("\nDBUS: %s: ", __FUNCTION__);
1043
1044         ws_dbus_libosso_errors (result);
1045
1046         if (result != OSSO_OK)
1047         {
1048                 g_debug ("Error message: %s\n", retval->value.s);
1049                 osso_rpc_free_val (retval);
1050                 g_free (retval);
1051                 return WS_DBUS_STATUS_ERROR;
1052         };
1053
1054         osso_rpc_free_val (retval);
1055         g_free (retval);
1056         return WS_DBUS_STATUS_OK;
1057 };
1058
1059 WSDBusStatus ws_dbus_server_return_extracted_dict(WSDBusData* ws_dbus_data, 
1060                                         gchar* path)
1061 {
1062         osso_return_t result;
1063         osso_rpc_t *retval;
1064
1065         retval = g_try_malloc (sizeof (osso_rpc_t));
1066
1067         if (retval == NULL) 
1068         {
1069                 g_debug("DBUS: Error in function %s:", __FUNCTION__);
1070                 g_debug(" Couldn't allocate memory for message's return value\n");
1071         };
1072
1073         result = osso_rpc_run(ws_dbus_data->context, 
1074                               ws_dbus_data->remote_service, 
1075                               ws_dbus_data->remote_object, 
1076                               ws_dbus_data->remote_iface, 
1077                               "return_extracted_dict", 
1078                               retval,
1079                               DBUS_TYPE_STRING,
1080                               path,
1081                               DBUS_TYPE_INVALID);
1082
1083         g_debug ("\nDBUS: %s: ", __FUNCTION__);
1084
1085         ws_dbus_libosso_errors (result);
1086
1087         if (result != OSSO_OK)
1088         {
1089                 g_debug ("Error message: %s\n", retval->value.s);
1090                  osso_rpc_free_val (retval);
1091                 g_free (retval);
1092                 return WS_DBUS_STATUS_ERROR;
1093         };
1094
1095         osso_rpc_free_val (retval);
1096         g_free (retval);
1097         return WS_DBUS_STATUS_OK;       
1098 };