U
    ȇw^I                     @   s   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlZdd ZG d	d
 d
Zdd Zedkre  dS )    )print_functionN)GLib)Gtk)IBusBaseDirectoryc                 C   s   t d| S )Nibus-pinyin)gettextdgettext)a r   $/usr/share/ibus-pinyin/setup/main.py<lambda>%       r   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!S )"PreferencesDialogc                 C   s>  t t jd td}td| tdd t	 | _
| j
 | _t | _| jd | jd | jd| _|   |dkrd| _t| j| j| _|   |   |   |   |   nd|d	kr d
| _t| j| j| _|   |   |   |   |   |   nt d t!  | j"#d d S )N ZIBUS_LOCALEDIRr   zUTF-8zibus-pinyin-preferences.uiZdialogpinyinzengine/Pinyinbopomofozengine/BopomofozError: Unknown Enginer   )$locale	setlocaleLC_ALLosgetenvr	   bindtextdomainbind_textdomain_codesetr   ZBusZ_PreferencesDialog__busZ
get_config_PreferencesDialog__configr   ZBuilder_PreferencesDialog__builderZset_translation_domainZadd_from_file
get_object_PreferencesDialog__dialog_PreferencesDialog__init_pages$_PreferencesDialog__config_namespacedictZ
get_values_PreferencesDialog__values _PreferencesDialog__init_general_PreferencesDialog__init_pinyin_PreferencesDialog__init_fuzzy#_PreferencesDialog__init_dictionary_PreferencesDialog__init_about!_PreferencesDialog__init_bopomofo4_PreferencesDialog__convert_fuzzy_pinyin_to_bopomofoprintexit_PreferencesDialog__pagesZset_current_page)selfZengineZ	localedirr   r   r   __init__(   s>    





zPreferencesDialog.__init__c                 C   s   | j d| _| j d| _| j d| _| j d| _| j d| _| j d| _| j d| _| j	  | j	  | j	  | j	  | j	  | j	  d S )NZpagesZpageGeneralZpagePinyinModeZpageBopomofoModeZ	pageFuzzyZpageDictionaryZ	pageAbout)
r   r   r,    _PreferencesDialog__page_general$_PreferencesDialog__page_pinyin_mode&_PreferencesDialog__page_bopomofo_mode_PreferencesDialog__page_fuzzy#_PreferencesDialog__page_dictionary_PreferencesDialog__page_aboutZhider-   r   r   r   Z__init_pagesM   s    




zPreferencesDialog.__init_pagesc                    s   j    jd _ jd _ jd _ jd _ jd _ jd _	 jd _
 jd _ jd	 _ jd
 _ j dd  j dd  j dd  j
 dd  j d
