1 # vim:set et sts=4 sw=4:
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library 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 Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program; if not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 # Boston, MA 02111-1307 USA
32 import keyboardshortcut
34 from xdg import BaseDirectory
37 from enginecombobox import EngineComboBox
38 from enginetreeview import EngineTreeView
39 from engineabout import EngineAbout
41 _ = lambda a : gettext.dgettext("ibus", a)
66 def __flush_gtk_events(self):
67 while gtk.events_pending():
71 super(Setup, self).__init__()
72 locale.bind_textdomain_codeset("ibus", "UTF-8")
73 glade.textdomain("ibus")
74 glade_file = path.join(path.dirname(__file__), "./setup.glade")
75 self.__xml = glade.XML(glade_file)
81 # add icon search path
82 self.__window = self.__xml.get_widget("window_preferences")
83 self.__window.connect("delete-event", gtk.main_quit)
85 self.__button_close = self.__xml.get_widget("button_close")
86 self.__button_close.connect("clicked", gtk.main_quit)
89 self.__checkbutton_auto_start = self.__xml.get_widget("checkbutton_auto_start")
90 self.__checkbutton_auto_start.set_active(self.__is_auto_start())
91 self.__checkbutton_auto_start.connect("toggled", self.__checkbutton_auto_start_toggled_cb)
95 self.__config = self.__bus.get_config()
96 shortcuts = self.__config.get_value(
97 "general/hotkey", "trigger",
98 ibus.CONFIG_GENERAL_SHORTCUT_TRIGGER_DEFAULT)
100 button = self.__xml.get_widget("button_trigger")
101 entry = self.__xml.get_widget("entry_trigger")
102 entry.set_text("; ".join(shortcuts))
103 button.connect("clicked", self.__shortcut_button_clicked_cb,
104 N_("trigger"), "general/hotkey", "trigger", entry)
107 shortcuts = self.__config.get_value(
108 "general/hotkey", "next_engine",
109 ibus.CONFIG_GENERAL_SHORTCUT_NEXT_ENGINE_DEFAULT)
110 button = self.__xml.get_widget("button_next_engine")
111 entry = self.__xml.get_widget("entry_next_engine")
112 entry.set_text("; ".join(shortcuts))
113 button.connect("clicked", self.__shortcut_button_clicked_cb,
114 N_("next input method"), "general/hotkey", "next_engine", entry)
117 shortcuts = self.__config.get_value(
118 "general/hotkey", "prev_engine",
119 ibus.CONFIG_GENERAL_SHORTCUT_PREV_ENGINE_DEFAULT)
120 button = self.__xml.get_widget("button_prev_engine")
121 entry = self.__xml.get_widget("entry_prev_engine")
122 entry.set_text("; ".join(shortcuts))
123 button.connect("clicked", self.__shortcut_button_clicked_cb,
124 N_("previous input method"), "general/hotkey", "prev_engine", entry)
126 # lookup table orientation
127 self.__combobox_lookup_table_orientation = self.__xml.get_widget("combobox_lookup_table_orientation")
128 self.__combobox_lookup_table_orientation.set_active(
129 self.__config.get_value("panel", "lookup_table_orientation", 0))
130 self.__combobox_lookup_table_orientation.connect("changed",
131 self.__combobox_lookup_table_orientation_changed_cb)
134 self.__combobox_panel_show = self.__xml.get_widget("combobox_panel_show")
135 self.__combobox_panel_show.set_active(
136 self.__config.get_value("panel", "show", 1))
137 self.__combobox_panel_show.connect("changed", self.__combobox_panel_show_changed_cb)
140 self.__checkbutton_custom_font = self.__xml.get_widget("checkbutton_custom_font")
141 self.__checkbutton_custom_font.set_active(
142 self.__config.get_value("panel", "use_custom_font", False))
143 self.__checkbutton_custom_font.connect("toggled", self.__checkbutton_custom_font_toggled_cb)
145 self.__label_custom_font = self.__xml.get_widget("label_custom_font")
146 self.__fontbutton_custom_font = self.__xml.get_widget("fontbutton_custom_font")
147 if self.__config.get_value("panel", "use_custom_font", False):
148 self.__label_custom_font.set_sensitive(True)
149 self.__fontbutton_custom_font.set_sensitive(True)
151 self.__label_custom_font.set_sensitive(False)
152 self.__fontbutton_custom_font.set_sensitive(False)
153 font_name = gtk.settings_get_default().get_property("gtk-font-name")
154 font_name = unicode(font_name, "utf-8")
155 font_name = self.__config.get_value("panel", "custom_font", font_name)
156 self.__fontbutton_custom_font.connect("notify::font-name", self.__fontbutton_custom_font_notify_cb)
157 self.__fontbutton_custom_font.set_font_name(font_name)
160 self.__engines = self.__bus.list_engines()
161 self.__combobox = EngineComboBox(self.__engines)
162 self.__combobox.show()
163 self.__xml.get_widget("alignment_engine_combobox").add(self.__combobox)
166 for e in self.__engines:
168 engine_names = self.__config.get_value("general", "preload_engines", [])
170 for n in engine_names:
172 engines.append(tmp_dict[n])
173 self.__treeview = EngineTreeView(engines)
174 self.__treeview.show()
175 self.__xml.get_widget("scrolledwindow_engine_treeview").add(self.__treeview)
177 self.__treeview.connect("changed", self.__treeview_changed_cb)
179 button = self.__xml.get_widget("button_engine_add")
180 button.connect("clicked",
181 lambda *args:self.__treeview.append_engine(self.__combobox.get_active_engine()))
182 button = self.__xml.get_widget("button_engine_remove")
183 button.connect("clicked", lambda *args:self.__treeview.remove_engine())
184 button = self.__xml.get_widget("button_engine_up")
185 button.connect("clicked", lambda *args:self.__treeview.move_up_engine())
187 button = self.__xml.get_widget("button_engine_down")
188 button.connect("clicked", lambda *args:self.__treeview.move_down_engine())
190 button = self.__xml.get_widget("button_engine_about")
191 button.connect("clicked", self.__button_engine_about_cb)
193 def __button_engine_about_cb(self, button):
194 engine = self.__treeview.get_select_engine()
195 about = EngineAbout(engine)
199 def __treeview_changed_cb(self, treeview):
200 engines = self.__treeview.get_engines()
201 engine_names = map(lambda e: e.name, engines)
202 self.__config.set_list("general", "preload_engines", engine_names, "s")
204 def __init_bus(self):
206 self.__bus = ibus.Bus()
207 # self.__bus.connect("config-value-changed", self.__config_value_changed_cb)
208 # self.__bus.connect("config-reloaded", self.__config_reloaded_cb)
209 # self.__bus.config_add_watch("/general")
210 # self.__bus.config_add_watch("/general/hotkey")
211 # self.__bus.config_add_watch("/panel")
213 while self.__bus == None:
214 message = _("IBus daemon is not started. Do you want to start it now?")
215 dlg = gtk.MessageDialog(type = gtk.MESSAGE_QUESTION,
216 buttons = gtk.BUTTONS_YES_NO,
217 message_format = message)
220 self.__flush_gtk_events()
221 if id != gtk.RESPONSE_YES:
223 pid = os.spawnlp(os.P_NOWAIT, "ibus-daemon", "ibus-daemon")
226 self.__bus = ibus.Bus()
229 message = _("IBus has been started! "
230 "If you can not use IBus, please add below lines in $HOME/.bashrc, and relogin your desktop.\n"
231 " export GTK_IM_MODULE=ibus\n"
232 " export XMODIFIERS=@im=ibus\n"
233 " export QT_IM_MODULE=ibus"
235 dlg = gtk.MessageDialog(type = gtk.MESSAGE_INFO,
236 buttons = gtk.BUTTONS_OK,
237 message_format = message)
240 self.__flush_gtk_events()
242 def __shortcut_button_clicked_cb(self, button, name, section, _name, entry):
243 buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK)
244 title = _("Select keyboard shortcut for %s") % _(name)
245 dialog = keyboardshortcut.KeyboardShortcutSelectionDialog(buttons = buttons, title = title)
246 text = entry.get_text()
248 shortcuts = text.split("; ")
251 dialog.set_shortcuts(shortcuts)
253 shortcuts = dialog.get_shortcuts()
255 if id != gtk.RESPONSE_OK:
257 self.__config.set_list(section, _name, shortcuts, "s")
258 entry.set_text("; ".join(shortcuts))
261 def __item_started_column_toggled_cb(self, cell, path_str, model):
264 iter = model.get_iter_from_string(path_str)
265 data = model.get_value(iter, COLUMN_DATA)
267 # do something with the value
268 if data[DATA_STARTED] == False:
270 self.__bus.register_start_engine(data[DATA_LANG], data[DATA_NAME])
272 dlg = gtk.MessageDialog(type = gtk.MESSAGE_ERROR,
273 buttons = gtk.BUTTONS_CLOSE,
274 message_format = str(e))
277 self.__flush_gtk_events()
281 self.__bus.register_stop_engine(data[DATA_LANG], data[DATA_NAME])
283 dlg = gtk.MessageDialog(type = gtk.MESSAGE_ERROR,
284 buttons = gtk.BUTTONS_CLOSE,
285 message_format = str(e))
288 self.__flush_gtk_events()
290 data[DATA_STARTED] = not data[DATA_STARTED]
293 model.set(iter, COLUMN_ENABLE, data[DATA_STARTED])
295 def __item_preload_column_toggled_cb(self, cell, path_str, model):
298 iter = model.get_iter_from_string(path_str)
299 data = model.get_value(iter, COLUMN_DATA)
301 data[DATA_PRELOAD] = not data[DATA_PRELOAD]
302 engine = "%s:%s" % (data[DATA_LANG], data[DATA_NAME])
304 if data[DATA_PRELOAD]:
305 if engine not in self.__preload_engines:
306 self.__preload_engines.add(engine)
307 self.__config.set_list("general", "preload_engines", list(self.__preload_engines), "s")
309 if engine in self.__preload_engines:
310 self.__preload_engines.remove(engine)
311 self.__config.set_list("general", "preload_engines", list(self.__preload_engines), "s")
314 model.set(iter, COLUMN_PRELOAD, data[DATA_PRELOAD])
316 def __is_auto_start(self):
317 link_file = path.join(BaseDirectory.xdg_config_home, "autostart/ibus.desktop")
318 ibus_desktop = path.join(os.getenv("IBUS_PREFIX"), "share/applications/ibus.desktop")
320 if not path.exists(link_file):
322 if not path.islink(link_file):
324 if path.realpath(link_file) != ibus_desktop:
328 def __checkbutton_auto_start_toggled_cb(self, button):
329 auto_start_dir = path.join(BaseDirectory.xdg_config_home, "autostart")
330 if not path.isdir(auto_start_dir):
331 os.makedirs(auto_start_dir)
333 link_file = path.join(BaseDirectory.xdg_config_home, "autostart/ibus.desktop")
334 ibus_desktop = path.join(os.getenv("IBUS_PREFIX"), "share/applications/ibus.desktop")
340 if self.__checkbutton_auto_start.get_active():
341 os.symlink(ibus_desktop, link_file)
343 def __combobox_lookup_table_orientation_changed_cb(self, combobox):
344 self.__config.set_value(
345 "panel", "lookup_table_orientation",
346 self.__combobox_lookup_table_orientation.get_active())
348 def __combobox_panel_show_changed_cb(self, combobox):
349 self.__config.set_value(
351 self.__combobox_panel_show.get_active())
353 def __checkbutton_custom_font_toggled_cb(self, button):
354 if self.__checkbutton_custom_font.get_active():
355 self.__label_custom_font.set_sensitive(True)
356 self.__fontbutton_custom_font.set_sensitive(True)
357 self.__config.set_value("panel", "use_custom_font", True)
359 self.__label_custom_font.set_sensitive(False)
360 self.__fontbutton_custom_font.set_sensitive(False)
361 self.__config.set_value("panel", "use_custom_font", False)
363 def __fontbutton_custom_font_notify_cb(self, button, arg):
364 font_name = self.__fontbutton_custom_font.get_font_name()
365 font_name = unicode(font_name, "utf-8")
366 self.__config.set_value("panel", "custom_font", font_name)
368 def __config_value_changed_cb(self, bus, section, name, value):
371 def __config_reloaded_cb(self, bus):
374 def __sigusr1_cb(self, *args):
375 self.__window.present()
378 self.__window.show_all()
379 signal.signal(signal.SIGUSR1, self.__sigusr1_cb)
382 if __name__ == "__main__":