1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
# -*- coding: utf-8 -*-
# vim: set noet ts=4:
#
# scim-python
#
# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
#
# $Id: $
#
import os.path as path
import scim
import gobject
import gtk
import gtk.gdk as gdk
import gtk.glade as glade
import sys
from gettext import dgettext
_ = lambda a : dgettext ("scim-python", a)
try:
from PYDict import *
except:
pathname = path.dirname (__file__)
pathname = path.abspath(pathname)
pathname = path.join (pathname, "../../engine/PinYin")
pathname = path.abspath(pathname)
sys.path.append(pathname)
from PYDict import *
RGB_COLOR = lambda c : ((c.red & 0xff00) << 8) + (c.green & 0xff00) + ((c.blue & 0xff00) >> 8)
GDK_COLOR = lambda c : gdk.Color (((c >> 16) & 0xff) * 256, ((c >> 8) & 0xff) * 256, (c & 0xff) * 256)
RGB = lambda r, g, b : ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)
class SetupUI ():
def __init__ (self, helper_info):
self._helper_info = helper_info
self._helper_agent = scim.HelperAgent ()
self._need_reload_config = False
self._options = {
"SupportGBK" : [True, self._checkbutton_op],
"ShuangPin" : [False, self._checkbutton_op],
"AutoCorrect" : [True, self._checkbutton_op],
"FuzzyPinYin" : [False, self._checkbutton_op],
"SpellCheck" : [True, self._checkbutton_op],
"UVToTemp" : [True, self._checkbutton_op],
"ShiftSelectCandidates" :
[True, self._checkbutton_op],
"CommaPageDownUp" : [True, self._checkbutton_op],
"EqualPageDownUp" : [True, self._checkbutton_op],
"AutoCommit" : [False, self._checkbutton_op],
"PhraseColor" : [RGB (0, 0, 0), self._colorbutton_op],
"NewPhraseColor" : [RGB (0xef, 0, 0), self._colorbutton_op],
"UserPhraseColor" : [RGB (0, 0, 0xbf), self._colorbutton_op],
"SpecialPhraseColor" :
[RGB (0, 0xbf, 0), self._colorbutton_op],
"EnglishPhraseColor" : [RGB (0, 0xbf, 0), self._colorbutton_op],
"ErrorEnglishPhraseColor" :
[RGB (0xef, 0, 0), self._colorbutton_op],
"PageSize" : [5, self._combobox_op, range (1, 10)],
"ShuangPinSchema" : ["MSPY", self._combobox_op, SHUANGPIN_SCHEMAS.keys ()],
}
def run (self, uuid, config, display):
self._config = config
self._uuid = uuid
self._display = display
self._init_agent ()
self._init_ui ()
self._load_config ()
gtk.main ()
if self._need_reload_config:
self._config.flush ()
self._helper_agent.reload_config ()
self._helper_agent.close_connection ()
def _combobox_op (self, name, opt, info):
widget = self._xml.get_widget (name)
if widget == None:
print >> sys.stderr, "Can not find widget %s" % name
return False
if opt == "read":
info[0] = self._read (name, info[0])
widget.set_active (info[2].index (info[0]))
return True
if opt == "write":
info[0] = info[2][widget.get_active ()]
self._write (name, info[0])
return True
if opt == "check":
return info[0] != info[2][widget.get_active ()]
if opt == "init":
model = gtk.ListStore (str)
for v in info[2]:
model.append ([_(str (v))])
widget.set_model (model)
return False
def _colorbutton_op (self, name, opt, info):
widget = self._xml.get_widget (name)
if widget == None:
print >> sys.stderr, "Can not find widget %s" % name
return False
if opt == "read":
info[0] = self._read (name, info[0])
widget.set_color (GDK_COLOR (info[0]))
return True
if opt == "write":
info[0] = RGB_COLOR (widget.get_color ())
self._write (name, info[0])
return True
if opt == "check":
return info[0] != RGB_COLOR (widget.get_color ())
return False
def _checkbutton_op (self, name, opt, info):
widget = self._xml.get_widget (name)
if widget == None:
print >> sys.stderr, "Can not find widget %s" % name
return False
if opt == "read":
info[0] = self._read (name, info[0])
widget.set_active (info[0])
return True
if opt == "write":
info[0] = widget.get_active ()
self._write (name, info[0])
return True
if opt == "check":
return info[0] != widget.get_active ()
return False
def _read (self, name, v):
return self._config.read ("/IMEngine/Python/PinYin/" + name, v)
def _write (self, name, v):
return self._config.write ("/IMEngine/Python/PinYin/" + name, v)
def _init_ui (self):
glade.textdomain("scim-python")
glade_file = path.join (path.dirname (__file__), "SetupUI.glade")
self._xml = glade.XML (glade_file)
self._window = self._xml.get_widget ("window_main")
for name, info in self._options.items ():
info[1] (name, "init", info)
info[1] (name, "read", info)
self._xml.signal_autoconnect (self)
self._window.show_all ()
def _init_agent (self):
fd = self._helper_agent.open_connection (self._helper_info, self._display)
if fd >= 0:
condition = gobject.IO_IN | gobject.IO_ERR | gobject.IO_HUP
gobject.io_add_watch (fd, condition, self.on_agent_event)
def _load_config (self):
for name, info in self._options.items ():
info[1] (name, "read", info)
def _save_config (self):
self._need_reload_config = True
for name, info in self._options.items ():
info[1] (name, "write", info)
def _query_changed (self):
for name, info in self._options.items ():
if info[1] (name, "check", info):
return True
return False
def _quit (self, need_confirm ):
if need_confirm == False:
gtk.main_quit ()
return True
else:
dlg = gtk.MessageDialog (self._window, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION,
gtk.BUTTONS_YES_NO, _("Are you sure to close Python PinYin Setup without save configure?"))
id = dlg.run ()
dlg.destroy ()
if id == gtk.RESPONSE_YES:
gtk.main_quit ()
return True
return False
def _optimize_user_db (self):
import sqlite3
dlg = gtk.MessageDialog (self._window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO,
gtk.BUTTONS_OK, _("The user phrases database will be reorganized! Please don't use python PinYin now."))
dlg.run ()
dlg.destroy ()
try:
db = sqlite3.connect (path.expanduser ("~/.scim/scim-python/pinyin/user.db"))
sqlstring = """
BEGIN TRANSACTION;
CREATE TABLE tmp AS SELECT * FROM py_phrase;
DELETE FROM py_phrase;
INSERT INTO py_phrase SELECT * FROM tmp ORDER BY ylen, y0, y1, y2, y3, yx, phrase;
DROP TABLE tmp;
COMMIT;
"""
db.executescript (sqlstring)
db.executescript ("VACUUM;")
except:
import traceback
traceback.print_exc ()
dlg.destroy ()
dlg = gtk.MessageDialog (self._window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO,
gtk.BUTTONS_OK, _("Reorganizing is over!"))
dlg.run ()
dlg.destroy ()
# events handlers
def on_window_main_delete_event (self, widget, event):
result = self._quit (True)
return True
def on_button_ok_clicked (self, button):
changed = self._query_changed ()
if changed:
self._save_config ()
self._quit (False)
def on_button_apply_clicked (self, button):
self._save_config ()
def on_button_cancel_clicked (self, button):
changed = self._query_changed ()
self._quit (changed)
def on_button_optimize_db_clicked (self, button):
self._optimize_user_db ()
def on_value_changed (self, widget, data = None):
if self._query_changed ():
self._xml.get_widget ("button_apply").set_sensitive (True)
else:
self._xml.get_widget ("button_apply").set_sensitive (False)
def on_agent_event (self, fd, condition):
if condition == gobject.IO_IN:
while self._helper_agent.has_pending_event ():
self._helper_agent.filter_event ()
return True
elif condition == gobject.IO_ERR or condition == gobject.IO_HUP:
gtk.main_quit ()
return False
return False
if __name__ == "__main__":
class CC:
def __init__ (self):
pass
def read (self, name, v):
return v
def write (self, name, v):
pass
def flush (self):
pass
helper_info = ("eebeecd7-cb22-48f4-8ced-70e42dad1a79", "", "", "", 1)
SetupUI (helper_info). run ("eebeecd7-cb22-48f4-8ced-70e42dad1a79", CC (), ":0.0")
|