Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / wrapper / ibus / src / main.cpp
1 /*
2  * Copyright (c) 2009 Kov Chai <tchaikov@gmail.com>
3  *
4  * The contents of this file are subject to the terms of either the GNU Lesser
5  * General Public License Version 2.1 only ("LGPL") or the Common Development and
6  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
7  * file except in compliance with the License. You can obtain a copy of the CDDL at
8  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
9  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the 
10  * specific language governing permissions and limitations under the License. When
11  * distributing the software, include this License Header Notice in each file and
12  * include the full text of the License in the License file as well as the
13  * following notice:
14  * 
15  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
16  * (CDDL)
17  * For Covered Software in this distribution, this License shall be governed by the
18  * laws of the State of California (excluding conflict-of-law provisions).
19  * Any litigation relating to this License shall be subject to the jurisdiction of
20  * the Federal Courts of the Northern District of California and the state courts
21  * of the State of California, with venue lying in Santa Clara County, California.
22  * 
23  * Contributor(s):
24  * 
25  * If you wish your version of this file to be governed by only the CDDL or only
26  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
27  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
28  * license." If you don't indicate a single choice of license, a recipient has the
29  * option to distribute your version of this file under either the CDDL or the LGPL
30  * Version 2.1, or to extend the choice of license to its licensees as provided
31  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
32  * Version 2 license, then the option applies only if the new code is made subject
33  * to such option by the copyright holder. 
34  */
35
36 #include <locale.h>
37 #include <libintl.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <ibus.h>
42 #include "engine.h"
43 #include "ibus_common.h"
44 #include "sunpinyin_config.h"
45
46 #define N_(String) (String)
47 #define _(String)  gettext(String)
48
49 static ibus::Factory factory;
50
51 // options
52 static gboolean by_ibus = FALSE;
53 static gboolean verbose = FALSE;
54
55 static const GOptionEntry entries[] = 
56 {
57     { "ibus",    'i', 0, G_OPTION_ARG_NONE, &by_ibus, "component is executed by ibus", NULL },
58     { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "verbose", NULL },
59     { NULL },
60 };
61
62
63 static void
64 ibus_disconnected_cb (IBusBus  *bus,
65                       gpointer  user_data)
66 {
67     ibus_quit ();
68 }
69
70 IBusBus *bus = NULL;
71 ibus::Component component;
72
73 static void
74 init ()
75 {
76     ibus_init ();
77     bus = ibus_bus_new ();
78     g_object_ref_sink(bus);
79     
80     if (!ibus_bus_is_connected (bus)) {
81         g_warning("Can not connect to ibus");
82         exit (0);
83     }
84     
85     g_signal_connect (bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL);
86         
87     IBusConfig* config = ibus_bus_get_config(bus);
88     g_object_ref_sink(config);
89     
90     SunPinyinConfig::set_config(config);
91
92     
93     component = ibus_component_new ("org.freedesktop.IBus.SunPinyin",
94                                     "SunPinyin2",
95                                     "0.1.0",
96                                     "LGPL/CDDL",
97                                     "Kov Chai <tchaikov@gmail.com>",
98                                     "http://code.google.com/p/sunpinyin/",
99                                     "",
100                                     "ibus-sunpinyin");
101     ibus_component_add_engine (component,
102                                ibus_engine_desc_new ("sunpinyin",
103                                                      "SunPinyin",
104                                                      _("Simplified Chinese Input Method developed by SUN"),
105                                                      "zh_CN",
106                                                      "LGPL/CDDL",
107                                                      "Kov Chai <tchaikov@gmail.com>",
108                                                      IBUS_SUNPINYIN_ICON_DIR"/sunpinyin-logo.png",
109                                                      "en"));
110     
111     factory = ibus_factory_new (ibus_bus_get_connection (bus));
112     ibus_factory_add_engine (factory, "sunpinyin", IBUS_TYPE_SUNPINYIN_ENGINE);
113
114     if (by_ibus) {
115         ibus_bus_request_name (bus, "org.freedesktop.IBus.SunPinyin", 0);
116     } else {
117         ibus_bus_register_component (bus, component);
118     }
119     ibus_main ();
120 }
121
122 int main(int argc, char *argv[])
123 {
124
125     setlocale (LC_ALL, "");
126     bindtextdomain (GETTEXT_PACKAGE, IBUS_SUNPINYIN_LOCALEDIR);
127     textdomain (GETTEXT_PACKAGE);
128     
129     GOptionContext* context;
130     context = g_option_context_new ("- ibus sunpinyin engine component");
131     g_option_context_add_main_entries (context, entries, "ibus-sunpinyin");
132     GError *error = NULL;
133     if (!g_option_context_parse (context, &argc, &argv, &error)) {
134         g_print ("Option parsing failed: %s\n", error->message);
135         return -1;
136     }
137     // mask SIGTERM so that the destroy() method has the chance to be called
138     // in case user quits X session.
139     sighold(SIGTERM);
140     init ();
141 }