clean up PYConfig.h/cc
[platform/upstream/ibus-libpinyin.git] / src / PYMain.cc
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., 675 Mass Ave, Cambridge, MA 02139, 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     PinyinConfig::init (bus);
95     BopomofoConfig::init (bus);
96 #ifdef IBUS_BUILD_LIBPINYIN
97     LibPinyinPinyinConfig::init (bus);
98     LibPinyinBopomofoConfig::init (bus);
99 #endif
100
101     g_signal_connect ((IBusBus *)bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL);
102
103     component = ibus_component_new ("org.freedesktop.IBus.Pinyin",
104                                     N_("Pinyin input method"),
105                                     VERSION,
106                                     "GPL",
107                                     "Peng Huang <shawn.p.huang@gmail.com>",
108                                     "http://code.google.com/p/ibus/",
109                                     "",
110                                     "ibus-pinyin");
111 #ifdef IBUS_BUILD_LIBPINYIN
112     ibus_component_add_engine (component,
113                                ibus_engine_desc_new ("libpinyin-debug",
114                                                      N_("Intelligent Pinyin (debug)"),
115                                                      N_("Intelligent Pinyin input method (debug)"),
116                                                      "zh_CN",
117                                                      "GPL",
118                                                      "Peng Huang <shawn.p.huang@gmail.com>\n"
119                                                      "Peng Wu <alexepico@gmail.com>\n"
120                                                      "BYVoid <byvoid1@gmail.com>",
121                                                      PKGDATADIR "/icons/ibus-pinyin.svg",
122                                                      "us"));
123     ibus_component_add_engine (component,
124                                ibus_engine_desc_new ("libbopomofo-debug",
125                                                      N_("Intelligent Bopomofo (debug)"),
126                                                      N_("Intelligent Bopomofo input method (debug)"),
127                                                      "zh_CN",
128                                                      "GPL",
129                                                      "BYVoid <byvoid1@gmail.com>\n"
130                                                      "Peng Wu <alexepico@gmail.com>\n"
131                                                      "Peng Huang <shawn.p.huang@gmail.com>",
132                                                      PKGDATADIR "/icons/ibus-bopomofo.svg",
133                                                      "us"));
134 #endif
135
136     factory = ibus_factory_new (ibus_bus_get_connection (bus));
137
138     if (ibus) {
139 #ifdef IBUS_BUILD_LIBPINYIN
140         ibus_factory_add_engine (factory, "libpinyin", IBUS_TYPE_PINYIN_ENGINE);
141         ibus_factory_add_engine (factory, "libbopomofo", IBUS_TYPE_PINYIN_ENGINE);
142 #endif
143         ibus_bus_request_name (bus, "org.freedesktop.IBus.Pinyin", 0);
144     }
145     else {
146 #ifdef IBUS_BUILD_LIBPINYIN
147         ibus_factory_add_engine (factory, "libpinyin-debug", IBUS_TYPE_PINYIN_ENGINE);
148         ibus_factory_add_engine (factory, "libbopomofo-debug", IBUS_TYPE_PINYIN_ENGINE);
149 #endif
150         ibus_bus_register_component (bus, component);
151     }
152
153     ibus_main ();
154 }
155
156 #include <signal.h>
157
158 static void
159 sigterm_cb (int sig)
160 {
161 #ifdef IBUS_BUILD_LIBPINYIN
162     LibPinyinBackEnd::finalize ();
163 #endif
164     ::exit (EXIT_FAILURE);
165 }
166
167 static void
168 atexit_cb (void)
169 {
170 #ifdef IBUS_BUILD_LIBPINYIN
171     LibPinyinBackEnd::finalize ();
172 #endif
173 }
174
175 int
176 main (gint argc, gchar **argv)
177 {
178     GError *error = NULL;
179     GOptionContext *context;
180
181     setlocale (LC_ALL, "");
182
183     context = g_option_context_new ("- ibus pinyin engine component");
184
185     g_option_context_add_main_entries (context, entries, "ibus-pinyin");
186
187     if (!g_option_context_parse (context, &argc, &argv, &error)) {
188         g_print ("Option parsing failed: %s\n", error->message);
189         exit (-1);
190     }
191
192     ::signal (SIGTERM, sigterm_cb);
193     ::signal (SIGINT, sigterm_cb);
194     g_atexit (atexit_cb);
195
196     start_component ();
197     return 0;
198 }