d  j d	d  jd jd  jd jd  jd jd  j
d jd  fdd} fdd} jd|  jd| d S )NZInitChineseZInitEnglishZInitFullZInitHalfZInitFullPunctZInitHalfPunctZInitSimplifiedChineseZIniTraditionalChineseLookupTablePageSizeLookupTableOrientationTFr      toggledc                    s     dt|   d S )Nr6   )_PreferencesDialog__set_valueintZ	get_value)Z
adjustmentr5   r   r   Z#__lookup_table_page_size_changed_cb}   s    zMPreferencesDialog.__init_general.<locals>.__lookup_table_page_size_changed_cbc                    s     d|   d S )Nr7   r:   
get_activewidgetr5   r   r   Z%__lookup_table_orientation_changed_cb   s    zOPreferencesDialog.__init_general.<locals>.__lookup_table_orientation_changed_cbchangedzvalue-changed)r/   showr   r   Z _PreferencesDialog__init_chineseZ _PreferencesDialog__init_englishZ_PreferencesDialog__init_fullZ_PreferencesDialog__init_halfZ#_PreferencesDialog__init_full_punctZ#_PreferencesDialog__init_half_punctZ_PreferencesDialog__init_simpZ_PreferencesDialog__init_tradZ*_PreferencesDialog__lookup_table_page_sizeZ,_PreferencesDialog__lookup_table_orientation
set_active_PreferencesDialog__get_value	set_valueconnect_PreferencesDialog__toggled_cb)r-   Z5_PreferencesDialog__lookup_table_page_size_changed_cbZ7_PreferencesDialog__lookup_table_orientation_changed_cbr   r5   r   Z__init_general]   s2    
z PreferencesDialog.__init_generalc                    s6   j    jd _ jd _ jd _ jd _ jd _ j	 
dd  j	 
dd   j	 
dd  j r jd  jd n jd  jd  fd	d
} fdd} jd|  jd jd  jd|  jd jd       d S )NZ
FullPinyinIncompletePinyinDoublePinyinDoublePinyinSchemaZDoublePinyinShowRawTFr   c                    s0   |   } d|  j|  j| d S )NrH   )r=   r:   (_PreferencesDialog__double_pinyin_schemaset_sensitive*_PreferencesDialog__double_pinyin_show_raw)r?   valr5   r   r   Z__double_pinyin_toggled_cb   s    zCPreferencesDialog.__init_pinyin.<locals>.__double_pinyin_toggled_cbc                    s     d|   d S )NrI   r<   r>   r5   r   r   Z!__double_pinyin_schema_changed_cb   s    zJPreferencesDialog.__init_pinyin.<locals>.__double_pinyin_schema_changed_cbr9   r@   )r0   rA   r   r   Z_PreferencesDialog__full_pinyinZ%_PreferencesDialog__incomplete_pinyinZ!_PreferencesDialog__double_pinyinrJ   rL   rB   rC   r=   rK   rE   rF   %_PreferencesDialog__init_input_custom'_PreferencesDialog__init_correct_pinyin)r-   Z,_PreferencesDialog__double_pinyin_toggled_cbZ3_PreferencesDialog__double_pinyin_schema_changed_cbr   r5   r   Z__init_pinyin   s,    

zPreferencesDialog.__init_pinyinc                    s   j    jd _ jd _ jd _ jd _ jd _ jd _	 jd _
 j dd  j d	d
  j dd  j dd  j dd  j	 dd  j
 dd  fdd} fdd} jd|  jd jd	  jd|  jd jd  jd jd  j	d jd  j
