Tizen 2.1 base
[external/enchant.git] / src / zemberek / zemberek.cpp
1 /* Copyright (C) 2006 Barış Metin <baris@pardus.org.tr>
2  * Copyright (C) 2007 Serkan Kaba <serkan_kaba@yahoo.com>
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, Dom Lachowicz
20  * gives permission to link the code of this program with
21  * non-LGPL Spelling Provider libraries (eg: a MSFT Office
22  * spell checker backend) and distribute linked combinations including
23  * the two.  You must obey the GNU Lesser General Public License in all
24  * respects for all of the code used other than said providers.  If you modify
25  * this file, you may extend this exception to your version of the
26  * file, but you are not obligated to do so.  If you do not wish to
27  * do so, delete this exception statement from your version.
28  */
29
30 #include "zemberek.h"
31
32 bool zemberek_service_is_running ()
33 {
34   DBusGConnection *connection;
35   DBusGProxy *proxy;
36
37   GError *Error = NULL;
38   g_type_init ();
39
40   connection = dbus_g_bus_get (DBUS_BUS_SYSTEM,
41                                &Error);
42   if (connection == NULL) {
43       g_error_free (Error);
44       return false;
45   }
46   proxy = dbus_g_proxy_new_for_name_owner (connection,
47                                      "net.zemberekserver.server.dbus",
48                                      "/net/zemberekserver/server/dbus/ZemberekDbus",
49                                      "net.zemberekserver.server.dbus.ZemberekDbusInterface",
50                                      &Error);
51
52   dbus_g_connection_unref (connection);
53   if (proxy == NULL) {
54     return false;
55   }
56
57    g_object_unref (proxy);
58    return true;
59 }
60
61 Zemberek::Zemberek()
62   : connection(NULL), proxy(NULL)
63 {
64   GError *Error = NULL;
65   g_type_init ();
66
67   connection = dbus_g_bus_get (DBUS_BUS_SYSTEM,
68                                &Error);
69   if (connection == NULL) {
70       g_error_free (Error);
71       throw "couldn't connect to the system bus";
72   }
73   proxy = dbus_g_proxy_new_for_name (connection,
74                                      "net.zemberekserver.server.dbus",
75                                      "/net/zemberekserver/server/dbus/ZemberekDbus",
76                                      "net.zemberekserver.server.dbus.ZemberekDbusInterface");
77
78   if (proxy == NULL) {
79     throw "couldn't connect to the Zemberek service";
80   }
81 }
82
83
84 Zemberek::~Zemberek()
85 {
86     if(proxy)
87             g_object_unref (proxy);
88     if(connection)
89             dbus_g_connection_unref (connection);
90 }
91
92
93 int Zemberek::checkWord(const char* word) const
94 {
95     gboolean result;
96     GError *Error = NULL;
97     if (!dbus_g_proxy_call (proxy, "kelimeDenetle", &Error,
98         G_TYPE_STRING,word,G_TYPE_INVALID,
99         G_TYPE_BOOLEAN, &result, G_TYPE_INVALID)) {
100         g_error_free (Error);
101         return -1;
102     }
103     else {
104         if (result)
105                 return 0;
106         else
107                 return 1;
108     }
109 }
110
111
112 char** Zemberek::suggestWord(const char* word, size_t *out_n_suggs)
113 {
114     char** suggs;
115     GError *Error = NULL;
116     if (!dbus_g_proxy_call (proxy, "oner", &Error,
117         G_TYPE_STRING,word,G_TYPE_INVALID,
118         G_TYPE_STRV, &suggs,G_TYPE_INVALID)) {
119         g_error_free (Error);
120         return NULL;
121     }
122     *out_n_suggs = g_strv_length(suggs);
123     return suggs;
124 }