fixes compile
[platform/upstream/ibus-libpinyin.git] / src / PYMain.cc
1 /* vim:set et ts=4 sts=4:
2  *
3  * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 #include <ibus.h>
26 #include <stdlib.h>
27 #include <locale.h>
28 #include "PYEngine.h"
29 #include "PYPointer.h"
30 #include "PYBus.h"
31 #include "PYConfig.h"
32 #include "PYPConfig.h"
33 #ifdef IBUS_BUILD_LIBPINYIN
34 #include "PYLibPinyin.h"
35 #endif
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
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
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
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 #ifdef IBUS_BUILD_LIBPINYIN
92     LibPinyinBackEnd::init ();
93 #endif
94
95 #ifdef IBUS_BUILD_LIBPINYIN
96     LibPinyinPinyinConfig::init (bus);
97     LibPinyinBopomofoConfig::init (bus);
98 #endif
99
100     g_signal_connect ((IBusBus *)bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL);
101
102     component = ibus_component_new ("org.freedesktop.IBus.Libpinyin",
103                                     N_("Libpinyin input method"),
104                                     VERSION,
105                                     "GPL",
106                                     "Peng Wu <alexepico@gmail.com>",
107                                     "https://github.com/libpinyin/ibus-libpinyin",
108                                     "",
109                                     "ibus-libpinyin");
110 #ifdef IBUS_BUILD_LIBPINYIN
111     ibus_component_add_engine (component,
112                                ibus_engine_desc_new ("libpinyin-debug",
113                                                      N_("Intelligent Pinyin (debug)"),
114                                                      N_("Intelligent Pinyin input method (debug)"),
115                                                      "zh_CN",
116                                                      "GPL",
117                                                      "Peng Huang <shawn.p.huang@gmail.com>\n"
118                                                      "Peng Wu <alexepico@gmail.com>\n"
119                                                      "BYVoid <byvoid1@gmail.com>",
120                                                      PKGDATADIR "/icons/ibus-pinyin.svg",
121                                                      "us"));
122     ibus_component_add_engine (component,
123                                ibus_engine_desc_new ("libbopomofo-debug",
124                                                      N_("Intelligent Bopomofo (debug)"),
125                                                      N_("Intelligent Bopomofo input method (debug)"),
126                                                      "zh_CN",
127                                                      "GPL",
128                                                      "BYVoid <byvoid1@gmail.com>\n"
129                                                      "Peng Wu <alexepico@gmail.com>\n"
130                                                      "Peng Huang <shawn.p.huang@gmail.com>",
131                                                      PKGDATADIR "/icons/ibus-bopomofo.svg",
132                                                      "us"));
133 #endif
134
135     factory = ibus_factory_new (ibus_bus_get_connection (bus));
136
137     if (ibus) {
138 #ifdef IBUS_BUILD_LIBPINYIN
139         ibus_factory_add_engine (factory, "libpinyin", IBUS_TYPE_PINYIN_ENGINE);
140         ibus_factory_add_engine (factory, "libbopomofo", IBUS_TYPE_PINYIN_ENGINE);
141 #endif
142         ibus_bus_request_name (bus, "org.freedesktop.IBus.Libpinyin", 0);
143     }
144     else {
145 #ifdef IBUS_BUILD_LIBPINYIN
146         ibus_factory_add_engine (factory, "libpinyin-debug", IBUS_TYPE_PINYIN_ENGINE);
147         ibus_factory_add_engine (factory, "libbopomofo-debug", IBUS_TYPE_PINYIN_ENGINE);
148 #endif
149         ibus_bus_register_component (bus, component);
150     }
151
152     ibus_main ();
153 }
154
155 #include <signal.h>
156
157 static void
158 sigterm_cb (int sig)
159 {
160 #ifdef IBUS_BUILD_LIBPINYIN
161     LibPinyinBackEnd::finalize ();
162 #endif
163     ::exit (EXIT_FAILURE);
164 }
165
166 static void
167 atexit_cb (void)
168 {
169 #ifdef IBUS_BUILD_LIBPINYIN
170     LibPinyinBackEnd::finalize ();
171 #endif
172 }
173
174 int
175 main (gint argc, gchar **argv)
176 {
177     GError *error = NULL;
178     GOptionContext *context;
179
180     setlocale (LC_ALL, "");
181
182     context = g_option_context_new ("- ibus pinyin engine component");
183
184     g_option_context_add_main_entries (context, entries, "ibus-libpinyin");
185
186     if (!g_option_context_parse (context, &argc, &argv, &error)) {
187         g_print ("Option parsing failed: %s\n", error->message);
188         exit (-1);
189     }
190
191     ::signal (SIGTERM, sigterm_cb);
192     ::signal (SIGINT, sigterm_cb);
193     g_atexit (atexit_cb);
194
195     start_component ();
196     return 0;
197 }