Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / wrapper / ibus / src / sunpinyin_property.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 <libintl.h>
37 #include <ibus.h>
38 #include "ibus_portable.h"
39 #include "sunpinyin_property.h"
40
41 #define N_(String) (String)
42 #define _(String)  gettext(String)
43
44 static const char *PROP_STATUS = "status";
45 static const char *PROP_LETTER = "full_letter";
46 static const char *PROP_PUNCT  = "full_punct";
47
48 PropertyInfo::PropertyInfo()
49     : label(NULL), tooltip(NULL)
50 {}
51
52 PropertyInfo::~PropertyInfo()
53 {}
54
55
56 SunPinyinProperty 
57 SunPinyinProperty::create_status_prop(ibus::Engine engine, bool state)
58 {
59     SunPinyinProperty prop(engine, PROP_STATUS);
60     prop.m_info[0].label = ibus_text_new_from_static_string("EN");
61     prop.m_info[0].icon  = IBUS_SUNPINYIN_ICON_DIR"/eng.svg";
62     prop.m_info[0].tooltip = ibus_text_new_from_static_string(_("Switch to Chinese input mode"));
63     prop.m_info[1].label = ibus_text_new_from_static_string("CN");
64     prop.m_info[1].icon  = IBUS_SUNPINYIN_ICON_DIR"/han.svg";
65     prop.m_info[1].tooltip = ibus_text_new_from_static_string(_("Switch to English input mode"));
66     prop.init(state);
67     
68     return prop;
69 }
70
71 SunPinyinProperty
72 SunPinyinProperty::create_letter_prop(ibus::Engine engine, bool state)
73 {
74     SunPinyinProperty prop(engine, PROP_LETTER);
75     prop.m_info[0].label = ibus_text_new_from_static_string("Aa");
76     prop.m_info[0].icon  = IBUS_SUNPINYIN_ICON_DIR"/halfwidth.svg";
77     prop.m_info[0].tooltip = ibus_text_new_from_static_string(_("Switch to full-width letter input mode"));
78     prop.m_info[1].label = ibus_text_new_from_static_string("Aa");
79     prop.m_info[1].icon  = IBUS_SUNPINYIN_ICON_DIR"/fullwidth.svg";
80     prop.m_info[1].tooltip = ibus_text_new_from_static_string(_("Switch to half-width letter input mode"));
81     prop.init(state);
82     return prop;
83 }
84
85 SunPinyinProperty 
86 SunPinyinProperty::create_punct_prop(ibus::Engine engine, bool state)
87 {
88     SunPinyinProperty prop(engine, PROP_PUNCT);
89     prop.m_info[0].label = ibus_text_new_from_static_string(",.");
90     prop.m_info[0].icon  = IBUS_SUNPINYIN_ICON_DIR"/enpunc.svg";
91     prop.m_info[0].tooltip = ibus_text_new_from_static_string(_("Switch to Chinese punctuation"));
92     prop.m_info[1].label = ibus_text_new_from_static_string(",。");
93     prop.m_info[1].icon  = IBUS_SUNPINYIN_ICON_DIR"/cnpunc.svg";
94     prop.m_info[1].tooltip = ibus_text_new_from_static_string(_("Switch to English punctuation"));
95     prop.init(state);
96     return prop;
97
98 }
99
100 SunPinyinProperty::SunPinyinProperty(ibus::Engine engine, const std::string& name)
101     : ibus::Property(
102         ibus_property_new(name.c_str(),
103                           PROP_TYPE_NORMAL,
104                           NULL, /* label */ NULL, /* icon */
105                           NULL, /* tooltip */ TRUE, /* sensitive */
106                           TRUE, /* visible */ PROP_STATE_UNCHECKED, /* state */
107                           NULL)),
108       m_engine(engine),
109       m_name(name),
110       m_state(false)
111 {}
112
113 SunPinyinProperty::~SunPinyinProperty()
114 {
115 }
116
117 bool
118 SunPinyinProperty::toggle(const std::string& name)
119 {
120     if (name == m_name) {
121         // called by ibus, simple toggle current state
122         update(!m_state);
123         return true;
124     }
125     return false;
126 }
127
128 void
129 SunPinyinProperty::update(bool state)
130 {
131     if (state == m_state)
132         return;
133     init(state);
134     ibus_engine_update_property(m_engine, *this);
135 }
136
137 void
138 SunPinyinProperty::init(bool state)
139 {
140     m_state = state;
141     int which = m_state ? 1 : 0;
142     PropertyInfo& info = m_info[which];
143     ibus_property_set_label(*this, info.label);
144     ibus_property_set_icon(*this, info.icon.c_str());
145     ibus_property_set_tooltip(*this, info.tooltip);
146     ibus_property_set_visible(*this, TRUE);
147     ibus_property_set_state(*this, state ? PROP_STATE_CHECKED : PROP_STATE_UNCHECKED);
148 }
149
150 bool
151 SunPinyinProperty::state() const
152 {
153     return m_state;
154 }
155
156 SetupLauncher::SetupLauncher()
157     : ibus::Property(
158         ibus_property_new("setup",
159                           PROP_TYPE_NORMAL,
160                           NULL, /* label */ NULL, /* icon */
161                           NULL, /* tooltip */ TRUE, /* sensitive */
162                           TRUE, /* visible */ PROP_STATE_UNCHECKED, /* state */
163                           NULL)),
164       m_name("setup")
165 {
166     m_info.label   = ibus_text_new_from_static_string(_("Preference"));
167     m_info.tooltip = ibus_text_new_from_static_string(_("Preference"));
168     m_info.icon    = IBUS_SUNPINYIN_ICON_DIR"/setup.svg";
169     init();
170 }
171
172 void
173 SetupLauncher::launch(const std::string& name)
174 {
175     if (m_name != name) return;
176     
177     GError *error = NULL;
178     gchar *argv[2] = { NULL, };
179         gchar *path;
180         const char* libexecdir;
181     
182         libexecdir = g_getenv("LIBEXECDIR");
183         if (libexecdir == NULL)
184             libexecdir = LIBEXECDIR;
185     
186         path = g_build_filename(libexecdir, "ibus-setup-sunpinyin", NULL);
187         argv[0] = path;
188         argv[1] = NULL;
189     gboolean success;
190     success = g_spawn_async (NULL, argv, NULL,
191                              G_SPAWN_SEARCH_PATH,
192                              NULL, NULL, NULL, &error);
193     if (!success) {
194         g_message("Unabled to launch \"%s\"", path);
195     }
196     g_free(path);
197 }
198
199 void
200 SetupLauncher::init()
201 {
202     ibus_property_set_label (*this, m_info.label);
203     ibus_property_set_icon (*this, m_info.icon.c_str());
204     ibus_property_set_tooltip (*this, m_info.tooltip);
205     ibus_property_set_visible (*this, TRUE);
206 }