1 /* vim:set et ts=4 sts=4:
2  *
3  * ibus-pinyin - The Chinese PinYin engine for IBus
4  *
5  * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 
26 #include <PyZy/InputContext.h>
27 #include <ibus.h>
28 #include <locale.h>
29 #include <stdlib.h>
30 #include <string>
31 
32 #include "PYBus.h"
33 #include "PYConfig.h"
34 #include "PYEngine.h"
35 #include "PYPointer.h"
36 
37 using namespace PY;
38 
39 #define N_(text) text
40 
41 static Pointer<IBusFactory> factory;
42 
43 /* options */
44 static gboolean ibus = FALSE;
45 static gboolean verbose = FALSE;
46 
47 static void
show_version_and_quit(void)48 show_version_and_quit (void)
49 {
50     g_print ("%s - Version %s\n", g_get_application_name (), VERSION);
51     exit (EXIT_SUCCESS);
52 }
53 
54 static const GOptionEntry entries[] =
55 {
56     { "version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
57         (gpointer) show_version_and_quit, "Show the application's version.", NULL },
58     { "ibus",    'i', 0, G_OPTION_ARG_NONE, &ibus, "component is executed by ibus", NULL },
59     { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "verbose", NULL },
60     { NULL },
61 };
62 
63 
64 static void
ibus_disconnected_cb(IBusBus * bus,gpointer user_data)65 ibus_disconnected_cb (IBusBus  *bus,
66                       gpointer  user_data)
67 {
68     g_debug ("bus disconnected");
69     ibus_quit ();
70 }
71 
72 
73 static void
start_component(void)74 start_component (void)
75 {
76     Pointer<IBusComponent> component;
77 
78     ibus_init ();
79     Bus bus;
80 
81     if (!bus.isConnected ()) {
82         g_warning ("Can not connect to ibus!");
83         exit (0);
84     }
85 
86     if (!ibus_bus_get_config (bus)) {
87         g_warning ("IBus config component is not ready!");
88         exit (0);
89     }
90 
91     PinyinConfig::init (bus);
92     BopomofoConfig::init (bus);
93     {
94       gchar *cache_dir =
95           g_build_filename (g_get_user_cache_dir (), "ibus", "pinyin", NULL);
96       gchar *config_dir =
97           g_build_filename (g_get_user_config_dir (), "ibus", "pinyin", NULL);
98       PyZy::InputContext::init (cache_dir, config_dir);
99       g_free (cache_dir);
100       g_free (config_dir);
101     }
102 
103     g_signal_connect ((IBusBus *)bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL);
104 
105     component = ibus_component_new ("org.freedesktop.IBus.Pinyin",
106                                     N_("Pinyin input method"),
107                                     VERSION,
108                                     "GPL",
109                                     "Peng Huang <shawn.p.huang@gmail.com>",
110                                     "http://code.google.com/p/ibus/",
111                                     "",
112                                     "ibus-pinyin");
113     ibus_component_add_engine (component,
114             ibus_engine_desc_new_varargs ("name", "pinyin-debug",
115                                           "longname", N_("Pinyin (debug)"),
116                                           "description", N_("Pinyin input method (debug)"),
117                                           "language", "zh_CN",
118                                           "license", "GPL",
119                                           "author", "Peng Huang <shawn.p.huang@gmail.com>\n"
120                                                     "BYVoid <byvoid1@gmail.com>",
121                                           "icon", PKGDATADIR "/icons/ibus-pinyin.svg",
122                                           "layout", "us",
123                                           "symbol", "\u4e2d",
124                                           NULL));
125     ibus_component_add_engine (component,
126             ibus_engine_desc_new_varargs ("name", "bopomofo-debug",
127                                           "longname", N_("Bopomofo (debug)"),
128                                           "description", N_("Bopomofo input method (debug)"),
129                                           "language", "zh_CN",
130                                           "license", "GPL",
131                                           "author", "BYVoid <byvoid1@gmail.com>\n"
132                                                     "Peng Huang <shawn.p.huang@gmail.com>",
133                                           "icon", PKGDATADIR "/icons/ibus-bopomofo.svg",
134                                           "layout", "us",
135                                           "symbol", "\u4e2d",
136                                           NULL));
137 
138     factory = ibus_factory_new (ibus_bus_get_connection (bus));
139 
140     if (ibus) {
141         ibus_factory_add_engine (factory, "pinyin", IBUS_TYPE_PINYIN_ENGINE);
142         ibus_factory_add_engine (factory, "bopomofo", IBUS_TYPE_PINYIN_ENGINE);
143         ibus_bus_request_name (bus, "org.freedesktop.IBus.Pinyin", 0);
144     }
145     else {
146         ibus_factory_add_engine (factory, "pinyin-debug", IBUS_TYPE_PINYIN_ENGINE);
147         ibus_factory_add_engine (factory, "bopomofo-debug", IBUS_TYPE_PINYIN_ENGINE);
148         ibus_bus_register_component (bus, component);
149     }
150 
151     ibus_main ();
152 }
153 
154 #include <signal.h>
155 
156 static void
sigterm_cb(int sig)157 sigterm_cb (int sig)
158 {
159     PyZy::InputContext::finalize ();
160     ::exit (EXIT_FAILURE);
161 }
162 
163 static void
atexit_cb(void)164 atexit_cb (void)
165 {
166     PyZy::InputContext::finalize ();
167 }
168 
169 int
main(gint argc,gchar ** argv)170 main (gint argc, gchar **argv)
171 {
172     GError *error = NULL;
173     GOptionContext *context;
174 
175     setlocale (LC_ALL, "");
176 
177     context = g_option_context_new ("- ibus pinyin engine component");
178 
179     g_option_context_add_main_entries (context, entries, "ibus-pinyin");
180 
181     if (!g_option_context_parse (context, &argc, &argv, &error)) {
182         g_print ("Option parsing failed: %s\n", error->message);
183         exit (-1);
184     }
185 
186     ::signal (SIGTERM, sigterm_cb);
187     ::signal (SIGINT, sigterm_cb);
188     g_atexit (atexit_cb);
189 
190     start_component ();
191     return 0;
192 }
193