| 1 | ## gajim_themes_window.py |
|---|
| 2 | ## |
|---|
| 3 | ## Contributors for this file: |
|---|
| 4 | ## - Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 5 | ## - Nikos Kouremenos <kourem@gmail.com> |
|---|
| 6 | ## - Dimitur Kirov <dkirov@gmail.com> |
|---|
| 7 | ## |
|---|
| 8 | ## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 9 | ## Vincent Hanquez <tab@snarc.org> |
|---|
| 10 | ## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 11 | ## Vincent Hanquez <tab@snarc.org> |
|---|
| 12 | ## Nikos Kouremenos <nkour@jabber.org> |
|---|
| 13 | ## Dimitur Kirov <dkirov@gmail.com> |
|---|
| 14 | ## Travis Shirk <travis@pobox.com> |
|---|
| 15 | ## Norman Rasmussen <norman@rasmussen.co.za> |
|---|
| 16 | ## |
|---|
| 17 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 18 | ## it under the terms of the GNU General Public License as published |
|---|
| 19 | ## by the Free Software Foundation; version 2 only. |
|---|
| 20 | ## |
|---|
| 21 | ## This program is distributed in the hope that it will be useful, |
|---|
| 22 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 23 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 24 | ## GNU General Public License for more details. |
|---|
| 25 | ## |
|---|
| 26 | |
|---|
| 27 | import gtk |
|---|
| 28 | import gtk.glade |
|---|
| 29 | import gobject |
|---|
| 30 | import pango |
|---|
| 31 | import dialogs |
|---|
| 32 | import gtkgui_helpers |
|---|
| 33 | |
|---|
| 34 | from common import gajim |
|---|
| 35 | from common import i18n |
|---|
| 36 | _ = i18n._ |
|---|
| 37 | APP = i18n.APP |
|---|
| 38 | gtk.glade.bindtextdomain (APP, i18n.DIR) |
|---|
| 39 | gtk.glade.textdomain (APP) |
|---|
| 40 | |
|---|
| 41 | GTKGUI_GLADE = 'gtkgui.glade' |
|---|
| 42 | |
|---|
| 43 | class GajimThemesWindow: |
|---|
| 44 | |
|---|
| 45 | def __init__(self): |
|---|
| 46 | self.xml = gtk.glade.XML(GTKGUI_GLADE, 'gajim_themes_window', APP) |
|---|
| 47 | self.window = self.xml.get_widget('gajim_themes_window') |
|---|
| 48 | |
|---|
| 49 | self.options = ['account', 'group', 'contact', 'banner'] |
|---|
| 50 | self.options_combobox = self.xml.get_widget('options_combobox') |
|---|
| 51 | self.textcolor_checkbutton = self.xml.get_widget('textcolor_checkbutton') |
|---|
| 52 | self.background_checkbutton = self.xml.get_widget('background_checkbutton') |
|---|
| 53 | self.textfont_checkbutton = self.xml.get_widget('textfont_checkbutton') |
|---|
| 54 | self.text_colorbutton = self.xml.get_widget('text_colorbutton') |
|---|
| 55 | self.background_colorbutton = self.xml.get_widget('background_colorbutton') |
|---|
| 56 | self.text_fontbutton = self.xml.get_widget('text_fontbutton') |
|---|
| 57 | self.bold_togglebutton = self.xml.get_widget('bold_togglebutton') |
|---|
| 58 | self.italic_togglebutton = self.xml.get_widget('italic_togglebutton') |
|---|
| 59 | self.themes_tree = self.xml.get_widget('themes_treeview') |
|---|
| 60 | self.theme_options_vbox = self.xml.get_widget('theme_options_vbox') |
|---|
| 61 | model = gtk.ListStore(str) |
|---|
| 62 | self.themes_tree.set_model(model) |
|---|
| 63 | col = gtk.TreeViewColumn(_('Theme')) |
|---|
| 64 | self.themes_tree.append_column(col) |
|---|
| 65 | renderer = gtk.CellRendererText() |
|---|
| 66 | col.pack_start(renderer, True) |
|---|
| 67 | col.set_attributes(renderer, text = 0) |
|---|
| 68 | renderer.connect('edited', self.on_theme_cell_edited) |
|---|
| 69 | renderer.set_property('editable', True) |
|---|
| 70 | self.current_theme = gajim.config.get('roster_theme') |
|---|
| 71 | self.no_update = False |
|---|
| 72 | self.fill_themes_treeview() |
|---|
| 73 | self.current_option = self.options[0] |
|---|
| 74 | self.set_theme_options(self.current_theme, self.current_option) |
|---|
| 75 | |
|---|
| 76 | self.xml.signal_autoconnect(self) |
|---|
| 77 | self.themes_tree.get_selection().connect('changed', |
|---|
| 78 | self.selection_changed) |
|---|
| 79 | self.window.show_all() |
|---|
| 80 | |
|---|
| 81 | def on_close_button_clicked(self, widget): |
|---|
| 82 | self.window.destroy() |
|---|
| 83 | |
|---|
| 84 | def on_theme_cell_edited(self, cell, row, new_name): |
|---|
| 85 | model = self.themes_tree.get_model() |
|---|
| 86 | iter = model.get_iter_from_string(row) |
|---|
| 87 | old_name = model.get_value(iter, 0).decode('utf-8') |
|---|
| 88 | new_name = new_name.decode('utf-8') |
|---|
| 89 | if old_name == new_name: |
|---|
| 90 | return |
|---|
| 91 | new_config_name = new_name.replace(' ', '_') |
|---|
| 92 | if new_config_name in gajim.config.get_per('themes'): |
|---|
| 93 | return |
|---|
| 94 | gajim.config.add_per('themes', new_config_name) |
|---|
| 95 | # Copy old theme values |
|---|
| 96 | old_config_name = old_name.replace(' ', '_') |
|---|
| 97 | properties = ['textcolor', 'bgcolor', 'font', 'fontattrs'] |
|---|
| 98 | gajim.config.add_per('themes', new_config_name) |
|---|
| 99 | for option in self.options: |
|---|
| 100 | for property in properties: |
|---|
| 101 | option_name = option + property |
|---|
| 102 | gajim.config.set_per('themes', new_config_name, option_name, |
|---|
| 103 | gajim.config.get_per('themes', old_config_name, option_name)) |
|---|
| 104 | gajim.config.del_per('themes', old_config_name) |
|---|
| 105 | if old_config_name == gajim.config.get('roster_theme'): |
|---|
| 106 | gajim.config.set('roster_theme', new_config_name) |
|---|
| 107 | model.set_value(iter, 0, new_name) |
|---|
| 108 | self.current_theme = new_name |
|---|
| 109 | gajim.interface.instances['preferences'].update_preferences_window() |
|---|
| 110 | |
|---|
| 111 | def fill_themes_treeview(self): |
|---|
| 112 | self.xml.get_widget('remove_button').set_sensitive(False) |
|---|
| 113 | self.theme_options_vbox.set_sensitive(False) |
|---|
| 114 | model = self.themes_tree.get_model() |
|---|
| 115 | model.clear() |
|---|
| 116 | for config_theme in gajim.config.get_per('themes'): |
|---|
| 117 | theme = config_theme.replace('_', ' ') |
|---|
| 118 | iter = model.append([theme]) |
|---|
| 119 | if gajim.config.get('roster_theme') == config_theme: |
|---|
| 120 | self.themes_tree.get_selection().select_iter(iter) |
|---|
| 121 | self.xml.get_widget('remove_button').set_sensitive(True) |
|---|
| 122 | self.theme_options_vbox.set_sensitive(True) |
|---|
| 123 | |
|---|
| 124 | def selection_changed(self, widget = None): |
|---|
| 125 | (model, iter) = self.themes_tree.get_selection().get_selected() |
|---|
| 126 | selected = self.themes_tree.get_selection().get_selected_rows() |
|---|
| 127 | if not iter or selected[1] == []: |
|---|
| 128 | self.theme_options_vbox.set_sensitive(False) |
|---|
| 129 | return |
|---|
| 130 | self.xml.get_widget('remove_button').set_sensitive(True) |
|---|
| 131 | self.theme_options_vbox.set_sensitive(True) |
|---|
| 132 | self.current_theme = model.get_value(iter, 0).decode('utf-8') |
|---|
| 133 | self.current_theme = self.current_theme.replace(' ', '_') |
|---|
| 134 | self.set_theme_options(self.current_theme) |
|---|
| 135 | |
|---|
| 136 | def on_add_button_clicked(self, widget): |
|---|
| 137 | model = self.themes_tree.get_model() |
|---|
| 138 | iter = model.append() |
|---|
| 139 | i = 0 |
|---|
| 140 | # don't confuse translators |
|---|
| 141 | theme_name = _('theme name') |
|---|
| 142 | theme_name_ns = theme_name.replace(' ', '_') |
|---|
| 143 | while theme_name_ns + unicode(i) in gajim.config.get_per('themes'): |
|---|
| 144 | i += 1 |
|---|
| 145 | model.set_value(iter, 0, theme_name + unicode(i)) |
|---|
| 146 | gajim.config.add_per('themes', theme_name_ns + unicode(i)) |
|---|
| 147 | self.themes_tree.get_selection().select_iter(iter) |
|---|
| 148 | col = self.themes_tree.get_column(0) |
|---|
| 149 | path = model.get_path(iter) |
|---|
| 150 | self.themes_tree.set_cursor(path, col, True) |
|---|
| 151 | gajim.interface.instances['preferences'].update_preferences_window() |
|---|
| 152 | |
|---|
| 153 | def on_remove_button_clicked(self, widget): |
|---|
| 154 | (model, iter) = self.themes_tree.get_selection().get_selected() |
|---|
| 155 | if not iter: |
|---|
| 156 | return |
|---|
| 157 | if self.current_theme == gajim.config.get('roster_theme'): |
|---|
| 158 | dialogs.ErrorDialog( |
|---|
| 159 | _('You cannot delete your current theme'), |
|---|
| 160 | _('Please first choose another for your current theme.')).get_response() |
|---|
| 161 | return |
|---|
| 162 | self.theme_options_vbox.set_sensitive(False) |
|---|
| 163 | gajim.config.del_per('themes', self.current_theme) |
|---|
| 164 | model.remove(iter) |
|---|
| 165 | gajim.interface.instances['preferences'].update_preferences_window() |
|---|
| 166 | |
|---|
| 167 | def set_theme_options(self, theme, option = 'account'): |
|---|
| 168 | self.no_update = True |
|---|
| 169 | self.options_combobox.set_active(self.options.index(option)) |
|---|
| 170 | textcolor = gajim.config.get_per('themes', theme, option + 'textcolor') |
|---|
| 171 | if textcolor: |
|---|
| 172 | state = True |
|---|
| 173 | self.text_colorbutton.set_color(gtk.gdk.color_parse(textcolor)) |
|---|
| 174 | else: |
|---|
| 175 | state = False |
|---|
| 176 | self.textcolor_checkbutton.set_active(state) |
|---|
| 177 | self.text_colorbutton.set_sensitive(state) |
|---|
| 178 | bgcolor = gajim.config.get_per('themes', theme, option + 'bgcolor') |
|---|
| 179 | if bgcolor: |
|---|
| 180 | state = True |
|---|
| 181 | self.background_colorbutton.set_color(gtk.gdk.color_parse( |
|---|
| 182 | bgcolor)) |
|---|
| 183 | else: |
|---|
| 184 | state = False |
|---|
| 185 | self.background_checkbutton.set_active(state) |
|---|
| 186 | self.background_colorbutton.set_sensitive(state) |
|---|
| 187 | |
|---|
| 188 | # get the font name before we set widgets and it will not be overriden |
|---|
| 189 | font_name = gajim.config.get_per('themes', theme, option + 'font') |
|---|
| 190 | font_attrs = gajim.config.get_per('themes', theme, option + 'fontattrs') |
|---|
| 191 | self._set_font_widgets(font_attrs) |
|---|
| 192 | if font_name: |
|---|
| 193 | state = True |
|---|
| 194 | self.text_fontbutton.set_font_name(font_name) |
|---|
| 195 | else: |
|---|
| 196 | state = False |
|---|
| 197 | self.textfont_checkbutton.set_active(state) |
|---|
| 198 | self.text_fontbutton.set_sensitive(state) |
|---|
| 199 | self.no_update = False |
|---|
| 200 | gajim.interface.roster.change_roster_style(None) |
|---|
| 201 | |
|---|
| 202 | def on_textcolor_checkbutton_toggled(self, widget): |
|---|
| 203 | state = widget.get_active() |
|---|
| 204 | self.text_colorbutton.set_sensitive(state) |
|---|
| 205 | self._set_color(state, self.text_colorbutton, |
|---|
| 206 | 'textcolor') |
|---|
| 207 | |
|---|
| 208 | def on_background_checkbutton_toggled(self, widget): |
|---|
| 209 | state = widget.get_active() |
|---|
| 210 | self.background_colorbutton.set_sensitive(state) |
|---|
| 211 | self._set_color(state, self.background_colorbutton, |
|---|
| 212 | 'bgcolor') |
|---|
| 213 | |
|---|
| 214 | def on_textfont_checkbutton_toggled(self, widget): |
|---|
| 215 | self.text_fontbutton.set_sensitive(widget.get_active()) |
|---|
| 216 | self._set_font() |
|---|
| 217 | |
|---|
| 218 | def on_text_colorbutton_color_set(self, widget): |
|---|
| 219 | self._set_color(True, widget, 'textcolor') |
|---|
| 220 | |
|---|
| 221 | def on_background_colorbutton_color_set(self, widget): |
|---|
| 222 | self._set_color(True, widget, 'bgcolor') |
|---|
| 223 | |
|---|
| 224 | def on_text_fontbutton_font_set(self, widget): |
|---|
| 225 | self._set_font() |
|---|
| 226 | |
|---|
| 227 | def on_options_combobox_changed(self, widget): |
|---|
| 228 | index = self.options_combobox.get_active() |
|---|
| 229 | if index == -1: |
|---|
| 230 | return |
|---|
| 231 | self.current_option = self.options[index] |
|---|
| 232 | self.set_theme_options(self.current_theme, |
|---|
| 233 | self.current_option) |
|---|
| 234 | |
|---|
| 235 | def on_bold_togglebutton_toggled(self, widget): |
|---|
| 236 | if not self.no_update: |
|---|
| 237 | self._set_font() |
|---|
| 238 | |
|---|
| 239 | def on_italic_togglebutton_toggled(self, widget): |
|---|
| 240 | if not self.no_update: |
|---|
| 241 | self._set_font() |
|---|
| 242 | |
|---|
| 243 | def _set_color(self, state, widget, option): |
|---|
| 244 | ''' set color value in prefs and update the UI ''' |
|---|
| 245 | if state: |
|---|
| 246 | color = widget.get_color() |
|---|
| 247 | color_string = gtkgui_helpers.make_color_string(color) |
|---|
| 248 | else: |
|---|
| 249 | color_string = '' |
|---|
| 250 | gajim.config.set_per('themes', self.current_theme, |
|---|
| 251 | self.current_option + option, color_string) |
|---|
| 252 | # use faster functions for this |
|---|
| 253 | if self.current_option == 'banner': |
|---|
| 254 | gajim.interface.roster.repaint_themed_widgets() |
|---|
| 255 | gajim.interface.save_config() |
|---|
| 256 | return |
|---|
| 257 | if self.no_update: |
|---|
| 258 | return |
|---|
| 259 | gajim.interface.roster.change_roster_style(self.current_option) |
|---|
| 260 | gajim.interface.save_config() |
|---|
| 261 | |
|---|
| 262 | def _set_font(self): |
|---|
| 263 | ''' set font value in prefs and update the UI ''' |
|---|
| 264 | state = self.textfont_checkbutton.get_active() |
|---|
| 265 | if state: |
|---|
| 266 | font_string = self.text_fontbutton.get_font_name() |
|---|
| 267 | else: |
|---|
| 268 | font_string = '' |
|---|
| 269 | gajim.config.set_per('themes', self.current_theme, |
|---|
| 270 | self.current_option + 'font', font_string) |
|---|
| 271 | font_attrs = self._get_font_attrs() |
|---|
| 272 | gajim.config.set_per('themes', self.current_theme, |
|---|
| 273 | self.current_option + 'fontattrs', font_attrs) |
|---|
| 274 | # use faster functions for this |
|---|
| 275 | if self.current_option == 'banner': |
|---|
| 276 | gajim.interface.roster.repaint_themed_widgets() |
|---|
| 277 | if self.no_update: |
|---|
| 278 | return |
|---|
| 279 | gajim.interface.roster.change_roster_style(self.current_option) |
|---|
| 280 | gajim.interface.save_config() |
|---|
| 281 | |
|---|
| 282 | def _toggle_font_widgets(self, font_props): |
|---|
| 283 | ''' toggle font buttons with the bool values of font_props tuple''' |
|---|
| 284 | self.bold_togglebutton.set_active(font_props[0]) |
|---|
| 285 | self.italic_togglebutton.set_active(font_props[1]) |
|---|
| 286 | |
|---|
| 287 | def _get_font_description(self): |
|---|
| 288 | ''' return a FontDescription from togglebuttons |
|---|
| 289 | states''' |
|---|
| 290 | fd = pango.FontDescription() |
|---|
| 291 | if self.bold_togglebutton.get_active(): |
|---|
| 292 | fd.set_weight(pango.WEIGHT_BOLD) |
|---|
| 293 | if self.italic_togglebutton.get_active(): |
|---|
| 294 | fd.set_style(pango.STYLE_ITALIC) |
|---|
| 295 | return fd |
|---|
| 296 | |
|---|
| 297 | def _set_font_widgets(self, font_attrs): |
|---|
| 298 | ''' set the correct toggle state of font style buttons by |
|---|
| 299 | a font string of type 'BI' ''' |
|---|
| 300 | font_props = [False, False, False] |
|---|
| 301 | if font_attrs: |
|---|
| 302 | if font_attrs.find('B') != -1: |
|---|
| 303 | font_props[0] = True |
|---|
| 304 | if font_attrs.find('I') != -1: |
|---|
| 305 | font_props[1] = True |
|---|
| 306 | self._toggle_font_widgets(font_props) |
|---|
| 307 | |
|---|
| 308 | def _get_font_attrs(self): |
|---|
| 309 | ''' get a string with letters of font attribures: 'BI' ''' |
|---|
| 310 | attrs = '' |
|---|
| 311 | if self.bold_togglebutton.get_active(): |
|---|
| 312 | attrs += 'B' |
|---|
| 313 | if self.italic_togglebutton.get_active(): |
|---|
| 314 | attrs += 'I' |
|---|
| 315 | return attrs |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | def _get_font_props(self, font_name): |
|---|
| 319 | ''' get tuple of font properties: Weight, Style ''' |
|---|
| 320 | font_props = [False, False, False] |
|---|
| 321 | font_description = pango.FontDescription(font_name) |
|---|
| 322 | if font_description.get_weight() != pango.WEIGHT_NORMAL: |
|---|
| 323 | font_props[0] = True |
|---|
| 324 | if font_description.get_style() != pango.STYLE_ITALIC: |
|---|
| 325 | font_props[1] = True |
|---|
| 326 | return font_props |
|---|