Changeset 3697e213ec444bb5343f5b796c8ce8a664d06086

Show
Ignore:
Timestamp:
12/18/08 01:53:30 (3 years ago)
Author:
Lincoln de Sousa <lincoln@…>
Children:
b7778a1ef9c982ea3c280f070f4ef9ac9968e6b7
Parents:
1f4c86616ef3c1e2c955fd3ae621ea6421262596
git-committer:
Lincoln de Sousa <lincoln@…> (12/18/08 01:53:30)
Message:

Making prefs compatible with new schema
improving its code, and adding some more options

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/prefs.py

    r12326de r3697e21  
    1919import re 
    2020import os 
     21import warnings 
    2122 
    2223import gtk 
     
    2526 
    2627from simplegladeapp import SimpleGladeApp 
    27 from guake_globals import GCONF_PATH 
     28from guake_globals import GCONF_PATH, KEY 
    2829from common import * 
    2930import globalhotkeys 
     
    4546# These tuples are going to be used to build a treeview with the 
    4647# hotkeys used in guake preferences. 
    47 GCONF_KEYS = GCONF_PATH + 'keybindings/' 
    48 GHOTKEYS = ((GCONF_KEYS+'global/show_hide', _('Toggle terminal visibility')),) 
    49 LHOTKEYS = ((GCONF_KEYS+'local/new_tab', _('New tab'),), 
    50             (GCONF_KEYS+'local/close_tab', _('Close tab')), 
    51             (GCONF_KEYS+'local/previous_tab', _('Go to previous tab')), 
    52             (GCONF_KEYS+'local/next_tab', _('Go to next tab'),), 
    53             (GCONF_KEYS+'local/rename_tab', _('Rename current tab'),), 
    54             (GCONF_KEYS+'local/clipboard_copy', _('Copy text to clipboard'),), 
    55             (GCONF_KEYS+'local/clipboard_paste', _('Paste text from clipboard'),), 
    56             (GCONF_KEYS+'local/toggle_fullscreen', _('Toggle Fullscreen'),), 
     48HKEYS = lambda x:GCONF_PATH + '/keybindings' + x 
     49GHOTKEYS = ((HKEYS('/global/show_hide'), _('Toggle terminal visibility')),) 
     50LHOTKEYS = ((HKEYS('/local/new_tab'), _('New tab'),), 
     51            (HKEYS('/local/close_tab'), _('Close tab')), 
     52            (HKEYS('/local/previous_tab'), _('Go to previous tab')), 
     53            (HKEYS('/local/next_tab'), _('Go to next tab'),), 
     54            (HKEYS('/local/rename_tab'), _('Rename current tab'),), 
     55            (HKEYS('/local/clipboard_copy'), _('Copy text to clipboard'),), 
     56            (HKEYS('/local/clipboard_paste'), _('Paste text from clipboard'),), 
     57            (HKEYS('/local/toggle_fullscreen'), _('Toggle Fullscreen'),), 
    5758) 
    5859 
    59 class KeyEntry(object): 
    60     def __init__(self, keycode, mask): 
    61         self.keycode = keycode 
    62         self.mask = mask 
    63  
    64     def __repr__(self): 
    65         return u'KeyEntry(%d, %d)' % ( 
    66             self.keycode, self.mask) 
    67  
    68     def __eq__(self, rval): 
    69         return self.keycode == rval.keycode and \ 
    70             self.mask == rval.mask 
     60class PrefsCallbacks(object): 
     61    """Holds callbacks that will be used in the PrefsDialg class. 
     62    """ 
     63 
     64    def __init__(self): 
     65        self.client = gconf.client_get_default() 
     66 
     67    # general tab 
     68 
     69    def on_default_shell_changed(self, combo): 
     70        """Changes the activity of default_shell in gconf 
     71        """ 
     72        citer = combo.get_active_iter() 
     73        if not citer: 
     74            return 
     75        shell = combo.get_model().get_value(citer, 0) 
     76        self.client.set_string(KEY('/general/default_shell'), shell) 
     77 
     78    def on_use_login_shell_toggled(self, chk): 
     79        """Changes the activity of use_login_shell in gconf 
     80        """ 
     81        self.client.set_bool(KEY('/general/use_login_shell'), chk.get_active()) 
     82 
     83    def on_use_trayicon_toggled(self, chk): 
     84        """Changes the activity of use_trayicon in gconf 
     85        """ 
     86        self.client.set_bool(KEY('/general/use_trayicon'), chk.get_active()) 
     87 
     88    def on_use_popup_toggled(self, chk): 
     89        """Changes the activity of use_popup in gconf 
     90        """ 
     91        self.client.set_bool(KEY('/general/use_popup'), chk.get_active()) 
     92 
     93    def on_window_ontop_toggled(self, chk): 
     94        """Changes the activity of window_ontop in gconf 
     95        """ 
     96        self.client.set_bool(KEY('/general/window_ontop'), chk.get_active()) 
     97 
     98    def on_window_losefocus_toggled(self, chk): 
     99        """Changes the activity of window_losefocus in gconf 
     100        """ 
     101        self.client.set_bool(KEY('/general/window_losefocus'), chk.get_active()) 
     102 
     103    def on_window_tabbar_toggled(self, chk): 
     104        """Changes the activity of window_tabbar in gconf 
     105        """ 
     106        self.client.set_bool(KEY('/general/window_tabbar'), chk.get_active()) 
     107 
     108    def on_window_size_value_changed(self, hscale): 
     109        """Changes the value of window_size in gconf 
     110        """ 
     111        val = hscale.get_value() 
     112        self.client.set_int(KEY('/general/window_size'), int(val)) 
     113 
     114    # scrolling tab 
     115 
     116    def on_use_scrollbar_toggled(self, chk): 
     117        """Changes the activity of use_scrollbar in gconf 
     118        """ 
     119        self.client.set_bool(KEY('/general/use_scrollbar'), chk.get_active()) 
     120 
     121    def on_history_size_value_changed(self, spin): 
     122        """Changes the value of history_size in gconf 
     123        """ 
     124        val = int(spin.get_value()) 
     125        self.client.set_int(KEY('/general/history_size'), val) 
     126 
     127    def on_scroll_output_toggled(self, chk): 
     128        """Changes the activity of scroll_output in gconf 
     129        """ 
     130        self.client.set_bool(KEY('/general/scroll_output'), chk.get_active()) 
     131 
     132    def on_scroll_keystroke_toggled(self, chk): 
     133        """Changes the activity of scroll_keystroke in gconf 
     134        """ 
     135        self.client.set_bool(KEY('/general/scroll_keystroke'), chk.get_active()) 
     136 
     137    # appearance tab 
     138 
     139    def on_use_default_font_toggled(self, chk): 
     140        """Changes the activity of use_default_font in gconf 
     141        """ 
     142        self.client.set_bool(KEY('/general/use_default_font'), chk.get_active()) 
     143 
     144    def on_font_style_font_set(self, fbtn): 
     145        """Changes the value of font_style in gconf 
     146        """ 
     147        self.client.set_string(KEY('/style/font/style'), fbtn.get_font_name()) 
     148 
     149    def on_font_color_color_set(self, btn): 
     150        """Changes the value of font_color in gconf 
     151        """ 
     152        color = hexify_color(btn.get_color()) 
     153        self.client.set_string(KEY('/style/font/color'), color) 
     154 
     155    def on_background_color_color_set(self, btn): 
     156        """Changes the value of background_color in gconf 
     157        """ 
     158        color = hexify_color(btn.get_color()) 
     159        self.client.set_string(KEY('/style/background/color'), color) 
     160 
     161    def on_background_image_changed(self, btn): 
     162        """Changes the value of background_image in gconf 
     163        """ 
     164        filename = btn.get_filename() 
     165        if filename: 
     166            self.client.set_string(KEY('/style/background/image'), filename) 
     167 
     168    def on_opacity_value_changed(self, hscale): 
     169        """Changes the value of background_opacity in gconf 
     170        """ 
     171        value = hscale.get_value() 
     172        self.client.set_int(KEY('/style/background/opacity'), int(value)) 
     173 
    71174 
    72175class PrefsDialog(SimpleGladeApp): 
    73     def __init__(self, guakeinstance): 
     176    """The Guake Preferences dialog. 
     177    """ 
     178    def __init__(self): 
     179        """Setup the preferences dialog interface, loading images, 
     180        adding filters to file choosers and connecting some signals. 
     181        """ 
    74182        super(PrefsDialog, self).__init__(gladefile('prefs.glade'), 
    75183                                          root='config-window') 
    76  
    77         self.guake = guakeinstance 
     184        self.add_callbacks(PrefsCallbacks()) 
     185 
    78186        self.client = gconf.client_get_default() 
    79187 
     
    128236        self.file_filter.add_pattern("*.svg") 
    129237        self.file_filter.add_pattern("*.jpeg") 
    130         self.bgfilechooser = self.get_widget('bgimage-filechooserbutton') 
     238        self.bgfilechooser = self.get_widget('background_image') 
    131239        self.bgfilechooser.set_preview_widget(self.selection_preview) 
    132240        self.bgfilechooser.set_filter(self.file_filter) 
     
    135243 
    136244    def show(self): 
     245        """Calls the main window show_all method and presents the 
     246        window in the desktop. 
     247        """ 
    137248        self.get_widget('config-window').show_all() 
    138249        self.get_widget('config-window').present() 
    139250 
    140251    def hide(self): 
     252        """Calls the main window hide function. 
     253        """ 
    141254        self.get_widget('config-window').hide() 
    142255 
     
    144257        # backspace erase binding 
    145258        combo = self.get_widget('backspace-binding-combobox') 
    146         binding = self.client.get_string(GCONF_PATH+'general/compat_backspace') 
     259        binding = self.client.get_string(KEY('/general/compat_backspace')) 
    147260        model = combo.get_model() 
    148261        bindex = ERASE_BINDINGS.values().index(binding) 
     
    154267        # delete erase binding 
    155268        combo = self.get_widget('delete-binding-combobox') 
    156         binding = self.client.get_string(GCONF_PATH+'general/compat_delete') 
     269        binding = self.client.get_string(KEY('/general/compat_delete')) 
    157270        model = combo.get_model() 
    158271        bindex = ERASE_BINDINGS.values().index(binding) 
     
    163276 
    164277    def load_configs(self): 
    165         # shells list 
    166         default = self.client.get_string(GCONF_PATH + 'general/default_shell') 
    167         combo = self.get_widget('shells-combobox') 
    168         model = combo.get_model() 
    169         for i in model: 
    170             value = model.get_value(i.iter, 0) 
    171             if value == default: 
     278        """Load configurations for all widgets in General, Scrolling 
     279        and Appearance tabs from gconf. 
     280        """ 
     281        # default_shell 
     282        combo = self.get_widget('default_shell') 
     283        for i in combo.get_model(): 
     284            if i[0] == self.client.get_string(KEY('/general/default_shell')): 
    172285                combo.set_active_iter(i.iter) 
    173286 
     287        # login shell 
     288        value = self.client.get_bool(KEY('/general/use_login_shell')) 
     289        self.get_widget('use_login_shell').set_active(value) 
     290 
     291        # tray icon 
     292        value = self.client.get_bool(KEY('/general/use_trayicon')) 
     293        self.get_widget('use_trayicon').set_active(value) 
     294 
     295        # popup 
     296        value = self.client.get_bool(KEY('/general/use_popup')) 
     297        self.get_widget('use_popup').set_active(value) 
     298 
     299        # ontop 
     300        value = self.client.get_bool(KEY('/general/window_ontop')) 
     301        self.get_widget('window_ontop').set_active(value) 
     302 
     303        # losefocus 
     304        value = self.client.get_bool(KEY('/general/window_losefocus')) 
     305        self.get_widget('window_losefocus').set_active(value) 
     306 
     307        # tabbar 
     308        value = self.client.get_bool(KEY('/general/window_tabbar')) 
     309        self.get_widget('window_tabbar').set_active(value) 
     310 
     311        # size 
     312        value = float(self.client.get_int(KEY('/general/window_size'))) 
     313        self.get_widget('window_size').set_value(value) 
     314 
     315        # scrollbar 
     316        value = self.client.get_bool(KEY('/general/use_scrollbar')) 
     317        self.get_widget('use_scrollbar').set_active(value) 
     318 
    174319        # history size 
    175         val = self.client.get_int(GCONF_PATH+'general/history_size') 
    176         self.get_widget('historysize-spinbutton').set_value(val) 
    177  
    178         # scrollbar 
    179         ac = self.client.get_bool(GCONF_PATH + 'general/use_scrollbar') 
    180         self.get_widget('show-scrollbar-checkbutton').set_active(ac) 
    181  
    182         # Tray icon 
    183         ac = self.client.get_bool(GCONF_PATH + 'general/use_trayicon') 
    184         self.get_widget('show-trayicon-checkbutton').set_active(ac) 
    185  
    186         # hide on lost focus 
    187         ac = self.client.get_bool(GCONF_PATH + 'general/hide_on_lost_focus') 
    188         self.get_widget('hide-onlostfocus-checkbutton').set_active(ac) 
    189  
    190         # animate flag 
    191         #ac = self.client.get_bool(GCONF_PATH + 'general/window_animate') 
    192         #self.get_widget('animate-checkbutton').set_active(ac) 
    193  
    194         # on top flag 
    195         ac = self.client.get_bool(GCONF_PATH + 'general/window_ontop') 
    196         self.get_widget('ontop-checkbutton').set_active(ac) 
    197  
    198         # winsize 
    199         val = float(self.client.get_int(GCONF_PATH + 'general/window_size')) 
    200         self.get_widget('winsize-hscale').set_value(val) 
     320        value = self.client.get_int(KEY('/general/history_size')) 
     321        self.get_widget('history_size').set_value(value) 
     322 
     323        # scroll output 
     324        value = self.client.get_bool(KEY('/general/scroll_output')) 
     325        self.get_widget('scroll_output').set_active(value) 
     326 
     327        # scroll keystroke 
     328        value = self.client.get_bool(KEY('/general/scroll_keystroke')) 
     329        self.get_widget('scroll_keystroke').set_active(value) 
     330 
     331        # default font 
     332        value = self.client.get_bool(KEY('/style/use_default_font')) 
     333        self.get_widget('use_default_font').set_active(value) 
    201334 
    202335        # font 
    203         val = self.client.get_string(GCONF_PATH + 'style/font/style') 
    204         self.get_widget('fontbutton').set_font_name(val) 
    205  
    206         val = self.client.get_string(GCONF_PATH + 'style/font/color') 
     336        value = self.client.get_string(KEY('/style/font/style')) 
     337        self.get_widget('font_style').set_font_name(value) 
     338 
     339        # font color 
     340        val = self.client.get_string(KEY('/style/font/color')) 
    207341        try: 
    208342            color = gtk.gdk.color_parse(val) 
    209             self.get_widget('font-colorbutton').set_color(color) 
     343            self.get_widget('font_color').set_color(color) 
    210344        except (ValueError, TypeError): 
    211345            warnings.warn('Unable to parse color %s' % val, Warning) 
    212346 
    213         # background 
    214         val = self.client.get_string(GCONF_PATH+'style/background/color') 
     347        # background color 
     348        value = self.client.get_string(KEY('/style/background/color')) 
    215349        try: 
    216             color = gtk.gdk.color_parse(val) 
    217             self.get_widget('bg-colorbutton').set_color(color) 
     350            color = gtk.gdk.color_parse(value) 
     351            self.get_widget('background_color').set_color(color) 
    218352        except (ValueError, TypeError): 
    219353            warnings.warn('Unable to parse color %s' % val, Warning) 
    220              
    221         use_bgimage = self.client.get_bool(GCONF_PATH+'general/use_bgimage') 
    222         self.get_widget('chk_bg_transparent').set_active(not use_bgimage) 
    223         self.get_widget('chk_bg_transparent').connect('toggled', self.on_chk_bg_transparent_toggled) 
    224          
    225         val = self.client.get_string(GCONF_PATH+'style/background/image') 
    226         self.get_widget('bgimage-filechooserbutton').set_filename(val) 
    227  
    228         val = self.client.get_int(GCONF_PATH+'style/background/transparency') 
    229         self.get_widget('transparency-hscale').set_value(val) 
     354 
     355        # background image 
     356        value = self.client.get_string(KEY('/style/background/image')) 
     357        self.get_widget('background_image').set_filename(value) 
     358 
     359        value = self.client.get_int(KEY('/style/background/opacity')) 
     360        self.get_widget('background_opacity').set_value(value) 
    230361 
    231362        # it's a separated method, to be reused. 
    232363        self.reload_erase_combos() 
    233364 
    234         # the terminal window can be opened and the user *must* see this window 
    235         self.get_widget('config-window').set_keep_above(True) 
    236  
    237365    # -- populate functions -- 
    238366 
    239367    def populate_shell_combo(self): 
    240         cb = self.get_widget('shells-combobox') 
     368        """Read the /etc/shells and looks for installed pythons to 
     369        fill the default_shell combobox. 
     370        """ 
     371        cb = self.get_widget('default_shell') 
    241372        if os.path.exists(SHELLS_FILE): 
    242373            lines = open(SHELLS_FILE).readlines() 
     
    293424 
    294425        self.get_widget('treeview-keys').expand_all() 
    295          
    296     # -- callbacks -- 
    297  
    298     def on_historysize_spinbutton_value_changed(self, spin): 
    299         val = int(spin.get_value()) 
    300         self.client.set_int(GCONF_PATH + 'general/history_size', val) 
    301          
    302     def on_show_scrollbar_checkbutton_toggled(self, chk): 
    303         fbool = chk.get_active() 
    304         self.client.set_bool(GCONF_PATH + 'general/use_scrollbar', fbool) 
    305         self.guake.toggle_scrollbars() 
    306  
    307     def on_show_trayicon_checkbutton_toggled(self, chk): 
    308         fbool = chk.get_active() 
    309         self.client.set_bool(GCONF_PATH + 'general/use_trayicon', fbool) 
    310         self.guake.toggle_trayicon() 
    311  
    312     def on_show_popup_checkbutton_toggled(self, chk): 
    313         fbool = chk.get_active() 
    314         self.client.set_bool(GCONF_PATH + 'general/use_popup', fbool) 
    315          
    316     def on_chk_lostfocus_toggled(self, chk): 
    317         fbool = chk.get_active() 
    318         self.client.set_bool(GCONF_PATH + 'general/hide_on_lost_focus', fbool) 
    319          
    320     def on_shells_combobox_changed(self, combo): 
    321         citer = combo.get_active_iter() 
    322         if not citer: 
    323             return 
    324         shell = combo.get_model().get_value(citer, 0) 
    325         self.client.set_string(GCONF_PATH + 'general/default_shell', shell) 
    326  
    327     def on_animate_checkbutton_toggled(self, bnt): 
    328         self.client.set_bool(GCONF_PATH + 'general/window_animate', 
    329                 bnt.get_active()) 
    330  
    331     def on_ontop_checkbutton_toggled(self, bnt): 
    332         self.client.set_bool(GCONF_PATH + 'general/window_ontop', 
    333                              bnt.get_active()) 
    334         self.guake.toggle_ontop() 
    335  
    336     def on_winsize_hscale_value_changed(self, hscale): 
    337         val = hscale.get_value() 
    338         self.client.set_int(GCONF_PATH + 'general/window_size', int(val)) 
    339         self.guake.resize(*self.guake.get_final_window_size()) 
    340  
    341     def on_fontbutton_font_set(self, fb): 
    342         self.client.set_string(GCONF_PATH + 'style/font/style', 
    343                 fb.get_font_name()) 
    344         self.guake.set_font() 
    345  
    346     def on_font_colorbutton_color_set(self, bnt): 
    347         c = hexify_color(bnt.get_color()) 
    348         self.client.set_string(GCONF_PATH + 'style/font/color', c) 
    349         self.guake.set_fgcolor() 
    350  
    351     def on_bg_colorbutton_color_set(self, bnt): 
    352         c = hexify_color(bnt.get_color()) 
    353         self.client.set_string(GCONF_PATH + 'style/background/color', c) 
    354         self.guake.set_bgcolor() 
    355  
    356     def on_bgimage_filechooserbutton_selection_changed(self, bnt): 
    357         f = bnt.get_filename() 
    358         if f: 
    359             self.client.set_string(GCONF_PATH + 'style/background/image', f) 
    360             self.guake.set_bgimage() 
    361  
    362     def on_chk_bg_transparent_toggled(self, togglebutton): 
    363         value = togglebutton.get_active() 
    364         self.client.set_bool(GCONF_PATH + 'general/use_bgimage', not value) 
    365         self.guake.set_bgimage() 
    366              
    367     def on_transparency_hscale_value_changed(self, hscale): 
    368         val = hscale.get_value() 
    369         self.client.set_int(GCONF_PATH + 'style/background/transparency', 
    370                 int(val)) 
    371         self.guake.set_alpha() 
     426 
     427    # ----------------------------------------------------------------------- 
    372428 
    373429    def on_backspace_binding_combobox_changed(self, combo): 
     
    375431        self.client.set_string(GCONF_PATH+'general/compat_backspace', 
    376432                               ERASE_BINDINGS[val]) 
    377         self.guake.set_erasebindings() 
    378433 
    379434    def on_delete_binding_combobox_changed(self, combo): 
     
    381436        self.client.set_string(GCONF_PATH+'general/compat_delete', 
    382437                               ERASE_BINDINGS[val]) 
    383         self.guake.set_erasebindings() 
    384438 
    385439    def on_reset_compat_defaults_button_clicked(self, bnt): 
     
    388442        self.client.unset(GCONF_PATH+'general/compat_delete') 
    389443        self.reload_erase_combos() 
    390         self.guake.set_erasebindings() 
    391444 
    392445    def on_key_edited(self, renderer, path, keycode, mask, keyval, model): 
     
    485538            renderer.set_property('accel-mods', 0) 
    486539 
    487     def update_preview_cb(self,file_chooser, preview): 
    488         """ 
    489         Used by filechooser to preview image files 
     540    def update_preview_cb(self, file_chooser, preview): 
     541        """Used by filechooser to preview image files 
    490542        """ 
    491543        filename = file_chooser.get_preview_filename() 
     
    497549                file_chooser.set_preview_widget_active(True) 
    498550            except gobject.GError: 
    499                 # this exception is raised when user chooses a non-image 
    500                 # file or a directory 
    501                 pass 
     551                # this exception is raised when user chooses a 
     552                # non-image file or a directory 
     553                warnings.warn('File %s is not an image' % filename) 
    502554        else: 
    503555            file_chooser.set_preview_widget_active(False) 
     
    527579 
    528580        return True 
     581 
     582class KeyEntry(object): 
     583    def __init__(self, keycode, mask): 
     584        self.keycode = keycode 
     585        self.mask = mask 
     586 
     587    def __repr__(self): 
     588        return u'KeyEntry(%d, %d)' % ( 
     589            self.keycode, self.mask) 
     590 
     591    def __eq__(self, rval): 
     592        return self.keycode == rval.keycode and \ 
     593            self.mask == rval.mask 
     594 
     595if __name__ == '__main__': 
     596    PrefsDialog().show() 
     597    gtk.main()