d jd d S )NZIncompleteBopomofoBopomofoKeyboardMapping
SelectKeysZGuideKeyZAuxiliarySelectKey_FZAuxiliarySelectKey_KPZCommitFirstCandidater   rG   F   ZEnterKeyTc                    s     d|   d S )NrP   r<   r>   r5   r   r   Z&__bopomofo_keyboard_mapping_changed_cb   s    zQPreferencesDialog.__init_bopomofo.<locals>.__bopomofo_keyboard_mapping_changed_cbc                    s     d|   d S )NrQ   r<   r>   r5   r   r   Z__select_keys_changed_cb   s    zCPreferencesDialog.__init_bopomofo.<locals>.__select_keys_changed_cbr@   r9   )r1   rA   r   r   Z'_PreferencesDialog__incomplete_bopomofoZ-_PreferencesDialog__bopomofo_keyboard_mappingZ_PreferencesDialog__select_keysZ_PreferencesDialog__guide_keyZ*_PreferencesDialog__auxiliary_select_key_fZ+_PreferencesDialog__auxiliary_select_key_kpZ_PreferencesDialog__enter_keyrB   rC   rE   rF   )r-   Z8_PreferencesDialog__bopomofo_keyboard_mapping_changed_cbZ*_PreferencesDialog__select_keys_changed_cbr   r5   r   Z__init_bopomofo   s0    
z!PreferencesDialog.__init_bopomofoc                 C   s   | j d| _| j d| _| j d| _| j d| _| j| dd | j| dd | j| dd | j| dd | jd| j	d | jd| j	d | jd| j	d | jd| j	d d S )NZShiftSelectCandidateZMinusEqualPageZCommaPeriodPageZ
AutoCommitFTr9   )
r   r   Z*_PreferencesDialog__shift_select_candidateZ$_PreferencesDialog__minus_equal_pageZ%_PreferencesDialog__comma_period_pageZ_PreferencesDialog__auto_commitrB   rC   rE   rF   r5   r   r   r   Z__init_input_custom   s    z%PreferencesDialog.__init_input_customc                    s    j d _dddddddd	g _ fd
d} jd|  j dd  jD ]&\}} j |}| || qX jd jd  jD ]$\}} j |}|d j| qd S )NZCorrectPinyin)ZCorrectPinyin_GN_NGT)ZCorrectPinyin_MG_NGT)ZCorrectPinyin_IOU_IUT)ZCorrectPinyin_UEI_UIT)ZCorrectPinyin_UEN_UNT)ZCorrectPinyin_UE_VET)ZCorrectPinyin_V_UT)ZCorrectPinyin_ON_ONGTc                    s.   |   } jD ]} j|d | qd S Nr   )r=   *_PreferencesDialog__correct_pinyin_widgetsr   r   rK   r?   rM   wr5   r   r   Z__correct_pinyin_toggled_cb   s    
zLPreferencesDialog.__init_correct_pinyin.<locals>.__correct_pinyin_toggled_cbr9   T)r   r   Z"_PreferencesDialog__correct_pinyinrT   rE   rB   rC   rF   )r-   Z-_PreferencesDialog__correct_pinyin_toggled_cbnamedefvalr?   r   r5   r   Z__init_correct_pinyin   s(    z'PreferencesDialog.__init_correct_pinyinc                    s    j    jd _dddddddd	d
dddddddddddg _ fdd} jd|  j dd  jD ]&\}} j|}| || qz jd j	d  jD ]$\}} j|}|d j	| qd S )NZFuzzyPinyin)FuzzyPinyin_C_CHT)FuzzyPinyin_Z_ZHT)FuzzyPinyin_S_SHT)FuzzyPinyin_CH_CF)FuzzyPinyin_ZH_ZF)FuzzyPinyin_SH_SF)FuzzyPinyin_L_NT)FuzzyPinyin_F_HT)FuzzyPinyin_L_RF)FuzzyPinyin_K_GT)FuzzyPinyin_N_LF)FuzzyPinyin_H_FF)FuzzyPinyin_R_LF)FuzzyPinyin_G_KF)FuzzyPinyin_AN_ANGT)FuzzyPinyin_EN_ENGT)FuzzyPinyin_IN_INGT)FuzzyPinyin_ANG_ANT)FuzzyPinyin_ENG_ENT)FuzzyPinyin_ING_INTc                    s.   |   } jD ]} j|d | qd S rS   )r=   (_PreferencesDialog__fuzzy_pinyin_widgetsr   r   rK   rU   r5   r   r   Z__fuzzy_pinyin_toggled_cb/  s    
zAPreferencesDialog.__init_fuzzy.<locals>.__fuzzy_pinyin_toggled_cbr9   F)
r2   rA   r   r   Z _PreferencesDialog__fuzzy_pinyinrm   rE   rB   rC   rF   )r-   Z+_PreferencesDialog__fuzzy_pinyin_toggled_cbrW   rX   r?   r   r5   r   Z__init_fuzzy  sB    
zPreferencesDialog.__init_fuzzyc                 C   sP   ddddddddd	d
ddddddddddg}|D ]\}}| j || q0d S )N)rY   u
   ㄘ => ㄔ)rZ   u
   ㄗ => ㄓ)r[   u
   ㄙ => ㄕ)r\   u
   ㄔ => ㄘ)r]   u
   ㄓ => ㄗ)r^   u
   ㄕ => ㄙ)r_   u
   ㄌ => ㄋ)r`   u
   ㄈ => ㄏ)ra   u
   ㄌ => ㄖ)rb   u
   ㄎ => ㄍ)rc   u
   ㄋ => ㄌ)rd   u
   ㄏ => ㄈ)re   u
   ㄖ => ㄌ)rf   u
   ㄍ => ㄎ)rg   u
   ㄢ => ㄤ)rh   u
   ㄣ => ㄥ)ri   u   ㄧㄣ => ㄧㄥ)rj   u
   ㄤ => ㄢ)rk   u
   ㄥ => ㄣ)rl   u   ㄧㄥ => ㄧㄣ)r   r   Z	set_label)r-   ZoptionsrW   Zlabelr   r   r   Z"__convert_fuzzy_pinyin_to_bopomofo@  s.    z4PreferencesDialog.__convert_fuzzy_pinyin_to_bopomofoc                 C   sf   | j   | jd| _| jd| _| j| dd dd }| jd| j	d | jd| d S )NZSpecialPhrasesZEditSpecialPhrasesTc                 S   s   ddl m} dd l}tj|jdd}tj|p:t| tj|d}tj|st	dpbd}tj|dd}|
|| td	|  d S )
Nr   r   Zibusr   zphrases.txtZIBUS_DATAROOTDIRz
/usr/sharer   zxdg-open %s)xdgr   shutilr   pathjoinZxdg_config_homeexistsmakedirsr   Zcopyfilesystem)r?   r   ro   rp   Zdatadirsrcr   r   r   Z!__edit_special_phrases_clicked_cbk  s    zNPreferencesDialog.__init_dictionary.<locals>.__edit_special_phrases_clicked_cbr9   Zclicked)
r3   rA   r   r   Z#_PreferencesDialog__special_phrasesZ(_PreferencesDialog__edit_special_phrasesrB   rC   rE   rF   )r-   Z3_PreferencesDialog__edit_special_phrases_clicked_cbr   r   r   Z__init_dictionary`  s    
z#PreferencesDialog.__init_dictionaryc                 C   s4   | j   | jd| _| jtdt   d S )NZNameVersionz <big><b>IBus Pinyin %s</b></big>)	r4   rA   r   r   Z _PreferencesDialog__name_versionZ
set_markup_versionZget_versionr5   r   r   r   Z__init_about{  s    
zPreferencesDialog.__init_aboutc                 C   s   |  ||  d S Nr<   r-   r?   rW   r   r   r   Z__changed_cb  s    zPreferencesDialog.__changed_cbc                 C   s   |  ||  d S rx   r<   ry   r   r   r   Z__toggled_cb  s    zPreferencesDialog.__toggled_cbc                 C   s6   || j kr&| j | }t|t|r&|S | || |S rx   )r"   
isinstancetyper:   )r-   rW   rX   varr   r   r   Z__get_value  s    

zPreferencesDialog.__get_valuec                 C   s   d }t |trtj|}nJt |tr4tj|}n2t |trLtj|}nt	dt
| tjd d S || j|< | j| j|| d S )Nzval(%s) is not in support type.)file)rz   boolr   ZVariantZnew_booleanr;   Z	new_int32strZ
new_stringr*   reprsysstderrr"   r   rD   r    )r-   rW   rM   r|   r   r   r   Z__set_value  s    



zPreferencesDialog.__set_valuec                 C   s
   | j  S rx   )r   runr5   r   r   r   r     s    zPreferencesDialog.runN)__name__
__module____qualname__r.   r   r#   r$   r(   rN   rO   r%   r)   r&   r'   Z_PreferencesDialog__changed_cbrF   rC   r:   r   r   r   r   r   r   '   s    %).(2 r   c                  C   s8   d} t tjdkrtjd } | dkr(d} t|   d S )Nr      rR   )r   r   )lenr   argvr   r   )rW   r   r   r   main  s    
r   __main__)Z
__future__r   r	   r   r   r   Zgi.repositoryr   r   r   rn   r   rw   rv   r   r   r   r   r   r   r   <module>   s      }	