PageRenderTime 506ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/python/engine/XingMa/XingMa.py

http://scim-python.googlecode.com/
Python | 1365 lines | 1141 code | 78 blank | 146 comment | 159 complexity | 89bfb9b85227f2726f49eb7b7bf568e0 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # vim: set noet ts=4:
  3. #
  4. # scim-python
  5. #
  6. # Copyright (c) 2008-2008 Yu Yuwei <acevery@gmail.com>
  7. #
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2 of the License, or (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this program; if not, write to the
  21. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  22. # Boston, MA 02111-1307 USA
  23. #
  24. # $Id: $
  25. #
  26. import scim
  27. from scim import IMEngine
  28. from scim import IMEngineFactory
  29. from scim import Property
  30. from scim import Attribute
  31. from scim import KeyCode
  32. from scim import KeyMask
  33. import scim.ascii as ascii
  34. import XMSQLiteDB
  35. import XMDict
  36. import re
  37. patt_edit = re.compile (r'(.*)###(.*)###(.*)')
  38. patt_uncommit = re.compile (r'(.*)@@@(.*)')
  39. from gettext import dgettext
  40. _ = lambda a : dgettext ("scim-python", a)
  41. N_ = lambda a : a
  42. class Editor:
  43. '''Hold user inputs chars and preedit string'''
  44. def __init__ (self,phrase_table_index,valid_input_chars, max_key_length, database, parser = XMDict.Parse, deparser = XMDict.Deparse, max_length = 64):
  45. self.db = database
  46. self._pt = phrase_table_index
  47. self._parser = parser
  48. self._deparser = deparser
  49. self._max_key_len = int(max_key_length)
  50. self._max_length = max_length
  51. self._valid_input_chars = valid_input_chars
  52. #
  53. # below vals will be reset in self.clear()
  54. #
  55. # we hold this: [str,str,...]
  56. # self._chars: hold user input in xingma mode (valid,invalid,prevalid)
  57. self._chars = [[],[],[]]
  58. #self._t_chars: hold total input for xingma mode for input check
  59. self._t_chars = []
  60. # self._u_chars: hold user input but not manual comitted chars
  61. self._u_chars = []
  62. # self._xmkey_list: hold XingMa_Key objects transform from user input chars
  63. self._xmkey_list = []
  64. # self._strings: hold preedit strings
  65. self._strings = []
  66. # self._cursor: the caret position in preedit phrases
  67. self._cursor = [0,0]
  68. # self._candidates: hold candidates selected from database [[now],[pre]]
  69. self._candidates = [[],[]]
  70. self._lookup_table = scim.LookupTable(XingMaEngine._page_size)
  71. self._lookup_table.fix_page_size (True)
  72. # self._py_mode: whether in pinyin mode
  73. self._py_mode = False
  74. # self._zi: the last Zi commit to preedit
  75. self._zi = u''
  76. # self._caret: caret position in lookup_table
  77. self._caret = 0
  78. # self._onechar: whether we only select single character
  79. self._onechar = False
  80. def clear (self):
  81. '''Remove data holded'''
  82. self.over_input ()
  83. self._t_chars = []
  84. self._strings = []
  85. self._cursor = [0,0]
  86. self._py_mode = False
  87. self._zi = u''
  88. self.update_candidates
  89. def is_empty (self):
  90. return len(self._t_chars) == 0
  91. def clear_input (self):
  92. '''
  93. Remove input characters held for XingMa mode,
  94. '''
  95. self._chars = [[],[],[]]
  96. self._xmkey_list = []
  97. self._lookup_table.clear()
  98. self._lookup_table.show_cursor(False)
  99. self._candidates = [[],[]]
  100. def over_input (self):
  101. '''
  102. Remove input characters held for XingMa mode,
  103. '''
  104. self.clear_input ()
  105. self._u_chars = []
  106. def set_parser (self, parser):
  107. '''change input parser'''
  108. self.clear ()
  109. self._parser = parser
  110. def add_input (self,c):
  111. '''add input character'''
  112. if len (self._t_chars) == self._max_length:
  113. return True
  114. self._zi = u''
  115. if self._cursor[1]:
  116. self.split_phrase()
  117. if (len (self._chars[0]) == self._max_key_len and (not self._py_mode)) or ( len (self._chars[0]) == 6 and self._py_mode ) :
  118. self.auto_commit_to_preedit()
  119. res = self.add_input (c)
  120. return res
  121. elif self._chars[1]:
  122. self._chars[1].append (c)
  123. else:
  124. if (not self._py_mode and ( c in self._valid_input_chars)) or\
  125. (self._py_mode and (c in u'abcdefghijklmnopqrstuvwxyz')):
  126. try:
  127. self._xmkey_list += self._parser (c)
  128. self._chars[0].append (c)
  129. except:
  130. self._chars[1].append (c)
  131. else:
  132. self._chars[1].append (c)
  133. self._t_chars.append(c)
  134. res = self.update_candidates ()
  135. return res
  136. def pop_input (self):
  137. '''remove and display last input char held'''
  138. _c =''
  139. if self._chars[1]:
  140. _c = self._chars[1].pop ()
  141. elif self._chars[0]:
  142. _c = self._chars[0].pop ()
  143. self._xmkey_list.pop()
  144. if (not self._chars[0]) and self._u_chars:
  145. self._chars[0] = self._u_chars.pop()
  146. self._chars[1] = self._chars[1][:-1]
  147. self._xmkey_list = self._parser (self._chars[0])
  148. self._strings.pop (self._cursor[0] - 1 )
  149. self._cursor[0] -= 1
  150. self._t_chars.pop()
  151. self.update_candidates ()
  152. return _c
  153. def get_input_chars (self):
  154. '''get characters held, valid and invalid'''
  155. return self._chars[0] + self._chars[1]
  156. def get_input_chars_string (self):
  157. '''Get valid input char string'''
  158. return u''.join(map(str,self._t_chars))
  159. def get_all_input_strings (self):
  160. '''Get all uncommit input characters, used in English mode or direct commit'''
  161. return u''.join( map(u''.join, self._u_chars + [self._chars[0]] \
  162. + [self._chars[1]]) )
  163. def get_index(self,key):
  164. '''Get the index of key in database table'''
  165. return self._pt.index(key)
  166. def split_phrase (self):
  167. '''Splite current phrase into two phrase'''
  168. _head = u''
  169. _end = u''
  170. try:
  171. _head = self._strings[self._cursor[0]][:self._cursor[1]]
  172. _end = self._strings[self._cursor[0]][self._cursor[1]:]
  173. self._strings.pop(self._cursor[0])
  174. self._strings.insert(self._cursor[0],_head)
  175. self._strings.insert(self._cursor[0]+1,_end)
  176. self._cursor[0] +=1
  177. self._cursor[1] = 0
  178. except:
  179. pass
  180. def remove_before_string (self):
  181. '''Remove string before cursor'''
  182. if self._cursor[1] != 0:
  183. self.split_phrase()
  184. if self._cursor[0] > 0:
  185. self._strings.pop(self._cursor[0]-1)
  186. self._cursor[0] -= 1
  187. else:
  188. pass
  189. # if we remove all characters in preedit string, we need to clear the self._t_chars
  190. if self._cursor == [0,0]:
  191. self._t_chars =[]
  192. def remove_after_string (self):
  193. '''Remove string after cursor'''
  194. if self._cursor[1] != 0:
  195. self.split_phrase()
  196. if self._cursor[0] >= len (self._strings):
  197. pass
  198. else:
  199. self._strings.pop(self._cursor[0])
  200. def remove_before_char (self):
  201. '''Remove character before cursor'''
  202. if self._cursor[1] > 0:
  203. _str = self._strings[ self._cursor[0] ]
  204. self._strings[ self._cursor[0] ] = _str[ : self._cursor[1]-1] + _str[ self._cursor[1] :]
  205. self._cursor[1] -= 1
  206. else:
  207. if self._cursor[0] == 0:
  208. pass
  209. else:
  210. if len ( self._strings[self._cursor[0] - 1] ) == 1:
  211. self.remove_before_string()
  212. else:
  213. self._strings[self._cursor[0] - 1] = self._strings[self._cursor[0] - 1][:-1]
  214. # if we remove all characters in preedit string, we need to clear the self._t_chars
  215. if self._cursor == [0,0]:
  216. self._t_chars =[]
  217. def remove_after_char (self):
  218. '''Remove character after cursor'''
  219. if self._cursor[1] == 0:
  220. if self._cursor[0] == len ( self._strings):
  221. pass
  222. else:
  223. if len( self._strings[ self._cursor[0] ]) == 1:
  224. self.remove_after_string ()
  225. else:
  226. self._strings[ self._cursor[0] ] = self._strings[ self._cursor[0] ][1:]
  227. else:
  228. if ( self._cursor[1] + 1 ) == len( self._strings[ self._cursor[0] ] ) :
  229. self.split_phrase ()
  230. self.remove_after_string ()
  231. else:
  232. string = self._strings[ self._cursor[0] ]
  233. self._strings[ self._cursor[0] ] = string[:self._cursor[1]] + string[ self._cursor[1] + 1 : ]
  234. def get_invalid_input_chars (self):
  235. '''get invalid characters held'''
  236. return self._chars[1]
  237. def get_invalid_input_string (self):
  238. '''get invalid characters in string form'''
  239. return u''.join (self._chars[1])
  240. def get_preedit_strings (self):
  241. '''Get preedit strings'''
  242. if self._candidates[0]:
  243. if self._py_mode:
  244. _p_index = 7
  245. else:
  246. _p_index = self.get_index ('phrase')
  247. _candi = u'###' + self._candidates[0][ int (self._lookup_table.get_cursor_pos() ) ][ _p_index ] + u'###'
  248. else:
  249. input_chars = self.get_input_chars ()
  250. if input_chars:
  251. _candi = u''.join( ['###'] + map( str, input_chars) + ['###'] )
  252. else:
  253. _candi = u''
  254. if self._strings:
  255. res = u''
  256. _cursor = self._cursor[0]
  257. _luc = len (self._u_chars)
  258. if _luc:
  259. res =u''.join( self._strings[ : _cursor - _luc] +[u'@@@'] + self._strings[_cursor - _luc : _cursor ] + [ _candi ] + self._strings[ _cursor : ])
  260. else:
  261. res = u''.join( self._strings[ : _cursor ] + [ _candi ] + self._strings[ _cursor : ])
  262. return res
  263. else:
  264. return _candi
  265. def add_caret (self, addstr):
  266. '''add length to caret position'''
  267. self._caret += len(addstr)
  268. def get_caret (self):
  269. '''Get caret position in preedit strings'''
  270. self._caret = 0
  271. if self._cursor[0] and self._strings:
  272. map (self.add_caret,self._strings[:self._cursor[0]])
  273. self._caret += self._cursor[1]
  274. if self._candidates[0]:
  275. if self._py_mode:
  276. _p_index = 7
  277. else:
  278. _p_index = self.get_index ('phrase')
  279. _candi =self._candidates[0][ int (self._lookup_table.get_cursor_pos() ) ][ _p_index ]
  280. else:
  281. _candi = u''.join( map( str,self.get_input_chars()) )
  282. self._caret += len( _candi )
  283. return self._caret
  284. def arrow_left (self):
  285. '''Process Arrow Left Key Event.
  286. Update cursor data when move caret left'''
  287. if self.get_preedit_strings ():
  288. if not( self.get_input_chars () or self._u_chars ):
  289. if self._cursor[1] > 0:
  290. self._cursor[1] -= 1
  291. else:
  292. if self._cursor[0] > 0:
  293. self._cursor[1] = len (self._strings[self._cursor[0]-1]) - 1
  294. self._cursor[0] -= 1
  295. else:
  296. self._cursor[0] = len(self._strings)
  297. self._cursor[1] = 0
  298. self.update_candidates ()
  299. return True
  300. else:
  301. return False
  302. def arrow_right (self):
  303. '''Process Arrow Right Key Event.
  304. Update cursor data when move caret right'''
  305. if self.get_preedit_strings ():
  306. if not( self.get_input_chars () or self._u_chars ):
  307. if self._cursor[1] == 0:
  308. if self._cursor[0] == len (self._strings):
  309. self._cursor[0] = 0
  310. else:
  311. self._cursor[1] += 1
  312. else:
  313. self._cursor[1] += 1
  314. if self._cursor[1] == len(self._strings[ self._cursor[0] ]):
  315. self._cursor[0] += 1
  316. self._cursor[1] = 0
  317. self.update_candidates ()
  318. return True
  319. else:
  320. return False
  321. def control_arrow_left (self):
  322. '''Process Control + Arrow Left Key Event.
  323. Update cursor data when move caret to string left'''
  324. if self.get_preedit_strings ():
  325. if not( self.get_input_chars () or self._u_chars ):
  326. if self._cursor[1] == 0:
  327. if self._cursor[0] == 0:
  328. self._cursor[0] = len (self._strings) - 1
  329. else:
  330. self._cursor[0] -= 1
  331. else:
  332. self._cursor[1] = 0
  333. self.update_candidates ()
  334. return True
  335. else:
  336. return False
  337. def control_arrow_right (self):
  338. '''Process Control + Arrow Right Key Event.
  339. Update cursor data when move caret to string right'''
  340. if self.get_preedit_strings ():
  341. if not( self.get_input_chars () or self._u_chars ):
  342. if self._cursor[1] == 0:
  343. if self._cursor[0] == len (self._strings):
  344. self._cursor[0] = 1
  345. else:
  346. self._cursor[0] += 1
  347. else:
  348. self._cursor[0] += 1
  349. self._cursor[1] = 0
  350. self.update_candidates ()
  351. return True
  352. else:
  353. return False
  354. def ap_candidate (self, candi):
  355. '''append candidate to lookup_table'''
  356. if not self._py_mode:
  357. _p_index = self.get_index('phrase')
  358. _fkey = self.get_index('m0')
  359. else:
  360. _p_index = 7
  361. _fkey = 1
  362. _phrase = candi[_p_index]
  363. _xm = u''.join( map(self._deparser , candi[_fkey + len(self._xmkey_list) : _p_index ] ) )
  364. # further color implementation needed :)
  365. # here -2 is the pos of num, -1 is the pos of . 0 is the pos of string
  366. attrs = [scim.Attribute (-2, 1 , scim.ATTR_FOREGROUND, 0x8e2626 )]
  367. if candi[-2] < 0 :
  368. # this is a user defined phrase:
  369. attrs.append ( scim.Attribute (0, len(_phrase), scim.ATTR_FOREGROUND, 0x7700c3 ) )
  370. elif candi[-1] > 0:
  371. # this is a sys phrase used by user:
  372. attrs.append ( scim.Attribute (0, len(_phrase), scim.ATTR_FOREGROUND, 0x000000 ) )
  373. else:
  374. # this is a system phrase haven't been used:
  375. attrs.append ( scim.Attribute (0, len(_phrase), scim.ATTR_FOREGROUND, 0x000000 ) )
  376. # this is the part of xmkey
  377. attrs.append( scim.Attribute (len(_phrase), len(_xm), scim.ATTR_FOREGROUND, 0x1973a2 ) )
  378. self._lookup_table.append_candidate ( _phrase + _xm, attrs )
  379. self._lookup_table.show_cursor (True)
  380. def update_candidates (self):
  381. '''Update lookuptable'''
  382. if (self._chars[0] == self._chars[2] and self._candidates[0]) \
  383. or self._chars[1]:
  384. # if no change in valid input char or we have invalid input,
  385. # we do not do sql enquery
  386. pass
  387. else:
  388. # do enquiry
  389. self._lookup_table.clear ()
  390. self._lookup_table.show_cursor (False)
  391. if self._xmkey_list:
  392. # here we need to consider two parts, xingma and pinyin
  393. # first xingma
  394. if not self._py_mode:
  395. self._candidates[0] = self.db.select_words( self._xmkey_list, self._onechar )
  396. else:
  397. self._candidates[0] = self.db.select_zi( self._xmkey_list )
  398. self._chars[2] = self._chars[0][:]
  399. else:
  400. self._candidates[0] =[]
  401. if self._candidates[0]:
  402. map ( self.ap_candidate, self._candidates[0])
  403. else:
  404. if self._chars[0]:
  405. ## old manner:
  406. #if self._candidates[1]:
  407. # #print self._candidates[1]
  408. # self._candidates[0] = self._candidates[1]
  409. # self._candidates[1] = []
  410. # last_input = self.pop_input ()
  411. # self.auto_commit_to_preedit ()
  412. # res = self.add_input( last_input )
  413. # return res
  414. #else:
  415. # self.pop_input ()
  416. # self._lookup_table.clear()
  417. # self._lookup_table.show_cursor (False)
  418. # return False
  419. ###################
  420. ## new manner, we add new char to invalid input
  421. ## chars
  422. if not self._chars[1]:
  423. # we don't have invalid input chars
  424. # here we need to check the last input
  425. # is a punctuation or not, if is a punct,
  426. # then we use old maner to summit the former valid
  427. # candidate
  428. if ascii.ispunct (self._chars[0][-1].encode('ascii')):
  429. ## old manner:
  430. if self._candidates[1]:
  431. self._candidates[0] = self._candidates[1]
  432. self._candidates[1] = []
  433. last_input = self.pop_input ()
  434. self.auto_commit_to_preedit ()
  435. res = self.add_input( last_input )
  436. return res
  437. else:
  438. self.pop_input ()
  439. self._lookup_table.clear()
  440. self._lookup_table.show_cursor (False)
  441. return False
  442. else:
  443. # this is not a punct
  444. self._chars[1].append( self._chars[0].pop() )
  445. self._xmkey_list.pop()
  446. else:
  447. pass
  448. self._candidates[0] =[]
  449. else:
  450. self._lookup_table.clear()
  451. self._lookup_table.show_cursor (False)
  452. self._candidates[1] = self._candidates[0]
  453. return True
  454. def commit_to_preedit (self):
  455. '''Add select phrase in lookup table to preedit string'''
  456. if not self._py_mode:
  457. _p_index = self.get_index('phrase')
  458. else:
  459. _p_index = 7
  460. try:
  461. self._strings.insert(self._cursor[0], self._candidates[0][ self.get_cursor_pos() ][_p_index])
  462. self._cursor [0] += 1
  463. if self._py_mode:
  464. self._zi = self._candidates[0][ self.get_cursor_pos() ][_p_index]
  465. self.over_input ()
  466. self.update_candidates ()
  467. except:
  468. pass
  469. def auto_commit_to_preedit (self):
  470. '''Add select phrase in lookup table to preedit string'''
  471. if not self._py_mode:
  472. _p_index = self.get_index('phrase')
  473. else:
  474. _p_index = 7
  475. try:
  476. self._u_chars.append( self._chars[0][:] )
  477. self._strings.insert(self._cursor[0], self._candidates[0][ self.get_cursor_pos() ][_p_index])
  478. self._cursor [0] += 1
  479. self.clear_input()
  480. self.update_candidates ()
  481. except:
  482. pass
  483. def get_aux_strings (self):
  484. '''Get aux strings'''
  485. input_chars = self.get_input_chars ()
  486. if input_chars:
  487. #aux_string = u' '.join( map( u''.join, self._u_chars + [self._chars[0]] ) )
  488. aux_string = u''.join (self._chars[0])
  489. return aux_string
  490. aux_string = u''
  491. if self._zi:
  492. # we have pinyin result
  493. xmcodes = self.db.find_zi_code(self._zi)
  494. aux_string = self._zi+u': '
  495. aux_string = u' '.join(xmcodes)
  496. # self._zi = u''
  497. cstr = u''.join(self._strings)
  498. if self.db.user_can_define_phrase:
  499. if len (cstr ) > 1:
  500. aux_string += (u'\t#: ' + self.db.parse_phrase_to_xm (cstr))
  501. return aux_string
  502. def arrow_down(self):
  503. '''Process Arrow Down Key Event
  504. Move Lookup Table cursor down'''
  505. res = self._lookup_table.cursor_down()
  506. self.update_candidates ()
  507. if not res and self._candidates[0]:
  508. return True
  509. return res
  510. def arrow_up(self):
  511. '''Process Arrow Up Key Event
  512. Move Lookup Table cursor up'''
  513. res = self._lookup_table.cursor_up()
  514. self.update_candidates ()
  515. if not res and self._candidates[0]:
  516. return True
  517. return res
  518. def page_down(self):
  519. '''Process Page Down Key Event
  520. Move Lookup Table page down'''
  521. res = self._lookup_table.page_down()
  522. self.update_candidates ()
  523. if not res and self._candidates[0]:
  524. return True
  525. return res
  526. def page_up(self):
  527. '''Process Page Up Key Event
  528. move Lookup Table page up'''
  529. res = self._lookup_table.page_up()
  530. self.update_candidates ()
  531. if not res and self._candidates[0]:
  532. return True
  533. return res
  534. def number (self, index):
  535. '''Select the candidates in Lookup Table
  536. index should start from 0'''
  537. self._lookup_table.set_cursor_pos_in_current_page ( index )
  538. if index != self._lookup_table.get_cursor_pos_in_current_page ():
  539. # the index given is out of range we do not commit string
  540. return False
  541. self.commit_to_preedit ()
  542. return True
  543. def alt_number (self,index):
  544. '''Remove the candidates in Lookup Table from user_db index should start from 0'''
  545. cps = self._lookup_table.get_current_page_start()
  546. pos = cps + index
  547. if len (self._candidates[0]) > pos:
  548. # this index is valid
  549. can = self._candidates[0][pos]
  550. if can[-2] <0 :
  551. # freq of this candidate is -1, means this a user phrase
  552. self.db.remove_phrase (can)
  553. # make update_candidates do sql enquiry
  554. self._chars[2].pop()
  555. self.update_candidates ()
  556. return True
  557. else:
  558. return False
  559. def get_cursor_pos (self):
  560. '''get lookup table cursor position'''
  561. return self._lookup_table.get_cursor_pos()
  562. def get_lookup_table (self):
  563. '''Get lookup table'''
  564. return self._lookup_table
  565. def is_lt_visible (self):
  566. '''Check whether lookup table is visible'''
  567. return self._lookup_table.is_cursor_visible ()
  568. def backspace (self):
  569. '''Process backspace Key Event'''
  570. self._zi = u''
  571. if self.get_input_chars():
  572. self.pop_input ()
  573. return True
  574. elif self.get_preedit_strings ():
  575. self.remove_before_char ()
  576. return True
  577. else:
  578. return False
  579. def control_backspace (self):
  580. '''Process control+backspace Key Event'''
  581. self._zi = u''
  582. if self.get_input_chars():
  583. self.over_input ()
  584. return True
  585. elif self.get_preedit_strings ():
  586. self.remove_before_string ()
  587. return True
  588. else:
  589. return False
  590. def delete (self):
  591. '''Process delete Key Event'''
  592. self._zi = u''
  593. if self.get_input_chars():
  594. return True
  595. elif self.get_preedit_strings ():
  596. self.remove_after_char ()
  597. return True
  598. else:
  599. return False
  600. def control_delete (self):
  601. '''Process control+delete Key Event'''
  602. self._zi = u''
  603. if self.get_input_chars ():
  604. return True
  605. elif self.get_preedit_strings ():
  606. self.remove_after_string ()
  607. return True
  608. else:
  609. return False
  610. def l_shift (self):
  611. '''Process Left Shift Key Event as immediately commit to preedit strings'''
  612. if self._chars[0]:
  613. self.commit_to_preedit ()
  614. return True
  615. else:
  616. return False
  617. def r_shift (self):
  618. '''Proess Right Shift Key Event as changed between PinYin Mode and XingMa Mode'''
  619. self._zi = u''
  620. if self._chars[0]:
  621. self.commit_to_preedit ()
  622. self._py_mode = not (self._py_mode)
  623. return True
  624. def space (self):
  625. '''Process space Key Event
  626. return (KeyProcessResult,whethercommit,commitstring)'''
  627. if self._chars[1]:
  628. # we have invalid input, so do not commit
  629. return (False,u'')
  630. if self._t_chars :
  631. # user has input sth
  632. self.commit_to_preedit ()
  633. pstr = self.get_preedit_strings ()
  634. self.clear()
  635. return (True,pstr)
  636. else:
  637. return (False,u' ')
  638. def one_candidate (self):
  639. '''Return true if there is only one candidate'''
  640. return len(self._candidates[0]) == 1
  641. class XingMaEngine (IMEngine):
  642. '''The IMEngine for XingMa'''
  643. # colors
  644. # _phrase_color = 0xffffff
  645. # _user_phrase_color = 0xffffff
  646. # _new_phrase_color = 0xffffff
  647. # lookup table page size
  648. _page_size = 6
  649. def __init__ (self, factory, config, encoding, id, db ):
  650. IMEngine.__init__ (self, factory, config, encoding, id)
  651. self._config = config
  652. self._lookup_table = scim.LookupTable (XingMaEngine._page_size)
  653. self._lookup_table.fix_page_size (True)
  654. # this is the backend sql db we need for our IME
  655. # we receive this db from IMEngineFactory
  656. #self.db = XMSQLiteDB.XMSQLiteDB( name = dbname )
  657. self.db = db
  658. # this is the parer which parse the input string to key object
  659. self._parser = XMDict.Parse
  660. # 0 = english input mode
  661. # 1 = xingma input mode
  662. self._mode = 1
  663. # self._ime_py: True / False this IME support pinyin mode
  664. self._ime_py = self.db.get_ime_property ('pinyin_mode')
  665. if self._ime_py:
  666. if self._ime_py.lower() == u'true':
  667. self._ime_py = True
  668. else:
  669. self._ime_py = False
  670. else:
  671. print 'We coult not find "pinyin_mode" entry in database, is it a outdated database?'
  672. self._ime_py = False
  673. self._status = self.db.get_ime_property('status_prompt').encode('utf8')
  674. # now we check and update the valid input characters
  675. self._chars = self.db.get_ime_property('valid_input_chars')
  676. self._valid_input_chars = []
  677. for _c in self._chars:
  678. if _c in XMDict.XingMa_List:
  679. self._valid_input_chars.append(_c)
  680. del self._chars
  681. self._pt = self.db.get_phrase_table_index ()
  682. self._ml = int(self.db.get_ime_property ('max_key_length'))
  683. # Containers we used:
  684. self._editor = Editor(self._pt, self._valid_input_chars, self._ml, self.db)
  685. # some other vals we used:
  686. # self._prev_key: hold the key event last time.
  687. self._prev_key = None
  688. self._prev_char = None
  689. self._double_quotation_state = False
  690. self._single_quotation_state = False
  691. # [ENmode,XMmode] we get XMmode properties from db
  692. self._full_width_letter = [
  693. False,
  694. self.db.get_ime_property('def_full_width_letter').lower() == u'true'
  695. ]
  696. self._full_width_punct = [
  697. False,
  698. self.db.get_ime_property('def_full_width_punct').lower() == u'true'
  699. ]
  700. self._direct_commit = False
  701. # some properties we will involved, Property is taken from scim.
  702. self._status_property = Property ("status", "")
  703. self._letter_property = Property ("full_letter", "")
  704. self._punct_property = Property ("full_punct", "")
  705. self._py_property = Property ("py_mode", "")
  706. self._onechar_property = Property ("onechar","")
  707. self._direct_commit_property = Property ("dcommit","")
  708. #self._setup_property = Property ("setup", _("Setup"))
  709. self.reset ()
  710. def reset (self):
  711. self._editor.clear ()
  712. self._double_quotation_state = False
  713. self._single_quotation_state = False
  714. self._prev_key = None
  715. # do not change onechar mode
  716. #self._editor._onechar = False
  717. self._init_properties ()
  718. self._update_ui ()
  719. IMEngine.reset (self)
  720. def _init_properties (self):
  721. properties= (
  722. self._status_property,
  723. self._letter_property,
  724. self._punct_property,
  725. self._py_property,
  726. self._onechar_property,
  727. self._direct_commit_property
  728. # self._setup_property
  729. )
  730. self.register_properties (properties)
  731. self._refresh_properties ()
  732. def _refresh_properties (self):
  733. '''Method used to update properties'''
  734. # taken and modified from PinYin.py :)
  735. if self._mode == 1: # refresh mode
  736. self._status_property.label = _(self._status)
  737. else:
  738. self._status_property.label = _("EN")
  739. if self._full_width_letter[self._mode]:
  740. self._letter_property.label = ''
  741. self._letter_property.icon = '/usr/share/scim/icons/full-letter.png'
  742. else:
  743. self._letter_property.label = ''
  744. self._letter_property.icon = '/usr/share/scim/icons/half-letter.png'
  745. if self._full_width_punct[self._mode]:
  746. self._punct_property.label = ''
  747. self._punct_property.icon ='/usr/share/scim/icons/full-punct.png'
  748. else:
  749. self._punct_property.label = ''
  750. self._punct_property.icon ='/usr/share/scim/icons/half-punct.png'
  751. if self._editor._py_mode:
  752. self._py_property.label = ''
  753. self._py_property.icon ='/usr/share/scim/icons/py-mode.png'
  754. else:
  755. self._py_property.label = ''
  756. self._py_property.icon ='/usr/share/scim/icons/xm-mode.png'
  757. if self._editor._onechar:
  758. self._onechar_property.label = ''
  759. self._onechar_property.icon = '/usr/share/scim/icons/onechar.png'
  760. else:
  761. self._onechar_property.label = ''
  762. self._onechar_property.icon = '/usr/share/scim/icons/phrase.png'
  763. if self._direct_commit:
  764. self._direct_commit_property.label = ''
  765. self._direct_commit_property.icon = '/usr/share/scim/icons/dcommit.png'
  766. else:
  767. self._direct_commit_property.label = ''
  768. self._direct_commit_property.icon = '/usr/share/scim/icons/ncommit.png'
  769. properties = (
  770. self._status_property,
  771. self._letter_property,
  772. self._punct_property,
  773. self._py_property,
  774. self._onechar_property,
  775. self._direct_commit_property
  776. )
  777. # use buildin method to update properties :)
  778. map (self.update_property, properties)
  779. def _change_mode (self):
  780. '''Shift input mode, XM -> EN -> XM
  781. '''
  782. self._mode = int (not self._mode)
  783. self.reset ()
  784. self._update_ui ()
  785. def trigger_property (self, property):
  786. '''Shift property'''
  787. if property == "status":
  788. self._change_mode ()
  789. elif property == "full_letter":
  790. self._full_width_letter [self._mode] = not self._full_width_letter [self._mode]
  791. elif property == "full_punct":
  792. self._full_width_punct [self._mode] = not self._full_width_punct [self._mode]
  793. elif property == 'py_mode' and self._ime_py:
  794. self._editor.r_shift ()
  795. elif property == 'onechar':
  796. self._editor._onechar = not self._editor._onechar
  797. elif property == 'dcommit':
  798. self._direct_commit = not self._direct_commit
  799. self._refresh_properties ()
  800. # elif property == "setup":
  801. # Need implementation
  802. # self.start_helper ("96c07b6f-0c3d-4403-ab57-908dd9b8d513")
  803. # at last invoke default method
  804. IMEngine.trigger_property (self, property)
  805. def _update_preedit (self):
  806. '''Update Preedit String in UI'''
  807. _str = self._editor.get_preedit_strings ()
  808. if _str == u'':
  809. self.hide_preedit_string ()
  810. else:
  811. attrs = []
  812. res = patt_edit.match (_str)
  813. if res:
  814. _str = u''
  815. ures = patt_uncommit.match (res.group(1))
  816. if ures:
  817. _str=u''.join (ures.groups())
  818. lc = len (ures.group(1) )
  819. lu = len (ures.group(2) )
  820. attrs.append (scim.Attribute(0,lc,scim.ATTR_FOREGROUND,0x1b3f03) )
  821. attrs.append (scim.Attribute(lc,lu,scim.ATTR_FOREGROUND,0x0895a2) )
  822. lg1 = len (_str)
  823. else:
  824. _str += res.group (1)
  825. lg1 = len ( res.group(1) )
  826. attrs.append (scim.Attribute(0,lg1,scim.ATTR_FOREGROUND,0x1b3f03) )
  827. _str += res.group(2)
  828. _str += res.group(3)
  829. lg2 = len ( res.group(2) )
  830. lg3 = len ( res.group(3) )
  831. attrs.append( scim.Attribute(lg1,lg2,scim.ATTR_FOREGROUND,0x0e0ea0) )
  832. attrs.append( scim.Attribute(lg1+lg2,lg3,scim.ATTR_FOREGROUND,0x1b3f03) )
  833. else:
  834. attrs.append( scim.Attribute(0,len(_str),scim.ATTR_FOREGROUND,0x1b3f03) )
  835. self.show_preedit_string ()
  836. self.update_preedit_string (_str,attrs)
  837. self.update_preedit_caret ( self._editor.get_caret() )
  838. self.show_preedit_string ()
  839. def _update_aux (self):
  840. '''Update Aux String in UI'''
  841. _ic = self._editor.get_aux_strings ()
  842. if _ic:
  843. self.show_aux_string ()
  844. attrs = [ scim.Attribute(0, len(_ic), scim.ATTR_FOREGROUND,0x9515b5) ]
  845. #attrs = [ scim.Attribute(0,len(_ic),scim.ATTR_FOREGROUND,0x5540c1)]
  846. self.update_aux_string (_ic,attrs)
  847. else:
  848. self.hide_aux_string ()
  849. self.update_aux_string (u'')
  850. def _update_lookup_table (self):
  851. '''Update Lookup Table in UI'''
  852. if self._editor.is_lt_visible ():
  853. self.update_lookup_table ( self._editor.get_lookup_table() )
  854. self.show_lookup_table ()
  855. else:
  856. self.hide_lookup_table ()
  857. def _update_ui (self):
  858. '''Update User Interface'''
  859. self._update_lookup_table ()
  860. self._update_preedit ()
  861. self._update_aux ()
  862. def commit_string (self,string):
  863. IMEngine.commit_string ( self, string )
  864. self._prev_char = string[-1]
  865. def _convert_to_full_width (self, c):
  866. '''convert half width character to full width'''
  867. if c in [u".", u"\\", u"^", u"_", u"$", u"\"", u"'", u">", u"<" ]:
  868. if c == u".":
  869. if self._prev_char and self._prev_char.isdigit () \
  870. and self._prev_key and chr (self._prev_key.code) == self._prev_char:
  871. return u"."
  872. else:
  873. return u"\u3002"
  874. elif c == u"\\":
  875. return u"\u3001"
  876. elif c == u"^":
  877. return u"\u2026\u2026"
  878. elif c == u"_":
  879. return u"\u2014\u2014"
  880. elif c == u"$":
  881. return u"\uffe5"
  882. elif c == u"\"":
  883. self._double_quotation_state = not self._double_quotation_state
  884. if self._double_quotation_state:
  885. return u"\u201c"
  886. else:
  887. return u"\u201d"
  888. elif c == u"'":
  889. self._single_quotation_state = not self._single_quotation_state
  890. if self._single_quotation_state:
  891. return u"\u2018"
  892. else:
  893. return u"\u2019"
  894. elif c == u"<":
  895. if self._mode:
  896. return u"\u300a"
  897. elif c == u">":
  898. if self._mode:
  899. return u"\u300b"
  900. return scim.unichar_half_to_full (c)
  901. def _match_hotkey (self, key, code, mask):
  902. if key.code == code and key.mask == mask:
  903. if self._prev_key and key.code == self._prev_key.code and key.mask & KeyMask.ReleaseMask:
  904. return True
  905. if not key.mask & KeyMask.ReleaseMask:
  906. return True
  907. return False
  908. def process_key_event (self, key):
  909. '''Process Key Events
  910. Key Events include Key Press and Key Release,
  911. KeyMask means Key Pressed
  912. '''
  913. result = self._process_key_event (key)
  914. self._prev_key = key
  915. return result
  916. def _process_key_event (self, key):
  917. '''Internal method to process key event'''
  918. # Match mode switch hotkey
  919. if not self._editor._t_chars and ( self._match_hotkey (key, KeyCode.KEY_Shift_L, KeyMask.ShiftMask + KeyMask.ReleaseMask)):
  920. self._change_mode ()
  921. return True
  922. # Match full half letter mode switch hotkey
  923. if self._match_hotkey (key, KeyCode.KEY_space, KeyMask.ShiftMask):
  924. self.trigger_property ("full_letter")
  925. return True
  926. # Match full half punct mode switch hotkey
  927. if self._match_hotkey (key, KeyCode.KEY_period, KeyMask.ControlMask):
  928. self.trigger_property ("full_punct")
  929. return True
  930. # we ignore all hotkeys
  931. # if key.mask & KeyMask.AltMask:
  932. # return False
  933. # Ignore key release event
  934. # if key.mask & KeyMask.ReleaseMask:
  935. # return True
  936. if self._mode:
  937. return self._xingma_mode_process_key_event (key)
  938. else:
  939. return self._english_mode_process_key_event (key)
  940. def _english_mode_process_key_event (self, key):
  941. '''English Mode Process Key Event'''
  942. # Ignore key release event
  943. if key.mask & KeyMask.ReleaseMask:
  944. return True
  945. if key.code >= 128:
  946. return False
  947. # we ignore all hotkeys here
  948. if key.mask & KeyMask.ControlMask+KeyMask.AltMask:
  949. return False
  950. c = unichr (key.code)
  951. if ascii.ispunct (key.code): # if key code is a punctation
  952. if self._full_width_punct[self._mode]:
  953. self.commit_string (self._convert_to_full_width (c))
  954. return True
  955. else:
  956. self.commit_string (c)
  957. return True
  958. if self._full_width_letter[self._mode]: # if key code is a letter or digit
  959. self.commit_string (self._convert_to_full_width (c))
  960. return True
  961. else:
  962. self.commit_string (c)
  963. return True
  964. # should not reach there
  965. return False
  966. def _xingma_mode_process_key_event (self, key):
  967. '''Xingma Mode Process Key Event'''
  968. cond_letter_translate = lambda (c): \
  969. self._convert_to_full_width (c) if self._full_width_letter [self._mode] else c
  970. cond_punct_translate = lambda (c): \
  971. self._convert_to_full_width (c) if self._full_width_punct [self._mode] else c
  972. # We have to process the pinyin mode change key event here,
  973. # because we ignore all Release event below.
  974. if self._match_hotkey (key, KeyCode.KEY_Shift_R, KeyMask.ShiftMask + KeyMask.ReleaseMask) and self._ime_py:
  975. res = self._editor.r_shift ()
  976. self._refresh_properties ()
  977. self._update_ui ()
  978. return res
  979. # process commit to preedit
  980. if self._match_hotkey (key, KeyCode.KEY_Shift_R, KeyMask.ShiftMask + KeyMask.ReleaseMask) or self._match_hotkey (key, KeyCode.KEY_Shift_L, KeyMask.ShiftMask + KeyMask.ReleaseMask):
  981. res = self._editor.l_shift ()
  982. self._update_ui ()
  983. return res
  984. # Match sigle char mode switch hotkey
  985. if self._match_hotkey (key, KeyCode.KEY_comma, KeyMask.ControlMask):
  986. self.trigger_property ( u"onechar" )
  987. return True
  988. # Match direct commit mode switch hotkey
  989. if self._match_hotkey (key, KeyCode.KEY_slash, KeyMask.ControlMask):
  990. self.trigger_property ( u"dcommit" )
  991. return True
  992. # Ignore key release event now :)
  993. if key.mask & KeyMask.ReleaseMask:
  994. return True
  995. if self._editor.is_empty ():
  996. # we have not input anything
  997. if key.code <= 127 and (not key.mask & KeyMask.AltMask + KeyMask.ControlMask):
  998. if ascii.ispunct (key.code) and ( unichr(key.code) not in self._valid_input_chars ) :
  999. self.commit_string (cond_punct_translate (unichr (key.code)))
  1000. return True
  1001. if ascii.isdigit (key.code):
  1002. self.commit_string (cond_letter_translate (unichr (key.code)))
  1003. return True
  1004. if key.code in (KeyCode.KEY_Return, KeyCode.KEY_KP_Enter):
  1005. if self._editor.is_empty ():
  1006. return False
  1007. else:
  1008. commit_string = self._editor.get_all_input_strings ()
  1009. self._editor.clear ()
  1010. self._update_ui ()
  1011. self.commit_string (commit_string)
  1012. self._refresh_properties ()
  1013. return True
  1014. elif key.code == KeyCode.KEY_space:
  1015. o_py = self._editor._py_mode
  1016. sp_res = self._editor.space ()
  1017. self._update_ui ()
  1018. #return (KeyProcessResult,whethercommit,commitstring)
  1019. if sp_res[0]:
  1020. self.commit_string (sp_res[1])
  1021. self.db.check_phrase (sp_res[1])
  1022. else:
  1023. if sp_res[1] == u' ':
  1024. self.commit_string (cond_letter_translate (u" "))
  1025. if o_py != self._editor._py_mode:
  1026. self._refresh_properties ()
  1027. return True
  1028. elif key.code == KeyCode.KEY_BackSpace and key.mask & KeyMask.ControlMask:
  1029. res = self._editor.control_backspace ()
  1030. self._update_ui ()
  1031. return res
  1032. elif key.code == KeyCode.KEY_BackSpace:
  1033. res = self._editor.backspace ()
  1034. self._update_ui ()
  1035. return res
  1036. elif key.code == KeyCode.KEY_Escape:
  1037. if self._editor.is_empty () and (not self._editor._py_mode) :
  1038. return False
  1039. else:
  1040. self.reset ()
  1041. self._update_ui ()
  1042. return True
  1043. elif key.code == KeyCode.KEY_Down :
  1044. res = self._editor.arrow_down ()
  1045. self._update_lookup_table ()
  1046. return res
  1047. elif key.code == KeyCode.KEY_Up:
  1048. res = self._editor.arrow_up ()
  1049. self._update_lookup_table ()
  1050. return res
  1051. elif key.code == KeyCode.KEY_Left and key.mask & KeyMask.ControlMask:
  1052. res = self._editor.control_arrow_left ()
  1053. self._update_ui ()
  1054. return res
  1055. elif key.code == KeyCode.KEY_Right and key.mask & KeyMask.ControlMask:
  1056. res = self._editor.control_arrow_right ()
  1057. self._update_ui ()
  1058. return res
  1059. elif key.code == KeyCode.KEY_Left:
  1060. res = self._editor.arrow_left ()
  1061. self._update_ui ()
  1062. return res
  1063. elif key.code == KeyCode.KEY_Right:
  1064. res = self._editor.arrow_right ()
  1065. self._update_ui ()
  1066. return res
  1067. elif key.code == KeyCode.KEY_Delete and key.mask & KeyMask.ControlMask:
  1068. res = self._editor.control_delete ()
  1069. self._update_ui ()
  1070. return res
  1071. elif key.code == KeyCode.KEY_Delete:
  1072. res = self._editor.delete ()
  1073. self._update_ui ()
  1074. return res
  1075. elif key.code >= KeyCode.KEY_1 and key.code <= KeyCode.KEY_9 and self._editor._candidates[0] and key.mask & KeyMask.ControlMask:
  1076. res = self._editor.number (key.code - KeyCode.KEY_1)
  1077. self._update_ui ()
  1078. return res
  1079. elif key.code >= KeyCode.KEY_1 and key.code <= KeyCode.KEY_9 and self._editor._candidates[0] and key.mask & KeyMask.AltMask:
  1080. res = self._editor.alt_number (key.code - KeyCode.KEY_1)
  1081. self._update_ui ()
  1082. return res
  1083. # now we ignore all else hotkeys
  1084. elif key.mask & KeyMask.ControlMask+KeyMask.AltMask:
  1085. return False
  1086. elif key.mask & KeyMask.AltMask:
  1087. return False
  1088. elif unichr(key.code) in self._valid_input_chars or \
  1089. ( self._editor._py_mode and \
  1090. unichr(key.code) in u'abcdefghijklmnopqrstuvwxyz' ):
  1091. if self._direct_commit and len(self._editor._chars[0]) == self._ml:
  1092. # it is time to direct commit
  1093. sp_res = self._editor.space ()
  1094. #return (whethercommit,commitstring)
  1095. if sp_res[0]:
  1096. self.commit_string (sp_res[1])
  1097. self.db.check_phrase (sp_res[1])
  1098. res = self._editor.add_input ( unichr(key.code) )
  1099. if not res:
  1100. if ascii.ispunct (key.code):
  1101. key_char = cond_punct_translate (unichr (key.code))
  1102. else:
  1103. key_char = cond_letter_translate (unichr (key.code))
  1104. sp_res = self._editor.space ()
  1105. self._update_ui ()
  1106. #return (KeyProcessResult,whethercommit,commitstring)
  1107. if sp_res[0]:
  1108. self.commit_string (sp_res[1] + key_char)
  1109. self.db.check_phrase (sp_res[1])
  1110. else:
  1111. self.commit_string ( key_char )
  1112. else:
  1113. if self._direct_commit and self._editor.one_candidate () \
  1114. and len(self._editor._chars[0]) == self._ml:
  1115. # it is time to direct commit
  1116. sp_res = self._editor.space ()
  1117. #return (whethercommit,commitstring)
  1118. if sp_res[0]:
  1119. self.commit_string (sp_res[1])
  1120. self.db.check_phrase (sp_res[1])
  1121. self._update_ui ()
  1122. return True
  1123. elif key.code in (KeyCode.KEY_equal, KeyCode.KEY_Page_Down, KeyCode.KEY_KP_Page_Down) and self._editor._candidates[0]:
  1124. res = self._editor.page_down()
  1125. self._update_ui ()
  1126. return res
  1127. elif key.code in (KeyCode.KEY_minus, KeyCode.KEY_Page_Up, KeyCode.KEY_KP_Page_Up) and self._editor._candidates[0]:
  1128. res = self._editor.page_up ()
  1129. self._update_ui ()
  1130. return res
  1131. elif key.code >= KeyCode.KEY_1 and key.code <= KeyCode.KEY_9 and self._editor._candidates[0]:
  1132. res = self._editor.number (key.code - KeyCode.KEY_1)
  1133. if res:
  1134. commit_string = self._editor.get_preedit_strings ()
  1135. self._editor.clear ()
  1136. self.commit_string (commit_string)
  1137. # modify freq info
  1138. self.db.check_phrase (commit_string)
  1139. self._refresh_properties ()
  1140. self._update_ui ()
  1141. return True
  1142. elif key.code <= 127:
  1143. if not self._editor._candidates[0]:
  1144. commit_string = self._editor.get_all_input_strings ()
  1145. else:
  1146. self._editor.commit_to_preedit ()
  1147. commit_string = self._editor.get_preedit_strings ()
  1148. self._editor.clear ()
  1149. if ascii.ispunct (key.code):
  1150. self.commit_string ( commit_string + cond_punct_translate (unichr (key.code)))
  1151. else:
  1152. self.commit_string ( commit_string + cond_letter_translate (unichr (key.code)))
  1153. self._update_ui ()
  1154. return True
  1155. return False
  1156. # below for initial test
  1157. def focus_in (self):
  1158. self._init_properties ()
  1159. IMEngine.focus_in (self)
  1160. self._update_ui ()
  1161. def focus_out (self):
  1162. IMEngine.focus_out (self)
  1163. def lookup_table_page_up (self):
  1164. if self._editor.page_up ():
  1165. self._update_lookup_table ()
  1166. return True
  1167. IMEngine.lookup_table_page_up (self)
  1168. return True
  1169. def lookup_table_page_down (self):
  1170. if self._editor.page_down ():
  1171. self._update_lookup_table ()
  1172. return True
  1173. IMEngine.lookup_table_page_down (self)
  1174. return False
  1175. def reload_config (self, config):
  1176. self._lookup_table.set_page_size (XingMaEngine._page_size)
  1177. self.focus_in ()
  1178. class Factory (IMEngineFactory):
  1179. """XingMa IM Engine Factory"""
  1180. def __init__ (self, config, db):
  1181. import os.path
  1182. import locale
  1183. IMEngineFactory.__init__ (self, config)
  1184. # this is the backend sql db we need for our IME
  1185. # we need get lots of IME property from this db :)
  1186. #self.db = XMSQLiteDB.XMSQLiteDB( name = database )
  1187. udb = os.path.basename(db).replace('.db','-user.db')
  1188. self.db = XMSQLiteDB.XMSQLiteDB( name = db,user_db = udb )
  1189. self.dbname = db
  1190. ulocale = locale.getdefaultlocale ()[0].lower()
  1191. self.name = self.db.get_ime_property ('name.%s' % ulocale)
  1192. if not self.name:
  1193. self.name = _( self.db.get_ime_property ('name') )
  1194. self.uuid = self.db.get_ime_property ('uuid')
  1195. self.authors = self.db.get_ime_property ('author')
  1196. self.icon_file = self.db.get_ime_property ('icon')
  1197. self.credits = self.db.get_ime_property ('credit')
  1198. self.help = _(u"Help For Python XingMa\n\tPlease read http://code.google.com/p/scim-python/wiki/XingMaUserGuide")
  1199. # init factory
  1200. self._config = config
  1201. self.set_languages ( self.db.get_ime_property ('languages') )
  1202. # self.reload_config (config)
  1203. self.db.db.commit()
  1204. def destroy (self):
  1205. '''Destructor, which finish some task for IME'''
  1206. # we need to sync the temp userdb in memory to the user_db on disk
  1207. self.db.sync_usrdb ()
  1208. def create_instance (self, encoding, id):
  1209. engine = XingMaEngine (self, self._config, encoding, id, self.db)
  1210. return engine
  1211. def reload_config (self, config):
  1212. pass
  1213. # XingMaEngine._page_size = \
  1214. # config.read ("/IMEngine/Python/XingMa/PageSize", 5)
  1215. # if XingMaEngine._page_size < 1 or XingMaEngine._page_size > 9:
  1216. # XingMaEngine._page_size = 5
  1217. # XingMaEngine._phrase_color = \
  1218. # config.read ("/IMEngine/Python/XingMa/PhraseColor", XingMaEngine._phrase_color)
  1219. # XingMaEngine._new_phrase_color = \
  1220. # config.read ("/IMEngine/Python/XingMa/NewPhraseColor", XingMaEngine._new_phrase_color)
  1221. # XingMaEngine._user_phrase_color = \
  1222. # config.read ("/IMEngine/Python/XingMa/UserPhraseColor", XingMaEngine._user_phrase_color)